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 | ||
1da177e4 LT |
17 | #include <linux/stddef.h> |
18 | #include <linux/mm.h> | |
19 | #include <linux/swap.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <linux/pagemap.h> | |
10ed273f | 22 | #include <linux/jiffies.h> |
1da177e4 LT |
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> | |
5a3135c2 | 31 | #include <linux/oom.h> |
1da177e4 LT |
32 | #include <linux/notifier.h> |
33 | #include <linux/topology.h> | |
34 | #include <linux/sysctl.h> | |
35 | #include <linux/cpu.h> | |
36 | #include <linux/cpuset.h> | |
bdc8cb98 | 37 | #include <linux/memory_hotplug.h> |
1da177e4 LT |
38 | #include <linux/nodemask.h> |
39 | #include <linux/vmalloc.h> | |
4be38e35 | 40 | #include <linux/mempolicy.h> |
6811378e | 41 | #include <linux/stop_machine.h> |
c713216d MG |
42 | #include <linux/sort.h> |
43 | #include <linux/pfn.h> | |
3fcfab16 | 44 | #include <linux/backing-dev.h> |
933e312e | 45 | #include <linux/fault-inject.h> |
a5d76b54 | 46 | #include <linux/page-isolation.h> |
52d4b9ac | 47 | #include <linux/page_cgroup.h> |
3ac7fe5a | 48 | #include <linux/debugobjects.h> |
1da177e4 LT |
49 | |
50 | #include <asm/tlbflush.h> | |
ac924c60 | 51 | #include <asm/div64.h> |
1da177e4 LT |
52 | #include "internal.h" |
53 | ||
54 | /* | |
13808910 | 55 | * Array of node states. |
1da177e4 | 56 | */ |
13808910 CL |
57 | nodemask_t node_states[NR_NODE_STATES] __read_mostly = { |
58 | [N_POSSIBLE] = NODE_MASK_ALL, | |
59 | [N_ONLINE] = { { [0] = 1UL } }, | |
60 | #ifndef CONFIG_NUMA | |
61 | [N_NORMAL_MEMORY] = { { [0] = 1UL } }, | |
62 | #ifdef CONFIG_HIGHMEM | |
63 | [N_HIGH_MEMORY] = { { [0] = 1UL } }, | |
64 | #endif | |
65 | [N_CPU] = { { [0] = 1UL } }, | |
66 | #endif /* NUMA */ | |
67 | }; | |
68 | EXPORT_SYMBOL(node_states); | |
69 | ||
6c231b7b | 70 | unsigned long totalram_pages __read_mostly; |
cb45b0e9 | 71 | unsigned long totalreserve_pages __read_mostly; |
22b31eec | 72 | unsigned long highest_memmap_pfn __read_mostly; |
8ad4b1fb | 73 | int percpu_pagelist_fraction; |
1da177e4 | 74 | |
d9c23400 MG |
75 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
76 | int pageblock_order __read_mostly; | |
77 | #endif | |
78 | ||
d98c7a09 | 79 | static void __free_pages_ok(struct page *page, unsigned int order); |
a226f6c8 | 80 | |
1da177e4 LT |
81 | /* |
82 | * results with 256, 32 in the lowmem_reserve sysctl: | |
83 | * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high) | |
84 | * 1G machine -> (16M dma, 784M normal, 224M high) | |
85 | * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA | |
86 | * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL | |
87 | * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA | |
a2f1b424 AK |
88 | * |
89 | * TBD: should special case ZONE_DMA32 machines here - in those we normally | |
90 | * don't need any ZONE_NORMAL reservation | |
1da177e4 | 91 | */ |
2f1b6248 | 92 | int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { |
4b51d669 | 93 | #ifdef CONFIG_ZONE_DMA |
2f1b6248 | 94 | 256, |
4b51d669 | 95 | #endif |
fb0e7942 | 96 | #ifdef CONFIG_ZONE_DMA32 |
2f1b6248 | 97 | 256, |
fb0e7942 | 98 | #endif |
e53ef38d | 99 | #ifdef CONFIG_HIGHMEM |
2a1e274a | 100 | 32, |
e53ef38d | 101 | #endif |
2a1e274a | 102 | 32, |
2f1b6248 | 103 | }; |
1da177e4 LT |
104 | |
105 | EXPORT_SYMBOL(totalram_pages); | |
1da177e4 | 106 | |
15ad7cdc | 107 | static char * const zone_names[MAX_NR_ZONES] = { |
4b51d669 | 108 | #ifdef CONFIG_ZONE_DMA |
2f1b6248 | 109 | "DMA", |
4b51d669 | 110 | #endif |
fb0e7942 | 111 | #ifdef CONFIG_ZONE_DMA32 |
2f1b6248 | 112 | "DMA32", |
fb0e7942 | 113 | #endif |
2f1b6248 | 114 | "Normal", |
e53ef38d | 115 | #ifdef CONFIG_HIGHMEM |
2a1e274a | 116 | "HighMem", |
e53ef38d | 117 | #endif |
2a1e274a | 118 | "Movable", |
2f1b6248 CL |
119 | }; |
120 | ||
1da177e4 LT |
121 | int min_free_kbytes = 1024; |
122 | ||
86356ab1 YG |
123 | unsigned long __meminitdata nr_kernel_pages; |
124 | unsigned long __meminitdata nr_all_pages; | |
a3142c8e | 125 | static unsigned long __meminitdata dma_reserve; |
1da177e4 | 126 | |
c713216d MG |
127 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
128 | /* | |
183ff22b | 129 | * MAX_ACTIVE_REGIONS determines the maximum number of distinct |
c713216d MG |
130 | * ranges of memory (RAM) that may be registered with add_active_range(). |
131 | * Ranges passed to add_active_range() will be merged if possible | |
132 | * so the number of times add_active_range() can be called is | |
133 | * related to the number of nodes and the number of holes | |
134 | */ | |
135 | #ifdef CONFIG_MAX_ACTIVE_REGIONS | |
136 | /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */ | |
137 | #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS | |
138 | #else | |
139 | #if MAX_NUMNODES >= 32 | |
140 | /* If there can be many nodes, allow up to 50 holes per node */ | |
141 | #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50) | |
142 | #else | |
143 | /* By default, allow up to 256 distinct regions */ | |
144 | #define MAX_ACTIVE_REGIONS 256 | |
145 | #endif | |
146 | #endif | |
147 | ||
98011f56 JB |
148 | static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS]; |
149 | static int __meminitdata nr_nodemap_entries; | |
150 | static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES]; | |
151 | static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES]; | |
b69a7288 | 152 | static unsigned long __initdata required_kernelcore; |
484f51f8 | 153 | static unsigned long __initdata required_movablecore; |
b69a7288 | 154 | static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES]; |
2a1e274a MG |
155 | |
156 | /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ | |
157 | int movable_zone; | |
158 | EXPORT_SYMBOL(movable_zone); | |
c713216d MG |
159 | #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */ |
160 | ||
418508c1 MS |
161 | #if MAX_NUMNODES > 1 |
162 | int nr_node_ids __read_mostly = MAX_NUMNODES; | |
163 | EXPORT_SYMBOL(nr_node_ids); | |
164 | #endif | |
165 | ||
9ef9acb0 MG |
166 | int page_group_by_mobility_disabled __read_mostly; |
167 | ||
b2a0ac88 MG |
168 | static void set_pageblock_migratetype(struct page *page, int migratetype) |
169 | { | |
170 | set_pageblock_flags_group(page, (unsigned long)migratetype, | |
171 | PB_migrate, PB_migrate_end); | |
172 | } | |
173 | ||
13e7444b | 174 | #ifdef CONFIG_DEBUG_VM |
c6a57e19 | 175 | static int page_outside_zone_boundaries(struct zone *zone, struct page *page) |
1da177e4 | 176 | { |
bdc8cb98 DH |
177 | int ret = 0; |
178 | unsigned seq; | |
179 | unsigned long pfn = page_to_pfn(page); | |
c6a57e19 | 180 | |
bdc8cb98 DH |
181 | do { |
182 | seq = zone_span_seqbegin(zone); | |
183 | if (pfn >= zone->zone_start_pfn + zone->spanned_pages) | |
184 | ret = 1; | |
185 | else if (pfn < zone->zone_start_pfn) | |
186 | ret = 1; | |
187 | } while (zone_span_seqretry(zone, seq)); | |
188 | ||
189 | return ret; | |
c6a57e19 DH |
190 | } |
191 | ||
192 | static int page_is_consistent(struct zone *zone, struct page *page) | |
193 | { | |
14e07298 | 194 | if (!pfn_valid_within(page_to_pfn(page))) |
c6a57e19 | 195 | return 0; |
1da177e4 | 196 | if (zone != page_zone(page)) |
c6a57e19 DH |
197 | return 0; |
198 | ||
199 | return 1; | |
200 | } | |
201 | /* | |
202 | * Temporary debugging check for pages not lying within a given zone. | |
203 | */ | |
204 | static int bad_range(struct zone *zone, struct page *page) | |
205 | { | |
206 | if (page_outside_zone_boundaries(zone, page)) | |
1da177e4 | 207 | return 1; |
c6a57e19 DH |
208 | if (!page_is_consistent(zone, page)) |
209 | return 1; | |
210 | ||
1da177e4 LT |
211 | return 0; |
212 | } | |
13e7444b NP |
213 | #else |
214 | static inline int bad_range(struct zone *zone, struct page *page) | |
215 | { | |
216 | return 0; | |
217 | } | |
218 | #endif | |
219 | ||
224abf92 | 220 | static void bad_page(struct page *page) |
1da177e4 | 221 | { |
d936cf9b HD |
222 | static unsigned long resume; |
223 | static unsigned long nr_shown; | |
224 | static unsigned long nr_unshown; | |
225 | ||
226 | /* | |
227 | * Allow a burst of 60 reports, then keep quiet for that minute; | |
228 | * or allow a steady drip of one report per second. | |
229 | */ | |
230 | if (nr_shown == 60) { | |
231 | if (time_before(jiffies, resume)) { | |
232 | nr_unshown++; | |
233 | goto out; | |
234 | } | |
235 | if (nr_unshown) { | |
1e9e6365 HD |
236 | printk(KERN_ALERT |
237 | "BUG: Bad page state: %lu messages suppressed\n", | |
d936cf9b HD |
238 | nr_unshown); |
239 | nr_unshown = 0; | |
240 | } | |
241 | nr_shown = 0; | |
242 | } | |
243 | if (nr_shown++ == 0) | |
244 | resume = jiffies + 60 * HZ; | |
245 | ||
1e9e6365 | 246 | printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n", |
3dc14741 | 247 | current->comm, page_to_pfn(page)); |
1e9e6365 | 248 | printk(KERN_ALERT |
3dc14741 HD |
249 | "page:%p flags:%p count:%d mapcount:%d mapping:%p index:%lx\n", |
250 | page, (void *)page->flags, page_count(page), | |
251 | page_mapcount(page), page->mapping, page->index); | |
3dc14741 | 252 | |
1da177e4 | 253 | dump_stack(); |
d936cf9b | 254 | out: |
8cc3b392 HD |
255 | /* Leave bad fields for debug, except PageBuddy could make trouble */ |
256 | __ClearPageBuddy(page); | |
9f158333 | 257 | add_taint(TAINT_BAD_PAGE); |
1da177e4 LT |
258 | } |
259 | ||
1da177e4 LT |
260 | /* |
261 | * Higher-order pages are called "compound pages". They are structured thusly: | |
262 | * | |
263 | * The first PAGE_SIZE page is called the "head page". | |
264 | * | |
265 | * The remaining PAGE_SIZE pages are called "tail pages". | |
266 | * | |
267 | * All pages have PG_compound set. All pages have their ->private pointing at | |
268 | * the head page (even the head page has this). | |
269 | * | |
41d78ba5 HD |
270 | * The first tail page's ->lru.next holds the address of the compound page's |
271 | * put_page() function. Its ->lru.prev holds the order of allocation. | |
272 | * This usage means that zero-order pages may not be compound. | |
1da177e4 | 273 | */ |
d98c7a09 HD |
274 | |
275 | static void free_compound_page(struct page *page) | |
276 | { | |
d85f3385 | 277 | __free_pages_ok(page, compound_order(page)); |
d98c7a09 HD |
278 | } |
279 | ||
01ad1c08 | 280 | void prep_compound_page(struct page *page, unsigned long order) |
18229df5 AW |
281 | { |
282 | int i; | |
283 | int nr_pages = 1 << order; | |
284 | ||
285 | set_compound_page_dtor(page, free_compound_page); | |
286 | set_compound_order(page, order); | |
287 | __SetPageHead(page); | |
288 | for (i = 1; i < nr_pages; i++) { | |
289 | struct page *p = page + i; | |
290 | ||
291 | __SetPageTail(p); | |
292 | p->first_page = page; | |
293 | } | |
294 | } | |
295 | ||
296 | #ifdef CONFIG_HUGETLBFS | |
297 | void prep_compound_gigantic_page(struct page *page, unsigned long order) | |
1da177e4 LT |
298 | { |
299 | int i; | |
300 | int nr_pages = 1 << order; | |
6babc32c | 301 | struct page *p = page + 1; |
1da177e4 | 302 | |
33f2ef89 | 303 | set_compound_page_dtor(page, free_compound_page); |
d85f3385 | 304 | set_compound_order(page, order); |
6d777953 | 305 | __SetPageHead(page); |
18229df5 | 306 | for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { |
d85f3385 | 307 | __SetPageTail(p); |
d85f3385 | 308 | p->first_page = page; |
1da177e4 LT |
309 | } |
310 | } | |
18229df5 | 311 | #endif |
1da177e4 | 312 | |
8cc3b392 | 313 | static int destroy_compound_page(struct page *page, unsigned long order) |
1da177e4 LT |
314 | { |
315 | int i; | |
316 | int nr_pages = 1 << order; | |
8cc3b392 | 317 | int bad = 0; |
1da177e4 | 318 | |
8cc3b392 HD |
319 | if (unlikely(compound_order(page) != order) || |
320 | unlikely(!PageHead(page))) { | |
224abf92 | 321 | bad_page(page); |
8cc3b392 HD |
322 | bad++; |
323 | } | |
1da177e4 | 324 | |
6d777953 | 325 | __ClearPageHead(page); |
8cc3b392 | 326 | |
18229df5 AW |
327 | for (i = 1; i < nr_pages; i++) { |
328 | struct page *p = page + i; | |
1da177e4 | 329 | |
e713a21d | 330 | if (unlikely(!PageTail(p) || (p->first_page != page))) { |
224abf92 | 331 | bad_page(page); |
8cc3b392 HD |
332 | bad++; |
333 | } | |
d85f3385 | 334 | __ClearPageTail(p); |
1da177e4 | 335 | } |
8cc3b392 HD |
336 | |
337 | return bad; | |
1da177e4 | 338 | } |
1da177e4 | 339 | |
17cf4406 NP |
340 | static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags) |
341 | { | |
342 | int i; | |
343 | ||
6626c5d5 AM |
344 | /* |
345 | * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO | |
346 | * and __GFP_HIGHMEM from hard or soft interrupt context. | |
347 | */ | |
725d704e | 348 | VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt()); |
17cf4406 NP |
349 | for (i = 0; i < (1 << order); i++) |
350 | clear_highpage(page + i); | |
351 | } | |
352 | ||
6aa3001b AM |
353 | static inline void set_page_order(struct page *page, int order) |
354 | { | |
4c21e2f2 | 355 | set_page_private(page, order); |
676165a8 | 356 | __SetPageBuddy(page); |
1da177e4 LT |
357 | } |
358 | ||
359 | static inline void rmv_page_order(struct page *page) | |
360 | { | |
676165a8 | 361 | __ClearPageBuddy(page); |
4c21e2f2 | 362 | set_page_private(page, 0); |
1da177e4 LT |
363 | } |
364 | ||
365 | /* | |
366 | * Locate the struct page for both the matching buddy in our | |
367 | * pair (buddy1) and the combined O(n+1) page they form (page). | |
368 | * | |
369 | * 1) Any buddy B1 will have an order O twin B2 which satisfies | |
370 | * the following equation: | |
371 | * B2 = B1 ^ (1 << O) | |
372 | * For example, if the starting buddy (buddy2) is #8 its order | |
373 | * 1 buddy is #10: | |
374 | * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 | |
375 | * | |
376 | * 2) Any buddy B will have an order O+1 parent P which | |
377 | * satisfies the following equation: | |
378 | * P = B & ~(1 << O) | |
379 | * | |
d6e05edc | 380 | * Assumption: *_mem_map is contiguous at least up to MAX_ORDER |
1da177e4 LT |
381 | */ |
382 | static inline struct page * | |
383 | __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order) | |
384 | { | |
385 | unsigned long buddy_idx = page_idx ^ (1 << order); | |
386 | ||
387 | return page + (buddy_idx - page_idx); | |
388 | } | |
389 | ||
390 | static inline unsigned long | |
391 | __find_combined_index(unsigned long page_idx, unsigned int order) | |
392 | { | |
393 | return (page_idx & ~(1 << order)); | |
394 | } | |
395 | ||
396 | /* | |
397 | * This function checks whether a page is free && is the buddy | |
398 | * we can do coalesce a page and its buddy if | |
13e7444b | 399 | * (a) the buddy is not in a hole && |
676165a8 | 400 | * (b) the buddy is in the buddy system && |
cb2b95e1 AW |
401 | * (c) a page and its buddy have the same order && |
402 | * (d) a page and its buddy are in the same zone. | |
676165a8 NP |
403 | * |
404 | * For recording whether a page is in the buddy system, we use PG_buddy. | |
405 | * Setting, clearing, and testing PG_buddy is serialized by zone->lock. | |
1da177e4 | 406 | * |
676165a8 | 407 | * For recording page's order, we use page_private(page). |
1da177e4 | 408 | */ |
cb2b95e1 AW |
409 | static inline int page_is_buddy(struct page *page, struct page *buddy, |
410 | int order) | |
1da177e4 | 411 | { |
14e07298 | 412 | if (!pfn_valid_within(page_to_pfn(buddy))) |
13e7444b | 413 | return 0; |
13e7444b | 414 | |
cb2b95e1 AW |
415 | if (page_zone_id(page) != page_zone_id(buddy)) |
416 | return 0; | |
417 | ||
418 | if (PageBuddy(buddy) && page_order(buddy) == order) { | |
419 | BUG_ON(page_count(buddy) != 0); | |
6aa3001b | 420 | return 1; |
676165a8 | 421 | } |
6aa3001b | 422 | return 0; |
1da177e4 LT |
423 | } |
424 | ||
425 | /* | |
426 | * Freeing function for a buddy system allocator. | |
427 | * | |
428 | * The concept of a buddy system is to maintain direct-mapped table | |
429 | * (containing bit values) for memory blocks of various "orders". | |
430 | * The bottom level table contains the map for the smallest allocatable | |
431 | * units of memory (here, pages), and each level above it describes | |
432 | * pairs of units from the levels below, hence, "buddies". | |
433 | * At a high level, all that happens here is marking the table entry | |
434 | * at the bottom level available, and propagating the changes upward | |
435 | * as necessary, plus some accounting needed to play nicely with other | |
436 | * parts of the VM system. | |
437 | * At each level, we keep a list of pages, which are heads of continuous | |
676165a8 | 438 | * free pages of length of (1 << order) and marked with PG_buddy. Page's |
4c21e2f2 | 439 | * order is recorded in page_private(page) field. |
1da177e4 LT |
440 | * So when we are allocating or freeing one, we can derive the state of the |
441 | * other. That is, if we allocate a small block, and both were | |
442 | * free, the remainder of the region must be split into blocks. | |
443 | * If a block is freed, and its buddy is also free, then this | |
444 | * triggers coalescing into a block of larger size. | |
445 | * | |
446 | * -- wli | |
447 | */ | |
448 | ||
48db57f8 | 449 | static inline void __free_one_page(struct page *page, |
1da177e4 LT |
450 | struct zone *zone, unsigned int order) |
451 | { | |
452 | unsigned long page_idx; | |
453 | int order_size = 1 << order; | |
b2a0ac88 | 454 | int migratetype = get_pageblock_migratetype(page); |
1da177e4 | 455 | |
224abf92 | 456 | if (unlikely(PageCompound(page))) |
8cc3b392 HD |
457 | if (unlikely(destroy_compound_page(page, order))) |
458 | return; | |
1da177e4 LT |
459 | |
460 | page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1); | |
461 | ||
725d704e NP |
462 | VM_BUG_ON(page_idx & (order_size - 1)); |
463 | VM_BUG_ON(bad_range(zone, page)); | |
1da177e4 | 464 | |
d23ad423 | 465 | __mod_zone_page_state(zone, NR_FREE_PAGES, order_size); |
1da177e4 LT |
466 | while (order < MAX_ORDER-1) { |
467 | unsigned long combined_idx; | |
1da177e4 LT |
468 | struct page *buddy; |
469 | ||
1da177e4 | 470 | buddy = __page_find_buddy(page, page_idx, order); |
cb2b95e1 | 471 | if (!page_is_buddy(page, buddy, order)) |
3c82d0ce | 472 | break; |
13e7444b | 473 | |
3c82d0ce | 474 | /* Our buddy is free, merge with it and move up one order. */ |
1da177e4 | 475 | list_del(&buddy->lru); |
b2a0ac88 | 476 | zone->free_area[order].nr_free--; |
1da177e4 | 477 | rmv_page_order(buddy); |
13e7444b | 478 | combined_idx = __find_combined_index(page_idx, order); |
1da177e4 LT |
479 | page = page + (combined_idx - page_idx); |
480 | page_idx = combined_idx; | |
481 | order++; | |
482 | } | |
483 | set_page_order(page, order); | |
b2a0ac88 MG |
484 | list_add(&page->lru, |
485 | &zone->free_area[order].free_list[migratetype]); | |
1da177e4 LT |
486 | zone->free_area[order].nr_free++; |
487 | } | |
488 | ||
224abf92 | 489 | static inline int free_pages_check(struct page *page) |
1da177e4 | 490 | { |
985737cf | 491 | free_page_mlock(page); |
92be2e33 NP |
492 | if (unlikely(page_mapcount(page) | |
493 | (page->mapping != NULL) | | |
494 | (page_count(page) != 0) | | |
8cc3b392 | 495 | (page->flags & PAGE_FLAGS_CHECK_AT_FREE))) { |
224abf92 | 496 | bad_page(page); |
79f4b7bf | 497 | return 1; |
8cc3b392 | 498 | } |
79f4b7bf HD |
499 | if (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
500 | page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; | |
501 | return 0; | |
1da177e4 LT |
502 | } |
503 | ||
504 | /* | |
505 | * Frees a list of pages. | |
506 | * Assumes all pages on list are in same zone, and of same order. | |
207f36ee | 507 | * count is the number of pages to free. |
1da177e4 LT |
508 | * |
509 | * If the zone was previously in an "all pages pinned" state then look to | |
510 | * see if this freeing clears that state. | |
511 | * | |
512 | * And clear the zone's pages_scanned counter, to hold off the "all pages are | |
513 | * pinned" detection logic. | |
514 | */ | |
48db57f8 NP |
515 | static void free_pages_bulk(struct zone *zone, int count, |
516 | struct list_head *list, int order) | |
1da177e4 | 517 | { |
c54ad30c | 518 | spin_lock(&zone->lock); |
e815af95 | 519 | zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE); |
1da177e4 | 520 | zone->pages_scanned = 0; |
48db57f8 NP |
521 | while (count--) { |
522 | struct page *page; | |
523 | ||
725d704e | 524 | VM_BUG_ON(list_empty(list)); |
1da177e4 | 525 | page = list_entry(list->prev, struct page, lru); |
48db57f8 | 526 | /* have to delete it as __free_one_page list manipulates */ |
1da177e4 | 527 | list_del(&page->lru); |
48db57f8 | 528 | __free_one_page(page, zone, order); |
1da177e4 | 529 | } |
c54ad30c | 530 | spin_unlock(&zone->lock); |
1da177e4 LT |
531 | } |
532 | ||
48db57f8 | 533 | static void free_one_page(struct zone *zone, struct page *page, int order) |
1da177e4 | 534 | { |
006d22d9 | 535 | spin_lock(&zone->lock); |
e815af95 | 536 | zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE); |
006d22d9 | 537 | zone->pages_scanned = 0; |
0798e519 | 538 | __free_one_page(page, zone, order); |
006d22d9 | 539 | spin_unlock(&zone->lock); |
48db57f8 NP |
540 | } |
541 | ||
542 | static void __free_pages_ok(struct page *page, unsigned int order) | |
543 | { | |
544 | unsigned long flags; | |
1da177e4 | 545 | int i; |
8cc3b392 | 546 | int bad = 0; |
1da177e4 | 547 | |
1da177e4 | 548 | for (i = 0 ; i < (1 << order) ; ++i) |
8cc3b392 HD |
549 | bad += free_pages_check(page + i); |
550 | if (bad) | |
689bcebf HD |
551 | return; |
552 | ||
3ac7fe5a | 553 | if (!PageHighMem(page)) { |
9858db50 | 554 | debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order); |
3ac7fe5a TG |
555 | debug_check_no_obj_freed(page_address(page), |
556 | PAGE_SIZE << order); | |
557 | } | |
dafb1367 | 558 | arch_free_page(page, order); |
48db57f8 | 559 | kernel_map_pages(page, 1 << order, 0); |
dafb1367 | 560 | |
c54ad30c | 561 | local_irq_save(flags); |
f8891e5e | 562 | __count_vm_events(PGFREE, 1 << order); |
48db57f8 | 563 | free_one_page(page_zone(page), page, order); |
c54ad30c | 564 | local_irq_restore(flags); |
1da177e4 LT |
565 | } |
566 | ||
a226f6c8 DH |
567 | /* |
568 | * permit the bootmem allocator to evade page validation on high-order frees | |
569 | */ | |
af370fb8 | 570 | void __meminit __free_pages_bootmem(struct page *page, unsigned int order) |
a226f6c8 DH |
571 | { |
572 | if (order == 0) { | |
573 | __ClearPageReserved(page); | |
574 | set_page_count(page, 0); | |
7835e98b | 575 | set_page_refcounted(page); |
545b1ea9 | 576 | __free_page(page); |
a226f6c8 | 577 | } else { |
a226f6c8 DH |
578 | int loop; |
579 | ||
545b1ea9 | 580 | prefetchw(page); |
a226f6c8 DH |
581 | for (loop = 0; loop < BITS_PER_LONG; loop++) { |
582 | struct page *p = &page[loop]; | |
583 | ||
545b1ea9 NP |
584 | if (loop + 1 < BITS_PER_LONG) |
585 | prefetchw(p + 1); | |
a226f6c8 DH |
586 | __ClearPageReserved(p); |
587 | set_page_count(p, 0); | |
588 | } | |
589 | ||
7835e98b | 590 | set_page_refcounted(page); |
545b1ea9 | 591 | __free_pages(page, order); |
a226f6c8 DH |
592 | } |
593 | } | |
594 | ||
1da177e4 LT |
595 | |
596 | /* | |
597 | * The order of subdivision here is critical for the IO subsystem. | |
598 | * Please do not alter this order without good reasons and regression | |
599 | * testing. Specifically, as large blocks of memory are subdivided, | |
600 | * the order in which smaller blocks are delivered depends on the order | |
601 | * they're subdivided in this function. This is the primary factor | |
602 | * influencing the order in which pages are delivered to the IO | |
603 | * subsystem according to empirical testing, and this is also justified | |
604 | * by considering the behavior of a buddy system containing a single | |
605 | * large block of memory acted on by a series of small allocations. | |
606 | * This behavior is a critical factor in sglist merging's success. | |
607 | * | |
608 | * -- wli | |
609 | */ | |
085cc7d5 | 610 | static inline void expand(struct zone *zone, struct page *page, |
b2a0ac88 MG |
611 | int low, int high, struct free_area *area, |
612 | int migratetype) | |
1da177e4 LT |
613 | { |
614 | unsigned long size = 1 << high; | |
615 | ||
616 | while (high > low) { | |
617 | area--; | |
618 | high--; | |
619 | size >>= 1; | |
725d704e | 620 | VM_BUG_ON(bad_range(zone, &page[size])); |
b2a0ac88 | 621 | list_add(&page[size].lru, &area->free_list[migratetype]); |
1da177e4 LT |
622 | area->nr_free++; |
623 | set_page_order(&page[size], high); | |
624 | } | |
1da177e4 LT |
625 | } |
626 | ||
1da177e4 LT |
627 | /* |
628 | * This page is about to be returned from the page allocator | |
629 | */ | |
17cf4406 | 630 | static int prep_new_page(struct page *page, int order, gfp_t gfp_flags) |
1da177e4 | 631 | { |
92be2e33 NP |
632 | if (unlikely(page_mapcount(page) | |
633 | (page->mapping != NULL) | | |
634 | (page_count(page) != 0) | | |
8cc3b392 | 635 | (page->flags & PAGE_FLAGS_CHECK_AT_PREP))) { |
224abf92 | 636 | bad_page(page); |
689bcebf | 637 | return 1; |
8cc3b392 | 638 | } |
689bcebf | 639 | |
4c21e2f2 | 640 | set_page_private(page, 0); |
7835e98b | 641 | set_page_refcounted(page); |
cc102509 NP |
642 | |
643 | arch_alloc_page(page, order); | |
1da177e4 | 644 | kernel_map_pages(page, 1 << order, 1); |
17cf4406 NP |
645 | |
646 | if (gfp_flags & __GFP_ZERO) | |
647 | prep_zero_page(page, order, gfp_flags); | |
648 | ||
649 | if (order && (gfp_flags & __GFP_COMP)) | |
650 | prep_compound_page(page, order); | |
651 | ||
689bcebf | 652 | return 0; |
1da177e4 LT |
653 | } |
654 | ||
56fd56b8 MG |
655 | /* |
656 | * Go through the free lists for the given migratetype and remove | |
657 | * the smallest available page from the freelists | |
658 | */ | |
659 | static struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, | |
660 | int migratetype) | |
661 | { | |
662 | unsigned int current_order; | |
663 | struct free_area * area; | |
664 | struct page *page; | |
665 | ||
666 | /* Find a page of the appropriate size in the preferred list */ | |
667 | for (current_order = order; current_order < MAX_ORDER; ++current_order) { | |
668 | area = &(zone->free_area[current_order]); | |
669 | if (list_empty(&area->free_list[migratetype])) | |
670 | continue; | |
671 | ||
672 | page = list_entry(area->free_list[migratetype].next, | |
673 | struct page, lru); | |
674 | list_del(&page->lru); | |
675 | rmv_page_order(page); | |
676 | area->nr_free--; | |
677 | __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order)); | |
678 | expand(zone, page, order, current_order, area, migratetype); | |
679 | return page; | |
680 | } | |
681 | ||
682 | return NULL; | |
683 | } | |
684 | ||
685 | ||
b2a0ac88 MG |
686 | /* |
687 | * This array describes the order lists are fallen back to when | |
688 | * the free lists for the desirable migrate type are depleted | |
689 | */ | |
690 | static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = { | |
64c5e135 MG |
691 | [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE }, |
692 | [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE }, | |
693 | [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE }, | |
694 | [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */ | |
b2a0ac88 MG |
695 | }; |
696 | ||
c361be55 MG |
697 | /* |
698 | * Move the free pages in a range to the free lists of the requested type. | |
d9c23400 | 699 | * Note that start_page and end_pages are not aligned on a pageblock |
c361be55 MG |
700 | * boundary. If alignment is required, use move_freepages_block() |
701 | */ | |
b69a7288 AB |
702 | static int move_freepages(struct zone *zone, |
703 | struct page *start_page, struct page *end_page, | |
704 | int migratetype) | |
c361be55 MG |
705 | { |
706 | struct page *page; | |
707 | unsigned long order; | |
d100313f | 708 | int pages_moved = 0; |
c361be55 MG |
709 | |
710 | #ifndef CONFIG_HOLES_IN_ZONE | |
711 | /* | |
712 | * page_zone is not safe to call in this context when | |
713 | * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant | |
714 | * anyway as we check zone boundaries in move_freepages_block(). | |
715 | * Remove at a later date when no bug reports exist related to | |
ac0e5b7a | 716 | * grouping pages by mobility |
c361be55 MG |
717 | */ |
718 | BUG_ON(page_zone(start_page) != page_zone(end_page)); | |
719 | #endif | |
720 | ||
721 | for (page = start_page; page <= end_page;) { | |
344c790e AL |
722 | /* Make sure we are not inadvertently changing nodes */ |
723 | VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone)); | |
724 | ||
c361be55 MG |
725 | if (!pfn_valid_within(page_to_pfn(page))) { |
726 | page++; | |
727 | continue; | |
728 | } | |
729 | ||
730 | if (!PageBuddy(page)) { | |
731 | page++; | |
732 | continue; | |
733 | } | |
734 | ||
735 | order = page_order(page); | |
736 | list_del(&page->lru); | |
737 | list_add(&page->lru, | |
738 | &zone->free_area[order].free_list[migratetype]); | |
739 | page += 1 << order; | |
d100313f | 740 | pages_moved += 1 << order; |
c361be55 MG |
741 | } |
742 | ||
d100313f | 743 | return pages_moved; |
c361be55 MG |
744 | } |
745 | ||
b69a7288 AB |
746 | static int move_freepages_block(struct zone *zone, struct page *page, |
747 | int migratetype) | |
c361be55 MG |
748 | { |
749 | unsigned long start_pfn, end_pfn; | |
750 | struct page *start_page, *end_page; | |
751 | ||
752 | start_pfn = page_to_pfn(page); | |
d9c23400 | 753 | start_pfn = start_pfn & ~(pageblock_nr_pages-1); |
c361be55 | 754 | start_page = pfn_to_page(start_pfn); |
d9c23400 MG |
755 | end_page = start_page + pageblock_nr_pages - 1; |
756 | end_pfn = start_pfn + pageblock_nr_pages - 1; | |
c361be55 MG |
757 | |
758 | /* Do not cross zone boundaries */ | |
759 | if (start_pfn < zone->zone_start_pfn) | |
760 | start_page = page; | |
761 | if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages) | |
762 | return 0; | |
763 | ||
764 | return move_freepages(zone, start_page, end_page, migratetype); | |
765 | } | |
766 | ||
b2a0ac88 MG |
767 | /* Remove an element from the buddy allocator from the fallback list */ |
768 | static struct page *__rmqueue_fallback(struct zone *zone, int order, | |
769 | int start_migratetype) | |
770 | { | |
771 | struct free_area * area; | |
772 | int current_order; | |
773 | struct page *page; | |
774 | int migratetype, i; | |
775 | ||
776 | /* Find the largest possible block of pages in the other list */ | |
777 | for (current_order = MAX_ORDER-1; current_order >= order; | |
778 | --current_order) { | |
779 | for (i = 0; i < MIGRATE_TYPES - 1; i++) { | |
780 | migratetype = fallbacks[start_migratetype][i]; | |
781 | ||
56fd56b8 MG |
782 | /* MIGRATE_RESERVE handled later if necessary */ |
783 | if (migratetype == MIGRATE_RESERVE) | |
784 | continue; | |
e010487d | 785 | |
b2a0ac88 MG |
786 | area = &(zone->free_area[current_order]); |
787 | if (list_empty(&area->free_list[migratetype])) | |
788 | continue; | |
789 | ||
790 | page = list_entry(area->free_list[migratetype].next, | |
791 | struct page, lru); | |
792 | area->nr_free--; | |
793 | ||
794 | /* | |
c361be55 | 795 | * If breaking a large block of pages, move all free |
46dafbca MG |
796 | * pages to the preferred allocation list. If falling |
797 | * back for a reclaimable kernel allocation, be more | |
798 | * agressive about taking ownership of free pages | |
b2a0ac88 | 799 | */ |
d9c23400 | 800 | if (unlikely(current_order >= (pageblock_order >> 1)) || |
46dafbca MG |
801 | start_migratetype == MIGRATE_RECLAIMABLE) { |
802 | unsigned long pages; | |
803 | pages = move_freepages_block(zone, page, | |
804 | start_migratetype); | |
805 | ||
806 | /* Claim the whole block if over half of it is free */ | |
d9c23400 | 807 | if (pages >= (1 << (pageblock_order-1))) |
46dafbca MG |
808 | set_pageblock_migratetype(page, |
809 | start_migratetype); | |
810 | ||
b2a0ac88 | 811 | migratetype = start_migratetype; |
c361be55 | 812 | } |
b2a0ac88 MG |
813 | |
814 | /* Remove the page from the freelists */ | |
815 | list_del(&page->lru); | |
816 | rmv_page_order(page); | |
817 | __mod_zone_page_state(zone, NR_FREE_PAGES, | |
818 | -(1UL << order)); | |
819 | ||
d9c23400 | 820 | if (current_order == pageblock_order) |
b2a0ac88 MG |
821 | set_pageblock_migratetype(page, |
822 | start_migratetype); | |
823 | ||
824 | expand(zone, page, order, current_order, area, migratetype); | |
825 | return page; | |
826 | } | |
827 | } | |
828 | ||
56fd56b8 MG |
829 | /* Use MIGRATE_RESERVE rather than fail an allocation */ |
830 | return __rmqueue_smallest(zone, order, MIGRATE_RESERVE); | |
b2a0ac88 MG |
831 | } |
832 | ||
56fd56b8 | 833 | /* |
1da177e4 LT |
834 | * Do the hard work of removing an element from the buddy allocator. |
835 | * Call me with the zone->lock already held. | |
836 | */ | |
b2a0ac88 MG |
837 | static struct page *__rmqueue(struct zone *zone, unsigned int order, |
838 | int migratetype) | |
1da177e4 | 839 | { |
1da177e4 LT |
840 | struct page *page; |
841 | ||
56fd56b8 | 842 | page = __rmqueue_smallest(zone, order, migratetype); |
b2a0ac88 | 843 | |
56fd56b8 MG |
844 | if (unlikely(!page)) |
845 | page = __rmqueue_fallback(zone, order, migratetype); | |
b2a0ac88 MG |
846 | |
847 | return page; | |
1da177e4 LT |
848 | } |
849 | ||
850 | /* | |
851 | * Obtain a specified number of elements from the buddy allocator, all under | |
852 | * a single hold of the lock, for efficiency. Add them to the supplied list. | |
853 | * Returns the number of new pages which were placed at *list. | |
854 | */ | |
855 | static int rmqueue_bulk(struct zone *zone, unsigned int order, | |
b2a0ac88 MG |
856 | unsigned long count, struct list_head *list, |
857 | int migratetype) | |
1da177e4 | 858 | { |
1da177e4 | 859 | int i; |
1da177e4 | 860 | |
c54ad30c | 861 | spin_lock(&zone->lock); |
1da177e4 | 862 | for (i = 0; i < count; ++i) { |
b2a0ac88 | 863 | struct page *page = __rmqueue(zone, order, migratetype); |
085cc7d5 | 864 | if (unlikely(page == NULL)) |
1da177e4 | 865 | break; |
81eabcbe MG |
866 | |
867 | /* | |
868 | * Split buddy pages returned by expand() are received here | |
869 | * in physical page order. The page is added to the callers and | |
870 | * list and the list head then moves forward. From the callers | |
871 | * perspective, the linked list is ordered by page number in | |
872 | * some conditions. This is useful for IO devices that can | |
873 | * merge IO requests if the physical pages are ordered | |
874 | * properly. | |
875 | */ | |
535131e6 MG |
876 | list_add(&page->lru, list); |
877 | set_page_private(page, migratetype); | |
81eabcbe | 878 | list = &page->lru; |
1da177e4 | 879 | } |
c54ad30c | 880 | spin_unlock(&zone->lock); |
085cc7d5 | 881 | return i; |
1da177e4 LT |
882 | } |
883 | ||
4ae7c039 | 884 | #ifdef CONFIG_NUMA |
8fce4d8e | 885 | /* |
4037d452 CL |
886 | * Called from the vmstat counter updater to drain pagesets of this |
887 | * currently executing processor on remote nodes after they have | |
888 | * expired. | |
889 | * | |
879336c3 CL |
890 | * Note that this function must be called with the thread pinned to |
891 | * a single processor. | |
8fce4d8e | 892 | */ |
4037d452 | 893 | void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) |
4ae7c039 | 894 | { |
4ae7c039 | 895 | unsigned long flags; |
4037d452 | 896 | int to_drain; |
4ae7c039 | 897 | |
4037d452 CL |
898 | local_irq_save(flags); |
899 | if (pcp->count >= pcp->batch) | |
900 | to_drain = pcp->batch; | |
901 | else | |
902 | to_drain = pcp->count; | |
903 | free_pages_bulk(zone, to_drain, &pcp->list, 0); | |
904 | pcp->count -= to_drain; | |
905 | local_irq_restore(flags); | |
4ae7c039 CL |
906 | } |
907 | #endif | |
908 | ||
9f8f2172 CL |
909 | /* |
910 | * Drain pages of the indicated processor. | |
911 | * | |
912 | * The processor must either be the current processor and the | |
913 | * thread pinned to the current processor or a processor that | |
914 | * is not online. | |
915 | */ | |
916 | static void drain_pages(unsigned int cpu) | |
1da177e4 | 917 | { |
c54ad30c | 918 | unsigned long flags; |
1da177e4 | 919 | struct zone *zone; |
1da177e4 | 920 | |
ee99c71c | 921 | for_each_populated_zone(zone) { |
1da177e4 | 922 | struct per_cpu_pageset *pset; |
3dfa5721 | 923 | struct per_cpu_pages *pcp; |
1da177e4 | 924 | |
e7c8d5c9 | 925 | pset = zone_pcp(zone, cpu); |
3dfa5721 CL |
926 | |
927 | pcp = &pset->pcp; | |
928 | local_irq_save(flags); | |
929 | free_pages_bulk(zone, pcp->count, &pcp->list, 0); | |
930 | pcp->count = 0; | |
931 | local_irq_restore(flags); | |
1da177e4 LT |
932 | } |
933 | } | |
1da177e4 | 934 | |
9f8f2172 CL |
935 | /* |
936 | * Spill all of this CPU's per-cpu pages back into the buddy allocator. | |
937 | */ | |
938 | void drain_local_pages(void *arg) | |
939 | { | |
940 | drain_pages(smp_processor_id()); | |
941 | } | |
942 | ||
943 | /* | |
944 | * Spill all the per-cpu pages from all CPUs back into the buddy allocator | |
945 | */ | |
946 | void drain_all_pages(void) | |
947 | { | |
15c8b6c1 | 948 | on_each_cpu(drain_local_pages, NULL, 1); |
9f8f2172 CL |
949 | } |
950 | ||
296699de | 951 | #ifdef CONFIG_HIBERNATION |
1da177e4 LT |
952 | |
953 | void mark_free_pages(struct zone *zone) | |
954 | { | |
f623f0db RW |
955 | unsigned long pfn, max_zone_pfn; |
956 | unsigned long flags; | |
b2a0ac88 | 957 | int order, t; |
1da177e4 LT |
958 | struct list_head *curr; |
959 | ||
960 | if (!zone->spanned_pages) | |
961 | return; | |
962 | ||
963 | spin_lock_irqsave(&zone->lock, flags); | |
f623f0db RW |
964 | |
965 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | |
966 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | |
967 | if (pfn_valid(pfn)) { | |
968 | struct page *page = pfn_to_page(pfn); | |
969 | ||
7be98234 RW |
970 | if (!swsusp_page_is_forbidden(page)) |
971 | swsusp_unset_page_free(page); | |
f623f0db | 972 | } |
1da177e4 | 973 | |
b2a0ac88 MG |
974 | for_each_migratetype_order(order, t) { |
975 | list_for_each(curr, &zone->free_area[order].free_list[t]) { | |
f623f0db | 976 | unsigned long i; |
1da177e4 | 977 | |
f623f0db RW |
978 | pfn = page_to_pfn(list_entry(curr, struct page, lru)); |
979 | for (i = 0; i < (1UL << order); i++) | |
7be98234 | 980 | swsusp_set_page_free(pfn_to_page(pfn + i)); |
f623f0db | 981 | } |
b2a0ac88 | 982 | } |
1da177e4 LT |
983 | spin_unlock_irqrestore(&zone->lock, flags); |
984 | } | |
e2c55dc8 | 985 | #endif /* CONFIG_PM */ |
1da177e4 | 986 | |
1da177e4 LT |
987 | /* |
988 | * Free a 0-order page | |
989 | */ | |
920c7a5d | 990 | static void free_hot_cold_page(struct page *page, int cold) |
1da177e4 LT |
991 | { |
992 | struct zone *zone = page_zone(page); | |
993 | struct per_cpu_pages *pcp; | |
994 | unsigned long flags; | |
995 | ||
1da177e4 LT |
996 | if (PageAnon(page)) |
997 | page->mapping = NULL; | |
224abf92 | 998 | if (free_pages_check(page)) |
689bcebf HD |
999 | return; |
1000 | ||
3ac7fe5a | 1001 | if (!PageHighMem(page)) { |
9858db50 | 1002 | debug_check_no_locks_freed(page_address(page), PAGE_SIZE); |
3ac7fe5a TG |
1003 | debug_check_no_obj_freed(page_address(page), PAGE_SIZE); |
1004 | } | |
dafb1367 | 1005 | arch_free_page(page, 0); |
689bcebf HD |
1006 | kernel_map_pages(page, 1, 0); |
1007 | ||
3dfa5721 | 1008 | pcp = &zone_pcp(zone, get_cpu())->pcp; |
1da177e4 | 1009 | local_irq_save(flags); |
f8891e5e | 1010 | __count_vm_event(PGFREE); |
3dfa5721 CL |
1011 | if (cold) |
1012 | list_add_tail(&page->lru, &pcp->list); | |
1013 | else | |
1014 | list_add(&page->lru, &pcp->list); | |
535131e6 | 1015 | set_page_private(page, get_pageblock_migratetype(page)); |
1da177e4 | 1016 | pcp->count++; |
48db57f8 NP |
1017 | if (pcp->count >= pcp->high) { |
1018 | free_pages_bulk(zone, pcp->batch, &pcp->list, 0); | |
1019 | pcp->count -= pcp->batch; | |
1020 | } | |
1da177e4 LT |
1021 | local_irq_restore(flags); |
1022 | put_cpu(); | |
1023 | } | |
1024 | ||
920c7a5d | 1025 | void free_hot_page(struct page *page) |
1da177e4 LT |
1026 | { |
1027 | free_hot_cold_page(page, 0); | |
1028 | } | |
1029 | ||
920c7a5d | 1030 | void free_cold_page(struct page *page) |
1da177e4 LT |
1031 | { |
1032 | free_hot_cold_page(page, 1); | |
1033 | } | |
1034 | ||
8dfcc9ba NP |
1035 | /* |
1036 | * split_page takes a non-compound higher-order page, and splits it into | |
1037 | * n (1<<order) sub-pages: page[0..n] | |
1038 | * Each sub-page must be freed individually. | |
1039 | * | |
1040 | * Note: this is probably too low level an operation for use in drivers. | |
1041 | * Please consult with lkml before using this in your driver. | |
1042 | */ | |
1043 | void split_page(struct page *page, unsigned int order) | |
1044 | { | |
1045 | int i; | |
1046 | ||
725d704e NP |
1047 | VM_BUG_ON(PageCompound(page)); |
1048 | VM_BUG_ON(!page_count(page)); | |
7835e98b NP |
1049 | for (i = 1; i < (1 << order); i++) |
1050 | set_page_refcounted(page + i); | |
8dfcc9ba | 1051 | } |
8dfcc9ba | 1052 | |
1da177e4 LT |
1053 | /* |
1054 | * Really, prep_compound_page() should be called from __rmqueue_bulk(). But | |
1055 | * we cheat by calling it from here, in the order > 0 path. Saves a branch | |
1056 | * or two. | |
1057 | */ | |
18ea7e71 | 1058 | static struct page *buffered_rmqueue(struct zone *preferred_zone, |
a74609fa | 1059 | struct zone *zone, int order, gfp_t gfp_flags) |
1da177e4 LT |
1060 | { |
1061 | unsigned long flags; | |
689bcebf | 1062 | struct page *page; |
1da177e4 | 1063 | int cold = !!(gfp_flags & __GFP_COLD); |
a74609fa | 1064 | int cpu; |
64c5e135 | 1065 | int migratetype = allocflags_to_migratetype(gfp_flags); |
1da177e4 | 1066 | |
689bcebf | 1067 | again: |
a74609fa | 1068 | cpu = get_cpu(); |
48db57f8 | 1069 | if (likely(order == 0)) { |
1da177e4 LT |
1070 | struct per_cpu_pages *pcp; |
1071 | ||
3dfa5721 | 1072 | pcp = &zone_pcp(zone, cpu)->pcp; |
1da177e4 | 1073 | local_irq_save(flags); |
a74609fa | 1074 | if (!pcp->count) { |
941c7105 | 1075 | pcp->count = rmqueue_bulk(zone, 0, |
b2a0ac88 | 1076 | pcp->batch, &pcp->list, migratetype); |
a74609fa NP |
1077 | if (unlikely(!pcp->count)) |
1078 | goto failed; | |
1da177e4 | 1079 | } |
b92a6edd | 1080 | |
535131e6 | 1081 | /* Find a page of the appropriate migrate type */ |
3dfa5721 CL |
1082 | if (cold) { |
1083 | list_for_each_entry_reverse(page, &pcp->list, lru) | |
1084 | if (page_private(page) == migratetype) | |
1085 | break; | |
1086 | } else { | |
1087 | list_for_each_entry(page, &pcp->list, lru) | |
1088 | if (page_private(page) == migratetype) | |
1089 | break; | |
1090 | } | |
535131e6 | 1091 | |
b92a6edd MG |
1092 | /* Allocate more to the pcp list if necessary */ |
1093 | if (unlikely(&page->lru == &pcp->list)) { | |
535131e6 MG |
1094 | pcp->count += rmqueue_bulk(zone, 0, |
1095 | pcp->batch, &pcp->list, migratetype); | |
1096 | page = list_entry(pcp->list.next, struct page, lru); | |
535131e6 | 1097 | } |
b92a6edd MG |
1098 | |
1099 | list_del(&page->lru); | |
1100 | pcp->count--; | |
7fb1d9fc | 1101 | } else { |
1da177e4 | 1102 | spin_lock_irqsave(&zone->lock, flags); |
b2a0ac88 | 1103 | page = __rmqueue(zone, order, migratetype); |
a74609fa NP |
1104 | spin_unlock(&zone->lock); |
1105 | if (!page) | |
1106 | goto failed; | |
1da177e4 LT |
1107 | } |
1108 | ||
f8891e5e | 1109 | __count_zone_vm_events(PGALLOC, zone, 1 << order); |
18ea7e71 | 1110 | zone_statistics(preferred_zone, zone); |
a74609fa NP |
1111 | local_irq_restore(flags); |
1112 | put_cpu(); | |
1da177e4 | 1113 | |
725d704e | 1114 | VM_BUG_ON(bad_range(zone, page)); |
17cf4406 | 1115 | if (prep_new_page(page, order, gfp_flags)) |
a74609fa | 1116 | goto again; |
1da177e4 | 1117 | return page; |
a74609fa NP |
1118 | |
1119 | failed: | |
1120 | local_irq_restore(flags); | |
1121 | put_cpu(); | |
1122 | return NULL; | |
1da177e4 LT |
1123 | } |
1124 | ||
7fb1d9fc | 1125 | #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */ |
3148890b NP |
1126 | #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */ |
1127 | #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */ | |
1128 | #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */ | |
1129 | #define ALLOC_HARDER 0x10 /* try to alloc harder */ | |
1130 | #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */ | |
1131 | #define ALLOC_CPUSET 0x40 /* check for correct cpuset */ | |
7fb1d9fc | 1132 | |
933e312e AM |
1133 | #ifdef CONFIG_FAIL_PAGE_ALLOC |
1134 | ||
1135 | static struct fail_page_alloc_attr { | |
1136 | struct fault_attr attr; | |
1137 | ||
1138 | u32 ignore_gfp_highmem; | |
1139 | u32 ignore_gfp_wait; | |
54114994 | 1140 | u32 min_order; |
933e312e AM |
1141 | |
1142 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS | |
1143 | ||
1144 | struct dentry *ignore_gfp_highmem_file; | |
1145 | struct dentry *ignore_gfp_wait_file; | |
54114994 | 1146 | struct dentry *min_order_file; |
933e312e AM |
1147 | |
1148 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ | |
1149 | ||
1150 | } fail_page_alloc = { | |
1151 | .attr = FAULT_ATTR_INITIALIZER, | |
6b1b60f4 DM |
1152 | .ignore_gfp_wait = 1, |
1153 | .ignore_gfp_highmem = 1, | |
54114994 | 1154 | .min_order = 1, |
933e312e AM |
1155 | }; |
1156 | ||
1157 | static int __init setup_fail_page_alloc(char *str) | |
1158 | { | |
1159 | return setup_fault_attr(&fail_page_alloc.attr, str); | |
1160 | } | |
1161 | __setup("fail_page_alloc=", setup_fail_page_alloc); | |
1162 | ||
1163 | static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) | |
1164 | { | |
54114994 AM |
1165 | if (order < fail_page_alloc.min_order) |
1166 | return 0; | |
933e312e AM |
1167 | if (gfp_mask & __GFP_NOFAIL) |
1168 | return 0; | |
1169 | if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM)) | |
1170 | return 0; | |
1171 | if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT)) | |
1172 | return 0; | |
1173 | ||
1174 | return should_fail(&fail_page_alloc.attr, 1 << order); | |
1175 | } | |
1176 | ||
1177 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS | |
1178 | ||
1179 | static int __init fail_page_alloc_debugfs(void) | |
1180 | { | |
1181 | mode_t mode = S_IFREG | S_IRUSR | S_IWUSR; | |
1182 | struct dentry *dir; | |
1183 | int err; | |
1184 | ||
1185 | err = init_fault_attr_dentries(&fail_page_alloc.attr, | |
1186 | "fail_page_alloc"); | |
1187 | if (err) | |
1188 | return err; | |
1189 | dir = fail_page_alloc.attr.dentries.dir; | |
1190 | ||
1191 | fail_page_alloc.ignore_gfp_wait_file = | |
1192 | debugfs_create_bool("ignore-gfp-wait", mode, dir, | |
1193 | &fail_page_alloc.ignore_gfp_wait); | |
1194 | ||
1195 | fail_page_alloc.ignore_gfp_highmem_file = | |
1196 | debugfs_create_bool("ignore-gfp-highmem", mode, dir, | |
1197 | &fail_page_alloc.ignore_gfp_highmem); | |
54114994 AM |
1198 | fail_page_alloc.min_order_file = |
1199 | debugfs_create_u32("min-order", mode, dir, | |
1200 | &fail_page_alloc.min_order); | |
933e312e AM |
1201 | |
1202 | if (!fail_page_alloc.ignore_gfp_wait_file || | |
54114994 AM |
1203 | !fail_page_alloc.ignore_gfp_highmem_file || |
1204 | !fail_page_alloc.min_order_file) { | |
933e312e AM |
1205 | err = -ENOMEM; |
1206 | debugfs_remove(fail_page_alloc.ignore_gfp_wait_file); | |
1207 | debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file); | |
54114994 | 1208 | debugfs_remove(fail_page_alloc.min_order_file); |
933e312e AM |
1209 | cleanup_fault_attr_dentries(&fail_page_alloc.attr); |
1210 | } | |
1211 | ||
1212 | return err; | |
1213 | } | |
1214 | ||
1215 | late_initcall(fail_page_alloc_debugfs); | |
1216 | ||
1217 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ | |
1218 | ||
1219 | #else /* CONFIG_FAIL_PAGE_ALLOC */ | |
1220 | ||
1221 | static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) | |
1222 | { | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | #endif /* CONFIG_FAIL_PAGE_ALLOC */ | |
1227 | ||
1da177e4 LT |
1228 | /* |
1229 | * Return 1 if free pages are above 'mark'. This takes into account the order | |
1230 | * of the allocation. | |
1231 | */ | |
1232 | int zone_watermark_ok(struct zone *z, int order, unsigned long mark, | |
7fb1d9fc | 1233 | int classzone_idx, int alloc_flags) |
1da177e4 LT |
1234 | { |
1235 | /* free_pages my go negative - that's OK */ | |
d23ad423 CL |
1236 | long min = mark; |
1237 | long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1; | |
1da177e4 LT |
1238 | int o; |
1239 | ||
7fb1d9fc | 1240 | if (alloc_flags & ALLOC_HIGH) |
1da177e4 | 1241 | min -= min / 2; |
7fb1d9fc | 1242 | if (alloc_flags & ALLOC_HARDER) |
1da177e4 LT |
1243 | min -= min / 4; |
1244 | ||
1245 | if (free_pages <= min + z->lowmem_reserve[classzone_idx]) | |
1246 | return 0; | |
1247 | for (o = 0; o < order; o++) { | |
1248 | /* At the next order, this order's pages become unavailable */ | |
1249 | free_pages -= z->free_area[o].nr_free << o; | |
1250 | ||
1251 | /* Require fewer higher order pages to be free */ | |
1252 | min >>= 1; | |
1253 | ||
1254 | if (free_pages <= min) | |
1255 | return 0; | |
1256 | } | |
1257 | return 1; | |
1258 | } | |
1259 | ||
9276b1bc PJ |
1260 | #ifdef CONFIG_NUMA |
1261 | /* | |
1262 | * zlc_setup - Setup for "zonelist cache". Uses cached zone data to | |
1263 | * skip over zones that are not allowed by the cpuset, or that have | |
1264 | * been recently (in last second) found to be nearly full. See further | |
1265 | * comments in mmzone.h. Reduces cache footprint of zonelist scans | |
183ff22b | 1266 | * that have to skip over a lot of full or unallowed zones. |
9276b1bc PJ |
1267 | * |
1268 | * If the zonelist cache is present in the passed in zonelist, then | |
1269 | * returns a pointer to the allowed node mask (either the current | |
37b07e41 | 1270 | * tasks mems_allowed, or node_states[N_HIGH_MEMORY].) |
9276b1bc PJ |
1271 | * |
1272 | * If the zonelist cache is not available for this zonelist, does | |
1273 | * nothing and returns NULL. | |
1274 | * | |
1275 | * If the fullzones BITMAP in the zonelist cache is stale (more than | |
1276 | * a second since last zap'd) then we zap it out (clear its bits.) | |
1277 | * | |
1278 | * We hold off even calling zlc_setup, until after we've checked the | |
1279 | * first zone in the zonelist, on the theory that most allocations will | |
1280 | * be satisfied from that first zone, so best to examine that zone as | |
1281 | * quickly as we can. | |
1282 | */ | |
1283 | static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags) | |
1284 | { | |
1285 | struct zonelist_cache *zlc; /* cached zonelist speedup info */ | |
1286 | nodemask_t *allowednodes; /* zonelist_cache approximation */ | |
1287 | ||
1288 | zlc = zonelist->zlcache_ptr; | |
1289 | if (!zlc) | |
1290 | return NULL; | |
1291 | ||
f05111f5 | 1292 | if (time_after(jiffies, zlc->last_full_zap + HZ)) { |
9276b1bc PJ |
1293 | bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST); |
1294 | zlc->last_full_zap = jiffies; | |
1295 | } | |
1296 | ||
1297 | allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ? | |
1298 | &cpuset_current_mems_allowed : | |
37b07e41 | 1299 | &node_states[N_HIGH_MEMORY]; |
9276b1bc PJ |
1300 | return allowednodes; |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * Given 'z' scanning a zonelist, run a couple of quick checks to see | |
1305 | * if it is worth looking at further for free memory: | |
1306 | * 1) Check that the zone isn't thought to be full (doesn't have its | |
1307 | * bit set in the zonelist_cache fullzones BITMAP). | |
1308 | * 2) Check that the zones node (obtained from the zonelist_cache | |
1309 | * z_to_n[] mapping) is allowed in the passed in allowednodes mask. | |
1310 | * Return true (non-zero) if zone is worth looking at further, or | |
1311 | * else return false (zero) if it is not. | |
1312 | * | |
1313 | * This check -ignores- the distinction between various watermarks, | |
1314 | * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is | |
1315 | * found to be full for any variation of these watermarks, it will | |
1316 | * be considered full for up to one second by all requests, unless | |
1317 | * we are so low on memory on all allowed nodes that we are forced | |
1318 | * into the second scan of the zonelist. | |
1319 | * | |
1320 | * In the second scan we ignore this zonelist cache and exactly | |
1321 | * apply the watermarks to all zones, even it is slower to do so. | |
1322 | * We are low on memory in the second scan, and should leave no stone | |
1323 | * unturned looking for a free page. | |
1324 | */ | |
dd1a239f | 1325 | static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z, |
9276b1bc PJ |
1326 | nodemask_t *allowednodes) |
1327 | { | |
1328 | struct zonelist_cache *zlc; /* cached zonelist speedup info */ | |
1329 | int i; /* index of *z in zonelist zones */ | |
1330 | int n; /* node that zone *z is on */ | |
1331 | ||
1332 | zlc = zonelist->zlcache_ptr; | |
1333 | if (!zlc) | |
1334 | return 1; | |
1335 | ||
dd1a239f | 1336 | i = z - zonelist->_zonerefs; |
9276b1bc PJ |
1337 | n = zlc->z_to_n[i]; |
1338 | ||
1339 | /* This zone is worth trying if it is allowed but not full */ | |
1340 | return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones); | |
1341 | } | |
1342 | ||
1343 | /* | |
1344 | * Given 'z' scanning a zonelist, set the corresponding bit in | |
1345 | * zlc->fullzones, so that subsequent attempts to allocate a page | |
1346 | * from that zone don't waste time re-examining it. | |
1347 | */ | |
dd1a239f | 1348 | static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z) |
9276b1bc PJ |
1349 | { |
1350 | struct zonelist_cache *zlc; /* cached zonelist speedup info */ | |
1351 | int i; /* index of *z in zonelist zones */ | |
1352 | ||
1353 | zlc = zonelist->zlcache_ptr; | |
1354 | if (!zlc) | |
1355 | return; | |
1356 | ||
dd1a239f | 1357 | i = z - zonelist->_zonerefs; |
9276b1bc PJ |
1358 | |
1359 | set_bit(i, zlc->fullzones); | |
1360 | } | |
1361 | ||
1362 | #else /* CONFIG_NUMA */ | |
1363 | ||
1364 | static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags) | |
1365 | { | |
1366 | return NULL; | |
1367 | } | |
1368 | ||
dd1a239f | 1369 | static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z, |
9276b1bc PJ |
1370 | nodemask_t *allowednodes) |
1371 | { | |
1372 | return 1; | |
1373 | } | |
1374 | ||
dd1a239f | 1375 | static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z) |
9276b1bc PJ |
1376 | { |
1377 | } | |
1378 | #endif /* CONFIG_NUMA */ | |
1379 | ||
7fb1d9fc | 1380 | /* |
0798e519 | 1381 | * get_page_from_freelist goes through the zonelist trying to allocate |
7fb1d9fc RS |
1382 | * a page. |
1383 | */ | |
1384 | static struct page * | |
19770b32 | 1385 | get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order, |
54a6eb5c | 1386 | struct zonelist *zonelist, int high_zoneidx, int alloc_flags) |
753ee728 | 1387 | { |
dd1a239f | 1388 | struct zoneref *z; |
7fb1d9fc | 1389 | struct page *page = NULL; |
54a6eb5c | 1390 | int classzone_idx; |
18ea7e71 | 1391 | struct zone *zone, *preferred_zone; |
9276b1bc PJ |
1392 | nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */ |
1393 | int zlc_active = 0; /* set if using zonelist_cache */ | |
1394 | int did_zlc_setup = 0; /* just call zlc_setup() one time */ | |
54a6eb5c | 1395 | |
19770b32 MG |
1396 | (void)first_zones_zonelist(zonelist, high_zoneidx, nodemask, |
1397 | &preferred_zone); | |
7eb54824 AW |
1398 | if (!preferred_zone) |
1399 | return NULL; | |
1400 | ||
19770b32 | 1401 | classzone_idx = zone_idx(preferred_zone); |
7fb1d9fc | 1402 | |
9276b1bc | 1403 | zonelist_scan: |
7fb1d9fc | 1404 | /* |
9276b1bc | 1405 | * Scan zonelist, looking for a zone with enough free. |
7fb1d9fc RS |
1406 | * See also cpuset_zone_allowed() comment in kernel/cpuset.c. |
1407 | */ | |
19770b32 MG |
1408 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
1409 | high_zoneidx, nodemask) { | |
9276b1bc PJ |
1410 | if (NUMA_BUILD && zlc_active && |
1411 | !zlc_zone_worth_trying(zonelist, z, allowednodes)) | |
1412 | continue; | |
7fb1d9fc | 1413 | if ((alloc_flags & ALLOC_CPUSET) && |
02a0e53d | 1414 | !cpuset_zone_allowed_softwall(zone, gfp_mask)) |
9276b1bc | 1415 | goto try_next_zone; |
7fb1d9fc RS |
1416 | |
1417 | if (!(alloc_flags & ALLOC_NO_WATERMARKS)) { | |
3148890b NP |
1418 | unsigned long mark; |
1419 | if (alloc_flags & ALLOC_WMARK_MIN) | |
1192d526 | 1420 | mark = zone->pages_min; |
3148890b | 1421 | else if (alloc_flags & ALLOC_WMARK_LOW) |
1192d526 | 1422 | mark = zone->pages_low; |
3148890b | 1423 | else |
1192d526 | 1424 | mark = zone->pages_high; |
0798e519 PJ |
1425 | if (!zone_watermark_ok(zone, order, mark, |
1426 | classzone_idx, alloc_flags)) { | |
9eeff239 | 1427 | if (!zone_reclaim_mode || |
1192d526 | 1428 | !zone_reclaim(zone, gfp_mask, order)) |
9276b1bc | 1429 | goto this_zone_full; |
0798e519 | 1430 | } |
7fb1d9fc RS |
1431 | } |
1432 | ||
18ea7e71 | 1433 | page = buffered_rmqueue(preferred_zone, zone, order, gfp_mask); |
0798e519 | 1434 | if (page) |
7fb1d9fc | 1435 | break; |
9276b1bc PJ |
1436 | this_zone_full: |
1437 | if (NUMA_BUILD) | |
1438 | zlc_mark_zone_full(zonelist, z); | |
1439 | try_next_zone: | |
1440 | if (NUMA_BUILD && !did_zlc_setup) { | |
1441 | /* we do zlc_setup after the first zone is tried */ | |
1442 | allowednodes = zlc_setup(zonelist, alloc_flags); | |
1443 | zlc_active = 1; | |
1444 | did_zlc_setup = 1; | |
1445 | } | |
54a6eb5c | 1446 | } |
9276b1bc PJ |
1447 | |
1448 | if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) { | |
1449 | /* Disable zlc cache for second zonelist scan */ | |
1450 | zlc_active = 0; | |
1451 | goto zonelist_scan; | |
1452 | } | |
7fb1d9fc | 1453 | return page; |
753ee728 MH |
1454 | } |
1455 | ||
1da177e4 LT |
1456 | /* |
1457 | * This is the 'heart' of the zoned buddy allocator. | |
1458 | */ | |
e4048e5d | 1459 | struct page * |
19770b32 MG |
1460 | __alloc_pages_internal(gfp_t gfp_mask, unsigned int order, |
1461 | struct zonelist *zonelist, nodemask_t *nodemask) | |
1da177e4 | 1462 | { |
260b2367 | 1463 | const gfp_t wait = gfp_mask & __GFP_WAIT; |
54a6eb5c | 1464 | enum zone_type high_zoneidx = gfp_zone(gfp_mask); |
dd1a239f MG |
1465 | struct zoneref *z; |
1466 | struct zone *zone; | |
1da177e4 LT |
1467 | struct page *page; |
1468 | struct reclaim_state reclaim_state; | |
1469 | struct task_struct *p = current; | |
1da177e4 | 1470 | int do_retry; |
7fb1d9fc | 1471 | int alloc_flags; |
a41f24ea NA |
1472 | unsigned long did_some_progress; |
1473 | unsigned long pages_reclaimed = 0; | |
1da177e4 | 1474 | |
cf40bd16 NP |
1475 | lockdep_trace_alloc(gfp_mask); |
1476 | ||
1da177e4 LT |
1477 | might_sleep_if(wait); |
1478 | ||
933e312e AM |
1479 | if (should_fail_alloc_page(gfp_mask, order)) |
1480 | return NULL; | |
1481 | ||
6b1de916 | 1482 | restart: |
dd1a239f | 1483 | z = zonelist->_zonerefs; /* the list of zones suitable for gfp_mask */ |
1da177e4 | 1484 | |
dd1a239f | 1485 | if (unlikely(!z->zone)) { |
523b9458 CL |
1486 | /* |
1487 | * Happens if we have an empty zonelist as a result of | |
1488 | * GFP_THISNODE being used on a memoryless node | |
1489 | */ | |
1da177e4 LT |
1490 | return NULL; |
1491 | } | |
6b1de916 | 1492 | |
19770b32 | 1493 | page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order, |
54a6eb5c | 1494 | zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET); |
7fb1d9fc RS |
1495 | if (page) |
1496 | goto got_pg; | |
1da177e4 | 1497 | |
952f3b51 CL |
1498 | /* |
1499 | * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and | |
1500 | * __GFP_NOWARN set) should not cause reclaim since the subsystem | |
1501 | * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim | |
1502 | * using a larger set of nodes after it has established that the | |
1503 | * allowed per node queues are empty and that nodes are | |
1504 | * over allocated. | |
1505 | */ | |
1506 | if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE) | |
1507 | goto nopage; | |
1508 | ||
dd1a239f MG |
1509 | for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) |
1510 | wakeup_kswapd(zone, order); | |
1da177e4 | 1511 | |
9bf2229f | 1512 | /* |
7fb1d9fc RS |
1513 | * OK, we're below the kswapd watermark and have kicked background |
1514 | * reclaim. Now things get more complex, so set up alloc_flags according | |
1515 | * to how we want to proceed. | |
1516 | * | |
1517 | * The caller may dip into page reserves a bit more if the caller | |
1518 | * cannot run direct reclaim, or if the caller has realtime scheduling | |
4eac915d PJ |
1519 | * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will |
1520 | * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH). | |
9bf2229f | 1521 | */ |
3148890b | 1522 | alloc_flags = ALLOC_WMARK_MIN; |
7fb1d9fc RS |
1523 | if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait) |
1524 | alloc_flags |= ALLOC_HARDER; | |
1525 | if (gfp_mask & __GFP_HIGH) | |
1526 | alloc_flags |= ALLOC_HIGH; | |
bdd804f4 PJ |
1527 | if (wait) |
1528 | alloc_flags |= ALLOC_CPUSET; | |
1da177e4 LT |
1529 | |
1530 | /* | |
1531 | * Go through the zonelist again. Let __GFP_HIGH and allocations | |
7fb1d9fc | 1532 | * coming from realtime tasks go deeper into reserves. |
1da177e4 LT |
1533 | * |
1534 | * This is the last chance, in general, before the goto nopage. | |
1535 | * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc. | |
9bf2229f | 1536 | * See also cpuset_zone_allowed() comment in kernel/cpuset.c. |
1da177e4 | 1537 | */ |
19770b32 | 1538 | page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist, |
54a6eb5c | 1539 | high_zoneidx, alloc_flags); |
7fb1d9fc RS |
1540 | if (page) |
1541 | goto got_pg; | |
1da177e4 LT |
1542 | |
1543 | /* This allocation should allow future memory freeing. */ | |
b84a35be | 1544 | |
b43a57bb | 1545 | rebalance: |
b84a35be NP |
1546 | if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE))) |
1547 | && !in_interrupt()) { | |
1548 | if (!(gfp_mask & __GFP_NOMEMALLOC)) { | |
885036d3 | 1549 | nofail_alloc: |
b84a35be | 1550 | /* go through the zonelist yet again, ignoring mins */ |
19770b32 | 1551 | page = get_page_from_freelist(gfp_mask, nodemask, order, |
54a6eb5c | 1552 | zonelist, high_zoneidx, ALLOC_NO_WATERMARKS); |
7fb1d9fc RS |
1553 | if (page) |
1554 | goto got_pg; | |
885036d3 | 1555 | if (gfp_mask & __GFP_NOFAIL) { |
3fcfab16 | 1556 | congestion_wait(WRITE, HZ/50); |
885036d3 KK |
1557 | goto nofail_alloc; |
1558 | } | |
1da177e4 LT |
1559 | } |
1560 | goto nopage; | |
1561 | } | |
1562 | ||
1563 | /* Atomic allocations - we can't balance anything */ | |
1564 | if (!wait) | |
1565 | goto nopage; | |
1566 | ||
1da177e4 LT |
1567 | cond_resched(); |
1568 | ||
1569 | /* We now go into synchronous reclaim */ | |
3e0d98b9 | 1570 | cpuset_memory_pressure_bump(); |
e33c3b5e DR |
1571 | /* |
1572 | * The task's cpuset might have expanded its set of allowable nodes | |
1573 | */ | |
1574 | cpuset_update_task_memory_state(); | |
1da177e4 | 1575 | p->flags |= PF_MEMALLOC; |
cf40bd16 NP |
1576 | |
1577 | lockdep_set_current_reclaim_state(gfp_mask); | |
1da177e4 LT |
1578 | reclaim_state.reclaimed_slab = 0; |
1579 | p->reclaim_state = &reclaim_state; | |
1580 | ||
327c0e96 KH |
1581 | did_some_progress = try_to_free_pages(zonelist, order, |
1582 | gfp_mask, nodemask); | |
1da177e4 LT |
1583 | |
1584 | p->reclaim_state = NULL; | |
cf40bd16 | 1585 | lockdep_clear_current_reclaim_state(); |
1da177e4 LT |
1586 | p->flags &= ~PF_MEMALLOC; |
1587 | ||
1588 | cond_resched(); | |
1589 | ||
e2c55dc8 | 1590 | if (order != 0) |
9f8f2172 | 1591 | drain_all_pages(); |
e2c55dc8 | 1592 | |
1da177e4 | 1593 | if (likely(did_some_progress)) { |
19770b32 | 1594 | page = get_page_from_freelist(gfp_mask, nodemask, order, |
54a6eb5c | 1595 | zonelist, high_zoneidx, alloc_flags); |
7fb1d9fc RS |
1596 | if (page) |
1597 | goto got_pg; | |
1da177e4 | 1598 | } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) { |
dd1a239f | 1599 | if (!try_set_zone_oom(zonelist, gfp_mask)) { |
ff0ceb9d DR |
1600 | schedule_timeout_uninterruptible(1); |
1601 | goto restart; | |
1602 | } | |
1603 | ||
1da177e4 LT |
1604 | /* |
1605 | * Go through the zonelist yet one more time, keep | |
1606 | * very high watermark here, this is only to catch | |
1607 | * a parallel oom killing, we must fail if we're still | |
1608 | * under heavy pressure. | |
1609 | */ | |
19770b32 MG |
1610 | page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, |
1611 | order, zonelist, high_zoneidx, | |
1612 | ALLOC_WMARK_HIGH|ALLOC_CPUSET); | |
ff0ceb9d | 1613 | if (page) { |
dd1a239f | 1614 | clear_zonelist_oom(zonelist, gfp_mask); |
7fb1d9fc | 1615 | goto got_pg; |
ff0ceb9d | 1616 | } |
1da177e4 | 1617 | |
a8bbf72a | 1618 | /* The OOM killer will not help higher order allocs so fail */ |
ff0ceb9d | 1619 | if (order > PAGE_ALLOC_COSTLY_ORDER) { |
dd1a239f | 1620 | clear_zonelist_oom(zonelist, gfp_mask); |
a8bbf72a | 1621 | goto nopage; |
ff0ceb9d | 1622 | } |
a8bbf72a | 1623 | |
9b0f8b04 | 1624 | out_of_memory(zonelist, gfp_mask, order); |
dd1a239f | 1625 | clear_zonelist_oom(zonelist, gfp_mask); |
1da177e4 LT |
1626 | goto restart; |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * Don't let big-order allocations loop unless the caller explicitly | |
1631 | * requests that. Wait for some write requests to complete then retry. | |
1632 | * | |
a41f24ea NA |
1633 | * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER |
1634 | * means __GFP_NOFAIL, but that may not be true in other | |
ab857d09 | 1635 | * implementations. |
a41f24ea NA |
1636 | * |
1637 | * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is | |
1638 | * specified, then we retry until we no longer reclaim any pages | |
1639 | * (above), or we've reclaimed an order of pages at least as | |
1640 | * large as the allocation's order. In both cases, if the | |
1641 | * allocation still fails, we stop retrying. | |
1da177e4 | 1642 | */ |
a41f24ea | 1643 | pages_reclaimed += did_some_progress; |
1da177e4 LT |
1644 | do_retry = 0; |
1645 | if (!(gfp_mask & __GFP_NORETRY)) { | |
a41f24ea | 1646 | if (order <= PAGE_ALLOC_COSTLY_ORDER) { |
1da177e4 | 1647 | do_retry = 1; |
a41f24ea NA |
1648 | } else { |
1649 | if (gfp_mask & __GFP_REPEAT && | |
1650 | pages_reclaimed < (1 << order)) | |
1651 | do_retry = 1; | |
1652 | } | |
1da177e4 LT |
1653 | if (gfp_mask & __GFP_NOFAIL) |
1654 | do_retry = 1; | |
1655 | } | |
1656 | if (do_retry) { | |
3fcfab16 | 1657 | congestion_wait(WRITE, HZ/50); |
1da177e4 LT |
1658 | goto rebalance; |
1659 | } | |
1660 | ||
1661 | nopage: | |
1662 | if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) { | |
1663 | printk(KERN_WARNING "%s: page allocation failure." | |
1664 | " order:%d, mode:0x%x\n", | |
1665 | p->comm, order, gfp_mask); | |
1666 | dump_stack(); | |
578c2fd6 | 1667 | show_mem(); |
1da177e4 | 1668 | } |
1da177e4 | 1669 | got_pg: |
1da177e4 LT |
1670 | return page; |
1671 | } | |
e4048e5d | 1672 | EXPORT_SYMBOL(__alloc_pages_internal); |
1da177e4 LT |
1673 | |
1674 | /* | |
1675 | * Common helper functions. | |
1676 | */ | |
920c7a5d | 1677 | unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) |
1da177e4 LT |
1678 | { |
1679 | struct page * page; | |
1680 | page = alloc_pages(gfp_mask, order); | |
1681 | if (!page) | |
1682 | return 0; | |
1683 | return (unsigned long) page_address(page); | |
1684 | } | |
1685 | ||
1686 | EXPORT_SYMBOL(__get_free_pages); | |
1687 | ||
920c7a5d | 1688 | unsigned long get_zeroed_page(gfp_t gfp_mask) |
1da177e4 LT |
1689 | { |
1690 | struct page * page; | |
1691 | ||
1692 | /* | |
1693 | * get_zeroed_page() returns a 32-bit address, which cannot represent | |
1694 | * a highmem page | |
1695 | */ | |
725d704e | 1696 | VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0); |
1da177e4 LT |
1697 | |
1698 | page = alloc_pages(gfp_mask | __GFP_ZERO, 0); | |
1699 | if (page) | |
1700 | return (unsigned long) page_address(page); | |
1701 | return 0; | |
1702 | } | |
1703 | ||
1704 | EXPORT_SYMBOL(get_zeroed_page); | |
1705 | ||
1706 | void __pagevec_free(struct pagevec *pvec) | |
1707 | { | |
1708 | int i = pagevec_count(pvec); | |
1709 | ||
1710 | while (--i >= 0) | |
1711 | free_hot_cold_page(pvec->pages[i], pvec->cold); | |
1712 | } | |
1713 | ||
920c7a5d | 1714 | void __free_pages(struct page *page, unsigned int order) |
1da177e4 | 1715 | { |
b5810039 | 1716 | if (put_page_testzero(page)) { |
1da177e4 LT |
1717 | if (order == 0) |
1718 | free_hot_page(page); | |
1719 | else | |
1720 | __free_pages_ok(page, order); | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | EXPORT_SYMBOL(__free_pages); | |
1725 | ||
920c7a5d | 1726 | void free_pages(unsigned long addr, unsigned int order) |
1da177e4 LT |
1727 | { |
1728 | if (addr != 0) { | |
725d704e | 1729 | VM_BUG_ON(!virt_addr_valid((void *)addr)); |
1da177e4 LT |
1730 | __free_pages(virt_to_page((void *)addr), order); |
1731 | } | |
1732 | } | |
1733 | ||
1734 | EXPORT_SYMBOL(free_pages); | |
1735 | ||
2be0ffe2 TT |
1736 | /** |
1737 | * alloc_pages_exact - allocate an exact number physically-contiguous pages. | |
1738 | * @size: the number of bytes to allocate | |
1739 | * @gfp_mask: GFP flags for the allocation | |
1740 | * | |
1741 | * This function is similar to alloc_pages(), except that it allocates the | |
1742 | * minimum number of pages to satisfy the request. alloc_pages() can only | |
1743 | * allocate memory in power-of-two pages. | |
1744 | * | |
1745 | * This function is also limited by MAX_ORDER. | |
1746 | * | |
1747 | * Memory allocated by this function must be released by free_pages_exact(). | |
1748 | */ | |
1749 | void *alloc_pages_exact(size_t size, gfp_t gfp_mask) | |
1750 | { | |
1751 | unsigned int order = get_order(size); | |
1752 | unsigned long addr; | |
1753 | ||
1754 | addr = __get_free_pages(gfp_mask, order); | |
1755 | if (addr) { | |
1756 | unsigned long alloc_end = addr + (PAGE_SIZE << order); | |
1757 | unsigned long used = addr + PAGE_ALIGN(size); | |
1758 | ||
1759 | split_page(virt_to_page(addr), order); | |
1760 | while (used < alloc_end) { | |
1761 | free_page(used); | |
1762 | used += PAGE_SIZE; | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | return (void *)addr; | |
1767 | } | |
1768 | EXPORT_SYMBOL(alloc_pages_exact); | |
1769 | ||
1770 | /** | |
1771 | * free_pages_exact - release memory allocated via alloc_pages_exact() | |
1772 | * @virt: the value returned by alloc_pages_exact. | |
1773 | * @size: size of allocation, same value as passed to alloc_pages_exact(). | |
1774 | * | |
1775 | * Release the memory allocated by a previous call to alloc_pages_exact. | |
1776 | */ | |
1777 | void free_pages_exact(void *virt, size_t size) | |
1778 | { | |
1779 | unsigned long addr = (unsigned long)virt; | |
1780 | unsigned long end = addr + PAGE_ALIGN(size); | |
1781 | ||
1782 | while (addr < end) { | |
1783 | free_page(addr); | |
1784 | addr += PAGE_SIZE; | |
1785 | } | |
1786 | } | |
1787 | EXPORT_SYMBOL(free_pages_exact); | |
1788 | ||
1da177e4 LT |
1789 | static unsigned int nr_free_zone_pages(int offset) |
1790 | { | |
dd1a239f | 1791 | struct zoneref *z; |
54a6eb5c MG |
1792 | struct zone *zone; |
1793 | ||
e310fd43 | 1794 | /* Just pick one node, since fallback list is circular */ |
1da177e4 LT |
1795 | unsigned int sum = 0; |
1796 | ||
0e88460d | 1797 | struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL); |
1da177e4 | 1798 | |
54a6eb5c | 1799 | for_each_zone_zonelist(zone, z, zonelist, offset) { |
e310fd43 MB |
1800 | unsigned long size = zone->present_pages; |
1801 | unsigned long high = zone->pages_high; | |
1802 | if (size > high) | |
1803 | sum += size - high; | |
1da177e4 LT |
1804 | } |
1805 | ||
1806 | return sum; | |
1807 | } | |
1808 | ||
1809 | /* | |
1810 | * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL | |
1811 | */ | |
1812 | unsigned int nr_free_buffer_pages(void) | |
1813 | { | |
af4ca457 | 1814 | return nr_free_zone_pages(gfp_zone(GFP_USER)); |
1da177e4 | 1815 | } |
c2f1a551 | 1816 | EXPORT_SYMBOL_GPL(nr_free_buffer_pages); |
1da177e4 LT |
1817 | |
1818 | /* | |
1819 | * Amount of free RAM allocatable within all zones | |
1820 | */ | |
1821 | unsigned int nr_free_pagecache_pages(void) | |
1822 | { | |
2a1e274a | 1823 | return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE)); |
1da177e4 | 1824 | } |
08e0f6a9 CL |
1825 | |
1826 | static inline void show_node(struct zone *zone) | |
1da177e4 | 1827 | { |
08e0f6a9 | 1828 | if (NUMA_BUILD) |
25ba77c1 | 1829 | printk("Node %d ", zone_to_nid(zone)); |
1da177e4 | 1830 | } |
1da177e4 | 1831 | |
1da177e4 LT |
1832 | void si_meminfo(struct sysinfo *val) |
1833 | { | |
1834 | val->totalram = totalram_pages; | |
1835 | val->sharedram = 0; | |
d23ad423 | 1836 | val->freeram = global_page_state(NR_FREE_PAGES); |
1da177e4 | 1837 | val->bufferram = nr_blockdev_pages(); |
1da177e4 LT |
1838 | val->totalhigh = totalhigh_pages; |
1839 | val->freehigh = nr_free_highpages(); | |
1da177e4 LT |
1840 | val->mem_unit = PAGE_SIZE; |
1841 | } | |
1842 | ||
1843 | EXPORT_SYMBOL(si_meminfo); | |
1844 | ||
1845 | #ifdef CONFIG_NUMA | |
1846 | void si_meminfo_node(struct sysinfo *val, int nid) | |
1847 | { | |
1848 | pg_data_t *pgdat = NODE_DATA(nid); | |
1849 | ||
1850 | val->totalram = pgdat->node_present_pages; | |
d23ad423 | 1851 | val->freeram = node_page_state(nid, NR_FREE_PAGES); |
98d2b0eb | 1852 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 1853 | val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages; |
d23ad423 CL |
1854 | val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM], |
1855 | NR_FREE_PAGES); | |
98d2b0eb CL |
1856 | #else |
1857 | val->totalhigh = 0; | |
1858 | val->freehigh = 0; | |
1859 | #endif | |
1da177e4 LT |
1860 | val->mem_unit = PAGE_SIZE; |
1861 | } | |
1862 | #endif | |
1863 | ||
1864 | #define K(x) ((x) << (PAGE_SHIFT-10)) | |
1865 | ||
1866 | /* | |
1867 | * Show free area list (used inside shift_scroll-lock stuff) | |
1868 | * We also calculate the percentage fragmentation. We do this by counting the | |
1869 | * memory on each free list with the exception of the first item on the list. | |
1870 | */ | |
1871 | void show_free_areas(void) | |
1872 | { | |
c7241913 | 1873 | int cpu; |
1da177e4 LT |
1874 | struct zone *zone; |
1875 | ||
ee99c71c | 1876 | for_each_populated_zone(zone) { |
c7241913 JS |
1877 | show_node(zone); |
1878 | printk("%s per-cpu:\n", zone->name); | |
1da177e4 | 1879 | |
6b482c67 | 1880 | for_each_online_cpu(cpu) { |
1da177e4 LT |
1881 | struct per_cpu_pageset *pageset; |
1882 | ||
e7c8d5c9 | 1883 | pageset = zone_pcp(zone, cpu); |
1da177e4 | 1884 | |
3dfa5721 CL |
1885 | printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n", |
1886 | cpu, pageset->pcp.high, | |
1887 | pageset->pcp.batch, pageset->pcp.count); | |
1da177e4 LT |
1888 | } |
1889 | } | |
1890 | ||
7b854121 LS |
1891 | printk("Active_anon:%lu active_file:%lu inactive_anon:%lu\n" |
1892 | " inactive_file:%lu" | |
1893 | //TODO: check/adjust line lengths | |
1894 | #ifdef CONFIG_UNEVICTABLE_LRU | |
1895 | " unevictable:%lu" | |
1896 | #endif | |
1897 | " dirty:%lu writeback:%lu unstable:%lu\n" | |
d23ad423 | 1898 | " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n", |
4f98a2fe RR |
1899 | global_page_state(NR_ACTIVE_ANON), |
1900 | global_page_state(NR_ACTIVE_FILE), | |
1901 | global_page_state(NR_INACTIVE_ANON), | |
1902 | global_page_state(NR_INACTIVE_FILE), | |
7b854121 LS |
1903 | #ifdef CONFIG_UNEVICTABLE_LRU |
1904 | global_page_state(NR_UNEVICTABLE), | |
1905 | #endif | |
b1e7a8fd | 1906 | global_page_state(NR_FILE_DIRTY), |
ce866b34 | 1907 | global_page_state(NR_WRITEBACK), |
fd39fc85 | 1908 | global_page_state(NR_UNSTABLE_NFS), |
d23ad423 | 1909 | global_page_state(NR_FREE_PAGES), |
972d1a7b CL |
1910 | global_page_state(NR_SLAB_RECLAIMABLE) + |
1911 | global_page_state(NR_SLAB_UNRECLAIMABLE), | |
65ba55f5 | 1912 | global_page_state(NR_FILE_MAPPED), |
a25700a5 AM |
1913 | global_page_state(NR_PAGETABLE), |
1914 | global_page_state(NR_BOUNCE)); | |
1da177e4 | 1915 | |
ee99c71c | 1916 | for_each_populated_zone(zone) { |
1da177e4 LT |
1917 | int i; |
1918 | ||
1919 | show_node(zone); | |
1920 | printk("%s" | |
1921 | " free:%lukB" | |
1922 | " min:%lukB" | |
1923 | " low:%lukB" | |
1924 | " high:%lukB" | |
4f98a2fe RR |
1925 | " active_anon:%lukB" |
1926 | " inactive_anon:%lukB" | |
1927 | " active_file:%lukB" | |
1928 | " inactive_file:%lukB" | |
7b854121 LS |
1929 | #ifdef CONFIG_UNEVICTABLE_LRU |
1930 | " unevictable:%lukB" | |
1931 | #endif | |
1da177e4 LT |
1932 | " present:%lukB" |
1933 | " pages_scanned:%lu" | |
1934 | " all_unreclaimable? %s" | |
1935 | "\n", | |
1936 | zone->name, | |
d23ad423 | 1937 | K(zone_page_state(zone, NR_FREE_PAGES)), |
1da177e4 LT |
1938 | K(zone->pages_min), |
1939 | K(zone->pages_low), | |
1940 | K(zone->pages_high), | |
4f98a2fe RR |
1941 | K(zone_page_state(zone, NR_ACTIVE_ANON)), |
1942 | K(zone_page_state(zone, NR_INACTIVE_ANON)), | |
1943 | K(zone_page_state(zone, NR_ACTIVE_FILE)), | |
1944 | K(zone_page_state(zone, NR_INACTIVE_FILE)), | |
7b854121 LS |
1945 | #ifdef CONFIG_UNEVICTABLE_LRU |
1946 | K(zone_page_state(zone, NR_UNEVICTABLE)), | |
1947 | #endif | |
1da177e4 LT |
1948 | K(zone->present_pages), |
1949 | zone->pages_scanned, | |
e815af95 | 1950 | (zone_is_all_unreclaimable(zone) ? "yes" : "no") |
1da177e4 LT |
1951 | ); |
1952 | printk("lowmem_reserve[]:"); | |
1953 | for (i = 0; i < MAX_NR_ZONES; i++) | |
1954 | printk(" %lu", zone->lowmem_reserve[i]); | |
1955 | printk("\n"); | |
1956 | } | |
1957 | ||
ee99c71c | 1958 | for_each_populated_zone(zone) { |
8f9de51a | 1959 | unsigned long nr[MAX_ORDER], flags, order, total = 0; |
1da177e4 LT |
1960 | |
1961 | show_node(zone); | |
1962 | printk("%s: ", zone->name); | |
1da177e4 LT |
1963 | |
1964 | spin_lock_irqsave(&zone->lock, flags); | |
1965 | for (order = 0; order < MAX_ORDER; order++) { | |
8f9de51a KK |
1966 | nr[order] = zone->free_area[order].nr_free; |
1967 | total += nr[order] << order; | |
1da177e4 LT |
1968 | } |
1969 | spin_unlock_irqrestore(&zone->lock, flags); | |
8f9de51a KK |
1970 | for (order = 0; order < MAX_ORDER; order++) |
1971 | printk("%lu*%lukB ", nr[order], K(1UL) << order); | |
1da177e4 LT |
1972 | printk("= %lukB\n", K(total)); |
1973 | } | |
1974 | ||
e6f3602d LW |
1975 | printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES)); |
1976 | ||
1da177e4 LT |
1977 | show_swap_cache_info(); |
1978 | } | |
1979 | ||
19770b32 MG |
1980 | static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref) |
1981 | { | |
1982 | zoneref->zone = zone; | |
1983 | zoneref->zone_idx = zone_idx(zone); | |
1984 | } | |
1985 | ||
1da177e4 LT |
1986 | /* |
1987 | * Builds allocation fallback zone lists. | |
1a93205b CL |
1988 | * |
1989 | * Add all populated zones of a node to the zonelist. | |
1da177e4 | 1990 | */ |
f0c0b2b8 KH |
1991 | static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, |
1992 | int nr_zones, enum zone_type zone_type) | |
1da177e4 | 1993 | { |
1a93205b CL |
1994 | struct zone *zone; |
1995 | ||
98d2b0eb | 1996 | BUG_ON(zone_type >= MAX_NR_ZONES); |
2f6726e5 | 1997 | zone_type++; |
02a68a5e CL |
1998 | |
1999 | do { | |
2f6726e5 | 2000 | zone_type--; |
070f8032 | 2001 | zone = pgdat->node_zones + zone_type; |
1a93205b | 2002 | if (populated_zone(zone)) { |
dd1a239f MG |
2003 | zoneref_set_zone(zone, |
2004 | &zonelist->_zonerefs[nr_zones++]); | |
070f8032 | 2005 | check_highest_zone(zone_type); |
1da177e4 | 2006 | } |
02a68a5e | 2007 | |
2f6726e5 | 2008 | } while (zone_type); |
070f8032 | 2009 | return nr_zones; |
1da177e4 LT |
2010 | } |
2011 | ||
f0c0b2b8 KH |
2012 | |
2013 | /* | |
2014 | * zonelist_order: | |
2015 | * 0 = automatic detection of better ordering. | |
2016 | * 1 = order by ([node] distance, -zonetype) | |
2017 | * 2 = order by (-zonetype, [node] distance) | |
2018 | * | |
2019 | * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create | |
2020 | * the same zonelist. So only NUMA can configure this param. | |
2021 | */ | |
2022 | #define ZONELIST_ORDER_DEFAULT 0 | |
2023 | #define ZONELIST_ORDER_NODE 1 | |
2024 | #define ZONELIST_ORDER_ZONE 2 | |
2025 | ||
2026 | /* zonelist order in the kernel. | |
2027 | * set_zonelist_order() will set this to NODE or ZONE. | |
2028 | */ | |
2029 | static int current_zonelist_order = ZONELIST_ORDER_DEFAULT; | |
2030 | static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"}; | |
2031 | ||
2032 | ||
1da177e4 | 2033 | #ifdef CONFIG_NUMA |
f0c0b2b8 KH |
2034 | /* The value user specified ....changed by config */ |
2035 | static int user_zonelist_order = ZONELIST_ORDER_DEFAULT; | |
2036 | /* string for sysctl */ | |
2037 | #define NUMA_ZONELIST_ORDER_LEN 16 | |
2038 | char numa_zonelist_order[16] = "default"; | |
2039 | ||
2040 | /* | |
2041 | * interface for configure zonelist ordering. | |
2042 | * command line option "numa_zonelist_order" | |
2043 | * = "[dD]efault - default, automatic configuration. | |
2044 | * = "[nN]ode - order by node locality, then by zone within node | |
2045 | * = "[zZ]one - order by zone, then by locality within zone | |
2046 | */ | |
2047 | ||
2048 | static int __parse_numa_zonelist_order(char *s) | |
2049 | { | |
2050 | if (*s == 'd' || *s == 'D') { | |
2051 | user_zonelist_order = ZONELIST_ORDER_DEFAULT; | |
2052 | } else if (*s == 'n' || *s == 'N') { | |
2053 | user_zonelist_order = ZONELIST_ORDER_NODE; | |
2054 | } else if (*s == 'z' || *s == 'Z') { | |
2055 | user_zonelist_order = ZONELIST_ORDER_ZONE; | |
2056 | } else { | |
2057 | printk(KERN_WARNING | |
2058 | "Ignoring invalid numa_zonelist_order value: " | |
2059 | "%s\n", s); | |
2060 | return -EINVAL; | |
2061 | } | |
2062 | return 0; | |
2063 | } | |
2064 | ||
2065 | static __init int setup_numa_zonelist_order(char *s) | |
2066 | { | |
2067 | if (s) | |
2068 | return __parse_numa_zonelist_order(s); | |
2069 | return 0; | |
2070 | } | |
2071 | early_param("numa_zonelist_order", setup_numa_zonelist_order); | |
2072 | ||
2073 | /* | |
2074 | * sysctl handler for numa_zonelist_order | |
2075 | */ | |
2076 | int numa_zonelist_order_handler(ctl_table *table, int write, | |
2077 | struct file *file, void __user *buffer, size_t *length, | |
2078 | loff_t *ppos) | |
2079 | { | |
2080 | char saved_string[NUMA_ZONELIST_ORDER_LEN]; | |
2081 | int ret; | |
2082 | ||
2083 | if (write) | |
2084 | strncpy(saved_string, (char*)table->data, | |
2085 | NUMA_ZONELIST_ORDER_LEN); | |
2086 | ret = proc_dostring(table, write, file, buffer, length, ppos); | |
2087 | if (ret) | |
2088 | return ret; | |
2089 | if (write) { | |
2090 | int oldval = user_zonelist_order; | |
2091 | if (__parse_numa_zonelist_order((char*)table->data)) { | |
2092 | /* | |
2093 | * bogus value. restore saved string | |
2094 | */ | |
2095 | strncpy((char*)table->data, saved_string, | |
2096 | NUMA_ZONELIST_ORDER_LEN); | |
2097 | user_zonelist_order = oldval; | |
2098 | } else if (oldval != user_zonelist_order) | |
2099 | build_all_zonelists(); | |
2100 | } | |
2101 | return 0; | |
2102 | } | |
2103 | ||
2104 | ||
1da177e4 | 2105 | #define MAX_NODE_LOAD (num_online_nodes()) |
f0c0b2b8 KH |
2106 | static int node_load[MAX_NUMNODES]; |
2107 | ||
1da177e4 | 2108 | /** |
4dc3b16b | 2109 | * find_next_best_node - find the next node that should appear in a given node's fallback list |
1da177e4 LT |
2110 | * @node: node whose fallback list we're appending |
2111 | * @used_node_mask: nodemask_t of already used nodes | |
2112 | * | |
2113 | * We use a number of factors to determine which is the next node that should | |
2114 | * appear on a given node's fallback list. The node should not have appeared | |
2115 | * already in @node's fallback list, and it should be the next closest node | |
2116 | * according to the distance array (which contains arbitrary distance values | |
2117 | * from each node to each node in the system), and should also prefer nodes | |
2118 | * with no CPUs, since presumably they'll have very little allocation pressure | |
2119 | * on them otherwise. | |
2120 | * It returns -1 if no node is found. | |
2121 | */ | |
f0c0b2b8 | 2122 | static int find_next_best_node(int node, nodemask_t *used_node_mask) |
1da177e4 | 2123 | { |
4cf808eb | 2124 | int n, val; |
1da177e4 LT |
2125 | int min_val = INT_MAX; |
2126 | int best_node = -1; | |
a70f7302 | 2127 | const struct cpumask *tmp = cpumask_of_node(0); |
1da177e4 | 2128 | |
4cf808eb LT |
2129 | /* Use the local node if we haven't already */ |
2130 | if (!node_isset(node, *used_node_mask)) { | |
2131 | node_set(node, *used_node_mask); | |
2132 | return node; | |
2133 | } | |
1da177e4 | 2134 | |
37b07e41 | 2135 | for_each_node_state(n, N_HIGH_MEMORY) { |
1da177e4 LT |
2136 | |
2137 | /* Don't want a node to appear more than once */ | |
2138 | if (node_isset(n, *used_node_mask)) | |
2139 | continue; | |
2140 | ||
1da177e4 LT |
2141 | /* Use the distance array to find the distance */ |
2142 | val = node_distance(node, n); | |
2143 | ||
4cf808eb LT |
2144 | /* Penalize nodes under us ("prefer the next node") */ |
2145 | val += (n < node); | |
2146 | ||
1da177e4 | 2147 | /* Give preference to headless and unused nodes */ |
a70f7302 RR |
2148 | tmp = cpumask_of_node(n); |
2149 | if (!cpumask_empty(tmp)) | |
1da177e4 LT |
2150 | val += PENALTY_FOR_NODE_WITH_CPUS; |
2151 | ||
2152 | /* Slight preference for less loaded node */ | |
2153 | val *= (MAX_NODE_LOAD*MAX_NUMNODES); | |
2154 | val += node_load[n]; | |
2155 | ||
2156 | if (val < min_val) { | |
2157 | min_val = val; | |
2158 | best_node = n; | |
2159 | } | |
2160 | } | |
2161 | ||
2162 | if (best_node >= 0) | |
2163 | node_set(best_node, *used_node_mask); | |
2164 | ||
2165 | return best_node; | |
2166 | } | |
2167 | ||
f0c0b2b8 KH |
2168 | |
2169 | /* | |
2170 | * Build zonelists ordered by node and zones within node. | |
2171 | * This results in maximum locality--normal zone overflows into local | |
2172 | * DMA zone, if any--but risks exhausting DMA zone. | |
2173 | */ | |
2174 | static void build_zonelists_in_node_order(pg_data_t *pgdat, int node) | |
1da177e4 | 2175 | { |
f0c0b2b8 | 2176 | int j; |
1da177e4 | 2177 | struct zonelist *zonelist; |
f0c0b2b8 | 2178 | |
54a6eb5c | 2179 | zonelist = &pgdat->node_zonelists[0]; |
dd1a239f | 2180 | for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++) |
54a6eb5c MG |
2181 | ; |
2182 | j = build_zonelists_node(NODE_DATA(node), zonelist, j, | |
2183 | MAX_NR_ZONES - 1); | |
dd1a239f MG |
2184 | zonelist->_zonerefs[j].zone = NULL; |
2185 | zonelist->_zonerefs[j].zone_idx = 0; | |
f0c0b2b8 KH |
2186 | } |
2187 | ||
523b9458 CL |
2188 | /* |
2189 | * Build gfp_thisnode zonelists | |
2190 | */ | |
2191 | static void build_thisnode_zonelists(pg_data_t *pgdat) | |
2192 | { | |
523b9458 CL |
2193 | int j; |
2194 | struct zonelist *zonelist; | |
2195 | ||
54a6eb5c MG |
2196 | zonelist = &pgdat->node_zonelists[1]; |
2197 | j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1); | |
dd1a239f MG |
2198 | zonelist->_zonerefs[j].zone = NULL; |
2199 | zonelist->_zonerefs[j].zone_idx = 0; | |
523b9458 CL |
2200 | } |
2201 | ||
f0c0b2b8 KH |
2202 | /* |
2203 | * Build zonelists ordered by zone and nodes within zones. | |
2204 | * This results in conserving DMA zone[s] until all Normal memory is | |
2205 | * exhausted, but results in overflowing to remote node while memory | |
2206 | * may still exist in local DMA zone. | |
2207 | */ | |
2208 | static int node_order[MAX_NUMNODES]; | |
2209 | ||
2210 | static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes) | |
2211 | { | |
f0c0b2b8 KH |
2212 | int pos, j, node; |
2213 | int zone_type; /* needs to be signed */ | |
2214 | struct zone *z; | |
2215 | struct zonelist *zonelist; | |
2216 | ||
54a6eb5c MG |
2217 | zonelist = &pgdat->node_zonelists[0]; |
2218 | pos = 0; | |
2219 | for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) { | |
2220 | for (j = 0; j < nr_nodes; j++) { | |
2221 | node = node_order[j]; | |
2222 | z = &NODE_DATA(node)->node_zones[zone_type]; | |
2223 | if (populated_zone(z)) { | |
dd1a239f MG |
2224 | zoneref_set_zone(z, |
2225 | &zonelist->_zonerefs[pos++]); | |
54a6eb5c | 2226 | check_highest_zone(zone_type); |
f0c0b2b8 KH |
2227 | } |
2228 | } | |
f0c0b2b8 | 2229 | } |
dd1a239f MG |
2230 | zonelist->_zonerefs[pos].zone = NULL; |
2231 | zonelist->_zonerefs[pos].zone_idx = 0; | |
f0c0b2b8 KH |
2232 | } |
2233 | ||
2234 | static int default_zonelist_order(void) | |
2235 | { | |
2236 | int nid, zone_type; | |
2237 | unsigned long low_kmem_size,total_size; | |
2238 | struct zone *z; | |
2239 | int average_size; | |
2240 | /* | |
2241 | * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem. | |
2242 | * If they are really small and used heavily, the system can fall | |
2243 | * into OOM very easily. | |
2244 | * This function detect ZONE_DMA/DMA32 size and confgigures zone order. | |
2245 | */ | |
2246 | /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */ | |
2247 | low_kmem_size = 0; | |
2248 | total_size = 0; | |
2249 | for_each_online_node(nid) { | |
2250 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) { | |
2251 | z = &NODE_DATA(nid)->node_zones[zone_type]; | |
2252 | if (populated_zone(z)) { | |
2253 | if (zone_type < ZONE_NORMAL) | |
2254 | low_kmem_size += z->present_pages; | |
2255 | total_size += z->present_pages; | |
2256 | } | |
2257 | } | |
2258 | } | |
2259 | if (!low_kmem_size || /* there are no DMA area. */ | |
2260 | low_kmem_size > total_size/2) /* DMA/DMA32 is big. */ | |
2261 | return ZONELIST_ORDER_NODE; | |
2262 | /* | |
2263 | * look into each node's config. | |
2264 | * If there is a node whose DMA/DMA32 memory is very big area on | |
2265 | * local memory, NODE_ORDER may be suitable. | |
2266 | */ | |
37b07e41 LS |
2267 | average_size = total_size / |
2268 | (nodes_weight(node_states[N_HIGH_MEMORY]) + 1); | |
f0c0b2b8 KH |
2269 | for_each_online_node(nid) { |
2270 | low_kmem_size = 0; | |
2271 | total_size = 0; | |
2272 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) { | |
2273 | z = &NODE_DATA(nid)->node_zones[zone_type]; | |
2274 | if (populated_zone(z)) { | |
2275 | if (zone_type < ZONE_NORMAL) | |
2276 | low_kmem_size += z->present_pages; | |
2277 | total_size += z->present_pages; | |
2278 | } | |
2279 | } | |
2280 | if (low_kmem_size && | |
2281 | total_size > average_size && /* ignore small node */ | |
2282 | low_kmem_size > total_size * 70/100) | |
2283 | return ZONELIST_ORDER_NODE; | |
2284 | } | |
2285 | return ZONELIST_ORDER_ZONE; | |
2286 | } | |
2287 | ||
2288 | static void set_zonelist_order(void) | |
2289 | { | |
2290 | if (user_zonelist_order == ZONELIST_ORDER_DEFAULT) | |
2291 | current_zonelist_order = default_zonelist_order(); | |
2292 | else | |
2293 | current_zonelist_order = user_zonelist_order; | |
2294 | } | |
2295 | ||
2296 | static void build_zonelists(pg_data_t *pgdat) | |
2297 | { | |
2298 | int j, node, load; | |
2299 | enum zone_type i; | |
1da177e4 | 2300 | nodemask_t used_mask; |
f0c0b2b8 KH |
2301 | int local_node, prev_node; |
2302 | struct zonelist *zonelist; | |
2303 | int order = current_zonelist_order; | |
1da177e4 LT |
2304 | |
2305 | /* initialize zonelists */ | |
523b9458 | 2306 | for (i = 0; i < MAX_ZONELISTS; i++) { |
1da177e4 | 2307 | zonelist = pgdat->node_zonelists + i; |
dd1a239f MG |
2308 | zonelist->_zonerefs[0].zone = NULL; |
2309 | zonelist->_zonerefs[0].zone_idx = 0; | |
1da177e4 LT |
2310 | } |
2311 | ||
2312 | /* NUMA-aware ordering of nodes */ | |
2313 | local_node = pgdat->node_id; | |
2314 | load = num_online_nodes(); | |
2315 | prev_node = local_node; | |
2316 | nodes_clear(used_mask); | |
f0c0b2b8 KH |
2317 | |
2318 | memset(node_load, 0, sizeof(node_load)); | |
2319 | memset(node_order, 0, sizeof(node_order)); | |
2320 | j = 0; | |
2321 | ||
1da177e4 | 2322 | while ((node = find_next_best_node(local_node, &used_mask)) >= 0) { |
9eeff239 CL |
2323 | int distance = node_distance(local_node, node); |
2324 | ||
2325 | /* | |
2326 | * If another node is sufficiently far away then it is better | |
2327 | * to reclaim pages in a zone before going off node. | |
2328 | */ | |
2329 | if (distance > RECLAIM_DISTANCE) | |
2330 | zone_reclaim_mode = 1; | |
2331 | ||
1da177e4 LT |
2332 | /* |
2333 | * We don't want to pressure a particular node. | |
2334 | * So adding penalty to the first node in same | |
2335 | * distance group to make it round-robin. | |
2336 | */ | |
9eeff239 | 2337 | if (distance != node_distance(local_node, prev_node)) |
f0c0b2b8 KH |
2338 | node_load[node] = load; |
2339 | ||
1da177e4 LT |
2340 | prev_node = node; |
2341 | load--; | |
f0c0b2b8 KH |
2342 | if (order == ZONELIST_ORDER_NODE) |
2343 | build_zonelists_in_node_order(pgdat, node); | |
2344 | else | |
2345 | node_order[j++] = node; /* remember order */ | |
2346 | } | |
1da177e4 | 2347 | |
f0c0b2b8 KH |
2348 | if (order == ZONELIST_ORDER_ZONE) { |
2349 | /* calculate node order -- i.e., DMA last! */ | |
2350 | build_zonelists_in_zone_order(pgdat, j); | |
1da177e4 | 2351 | } |
523b9458 CL |
2352 | |
2353 | build_thisnode_zonelists(pgdat); | |
1da177e4 LT |
2354 | } |
2355 | ||
9276b1bc | 2356 | /* Construct the zonelist performance cache - see further mmzone.h */ |
f0c0b2b8 | 2357 | static void build_zonelist_cache(pg_data_t *pgdat) |
9276b1bc | 2358 | { |
54a6eb5c MG |
2359 | struct zonelist *zonelist; |
2360 | struct zonelist_cache *zlc; | |
dd1a239f | 2361 | struct zoneref *z; |
9276b1bc | 2362 | |
54a6eb5c MG |
2363 | zonelist = &pgdat->node_zonelists[0]; |
2364 | zonelist->zlcache_ptr = zlc = &zonelist->zlcache; | |
2365 | bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST); | |
dd1a239f MG |
2366 | for (z = zonelist->_zonerefs; z->zone; z++) |
2367 | zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z); | |
9276b1bc PJ |
2368 | } |
2369 | ||
f0c0b2b8 | 2370 | |
1da177e4 LT |
2371 | #else /* CONFIG_NUMA */ |
2372 | ||
f0c0b2b8 KH |
2373 | static void set_zonelist_order(void) |
2374 | { | |
2375 | current_zonelist_order = ZONELIST_ORDER_ZONE; | |
2376 | } | |
2377 | ||
2378 | static void build_zonelists(pg_data_t *pgdat) | |
1da177e4 | 2379 | { |
19655d34 | 2380 | int node, local_node; |
54a6eb5c MG |
2381 | enum zone_type j; |
2382 | struct zonelist *zonelist; | |
1da177e4 LT |
2383 | |
2384 | local_node = pgdat->node_id; | |
1da177e4 | 2385 | |
54a6eb5c MG |
2386 | zonelist = &pgdat->node_zonelists[0]; |
2387 | j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1); | |
1da177e4 | 2388 | |
54a6eb5c MG |
2389 | /* |
2390 | * Now we build the zonelist so that it contains the zones | |
2391 | * of all the other nodes. | |
2392 | * We don't want to pressure a particular node, so when | |
2393 | * building the zones for node N, we make sure that the | |
2394 | * zones coming right after the local ones are those from | |
2395 | * node N+1 (modulo N) | |
2396 | */ | |
2397 | for (node = local_node + 1; node < MAX_NUMNODES; node++) { | |
2398 | if (!node_online(node)) | |
2399 | continue; | |
2400 | j = build_zonelists_node(NODE_DATA(node), zonelist, j, | |
2401 | MAX_NR_ZONES - 1); | |
1da177e4 | 2402 | } |
54a6eb5c MG |
2403 | for (node = 0; node < local_node; node++) { |
2404 | if (!node_online(node)) | |
2405 | continue; | |
2406 | j = build_zonelists_node(NODE_DATA(node), zonelist, j, | |
2407 | MAX_NR_ZONES - 1); | |
2408 | } | |
2409 | ||
dd1a239f MG |
2410 | zonelist->_zonerefs[j].zone = NULL; |
2411 | zonelist->_zonerefs[j].zone_idx = 0; | |
1da177e4 LT |
2412 | } |
2413 | ||
9276b1bc | 2414 | /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */ |
f0c0b2b8 | 2415 | static void build_zonelist_cache(pg_data_t *pgdat) |
9276b1bc | 2416 | { |
54a6eb5c | 2417 | pgdat->node_zonelists[0].zlcache_ptr = NULL; |
9276b1bc PJ |
2418 | } |
2419 | ||
1da177e4 LT |
2420 | #endif /* CONFIG_NUMA */ |
2421 | ||
9b1a4d38 | 2422 | /* return values int ....just for stop_machine() */ |
f0c0b2b8 | 2423 | static int __build_all_zonelists(void *dummy) |
1da177e4 | 2424 | { |
6811378e | 2425 | int nid; |
9276b1bc PJ |
2426 | |
2427 | for_each_online_node(nid) { | |
7ea1530a CL |
2428 | pg_data_t *pgdat = NODE_DATA(nid); |
2429 | ||
2430 | build_zonelists(pgdat); | |
2431 | build_zonelist_cache(pgdat); | |
9276b1bc | 2432 | } |
6811378e YG |
2433 | return 0; |
2434 | } | |
2435 | ||
f0c0b2b8 | 2436 | void build_all_zonelists(void) |
6811378e | 2437 | { |
f0c0b2b8 KH |
2438 | set_zonelist_order(); |
2439 | ||
6811378e | 2440 | if (system_state == SYSTEM_BOOTING) { |
423b41d7 | 2441 | __build_all_zonelists(NULL); |
68ad8df4 | 2442 | mminit_verify_zonelist(); |
6811378e YG |
2443 | cpuset_init_current_mems_allowed(); |
2444 | } else { | |
183ff22b | 2445 | /* we have to stop all cpus to guarantee there is no user |
6811378e | 2446 | of zonelist */ |
9b1a4d38 | 2447 | stop_machine(__build_all_zonelists, NULL, NULL); |
6811378e YG |
2448 | /* cpuset refresh routine should be here */ |
2449 | } | |
bd1e22b8 | 2450 | vm_total_pages = nr_free_pagecache_pages(); |
9ef9acb0 MG |
2451 | /* |
2452 | * Disable grouping by mobility if the number of pages in the | |
2453 | * system is too low to allow the mechanism to work. It would be | |
2454 | * more accurate, but expensive to check per-zone. This check is | |
2455 | * made on memory-hotadd so a system can start with mobility | |
2456 | * disabled and enable it later | |
2457 | */ | |
d9c23400 | 2458 | if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES)) |
9ef9acb0 MG |
2459 | page_group_by_mobility_disabled = 1; |
2460 | else | |
2461 | page_group_by_mobility_disabled = 0; | |
2462 | ||
2463 | printk("Built %i zonelists in %s order, mobility grouping %s. " | |
2464 | "Total pages: %ld\n", | |
f0c0b2b8 KH |
2465 | num_online_nodes(), |
2466 | zonelist_order_name[current_zonelist_order], | |
9ef9acb0 | 2467 | page_group_by_mobility_disabled ? "off" : "on", |
f0c0b2b8 KH |
2468 | vm_total_pages); |
2469 | #ifdef CONFIG_NUMA | |
2470 | printk("Policy zone: %s\n", zone_names[policy_zone]); | |
2471 | #endif | |
1da177e4 LT |
2472 | } |
2473 | ||
2474 | /* | |
2475 | * Helper functions to size the waitqueue hash table. | |
2476 | * Essentially these want to choose hash table sizes sufficiently | |
2477 | * large so that collisions trying to wait on pages are rare. | |
2478 | * But in fact, the number of active page waitqueues on typical | |
2479 | * systems is ridiculously low, less than 200. So this is even | |
2480 | * conservative, even though it seems large. | |
2481 | * | |
2482 | * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to | |
2483 | * waitqueues, i.e. the size of the waitq table given the number of pages. | |
2484 | */ | |
2485 | #define PAGES_PER_WAITQUEUE 256 | |
2486 | ||
cca448fe | 2487 | #ifndef CONFIG_MEMORY_HOTPLUG |
02b694de | 2488 | static inline unsigned long wait_table_hash_nr_entries(unsigned long pages) |
1da177e4 LT |
2489 | { |
2490 | unsigned long size = 1; | |
2491 | ||
2492 | pages /= PAGES_PER_WAITQUEUE; | |
2493 | ||
2494 | while (size < pages) | |
2495 | size <<= 1; | |
2496 | ||
2497 | /* | |
2498 | * Once we have dozens or even hundreds of threads sleeping | |
2499 | * on IO we've got bigger problems than wait queue collision. | |
2500 | * Limit the size of the wait table to a reasonable size. | |
2501 | */ | |
2502 | size = min(size, 4096UL); | |
2503 | ||
2504 | return max(size, 4UL); | |
2505 | } | |
cca448fe YG |
2506 | #else |
2507 | /* | |
2508 | * A zone's size might be changed by hot-add, so it is not possible to determine | |
2509 | * a suitable size for its wait_table. So we use the maximum size now. | |
2510 | * | |
2511 | * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie: | |
2512 | * | |
2513 | * i386 (preemption config) : 4096 x 16 = 64Kbyte. | |
2514 | * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte. | |
2515 | * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte. | |
2516 | * | |
2517 | * The maximum entries are prepared when a zone's memory is (512K + 256) pages | |
2518 | * or more by the traditional way. (See above). It equals: | |
2519 | * | |
2520 | * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte. | |
2521 | * ia64(16K page size) : = ( 8G + 4M)byte. | |
2522 | * powerpc (64K page size) : = (32G +16M)byte. | |
2523 | */ | |
2524 | static inline unsigned long wait_table_hash_nr_entries(unsigned long pages) | |
2525 | { | |
2526 | return 4096UL; | |
2527 | } | |
2528 | #endif | |
1da177e4 LT |
2529 | |
2530 | /* | |
2531 | * This is an integer logarithm so that shifts can be used later | |
2532 | * to extract the more random high bits from the multiplicative | |
2533 | * hash function before the remainder is taken. | |
2534 | */ | |
2535 | static inline unsigned long wait_table_bits(unsigned long size) | |
2536 | { | |
2537 | return ffz(~size); | |
2538 | } | |
2539 | ||
2540 | #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1)) | |
2541 | ||
56fd56b8 | 2542 | /* |
d9c23400 | 2543 | * Mark a number of pageblocks as MIGRATE_RESERVE. The number |
56fd56b8 MG |
2544 | * of blocks reserved is based on zone->pages_min. The memory within the |
2545 | * reserve will tend to store contiguous free pages. Setting min_free_kbytes | |
2546 | * higher will lead to a bigger reserve which will get freed as contiguous | |
2547 | * blocks as reclaim kicks in | |
2548 | */ | |
2549 | static void setup_zone_migrate_reserve(struct zone *zone) | |
2550 | { | |
2551 | unsigned long start_pfn, pfn, end_pfn; | |
2552 | struct page *page; | |
2553 | unsigned long reserve, block_migratetype; | |
2554 | ||
2555 | /* Get the start pfn, end pfn and the number of blocks to reserve */ | |
2556 | start_pfn = zone->zone_start_pfn; | |
2557 | end_pfn = start_pfn + zone->spanned_pages; | |
d9c23400 MG |
2558 | reserve = roundup(zone->pages_min, pageblock_nr_pages) >> |
2559 | pageblock_order; | |
56fd56b8 | 2560 | |
d9c23400 | 2561 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
56fd56b8 MG |
2562 | if (!pfn_valid(pfn)) |
2563 | continue; | |
2564 | page = pfn_to_page(pfn); | |
2565 | ||
344c790e AL |
2566 | /* Watch out for overlapping nodes */ |
2567 | if (page_to_nid(page) != zone_to_nid(zone)) | |
2568 | continue; | |
2569 | ||
56fd56b8 MG |
2570 | /* Blocks with reserved pages will never free, skip them. */ |
2571 | if (PageReserved(page)) | |
2572 | continue; | |
2573 | ||
2574 | block_migratetype = get_pageblock_migratetype(page); | |
2575 | ||
2576 | /* If this block is reserved, account for it */ | |
2577 | if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) { | |
2578 | reserve--; | |
2579 | continue; | |
2580 | } | |
2581 | ||
2582 | /* Suitable for reserving if this block is movable */ | |
2583 | if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) { | |
2584 | set_pageblock_migratetype(page, MIGRATE_RESERVE); | |
2585 | move_freepages_block(zone, page, MIGRATE_RESERVE); | |
2586 | reserve--; | |
2587 | continue; | |
2588 | } | |
2589 | ||
2590 | /* | |
2591 | * If the reserve is met and this is a previous reserved block, | |
2592 | * take it back | |
2593 | */ | |
2594 | if (block_migratetype == MIGRATE_RESERVE) { | |
2595 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); | |
2596 | move_freepages_block(zone, page, MIGRATE_MOVABLE); | |
2597 | } | |
2598 | } | |
2599 | } | |
ac0e5b7a | 2600 | |
1da177e4 LT |
2601 | /* |
2602 | * Initially all pages are reserved - free ones are freed | |
2603 | * up by free_all_bootmem() once the early boot process is | |
2604 | * done. Non-atomic initialization, single-pass. | |
2605 | */ | |
c09b4240 | 2606 | void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, |
a2f3aa02 | 2607 | unsigned long start_pfn, enum memmap_context context) |
1da177e4 | 2608 | { |
1da177e4 | 2609 | struct page *page; |
29751f69 AW |
2610 | unsigned long end_pfn = start_pfn + size; |
2611 | unsigned long pfn; | |
86051ca5 | 2612 | struct zone *z; |
1da177e4 | 2613 | |
22b31eec HD |
2614 | if (highest_memmap_pfn < end_pfn - 1) |
2615 | highest_memmap_pfn = end_pfn - 1; | |
2616 | ||
86051ca5 | 2617 | z = &NODE_DATA(nid)->node_zones[zone]; |
cbe8dd4a | 2618 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
a2f3aa02 DH |
2619 | /* |
2620 | * There can be holes in boot-time mem_map[]s | |
2621 | * handed to this function. They do not | |
2622 | * exist on hotplugged memory. | |
2623 | */ | |
2624 | if (context == MEMMAP_EARLY) { | |
2625 | if (!early_pfn_valid(pfn)) | |
2626 | continue; | |
2627 | if (!early_pfn_in_nid(pfn, nid)) | |
2628 | continue; | |
2629 | } | |
d41dee36 AW |
2630 | page = pfn_to_page(pfn); |
2631 | set_page_links(page, zone, nid, pfn); | |
708614e6 | 2632 | mminit_verify_page_links(page, zone, nid, pfn); |
7835e98b | 2633 | init_page_count(page); |
1da177e4 LT |
2634 | reset_page_mapcount(page); |
2635 | SetPageReserved(page); | |
b2a0ac88 MG |
2636 | /* |
2637 | * Mark the block movable so that blocks are reserved for | |
2638 | * movable at startup. This will force kernel allocations | |
2639 | * to reserve their blocks rather than leaking throughout | |
2640 | * the address space during boot when many long-lived | |
56fd56b8 MG |
2641 | * kernel allocations are made. Later some blocks near |
2642 | * the start are marked MIGRATE_RESERVE by | |
2643 | * setup_zone_migrate_reserve() | |
86051ca5 KH |
2644 | * |
2645 | * bitmap is created for zone's valid pfn range. but memmap | |
2646 | * can be created for invalid pages (for alignment) | |
2647 | * check here not to call set_pageblock_migratetype() against | |
2648 | * pfn out of zone. | |
b2a0ac88 | 2649 | */ |
86051ca5 KH |
2650 | if ((z->zone_start_pfn <= pfn) |
2651 | && (pfn < z->zone_start_pfn + z->spanned_pages) | |
2652 | && !(pfn & (pageblock_nr_pages - 1))) | |
56fd56b8 | 2653 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
b2a0ac88 | 2654 | |
1da177e4 LT |
2655 | INIT_LIST_HEAD(&page->lru); |
2656 | #ifdef WANT_PAGE_VIRTUAL | |
2657 | /* The shift won't overflow because ZONE_NORMAL is below 4G. */ | |
2658 | if (!is_highmem_idx(zone)) | |
3212c6be | 2659 | set_page_address(page, __va(pfn << PAGE_SHIFT)); |
1da177e4 | 2660 | #endif |
1da177e4 LT |
2661 | } |
2662 | } | |
2663 | ||
1e548deb | 2664 | static void __meminit zone_init_free_lists(struct zone *zone) |
1da177e4 | 2665 | { |
b2a0ac88 MG |
2666 | int order, t; |
2667 | for_each_migratetype_order(order, t) { | |
2668 | INIT_LIST_HEAD(&zone->free_area[order].free_list[t]); | |
1da177e4 LT |
2669 | zone->free_area[order].nr_free = 0; |
2670 | } | |
2671 | } | |
2672 | ||
2673 | #ifndef __HAVE_ARCH_MEMMAP_INIT | |
2674 | #define memmap_init(size, nid, zone, start_pfn) \ | |
a2f3aa02 | 2675 | memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY) |
1da177e4 LT |
2676 | #endif |
2677 | ||
1d6f4e60 | 2678 | static int zone_batchsize(struct zone *zone) |
e7c8d5c9 | 2679 | { |
3a6be87f | 2680 | #ifdef CONFIG_MMU |
e7c8d5c9 CL |
2681 | int batch; |
2682 | ||
2683 | /* | |
2684 | * The per-cpu-pages pools are set to around 1000th of the | |
ba56e91c | 2685 | * size of the zone. But no more than 1/2 of a meg. |
e7c8d5c9 CL |
2686 | * |
2687 | * OK, so we don't know how big the cache is. So guess. | |
2688 | */ | |
2689 | batch = zone->present_pages / 1024; | |
ba56e91c SR |
2690 | if (batch * PAGE_SIZE > 512 * 1024) |
2691 | batch = (512 * 1024) / PAGE_SIZE; | |
e7c8d5c9 CL |
2692 | batch /= 4; /* We effectively *= 4 below */ |
2693 | if (batch < 1) | |
2694 | batch = 1; | |
2695 | ||
2696 | /* | |
0ceaacc9 NP |
2697 | * Clamp the batch to a 2^n - 1 value. Having a power |
2698 | * of 2 value was found to be more likely to have | |
2699 | * suboptimal cache aliasing properties in some cases. | |
e7c8d5c9 | 2700 | * |
0ceaacc9 NP |
2701 | * For example if 2 tasks are alternately allocating |
2702 | * batches of pages, one task can end up with a lot | |
2703 | * of pages of one half of the possible page colors | |
2704 | * and the other with pages of the other colors. | |
e7c8d5c9 | 2705 | */ |
9155203a | 2706 | batch = rounddown_pow_of_two(batch + batch/2) - 1; |
ba56e91c | 2707 | |
e7c8d5c9 | 2708 | return batch; |
3a6be87f DH |
2709 | |
2710 | #else | |
2711 | /* The deferral and batching of frees should be suppressed under NOMMU | |
2712 | * conditions. | |
2713 | * | |
2714 | * The problem is that NOMMU needs to be able to allocate large chunks | |
2715 | * of contiguous memory as there's no hardware page translation to | |
2716 | * assemble apparent contiguous memory from discontiguous pages. | |
2717 | * | |
2718 | * Queueing large contiguous runs of pages for batching, however, | |
2719 | * causes the pages to actually be freed in smaller chunks. As there | |
2720 | * can be a significant delay between the individual batches being | |
2721 | * recycled, this leads to the once large chunks of space being | |
2722 | * fragmented and becoming unavailable for high-order allocations. | |
2723 | */ | |
2724 | return 0; | |
2725 | #endif | |
e7c8d5c9 CL |
2726 | } |
2727 | ||
b69a7288 | 2728 | static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch) |
2caaad41 CL |
2729 | { |
2730 | struct per_cpu_pages *pcp; | |
2731 | ||
1c6fe946 MD |
2732 | memset(p, 0, sizeof(*p)); |
2733 | ||
3dfa5721 | 2734 | pcp = &p->pcp; |
2caaad41 | 2735 | pcp->count = 0; |
2caaad41 CL |
2736 | pcp->high = 6 * batch; |
2737 | pcp->batch = max(1UL, 1 * batch); | |
2738 | INIT_LIST_HEAD(&pcp->list); | |
2caaad41 CL |
2739 | } |
2740 | ||
8ad4b1fb RS |
2741 | /* |
2742 | * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist | |
2743 | * to the value high for the pageset p. | |
2744 | */ | |
2745 | ||
2746 | static void setup_pagelist_highmark(struct per_cpu_pageset *p, | |
2747 | unsigned long high) | |
2748 | { | |
2749 | struct per_cpu_pages *pcp; | |
2750 | ||
3dfa5721 | 2751 | pcp = &p->pcp; |
8ad4b1fb RS |
2752 | pcp->high = high; |
2753 | pcp->batch = max(1UL, high/4); | |
2754 | if ((high/4) > (PAGE_SHIFT * 8)) | |
2755 | pcp->batch = PAGE_SHIFT * 8; | |
2756 | } | |
2757 | ||
2758 | ||
e7c8d5c9 CL |
2759 | #ifdef CONFIG_NUMA |
2760 | /* | |
2caaad41 CL |
2761 | * Boot pageset table. One per cpu which is going to be used for all |
2762 | * zones and all nodes. The parameters will be set in such a way | |
2763 | * that an item put on a list will immediately be handed over to | |
2764 | * the buddy list. This is safe since pageset manipulation is done | |
2765 | * with interrupts disabled. | |
2766 | * | |
2767 | * Some NUMA counter updates may also be caught by the boot pagesets. | |
b7c84c6a CL |
2768 | * |
2769 | * The boot_pagesets must be kept even after bootup is complete for | |
2770 | * unused processors and/or zones. They do play a role for bootstrapping | |
2771 | * hotplugged processors. | |
2772 | * | |
2773 | * zoneinfo_show() and maybe other functions do | |
2774 | * not check if the processor is online before following the pageset pointer. | |
2775 | * Other parts of the kernel may not check if the zone is available. | |
2caaad41 | 2776 | */ |
88a2a4ac | 2777 | static struct per_cpu_pageset boot_pageset[NR_CPUS]; |
2caaad41 CL |
2778 | |
2779 | /* | |
2780 | * Dynamically allocate memory for the | |
e7c8d5c9 CL |
2781 | * per cpu pageset array in struct zone. |
2782 | */ | |
6292d9aa | 2783 | static int __cpuinit process_zones(int cpu) |
e7c8d5c9 CL |
2784 | { |
2785 | struct zone *zone, *dzone; | |
37c0708d CL |
2786 | int node = cpu_to_node(cpu); |
2787 | ||
2788 | node_set_state(node, N_CPU); /* this node has a cpu */ | |
e7c8d5c9 | 2789 | |
ee99c71c | 2790 | for_each_populated_zone(zone) { |
23316bc8 | 2791 | zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset), |
37c0708d | 2792 | GFP_KERNEL, node); |
23316bc8 | 2793 | if (!zone_pcp(zone, cpu)) |
e7c8d5c9 | 2794 | goto bad; |
e7c8d5c9 | 2795 | |
23316bc8 | 2796 | setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone)); |
8ad4b1fb RS |
2797 | |
2798 | if (percpu_pagelist_fraction) | |
2799 | setup_pagelist_highmark(zone_pcp(zone, cpu), | |
2800 | (zone->present_pages / percpu_pagelist_fraction)); | |
e7c8d5c9 CL |
2801 | } |
2802 | ||
2803 | return 0; | |
2804 | bad: | |
2805 | for_each_zone(dzone) { | |
64191688 AM |
2806 | if (!populated_zone(dzone)) |
2807 | continue; | |
e7c8d5c9 CL |
2808 | if (dzone == zone) |
2809 | break; | |
23316bc8 NP |
2810 | kfree(zone_pcp(dzone, cpu)); |
2811 | zone_pcp(dzone, cpu) = NULL; | |
e7c8d5c9 CL |
2812 | } |
2813 | return -ENOMEM; | |
2814 | } | |
2815 | ||
2816 | static inline void free_zone_pagesets(int cpu) | |
2817 | { | |
e7c8d5c9 CL |
2818 | struct zone *zone; |
2819 | ||
2820 | for_each_zone(zone) { | |
2821 | struct per_cpu_pageset *pset = zone_pcp(zone, cpu); | |
2822 | ||
f3ef9ead DR |
2823 | /* Free per_cpu_pageset if it is slab allocated */ |
2824 | if (pset != &boot_pageset[cpu]) | |
2825 | kfree(pset); | |
e7c8d5c9 | 2826 | zone_pcp(zone, cpu) = NULL; |
e7c8d5c9 | 2827 | } |
e7c8d5c9 CL |
2828 | } |
2829 | ||
9c7b216d | 2830 | static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb, |
e7c8d5c9 CL |
2831 | unsigned long action, |
2832 | void *hcpu) | |
2833 | { | |
2834 | int cpu = (long)hcpu; | |
2835 | int ret = NOTIFY_OK; | |
2836 | ||
2837 | switch (action) { | |
ce421c79 | 2838 | case CPU_UP_PREPARE: |
8bb78442 | 2839 | case CPU_UP_PREPARE_FROZEN: |
ce421c79 AW |
2840 | if (process_zones(cpu)) |
2841 | ret = NOTIFY_BAD; | |
2842 | break; | |
2843 | case CPU_UP_CANCELED: | |
8bb78442 | 2844 | case CPU_UP_CANCELED_FROZEN: |
ce421c79 | 2845 | case CPU_DEAD: |
8bb78442 | 2846 | case CPU_DEAD_FROZEN: |
ce421c79 AW |
2847 | free_zone_pagesets(cpu); |
2848 | break; | |
2849 | default: | |
2850 | break; | |
e7c8d5c9 CL |
2851 | } |
2852 | return ret; | |
2853 | } | |
2854 | ||
74b85f37 | 2855 | static struct notifier_block __cpuinitdata pageset_notifier = |
e7c8d5c9 CL |
2856 | { &pageset_cpuup_callback, NULL, 0 }; |
2857 | ||
78d9955b | 2858 | void __init setup_per_cpu_pageset(void) |
e7c8d5c9 CL |
2859 | { |
2860 | int err; | |
2861 | ||
2862 | /* Initialize per_cpu_pageset for cpu 0. | |
2863 | * A cpuup callback will do this for every cpu | |
2864 | * as it comes online | |
2865 | */ | |
2866 | err = process_zones(smp_processor_id()); | |
2867 | BUG_ON(err); | |
2868 | register_cpu_notifier(&pageset_notifier); | |
2869 | } | |
2870 | ||
2871 | #endif | |
2872 | ||
577a32f6 | 2873 | static noinline __init_refok |
cca448fe | 2874 | int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages) |
ed8ece2e DH |
2875 | { |
2876 | int i; | |
2877 | struct pglist_data *pgdat = zone->zone_pgdat; | |
cca448fe | 2878 | size_t alloc_size; |
ed8ece2e DH |
2879 | |
2880 | /* | |
2881 | * The per-page waitqueue mechanism uses hashed waitqueues | |
2882 | * per zone. | |
2883 | */ | |
02b694de YG |
2884 | zone->wait_table_hash_nr_entries = |
2885 | wait_table_hash_nr_entries(zone_size_pages); | |
2886 | zone->wait_table_bits = | |
2887 | wait_table_bits(zone->wait_table_hash_nr_entries); | |
cca448fe YG |
2888 | alloc_size = zone->wait_table_hash_nr_entries |
2889 | * sizeof(wait_queue_head_t); | |
2890 | ||
cd94b9db | 2891 | if (!slab_is_available()) { |
cca448fe YG |
2892 | zone->wait_table = (wait_queue_head_t *) |
2893 | alloc_bootmem_node(pgdat, alloc_size); | |
2894 | } else { | |
2895 | /* | |
2896 | * This case means that a zone whose size was 0 gets new memory | |
2897 | * via memory hot-add. | |
2898 | * But it may be the case that a new node was hot-added. In | |
2899 | * this case vmalloc() will not be able to use this new node's | |
2900 | * memory - this wait_table must be initialized to use this new | |
2901 | * node itself as well. | |
2902 | * To use this new node's memory, further consideration will be | |
2903 | * necessary. | |
2904 | */ | |
8691f3a7 | 2905 | zone->wait_table = vmalloc(alloc_size); |
cca448fe YG |
2906 | } |
2907 | if (!zone->wait_table) | |
2908 | return -ENOMEM; | |
ed8ece2e | 2909 | |
02b694de | 2910 | for(i = 0; i < zone->wait_table_hash_nr_entries; ++i) |
ed8ece2e | 2911 | init_waitqueue_head(zone->wait_table + i); |
cca448fe YG |
2912 | |
2913 | return 0; | |
ed8ece2e DH |
2914 | } |
2915 | ||
c09b4240 | 2916 | static __meminit void zone_pcp_init(struct zone *zone) |
ed8ece2e DH |
2917 | { |
2918 | int cpu; | |
2919 | unsigned long batch = zone_batchsize(zone); | |
2920 | ||
2921 | for (cpu = 0; cpu < NR_CPUS; cpu++) { | |
2922 | #ifdef CONFIG_NUMA | |
2923 | /* Early boot. Slab allocator not functional yet */ | |
23316bc8 | 2924 | zone_pcp(zone, cpu) = &boot_pageset[cpu]; |
ed8ece2e DH |
2925 | setup_pageset(&boot_pageset[cpu],0); |
2926 | #else | |
2927 | setup_pageset(zone_pcp(zone,cpu), batch); | |
2928 | #endif | |
2929 | } | |
f5335c0f AB |
2930 | if (zone->present_pages) |
2931 | printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n", | |
2932 | zone->name, zone->present_pages, batch); | |
ed8ece2e DH |
2933 | } |
2934 | ||
718127cc YG |
2935 | __meminit int init_currently_empty_zone(struct zone *zone, |
2936 | unsigned long zone_start_pfn, | |
a2f3aa02 DH |
2937 | unsigned long size, |
2938 | enum memmap_context context) | |
ed8ece2e DH |
2939 | { |
2940 | struct pglist_data *pgdat = zone->zone_pgdat; | |
cca448fe YG |
2941 | int ret; |
2942 | ret = zone_wait_table_init(zone, size); | |
2943 | if (ret) | |
2944 | return ret; | |
ed8ece2e DH |
2945 | pgdat->nr_zones = zone_idx(zone) + 1; |
2946 | ||
ed8ece2e DH |
2947 | zone->zone_start_pfn = zone_start_pfn; |
2948 | ||
708614e6 MG |
2949 | mminit_dprintk(MMINIT_TRACE, "memmap_init", |
2950 | "Initialising map node %d zone %lu pfns %lu -> %lu\n", | |
2951 | pgdat->node_id, | |
2952 | (unsigned long)zone_idx(zone), | |
2953 | zone_start_pfn, (zone_start_pfn + size)); | |
2954 | ||
1e548deb | 2955 | zone_init_free_lists(zone); |
718127cc YG |
2956 | |
2957 | return 0; | |
ed8ece2e DH |
2958 | } |
2959 | ||
c713216d MG |
2960 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
2961 | /* | |
2962 | * Basic iterator support. Return the first range of PFNs for a node | |
2963 | * Note: nid == MAX_NUMNODES returns first region regardless of node | |
2964 | */ | |
a3142c8e | 2965 | static int __meminit first_active_region_index_in_nid(int nid) |
c713216d MG |
2966 | { |
2967 | int i; | |
2968 | ||
2969 | for (i = 0; i < nr_nodemap_entries; i++) | |
2970 | if (nid == MAX_NUMNODES || early_node_map[i].nid == nid) | |
2971 | return i; | |
2972 | ||
2973 | return -1; | |
2974 | } | |
2975 | ||
2976 | /* | |
2977 | * Basic iterator support. Return the next active range of PFNs for a node | |
183ff22b | 2978 | * Note: nid == MAX_NUMNODES returns next region regardless of node |
c713216d | 2979 | */ |
a3142c8e | 2980 | static int __meminit next_active_region_index_in_nid(int index, int nid) |
c713216d MG |
2981 | { |
2982 | for (index = index + 1; index < nr_nodemap_entries; index++) | |
2983 | if (nid == MAX_NUMNODES || early_node_map[index].nid == nid) | |
2984 | return index; | |
2985 | ||
2986 | return -1; | |
2987 | } | |
2988 | ||
2989 | #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID | |
2990 | /* | |
2991 | * Required by SPARSEMEM. Given a PFN, return what node the PFN is on. | |
2992 | * Architectures may implement their own version but if add_active_range() | |
2993 | * was used and there are no special requirements, this is a convenient | |
2994 | * alternative | |
2995 | */ | |
f2dbcfa7 | 2996 | int __meminit __early_pfn_to_nid(unsigned long pfn) |
c713216d MG |
2997 | { |
2998 | int i; | |
2999 | ||
3000 | for (i = 0; i < nr_nodemap_entries; i++) { | |
3001 | unsigned long start_pfn = early_node_map[i].start_pfn; | |
3002 | unsigned long end_pfn = early_node_map[i].end_pfn; | |
3003 | ||
3004 | if (start_pfn <= pfn && pfn < end_pfn) | |
3005 | return early_node_map[i].nid; | |
3006 | } | |
cc2559bc KH |
3007 | /* This is a memory hole */ |
3008 | return -1; | |
c713216d MG |
3009 | } |
3010 | #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */ | |
3011 | ||
f2dbcfa7 KH |
3012 | int __meminit early_pfn_to_nid(unsigned long pfn) |
3013 | { | |
cc2559bc KH |
3014 | int nid; |
3015 | ||
3016 | nid = __early_pfn_to_nid(pfn); | |
3017 | if (nid >= 0) | |
3018 | return nid; | |
3019 | /* just returns 0 */ | |
3020 | return 0; | |
f2dbcfa7 KH |
3021 | } |
3022 | ||
cc2559bc KH |
3023 | #ifdef CONFIG_NODES_SPAN_OTHER_NODES |
3024 | bool __meminit early_pfn_in_nid(unsigned long pfn, int node) | |
3025 | { | |
3026 | int nid; | |
3027 | ||
3028 | nid = __early_pfn_to_nid(pfn); | |
3029 | if (nid >= 0 && nid != node) | |
3030 | return false; | |
3031 | return true; | |
3032 | } | |
3033 | #endif | |
f2dbcfa7 | 3034 | |
c713216d MG |
3035 | /* Basic iterator support to walk early_node_map[] */ |
3036 | #define for_each_active_range_index_in_nid(i, nid) \ | |
3037 | for (i = first_active_region_index_in_nid(nid); i != -1; \ | |
3038 | i = next_active_region_index_in_nid(i, nid)) | |
3039 | ||
3040 | /** | |
3041 | * free_bootmem_with_active_regions - Call free_bootmem_node for each active range | |
88ca3b94 RD |
3042 | * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed. |
3043 | * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node | |
c713216d MG |
3044 | * |
3045 | * If an architecture guarantees that all ranges registered with | |
3046 | * add_active_ranges() contain no holes and may be freed, this | |
3047 | * this function may be used instead of calling free_bootmem() manually. | |
3048 | */ | |
3049 | void __init free_bootmem_with_active_regions(int nid, | |
3050 | unsigned long max_low_pfn) | |
3051 | { | |
3052 | int i; | |
3053 | ||
3054 | for_each_active_range_index_in_nid(i, nid) { | |
3055 | unsigned long size_pages = 0; | |
3056 | unsigned long end_pfn = early_node_map[i].end_pfn; | |
3057 | ||
3058 | if (early_node_map[i].start_pfn >= max_low_pfn) | |
3059 | continue; | |
3060 | ||
3061 | if (end_pfn > max_low_pfn) | |
3062 | end_pfn = max_low_pfn; | |
3063 | ||
3064 | size_pages = end_pfn - early_node_map[i].start_pfn; | |
3065 | free_bootmem_node(NODE_DATA(early_node_map[i].nid), | |
3066 | PFN_PHYS(early_node_map[i].start_pfn), | |
3067 | size_pages << PAGE_SHIFT); | |
3068 | } | |
3069 | } | |
3070 | ||
b5bc6c0e YL |
3071 | void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data) |
3072 | { | |
3073 | int i; | |
d52d53b8 | 3074 | int ret; |
b5bc6c0e | 3075 | |
d52d53b8 YL |
3076 | for_each_active_range_index_in_nid(i, nid) { |
3077 | ret = work_fn(early_node_map[i].start_pfn, | |
3078 | early_node_map[i].end_pfn, data); | |
3079 | if (ret) | |
3080 | break; | |
3081 | } | |
b5bc6c0e | 3082 | } |
c713216d MG |
3083 | /** |
3084 | * sparse_memory_present_with_active_regions - Call memory_present for each active range | |
88ca3b94 | 3085 | * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used. |
c713216d MG |
3086 | * |
3087 | * If an architecture guarantees that all ranges registered with | |
3088 | * add_active_ranges() contain no holes and may be freed, this | |
88ca3b94 | 3089 | * function may be used instead of calling memory_present() manually. |
c713216d MG |
3090 | */ |
3091 | void __init sparse_memory_present_with_active_regions(int nid) | |
3092 | { | |
3093 | int i; | |
3094 | ||
3095 | for_each_active_range_index_in_nid(i, nid) | |
3096 | memory_present(early_node_map[i].nid, | |
3097 | early_node_map[i].start_pfn, | |
3098 | early_node_map[i].end_pfn); | |
3099 | } | |
3100 | ||
3101 | /** | |
3102 | * get_pfn_range_for_nid - Return the start and end page frames for a node | |
88ca3b94 RD |
3103 | * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned. |
3104 | * @start_pfn: Passed by reference. On return, it will have the node start_pfn. | |
3105 | * @end_pfn: Passed by reference. On return, it will have the node end_pfn. | |
c713216d MG |
3106 | * |
3107 | * It returns the start and end page frame of a node based on information | |
3108 | * provided by an arch calling add_active_range(). If called for a node | |
3109 | * with no available memory, a warning is printed and the start and end | |
88ca3b94 | 3110 | * PFNs will be 0. |
c713216d | 3111 | */ |
a3142c8e | 3112 | void __meminit get_pfn_range_for_nid(unsigned int nid, |
c713216d MG |
3113 | unsigned long *start_pfn, unsigned long *end_pfn) |
3114 | { | |
3115 | int i; | |
3116 | *start_pfn = -1UL; | |
3117 | *end_pfn = 0; | |
3118 | ||
3119 | for_each_active_range_index_in_nid(i, nid) { | |
3120 | *start_pfn = min(*start_pfn, early_node_map[i].start_pfn); | |
3121 | *end_pfn = max(*end_pfn, early_node_map[i].end_pfn); | |
3122 | } | |
3123 | ||
633c0666 | 3124 | if (*start_pfn == -1UL) |
c713216d | 3125 | *start_pfn = 0; |
c713216d MG |
3126 | } |
3127 | ||
2a1e274a MG |
3128 | /* |
3129 | * This finds a zone that can be used for ZONE_MOVABLE pages. The | |
3130 | * assumption is made that zones within a node are ordered in monotonic | |
3131 | * increasing memory addresses so that the "highest" populated zone is used | |
3132 | */ | |
b69a7288 | 3133 | static void __init find_usable_zone_for_movable(void) |
2a1e274a MG |
3134 | { |
3135 | int zone_index; | |
3136 | for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) { | |
3137 | if (zone_index == ZONE_MOVABLE) | |
3138 | continue; | |
3139 | ||
3140 | if (arch_zone_highest_possible_pfn[zone_index] > | |
3141 | arch_zone_lowest_possible_pfn[zone_index]) | |
3142 | break; | |
3143 | } | |
3144 | ||
3145 | VM_BUG_ON(zone_index == -1); | |
3146 | movable_zone = zone_index; | |
3147 | } | |
3148 | ||
3149 | /* | |
3150 | * The zone ranges provided by the architecture do not include ZONE_MOVABLE | |
3151 | * because it is sized independant of architecture. Unlike the other zones, | |
3152 | * the starting point for ZONE_MOVABLE is not fixed. It may be different | |
3153 | * in each node depending on the size of each node and how evenly kernelcore | |
3154 | * is distributed. This helper function adjusts the zone ranges | |
3155 | * provided by the architecture for a given node by using the end of the | |
3156 | * highest usable zone for ZONE_MOVABLE. This preserves the assumption that | |
3157 | * zones within a node are in order of monotonic increases memory addresses | |
3158 | */ | |
b69a7288 | 3159 | static void __meminit adjust_zone_range_for_zone_movable(int nid, |
2a1e274a MG |
3160 | unsigned long zone_type, |
3161 | unsigned long node_start_pfn, | |
3162 | unsigned long node_end_pfn, | |
3163 | unsigned long *zone_start_pfn, | |
3164 | unsigned long *zone_end_pfn) | |
3165 | { | |
3166 | /* Only adjust if ZONE_MOVABLE is on this node */ | |
3167 | if (zone_movable_pfn[nid]) { | |
3168 | /* Size ZONE_MOVABLE */ | |
3169 | if (zone_type == ZONE_MOVABLE) { | |
3170 | *zone_start_pfn = zone_movable_pfn[nid]; | |
3171 | *zone_end_pfn = min(node_end_pfn, | |
3172 | arch_zone_highest_possible_pfn[movable_zone]); | |
3173 | ||
3174 | /* Adjust for ZONE_MOVABLE starting within this range */ | |
3175 | } else if (*zone_start_pfn < zone_movable_pfn[nid] && | |
3176 | *zone_end_pfn > zone_movable_pfn[nid]) { | |
3177 | *zone_end_pfn = zone_movable_pfn[nid]; | |
3178 | ||
3179 | /* Check if this whole range is within ZONE_MOVABLE */ | |
3180 | } else if (*zone_start_pfn >= zone_movable_pfn[nid]) | |
3181 | *zone_start_pfn = *zone_end_pfn; | |
3182 | } | |
3183 | } | |
3184 | ||
c713216d MG |
3185 | /* |
3186 | * Return the number of pages a zone spans in a node, including holes | |
3187 | * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node() | |
3188 | */ | |
6ea6e688 | 3189 | static unsigned long __meminit zone_spanned_pages_in_node(int nid, |
c713216d MG |
3190 | unsigned long zone_type, |
3191 | unsigned long *ignored) | |
3192 | { | |
3193 | unsigned long node_start_pfn, node_end_pfn; | |
3194 | unsigned long zone_start_pfn, zone_end_pfn; | |
3195 | ||
3196 | /* Get the start and end of the node and zone */ | |
3197 | get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn); | |
3198 | zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type]; | |
3199 | zone_end_pfn = arch_zone_highest_possible_pfn[zone_type]; | |
2a1e274a MG |
3200 | adjust_zone_range_for_zone_movable(nid, zone_type, |
3201 | node_start_pfn, node_end_pfn, | |
3202 | &zone_start_pfn, &zone_end_pfn); | |
c713216d MG |
3203 | |
3204 | /* Check that this node has pages within the zone's required range */ | |
3205 | if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn) | |
3206 | return 0; | |
3207 | ||
3208 | /* Move the zone boundaries inside the node if necessary */ | |
3209 | zone_end_pfn = min(zone_end_pfn, node_end_pfn); | |
3210 | zone_start_pfn = max(zone_start_pfn, node_start_pfn); | |
3211 | ||
3212 | /* Return the spanned pages */ | |
3213 | return zone_end_pfn - zone_start_pfn; | |
3214 | } | |
3215 | ||
3216 | /* | |
3217 | * Return the number of holes in a range on a node. If nid is MAX_NUMNODES, | |
88ca3b94 | 3218 | * then all holes in the requested range will be accounted for. |
c713216d | 3219 | */ |
b69a7288 | 3220 | static unsigned long __meminit __absent_pages_in_range(int nid, |
c713216d MG |
3221 | unsigned long range_start_pfn, |
3222 | unsigned long range_end_pfn) | |
3223 | { | |
3224 | int i = 0; | |
3225 | unsigned long prev_end_pfn = 0, hole_pages = 0; | |
3226 | unsigned long start_pfn; | |
3227 | ||
3228 | /* Find the end_pfn of the first active range of pfns in the node */ | |
3229 | i = first_active_region_index_in_nid(nid); | |
3230 | if (i == -1) | |
3231 | return 0; | |
3232 | ||
b5445f95 MG |
3233 | prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn); |
3234 | ||
9c7cd687 MG |
3235 | /* Account for ranges before physical memory on this node */ |
3236 | if (early_node_map[i].start_pfn > range_start_pfn) | |
b5445f95 | 3237 | hole_pages = prev_end_pfn - range_start_pfn; |
c713216d MG |
3238 | |
3239 | /* Find all holes for the zone within the node */ | |
3240 | for (; i != -1; i = next_active_region_index_in_nid(i, nid)) { | |
3241 | ||
3242 | /* No need to continue if prev_end_pfn is outside the zone */ | |
3243 | if (prev_end_pfn >= range_end_pfn) | |
3244 | break; | |
3245 | ||
3246 | /* Make sure the end of the zone is not within the hole */ | |
3247 | start_pfn = min(early_node_map[i].start_pfn, range_end_pfn); | |
3248 | prev_end_pfn = max(prev_end_pfn, range_start_pfn); | |
3249 | ||
3250 | /* Update the hole size cound and move on */ | |
3251 | if (start_pfn > range_start_pfn) { | |
3252 | BUG_ON(prev_end_pfn > start_pfn); | |
3253 | hole_pages += start_pfn - prev_end_pfn; | |
3254 | } | |
3255 | prev_end_pfn = early_node_map[i].end_pfn; | |
3256 | } | |
3257 | ||
9c7cd687 MG |
3258 | /* Account for ranges past physical memory on this node */ |
3259 | if (range_end_pfn > prev_end_pfn) | |
0c6cb974 | 3260 | hole_pages += range_end_pfn - |
9c7cd687 MG |
3261 | max(range_start_pfn, prev_end_pfn); |
3262 | ||
c713216d MG |
3263 | return hole_pages; |
3264 | } | |
3265 | ||
3266 | /** | |
3267 | * absent_pages_in_range - Return number of page frames in holes within a range | |
3268 | * @start_pfn: The start PFN to start searching for holes | |
3269 | * @end_pfn: The end PFN to stop searching for holes | |
3270 | * | |
88ca3b94 | 3271 | * It returns the number of pages frames in memory holes within a range. |
c713216d MG |
3272 | */ |
3273 | unsigned long __init absent_pages_in_range(unsigned long start_pfn, | |
3274 | unsigned long end_pfn) | |
3275 | { | |
3276 | return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn); | |
3277 | } | |
3278 | ||
3279 | /* Return the number of page frames in holes in a zone on a node */ | |
6ea6e688 | 3280 | static unsigned long __meminit zone_absent_pages_in_node(int nid, |
c713216d MG |
3281 | unsigned long zone_type, |
3282 | unsigned long *ignored) | |
3283 | { | |
9c7cd687 MG |
3284 | unsigned long node_start_pfn, node_end_pfn; |
3285 | unsigned long zone_start_pfn, zone_end_pfn; | |
3286 | ||
3287 | get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn); | |
3288 | zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type], | |
3289 | node_start_pfn); | |
3290 | zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type], | |
3291 | node_end_pfn); | |
3292 | ||
2a1e274a MG |
3293 | adjust_zone_range_for_zone_movable(nid, zone_type, |
3294 | node_start_pfn, node_end_pfn, | |
3295 | &zone_start_pfn, &zone_end_pfn); | |
9c7cd687 | 3296 | return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn); |
c713216d | 3297 | } |
0e0b864e | 3298 | |
c713216d | 3299 | #else |
6ea6e688 | 3300 | static inline unsigned long __meminit zone_spanned_pages_in_node(int nid, |
c713216d MG |
3301 | unsigned long zone_type, |
3302 | unsigned long *zones_size) | |
3303 | { | |
3304 | return zones_size[zone_type]; | |
3305 | } | |
3306 | ||
6ea6e688 | 3307 | static inline unsigned long __meminit zone_absent_pages_in_node(int nid, |
c713216d MG |
3308 | unsigned long zone_type, |
3309 | unsigned long *zholes_size) | |
3310 | { | |
3311 | if (!zholes_size) | |
3312 | return 0; | |
3313 | ||
3314 | return zholes_size[zone_type]; | |
3315 | } | |
0e0b864e | 3316 | |
c713216d MG |
3317 | #endif |
3318 | ||
a3142c8e | 3319 | static void __meminit calculate_node_totalpages(struct pglist_data *pgdat, |
c713216d MG |
3320 | unsigned long *zones_size, unsigned long *zholes_size) |
3321 | { | |
3322 | unsigned long realtotalpages, totalpages = 0; | |
3323 | enum zone_type i; | |
3324 | ||
3325 | for (i = 0; i < MAX_NR_ZONES; i++) | |
3326 | totalpages += zone_spanned_pages_in_node(pgdat->node_id, i, | |
3327 | zones_size); | |
3328 | pgdat->node_spanned_pages = totalpages; | |
3329 | ||
3330 | realtotalpages = totalpages; | |
3331 | for (i = 0; i < MAX_NR_ZONES; i++) | |
3332 | realtotalpages -= | |
3333 | zone_absent_pages_in_node(pgdat->node_id, i, | |
3334 | zholes_size); | |
3335 | pgdat->node_present_pages = realtotalpages; | |
3336 | printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, | |
3337 | realtotalpages); | |
3338 | } | |
3339 | ||
835c134e MG |
3340 | #ifndef CONFIG_SPARSEMEM |
3341 | /* | |
3342 | * Calculate the size of the zone->blockflags rounded to an unsigned long | |
d9c23400 MG |
3343 | * Start by making sure zonesize is a multiple of pageblock_order by rounding |
3344 | * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally | |
835c134e MG |
3345 | * round what is now in bits to nearest long in bits, then return it in |
3346 | * bytes. | |
3347 | */ | |
3348 | static unsigned long __init usemap_size(unsigned long zonesize) | |
3349 | { | |
3350 | unsigned long usemapsize; | |
3351 | ||
d9c23400 MG |
3352 | usemapsize = roundup(zonesize, pageblock_nr_pages); |
3353 | usemapsize = usemapsize >> pageblock_order; | |
835c134e MG |
3354 | usemapsize *= NR_PAGEBLOCK_BITS; |
3355 | usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long)); | |
3356 | ||
3357 | return usemapsize / 8; | |
3358 | } | |
3359 | ||
3360 | static void __init setup_usemap(struct pglist_data *pgdat, | |
3361 | struct zone *zone, unsigned long zonesize) | |
3362 | { | |
3363 | unsigned long usemapsize = usemap_size(zonesize); | |
3364 | zone->pageblock_flags = NULL; | |
58a01a45 | 3365 | if (usemapsize) |
835c134e | 3366 | zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize); |
835c134e MG |
3367 | } |
3368 | #else | |
3369 | static void inline setup_usemap(struct pglist_data *pgdat, | |
3370 | struct zone *zone, unsigned long zonesize) {} | |
3371 | #endif /* CONFIG_SPARSEMEM */ | |
3372 | ||
d9c23400 | 3373 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
ba72cb8c MG |
3374 | |
3375 | /* Return a sensible default order for the pageblock size. */ | |
3376 | static inline int pageblock_default_order(void) | |
3377 | { | |
3378 | if (HPAGE_SHIFT > PAGE_SHIFT) | |
3379 | return HUGETLB_PAGE_ORDER; | |
3380 | ||
3381 | return MAX_ORDER-1; | |
3382 | } | |
3383 | ||
d9c23400 MG |
3384 | /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */ |
3385 | static inline void __init set_pageblock_order(unsigned int order) | |
3386 | { | |
3387 | /* Check that pageblock_nr_pages has not already been setup */ | |
3388 | if (pageblock_order) | |
3389 | return; | |
3390 | ||
3391 | /* | |
3392 | * Assume the largest contiguous order of interest is a huge page. | |
3393 | * This value may be variable depending on boot parameters on IA64 | |
3394 | */ | |
3395 | pageblock_order = order; | |
3396 | } | |
3397 | #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ | |
3398 | ||
ba72cb8c MG |
3399 | /* |
3400 | * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order() | |
3401 | * and pageblock_default_order() are unused as pageblock_order is set | |
3402 | * at compile-time. See include/linux/pageblock-flags.h for the values of | |
3403 | * pageblock_order based on the kernel config | |
3404 | */ | |
3405 | static inline int pageblock_default_order(unsigned int order) | |
3406 | { | |
3407 | return MAX_ORDER-1; | |
3408 | } | |
d9c23400 MG |
3409 | #define set_pageblock_order(x) do {} while (0) |
3410 | ||
3411 | #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ | |
3412 | ||
1da177e4 LT |
3413 | /* |
3414 | * Set up the zone data structures: | |
3415 | * - mark all pages reserved | |
3416 | * - mark all memory queues empty | |
3417 | * - clear the memory bitmaps | |
3418 | */ | |
b5a0e011 | 3419 | static void __paginginit free_area_init_core(struct pglist_data *pgdat, |
1da177e4 LT |
3420 | unsigned long *zones_size, unsigned long *zholes_size) |
3421 | { | |
2f1b6248 | 3422 | enum zone_type j; |
ed8ece2e | 3423 | int nid = pgdat->node_id; |
1da177e4 | 3424 | unsigned long zone_start_pfn = pgdat->node_start_pfn; |
718127cc | 3425 | int ret; |
1da177e4 | 3426 | |
208d54e5 | 3427 | pgdat_resize_init(pgdat); |
1da177e4 LT |
3428 | pgdat->nr_zones = 0; |
3429 | init_waitqueue_head(&pgdat->kswapd_wait); | |
3430 | pgdat->kswapd_max_order = 0; | |
52d4b9ac | 3431 | pgdat_page_cgroup_init(pgdat); |
1da177e4 LT |
3432 | |
3433 | for (j = 0; j < MAX_NR_ZONES; j++) { | |
3434 | struct zone *zone = pgdat->node_zones + j; | |
0e0b864e | 3435 | unsigned long size, realsize, memmap_pages; |
b69408e8 | 3436 | enum lru_list l; |
1da177e4 | 3437 | |
c713216d MG |
3438 | size = zone_spanned_pages_in_node(nid, j, zones_size); |
3439 | realsize = size - zone_absent_pages_in_node(nid, j, | |
3440 | zholes_size); | |
1da177e4 | 3441 | |
0e0b864e MG |
3442 | /* |
3443 | * Adjust realsize so that it accounts for how much memory | |
3444 | * is used by this zone for memmap. This affects the watermark | |
3445 | * and per-cpu initialisations | |
3446 | */ | |
f7232154 JW |
3447 | memmap_pages = |
3448 | PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT; | |
0e0b864e MG |
3449 | if (realsize >= memmap_pages) { |
3450 | realsize -= memmap_pages; | |
5594c8c8 YL |
3451 | if (memmap_pages) |
3452 | printk(KERN_DEBUG | |
3453 | " %s zone: %lu pages used for memmap\n", | |
3454 | zone_names[j], memmap_pages); | |
0e0b864e MG |
3455 | } else |
3456 | printk(KERN_WARNING | |
3457 | " %s zone: %lu pages exceeds realsize %lu\n", | |
3458 | zone_names[j], memmap_pages, realsize); | |
3459 | ||
6267276f CL |
3460 | /* Account for reserved pages */ |
3461 | if (j == 0 && realsize > dma_reserve) { | |
0e0b864e | 3462 | realsize -= dma_reserve; |
d903ef9f | 3463 | printk(KERN_DEBUG " %s zone: %lu pages reserved\n", |
6267276f | 3464 | zone_names[0], dma_reserve); |
0e0b864e MG |
3465 | } |
3466 | ||
98d2b0eb | 3467 | if (!is_highmem_idx(j)) |
1da177e4 LT |
3468 | nr_kernel_pages += realsize; |
3469 | nr_all_pages += realsize; | |
3470 | ||
3471 | zone->spanned_pages = size; | |
3472 | zone->present_pages = realsize; | |
9614634f | 3473 | #ifdef CONFIG_NUMA |
d5f541ed | 3474 | zone->node = nid; |
8417bba4 | 3475 | zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio) |
9614634f | 3476 | / 100; |
0ff38490 | 3477 | zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100; |
9614634f | 3478 | #endif |
1da177e4 LT |
3479 | zone->name = zone_names[j]; |
3480 | spin_lock_init(&zone->lock); | |
3481 | spin_lock_init(&zone->lru_lock); | |
bdc8cb98 | 3482 | zone_seqlock_init(zone); |
1da177e4 | 3483 | zone->zone_pgdat = pgdat; |
1da177e4 | 3484 | |
3bb1a852 | 3485 | zone->prev_priority = DEF_PRIORITY; |
1da177e4 | 3486 | |
ed8ece2e | 3487 | zone_pcp_init(zone); |
b69408e8 CL |
3488 | for_each_lru(l) { |
3489 | INIT_LIST_HEAD(&zone->lru[l].list); | |
3490 | zone->lru[l].nr_scan = 0; | |
3491 | } | |
6e901571 KM |
3492 | zone->reclaim_stat.recent_rotated[0] = 0; |
3493 | zone->reclaim_stat.recent_rotated[1] = 0; | |
3494 | zone->reclaim_stat.recent_scanned[0] = 0; | |
3495 | zone->reclaim_stat.recent_scanned[1] = 0; | |
2244b95a | 3496 | zap_zone_vm_stats(zone); |
e815af95 | 3497 | zone->flags = 0; |
1da177e4 LT |
3498 | if (!size) |
3499 | continue; | |
3500 | ||
ba72cb8c | 3501 | set_pageblock_order(pageblock_default_order()); |
835c134e | 3502 | setup_usemap(pgdat, zone, size); |
a2f3aa02 DH |
3503 | ret = init_currently_empty_zone(zone, zone_start_pfn, |
3504 | size, MEMMAP_EARLY); | |
718127cc | 3505 | BUG_ON(ret); |
76cdd58e | 3506 | memmap_init(size, nid, j, zone_start_pfn); |
1da177e4 | 3507 | zone_start_pfn += size; |
1da177e4 LT |
3508 | } |
3509 | } | |
3510 | ||
577a32f6 | 3511 | static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat) |
1da177e4 | 3512 | { |
1da177e4 LT |
3513 | /* Skip empty nodes */ |
3514 | if (!pgdat->node_spanned_pages) | |
3515 | return; | |
3516 | ||
d41dee36 | 3517 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
1da177e4 LT |
3518 | /* ia64 gets its own node_mem_map, before this, without bootmem */ |
3519 | if (!pgdat->node_mem_map) { | |
e984bb43 | 3520 | unsigned long size, start, end; |
d41dee36 AW |
3521 | struct page *map; |
3522 | ||
e984bb43 BP |
3523 | /* |
3524 | * The zone's endpoints aren't required to be MAX_ORDER | |
3525 | * aligned but the node_mem_map endpoints must be in order | |
3526 | * for the buddy allocator to function correctly. | |
3527 | */ | |
3528 | start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1); | |
3529 | end = pgdat->node_start_pfn + pgdat->node_spanned_pages; | |
3530 | end = ALIGN(end, MAX_ORDER_NR_PAGES); | |
3531 | size = (end - start) * sizeof(struct page); | |
6f167ec7 DH |
3532 | map = alloc_remap(pgdat->node_id, size); |
3533 | if (!map) | |
3534 | map = alloc_bootmem_node(pgdat, size); | |
e984bb43 | 3535 | pgdat->node_mem_map = map + (pgdat->node_start_pfn - start); |
1da177e4 | 3536 | } |
12d810c1 | 3537 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
3538 | /* |
3539 | * With no DISCONTIG, the global mem_map is just set as node 0's | |
3540 | */ | |
c713216d | 3541 | if (pgdat == NODE_DATA(0)) { |
1da177e4 | 3542 | mem_map = NODE_DATA(0)->node_mem_map; |
c713216d MG |
3543 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
3544 | if (page_to_pfn(mem_map) != pgdat->node_start_pfn) | |
467bc461 | 3545 | mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET); |
c713216d MG |
3546 | #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */ |
3547 | } | |
1da177e4 | 3548 | #endif |
d41dee36 | 3549 | #endif /* CONFIG_FLAT_NODE_MEM_MAP */ |
1da177e4 LT |
3550 | } |
3551 | ||
9109fb7b JW |
3552 | void __paginginit free_area_init_node(int nid, unsigned long *zones_size, |
3553 | unsigned long node_start_pfn, unsigned long *zholes_size) | |
1da177e4 | 3554 | { |
9109fb7b JW |
3555 | pg_data_t *pgdat = NODE_DATA(nid); |
3556 | ||
1da177e4 LT |
3557 | pgdat->node_id = nid; |
3558 | pgdat->node_start_pfn = node_start_pfn; | |
c713216d | 3559 | calculate_node_totalpages(pgdat, zones_size, zholes_size); |
1da177e4 LT |
3560 | |
3561 | alloc_node_mem_map(pgdat); | |
e8c27ac9 YL |
3562 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
3563 | printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n", | |
3564 | nid, (unsigned long)pgdat, | |
3565 | (unsigned long)pgdat->node_mem_map); | |
3566 | #endif | |
1da177e4 LT |
3567 | |
3568 | free_area_init_core(pgdat, zones_size, zholes_size); | |
3569 | } | |
3570 | ||
c713216d | 3571 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
418508c1 MS |
3572 | |
3573 | #if MAX_NUMNODES > 1 | |
3574 | /* | |
3575 | * Figure out the number of possible node ids. | |
3576 | */ | |
3577 | static void __init setup_nr_node_ids(void) | |
3578 | { | |
3579 | unsigned int node; | |
3580 | unsigned int highest = 0; | |
3581 | ||
3582 | for_each_node_mask(node, node_possible_map) | |
3583 | highest = node; | |
3584 | nr_node_ids = highest + 1; | |
3585 | } | |
3586 | #else | |
3587 | static inline void setup_nr_node_ids(void) | |
3588 | { | |
3589 | } | |
3590 | #endif | |
3591 | ||
c713216d MG |
3592 | /** |
3593 | * add_active_range - Register a range of PFNs backed by physical memory | |
3594 | * @nid: The node ID the range resides on | |
3595 | * @start_pfn: The start PFN of the available physical memory | |
3596 | * @end_pfn: The end PFN of the available physical memory | |
3597 | * | |
3598 | * These ranges are stored in an early_node_map[] and later used by | |
3599 | * free_area_init_nodes() to calculate zone sizes and holes. If the | |
3600 | * range spans a memory hole, it is up to the architecture to ensure | |
3601 | * the memory is not freed by the bootmem allocator. If possible | |
3602 | * the range being registered will be merged with existing ranges. | |
3603 | */ | |
3604 | void __init add_active_range(unsigned int nid, unsigned long start_pfn, | |
3605 | unsigned long end_pfn) | |
3606 | { | |
3607 | int i; | |
3608 | ||
6b74ab97 MG |
3609 | mminit_dprintk(MMINIT_TRACE, "memory_register", |
3610 | "Entering add_active_range(%d, %#lx, %#lx) " | |
3611 | "%d entries of %d used\n", | |
3612 | nid, start_pfn, end_pfn, | |
3613 | nr_nodemap_entries, MAX_ACTIVE_REGIONS); | |
c713216d | 3614 | |
2dbb51c4 MG |
3615 | mminit_validate_memmodel_limits(&start_pfn, &end_pfn); |
3616 | ||
c713216d MG |
3617 | /* Merge with existing active regions if possible */ |
3618 | for (i = 0; i < nr_nodemap_entries; i++) { | |
3619 | if (early_node_map[i].nid != nid) | |
3620 | continue; | |
3621 | ||
3622 | /* Skip if an existing region covers this new one */ | |
3623 | if (start_pfn >= early_node_map[i].start_pfn && | |
3624 | end_pfn <= early_node_map[i].end_pfn) | |
3625 | return; | |
3626 | ||
3627 | /* Merge forward if suitable */ | |
3628 | if (start_pfn <= early_node_map[i].end_pfn && | |
3629 | end_pfn > early_node_map[i].end_pfn) { | |
3630 | early_node_map[i].end_pfn = end_pfn; | |
3631 | return; | |
3632 | } | |
3633 | ||
3634 | /* Merge backward if suitable */ | |
3635 | if (start_pfn < early_node_map[i].end_pfn && | |
3636 | end_pfn >= early_node_map[i].start_pfn) { | |
3637 | early_node_map[i].start_pfn = start_pfn; | |
3638 | return; | |
3639 | } | |
3640 | } | |
3641 | ||
3642 | /* Check that early_node_map is large enough */ | |
3643 | if (i >= MAX_ACTIVE_REGIONS) { | |
3644 | printk(KERN_CRIT "More than %d memory regions, truncating\n", | |
3645 | MAX_ACTIVE_REGIONS); | |
3646 | return; | |
3647 | } | |
3648 | ||
3649 | early_node_map[i].nid = nid; | |
3650 | early_node_map[i].start_pfn = start_pfn; | |
3651 | early_node_map[i].end_pfn = end_pfn; | |
3652 | nr_nodemap_entries = i + 1; | |
3653 | } | |
3654 | ||
3655 | /** | |
cc1050ba | 3656 | * remove_active_range - Shrink an existing registered range of PFNs |
c713216d | 3657 | * @nid: The node id the range is on that should be shrunk |
cc1050ba YL |
3658 | * @start_pfn: The new PFN of the range |
3659 | * @end_pfn: The new PFN of the range | |
c713216d MG |
3660 | * |
3661 | * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node. | |
cc1a9d86 YL |
3662 | * The map is kept near the end physical page range that has already been |
3663 | * registered. This function allows an arch to shrink an existing registered | |
3664 | * range. | |
c713216d | 3665 | */ |
cc1050ba YL |
3666 | void __init remove_active_range(unsigned int nid, unsigned long start_pfn, |
3667 | unsigned long end_pfn) | |
c713216d | 3668 | { |
cc1a9d86 YL |
3669 | int i, j; |
3670 | int removed = 0; | |
c713216d | 3671 | |
cc1050ba YL |
3672 | printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n", |
3673 | nid, start_pfn, end_pfn); | |
3674 | ||
c713216d | 3675 | /* Find the old active region end and shrink */ |
cc1a9d86 | 3676 | for_each_active_range_index_in_nid(i, nid) { |
cc1050ba YL |
3677 | if (early_node_map[i].start_pfn >= start_pfn && |
3678 | early_node_map[i].end_pfn <= end_pfn) { | |
cc1a9d86 | 3679 | /* clear it */ |
cc1050ba | 3680 | early_node_map[i].start_pfn = 0; |
cc1a9d86 YL |
3681 | early_node_map[i].end_pfn = 0; |
3682 | removed = 1; | |
3683 | continue; | |
3684 | } | |
cc1050ba YL |
3685 | if (early_node_map[i].start_pfn < start_pfn && |
3686 | early_node_map[i].end_pfn > start_pfn) { | |
3687 | unsigned long temp_end_pfn = early_node_map[i].end_pfn; | |
3688 | early_node_map[i].end_pfn = start_pfn; | |
3689 | if (temp_end_pfn > end_pfn) | |
3690 | add_active_range(nid, end_pfn, temp_end_pfn); | |
3691 | continue; | |
3692 | } | |
3693 | if (early_node_map[i].start_pfn >= start_pfn && | |
3694 | early_node_map[i].end_pfn > end_pfn && | |
3695 | early_node_map[i].start_pfn < end_pfn) { | |
3696 | early_node_map[i].start_pfn = end_pfn; | |
cc1a9d86 | 3697 | continue; |
c713216d | 3698 | } |
cc1a9d86 YL |
3699 | } |
3700 | ||
3701 | if (!removed) | |
3702 | return; | |
3703 | ||
3704 | /* remove the blank ones */ | |
3705 | for (i = nr_nodemap_entries - 1; i > 0; i--) { | |
3706 | if (early_node_map[i].nid != nid) | |
3707 | continue; | |
3708 | if (early_node_map[i].end_pfn) | |
3709 | continue; | |
3710 | /* we found it, get rid of it */ | |
3711 | for (j = i; j < nr_nodemap_entries - 1; j++) | |
3712 | memcpy(&early_node_map[j], &early_node_map[j+1], | |
3713 | sizeof(early_node_map[j])); | |
3714 | j = nr_nodemap_entries - 1; | |
3715 | memset(&early_node_map[j], 0, sizeof(early_node_map[j])); | |
3716 | nr_nodemap_entries--; | |
3717 | } | |
c713216d MG |
3718 | } |
3719 | ||
3720 | /** | |
3721 | * remove_all_active_ranges - Remove all currently registered regions | |
88ca3b94 | 3722 | * |
c713216d MG |
3723 | * During discovery, it may be found that a table like SRAT is invalid |
3724 | * and an alternative discovery method must be used. This function removes | |
3725 | * all currently registered regions. | |
3726 | */ | |
88ca3b94 | 3727 | void __init remove_all_active_ranges(void) |
c713216d MG |
3728 | { |
3729 | memset(early_node_map, 0, sizeof(early_node_map)); | |
3730 | nr_nodemap_entries = 0; | |
3731 | } | |
3732 | ||
3733 | /* Compare two active node_active_regions */ | |
3734 | static int __init cmp_node_active_region(const void *a, const void *b) | |
3735 | { | |
3736 | struct node_active_region *arange = (struct node_active_region *)a; | |
3737 | struct node_active_region *brange = (struct node_active_region *)b; | |
3738 | ||
3739 | /* Done this way to avoid overflows */ | |
3740 | if (arange->start_pfn > brange->start_pfn) | |
3741 | return 1; | |
3742 | if (arange->start_pfn < brange->start_pfn) | |
3743 | return -1; | |
3744 | ||
3745 | return 0; | |
3746 | } | |
3747 | ||
3748 | /* sort the node_map by start_pfn */ | |
3749 | static void __init sort_node_map(void) | |
3750 | { | |
3751 | sort(early_node_map, (size_t)nr_nodemap_entries, | |
3752 | sizeof(struct node_active_region), | |
3753 | cmp_node_active_region, NULL); | |
3754 | } | |
3755 | ||
a6af2bc3 | 3756 | /* Find the lowest pfn for a node */ |
b69a7288 | 3757 | static unsigned long __init find_min_pfn_for_node(int nid) |
c713216d MG |
3758 | { |
3759 | int i; | |
a6af2bc3 | 3760 | unsigned long min_pfn = ULONG_MAX; |
1abbfb41 | 3761 | |
c713216d MG |
3762 | /* Assuming a sorted map, the first range found has the starting pfn */ |
3763 | for_each_active_range_index_in_nid(i, nid) | |
a6af2bc3 | 3764 | min_pfn = min(min_pfn, early_node_map[i].start_pfn); |
c713216d | 3765 | |
a6af2bc3 MG |
3766 | if (min_pfn == ULONG_MAX) { |
3767 | printk(KERN_WARNING | |
2bc0d261 | 3768 | "Could not find start_pfn for node %d\n", nid); |
a6af2bc3 MG |
3769 | return 0; |
3770 | } | |
3771 | ||
3772 | return min_pfn; | |
c713216d MG |
3773 | } |
3774 | ||
3775 | /** | |
3776 | * find_min_pfn_with_active_regions - Find the minimum PFN registered | |
3777 | * | |
3778 | * It returns the minimum PFN based on information provided via | |
88ca3b94 | 3779 | * add_active_range(). |
c713216d MG |
3780 | */ |
3781 | unsigned long __init find_min_pfn_with_active_regions(void) | |
3782 | { | |
3783 | return find_min_pfn_for_node(MAX_NUMNODES); | |
3784 | } | |
3785 | ||
37b07e41 LS |
3786 | /* |
3787 | * early_calculate_totalpages() | |
3788 | * Sum pages in active regions for movable zone. | |
3789 | * Populate N_HIGH_MEMORY for calculating usable_nodes. | |
3790 | */ | |
484f51f8 | 3791 | static unsigned long __init early_calculate_totalpages(void) |
7e63efef MG |
3792 | { |
3793 | int i; | |
3794 | unsigned long totalpages = 0; | |
3795 | ||
37b07e41 LS |
3796 | for (i = 0; i < nr_nodemap_entries; i++) { |
3797 | unsigned long pages = early_node_map[i].end_pfn - | |
7e63efef | 3798 | early_node_map[i].start_pfn; |
37b07e41 LS |
3799 | totalpages += pages; |
3800 | if (pages) | |
3801 | node_set_state(early_node_map[i].nid, N_HIGH_MEMORY); | |
3802 | } | |
3803 | return totalpages; | |
7e63efef MG |
3804 | } |
3805 | ||
2a1e274a MG |
3806 | /* |
3807 | * Find the PFN the Movable zone begins in each node. Kernel memory | |
3808 | * is spread evenly between nodes as long as the nodes have enough | |
3809 | * memory. When they don't, some nodes will have more kernelcore than | |
3810 | * others | |
3811 | */ | |
b69a7288 | 3812 | static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn) |
2a1e274a MG |
3813 | { |
3814 | int i, nid; | |
3815 | unsigned long usable_startpfn; | |
3816 | unsigned long kernelcore_node, kernelcore_remaining; | |
37b07e41 LS |
3817 | unsigned long totalpages = early_calculate_totalpages(); |
3818 | int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]); | |
2a1e274a | 3819 | |
7e63efef MG |
3820 | /* |
3821 | * If movablecore was specified, calculate what size of | |
3822 | * kernelcore that corresponds so that memory usable for | |
3823 | * any allocation type is evenly spread. If both kernelcore | |
3824 | * and movablecore are specified, then the value of kernelcore | |
3825 | * will be used for required_kernelcore if it's greater than | |
3826 | * what movablecore would have allowed. | |
3827 | */ | |
3828 | if (required_movablecore) { | |
7e63efef MG |
3829 | unsigned long corepages; |
3830 | ||
3831 | /* | |
3832 | * Round-up so that ZONE_MOVABLE is at least as large as what | |
3833 | * was requested by the user | |
3834 | */ | |
3835 | required_movablecore = | |
3836 | roundup(required_movablecore, MAX_ORDER_NR_PAGES); | |
3837 | corepages = totalpages - required_movablecore; | |
3838 | ||
3839 | required_kernelcore = max(required_kernelcore, corepages); | |
3840 | } | |
3841 | ||
2a1e274a MG |
3842 | /* If kernelcore was not specified, there is no ZONE_MOVABLE */ |
3843 | if (!required_kernelcore) | |
3844 | return; | |
3845 | ||
3846 | /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */ | |
3847 | find_usable_zone_for_movable(); | |
3848 | usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone]; | |
3849 | ||
3850 | restart: | |
3851 | /* Spread kernelcore memory as evenly as possible throughout nodes */ | |
3852 | kernelcore_node = required_kernelcore / usable_nodes; | |
37b07e41 | 3853 | for_each_node_state(nid, N_HIGH_MEMORY) { |
2a1e274a MG |
3854 | /* |
3855 | * Recalculate kernelcore_node if the division per node | |
3856 | * now exceeds what is necessary to satisfy the requested | |
3857 | * amount of memory for the kernel | |
3858 | */ | |
3859 | if (required_kernelcore < kernelcore_node) | |
3860 | kernelcore_node = required_kernelcore / usable_nodes; | |
3861 | ||
3862 | /* | |
3863 | * As the map is walked, we track how much memory is usable | |
3864 | * by the kernel using kernelcore_remaining. When it is | |
3865 | * 0, the rest of the node is usable by ZONE_MOVABLE | |
3866 | */ | |
3867 | kernelcore_remaining = kernelcore_node; | |
3868 | ||
3869 | /* Go through each range of PFNs within this node */ | |
3870 | for_each_active_range_index_in_nid(i, nid) { | |
3871 | unsigned long start_pfn, end_pfn; | |
3872 | unsigned long size_pages; | |
3873 | ||
3874 | start_pfn = max(early_node_map[i].start_pfn, | |
3875 | zone_movable_pfn[nid]); | |
3876 | end_pfn = early_node_map[i].end_pfn; | |
3877 | if (start_pfn >= end_pfn) | |
3878 | continue; | |
3879 | ||
3880 | /* Account for what is only usable for kernelcore */ | |
3881 | if (start_pfn < usable_startpfn) { | |
3882 | unsigned long kernel_pages; | |
3883 | kernel_pages = min(end_pfn, usable_startpfn) | |
3884 | - start_pfn; | |
3885 | ||
3886 | kernelcore_remaining -= min(kernel_pages, | |
3887 | kernelcore_remaining); | |
3888 | required_kernelcore -= min(kernel_pages, | |
3889 | required_kernelcore); | |
3890 | ||
3891 | /* Continue if range is now fully accounted */ | |
3892 | if (end_pfn <= usable_startpfn) { | |
3893 | ||
3894 | /* | |
3895 | * Push zone_movable_pfn to the end so | |
3896 | * that if we have to rebalance | |
3897 | * kernelcore across nodes, we will | |
3898 | * not double account here | |
3899 | */ | |
3900 | zone_movable_pfn[nid] = end_pfn; | |
3901 | continue; | |
3902 | } | |
3903 | start_pfn = usable_startpfn; | |
3904 | } | |
3905 | ||
3906 | /* | |
3907 | * The usable PFN range for ZONE_MOVABLE is from | |
3908 | * start_pfn->end_pfn. Calculate size_pages as the | |
3909 | * number of pages used as kernelcore | |
3910 | */ | |
3911 | size_pages = end_pfn - start_pfn; | |
3912 | if (size_pages > kernelcore_remaining) | |
3913 | size_pages = kernelcore_remaining; | |
3914 | zone_movable_pfn[nid] = start_pfn + size_pages; | |
3915 | ||
3916 | /* | |
3917 | * Some kernelcore has been met, update counts and | |
3918 | * break if the kernelcore for this node has been | |
3919 | * satisified | |
3920 | */ | |
3921 | required_kernelcore -= min(required_kernelcore, | |
3922 | size_pages); | |
3923 | kernelcore_remaining -= size_pages; | |
3924 | if (!kernelcore_remaining) | |
3925 | break; | |
3926 | } | |
3927 | } | |
3928 | ||
3929 | /* | |
3930 | * If there is still required_kernelcore, we do another pass with one | |
3931 | * less node in the count. This will push zone_movable_pfn[nid] further | |
3932 | * along on the nodes that still have memory until kernelcore is | |
3933 | * satisified | |
3934 | */ | |
3935 | usable_nodes--; | |
3936 | if (usable_nodes && required_kernelcore > usable_nodes) | |
3937 | goto restart; | |
3938 | ||
3939 | /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */ | |
3940 | for (nid = 0; nid < MAX_NUMNODES; nid++) | |
3941 | zone_movable_pfn[nid] = | |
3942 | roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES); | |
3943 | } | |
3944 | ||
37b07e41 LS |
3945 | /* Any regular memory on that node ? */ |
3946 | static void check_for_regular_memory(pg_data_t *pgdat) | |
3947 | { | |
3948 | #ifdef CONFIG_HIGHMEM | |
3949 | enum zone_type zone_type; | |
3950 | ||
3951 | for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) { | |
3952 | struct zone *zone = &pgdat->node_zones[zone_type]; | |
3953 | if (zone->present_pages) | |
3954 | node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY); | |
3955 | } | |
3956 | #endif | |
3957 | } | |
3958 | ||
c713216d MG |
3959 | /** |
3960 | * free_area_init_nodes - Initialise all pg_data_t and zone data | |
88ca3b94 | 3961 | * @max_zone_pfn: an array of max PFNs for each zone |
c713216d MG |
3962 | * |
3963 | * This will call free_area_init_node() for each active node in the system. | |
3964 | * Using the page ranges provided by add_active_range(), the size of each | |
3965 | * zone in each node and their holes is calculated. If the maximum PFN | |
3966 | * between two adjacent zones match, it is assumed that the zone is empty. | |
3967 | * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed | |
3968 | * that arch_max_dma32_pfn has no pages. It is also assumed that a zone | |
3969 | * starts where the previous one ended. For example, ZONE_DMA32 starts | |
3970 | * at arch_max_dma_pfn. | |
3971 | */ | |
3972 | void __init free_area_init_nodes(unsigned long *max_zone_pfn) | |
3973 | { | |
3974 | unsigned long nid; | |
db99100d | 3975 | int i; |
c713216d | 3976 | |
a6af2bc3 MG |
3977 | /* Sort early_node_map as initialisation assumes it is sorted */ |
3978 | sort_node_map(); | |
3979 | ||
c713216d MG |
3980 | /* Record where the zone boundaries are */ |
3981 | memset(arch_zone_lowest_possible_pfn, 0, | |
3982 | sizeof(arch_zone_lowest_possible_pfn)); | |
3983 | memset(arch_zone_highest_possible_pfn, 0, | |
3984 | sizeof(arch_zone_highest_possible_pfn)); | |
3985 | arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions(); | |
3986 | arch_zone_highest_possible_pfn[0] = max_zone_pfn[0]; | |
3987 | for (i = 1; i < MAX_NR_ZONES; i++) { | |
2a1e274a MG |
3988 | if (i == ZONE_MOVABLE) |
3989 | continue; | |
c713216d MG |
3990 | arch_zone_lowest_possible_pfn[i] = |
3991 | arch_zone_highest_possible_pfn[i-1]; | |
3992 | arch_zone_highest_possible_pfn[i] = | |
3993 | max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]); | |
3994 | } | |
2a1e274a MG |
3995 | arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0; |
3996 | arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0; | |
3997 | ||
3998 | /* Find the PFNs that ZONE_MOVABLE begins at in each node */ | |
3999 | memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn)); | |
4000 | find_zone_movable_pfns_for_nodes(zone_movable_pfn); | |
c713216d | 4001 | |
c713216d MG |
4002 | /* Print out the zone ranges */ |
4003 | printk("Zone PFN ranges:\n"); | |
2a1e274a MG |
4004 | for (i = 0; i < MAX_NR_ZONES; i++) { |
4005 | if (i == ZONE_MOVABLE) | |
4006 | continue; | |
5dab8ec1 | 4007 | printk(" %-8s %0#10lx -> %0#10lx\n", |
c713216d MG |
4008 | zone_names[i], |
4009 | arch_zone_lowest_possible_pfn[i], | |
4010 | arch_zone_highest_possible_pfn[i]); | |
2a1e274a MG |
4011 | } |
4012 | ||
4013 | /* Print out the PFNs ZONE_MOVABLE begins at in each node */ | |
4014 | printk("Movable zone start PFN for each node\n"); | |
4015 | for (i = 0; i < MAX_NUMNODES; i++) { | |
4016 | if (zone_movable_pfn[i]) | |
4017 | printk(" Node %d: %lu\n", i, zone_movable_pfn[i]); | |
4018 | } | |
c713216d MG |
4019 | |
4020 | /* Print out the early_node_map[] */ | |
4021 | printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries); | |
4022 | for (i = 0; i < nr_nodemap_entries; i++) | |
5dab8ec1 | 4023 | printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid, |
c713216d MG |
4024 | early_node_map[i].start_pfn, |
4025 | early_node_map[i].end_pfn); | |
4026 | ||
4027 | /* Initialise every node */ | |
708614e6 | 4028 | mminit_verify_pageflags_layout(); |
8ef82866 | 4029 | setup_nr_node_ids(); |
c713216d MG |
4030 | for_each_online_node(nid) { |
4031 | pg_data_t *pgdat = NODE_DATA(nid); | |
9109fb7b | 4032 | free_area_init_node(nid, NULL, |
c713216d | 4033 | find_min_pfn_for_node(nid), NULL); |
37b07e41 LS |
4034 | |
4035 | /* Any memory on that node */ | |
4036 | if (pgdat->node_present_pages) | |
4037 | node_set_state(nid, N_HIGH_MEMORY); | |
4038 | check_for_regular_memory(pgdat); | |
c713216d MG |
4039 | } |
4040 | } | |
2a1e274a | 4041 | |
7e63efef | 4042 | static int __init cmdline_parse_core(char *p, unsigned long *core) |
2a1e274a MG |
4043 | { |
4044 | unsigned long long coremem; | |
4045 | if (!p) | |
4046 | return -EINVAL; | |
4047 | ||
4048 | coremem = memparse(p, &p); | |
7e63efef | 4049 | *core = coremem >> PAGE_SHIFT; |
2a1e274a | 4050 | |
7e63efef | 4051 | /* Paranoid check that UL is enough for the coremem value */ |
2a1e274a MG |
4052 | WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX); |
4053 | ||
4054 | return 0; | |
4055 | } | |
ed7ed365 | 4056 | |
7e63efef MG |
4057 | /* |
4058 | * kernelcore=size sets the amount of memory for use for allocations that | |
4059 | * cannot be reclaimed or migrated. | |
4060 | */ | |
4061 | static int __init cmdline_parse_kernelcore(char *p) | |
4062 | { | |
4063 | return cmdline_parse_core(p, &required_kernelcore); | |
4064 | } | |
4065 | ||
4066 | /* | |
4067 | * movablecore=size sets the amount of memory for use for allocations that | |
4068 | * can be reclaimed or migrated. | |
4069 | */ | |
4070 | static int __init cmdline_parse_movablecore(char *p) | |
4071 | { | |
4072 | return cmdline_parse_core(p, &required_movablecore); | |
4073 | } | |
4074 | ||
ed7ed365 | 4075 | early_param("kernelcore", cmdline_parse_kernelcore); |
7e63efef | 4076 | early_param("movablecore", cmdline_parse_movablecore); |
ed7ed365 | 4077 | |
c713216d MG |
4078 | #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */ |
4079 | ||
0e0b864e | 4080 | /** |
88ca3b94 RD |
4081 | * set_dma_reserve - set the specified number of pages reserved in the first zone |
4082 | * @new_dma_reserve: The number of pages to mark reserved | |
0e0b864e MG |
4083 | * |
4084 | * The per-cpu batchsize and zone watermarks are determined by present_pages. | |
4085 | * In the DMA zone, a significant percentage may be consumed by kernel image | |
4086 | * and other unfreeable allocations which can skew the watermarks badly. This | |
88ca3b94 RD |
4087 | * function may optionally be used to account for unfreeable pages in the |
4088 | * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and | |
4089 | * smaller per-cpu batchsize. | |
0e0b864e MG |
4090 | */ |
4091 | void __init set_dma_reserve(unsigned long new_dma_reserve) | |
4092 | { | |
4093 | dma_reserve = new_dma_reserve; | |
4094 | } | |
4095 | ||
93b7504e | 4096 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
52765583 | 4097 | struct pglist_data __refdata contig_page_data = { .bdata = &bootmem_node_data[0] }; |
1da177e4 | 4098 | EXPORT_SYMBOL(contig_page_data); |
93b7504e | 4099 | #endif |
1da177e4 LT |
4100 | |
4101 | void __init free_area_init(unsigned long *zones_size) | |
4102 | { | |
9109fb7b | 4103 | free_area_init_node(0, zones_size, |
1da177e4 LT |
4104 | __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL); |
4105 | } | |
1da177e4 | 4106 | |
1da177e4 LT |
4107 | static int page_alloc_cpu_notify(struct notifier_block *self, |
4108 | unsigned long action, void *hcpu) | |
4109 | { | |
4110 | int cpu = (unsigned long)hcpu; | |
1da177e4 | 4111 | |
8bb78442 | 4112 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { |
9f8f2172 CL |
4113 | drain_pages(cpu); |
4114 | ||
4115 | /* | |
4116 | * Spill the event counters of the dead processor | |
4117 | * into the current processors event counters. | |
4118 | * This artificially elevates the count of the current | |
4119 | * processor. | |
4120 | */ | |
f8891e5e | 4121 | vm_events_fold_cpu(cpu); |
9f8f2172 CL |
4122 | |
4123 | /* | |
4124 | * Zero the differential counters of the dead processor | |
4125 | * so that the vm statistics are consistent. | |
4126 | * | |
4127 | * This is only okay since the processor is dead and cannot | |
4128 | * race with what we are doing. | |
4129 | */ | |
2244b95a | 4130 | refresh_cpu_vm_stats(cpu); |
1da177e4 LT |
4131 | } |
4132 | return NOTIFY_OK; | |
4133 | } | |
1da177e4 LT |
4134 | |
4135 | void __init page_alloc_init(void) | |
4136 | { | |
4137 | hotcpu_notifier(page_alloc_cpu_notify, 0); | |
4138 | } | |
4139 | ||
cb45b0e9 HA |
4140 | /* |
4141 | * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio | |
4142 | * or min_free_kbytes changes. | |
4143 | */ | |
4144 | static void calculate_totalreserve_pages(void) | |
4145 | { | |
4146 | struct pglist_data *pgdat; | |
4147 | unsigned long reserve_pages = 0; | |
2f6726e5 | 4148 | enum zone_type i, j; |
cb45b0e9 HA |
4149 | |
4150 | for_each_online_pgdat(pgdat) { | |
4151 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
4152 | struct zone *zone = pgdat->node_zones + i; | |
4153 | unsigned long max = 0; | |
4154 | ||
4155 | /* Find valid and maximum lowmem_reserve in the zone */ | |
4156 | for (j = i; j < MAX_NR_ZONES; j++) { | |
4157 | if (zone->lowmem_reserve[j] > max) | |
4158 | max = zone->lowmem_reserve[j]; | |
4159 | } | |
4160 | ||
4161 | /* we treat pages_high as reserved pages. */ | |
4162 | max += zone->pages_high; | |
4163 | ||
4164 | if (max > zone->present_pages) | |
4165 | max = zone->present_pages; | |
4166 | reserve_pages += max; | |
4167 | } | |
4168 | } | |
4169 | totalreserve_pages = reserve_pages; | |
4170 | } | |
4171 | ||
1da177e4 LT |
4172 | /* |
4173 | * setup_per_zone_lowmem_reserve - called whenever | |
4174 | * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone | |
4175 | * has a correct pages reserved value, so an adequate number of | |
4176 | * pages are left in the zone after a successful __alloc_pages(). | |
4177 | */ | |
4178 | static void setup_per_zone_lowmem_reserve(void) | |
4179 | { | |
4180 | struct pglist_data *pgdat; | |
2f6726e5 | 4181 | enum zone_type j, idx; |
1da177e4 | 4182 | |
ec936fc5 | 4183 | for_each_online_pgdat(pgdat) { |
1da177e4 LT |
4184 | for (j = 0; j < MAX_NR_ZONES; j++) { |
4185 | struct zone *zone = pgdat->node_zones + j; | |
4186 | unsigned long present_pages = zone->present_pages; | |
4187 | ||
4188 | zone->lowmem_reserve[j] = 0; | |
4189 | ||
2f6726e5 CL |
4190 | idx = j; |
4191 | while (idx) { | |
1da177e4 LT |
4192 | struct zone *lower_zone; |
4193 | ||
2f6726e5 CL |
4194 | idx--; |
4195 | ||
1da177e4 LT |
4196 | if (sysctl_lowmem_reserve_ratio[idx] < 1) |
4197 | sysctl_lowmem_reserve_ratio[idx] = 1; | |
4198 | ||
4199 | lower_zone = pgdat->node_zones + idx; | |
4200 | lower_zone->lowmem_reserve[j] = present_pages / | |
4201 | sysctl_lowmem_reserve_ratio[idx]; | |
4202 | present_pages += lower_zone->present_pages; | |
4203 | } | |
4204 | } | |
4205 | } | |
cb45b0e9 HA |
4206 | |
4207 | /* update totalreserve_pages */ | |
4208 | calculate_totalreserve_pages(); | |
1da177e4 LT |
4209 | } |
4210 | ||
88ca3b94 RD |
4211 | /** |
4212 | * setup_per_zone_pages_min - called when min_free_kbytes changes. | |
4213 | * | |
4214 | * Ensures that the pages_{min,low,high} values for each zone are set correctly | |
4215 | * with respect to min_free_kbytes. | |
1da177e4 | 4216 | */ |
3947be19 | 4217 | void setup_per_zone_pages_min(void) |
1da177e4 LT |
4218 | { |
4219 | unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10); | |
4220 | unsigned long lowmem_pages = 0; | |
4221 | struct zone *zone; | |
4222 | unsigned long flags; | |
4223 | ||
4224 | /* Calculate total number of !ZONE_HIGHMEM pages */ | |
4225 | for_each_zone(zone) { | |
4226 | if (!is_highmem(zone)) | |
4227 | lowmem_pages += zone->present_pages; | |
4228 | } | |
4229 | ||
4230 | for_each_zone(zone) { | |
ac924c60 AM |
4231 | u64 tmp; |
4232 | ||
1125b4e3 | 4233 | spin_lock_irqsave(&zone->lock, flags); |
ac924c60 AM |
4234 | tmp = (u64)pages_min * zone->present_pages; |
4235 | do_div(tmp, lowmem_pages); | |
1da177e4 LT |
4236 | if (is_highmem(zone)) { |
4237 | /* | |
669ed175 NP |
4238 | * __GFP_HIGH and PF_MEMALLOC allocations usually don't |
4239 | * need highmem pages, so cap pages_min to a small | |
4240 | * value here. | |
4241 | * | |
4242 | * The (pages_high-pages_low) and (pages_low-pages_min) | |
4243 | * deltas controls asynch page reclaim, and so should | |
4244 | * not be capped for highmem. | |
1da177e4 LT |
4245 | */ |
4246 | int min_pages; | |
4247 | ||
4248 | min_pages = zone->present_pages / 1024; | |
4249 | if (min_pages < SWAP_CLUSTER_MAX) | |
4250 | min_pages = SWAP_CLUSTER_MAX; | |
4251 | if (min_pages > 128) | |
4252 | min_pages = 128; | |
4253 | zone->pages_min = min_pages; | |
4254 | } else { | |
669ed175 NP |
4255 | /* |
4256 | * If it's a lowmem zone, reserve a number of pages | |
1da177e4 LT |
4257 | * proportionate to the zone's size. |
4258 | */ | |
669ed175 | 4259 | zone->pages_min = tmp; |
1da177e4 LT |
4260 | } |
4261 | ||
ac924c60 AM |
4262 | zone->pages_low = zone->pages_min + (tmp >> 2); |
4263 | zone->pages_high = zone->pages_min + (tmp >> 1); | |
56fd56b8 | 4264 | setup_zone_migrate_reserve(zone); |
1125b4e3 | 4265 | spin_unlock_irqrestore(&zone->lock, flags); |
1da177e4 | 4266 | } |
cb45b0e9 HA |
4267 | |
4268 | /* update totalreserve_pages */ | |
4269 | calculate_totalreserve_pages(); | |
1da177e4 LT |
4270 | } |
4271 | ||
556adecb RR |
4272 | /** |
4273 | * setup_per_zone_inactive_ratio - called when min_free_kbytes changes. | |
4274 | * | |
4275 | * The inactive anon list should be small enough that the VM never has to | |
4276 | * do too much work, but large enough that each inactive page has a chance | |
4277 | * to be referenced again before it is swapped out. | |
4278 | * | |
4279 | * The inactive_anon ratio is the target ratio of ACTIVE_ANON to | |
4280 | * INACTIVE_ANON pages on this zone's LRU, maintained by the | |
4281 | * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of | |
4282 | * the anonymous pages are kept on the inactive list. | |
4283 | * | |
4284 | * total target max | |
4285 | * memory ratio inactive anon | |
4286 | * ------------------------------------- | |
4287 | * 10MB 1 5MB | |
4288 | * 100MB 1 50MB | |
4289 | * 1GB 3 250MB | |
4290 | * 10GB 10 0.9GB | |
4291 | * 100GB 31 3GB | |
4292 | * 1TB 101 10GB | |
4293 | * 10TB 320 32GB | |
4294 | */ | |
efab8186 | 4295 | static void setup_per_zone_inactive_ratio(void) |
556adecb RR |
4296 | { |
4297 | struct zone *zone; | |
4298 | ||
4299 | for_each_zone(zone) { | |
4300 | unsigned int gb, ratio; | |
4301 | ||
4302 | /* Zone size in gigabytes */ | |
4303 | gb = zone->present_pages >> (30 - PAGE_SHIFT); | |
4304 | ratio = int_sqrt(10 * gb); | |
4305 | if (!ratio) | |
4306 | ratio = 1; | |
4307 | ||
4308 | zone->inactive_ratio = ratio; | |
4309 | } | |
4310 | } | |
4311 | ||
1da177e4 LT |
4312 | /* |
4313 | * Initialise min_free_kbytes. | |
4314 | * | |
4315 | * For small machines we want it small (128k min). For large machines | |
4316 | * we want it large (64MB max). But it is not linear, because network | |
4317 | * bandwidth does not increase linearly with machine size. We use | |
4318 | * | |
4319 | * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy: | |
4320 | * min_free_kbytes = sqrt(lowmem_kbytes * 16) | |
4321 | * | |
4322 | * which yields | |
4323 | * | |
4324 | * 16MB: 512k | |
4325 | * 32MB: 724k | |
4326 | * 64MB: 1024k | |
4327 | * 128MB: 1448k | |
4328 | * 256MB: 2048k | |
4329 | * 512MB: 2896k | |
4330 | * 1024MB: 4096k | |
4331 | * 2048MB: 5792k | |
4332 | * 4096MB: 8192k | |
4333 | * 8192MB: 11584k | |
4334 | * 16384MB: 16384k | |
4335 | */ | |
4336 | static int __init init_per_zone_pages_min(void) | |
4337 | { | |
4338 | unsigned long lowmem_kbytes; | |
4339 | ||
4340 | lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10); | |
4341 | ||
4342 | min_free_kbytes = int_sqrt(lowmem_kbytes * 16); | |
4343 | if (min_free_kbytes < 128) | |
4344 | min_free_kbytes = 128; | |
4345 | if (min_free_kbytes > 65536) | |
4346 | min_free_kbytes = 65536; | |
4347 | setup_per_zone_pages_min(); | |
4348 | setup_per_zone_lowmem_reserve(); | |
556adecb | 4349 | setup_per_zone_inactive_ratio(); |
1da177e4 LT |
4350 | return 0; |
4351 | } | |
4352 | module_init(init_per_zone_pages_min) | |
4353 | ||
4354 | /* | |
4355 | * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so | |
4356 | * that we can call two helper functions whenever min_free_kbytes | |
4357 | * changes. | |
4358 | */ | |
4359 | int min_free_kbytes_sysctl_handler(ctl_table *table, int write, | |
4360 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) | |
4361 | { | |
4362 | proc_dointvec(table, write, file, buffer, length, ppos); | |
3b1d92c5 MG |
4363 | if (write) |
4364 | setup_per_zone_pages_min(); | |
1da177e4 LT |
4365 | return 0; |
4366 | } | |
4367 | ||
9614634f CL |
4368 | #ifdef CONFIG_NUMA |
4369 | int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write, | |
4370 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) | |
4371 | { | |
4372 | struct zone *zone; | |
4373 | int rc; | |
4374 | ||
4375 | rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos); | |
4376 | if (rc) | |
4377 | return rc; | |
4378 | ||
4379 | for_each_zone(zone) | |
8417bba4 | 4380 | zone->min_unmapped_pages = (zone->present_pages * |
9614634f CL |
4381 | sysctl_min_unmapped_ratio) / 100; |
4382 | return 0; | |
4383 | } | |
0ff38490 CL |
4384 | |
4385 | int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write, | |
4386 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) | |
4387 | { | |
4388 | struct zone *zone; | |
4389 | int rc; | |
4390 | ||
4391 | rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos); | |
4392 | if (rc) | |
4393 | return rc; | |
4394 | ||
4395 | for_each_zone(zone) | |
4396 | zone->min_slab_pages = (zone->present_pages * | |
4397 | sysctl_min_slab_ratio) / 100; | |
4398 | return 0; | |
4399 | } | |
9614634f CL |
4400 | #endif |
4401 | ||
1da177e4 LT |
4402 | /* |
4403 | * lowmem_reserve_ratio_sysctl_handler - just a wrapper around | |
4404 | * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve() | |
4405 | * whenever sysctl_lowmem_reserve_ratio changes. | |
4406 | * | |
4407 | * The reserve ratio obviously has absolutely no relation with the | |
4408 | * pages_min watermarks. The lowmem reserve ratio can only make sense | |
4409 | * if in function of the boot time zone sizes. | |
4410 | */ | |
4411 | int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write, | |
4412 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) | |
4413 | { | |
4414 | proc_dointvec_minmax(table, write, file, buffer, length, ppos); | |
4415 | setup_per_zone_lowmem_reserve(); | |
4416 | return 0; | |
4417 | } | |
4418 | ||
8ad4b1fb RS |
4419 | /* |
4420 | * percpu_pagelist_fraction - changes the pcp->high for each zone on each | |
4421 | * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist | |
4422 | * can have before it gets flushed back to buddy allocator. | |
4423 | */ | |
4424 | ||
4425 | int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write, | |
4426 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) | |
4427 | { | |
4428 | struct zone *zone; | |
4429 | unsigned int cpu; | |
4430 | int ret; | |
4431 | ||
4432 | ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos); | |
4433 | if (!write || (ret == -EINVAL)) | |
4434 | return ret; | |
4435 | for_each_zone(zone) { | |
4436 | for_each_online_cpu(cpu) { | |
4437 | unsigned long high; | |
4438 | high = zone->present_pages / percpu_pagelist_fraction; | |
4439 | setup_pagelist_highmark(zone_pcp(zone, cpu), high); | |
4440 | } | |
4441 | } | |
4442 | return 0; | |
4443 | } | |
4444 | ||
f034b5d4 | 4445 | int hashdist = HASHDIST_DEFAULT; |
1da177e4 LT |
4446 | |
4447 | #ifdef CONFIG_NUMA | |
4448 | static int __init set_hashdist(char *str) | |
4449 | { | |
4450 | if (!str) | |
4451 | return 0; | |
4452 | hashdist = simple_strtoul(str, &str, 0); | |
4453 | return 1; | |
4454 | } | |
4455 | __setup("hashdist=", set_hashdist); | |
4456 | #endif | |
4457 | ||
4458 | /* | |
4459 | * allocate a large system hash table from bootmem | |
4460 | * - it is assumed that the hash table must contain an exact power-of-2 | |
4461 | * quantity of entries | |
4462 | * - limit is the number of hash buckets, not the total allocation size | |
4463 | */ | |
4464 | void *__init alloc_large_system_hash(const char *tablename, | |
4465 | unsigned long bucketsize, | |
4466 | unsigned long numentries, | |
4467 | int scale, | |
4468 | int flags, | |
4469 | unsigned int *_hash_shift, | |
4470 | unsigned int *_hash_mask, | |
4471 | unsigned long limit) | |
4472 | { | |
4473 | unsigned long long max = limit; | |
4474 | unsigned long log2qty, size; | |
4475 | void *table = NULL; | |
4476 | ||
4477 | /* allow the kernel cmdline to have a say */ | |
4478 | if (!numentries) { | |
4479 | /* round applicable memory size up to nearest megabyte */ | |
04903664 | 4480 | numentries = nr_kernel_pages; |
1da177e4 LT |
4481 | numentries += (1UL << (20 - PAGE_SHIFT)) - 1; |
4482 | numentries >>= 20 - PAGE_SHIFT; | |
4483 | numentries <<= 20 - PAGE_SHIFT; | |
4484 | ||
4485 | /* limit to 1 bucket per 2^scale bytes of low memory */ | |
4486 | if (scale > PAGE_SHIFT) | |
4487 | numentries >>= (scale - PAGE_SHIFT); | |
4488 | else | |
4489 | numentries <<= (PAGE_SHIFT - scale); | |
9ab37b8f PM |
4490 | |
4491 | /* Make sure we've got at least a 0-order allocation.. */ | |
4492 | if (unlikely((numentries * bucketsize) < PAGE_SIZE)) | |
4493 | numentries = PAGE_SIZE / bucketsize; | |
1da177e4 | 4494 | } |
6e692ed3 | 4495 | numentries = roundup_pow_of_two(numentries); |
1da177e4 LT |
4496 | |
4497 | /* limit allocation size to 1/16 total memory by default */ | |
4498 | if (max == 0) { | |
4499 | max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4; | |
4500 | do_div(max, bucketsize); | |
4501 | } | |
4502 | ||
4503 | if (numentries > max) | |
4504 | numentries = max; | |
4505 | ||
f0d1b0b3 | 4506 | log2qty = ilog2(numentries); |
1da177e4 LT |
4507 | |
4508 | do { | |
4509 | size = bucketsize << log2qty; | |
4510 | if (flags & HASH_EARLY) | |
74768ed8 | 4511 | table = alloc_bootmem_nopanic(size); |
1da177e4 LT |
4512 | else if (hashdist) |
4513 | table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL); | |
4514 | else { | |
2309f9e6 | 4515 | unsigned long order = get_order(size); |
1da177e4 | 4516 | table = (void*) __get_free_pages(GFP_ATOMIC, order); |
1037b83b ED |
4517 | /* |
4518 | * If bucketsize is not a power-of-two, we may free | |
4519 | * some pages at the end of hash table. | |
4520 | */ | |
4521 | if (table) { | |
4522 | unsigned long alloc_end = (unsigned long)table + | |
4523 | (PAGE_SIZE << order); | |
4524 | unsigned long used = (unsigned long)table + | |
4525 | PAGE_ALIGN(size); | |
4526 | split_page(virt_to_page(table), order); | |
4527 | while (used < alloc_end) { | |
4528 | free_page(used); | |
4529 | used += PAGE_SIZE; | |
4530 | } | |
4531 | } | |
1da177e4 LT |
4532 | } |
4533 | } while (!table && size > PAGE_SIZE && --log2qty); | |
4534 | ||
4535 | if (!table) | |
4536 | panic("Failed to allocate %s hash table\n", tablename); | |
4537 | ||
b49ad484 | 4538 | printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n", |
1da177e4 LT |
4539 | tablename, |
4540 | (1U << log2qty), | |
f0d1b0b3 | 4541 | ilog2(size) - PAGE_SHIFT, |
1da177e4 LT |
4542 | size); |
4543 | ||
4544 | if (_hash_shift) | |
4545 | *_hash_shift = log2qty; | |
4546 | if (_hash_mask) | |
4547 | *_hash_mask = (1 << log2qty) - 1; | |
4548 | ||
4549 | return table; | |
4550 | } | |
a117e66e | 4551 | |
835c134e MG |
4552 | /* Return a pointer to the bitmap storing bits affecting a block of pages */ |
4553 | static inline unsigned long *get_pageblock_bitmap(struct zone *zone, | |
4554 | unsigned long pfn) | |
4555 | { | |
4556 | #ifdef CONFIG_SPARSEMEM | |
4557 | return __pfn_to_section(pfn)->pageblock_flags; | |
4558 | #else | |
4559 | return zone->pageblock_flags; | |
4560 | #endif /* CONFIG_SPARSEMEM */ | |
4561 | } | |
4562 | ||
4563 | static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn) | |
4564 | { | |
4565 | #ifdef CONFIG_SPARSEMEM | |
4566 | pfn &= (PAGES_PER_SECTION-1); | |
d9c23400 | 4567 | return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
835c134e MG |
4568 | #else |
4569 | pfn = pfn - zone->zone_start_pfn; | |
d9c23400 | 4570 | return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
835c134e MG |
4571 | #endif /* CONFIG_SPARSEMEM */ |
4572 | } | |
4573 | ||
4574 | /** | |
d9c23400 | 4575 | * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages |
835c134e MG |
4576 | * @page: The page within the block of interest |
4577 | * @start_bitidx: The first bit of interest to retrieve | |
4578 | * @end_bitidx: The last bit of interest | |
4579 | * returns pageblock_bits flags | |
4580 | */ | |
4581 | unsigned long get_pageblock_flags_group(struct page *page, | |
4582 | int start_bitidx, int end_bitidx) | |
4583 | { | |
4584 | struct zone *zone; | |
4585 | unsigned long *bitmap; | |
4586 | unsigned long pfn, bitidx; | |
4587 | unsigned long flags = 0; | |
4588 | unsigned long value = 1; | |
4589 | ||
4590 | zone = page_zone(page); | |
4591 | pfn = page_to_pfn(page); | |
4592 | bitmap = get_pageblock_bitmap(zone, pfn); | |
4593 | bitidx = pfn_to_bitidx(zone, pfn); | |
4594 | ||
4595 | for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1) | |
4596 | if (test_bit(bitidx + start_bitidx, bitmap)) | |
4597 | flags |= value; | |
6220ec78 | 4598 | |
835c134e MG |
4599 | return flags; |
4600 | } | |
4601 | ||
4602 | /** | |
d9c23400 | 4603 | * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages |
835c134e MG |
4604 | * @page: The page within the block of interest |
4605 | * @start_bitidx: The first bit of interest | |
4606 | * @end_bitidx: The last bit of interest | |
4607 | * @flags: The flags to set | |
4608 | */ | |
4609 | void set_pageblock_flags_group(struct page *page, unsigned long flags, | |
4610 | int start_bitidx, int end_bitidx) | |
4611 | { | |
4612 | struct zone *zone; | |
4613 | unsigned long *bitmap; | |
4614 | unsigned long pfn, bitidx; | |
4615 | unsigned long value = 1; | |
4616 | ||
4617 | zone = page_zone(page); | |
4618 | pfn = page_to_pfn(page); | |
4619 | bitmap = get_pageblock_bitmap(zone, pfn); | |
4620 | bitidx = pfn_to_bitidx(zone, pfn); | |
86051ca5 KH |
4621 | VM_BUG_ON(pfn < zone->zone_start_pfn); |
4622 | VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages); | |
835c134e MG |
4623 | |
4624 | for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1) | |
4625 | if (flags & value) | |
4626 | __set_bit(bitidx + start_bitidx, bitmap); | |
4627 | else | |
4628 | __clear_bit(bitidx + start_bitidx, bitmap); | |
4629 | } | |
a5d76b54 KH |
4630 | |
4631 | /* | |
4632 | * This is designed as sub function...plz see page_isolation.c also. | |
4633 | * set/clear page block's type to be ISOLATE. | |
4634 | * page allocater never alloc memory from ISOLATE block. | |
4635 | */ | |
4636 | ||
4637 | int set_migratetype_isolate(struct page *page) | |
4638 | { | |
4639 | struct zone *zone; | |
4640 | unsigned long flags; | |
4641 | int ret = -EBUSY; | |
4642 | ||
4643 | zone = page_zone(page); | |
4644 | spin_lock_irqsave(&zone->lock, flags); | |
4645 | /* | |
4646 | * In future, more migrate types will be able to be isolation target. | |
4647 | */ | |
4648 | if (get_pageblock_migratetype(page) != MIGRATE_MOVABLE) | |
4649 | goto out; | |
4650 | set_pageblock_migratetype(page, MIGRATE_ISOLATE); | |
4651 | move_freepages_block(zone, page, MIGRATE_ISOLATE); | |
4652 | ret = 0; | |
4653 | out: | |
4654 | spin_unlock_irqrestore(&zone->lock, flags); | |
4655 | if (!ret) | |
9f8f2172 | 4656 | drain_all_pages(); |
a5d76b54 KH |
4657 | return ret; |
4658 | } | |
4659 | ||
4660 | void unset_migratetype_isolate(struct page *page) | |
4661 | { | |
4662 | struct zone *zone; | |
4663 | unsigned long flags; | |
4664 | zone = page_zone(page); | |
4665 | spin_lock_irqsave(&zone->lock, flags); | |
4666 | if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE) | |
4667 | goto out; | |
4668 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); | |
4669 | move_freepages_block(zone, page, MIGRATE_MOVABLE); | |
4670 | out: | |
4671 | spin_unlock_irqrestore(&zone->lock, flags); | |
4672 | } | |
0c0e6195 KH |
4673 | |
4674 | #ifdef CONFIG_MEMORY_HOTREMOVE | |
4675 | /* | |
4676 | * All pages in the range must be isolated before calling this. | |
4677 | */ | |
4678 | void | |
4679 | __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) | |
4680 | { | |
4681 | struct page *page; | |
4682 | struct zone *zone; | |
4683 | int order, i; | |
4684 | unsigned long pfn; | |
4685 | unsigned long flags; | |
4686 | /* find the first valid pfn */ | |
4687 | for (pfn = start_pfn; pfn < end_pfn; pfn++) | |
4688 | if (pfn_valid(pfn)) | |
4689 | break; | |
4690 | if (pfn == end_pfn) | |
4691 | return; | |
4692 | zone = page_zone(pfn_to_page(pfn)); | |
4693 | spin_lock_irqsave(&zone->lock, flags); | |
4694 | pfn = start_pfn; | |
4695 | while (pfn < end_pfn) { | |
4696 | if (!pfn_valid(pfn)) { | |
4697 | pfn++; | |
4698 | continue; | |
4699 | } | |
4700 | page = pfn_to_page(pfn); | |
4701 | BUG_ON(page_count(page)); | |
4702 | BUG_ON(!PageBuddy(page)); | |
4703 | order = page_order(page); | |
4704 | #ifdef CONFIG_DEBUG_VM | |
4705 | printk(KERN_INFO "remove from free list %lx %d %lx\n", | |
4706 | pfn, 1 << order, end_pfn); | |
4707 | #endif | |
4708 | list_del(&page->lru); | |
4709 | rmv_page_order(page); | |
4710 | zone->free_area[order].nr_free--; | |
4711 | __mod_zone_page_state(zone, NR_FREE_PAGES, | |
4712 | - (1UL << order)); | |
4713 | for (i = 0; i < (1 << order); i++) | |
4714 | SetPageReserved((page+i)); | |
4715 | pfn += (1 << order); | |
4716 | } | |
4717 | spin_unlock_irqrestore(&zone->lock, flags); | |
4718 | } | |
4719 | #endif |