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