Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sparse memory mappings. |
| 3 | */ |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 4 | #include <linux/mm.h> |
| 5 | #include <linux/mmzone.h> |
| 6 | #include <linux/bootmem.h> |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 7 | #include <linux/highmem.h> |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 10 | #include <linux/vmalloc.h> |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 11 | #include <asm/dma.h> |
| 12 | |
| 13 | /* |
| 14 | * Permanent SPARSEMEM data: |
| 15 | * |
| 16 | * 1) mem_section - memory sections, mem_map's for valid memory |
| 17 | */ |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 18 | #ifdef CONFIG_SPARSEMEM_EXTREME |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 19 | struct mem_section *mem_section[NR_SECTION_ROOTS] |
Ravikiran G Thirumalai | 22fc6ec | 2006-01-08 01:01:27 -0800 | [diff] [blame] | 20 | ____cacheline_internodealigned_in_smp; |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 21 | #else |
| 22 | struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT] |
Ravikiran G Thirumalai | 22fc6ec | 2006-01-08 01:01:27 -0800 | [diff] [blame] | 23 | ____cacheline_internodealigned_in_smp; |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 24 | #endif |
| 25 | EXPORT_SYMBOL(mem_section); |
| 26 | |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 27 | #ifdef NODE_NOT_IN_PAGE_FLAGS |
| 28 | /* |
| 29 | * If we did not store the node number in the page then we have to |
| 30 | * do a lookup in the section_to_node_table in order to find which |
| 31 | * node the page belongs to. |
| 32 | */ |
| 33 | #if MAX_NUMNODES <= 256 |
| 34 | static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; |
| 35 | #else |
| 36 | static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; |
| 37 | #endif |
| 38 | |
Andy Whitcroft | 25ba77c | 2006-12-06 20:33:03 -0800 | [diff] [blame] | 39 | int page_to_nid(struct page *page) |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 40 | { |
| 41 | return section_to_node_table[page_to_section(page)]; |
| 42 | } |
| 43 | EXPORT_SYMBOL(page_to_nid); |
Andy Whitcroft | 85770ff | 2007-08-22 14:01:03 -0700 | [diff] [blame] | 44 | |
| 45 | static void set_section_nid(unsigned long section_nr, int nid) |
| 46 | { |
| 47 | section_to_node_table[section_nr] = nid; |
| 48 | } |
| 49 | #else /* !NODE_NOT_IN_PAGE_FLAGS */ |
| 50 | static inline void set_section_nid(unsigned long section_nr, int nid) |
| 51 | { |
| 52 | } |
Christoph Lameter | 89689ae | 2006-12-06 20:31:45 -0800 | [diff] [blame] | 53 | #endif |
| 54 | |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 55 | #ifdef CONFIG_SPARSEMEM_EXTREME |
Sam Ravnborg | 577a32f | 2007-05-17 23:29:25 +0200 | [diff] [blame] | 56 | static struct mem_section noinline __init_refok *sparse_index_alloc(int nid) |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 57 | { |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 58 | struct mem_section *section = NULL; |
| 59 | unsigned long array_size = SECTIONS_PER_ROOT * |
| 60 | sizeof(struct mem_section); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 61 | |
Mike Kravetz | 39d24e6 | 2006-05-15 09:44:13 -0700 | [diff] [blame] | 62 | if (slab_is_available()) |
Mike Kravetz | 46a66ee | 2006-05-01 12:16:09 -0700 | [diff] [blame] | 63 | section = kmalloc_node(array_size, GFP_KERNEL, nid); |
| 64 | else |
| 65 | section = alloc_bootmem_node(NODE_DATA(nid), array_size); |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 66 | |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 67 | if (section) |
| 68 | memset(section, 0, array_size); |
Bob Picco | 3e34726 | 2005-09-03 15:54:28 -0700 | [diff] [blame] | 69 | |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 70 | return section; |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 71 | } |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 72 | |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 73 | static int __meminit sparse_index_init(unsigned long section_nr, int nid) |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 74 | { |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 75 | static DEFINE_SPINLOCK(index_init_lock); |
Dave Hansen | 28ae55c | 2005-09-03 15:54:29 -0700 | [diff] [blame] | 76 | unsigned long root = SECTION_NR_TO_ROOT(section_nr); |
| 77 | struct mem_section *section; |
| 78 | int ret = 0; |
| 79 | |
| 80 | if (mem_section[root]) |
| 81 | return -EEXIST; |
| 82 | |
| 83 | section = sparse_index_alloc(nid); |
| 84 | /* |
| 85 | * This lock keeps two different sections from |
| 86 | * reallocating for the same index |
| 87 | */ |
| 88 | spin_lock(&index_init_lock); |
| 89 | |
| 90 | if (mem_section[root]) { |
| 91 | ret = -EEXIST; |
| 92 | goto out; |
| 93 | } |
| 94 | |
| 95 | mem_section[root] = section; |
| 96 | out: |
| 97 | spin_unlock(&index_init_lock); |
| 98 | return ret; |
| 99 | } |
| 100 | #else /* !SPARSEMEM_EXTREME */ |
| 101 | static inline int sparse_index_init(unsigned long section_nr, int nid) |
| 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | #endif |
| 106 | |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Although written for the SPARSEMEM_EXTREME case, this happens |
| 109 | * to also work for the flat array case becase |
| 110 | * NR_SECTION_ROOTS==NR_MEM_SECTIONS. |
| 111 | */ |
| 112 | int __section_nr(struct mem_section* ms) |
| 113 | { |
| 114 | unsigned long root_nr; |
| 115 | struct mem_section* root; |
| 116 | |
Mike Kravetz | 12783b0 | 2006-05-20 15:00:05 -0700 | [diff] [blame] | 117 | for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) { |
| 118 | root = __nr_to_section(root_nr * SECTIONS_PER_ROOT); |
Dave Hansen | 4ca644d | 2005-10-29 18:16:51 -0700 | [diff] [blame] | 119 | if (!root) |
| 120 | continue; |
| 121 | |
| 122 | if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT))) |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | return (root_nr * SECTIONS_PER_ROOT) + (ms - root); |
| 127 | } |
| 128 | |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 129 | /* |
| 130 | * During early boot, before section_mem_map is used for an actual |
| 131 | * mem_map, we use section_mem_map to store the section's NUMA |
| 132 | * node. This keeps us from having to use another data structure. The |
| 133 | * node information is cleared just before we store the real mem_map. |
| 134 | */ |
| 135 | static inline unsigned long sparse_encode_early_nid(int nid) |
| 136 | { |
| 137 | return (nid << SECTION_NID_SHIFT); |
| 138 | } |
| 139 | |
| 140 | static inline int sparse_early_nid(struct mem_section *section) |
| 141 | { |
| 142 | return (section->section_mem_map >> SECTION_NID_SHIFT); |
| 143 | } |
| 144 | |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 145 | /* Record a memory area against a node. */ |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 146 | void __init memory_present(int nid, unsigned long start, unsigned long end) |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 147 | { |
| 148 | unsigned long pfn; |
| 149 | |
| 150 | start &= PAGE_SECTION_MASK; |
| 151 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) { |
| 152 | unsigned long section = pfn_to_section_nr(pfn); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 153 | struct mem_section *ms; |
| 154 | |
| 155 | sparse_index_init(section, nid); |
Andy Whitcroft | 85770ff | 2007-08-22 14:01:03 -0700 | [diff] [blame] | 156 | set_section_nid(section, nid); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 157 | |
| 158 | ms = __nr_to_section(section); |
| 159 | if (!ms->section_mem_map) |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 160 | ms->section_mem_map = sparse_encode_early_nid(nid) | |
| 161 | SECTION_MARKED_PRESENT; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Only used by the i386 NUMA architecures, but relatively |
| 167 | * generic code. |
| 168 | */ |
| 169 | unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn, |
| 170 | unsigned long end_pfn) |
| 171 | { |
| 172 | unsigned long pfn; |
| 173 | unsigned long nr_pages = 0; |
| 174 | |
| 175 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 176 | if (nid != early_pfn_to_nid(pfn)) |
| 177 | continue; |
| 178 | |
| 179 | if (pfn_valid(pfn)) |
| 180 | nr_pages += PAGES_PER_SECTION; |
| 181 | } |
| 182 | |
| 183 | return nr_pages * sizeof(struct page); |
| 184 | } |
| 185 | |
| 186 | /* |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 187 | * Subtle, we encode the real pfn into the mem_map such that |
| 188 | * the identity pfn - section_mem_map will return the actual |
| 189 | * physical page frame number. |
| 190 | */ |
| 191 | static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum) |
| 192 | { |
| 193 | return (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * We need this if we ever free the mem_maps. While not implemented yet, |
| 198 | * this function is included for parity with its sibling. |
| 199 | */ |
| 200 | static __attribute((unused)) |
| 201 | struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum) |
| 202 | { |
| 203 | return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum); |
| 204 | } |
| 205 | |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 206 | static int __meminit sparse_init_one_section(struct mem_section *ms, |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 207 | unsigned long pnum, struct page *mem_map) |
| 208 | { |
| 209 | if (!valid_section(ms)) |
| 210 | return -EINVAL; |
| 211 | |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 212 | ms->section_mem_map &= ~SECTION_MAP_MASK; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 213 | ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum); |
| 214 | |
| 215 | return 1; |
| 216 | } |
| 217 | |
Sam Ravnborg | dec2e6b | 2007-07-22 11:12:44 +0200 | [diff] [blame] | 218 | __attribute__((weak)) __init |
Zou Nan hai | 2e1c49d | 2007-06-01 00:46:28 -0700 | [diff] [blame] | 219 | void *alloc_bootmem_high_node(pg_data_t *pgdat, unsigned long size) |
| 220 | { |
| 221 | return NULL; |
| 222 | } |
| 223 | |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 224 | static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum) |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 225 | { |
| 226 | struct page *map; |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 227 | struct mem_section *ms = __nr_to_section(pnum); |
Andy Whitcroft | 30c253e | 2006-06-23 02:03:41 -0700 | [diff] [blame] | 228 | int nid = sparse_early_nid(ms); |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 229 | |
| 230 | map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION); |
| 231 | if (map) |
| 232 | return map; |
| 233 | |
Zou Nan hai | 2e1c49d | 2007-06-01 00:46:28 -0700 | [diff] [blame] | 234 | map = alloc_bootmem_high_node(NODE_DATA(nid), |
| 235 | sizeof(struct page) * PAGES_PER_SECTION); |
| 236 | if (map) |
| 237 | return map; |
| 238 | |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 239 | map = alloc_bootmem_node(NODE_DATA(nid), |
| 240 | sizeof(struct page) * PAGES_PER_SECTION); |
| 241 | if (map) |
| 242 | return map; |
| 243 | |
| 244 | printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__); |
Bob Picco | 802f192 | 2005-09-03 15:54:26 -0700 | [diff] [blame] | 245 | ms->section_mem_map = 0; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 246 | return NULL; |
| 247 | } |
| 248 | |
Stephen Rothwell | 193faea | 2007-06-08 13:46:51 -0700 | [diff] [blame] | 249 | /* |
| 250 | * Allocate the accumulated non-linear sections, allocate a mem_map |
| 251 | * for each and record the physical to section mapping. |
| 252 | */ |
| 253 | void __init sparse_init(void) |
| 254 | { |
| 255 | unsigned long pnum; |
| 256 | struct page *map; |
| 257 | |
| 258 | for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) { |
| 259 | if (!valid_section_nr(pnum)) |
| 260 | continue; |
| 261 | |
| 262 | map = sparse_early_mem_map_alloc(pnum); |
| 263 | if (!map) |
| 264 | continue; |
| 265 | sparse_init_one_section(__nr_to_section(pnum), pnum, map); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #ifdef CONFIG_MEMORY_HOTPLUG |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 270 | static struct page *__kmalloc_section_memmap(unsigned long nr_pages) |
| 271 | { |
| 272 | struct page *page, *ret; |
| 273 | unsigned long memmap_size = sizeof(struct page) * nr_pages; |
| 274 | |
Yasunori Goto | f2d0aa5 | 2006-10-28 10:38:32 -0700 | [diff] [blame] | 275 | page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size)); |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 276 | if (page) |
| 277 | goto got_map_page; |
| 278 | |
| 279 | ret = vmalloc(memmap_size); |
| 280 | if (ret) |
| 281 | goto got_map_ptr; |
| 282 | |
| 283 | return NULL; |
| 284 | got_map_page: |
| 285 | ret = (struct page *)pfn_to_kaddr(page_to_pfn(page)); |
| 286 | got_map_ptr: |
| 287 | memset(ret, 0, memmap_size); |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int vaddr_in_vmalloc_area(void *addr) |
| 293 | { |
| 294 | if (addr >= (void *)VMALLOC_START && |
| 295 | addr < (void *)VMALLOC_END) |
| 296 | return 1; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages) |
| 301 | { |
| 302 | if (vaddr_in_vmalloc_area(memmap)) |
| 303 | vfree(memmap); |
| 304 | else |
| 305 | free_pages((unsigned long)memmap, |
| 306 | get_order(sizeof(struct page) * nr_pages)); |
| 307 | } |
| 308 | |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 309 | /* |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 310 | * returns the number of sections whose mem_maps were properly |
| 311 | * set. If this is <=0, then that means that the passed-in |
| 312 | * map was not consumed and must be freed. |
| 313 | */ |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 314 | int sparse_add_one_section(struct zone *zone, unsigned long start_pfn, |
| 315 | int nr_pages) |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 316 | { |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 317 | unsigned long section_nr = pfn_to_section_nr(start_pfn); |
| 318 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 319 | struct mem_section *ms; |
| 320 | struct page *memmap; |
| 321 | unsigned long flags; |
| 322 | int ret; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 323 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 324 | /* |
| 325 | * no locking for this, because it does its own |
| 326 | * plus, it does a kmalloc |
| 327 | */ |
| 328 | sparse_index_init(section_nr, pgdat->node_id); |
| 329 | memmap = __kmalloc_section_memmap(nr_pages); |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 330 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 331 | pgdat_resize_lock(pgdat, &flags); |
| 332 | |
| 333 | ms = __pfn_to_section(start_pfn); |
| 334 | if (ms->section_mem_map & SECTION_MARKED_PRESENT) { |
| 335 | ret = -EEXIST; |
| 336 | goto out; |
| 337 | } |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 338 | ms->section_mem_map |= SECTION_MARKED_PRESENT; |
| 339 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 340 | ret = sparse_init_one_section(ms, section_nr, memmap); |
| 341 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 342 | out: |
| 343 | pgdat_resize_unlock(pgdat, &flags); |
Mike Kravetz | 46a66ee | 2006-05-01 12:16:09 -0700 | [diff] [blame] | 344 | if (ret <= 0) |
| 345 | __kfree_section_memmap(memmap, nr_pages); |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 346 | return ret; |
Andy Whitcroft | 29751f6 | 2005-06-23 00:08:00 -0700 | [diff] [blame] | 347 | } |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 348 | #endif |