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