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