sparse.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * sparse memory mappings.
  3. */
  4. #include <linux/config.h>
  5. #include <linux/mm.h>
  6. #include <linux/mmzone.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/module.h>
  9. #include <linux/spinlock.h>
  10. #include <asm/dma.h>
  11. /*
  12. * Permanent SPARSEMEM data:
  13. *
  14. * 1) mem_section - memory sections, mem_map's for valid memory
  15. */
  16. #ifdef CONFIG_SPARSEMEM_EXTREME
  17. struct mem_section *mem_section[NR_SECTION_ROOTS]
  18. ____cacheline_maxaligned_in_smp;
  19. #else
  20. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  21. ____cacheline_maxaligned_in_smp;
  22. #endif
  23. EXPORT_SYMBOL(mem_section);
  24. #ifdef CONFIG_SPARSEMEM_EXTREME
  25. static struct mem_section *sparse_index_alloc(int nid)
  26. {
  27. struct mem_section *section = NULL;
  28. unsigned long array_size = SECTIONS_PER_ROOT *
  29. sizeof(struct mem_section);
  30. section = alloc_bootmem_node(NODE_DATA(nid), array_size);
  31. if (section)
  32. memset(section, 0, array_size);
  33. return section;
  34. }
  35. static int sparse_index_init(unsigned long section_nr, int nid)
  36. {
  37. static spinlock_t index_init_lock = SPIN_LOCK_UNLOCKED;
  38. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  39. struct mem_section *section;
  40. int ret = 0;
  41. if (mem_section[root])
  42. return -EEXIST;
  43. section = sparse_index_alloc(nid);
  44. /*
  45. * This lock keeps two different sections from
  46. * reallocating for the same index
  47. */
  48. spin_lock(&index_init_lock);
  49. if (mem_section[root]) {
  50. ret = -EEXIST;
  51. goto out;
  52. }
  53. mem_section[root] = section;
  54. out:
  55. spin_unlock(&index_init_lock);
  56. return ret;
  57. }
  58. #else /* !SPARSEMEM_EXTREME */
  59. static inline int sparse_index_init(unsigned long section_nr, int nid)
  60. {
  61. return 0;
  62. }
  63. #endif
  64. /* Record a memory area against a node. */
  65. void memory_present(int nid, unsigned long start, unsigned long end)
  66. {
  67. unsigned long pfn;
  68. start &= PAGE_SECTION_MASK;
  69. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  70. unsigned long section = pfn_to_section_nr(pfn);
  71. struct mem_section *ms;
  72. sparse_index_init(section, nid);
  73. ms = __nr_to_section(section);
  74. if (!ms->section_mem_map)
  75. ms->section_mem_map = SECTION_MARKED_PRESENT;
  76. }
  77. }
  78. /*
  79. * Only used by the i386 NUMA architecures, but relatively
  80. * generic code.
  81. */
  82. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  83. unsigned long end_pfn)
  84. {
  85. unsigned long pfn;
  86. unsigned long nr_pages = 0;
  87. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  88. if (nid != early_pfn_to_nid(pfn))
  89. continue;
  90. if (pfn_valid(pfn))
  91. nr_pages += PAGES_PER_SECTION;
  92. }
  93. return nr_pages * sizeof(struct page);
  94. }
  95. /*
  96. * Subtle, we encode the real pfn into the mem_map such that
  97. * the identity pfn - section_mem_map will return the actual
  98. * physical page frame number.
  99. */
  100. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  101. {
  102. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  103. }
  104. /*
  105. * We need this if we ever free the mem_maps. While not implemented yet,
  106. * this function is included for parity with its sibling.
  107. */
  108. static __attribute((unused))
  109. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  110. {
  111. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  112. }
  113. static int sparse_init_one_section(struct mem_section *ms,
  114. unsigned long pnum, struct page *mem_map)
  115. {
  116. if (!valid_section(ms))
  117. return -EINVAL;
  118. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
  119. return 1;
  120. }
  121. static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
  122. {
  123. struct page *map;
  124. int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
  125. struct mem_section *ms = __nr_to_section(pnum);
  126. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  127. if (map)
  128. return map;
  129. map = alloc_bootmem_node(NODE_DATA(nid),
  130. sizeof(struct page) * PAGES_PER_SECTION);
  131. if (map)
  132. return map;
  133. printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
  134. ms->section_mem_map = 0;
  135. return NULL;
  136. }
  137. /*
  138. * Allocate the accumulated non-linear sections, allocate a mem_map
  139. * for each and record the physical to section mapping.
  140. */
  141. void sparse_init(void)
  142. {
  143. unsigned long pnum;
  144. struct page *map;
  145. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  146. if (!valid_section_nr(pnum))
  147. continue;
  148. map = sparse_early_mem_map_alloc(pnum);
  149. if (!map)
  150. continue;
  151. sparse_init_one_section(__nr_to_section(pnum), pnum, map);
  152. }
  153. }
  154. /*
  155. * returns the number of sections whose mem_maps were properly
  156. * set. If this is <=0, then that means that the passed-in
  157. * map was not consumed and must be freed.
  158. */
  159. int sparse_add_one_section(unsigned long start_pfn, int nr_pages, struct page *map)
  160. {
  161. struct mem_section *ms = __pfn_to_section(start_pfn);
  162. if (ms->section_mem_map & SECTION_MARKED_PRESENT)
  163. return -EEXIST;
  164. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  165. return sparse_init_one_section(ms, pfn_to_section_nr(start_pfn), map);
  166. }