sparse.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /*
  65. * Although written for the SPARSEMEM_EXTREME case, this happens
  66. * to also work for the flat array case becase
  67. * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
  68. */
  69. int __section_nr(struct mem_section* ms)
  70. {
  71. unsigned long root_nr;
  72. struct mem_section* root;
  73. for (root_nr = 0;
  74. root_nr < NR_MEM_SECTIONS;
  75. root_nr += SECTIONS_PER_ROOT) {
  76. root = __nr_to_section(root_nr);
  77. if (!root)
  78. continue;
  79. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  80. break;
  81. }
  82. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  83. }
  84. /* Record a memory area against a node. */
  85. void memory_present(int nid, unsigned long start, unsigned long end)
  86. {
  87. unsigned long pfn;
  88. start &= PAGE_SECTION_MASK;
  89. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  90. unsigned long section = pfn_to_section_nr(pfn);
  91. struct mem_section *ms;
  92. sparse_index_init(section, nid);
  93. ms = __nr_to_section(section);
  94. if (!ms->section_mem_map)
  95. ms->section_mem_map = SECTION_MARKED_PRESENT;
  96. }
  97. }
  98. /*
  99. * Only used by the i386 NUMA architecures, but relatively
  100. * generic code.
  101. */
  102. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  103. unsigned long end_pfn)
  104. {
  105. unsigned long pfn;
  106. unsigned long nr_pages = 0;
  107. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  108. if (nid != early_pfn_to_nid(pfn))
  109. continue;
  110. if (pfn_valid(pfn))
  111. nr_pages += PAGES_PER_SECTION;
  112. }
  113. return nr_pages * sizeof(struct page);
  114. }
  115. /*
  116. * Subtle, we encode the real pfn into the mem_map such that
  117. * the identity pfn - section_mem_map will return the actual
  118. * physical page frame number.
  119. */
  120. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  121. {
  122. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  123. }
  124. /*
  125. * We need this if we ever free the mem_maps. While not implemented yet,
  126. * this function is included for parity with its sibling.
  127. */
  128. static __attribute((unused))
  129. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  130. {
  131. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  132. }
  133. static int sparse_init_one_section(struct mem_section *ms,
  134. unsigned long pnum, struct page *mem_map)
  135. {
  136. if (!valid_section(ms))
  137. return -EINVAL;
  138. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
  139. return 1;
  140. }
  141. static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
  142. {
  143. struct page *map;
  144. int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
  145. struct mem_section *ms = __nr_to_section(pnum);
  146. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  147. if (map)
  148. return map;
  149. map = alloc_bootmem_node(NODE_DATA(nid),
  150. sizeof(struct page) * PAGES_PER_SECTION);
  151. if (map)
  152. return map;
  153. printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
  154. ms->section_mem_map = 0;
  155. return NULL;
  156. }
  157. /*
  158. * Allocate the accumulated non-linear sections, allocate a mem_map
  159. * for each and record the physical to section mapping.
  160. */
  161. void sparse_init(void)
  162. {
  163. unsigned long pnum;
  164. struct page *map;
  165. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  166. if (!valid_section_nr(pnum))
  167. continue;
  168. map = sparse_early_mem_map_alloc(pnum);
  169. if (!map)
  170. continue;
  171. sparse_init_one_section(__nr_to_section(pnum), pnum, map);
  172. }
  173. }
  174. /*
  175. * returns the number of sections whose mem_maps were properly
  176. * set. If this is <=0, then that means that the passed-in
  177. * map was not consumed and must be freed.
  178. */
  179. int sparse_add_one_section(unsigned long start_pfn, int nr_pages, struct page *map)
  180. {
  181. struct mem_section *ms = __pfn_to_section(start_pfn);
  182. if (ms->section_mem_map & SECTION_MARKED_PRESENT)
  183. return -EEXIST;
  184. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  185. return sparse_init_one_section(ms, pfn_to_section_nr(start_pfn), map);
  186. }