blob: 7adc9526d329fb99957b091333b9f0f54ccdc2a3 [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
David Howellsa226f6c2006-01-06 00:11:08 -080056static void fastcall free_hot_cold_page(struct page *page, int cold);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * results with 256, 32 in the lowmem_reserve sysctl:
60 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
61 * 1G machine -> (16M dma, 784M normal, 224M high)
62 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
63 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
64 * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
Andi Kleena2f1b422005-11-05 17:25:53 +010065 *
66 * TBD: should special case ZONE_DMA32 machines here - in those we normally
67 * don't need any ZONE_NORMAL reservation
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
Andi Kleena2f1b422005-11-05 17:25:53 +010069int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 256, 32 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71EXPORT_SYMBOL(totalram_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Used by page_zone() to look up the address of the struct zone whose
75 * id is encoded in the upper bits of page->flags
76 */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070077struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078EXPORT_SYMBOL(zone_table);
79
Andi Kleena2f1b422005-11-05 17:25:53 +010080static char *zone_names[MAX_NR_ZONES] = { "DMA", "DMA32", "Normal", "HighMem" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int min_free_kbytes = 1024;
82
83unsigned long __initdata nr_kernel_pages;
84unsigned long __initdata nr_all_pages;
85
Nick Piggin13e74442006-01-06 00:10:58 -080086#ifdef CONFIG_DEBUG_VM
Dave Hansenc6a57e12005-10-29 18:16:52 -070087static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Dave Hansenbdc8cb92005-10-29 18:16:53 -070089 int ret = 0;
90 unsigned seq;
91 unsigned long pfn = page_to_pfn(page);
Dave Hansenc6a57e12005-10-29 18:16:52 -070092
Dave Hansenbdc8cb92005-10-29 18:16:53 -070093 do {
94 seq = zone_span_seqbegin(zone);
95 if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
96 ret = 1;
97 else if (pfn < zone->zone_start_pfn)
98 ret = 1;
99 } while (zone_span_seqretry(zone, seq));
100
101 return ret;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700102}
103
104static int page_is_consistent(struct zone *zone, struct page *page)
105{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#ifdef CONFIG_HOLES_IN_ZONE
107 if (!pfn_valid(page_to_pfn(page)))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110 if (zone != page_zone(page))
Dave Hansenc6a57e12005-10-29 18:16:52 -0700111 return 0;
112
113 return 1;
114}
115/*
116 * Temporary debugging check for pages not lying within a given zone.
117 */
118static int bad_range(struct zone *zone, struct page *page)
119{
120 if (page_outside_zone_boundaries(zone, page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 1;
Dave Hansenc6a57e12005-10-29 18:16:52 -0700122 if (!page_is_consistent(zone, page))
123 return 1;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Nick Piggin13e74442006-01-06 00:10:58 -0800128#else
129static inline int bad_range(struct zone *zone, struct page *page)
130{
131 return 0;
132}
133#endif
134
Nick Piggin224abf92006-01-06 00:11:11 -0800135static void bad_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Nick Piggin224abf92006-01-06 00:11:11 -0800137 printk(KERN_EMERG "Bad page state in process '%s'\n"
138 "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
139 "Trying to fix it up, but a reboot is needed\n"
140 "Backtrace:\n",
141 current->comm, page, (int)(2*sizeof(unsigned long)),
142 (unsigned long)page->flags, page->mapping,
143 page_mapcount(page), page_count(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dump_stack();
Hugh Dickins334795e2005-06-21 17:15:08 -0700145 page->flags &= ~(1 << PG_lru |
146 1 << PG_private |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 1 << PG_active |
149 1 << PG_dirty |
Hugh Dickins334795e2005-06-21 17:15:08 -0700150 1 << PG_reclaim |
151 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 1 << PG_swapcache |
Hugh Dickins689bceb2005-11-21 21:32:20 -0800153 1 << PG_writeback );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 set_page_count(page, 0);
155 reset_page_mapcount(page);
156 page->mapping = NULL;
Randy Dunlap9f158332005-09-13 01:25:16 -0700157 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Higher-order pages are called "compound pages". They are structured thusly:
162 *
163 * The first PAGE_SIZE page is called the "head page".
164 *
165 * The remaining PAGE_SIZE pages are called "tail pages".
166 *
167 * All pages have PG_compound set. All pages have their ->private pointing at
168 * the head page (even the head page has this).
169 *
170 * The first tail page's ->mapping, if non-zero, holds the address of the
171 * compound page's put_page() function.
172 *
173 * The order of the allocation is stored in the first tail page's ->index
174 * This is only for debug at present. This usage means that zero-order pages
175 * may not be compound.
176 */
177static void prep_compound_page(struct page *page, unsigned long order)
178{
179 int i;
180 int nr_pages = 1 << order;
181
182 page[1].mapping = NULL;
183 page[1].index = order;
184 for (i = 0; i < nr_pages; i++) {
185 struct page *p = page + i;
186
187 SetPageCompound(p);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700188 set_page_private(p, (unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190}
191
192static void destroy_compound_page(struct page *page, unsigned long order)
193{
194 int i;
195 int nr_pages = 1 << order;
196
Nick Piggin224abf92006-01-06 00:11:11 -0800197 if (unlikely(page[1].index != order))
198 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 for (i = 0; i < nr_pages; i++) {
201 struct page *p = page + i;
202
Nick Piggin224abf92006-01-06 00:11:11 -0800203 if (unlikely(!PageCompound(p) |
204 (page_private(p) != (unsigned long)page)))
205 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ClearPageCompound(p);
207 }
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
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
Nick Piggin13e74442006-01-06 00:10:58 -0800264 * (a) the buddy is not in a hole &&
265 * (b) the buddy is free &&
266 * (c) the buddy is on the buddy system &&
267 * (d) a page and its buddy have the same order.
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700268 * for recording page's order, we use page_private(page) and PG_private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
270 */
271static inline int page_is_buddy(struct page *page, int order)
272{
Nick Piggin13e74442006-01-06 00:10:58 -0800273#ifdef CONFIG_HOLES_IN_ZONE
274 if (!pfn_valid(page_to_pfn(page)))
275 return 0;
276#endif
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (PagePrivate(page) &&
279 (page_order(page) == order) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 page_count(page) == 0)
281 return 1;
282 return 0;
283}
284
285/*
286 * Freeing function for a buddy system allocator.
287 *
288 * The concept of a buddy system is to maintain direct-mapped table
289 * (containing bit values) for memory blocks of various "orders".
290 * The bottom level table contains the map for the smallest allocatable
291 * units of memory (here, pages), and each level above it describes
292 * pairs of units from the levels below, hence, "buddies".
293 * At a high level, all that happens here is marking the table entry
294 * at the bottom level available, and propagating the changes upward
295 * as necessary, plus some accounting needed to play nicely with other
296 * parts of the VM system.
297 * At each level, we keep a list of pages, which are heads of continuous
298 * free pages of length of (1 << order) and marked with PG_Private.Page's
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700299 * order is recorded in page_private(page) field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * So when we are allocating or freeing one, we can derive the state of the
301 * other. That is, if we allocate a small block, and both were
302 * free, the remainder of the region must be split into blocks.
303 * If a block is freed, and its buddy is also free, then this
304 * triggers coalescing into a block of larger size.
305 *
306 * -- wli
307 */
308
309static inline void __free_pages_bulk (struct page *page,
310 struct zone *zone, unsigned int order)
311{
312 unsigned long page_idx;
313 int order_size = 1 << order;
314
Nick Piggin224abf92006-01-06 00:11:11 -0800315 if (unlikely(PageCompound(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 destroy_compound_page(page, order);
317
318 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
319
320 BUG_ON(page_idx & (order_size - 1));
321 BUG_ON(bad_range(zone, page));
322
323 zone->free_pages += order_size;
324 while (order < MAX_ORDER-1) {
325 unsigned long combined_idx;
326 struct free_area *area;
327 struct page *buddy;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 buddy = __page_find_buddy(page, page_idx, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!page_is_buddy(buddy, order))
331 break; /* Move the buddy up one level. */
Nick Piggin13e74442006-01-06 00:10:58 -0800332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 list_del(&buddy->lru);
334 area = zone->free_area + order;
335 area->nr_free--;
336 rmv_page_order(buddy);
Nick Piggin13e74442006-01-06 00:10:58 -0800337 combined_idx = __find_combined_index(page_idx, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 page = page + (combined_idx - page_idx);
339 page_idx = combined_idx;
340 order++;
341 }
342 set_page_order(page, order);
343 list_add(&page->lru, &zone->free_area[order].free_list);
344 zone->free_area[order].nr_free++;
345}
346
Nick Piggin224abf92006-01-06 00:11:11 -0800347static inline int free_pages_check(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Nick Piggin92be2e32006-01-06 00:10:57 -0800349 if (unlikely(page_mapcount(page) |
350 (page->mapping != NULL) |
351 (page_count(page) != 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 (page->flags & (
353 1 << PG_lru |
354 1 << PG_private |
355 1 << PG_locked |
356 1 << PG_active |
357 1 << PG_reclaim |
358 1 << PG_slab |
359 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700360 1 << PG_writeback |
Nick Piggin92be2e32006-01-06 00:10:57 -0800361 1 << PG_reserved ))))
Nick Piggin224abf92006-01-06 00:11:11 -0800362 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (PageDirty(page))
Nick Piggin242e5462005-09-03 15:54:50 -0700364 __ClearPageDirty(page);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800365 /*
366 * For now, we report if PG_reserved was found set, but do not
367 * clear it, and do not free the page. But we shall soon need
368 * to do more, for when the ZERO_PAGE count wraps negative.
369 */
370 return PageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/*
374 * Frees a list of pages.
375 * Assumes all pages on list are in same zone, and of same order.
Renaud Lienhart207f36e2005-09-10 00:26:59 -0700376 * count is the number of pages to free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * If the zone was previously in an "all pages pinned" state then look to
379 * see if this freeing clears that state.
380 *
381 * And clear the zone's pages_scanned counter, to hold off the "all pages are
382 * pinned" detection logic.
383 */
384static int
385free_pages_bulk(struct zone *zone, int count,
386 struct list_head *list, unsigned int order)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct page *page = NULL;
389 int ret = 0;
390
Nick Pigginc54ad302006-01-06 00:10:56 -0800391 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 zone->all_unreclaimable = 0;
393 zone->pages_scanned = 0;
394 while (!list_empty(list) && count--) {
395 page = list_entry(list->prev, struct page, lru);
396 /* have to delete it as __free_pages_bulk list manipulates */
397 list_del(&page->lru);
398 __free_pages_bulk(page, zone, order);
399 ret++;
400 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800401 spin_unlock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return ret;
403}
404
405void __free_pages_ok(struct page *page, unsigned int order)
406{
Nick Pigginc54ad302006-01-06 00:10:56 -0800407 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 LIST_HEAD(list);
409 int i;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800410 int reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 arch_free_page(page, order);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifndef CONFIG_MMU
415 if (order > 0)
416 for (i = 1 ; i < (1 << order) ; ++i)
417 __put_page(page + i);
418#endif
419
420 for (i = 0 ; i < (1 << order) ; ++i)
Nick Piggin224abf92006-01-06 00:11:11 -0800421 reserved += free_pages_check(page + i);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800422 if (reserved)
423 return;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 list_add(&page->lru, &list);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800426 mod_page_state(pgfree, 1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kernel_map_pages(page, 1<<order, 0);
Nick Pigginc54ad302006-01-06 00:10:56 -0800428 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 free_pages_bulk(page_zone(page), 1, &list, order);
Nick Pigginc54ad302006-01-06 00:10:56 -0800430 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
David Howellsa226f6c2006-01-06 00:11:08 -0800433/*
434 * permit the bootmem allocator to evade page validation on high-order frees
435 */
436void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
437{
438 if (order == 0) {
439 __ClearPageReserved(page);
440 set_page_count(page, 0);
441
442 free_hot_cold_page(page, 0);
443 } else {
444 LIST_HEAD(list);
445 int loop;
446
447 for (loop = 0; loop < BITS_PER_LONG; loop++) {
448 struct page *p = &page[loop];
449
450 if (loop + 16 < BITS_PER_LONG)
451 prefetchw(p + 16);
452 __ClearPageReserved(p);
453 set_page_count(p, 0);
454 }
455
456 arch_free_page(page, order);
457
458 mod_page_state(pgfree, 1 << order);
459
460 list_add(&page->lru, &list);
461 kernel_map_pages(page, 1 << order, 0);
462 free_pages_bulk(page_zone(page), 1, &list, order);
463 }
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467/*
468 * The order of subdivision here is critical for the IO subsystem.
469 * Please do not alter this order without good reasons and regression
470 * testing. Specifically, as large blocks of memory are subdivided,
471 * the order in which smaller blocks are delivered depends on the order
472 * they're subdivided in this function. This is the primary factor
473 * influencing the order in which pages are delivered to the IO
474 * subsystem according to empirical testing, and this is also justified
475 * by considering the behavior of a buddy system containing a single
476 * large block of memory acted on by a series of small allocations.
477 * This behavior is a critical factor in sglist merging's success.
478 *
479 * -- wli
480 */
Nick Piggin085cc7d2006-01-06 00:11:01 -0800481static inline void expand(struct zone *zone, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int low, int high, struct free_area *area)
483{
484 unsigned long size = 1 << high;
485
486 while (high > low) {
487 area--;
488 high--;
489 size >>= 1;
490 BUG_ON(bad_range(zone, &page[size]));
491 list_add(&page[size].lru, &area->free_list);
492 area->nr_free++;
493 set_page_order(&page[size], high);
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * This page is about to be returned from the page allocator
499 */
Hugh Dickins689bceb2005-11-21 21:32:20 -0800500static int prep_new_page(struct page *page, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Nick Piggin92be2e32006-01-06 00:10:57 -0800502 if (unlikely(page_mapcount(page) |
503 (page->mapping != NULL) |
504 (page_count(page) != 0) |
Hugh Dickins334795e2005-06-21 17:15:08 -0700505 (page->flags & (
506 1 << PG_lru |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 1 << PG_private |
508 1 << PG_locked |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 1 << PG_active |
510 1 << PG_dirty |
511 1 << PG_reclaim |
Hugh Dickins334795e2005-06-21 17:15:08 -0700512 1 << PG_slab |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 1 << PG_swapcache |
Nick Pigginb5810032005-10-29 18:16:12 -0700514 1 << PG_writeback |
Nick Piggin92be2e32006-01-06 00:10:57 -0800515 1 << PG_reserved ))))
Nick Piggin224abf92006-01-06 00:11:11 -0800516 bad_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Hugh Dickins689bceb2005-11-21 21:32:20 -0800518 /*
519 * For now, we report if PG_reserved was found set, but do not
520 * clear it, and do not allocate the page: as a safety net.
521 */
522 if (PageReserved(page))
523 return 1;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
526 1 << PG_referenced | 1 << PG_arch_1 |
527 1 << PG_checked | 1 << PG_mappedtodisk);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700528 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 set_page_refs(page, order);
530 kernel_map_pages(page, 1 << order, 1);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * Do the hard work of removing an element from the buddy allocator.
536 * Call me with the zone->lock already held.
537 */
538static struct page *__rmqueue(struct zone *zone, unsigned int order)
539{
540 struct free_area * area;
541 unsigned int current_order;
542 struct page *page;
543
544 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
545 area = zone->free_area + current_order;
546 if (list_empty(&area->free_list))
547 continue;
548
549 page = list_entry(area->free_list.next, struct page, lru);
550 list_del(&page->lru);
551 rmv_page_order(page);
552 area->nr_free--;
553 zone->free_pages -= 1UL << order;
Nick Piggin085cc7d2006-01-06 00:11:01 -0800554 expand(zone, page, order, current_order, area);
555 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 return NULL;
559}
560
561/*
562 * Obtain a specified number of elements from the buddy allocator, all under
563 * a single hold of the lock, for efficiency. Add them to the supplied list.
564 * Returns the number of new pages which were placed at *list.
565 */
566static int rmqueue_bulk(struct zone *zone, unsigned int order,
567 unsigned long count, struct list_head *list)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Nick Pigginc54ad302006-01-06 00:10:56 -0800571 spin_lock(&zone->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 for (i = 0; i < count; ++i) {
Nick Piggin085cc7d2006-01-06 00:11:01 -0800573 struct page *page = __rmqueue(zone, order);
574 if (unlikely(page == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 list_add_tail(&page->lru, list);
577 }
Nick Pigginc54ad302006-01-06 00:10:56 -0800578 spin_unlock(&zone->lock);
Nick Piggin085cc7d2006-01-06 00:11:01 -0800579 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Christoph Lameter4ae7c032005-06-21 17:14:57 -0700582#ifdef CONFIG_NUMA
583/* Called from the slab reaper to drain remote pagesets */
584void drain_remote_pages(void)
585{
586 struct zone *zone;
587 int i;
588 unsigned long flags;
589
590 local_irq_save(flags);
591 for_each_zone(zone) {
592 struct per_cpu_pageset *pset;
593
594 /* Do not drain local pagesets */
595 if (zone->zone_pgdat->node_id == numa_node_id())
596 continue;
597
598 pset = zone->pageset[smp_processor_id()];
599 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
600 struct per_cpu_pages *pcp;
601
602 pcp = &pset->pcp[i];
603 if (pcp->count)
604 pcp->count -= free_pages_bulk(zone, pcp->count,
605 &pcp->list, 0);
606 }
607 }
608 local_irq_restore(flags);
609}
610#endif
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
613static void __drain_pages(unsigned int cpu)
614{
Nick Pigginc54ad302006-01-06 00:10:56 -0800615 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct zone *zone;
617 int i;
618
619 for_each_zone(zone) {
620 struct per_cpu_pageset *pset;
621
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700622 pset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
624 struct per_cpu_pages *pcp;
625
626 pcp = &pset->pcp[i];
Nick Pigginc54ad302006-01-06 00:10:56 -0800627 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 pcp->count -= free_pages_bulk(zone, pcp->count,
629 &pcp->list, 0);
Nick Pigginc54ad302006-01-06 00:10:56 -0800630 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 }
633}
634#endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
635
636#ifdef CONFIG_PM
637
638void mark_free_pages(struct zone *zone)
639{
640 unsigned long zone_pfn, flags;
641 int order;
642 struct list_head *curr;
643
644 if (!zone->spanned_pages)
645 return;
646
647 spin_lock_irqsave(&zone->lock, flags);
648 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
649 ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
650
651 for (order = MAX_ORDER - 1; order >= 0; --order)
652 list_for_each(curr, &zone->free_area[order].free_list) {
653 unsigned long start_pfn, i;
654
655 start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
656
657 for (i=0; i < (1<<order); i++)
658 SetPageNosaveFree(pfn_to_page(start_pfn+i));
659 }
660 spin_unlock_irqrestore(&zone->lock, flags);
661}
662
663/*
664 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
665 */
666void drain_local_pages(void)
667{
668 unsigned long flags;
669
670 local_irq_save(flags);
671 __drain_pages(smp_processor_id());
672 local_irq_restore(flags);
673}
674#endif /* CONFIG_PM */
675
676static void zone_statistics(struct zonelist *zonelist, struct zone *z)
677{
678#ifdef CONFIG_NUMA
679 unsigned long flags;
680 int cpu;
681 pg_data_t *pg = z->zone_pgdat;
682 pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
683 struct per_cpu_pageset *p;
684
685 local_irq_save(flags);
686 cpu = smp_processor_id();
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700687 p = zone_pcp(z,cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (pg == orig) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700689 p->numa_hit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else {
691 p->numa_miss++;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700692 zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 if (pg == NODE_DATA(numa_node_id()))
695 p->local_node++;
696 else
697 p->other_node++;
698 local_irq_restore(flags);
699#endif
700}
701
702/*
703 * Free a 0-order page
704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static void fastcall free_hot_cold_page(struct page *page, int cold)
706{
707 struct zone *zone = page_zone(page);
708 struct per_cpu_pages *pcp;
709 unsigned long flags;
710
711 arch_free_page(page, 0);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (PageAnon(page))
714 page->mapping = NULL;
Nick Piggin224abf92006-01-06 00:11:11 -0800715 if (free_pages_check(page))
Hugh Dickins689bceb2005-11-21 21:32:20 -0800716 return;
717
718 inc_page_state(pgfree);
719 kernel_map_pages(page, 1, 0);
720
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700721 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 list_add(&page->lru, &pcp->list);
724 pcp->count++;
Christoph Lameter2caaad42005-06-21 17:15:00 -0700725 if (pcp->count >= pcp->high)
726 pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 local_irq_restore(flags);
728 put_cpu();
729}
730
731void fastcall free_hot_page(struct page *page)
732{
733 free_hot_cold_page(page, 0);
734}
735
736void fastcall free_cold_page(struct page *page)
737{
738 free_hot_cold_page(page, 1);
739}
740
Al Virodd0fc662005-10-07 07:46:04 +0100741static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 int i;
744
745 BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
746 for(i = 0; i < (1 << order); i++)
747 clear_highpage(page + i);
748}
749
750/*
751 * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
752 * we cheat by calling it from here, in the order > 0 path. Saves a branch
753 * or two.
754 */
755static struct page *
Al Virodd0fc662005-10-07 07:46:04 +0100756buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 unsigned long flags;
Hugh Dickins689bceb2005-11-21 21:32:20 -0800759 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int cold = !!(gfp_flags & __GFP_COLD);
761
Hugh Dickins689bceb2005-11-21 21:32:20 -0800762again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (order == 0) {
764 struct per_cpu_pages *pcp;
765
Hugh Dickins689bceb2005-11-21 21:32:20 -0800766 page = NULL;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700767 pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 local_irq_save(flags);
Nick Piggin2d92c5c2006-01-06 00:10:59 -0800769 if (!pcp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pcp->count += rmqueue_bulk(zone, 0,
771 pcp->batch, &pcp->list);
Nick Pigginc54ad302006-01-06 00:10:56 -0800772 if (likely(pcp->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 page = list_entry(pcp->list.next, struct page, lru);
774 list_del(&page->lru);
775 pcp->count--;
776 }
777 local_irq_restore(flags);
778 put_cpu();
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800779 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_lock_irqsave(&zone->lock, flags);
781 page = __rmqueue(zone, order);
782 spin_unlock_irqrestore(&zone->lock, flags);
783 }
784
785 if (page != NULL) {
786 BUG_ON(bad_range(zone, page));
787 mod_page_state_zone(zone, pgalloc, 1 << order);
Hugh Dickins689bceb2005-11-21 21:32:20 -0800788 if (prep_new_page(page, order))
789 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (gfp_flags & __GFP_ZERO)
792 prep_zero_page(page, order, gfp_flags);
793
794 if (order && (gfp_flags & __GFP_COMP))
795 prep_compound_page(page, order);
796 }
797 return page;
798}
799
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800800#define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
Nick Piggin31488902005-11-28 13:44:03 -0800801#define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
802#define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
803#define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
804#define ALLOC_HARDER 0x10 /* try to alloc harder */
805#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
806#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * Return 1 if free pages are above 'mark'. This takes into account the order
810 * of the allocation.
811 */
812int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800813 int classzone_idx, int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 /* free_pages my go negative - that's OK */
816 long min = mark, free_pages = z->free_pages - (1 << order) + 1;
817 int o;
818
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800819 if (alloc_flags & ALLOC_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 min -= min / 2;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800821 if (alloc_flags & ALLOC_HARDER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 min -= min / 4;
823
824 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
825 return 0;
826 for (o = 0; o < order; o++) {
827 /* At the next order, this order's pages become unavailable */
828 free_pages -= z->free_area[o].nr_free << o;
829
830 /* Require fewer higher order pages to be free */
831 min >>= 1;
832
833 if (free_pages <= min)
834 return 0;
835 }
836 return 1;
837}
838
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800839/*
840 * get_page_from_freeliest goes through the zonelist trying to allocate
841 * a page.
842 */
843static struct page *
844get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
845 struct zonelist *zonelist, int alloc_flags)
Martin Hicks753ee722005-06-21 17:14:41 -0700846{
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800847 struct zone **z = zonelist->zones;
848 struct page *page = NULL;
849 int classzone_idx = zone_idx(*z);
850
851 /*
852 * Go through the zonelist once, looking for a zone with enough free.
853 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
854 */
855 do {
856 if ((alloc_flags & ALLOC_CPUSET) &&
857 !cpuset_zone_allowed(*z, gfp_mask))
858 continue;
859
860 if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
Nick Piggin31488902005-11-28 13:44:03 -0800861 unsigned long mark;
862 if (alloc_flags & ALLOC_WMARK_MIN)
863 mark = (*z)->pages_min;
864 else if (alloc_flags & ALLOC_WMARK_LOW)
865 mark = (*z)->pages_low;
866 else
867 mark = (*z)->pages_high;
868 if (!zone_watermark_ok(*z, order, mark,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800869 classzone_idx, alloc_flags))
870 continue;
871 }
872
873 page = buffered_rmqueue(*z, order, gfp_mask);
874 if (page) {
875 zone_statistics(zonelist, *z);
876 break;
877 }
878 } while (*(++z) != NULL);
879 return page;
Martin Hicks753ee722005-06-21 17:14:41 -0700880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/*
883 * This is the 'heart' of the zoned buddy allocator.
884 */
885struct page * fastcall
Al Virodd0fc662005-10-07 07:46:04 +0100886__alloc_pages(gfp_t gfp_mask, unsigned int order,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct zonelist *zonelist)
888{
Al Viro260b2362005-10-21 03:22:44 -0400889 const gfp_t wait = gfp_mask & __GFP_WAIT;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800890 struct zone **z;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct page *page;
892 struct reclaim_state reclaim_state;
893 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int do_retry;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800895 int alloc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int did_some_progress;
897
898 might_sleep_if(wait);
899
Jens Axboe6b1de912005-11-17 21:35:02 +0100900restart:
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800901 z = zonelist->zones; /* the list of zones suitable for gfp_mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800903 if (unlikely(*z == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Should this ever happen?? */
905 return NULL;
906 }
Jens Axboe6b1de912005-11-17 21:35:02 +0100907
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800908 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -0800909 zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800910 if (page)
911 goto got_pg;
912
Jens Axboe6b1de912005-11-17 21:35:02 +0100913 do {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800914 wakeup_kswapd(*z, order);
Jens Axboe6b1de912005-11-17 21:35:02 +0100915 } while (*(++z));
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800916
Paul Jackson9bf22292005-09-06 15:18:12 -0700917 /*
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800918 * OK, we're below the kswapd watermark and have kicked background
919 * reclaim. Now things get more complex, so set up alloc_flags according
920 * to how we want to proceed.
921 *
922 * The caller may dip into page reserves a bit more if the caller
923 * cannot run direct reclaim, or if the caller has realtime scheduling
924 * policy.
Paul Jackson9bf22292005-09-06 15:18:12 -0700925 */
Nick Piggin31488902005-11-28 13:44:03 -0800926 alloc_flags = ALLOC_WMARK_MIN;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800927 if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
928 alloc_flags |= ALLOC_HARDER;
929 if (gfp_mask & __GFP_HIGH)
930 alloc_flags |= ALLOC_HIGH;
Paul Jackson47f3a862006-01-06 00:10:32 -0800931 alloc_flags |= ALLOC_CPUSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /*
934 * Go through the zonelist again. Let __GFP_HIGH and allocations
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800935 * coming from realtime tasks go deeper into reserves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
937 * This is the last chance, in general, before the goto nopage.
938 * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
Paul Jackson9bf22292005-09-06 15:18:12 -0700939 * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800941 page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
942 if (page)
943 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* This allocation should allow future memory freeing. */
Nick Pigginb84a35b2005-05-01 08:58:36 -0700946
947 if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
948 && !in_interrupt()) {
949 if (!(gfp_mask & __GFP_NOMEMALLOC)) {
Kirill Korotaev885036d2005-11-13 16:06:41 -0800950nofail_alloc:
Nick Pigginb84a35b2005-05-01 08:58:36 -0700951 /* go through the zonelist yet again, ignoring mins */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800952 page = get_page_from_freelist(gfp_mask, order,
Paul Jackson47f3a862006-01-06 00:10:32 -0800953 zonelist, ALLOC_NO_WATERMARKS);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800954 if (page)
955 goto got_pg;
Kirill Korotaev885036d2005-11-13 16:06:41 -0800956 if (gfp_mask & __GFP_NOFAIL) {
957 blk_congestion_wait(WRITE, HZ/50);
958 goto nofail_alloc;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 goto nopage;
962 }
963
964 /* Atomic allocations - we can't balance anything */
965 if (!wait)
966 goto nopage;
967
968rebalance:
969 cond_resched();
970
971 /* We now go into synchronous reclaim */
972 p->flags |= PF_MEMALLOC;
973 reclaim_state.reclaimed_slab = 0;
974 p->reclaim_state = &reclaim_state;
975
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800976 did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 p->reclaim_state = NULL;
979 p->flags &= ~PF_MEMALLOC;
980
981 cond_resched();
982
983 if (likely(did_some_progress)) {
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800984 page = get_page_from_freelist(gfp_mask, order,
985 zonelist, alloc_flags);
986 if (page)
987 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
989 /*
990 * Go through the zonelist yet one more time, keep
991 * very high watermark here, this is only to catch
992 * a parallel oom killing, we must fail if we're still
993 * under heavy pressure.
994 */
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800995 page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
Nick Piggin31488902005-11-28 13:44:03 -0800996 zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
Rohit Seth7fb1d9f2005-11-13 16:06:43 -0800997 if (page)
998 goto got_pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Marcelo Tosatti79b9ce32005-07-07 17:56:04 -07001000 out_of_memory(gfp_mask, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto restart;
1002 }
1003
1004 /*
1005 * Don't let big-order allocations loop unless the caller explicitly
1006 * requests that. Wait for some write requests to complete then retry.
1007 *
1008 * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
1009 * <= 3, but that may not be true in other implementations.
1010 */
1011 do_retry = 0;
1012 if (!(gfp_mask & __GFP_NORETRY)) {
1013 if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
1014 do_retry = 1;
1015 if (gfp_mask & __GFP_NOFAIL)
1016 do_retry = 1;
1017 }
1018 if (do_retry) {
1019 blk_congestion_wait(WRITE, HZ/50);
1020 goto rebalance;
1021 }
1022
1023nopage:
1024 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
1025 printk(KERN_WARNING "%s: page allocation failure."
1026 " order:%d, mode:0x%x\n",
1027 p->comm, order, gfp_mask);
1028 dump_stack();
Janet Morgan578c2fd2005-06-21 17:14:56 -07001029 show_mem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031got_pg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return page;
1033}
1034
1035EXPORT_SYMBOL(__alloc_pages);
1036
1037/*
1038 * Common helper functions.
1039 */
Al Virodd0fc662005-10-07 07:46:04 +01001040fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct page * page;
1043 page = alloc_pages(gfp_mask, order);
1044 if (!page)
1045 return 0;
1046 return (unsigned long) page_address(page);
1047}
1048
1049EXPORT_SYMBOL(__get_free_pages);
1050
Al Virodd0fc662005-10-07 07:46:04 +01001051fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct page * page;
1054
1055 /*
1056 * get_zeroed_page() returns a 32-bit address, which cannot represent
1057 * a highmem page
1058 */
Al Viro260b2362005-10-21 03:22:44 -04001059 BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
1062 if (page)
1063 return (unsigned long) page_address(page);
1064 return 0;
1065}
1066
1067EXPORT_SYMBOL(get_zeroed_page);
1068
1069void __pagevec_free(struct pagevec *pvec)
1070{
1071 int i = pagevec_count(pvec);
1072
1073 while (--i >= 0)
1074 free_hot_cold_page(pvec->pages[i], pvec->cold);
1075}
1076
1077fastcall void __free_pages(struct page *page, unsigned int order)
1078{
Nick Pigginb5810032005-10-29 18:16:12 -07001079 if (put_page_testzero(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (order == 0)
1081 free_hot_page(page);
1082 else
1083 __free_pages_ok(page, order);
1084 }
1085}
1086
1087EXPORT_SYMBOL(__free_pages);
1088
1089fastcall void free_pages(unsigned long addr, unsigned int order)
1090{
1091 if (addr != 0) {
1092 BUG_ON(!virt_addr_valid((void *)addr));
1093 __free_pages(virt_to_page((void *)addr), order);
1094 }
1095}
1096
1097EXPORT_SYMBOL(free_pages);
1098
1099/*
1100 * Total amount of free (allocatable) RAM:
1101 */
1102unsigned int nr_free_pages(void)
1103{
1104 unsigned int sum = 0;
1105 struct zone *zone;
1106
1107 for_each_zone(zone)
1108 sum += zone->free_pages;
1109
1110 return sum;
1111}
1112
1113EXPORT_SYMBOL(nr_free_pages);
1114
1115#ifdef CONFIG_NUMA
1116unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
1117{
1118 unsigned int i, sum = 0;
1119
1120 for (i = 0; i < MAX_NR_ZONES; i++)
1121 sum += pgdat->node_zones[i].free_pages;
1122
1123 return sum;
1124}
1125#endif
1126
1127static unsigned int nr_free_zone_pages(int offset)
1128{
Martin J. Blighe310fd42005-07-29 22:59:18 -07001129 /* Just pick one node, since fallback list is circular */
1130 pg_data_t *pgdat = NODE_DATA(numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 unsigned int sum = 0;
1132
Martin J. Blighe310fd42005-07-29 22:59:18 -07001133 struct zonelist *zonelist = pgdat->node_zonelists + offset;
1134 struct zone **zonep = zonelist->zones;
1135 struct zone *zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Martin J. Blighe310fd42005-07-29 22:59:18 -07001137 for (zone = *zonep++; zone; zone = *zonep++) {
1138 unsigned long size = zone->present_pages;
1139 unsigned long high = zone->pages_high;
1140 if (size > high)
1141 sum += size - high;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 return sum;
1145}
1146
1147/*
1148 * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
1149 */
1150unsigned int nr_free_buffer_pages(void)
1151{
Al Viroaf4ca452005-10-21 02:55:38 -04001152 return nr_free_zone_pages(gfp_zone(GFP_USER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155/*
1156 * Amount of free RAM allocatable within all zones
1157 */
1158unsigned int nr_free_pagecache_pages(void)
1159{
Al Viroaf4ca452005-10-21 02:55:38 -04001160 return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163#ifdef CONFIG_HIGHMEM
1164unsigned int nr_free_highpages (void)
1165{
1166 pg_data_t *pgdat;
1167 unsigned int pages = 0;
1168
1169 for_each_pgdat(pgdat)
1170 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1171
1172 return pages;
1173}
1174#endif
1175
1176#ifdef CONFIG_NUMA
1177static void show_node(struct zone *zone)
1178{
1179 printk("Node %d ", zone->zone_pgdat->node_id);
1180}
1181#else
1182#define show_node(zone) do { } while (0)
1183#endif
1184
1185/*
1186 * Accumulate the page_state information across all CPUs.
1187 * The result is unavoidably approximate - it can change
1188 * during and after execution of this function.
1189 */
1190static DEFINE_PER_CPU(struct page_state, page_states) = {0};
1191
1192atomic_t nr_pagecache = ATOMIC_INIT(0);
1193EXPORT_SYMBOL(nr_pagecache);
1194#ifdef CONFIG_SMP
1195DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
1196#endif
1197
Nick Piggina86b1f52006-01-06 00:11:00 -08001198static void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 int cpu = 0;
1201
1202 memset(ret, 0, sizeof(*ret));
1203
Martin Hicksc07e02d2005-09-03 15:55:11 -07001204 cpu = first_cpu(*cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 while (cpu < NR_CPUS) {
1206 unsigned long *in, *out, off;
1207
1208 in = (unsigned long *)&per_cpu(page_states, cpu);
1209
Martin Hicksc07e02d2005-09-03 15:55:11 -07001210 cpu = next_cpu(cpu, *cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (cpu < NR_CPUS)
1213 prefetch(&per_cpu(page_states, cpu));
1214
1215 out = (unsigned long *)ret;
1216 for (off = 0; off < nr; off++)
1217 *out++ += *in++;
1218 }
1219}
1220
Martin Hicksc07e02d2005-09-03 15:55:11 -07001221void get_page_state_node(struct page_state *ret, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 int nr;
Martin Hicksc07e02d2005-09-03 15:55:11 -07001224 cpumask_t mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1227 nr /= sizeof(unsigned long);
1228
Martin Hicksc07e02d2005-09-03 15:55:11 -07001229 __get_page_state(ret, nr+1, &mask);
1230}
1231
1232void get_page_state(struct page_state *ret)
1233{
1234 int nr;
1235 cpumask_t mask = CPU_MASK_ALL;
1236
1237 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
1238 nr /= sizeof(unsigned long);
1239
1240 __get_page_state(ret, nr + 1, &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243void get_full_page_state(struct page_state *ret)
1244{
Martin Hicksc07e02d2005-09-03 15:55:11 -07001245 cpumask_t mask = CPU_MASK_ALL;
1246
1247 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Benjamin LaHaisec2f29ea2005-06-21 17:14:55 -07001250unsigned long __read_page_state(unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 unsigned long ret = 0;
1253 int cpu;
1254
Nick Piggina86b1f52006-01-06 00:11:00 -08001255 for_each_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 unsigned long in;
1257
1258 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
1259 ret += *((unsigned long *)in);
1260 }
1261 return ret;
1262}
1263
Benjamin LaHaise83e5d8f2005-06-21 17:14:54 -07001264void __mod_page_state(unsigned long offset, unsigned long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 unsigned long flags;
1267 void* ptr;
1268
1269 local_irq_save(flags);
1270 ptr = &__get_cpu_var(page_states);
1271 *(unsigned long*)(ptr + offset) += delta;
1272 local_irq_restore(flags);
1273}
1274
1275EXPORT_SYMBOL(__mod_page_state);
1276
1277void __get_zone_counts(unsigned long *active, unsigned long *inactive,
1278 unsigned long *free, struct pglist_data *pgdat)
1279{
1280 struct zone *zones = pgdat->node_zones;
1281 int i;
1282
1283 *active = 0;
1284 *inactive = 0;
1285 *free = 0;
1286 for (i = 0; i < MAX_NR_ZONES; i++) {
1287 *active += zones[i].nr_active;
1288 *inactive += zones[i].nr_inactive;
1289 *free += zones[i].free_pages;
1290 }
1291}
1292
1293void get_zone_counts(unsigned long *active,
1294 unsigned long *inactive, unsigned long *free)
1295{
1296 struct pglist_data *pgdat;
1297
1298 *active = 0;
1299 *inactive = 0;
1300 *free = 0;
1301 for_each_pgdat(pgdat) {
1302 unsigned long l, m, n;
1303 __get_zone_counts(&l, &m, &n, pgdat);
1304 *active += l;
1305 *inactive += m;
1306 *free += n;
1307 }
1308}
1309
1310void si_meminfo(struct sysinfo *val)
1311{
1312 val->totalram = totalram_pages;
1313 val->sharedram = 0;
1314 val->freeram = nr_free_pages();
1315 val->bufferram = nr_blockdev_pages();
1316#ifdef CONFIG_HIGHMEM
1317 val->totalhigh = totalhigh_pages;
1318 val->freehigh = nr_free_highpages();
1319#else
1320 val->totalhigh = 0;
1321 val->freehigh = 0;
1322#endif
1323 val->mem_unit = PAGE_SIZE;
1324}
1325
1326EXPORT_SYMBOL(si_meminfo);
1327
1328#ifdef CONFIG_NUMA
1329void si_meminfo_node(struct sysinfo *val, int nid)
1330{
1331 pg_data_t *pgdat = NODE_DATA(nid);
1332
1333 val->totalram = pgdat->node_present_pages;
1334 val->freeram = nr_free_pages_pgdat(pgdat);
1335 val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
1336 val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
1337 val->mem_unit = PAGE_SIZE;
1338}
1339#endif
1340
1341#define K(x) ((x) << (PAGE_SHIFT-10))
1342
1343/*
1344 * Show free area list (used inside shift_scroll-lock stuff)
1345 * We also calculate the percentage fragmentation. We do this by counting the
1346 * memory on each free list with the exception of the first item on the list.
1347 */
1348void show_free_areas(void)
1349{
1350 struct page_state ps;
1351 int cpu, temperature;
1352 unsigned long active;
1353 unsigned long inactive;
1354 unsigned long free;
1355 struct zone *zone;
1356
1357 for_each_zone(zone) {
1358 show_node(zone);
1359 printk("%s per-cpu:", zone->name);
1360
Con Kolivasf3fe6512006-01-06 00:11:15 -08001361 if (!populated_zone(zone)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 printk(" empty\n");
1363 continue;
1364 } else
1365 printk("\n");
1366
Dave Jones6b482c62005-11-10 15:45:56 -05001367 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct per_cpu_pageset *pageset;
1369
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001370 pageset = zone_pcp(zone, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 for (temperature = 0; temperature < 2; temperature++)
Nick Piggin2d92c5c2006-01-06 00:10:59 -08001373 printk("cpu %d %s: high %d, batch %d used:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 cpu,
1375 temperature ? "cold" : "hot",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 pageset->pcp[temperature].high,
Christoph Lameter4ae7c032005-06-21 17:14:57 -07001377 pageset->pcp[temperature].batch,
1378 pageset->pcp[temperature].count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 }
1381
1382 get_page_state(&ps);
1383 get_zone_counts(&active, &inactive, &free);
1384
Denis Vlasenkoc0d62212005-06-21 17:15:14 -07001385 printk("Free pages: %11ukB (%ukB HighMem)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 K(nr_free_pages()),
1387 K(nr_free_highpages()));
1388
1389 printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
1390 "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
1391 active,
1392 inactive,
1393 ps.nr_dirty,
1394 ps.nr_writeback,
1395 ps.nr_unstable,
1396 nr_free_pages(),
1397 ps.nr_slab,
1398 ps.nr_mapped,
1399 ps.nr_page_table_pages);
1400
1401 for_each_zone(zone) {
1402 int i;
1403
1404 show_node(zone);
1405 printk("%s"
1406 " free:%lukB"
1407 " min:%lukB"
1408 " low:%lukB"
1409 " high:%lukB"
1410 " active:%lukB"
1411 " inactive:%lukB"
1412 " present:%lukB"
1413 " pages_scanned:%lu"
1414 " all_unreclaimable? %s"
1415 "\n",
1416 zone->name,
1417 K(zone->free_pages),
1418 K(zone->pages_min),
1419 K(zone->pages_low),
1420 K(zone->pages_high),
1421 K(zone->nr_active),
1422 K(zone->nr_inactive),
1423 K(zone->present_pages),
1424 zone->pages_scanned,
1425 (zone->all_unreclaimable ? "yes" : "no")
1426 );
1427 printk("lowmem_reserve[]:");
1428 for (i = 0; i < MAX_NR_ZONES; i++)
1429 printk(" %lu", zone->lowmem_reserve[i]);
1430 printk("\n");
1431 }
1432
1433 for_each_zone(zone) {
1434 unsigned long nr, flags, order, total = 0;
1435
1436 show_node(zone);
1437 printk("%s: ", zone->name);
Con Kolivasf3fe6512006-01-06 00:11:15 -08001438 if (!populated_zone(zone)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 printk("empty\n");
1440 continue;
1441 }
1442
1443 spin_lock_irqsave(&zone->lock, flags);
1444 for (order = 0; order < MAX_ORDER; order++) {
1445 nr = zone->free_area[order].nr_free;
1446 total += nr << order;
1447 printk("%lu*%lukB ", nr, K(1UL) << order);
1448 }
1449 spin_unlock_irqrestore(&zone->lock, flags);
1450 printk("= %lukB\n", K(total));
1451 }
1452
1453 show_swap_cache_info();
1454}
1455
1456/*
1457 * Builds allocation fallback zone lists.
Christoph Lameter1a932052006-01-06 00:11:16 -08001458 *
1459 * Add all populated zones of a node to the zonelist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
Christoph Lameter1a932052006-01-06 00:11:16 -08001461static int __init build_zonelists_node(pg_data_t *pgdat,
1462 struct zonelist *zonelist, int j, int k)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Christoph Lameter1a932052006-01-06 00:11:16 -08001464 struct zone *zone;
1465
1466 BUG_ON(k > ZONE_HIGHMEM);
1467 for (zone = pgdat->node_zones + k; zone >= pgdat->node_zones; zone--) {
1468 if (populated_zone(zone)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#ifndef CONFIG_HIGHMEM
Christoph Lameter1a932052006-01-06 00:11:16 -08001470 BUG_ON(zone - pgdat->node_zones > ZONE_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471#endif
1472 zonelist->zones[j++] = zone;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return j;
1476}
1477
Al Viro260b2362005-10-21 03:22:44 -04001478static inline int highest_zone(int zone_bits)
1479{
1480 int res = ZONE_NORMAL;
1481 if (zone_bits & (__force int)__GFP_HIGHMEM)
1482 res = ZONE_HIGHMEM;
Andi Kleena2f1b422005-11-05 17:25:53 +01001483 if (zone_bits & (__force int)__GFP_DMA32)
1484 res = ZONE_DMA32;
Al Viro260b2362005-10-21 03:22:44 -04001485 if (zone_bits & (__force int)__GFP_DMA)
1486 res = ZONE_DMA;
1487 return res;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490#ifdef CONFIG_NUMA
1491#define MAX_NODE_LOAD (num_online_nodes())
1492static int __initdata node_load[MAX_NUMNODES];
1493/**
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001494 * 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 -07001495 * @node: node whose fallback list we're appending
1496 * @used_node_mask: nodemask_t of already used nodes
1497 *
1498 * We use a number of factors to determine which is the next node that should
1499 * appear on a given node's fallback list. The node should not have appeared
1500 * already in @node's fallback list, and it should be the next closest node
1501 * according to the distance array (which contains arbitrary distance values
1502 * from each node to each node in the system), and should also prefer nodes
1503 * with no CPUs, since presumably they'll have very little allocation pressure
1504 * on them otherwise.
1505 * It returns -1 if no node is found.
1506 */
1507static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
1508{
1509 int i, n, val;
1510 int min_val = INT_MAX;
1511 int best_node = -1;
1512
1513 for_each_online_node(i) {
1514 cpumask_t tmp;
1515
1516 /* Start from local node */
1517 n = (node+i) % num_online_nodes();
1518
1519 /* Don't want a node to appear more than once */
1520 if (node_isset(n, *used_node_mask))
1521 continue;
1522
1523 /* Use the local node if we haven't already */
1524 if (!node_isset(node, *used_node_mask)) {
1525 best_node = node;
1526 break;
1527 }
1528
1529 /* Use the distance array to find the distance */
1530 val = node_distance(node, n);
1531
1532 /* Give preference to headless and unused nodes */
1533 tmp = node_to_cpumask(n);
1534 if (!cpus_empty(tmp))
1535 val += PENALTY_FOR_NODE_WITH_CPUS;
1536
1537 /* Slight preference for less loaded node */
1538 val *= (MAX_NODE_LOAD*MAX_NUMNODES);
1539 val += node_load[n];
1540
1541 if (val < min_val) {
1542 min_val = val;
1543 best_node = n;
1544 }
1545 }
1546
1547 if (best_node >= 0)
1548 node_set(best_node, *used_node_mask);
1549
1550 return best_node;
1551}
1552
1553static void __init build_zonelists(pg_data_t *pgdat)
1554{
1555 int i, j, k, node, local_node;
1556 int prev_node, load;
1557 struct zonelist *zonelist;
1558 nodemask_t used_mask;
1559
1560 /* initialize zonelists */
1561 for (i = 0; i < GFP_ZONETYPES; i++) {
1562 zonelist = pgdat->node_zonelists + i;
1563 zonelist->zones[0] = NULL;
1564 }
1565
1566 /* NUMA-aware ordering of nodes */
1567 local_node = pgdat->node_id;
1568 load = num_online_nodes();
1569 prev_node = local_node;
1570 nodes_clear(used_mask);
1571 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
1572 /*
1573 * We don't want to pressure a particular node.
1574 * So adding penalty to the first node in same
1575 * distance group to make it round-robin.
1576 */
1577 if (node_distance(local_node, node) !=
1578 node_distance(local_node, prev_node))
1579 node_load[node] += load;
1580 prev_node = node;
1581 load--;
1582 for (i = 0; i < GFP_ZONETYPES; i++) {
1583 zonelist = pgdat->node_zonelists + i;
1584 for (j = 0; zonelist->zones[j] != NULL; j++);
1585
Al Viro260b2362005-10-21 03:22:44 -04001586 k = highest_zone(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1589 zonelist->zones[j] = NULL;
1590 }
1591 }
1592}
1593
1594#else /* CONFIG_NUMA */
1595
1596static void __init build_zonelists(pg_data_t *pgdat)
1597{
1598 int i, j, k, node, local_node;
1599
1600 local_node = pgdat->node_id;
1601 for (i = 0; i < GFP_ZONETYPES; i++) {
1602 struct zonelist *zonelist;
1603
1604 zonelist = pgdat->node_zonelists + i;
1605
1606 j = 0;
Al Viro260b2362005-10-21 03:22:44 -04001607 k = highest_zone(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 j = build_zonelists_node(pgdat, zonelist, j, k);
1609 /*
1610 * Now we build the zonelist so that it contains the zones
1611 * of all the other nodes.
1612 * We don't want to pressure a particular node, so when
1613 * building the zones for node N, we make sure that the
1614 * zones coming right after the local ones are those from
1615 * node N+1 (modulo N)
1616 */
1617 for (node = local_node + 1; node < MAX_NUMNODES; node++) {
1618 if (!node_online(node))
1619 continue;
1620 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1621 }
1622 for (node = 0; node < local_node; node++) {
1623 if (!node_online(node))
1624 continue;
1625 j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
1626 }
1627
1628 zonelist->zones[j] = NULL;
1629 }
1630}
1631
1632#endif /* CONFIG_NUMA */
1633
1634void __init build_all_zonelists(void)
1635{
1636 int i;
1637
1638 for_each_online_node(i)
1639 build_zonelists(NODE_DATA(i));
1640 printk("Built %i zonelists\n", num_online_nodes());
1641 cpuset_init_current_mems_allowed();
1642}
1643
1644/*
1645 * Helper functions to size the waitqueue hash table.
1646 * Essentially these want to choose hash table sizes sufficiently
1647 * large so that collisions trying to wait on pages are rare.
1648 * But in fact, the number of active page waitqueues on typical
1649 * systems is ridiculously low, less than 200. So this is even
1650 * conservative, even though it seems large.
1651 *
1652 * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
1653 * waitqueues, i.e. the size of the waitq table given the number of pages.
1654 */
1655#define PAGES_PER_WAITQUEUE 256
1656
1657static inline unsigned long wait_table_size(unsigned long pages)
1658{
1659 unsigned long size = 1;
1660
1661 pages /= PAGES_PER_WAITQUEUE;
1662
1663 while (size < pages)
1664 size <<= 1;
1665
1666 /*
1667 * Once we have dozens or even hundreds of threads sleeping
1668 * on IO we've got bigger problems than wait queue collision.
1669 * Limit the size of the wait table to a reasonable size.
1670 */
1671 size = min(size, 4096UL);
1672
1673 return max(size, 4UL);
1674}
1675
1676/*
1677 * This is an integer logarithm so that shifts can be used later
1678 * to extract the more random high bits from the multiplicative
1679 * hash function before the remainder is taken.
1680 */
1681static inline unsigned long wait_table_bits(unsigned long size)
1682{
1683 return ffz(~size);
1684}
1685
1686#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1687
1688static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
1689 unsigned long *zones_size, unsigned long *zholes_size)
1690{
1691 unsigned long realtotalpages, totalpages = 0;
1692 int i;
1693
1694 for (i = 0; i < MAX_NR_ZONES; i++)
1695 totalpages += zones_size[i];
1696 pgdat->node_spanned_pages = totalpages;
1697
1698 realtotalpages = totalpages;
1699 if (zholes_size)
1700 for (i = 0; i < MAX_NR_ZONES; i++)
1701 realtotalpages -= zholes_size[i];
1702 pgdat->node_present_pages = realtotalpages;
1703 printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
1704}
1705
1706
1707/*
1708 * Initially all pages are reserved - free ones are freed
1709 * up by free_all_bootmem() once the early boot process is
1710 * done. Non-atomic initialization, single-pass.
1711 */
Dave Hansen3947be12005-10-29 18:16:54 -07001712void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 unsigned long start_pfn)
1714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct page *page;
Andy Whitcroft29751f62005-06-23 00:08:00 -07001716 unsigned long end_pfn = start_pfn + size;
1717 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001719 for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
1720 if (!early_pfn_valid(pfn))
1721 continue;
1722 page = pfn_to_page(pfn);
1723 set_page_links(page, zone, nid, pfn);
Nick Pigginb5810032005-10-29 18:16:12 -07001724 set_page_count(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 reset_page_mapcount(page);
1726 SetPageReserved(page);
1727 INIT_LIST_HEAD(&page->lru);
1728#ifdef WANT_PAGE_VIRTUAL
1729 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1730 if (!is_highmem_idx(zone))
Bob Picco3212c6b2005-06-27 14:36:28 -07001731 set_page_address(page, __va(pfn << PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734}
1735
1736void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
1737 unsigned long size)
1738{
1739 int order;
1740 for (order = 0; order < MAX_ORDER ; order++) {
1741 INIT_LIST_HEAD(&zone->free_area[order].free_list);
1742 zone->free_area[order].nr_free = 0;
1743 }
1744}
1745
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001746#define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
1747void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
1748 unsigned long size)
1749{
1750 unsigned long snum = pfn_to_section_nr(pfn);
1751 unsigned long end = pfn_to_section_nr(pfn + size);
1752
1753 if (FLAGS_HAS_NODE)
1754 zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
1755 else
1756 for (; snum <= end; snum++)
1757 zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
1758}
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#ifndef __HAVE_ARCH_MEMMAP_INIT
1761#define memmap_init(size, nid, zone, start_pfn) \
1762 memmap_init_zone((size), (nid), (zone), (start_pfn))
1763#endif
1764
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001765static int __devinit zone_batchsize(struct zone *zone)
1766{
1767 int batch;
1768
1769 /*
1770 * The per-cpu-pages pools are set to around 1000th of the
Seth, Rohitba56e912005-10-29 18:15:47 -07001771 * size of the zone. But no more than 1/2 of a meg.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001772 *
1773 * OK, so we don't know how big the cache is. So guess.
1774 */
1775 batch = zone->present_pages / 1024;
Seth, Rohitba56e912005-10-29 18:15:47 -07001776 if (batch * PAGE_SIZE > 512 * 1024)
1777 batch = (512 * 1024) / PAGE_SIZE;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001778 batch /= 4; /* We effectively *= 4 below */
1779 if (batch < 1)
1780 batch = 1;
1781
1782 /*
Nick Piggin0ceaacc2005-12-04 13:55:25 +11001783 * Clamp the batch to a 2^n - 1 value. Having a power
1784 * of 2 value was found to be more likely to have
1785 * suboptimal cache aliasing properties in some cases.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001786 *
Nick Piggin0ceaacc2005-12-04 13:55:25 +11001787 * For example if 2 tasks are alternately allocating
1788 * batches of pages, one task can end up with a lot
1789 * of pages of one half of the possible page colors
1790 * and the other with pages of the other colors.
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001791 */
Nick Piggin0ceaacc2005-12-04 13:55:25 +11001792 batch = (1 << (fls(batch + batch/2)-1)) - 1;
Seth, Rohitba56e912005-10-29 18:15:47 -07001793
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001794 return batch;
1795}
1796
Christoph Lameter2caaad42005-06-21 17:15:00 -07001797inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
1798{
1799 struct per_cpu_pages *pcp;
1800
Magnus Damm1c6fe942005-10-26 01:58:59 -07001801 memset(p, 0, sizeof(*p));
1802
Christoph Lameter2caaad42005-06-21 17:15:00 -07001803 pcp = &p->pcp[0]; /* hot */
1804 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07001805 pcp->high = 6 * batch;
1806 pcp->batch = max(1UL, 1 * batch);
1807 INIT_LIST_HEAD(&pcp->list);
1808
1809 pcp = &p->pcp[1]; /* cold*/
1810 pcp->count = 0;
Christoph Lameter2caaad42005-06-21 17:15:00 -07001811 pcp->high = 2 * batch;
Seth, Rohite46a5e22005-10-29 18:15:48 -07001812 pcp->batch = max(1UL, batch/2);
Christoph Lameter2caaad42005-06-21 17:15:00 -07001813 INIT_LIST_HEAD(&pcp->list);
1814}
1815
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001816#ifdef CONFIG_NUMA
1817/*
Christoph Lameter2caaad42005-06-21 17:15:00 -07001818 * Boot pageset table. One per cpu which is going to be used for all
1819 * zones and all nodes. The parameters will be set in such a way
1820 * that an item put on a list will immediately be handed over to
1821 * the buddy list. This is safe since pageset manipulation is done
1822 * with interrupts disabled.
1823 *
1824 * Some NUMA counter updates may also be caught by the boot pagesets.
Christoph Lameterb7c84c62005-06-22 20:26:07 -07001825 *
1826 * The boot_pagesets must be kept even after bootup is complete for
1827 * unused processors and/or zones. They do play a role for bootstrapping
1828 * hotplugged processors.
1829 *
1830 * zoneinfo_show() and maybe other functions do
1831 * not check if the processor is online before following the pageset pointer.
1832 * Other parts of the kernel may not check if the zone is available.
Christoph Lameter2caaad42005-06-21 17:15:00 -07001833 */
1834static struct per_cpu_pageset
Christoph Lameterb7c84c62005-06-22 20:26:07 -07001835 boot_pageset[NR_CPUS];
Christoph Lameter2caaad42005-06-21 17:15:00 -07001836
1837/*
1838 * Dynamically allocate memory for the
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001839 * per cpu pageset array in struct zone.
1840 */
1841static int __devinit process_zones(int cpu)
1842{
1843 struct zone *zone, *dzone;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001844
1845 for_each_zone(zone) {
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001846
Christoph Lameter2caaad42005-06-21 17:15:00 -07001847 zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001848 GFP_KERNEL, cpu_to_node(cpu));
Christoph Lameter2caaad42005-06-21 17:15:00 -07001849 if (!zone->pageset[cpu])
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001850 goto bad;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001851
Christoph Lameter2caaad42005-06-21 17:15:00 -07001852 setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001853 }
1854
1855 return 0;
1856bad:
1857 for_each_zone(dzone) {
1858 if (dzone == zone)
1859 break;
1860 kfree(dzone->pageset[cpu]);
1861 dzone->pageset[cpu] = NULL;
1862 }
1863 return -ENOMEM;
1864}
1865
1866static inline void free_zone_pagesets(int cpu)
1867{
1868#ifdef CONFIG_NUMA
1869 struct zone *zone;
1870
1871 for_each_zone(zone) {
1872 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
1873
1874 zone_pcp(zone, cpu) = NULL;
1875 kfree(pset);
1876 }
1877#endif
1878}
1879
1880static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
1881 unsigned long action,
1882 void *hcpu)
1883{
1884 int cpu = (long)hcpu;
1885 int ret = NOTIFY_OK;
1886
1887 switch (action) {
1888 case CPU_UP_PREPARE:
1889 if (process_zones(cpu))
1890 ret = NOTIFY_BAD;
1891 break;
Andi Kleenb0d41692005-11-05 17:25:53 +01001892 case CPU_UP_CANCELED:
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001893 case CPU_DEAD:
1894 free_zone_pagesets(cpu);
1895 break;
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001896 default:
1897 break;
1898 }
1899 return ret;
1900}
1901
1902static struct notifier_block pageset_notifier =
1903 { &pageset_cpuup_callback, NULL, 0 };
1904
Al Viro78d99552005-12-15 09:18:25 +00001905void __init setup_per_cpu_pageset(void)
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07001906{
1907 int err;
1908
1909 /* Initialize per_cpu_pageset for cpu 0.
1910 * A cpuup callback will do this for every cpu
1911 * as it comes online
1912 */
1913 err = process_zones(smp_processor_id());
1914 BUG_ON(err);
1915 register_cpu_notifier(&pageset_notifier);
1916}
1917
1918#endif
1919
Dave Hansened8ece22005-10-29 18:16:50 -07001920static __devinit
1921void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
1922{
1923 int i;
1924 struct pglist_data *pgdat = zone->zone_pgdat;
1925
1926 /*
1927 * The per-page waitqueue mechanism uses hashed waitqueues
1928 * per zone.
1929 */
1930 zone->wait_table_size = wait_table_size(zone_size_pages);
1931 zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
1932 zone->wait_table = (wait_queue_head_t *)
1933 alloc_bootmem_node(pgdat, zone->wait_table_size
1934 * sizeof(wait_queue_head_t));
1935
1936 for(i = 0; i < zone->wait_table_size; ++i)
1937 init_waitqueue_head(zone->wait_table + i);
1938}
1939
1940static __devinit void zone_pcp_init(struct zone *zone)
1941{
1942 int cpu;
1943 unsigned long batch = zone_batchsize(zone);
1944
1945 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1946#ifdef CONFIG_NUMA
1947 /* Early boot. Slab allocator not functional yet */
1948 zone->pageset[cpu] = &boot_pageset[cpu];
1949 setup_pageset(&boot_pageset[cpu],0);
1950#else
1951 setup_pageset(zone_pcp(zone,cpu), batch);
1952#endif
1953 }
1954 printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
1955 zone->name, zone->present_pages, batch);
1956}
1957
1958static __devinit void init_currently_empty_zone(struct zone *zone,
1959 unsigned long zone_start_pfn, unsigned long size)
1960{
1961 struct pglist_data *pgdat = zone->zone_pgdat;
1962
1963 zone_wait_table_init(zone, size);
1964 pgdat->nr_zones = zone_idx(zone) + 1;
1965
1966 zone->zone_mem_map = pfn_to_page(zone_start_pfn);
1967 zone->zone_start_pfn = zone_start_pfn;
1968
1969 memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
1970
1971 zone_init_free_lists(pgdat, zone, zone->spanned_pages);
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
1975 * Set up the zone data structures:
1976 * - mark all pages reserved
1977 * - mark all memory queues empty
1978 * - clear the memory bitmaps
1979 */
1980static void __init free_area_init_core(struct pglist_data *pgdat,
1981 unsigned long *zones_size, unsigned long *zholes_size)
1982{
Dave Hansened8ece22005-10-29 18:16:50 -07001983 unsigned long j;
1984 int nid = pgdat->node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 unsigned long zone_start_pfn = pgdat->node_start_pfn;
1986
Dave Hansen208d54e2005-10-29 18:16:52 -07001987 pgdat_resize_init(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 pgdat->nr_zones = 0;
1989 init_waitqueue_head(&pgdat->kswapd_wait);
1990 pgdat->kswapd_max_order = 0;
1991
1992 for (j = 0; j < MAX_NR_ZONES; j++) {
1993 struct zone *zone = pgdat->node_zones + j;
1994 unsigned long size, realsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 realsize = size = zones_size[j];
1997 if (zholes_size)
1998 realsize -= zholes_size[j];
1999
Andi Kleena2f1b422005-11-05 17:25:53 +01002000 if (j < ZONE_HIGHMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 nr_kernel_pages += realsize;
2002 nr_all_pages += realsize;
2003
2004 zone->spanned_pages = size;
2005 zone->present_pages = realsize;
2006 zone->name = zone_names[j];
2007 spin_lock_init(&zone->lock);
2008 spin_lock_init(&zone->lru_lock);
Dave Hansenbdc8cb92005-10-29 18:16:53 -07002009 zone_seqlock_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 zone->zone_pgdat = pgdat;
2011 zone->free_pages = 0;
2012
2013 zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
2014
Dave Hansened8ece22005-10-29 18:16:50 -07002015 zone_pcp_init(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 INIT_LIST_HEAD(&zone->active_list);
2017 INIT_LIST_HEAD(&zone->inactive_list);
2018 zone->nr_scan_active = 0;
2019 zone->nr_scan_inactive = 0;
2020 zone->nr_active = 0;
2021 zone->nr_inactive = 0;
Martin Hicks53e9a612005-09-03 15:54:51 -07002022 atomic_set(&zone->reclaim_in_progress, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (!size)
2024 continue;
2025
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002026 zonetable_add(zone, nid, j, zone_start_pfn, size);
Dave Hansened8ece22005-10-29 18:16:50 -07002027 init_currently_empty_zone(zone, zone_start_pfn, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 zone_start_pfn += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030}
2031
2032static void __init alloc_node_mem_map(struct pglist_data *pgdat)
2033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* Skip empty nodes */
2035 if (!pgdat->node_spanned_pages)
2036 return;
2037
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002038#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* ia64 gets its own node_mem_map, before this, without bootmem */
2040 if (!pgdat->node_mem_map) {
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002041 unsigned long size;
2042 struct page *map;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
Dave Hansen6f167ec2005-06-23 00:07:39 -07002045 map = alloc_remap(pgdat->node_id, size);
2046 if (!map)
2047 map = alloc_bootmem_node(pgdat, size);
2048 pgdat->node_mem_map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002050#ifdef CONFIG_FLATMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
2052 * With no DISCONTIG, the global mem_map is just set as node 0's
2053 */
2054 if (pgdat == NODE_DATA(0))
2055 mem_map = NODE_DATA(0)->node_mem_map;
2056#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -07002057#endif /* CONFIG_FLAT_NODE_MEM_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
2060void __init free_area_init_node(int nid, struct pglist_data *pgdat,
2061 unsigned long *zones_size, unsigned long node_start_pfn,
2062 unsigned long *zholes_size)
2063{
2064 pgdat->node_id = nid;
2065 pgdat->node_start_pfn = node_start_pfn;
2066 calculate_zone_totalpages(pgdat, zones_size, zholes_size);
2067
2068 alloc_node_mem_map(pgdat);
2069
2070 free_area_init_core(pgdat, zones_size, zholes_size);
2071}
2072
Dave Hansen93b75042005-06-23 00:07:47 -07002073#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074static bootmem_data_t contig_bootmem_data;
2075struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
2076
2077EXPORT_SYMBOL(contig_page_data);
Dave Hansen93b75042005-06-23 00:07:47 -07002078#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080void __init free_area_init(unsigned long *zones_size)
2081{
Dave Hansen93b75042005-06-23 00:07:47 -07002082 free_area_init_node(0, NODE_DATA(0), zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
2084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086#ifdef CONFIG_PROC_FS
2087
2088#include <linux/seq_file.h>
2089
2090static void *frag_start(struct seq_file *m, loff_t *pos)
2091{
2092 pg_data_t *pgdat;
2093 loff_t node = *pos;
2094
2095 for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
2096 --node;
2097
2098 return pgdat;
2099}
2100
2101static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
2102{
2103 pg_data_t *pgdat = (pg_data_t *)arg;
2104
2105 (*pos)++;
2106 return pgdat->pgdat_next;
2107}
2108
2109static void frag_stop(struct seq_file *m, void *arg)
2110{
2111}
2112
2113/*
2114 * This walks the free areas for each zone.
2115 */
2116static int frag_show(struct seq_file *m, void *arg)
2117{
2118 pg_data_t *pgdat = (pg_data_t *)arg;
2119 struct zone *zone;
2120 struct zone *node_zones = pgdat->node_zones;
2121 unsigned long flags;
2122 int order;
2123
2124 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
Con Kolivasf3fe6512006-01-06 00:11:15 -08002125 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 continue;
2127
2128 spin_lock_irqsave(&zone->lock, flags);
2129 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
2130 for (order = 0; order < MAX_ORDER; ++order)
2131 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
2132 spin_unlock_irqrestore(&zone->lock, flags);
2133 seq_putc(m, '\n');
2134 }
2135 return 0;
2136}
2137
2138struct seq_operations fragmentation_op = {
2139 .start = frag_start,
2140 .next = frag_next,
2141 .stop = frag_stop,
2142 .show = frag_show,
2143};
2144
Nikita Danilov295ab932005-06-21 17:14:38 -07002145/*
2146 * Output information about zones in @pgdat.
2147 */
2148static int zoneinfo_show(struct seq_file *m, void *arg)
2149{
2150 pg_data_t *pgdat = arg;
2151 struct zone *zone;
2152 struct zone *node_zones = pgdat->node_zones;
2153 unsigned long flags;
2154
2155 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
2156 int i;
2157
Con Kolivasf3fe6512006-01-06 00:11:15 -08002158 if (!populated_zone(zone))
Nikita Danilov295ab932005-06-21 17:14:38 -07002159 continue;
2160
2161 spin_lock_irqsave(&zone->lock, flags);
2162 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
2163 seq_printf(m,
2164 "\n pages free %lu"
2165 "\n min %lu"
2166 "\n low %lu"
2167 "\n high %lu"
2168 "\n active %lu"
2169 "\n inactive %lu"
2170 "\n scanned %lu (a: %lu i: %lu)"
2171 "\n spanned %lu"
2172 "\n present %lu",
2173 zone->free_pages,
2174 zone->pages_min,
2175 zone->pages_low,
2176 zone->pages_high,
2177 zone->nr_active,
2178 zone->nr_inactive,
2179 zone->pages_scanned,
2180 zone->nr_scan_active, zone->nr_scan_inactive,
2181 zone->spanned_pages,
2182 zone->present_pages);
2183 seq_printf(m,
2184 "\n protection: (%lu",
2185 zone->lowmem_reserve[0]);
2186 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
2187 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
2188 seq_printf(m,
2189 ")"
2190 "\n pagesets");
2191 for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
2192 struct per_cpu_pageset *pageset;
2193 int j;
2194
Christoph Lametere7c8d5c2005-06-21 17:14:47 -07002195 pageset = zone_pcp(zone, i);
Nikita Danilov295ab932005-06-21 17:14:38 -07002196 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2197 if (pageset->pcp[j].count)
2198 break;
2199 }
2200 if (j == ARRAY_SIZE(pageset->pcp))
2201 continue;
2202 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
2203 seq_printf(m,
2204 "\n cpu: %i pcp: %i"
2205 "\n count: %i"
Nikita Danilov295ab932005-06-21 17:14:38 -07002206 "\n high: %i"
2207 "\n batch: %i",
2208 i, j,
2209 pageset->pcp[j].count,
Nikita Danilov295ab932005-06-21 17:14:38 -07002210 pageset->pcp[j].high,
2211 pageset->pcp[j].batch);
2212 }
2213#ifdef CONFIG_NUMA
2214 seq_printf(m,
2215 "\n numa_hit: %lu"
2216 "\n numa_miss: %lu"
2217 "\n numa_foreign: %lu"
2218 "\n interleave_hit: %lu"
2219 "\n local_node: %lu"
2220 "\n other_node: %lu",
2221 pageset->numa_hit,
2222 pageset->numa_miss,
2223 pageset->numa_foreign,
2224 pageset->interleave_hit,
2225 pageset->local_node,
2226 pageset->other_node);
2227#endif
2228 }
2229 seq_printf(m,
2230 "\n all_unreclaimable: %u"
2231 "\n prev_priority: %i"
2232 "\n temp_priority: %i"
2233 "\n start_pfn: %lu",
2234 zone->all_unreclaimable,
2235 zone->prev_priority,
2236 zone->temp_priority,
2237 zone->zone_start_pfn);
2238 spin_unlock_irqrestore(&zone->lock, flags);
2239 seq_putc(m, '\n');
2240 }
2241 return 0;
2242}
2243
2244struct seq_operations zoneinfo_op = {
2245 .start = frag_start, /* iterate over all zones. The same as in
2246 * fragmentation. */
2247 .next = frag_next,
2248 .stop = frag_stop,
2249 .show = zoneinfo_show,
2250};
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static char *vmstat_text[] = {
2253 "nr_dirty",
2254 "nr_writeback",
2255 "nr_unstable",
2256 "nr_page_table_pages",
2257 "nr_mapped",
2258 "nr_slab",
2259
2260 "pgpgin",
2261 "pgpgout",
2262 "pswpin",
2263 "pswpout",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Nick Piggin9328b8f2006-01-06 00:11:10 -08002265 "pgalloc_high",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 "pgalloc_normal",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002267 "pgalloc_dma32",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 "pgalloc_dma",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 "pgfree",
2271 "pgactivate",
2272 "pgdeactivate",
2273
2274 "pgfault",
2275 "pgmajfault",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 "pgrefill_high",
2278 "pgrefill_normal",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002279 "pgrefill_dma32",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 "pgrefill_dma",
2281
2282 "pgsteal_high",
2283 "pgsteal_normal",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002284 "pgsteal_dma32",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 "pgsteal_dma",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 "pgscan_kswapd_high",
2288 "pgscan_kswapd_normal",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002289 "pgscan_kswapd_dma32",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 "pgscan_kswapd_dma",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 "pgscan_direct_high",
2293 "pgscan_direct_normal",
Nick Piggin9328b8f2006-01-06 00:11:10 -08002294 "pgscan_direct_dma32",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 "pgscan_direct_dma",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Nick Piggin9328b8f2006-01-06 00:11:10 -08002297 "pginodesteal",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 "slabs_scanned",
2299 "kswapd_steal",
2300 "kswapd_inodesteal",
2301 "pageoutrun",
2302 "allocstall",
2303
2304 "pgrotated",
KAMEZAWA Hiroyukiedfbe2b2005-05-01 08:58:37 -07002305 "nr_bounce",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306};
2307
2308static void *vmstat_start(struct seq_file *m, loff_t *pos)
2309{
2310 struct page_state *ps;
2311
2312 if (*pos >= ARRAY_SIZE(vmstat_text))
2313 return NULL;
2314
2315 ps = kmalloc(sizeof(*ps), GFP_KERNEL);
2316 m->private = ps;
2317 if (!ps)
2318 return ERR_PTR(-ENOMEM);
2319 get_full_page_state(ps);
2320 ps->pgpgin /= 2; /* sectors -> kbytes */
2321 ps->pgpgout /= 2;
2322 return (unsigned long *)ps + *pos;
2323}
2324
2325static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
2326{
2327 (*pos)++;
2328 if (*pos >= ARRAY_SIZE(vmstat_text))
2329 return NULL;
2330 return (unsigned long *)m->private + *pos;
2331}
2332
2333static int vmstat_show(struct seq_file *m, void *arg)
2334{
2335 unsigned long *l = arg;
2336 unsigned long off = l - (unsigned long *)m->private;
2337
2338 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
2339 return 0;
2340}
2341
2342static void vmstat_stop(struct seq_file *m, void *arg)
2343{
2344 kfree(m->private);
2345 m->private = NULL;
2346}
2347
2348struct seq_operations vmstat_op = {
2349 .start = vmstat_start,
2350 .next = vmstat_next,
2351 .stop = vmstat_stop,
2352 .show = vmstat_show,
2353};
2354
2355#endif /* CONFIG_PROC_FS */
2356
2357#ifdef CONFIG_HOTPLUG_CPU
2358static int page_alloc_cpu_notify(struct notifier_block *self,
2359 unsigned long action, void *hcpu)
2360{
2361 int cpu = (unsigned long)hcpu;
2362 long *count;
2363 unsigned long *src, *dest;
2364
2365 if (action == CPU_DEAD) {
2366 int i;
2367
2368 /* Drain local pagecache count. */
2369 count = &per_cpu(nr_pagecache_local, cpu);
2370 atomic_add(*count, &nr_pagecache);
2371 *count = 0;
2372 local_irq_disable();
2373 __drain_pages(cpu);
2374
2375 /* Add dead cpu's page_states to our own. */
2376 dest = (unsigned long *)&__get_cpu_var(page_states);
2377 src = (unsigned long *)&per_cpu(page_states, cpu);
2378
2379 for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
2380 i++) {
2381 dest[i] += src[i];
2382 src[i] = 0;
2383 }
2384
2385 local_irq_enable();
2386 }
2387 return NOTIFY_OK;
2388}
2389#endif /* CONFIG_HOTPLUG_CPU */
2390
2391void __init page_alloc_init(void)
2392{
2393 hotcpu_notifier(page_alloc_cpu_notify, 0);
2394}
2395
2396/*
2397 * setup_per_zone_lowmem_reserve - called whenever
2398 * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
2399 * has a correct pages reserved value, so an adequate number of
2400 * pages are left in the zone after a successful __alloc_pages().
2401 */
2402static void setup_per_zone_lowmem_reserve(void)
2403{
2404 struct pglist_data *pgdat;
2405 int j, idx;
2406
2407 for_each_pgdat(pgdat) {
2408 for (j = 0; j < MAX_NR_ZONES; j++) {
2409 struct zone *zone = pgdat->node_zones + j;
2410 unsigned long present_pages = zone->present_pages;
2411
2412 zone->lowmem_reserve[j] = 0;
2413
2414 for (idx = j-1; idx >= 0; idx--) {
2415 struct zone *lower_zone;
2416
2417 if (sysctl_lowmem_reserve_ratio[idx] < 1)
2418 sysctl_lowmem_reserve_ratio[idx] = 1;
2419
2420 lower_zone = pgdat->node_zones + idx;
2421 lower_zone->lowmem_reserve[j] = present_pages /
2422 sysctl_lowmem_reserve_ratio[idx];
2423 present_pages += lower_zone->present_pages;
2424 }
2425 }
2426 }
2427}
2428
2429/*
2430 * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
2431 * that the pages_{min,low,high} values for each zone are set correctly
2432 * with respect to min_free_kbytes.
2433 */
Dave Hansen3947be12005-10-29 18:16:54 -07002434void setup_per_zone_pages_min(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
2437 unsigned long lowmem_pages = 0;
2438 struct zone *zone;
2439 unsigned long flags;
2440
2441 /* Calculate total number of !ZONE_HIGHMEM pages */
2442 for_each_zone(zone) {
2443 if (!is_highmem(zone))
2444 lowmem_pages += zone->present_pages;
2445 }
2446
2447 for_each_zone(zone) {
Nick Piggin669ed172005-11-13 16:06:45 -08002448 unsigned long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 spin_lock_irqsave(&zone->lru_lock, flags);
Nick Piggin669ed172005-11-13 16:06:45 -08002450 tmp = (pages_min * zone->present_pages) / lowmem_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (is_highmem(zone)) {
2452 /*
Nick Piggin669ed172005-11-13 16:06:45 -08002453 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
2454 * need highmem pages, so cap pages_min to a small
2455 * value here.
2456 *
2457 * The (pages_high-pages_low) and (pages_low-pages_min)
2458 * deltas controls asynch page reclaim, and so should
2459 * not be capped for highmem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
2461 int min_pages;
2462
2463 min_pages = zone->present_pages / 1024;
2464 if (min_pages < SWAP_CLUSTER_MAX)
2465 min_pages = SWAP_CLUSTER_MAX;
2466 if (min_pages > 128)
2467 min_pages = 128;
2468 zone->pages_min = min_pages;
2469 } else {
Nick Piggin669ed172005-11-13 16:06:45 -08002470 /*
2471 * If it's a lowmem zone, reserve a number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * proportionate to the zone's size.
2473 */
Nick Piggin669ed172005-11-13 16:06:45 -08002474 zone->pages_min = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Nick Piggin669ed172005-11-13 16:06:45 -08002477 zone->pages_low = zone->pages_min + tmp / 4;
2478 zone->pages_high = zone->pages_min + tmp / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 spin_unlock_irqrestore(&zone->lru_lock, flags);
2480 }
2481}
2482
2483/*
2484 * Initialise min_free_kbytes.
2485 *
2486 * For small machines we want it small (128k min). For large machines
2487 * we want it large (64MB max). But it is not linear, because network
2488 * bandwidth does not increase linearly with machine size. We use
2489 *
2490 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
2491 * min_free_kbytes = sqrt(lowmem_kbytes * 16)
2492 *
2493 * which yields
2494 *
2495 * 16MB: 512k
2496 * 32MB: 724k
2497 * 64MB: 1024k
2498 * 128MB: 1448k
2499 * 256MB: 2048k
2500 * 512MB: 2896k
2501 * 1024MB: 4096k
2502 * 2048MB: 5792k
2503 * 4096MB: 8192k
2504 * 8192MB: 11584k
2505 * 16384MB: 16384k
2506 */
2507static int __init init_per_zone_pages_min(void)
2508{
2509 unsigned long lowmem_kbytes;
2510
2511 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
2512
2513 min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
2514 if (min_free_kbytes < 128)
2515 min_free_kbytes = 128;
2516 if (min_free_kbytes > 65536)
2517 min_free_kbytes = 65536;
2518 setup_per_zone_pages_min();
2519 setup_per_zone_lowmem_reserve();
2520 return 0;
2521}
2522module_init(init_per_zone_pages_min)
2523
2524/*
2525 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
2526 * that we can call two helper functions whenever min_free_kbytes
2527 * changes.
2528 */
2529int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
2530 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2531{
2532 proc_dointvec(table, write, file, buffer, length, ppos);
2533 setup_per_zone_pages_min();
2534 return 0;
2535}
2536
2537/*
2538 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
2539 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
2540 * whenever sysctl_lowmem_reserve_ratio changes.
2541 *
2542 * The reserve ratio obviously has absolutely no relation with the
2543 * pages_min watermarks. The lowmem reserve ratio can only make sense
2544 * if in function of the boot time zone sizes.
2545 */
2546int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
2547 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2548{
2549 proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2550 setup_per_zone_lowmem_reserve();
2551 return 0;
2552}
2553
2554__initdata int hashdist = HASHDIST_DEFAULT;
2555
2556#ifdef CONFIG_NUMA
2557static int __init set_hashdist(char *str)
2558{
2559 if (!str)
2560 return 0;
2561 hashdist = simple_strtoul(str, &str, 0);
2562 return 1;
2563}
2564__setup("hashdist=", set_hashdist);
2565#endif
2566
2567/*
2568 * allocate a large system hash table from bootmem
2569 * - it is assumed that the hash table must contain an exact power-of-2
2570 * quantity of entries
2571 * - limit is the number of hash buckets, not the total allocation size
2572 */
2573void *__init alloc_large_system_hash(const char *tablename,
2574 unsigned long bucketsize,
2575 unsigned long numentries,
2576 int scale,
2577 int flags,
2578 unsigned int *_hash_shift,
2579 unsigned int *_hash_mask,
2580 unsigned long limit)
2581{
2582 unsigned long long max = limit;
2583 unsigned long log2qty, size;
2584 void *table = NULL;
2585
2586 /* allow the kernel cmdline to have a say */
2587 if (!numentries) {
2588 /* round applicable memory size up to nearest megabyte */
2589 numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
2590 numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
2591 numentries >>= 20 - PAGE_SHIFT;
2592 numentries <<= 20 - PAGE_SHIFT;
2593
2594 /* limit to 1 bucket per 2^scale bytes of low memory */
2595 if (scale > PAGE_SHIFT)
2596 numentries >>= (scale - PAGE_SHIFT);
2597 else
2598 numentries <<= (PAGE_SHIFT - scale);
2599 }
2600 /* rounded up to nearest power of 2 in size */
2601 numentries = 1UL << (long_log2(numentries) + 1);
2602
2603 /* limit allocation size to 1/16 total memory by default */
2604 if (max == 0) {
2605 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
2606 do_div(max, bucketsize);
2607 }
2608
2609 if (numentries > max)
2610 numentries = max;
2611
2612 log2qty = long_log2(numentries);
2613
2614 do {
2615 size = bucketsize << log2qty;
2616 if (flags & HASH_EARLY)
2617 table = alloc_bootmem(size);
2618 else if (hashdist)
2619 table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
2620 else {
2621 unsigned long order;
2622 for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
2623 ;
2624 table = (void*) __get_free_pages(GFP_ATOMIC, order);
2625 }
2626 } while (!table && size > PAGE_SIZE && --log2qty);
2627
2628 if (!table)
2629 panic("Failed to allocate %s hash table\n", tablename);
2630
2631 printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
2632 tablename,
2633 (1U << log2qty),
2634 long_log2(size) - PAGE_SHIFT,
2635 size);
2636
2637 if (_hash_shift)
2638 *_hash_shift = log2qty;
2639 if (_hash_mask)
2640 *_hash_mask = (1 << log2qty) - 1;
2641
2642 return table;
2643}