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