Merge origin/master into drm-misc-fixes
[linux/fpc-iii.git] / mm / sparse.c
blob7b4be3fd5cac074177b7f17ebdeb12dfa56d22e5
1 /*
2 * sparse memory mappings.
3 */
4 #include <linux/mm.h>
5 #include <linux/slab.h>
6 #include <linux/mmzone.h>
7 #include <linux/bootmem.h>
8 #include <linux/compiler.h>
9 #include <linux/highmem.h>
10 #include <linux/export.h>
11 #include <linux/spinlock.h>
12 #include <linux/vmalloc.h>
14 #include "internal.h"
15 #include <asm/dma.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
20 * Permanent SPARSEMEM data:
22 * 1) mem_section - memory sections, mem_map's for valid memory
24 #ifdef CONFIG_SPARSEMEM_EXTREME
25 struct mem_section *mem_section[NR_SECTION_ROOTS]
26 ____cacheline_internodealigned_in_smp;
27 #else
28 struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
29 ____cacheline_internodealigned_in_smp;
30 #endif
31 EXPORT_SYMBOL(mem_section);
33 #ifdef NODE_NOT_IN_PAGE_FLAGS
35 * If we did not store the node number in the page then we have to
36 * do a lookup in the section_to_node_table in order to find which
37 * node the page belongs to.
39 #if MAX_NUMNODES <= 256
40 static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
41 #else
42 static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
43 #endif
45 int page_to_nid(const struct page *page)
47 return section_to_node_table[page_to_section(page)];
49 EXPORT_SYMBOL(page_to_nid);
51 static void set_section_nid(unsigned long section_nr, int nid)
53 section_to_node_table[section_nr] = nid;
55 #else /* !NODE_NOT_IN_PAGE_FLAGS */
56 static inline void set_section_nid(unsigned long section_nr, int nid)
59 #endif
61 #ifdef CONFIG_SPARSEMEM_EXTREME
62 static noinline struct mem_section __ref *sparse_index_alloc(int nid)
64 struct mem_section *section = NULL;
65 unsigned long array_size = SECTIONS_PER_ROOT *
66 sizeof(struct mem_section);
68 if (slab_is_available()) {
69 if (node_state(nid, N_HIGH_MEMORY))
70 section = kzalloc_node(array_size, GFP_KERNEL, nid);
71 else
72 section = kzalloc(array_size, GFP_KERNEL);
73 } else {
74 section = memblock_virt_alloc_node(array_size, nid);
77 return section;
80 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
82 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
83 struct mem_section *section;
85 if (mem_section[root])
86 return -EEXIST;
88 section = sparse_index_alloc(nid);
89 if (!section)
90 return -ENOMEM;
92 mem_section[root] = section;
94 return 0;
96 #else /* !SPARSEMEM_EXTREME */
97 static inline int sparse_index_init(unsigned long section_nr, int nid)
99 return 0;
101 #endif
103 #ifdef CONFIG_SPARSEMEM_EXTREME
104 int __section_nr(struct mem_section* ms)
106 unsigned long root_nr;
107 struct mem_section* root;
109 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
110 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
111 if (!root)
112 continue;
114 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
115 break;
118 VM_BUG_ON(root_nr == NR_SECTION_ROOTS);
120 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
122 #else
123 int __section_nr(struct mem_section* ms)
125 return (int)(ms - mem_section[0]);
127 #endif
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.
135 static inline unsigned long sparse_encode_early_nid(int nid)
137 return (nid << SECTION_NID_SHIFT);
140 static inline int sparse_early_nid(struct mem_section *section)
142 return (section->section_mem_map >> SECTION_NID_SHIFT);
145 /* Validate the physical addressing limitations of the model */
146 void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
147 unsigned long *end_pfn)
149 unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
152 * Sanity checks - do not allow an architecture to pass
153 * in larger pfns than the maximum scope of sparsemem:
155 if (*start_pfn > max_sparsemem_pfn) {
156 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
157 "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
158 *start_pfn, *end_pfn, max_sparsemem_pfn);
159 WARN_ON_ONCE(1);
160 *start_pfn = max_sparsemem_pfn;
161 *end_pfn = max_sparsemem_pfn;
162 } else if (*end_pfn > max_sparsemem_pfn) {
163 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
164 "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
165 *start_pfn, *end_pfn, max_sparsemem_pfn);
166 WARN_ON_ONCE(1);
167 *end_pfn = max_sparsemem_pfn;
172 * There are a number of times that we loop over NR_MEM_SECTIONS,
173 * looking for section_present() on each. But, when we have very
174 * large physical address spaces, NR_MEM_SECTIONS can also be
175 * very large which makes the loops quite long.
177 * Keeping track of this gives us an easy way to break out of
178 * those loops early.
180 int __highest_present_section_nr;
181 static void section_mark_present(struct mem_section *ms)
183 int section_nr = __section_nr(ms);
185 if (section_nr > __highest_present_section_nr)
186 __highest_present_section_nr = section_nr;
188 ms->section_mem_map |= SECTION_MARKED_PRESENT;
191 static inline int next_present_section_nr(int section_nr)
193 do {
194 section_nr++;
195 if (present_section_nr(section_nr))
196 return section_nr;
197 } while ((section_nr < NR_MEM_SECTIONS) &&
198 (section_nr <= __highest_present_section_nr));
200 return -1;
202 #define for_each_present_section_nr(start, section_nr) \
203 for (section_nr = next_present_section_nr(start-1); \
204 ((section_nr >= 0) && \
205 (section_nr < NR_MEM_SECTIONS) && \
206 (section_nr <= __highest_present_section_nr)); \
207 section_nr = next_present_section_nr(section_nr))
209 /* Record a memory area against a node. */
210 void __init memory_present(int nid, unsigned long start, unsigned long end)
212 unsigned long pfn;
214 start &= PAGE_SECTION_MASK;
215 mminit_validate_memmodel_limits(&start, &end);
216 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
217 unsigned long section = pfn_to_section_nr(pfn);
218 struct mem_section *ms;
220 sparse_index_init(section, nid);
221 set_section_nid(section, nid);
223 ms = __nr_to_section(section);
224 if (!ms->section_mem_map) {
225 ms->section_mem_map = sparse_encode_early_nid(nid) |
226 SECTION_IS_ONLINE;
227 section_mark_present(ms);
233 * Only used by the i386 NUMA architecures, but relatively
234 * generic code.
236 unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
237 unsigned long end_pfn)
239 unsigned long pfn;
240 unsigned long nr_pages = 0;
242 mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
243 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
244 if (nid != early_pfn_to_nid(pfn))
245 continue;
247 if (pfn_present(pfn))
248 nr_pages += PAGES_PER_SECTION;
251 return nr_pages * sizeof(struct page);
255 * Subtle, we encode the real pfn into the mem_map such that
256 * the identity pfn - section_mem_map will return the actual
257 * physical page frame number.
259 static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
261 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
265 * Decode mem_map from the coded memmap
267 struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
269 /* mask off the extra low bits of information */
270 coded_mem_map &= SECTION_MAP_MASK;
271 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
274 static int __meminit sparse_init_one_section(struct mem_section *ms,
275 unsigned long pnum, struct page *mem_map,
276 unsigned long *pageblock_bitmap)
278 if (!present_section(ms))
279 return -EINVAL;
281 ms->section_mem_map &= ~SECTION_MAP_MASK;
282 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
283 SECTION_HAS_MEM_MAP;
284 ms->pageblock_flags = pageblock_bitmap;
286 return 1;
289 unsigned long usemap_size(void)
291 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
294 #ifdef CONFIG_MEMORY_HOTPLUG
295 static unsigned long *__kmalloc_section_usemap(void)
297 return kmalloc(usemap_size(), GFP_KERNEL);
299 #endif /* CONFIG_MEMORY_HOTPLUG */
301 #ifdef CONFIG_MEMORY_HOTREMOVE
302 static unsigned long * __init
303 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
304 unsigned long size)
306 unsigned long goal, limit;
307 unsigned long *p;
308 int nid;
310 * A page may contain usemaps for other sections preventing the
311 * page being freed and making a section unremovable while
312 * other sections referencing the usemap remain active. Similarly,
313 * a pgdat can prevent a section being removed. If section A
314 * contains a pgdat and section B contains the usemap, both
315 * sections become inter-dependent. This allocates usemaps
316 * from the same section as the pgdat where possible to avoid
317 * this problem.
319 goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
320 limit = goal + (1UL << PA_SECTION_SHIFT);
321 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
322 again:
323 p = memblock_virt_alloc_try_nid_nopanic(size,
324 SMP_CACHE_BYTES, goal, limit,
325 nid);
326 if (!p && limit) {
327 limit = 0;
328 goto again;
330 return p;
333 static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
335 unsigned long usemap_snr, pgdat_snr;
336 static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
337 static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
338 struct pglist_data *pgdat = NODE_DATA(nid);
339 int usemap_nid;
341 usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
342 pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
343 if (usemap_snr == pgdat_snr)
344 return;
346 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
347 /* skip redundant message */
348 return;
350 old_usemap_snr = usemap_snr;
351 old_pgdat_snr = pgdat_snr;
353 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
354 if (usemap_nid != nid) {
355 pr_info("node %d must be removed before remove section %ld\n",
356 nid, usemap_snr);
357 return;
360 * There is a circular dependency.
361 * Some platforms allow un-removable section because they will just
362 * gather other removable sections for dynamic partitioning.
363 * Just notify un-removable section's number here.
365 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
366 usemap_snr, pgdat_snr, nid);
368 #else
369 static unsigned long * __init
370 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
371 unsigned long size)
373 return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
376 static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
379 #endif /* CONFIG_MEMORY_HOTREMOVE */
381 static void __init sparse_early_usemaps_alloc_node(void *data,
382 unsigned long pnum_begin,
383 unsigned long pnum_end,
384 unsigned long usemap_count, int nodeid)
386 void *usemap;
387 unsigned long pnum;
388 unsigned long **usemap_map = (unsigned long **)data;
389 int size = usemap_size();
391 usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
392 size * usemap_count);
393 if (!usemap) {
394 pr_warn("%s: allocation failed\n", __func__);
395 return;
398 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
399 if (!present_section_nr(pnum))
400 continue;
401 usemap_map[pnum] = usemap;
402 usemap += size;
403 check_usemap_section_nr(nodeid, usemap_map[pnum]);
407 #ifndef CONFIG_SPARSEMEM_VMEMMAP
408 struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
410 struct page *map;
411 unsigned long size;
413 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
414 if (map)
415 return map;
417 size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
418 map = memblock_virt_alloc_try_nid(size,
419 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
420 BOOTMEM_ALLOC_ACCESSIBLE, nid);
421 return map;
423 void __init sparse_mem_maps_populate_node(struct page **map_map,
424 unsigned long pnum_begin,
425 unsigned long pnum_end,
426 unsigned long map_count, int nodeid)
428 void *map;
429 unsigned long pnum;
430 unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
432 map = alloc_remap(nodeid, size * map_count);
433 if (map) {
434 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
435 if (!present_section_nr(pnum))
436 continue;
437 map_map[pnum] = map;
438 map += size;
440 return;
443 size = PAGE_ALIGN(size);
444 map = memblock_virt_alloc_try_nid(size * map_count,
445 PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
446 BOOTMEM_ALLOC_ACCESSIBLE, nodeid);
447 if (map) {
448 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
449 if (!present_section_nr(pnum))
450 continue;
451 map_map[pnum] = map;
452 map += size;
454 return;
457 /* fallback */
458 for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
459 struct mem_section *ms;
461 if (!present_section_nr(pnum))
462 continue;
463 map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
464 if (map_map[pnum])
465 continue;
466 ms = __nr_to_section(pnum);
467 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
468 __func__);
469 ms->section_mem_map = 0;
472 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
474 #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
475 static void __init sparse_early_mem_maps_alloc_node(void *data,
476 unsigned long pnum_begin,
477 unsigned long pnum_end,
478 unsigned long map_count, int nodeid)
480 struct page **map_map = (struct page **)data;
481 sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
482 map_count, nodeid);
484 #else
485 static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
487 struct page *map;
488 struct mem_section *ms = __nr_to_section(pnum);
489 int nid = sparse_early_nid(ms);
491 map = sparse_mem_map_populate(pnum, nid);
492 if (map)
493 return map;
495 pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
496 __func__);
497 ms->section_mem_map = 0;
498 return NULL;
500 #endif
502 void __weak __meminit vmemmap_populate_print_last(void)
507 * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
508 * @map: usemap_map for pageblock flags or mmap_map for vmemmap
510 static void __init alloc_usemap_and_memmap(void (*alloc_func)
511 (void *, unsigned long, unsigned long,
512 unsigned long, int), void *data)
514 unsigned long pnum;
515 unsigned long map_count;
516 int nodeid_begin = 0;
517 unsigned long pnum_begin = 0;
519 for_each_present_section_nr(0, pnum) {
520 struct mem_section *ms;
522 ms = __nr_to_section(pnum);
523 nodeid_begin = sparse_early_nid(ms);
524 pnum_begin = pnum;
525 break;
527 map_count = 1;
528 for_each_present_section_nr(pnum_begin + 1, pnum) {
529 struct mem_section *ms;
530 int nodeid;
532 ms = __nr_to_section(pnum);
533 nodeid = sparse_early_nid(ms);
534 if (nodeid == nodeid_begin) {
535 map_count++;
536 continue;
538 /* ok, we need to take cake of from pnum_begin to pnum - 1*/
539 alloc_func(data, pnum_begin, pnum,
540 map_count, nodeid_begin);
541 /* new start, update count etc*/
542 nodeid_begin = nodeid;
543 pnum_begin = pnum;
544 map_count = 1;
546 /* ok, last chunk */
547 alloc_func(data, pnum_begin, NR_MEM_SECTIONS,
548 map_count, nodeid_begin);
552 * Allocate the accumulated non-linear sections, allocate a mem_map
553 * for each and record the physical to section mapping.
555 void __init sparse_init(void)
557 unsigned long pnum;
558 struct page *map;
559 unsigned long *usemap;
560 unsigned long **usemap_map;
561 int size;
562 #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
563 int size2;
564 struct page **map_map;
565 #endif
567 /* see include/linux/mmzone.h 'struct mem_section' definition */
568 BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
570 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
571 set_pageblock_order();
574 * map is using big page (aka 2M in x86 64 bit)
575 * usemap is less one page (aka 24 bytes)
576 * so alloc 2M (with 2M align) and 24 bytes in turn will
577 * make next 2M slip to one more 2M later.
578 * then in big system, the memory will have a lot of holes...
579 * here try to allocate 2M pages continuously.
581 * powerpc need to call sparse_init_one_section right after each
582 * sparse_early_mem_map_alloc, so allocate usemap_map at first.
584 size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
585 usemap_map = memblock_virt_alloc(size, 0);
586 if (!usemap_map)
587 panic("can not allocate usemap_map\n");
588 alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
589 (void *)usemap_map);
591 #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
592 size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
593 map_map = memblock_virt_alloc(size2, 0);
594 if (!map_map)
595 panic("can not allocate map_map\n");
596 alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
597 (void *)map_map);
598 #endif
600 for_each_present_section_nr(0, pnum) {
601 usemap = usemap_map[pnum];
602 if (!usemap)
603 continue;
605 #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
606 map = map_map[pnum];
607 #else
608 map = sparse_early_mem_map_alloc(pnum);
609 #endif
610 if (!map)
611 continue;
613 sparse_init_one_section(__nr_to_section(pnum), pnum, map,
614 usemap);
617 vmemmap_populate_print_last();
619 #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
620 memblock_free_early(__pa(map_map), size2);
621 #endif
622 memblock_free_early(__pa(usemap_map), size);
625 #ifdef CONFIG_MEMORY_HOTPLUG
627 /* Mark all memory sections within the pfn range as online */
628 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
630 unsigned long pfn;
632 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
633 unsigned long section_nr = pfn_to_section_nr(start_pfn);
634 struct mem_section *ms;
636 /* onlining code should never touch invalid ranges */
637 if (WARN_ON(!valid_section_nr(section_nr)))
638 continue;
640 ms = __nr_to_section(section_nr);
641 ms->section_mem_map |= SECTION_IS_ONLINE;
645 #ifdef CONFIG_MEMORY_HOTREMOVE
646 /* Mark all memory sections within the pfn range as online */
647 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
649 unsigned long pfn;
651 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
652 unsigned long section_nr = pfn_to_section_nr(start_pfn);
653 struct mem_section *ms;
656 * TODO this needs some double checking. Offlining code makes
657 * sure to check pfn_valid but those checks might be just bogus
659 if (WARN_ON(!valid_section_nr(section_nr)))
660 continue;
662 ms = __nr_to_section(section_nr);
663 ms->section_mem_map &= ~SECTION_IS_ONLINE;
666 #endif
668 #ifdef CONFIG_SPARSEMEM_VMEMMAP
669 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
671 /* This will make the necessary allocations eventually. */
672 return sparse_mem_map_populate(pnum, nid);
674 static void __kfree_section_memmap(struct page *memmap)
676 unsigned long start = (unsigned long)memmap;
677 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
679 vmemmap_free(start, end);
681 #ifdef CONFIG_MEMORY_HOTREMOVE
682 static void free_map_bootmem(struct page *memmap)
684 unsigned long start = (unsigned long)memmap;
685 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
687 vmemmap_free(start, end);
689 #endif /* CONFIG_MEMORY_HOTREMOVE */
690 #else
691 static struct page *__kmalloc_section_memmap(void)
693 struct page *page, *ret;
694 unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
696 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
697 if (page)
698 goto got_map_page;
700 ret = vmalloc(memmap_size);
701 if (ret)
702 goto got_map_ptr;
704 return NULL;
705 got_map_page:
706 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
707 got_map_ptr:
709 return ret;
712 static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
714 return __kmalloc_section_memmap();
717 static void __kfree_section_memmap(struct page *memmap)
719 if (is_vmalloc_addr(memmap))
720 vfree(memmap);
721 else
722 free_pages((unsigned long)memmap,
723 get_order(sizeof(struct page) * PAGES_PER_SECTION));
726 #ifdef CONFIG_MEMORY_HOTREMOVE
727 static void free_map_bootmem(struct page *memmap)
729 unsigned long maps_section_nr, removing_section_nr, i;
730 unsigned long magic, nr_pages;
731 struct page *page = virt_to_page(memmap);
733 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
734 >> PAGE_SHIFT;
736 for (i = 0; i < nr_pages; i++, page++) {
737 magic = (unsigned long) page->freelist;
739 BUG_ON(magic == NODE_INFO);
741 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
742 removing_section_nr = page_private(page);
745 * When this function is called, the removing section is
746 * logical offlined state. This means all pages are isolated
747 * from page allocator. If removing section's memmap is placed
748 * on the same section, it must not be freed.
749 * If it is freed, page allocator may allocate it which will
750 * be removed physically soon.
752 if (maps_section_nr != removing_section_nr)
753 put_page_bootmem(page);
756 #endif /* CONFIG_MEMORY_HOTREMOVE */
757 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
760 * returns the number of sections whose mem_maps were properly
761 * set. If this is <=0, then that means that the passed-in
762 * map was not consumed and must be freed.
764 int __meminit sparse_add_one_section(struct pglist_data *pgdat, unsigned long start_pfn)
766 unsigned long section_nr = pfn_to_section_nr(start_pfn);
767 struct mem_section *ms;
768 struct page *memmap;
769 unsigned long *usemap;
770 unsigned long flags;
771 int ret;
774 * no locking for this, because it does its own
775 * plus, it does a kmalloc
777 ret = sparse_index_init(section_nr, pgdat->node_id);
778 if (ret < 0 && ret != -EEXIST)
779 return ret;
780 memmap = kmalloc_section_memmap(section_nr, pgdat->node_id);
781 if (!memmap)
782 return -ENOMEM;
783 usemap = __kmalloc_section_usemap();
784 if (!usemap) {
785 __kfree_section_memmap(memmap);
786 return -ENOMEM;
789 pgdat_resize_lock(pgdat, &flags);
791 ms = __pfn_to_section(start_pfn);
792 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
793 ret = -EEXIST;
794 goto out;
797 memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION);
799 section_mark_present(ms);
801 ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
803 out:
804 pgdat_resize_unlock(pgdat, &flags);
805 if (ret <= 0) {
806 kfree(usemap);
807 __kfree_section_memmap(memmap);
809 return ret;
812 #ifdef CONFIG_MEMORY_HOTREMOVE
813 #ifdef CONFIG_MEMORY_FAILURE
814 static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
816 int i;
818 if (!memmap)
819 return;
821 for (i = 0; i < nr_pages; i++) {
822 if (PageHWPoison(&memmap[i])) {
823 atomic_long_sub(1, &num_poisoned_pages);
824 ClearPageHWPoison(&memmap[i]);
828 #else
829 static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
832 #endif
834 static void free_section_usemap(struct page *memmap, unsigned long *usemap)
836 struct page *usemap_page;
838 if (!usemap)
839 return;
841 usemap_page = virt_to_page(usemap);
843 * Check to see if allocation came from hot-plug-add
845 if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
846 kfree(usemap);
847 if (memmap)
848 __kfree_section_memmap(memmap);
849 return;
853 * The usemap came from bootmem. This is packed with other usemaps
854 * on the section which has pgdat at boot time. Just keep it as is now.
857 if (memmap)
858 free_map_bootmem(memmap);
861 void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
862 unsigned long map_offset)
864 struct page *memmap = NULL;
865 unsigned long *usemap = NULL, flags;
866 struct pglist_data *pgdat = zone->zone_pgdat;
868 pgdat_resize_lock(pgdat, &flags);
869 if (ms->section_mem_map) {
870 usemap = ms->pageblock_flags;
871 memmap = sparse_decode_mem_map(ms->section_mem_map,
872 __section_nr(ms));
873 ms->section_mem_map = 0;
874 ms->pageblock_flags = NULL;
876 pgdat_resize_unlock(pgdat, &flags);
878 clear_hwpoisoned_pages(memmap + map_offset,
879 PAGES_PER_SECTION - map_offset);
880 free_section_usemap(memmap, usemap);
882 #endif /* CONFIG_MEMORY_HOTREMOVE */
883 #endif /* CONFIG_MEMORY_HOTPLUG */