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