page_ext.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <linux/mm.h>
  2. #include <linux/mmzone.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/page_ext.h>
  5. #include <linux/memory.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/kmemleak.h>
  8. #include <linux/page_owner.h>
  9. /*
  10. * struct page extension
  11. *
  12. * This is the feature to manage memory for extended data per page.
  13. *
  14. * Until now, we must modify struct page itself to store extra data per page.
  15. * This requires rebuilding the kernel and it is really time consuming process.
  16. * And, sometimes, rebuild is impossible due to third party module dependency.
  17. * At last, enlarging struct page could cause un-wanted system behaviour change.
  18. *
  19. * This feature is intended to overcome above mentioned problems. This feature
  20. * allocates memory for extended data per page in certain place rather than
  21. * the struct page itself. This memory can be accessed by the accessor
  22. * functions provided by this code. During the boot process, it checks whether
  23. * allocation of huge chunk of memory is needed or not. If not, it avoids
  24. * allocating memory at all. With this advantage, we can include this feature
  25. * into the kernel in default and can avoid rebuild and solve related problems.
  26. *
  27. * To help these things to work well, there are two callbacks for clients. One
  28. * is the need callback which is mandatory if user wants to avoid useless
  29. * memory allocation at boot-time. The other is optional, init callback, which
  30. * is used to do proper initialization after memory is allocated.
  31. *
  32. * The need callback is used to decide whether extended memory allocation is
  33. * needed or not. Sometimes users want to deactivate some features in this
  34. * boot and extra memory would be unneccessary. In this case, to avoid
  35. * allocating huge chunk of memory, each clients represent their need of
  36. * extra memory through the need callback. If one of the need callbacks
  37. * returns true, it means that someone needs extra memory so that
  38. * page extension core should allocates memory for page extension. If
  39. * none of need callbacks return true, memory isn't needed at all in this boot
  40. * and page extension core can skip to allocate memory. As result,
  41. * none of memory is wasted.
  42. *
  43. * The init callback is used to do proper initialization after page extension
  44. * is completely initialized. In sparse memory system, extra memory is
  45. * allocated some time later than memmap is allocated. In other words, lifetime
  46. * of memory for page extension isn't same with memmap for struct page.
  47. * Therefore, clients can't store extra data until page extension is
  48. * initialized, even if pages are allocated and used freely. This could
  49. * cause inadequate state of extra data per page, so, to prevent it, client
  50. * can utilize this callback to initialize the state of it correctly.
  51. */
  52. static struct page_ext_operations *page_ext_ops[] = {
  53. &debug_guardpage_ops,
  54. #ifdef CONFIG_PAGE_POISONING
  55. &page_poisoning_ops,
  56. #endif
  57. #ifdef CONFIG_PAGE_OWNER
  58. &page_owner_ops,
  59. #endif
  60. };
  61. static unsigned long total_usage;
  62. static bool __init invoke_need_callbacks(void)
  63. {
  64. int i;
  65. int entries = ARRAY_SIZE(page_ext_ops);
  66. for (i = 0; i < entries; i++) {
  67. if (page_ext_ops[i]->need && page_ext_ops[i]->need())
  68. return true;
  69. }
  70. return false;
  71. }
  72. static void __init invoke_init_callbacks(void)
  73. {
  74. int i;
  75. int entries = ARRAY_SIZE(page_ext_ops);
  76. for (i = 0; i < entries; i++) {
  77. if (page_ext_ops[i]->init)
  78. page_ext_ops[i]->init();
  79. }
  80. }
  81. #if !defined(CONFIG_SPARSEMEM)
  82. void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
  83. {
  84. pgdat->node_page_ext = NULL;
  85. }
  86. struct page_ext *lookup_page_ext(struct page *page)
  87. {
  88. unsigned long pfn = page_to_pfn(page);
  89. unsigned long offset;
  90. struct page_ext *base;
  91. base = NODE_DATA(page_to_nid(page))->node_page_ext;
  92. #ifdef CONFIG_DEBUG_VM
  93. /*
  94. * The sanity checks the page allocator does upon freeing a
  95. * page can reach here before the page_ext arrays are
  96. * allocated when feeding a range of pages to the allocator
  97. * for the first time during bootup or memory hotplug.
  98. */
  99. if (unlikely(!base))
  100. return NULL;
  101. #endif
  102. offset = pfn - round_down(node_start_pfn(page_to_nid(page)),
  103. MAX_ORDER_NR_PAGES);
  104. return base + offset;
  105. }
  106. static int __init alloc_node_page_ext(int nid)
  107. {
  108. struct page_ext *base;
  109. unsigned long table_size;
  110. unsigned long nr_pages;
  111. nr_pages = NODE_DATA(nid)->node_spanned_pages;
  112. if (!nr_pages)
  113. return 0;
  114. /*
  115. * Need extra space if node range is not aligned with
  116. * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
  117. * checks buddy's status, range could be out of exact node range.
  118. */
  119. if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
  120. !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
  121. nr_pages += MAX_ORDER_NR_PAGES;
  122. table_size = sizeof(struct page_ext) * nr_pages;
  123. base = memblock_virt_alloc_try_nid_nopanic(
  124. table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  125. BOOTMEM_ALLOC_ACCESSIBLE, nid);
  126. if (!base)
  127. return -ENOMEM;
  128. NODE_DATA(nid)->node_page_ext = base;
  129. total_usage += table_size;
  130. return 0;
  131. }
  132. void __init page_ext_init_flatmem(void)
  133. {
  134. int nid, fail;
  135. if (!invoke_need_callbacks())
  136. return;
  137. for_each_online_node(nid) {
  138. fail = alloc_node_page_ext(nid);
  139. if (fail)
  140. goto fail;
  141. }
  142. pr_info("allocated %ld bytes of page_ext\n", total_usage);
  143. invoke_init_callbacks();
  144. return;
  145. fail:
  146. pr_crit("allocation of page_ext failed.\n");
  147. panic("Out of memory");
  148. }
  149. #else /* CONFIG_FLAT_NODE_MEM_MAP */
  150. struct page_ext *lookup_page_ext(struct page *page)
  151. {
  152. unsigned long pfn = page_to_pfn(page);
  153. struct mem_section *section = __pfn_to_section(pfn);
  154. #ifdef CONFIG_DEBUG_VM
  155. /*
  156. * The sanity checks the page allocator does upon freeing a
  157. * page can reach here before the page_ext arrays are
  158. * allocated when feeding a range of pages to the allocator
  159. * for the first time during bootup or memory hotplug.
  160. */
  161. if (!section->page_ext)
  162. return NULL;
  163. #endif
  164. return section->page_ext + pfn;
  165. }
  166. static void *__meminit alloc_page_ext(size_t size, int nid)
  167. {
  168. gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
  169. void *addr = NULL;
  170. addr = alloc_pages_exact_nid(nid, size, flags);
  171. if (addr) {
  172. kmemleak_alloc(addr, size, 1, flags);
  173. return addr;
  174. }
  175. if (node_state(nid, N_HIGH_MEMORY))
  176. addr = vzalloc_node(size, nid);
  177. else
  178. addr = vzalloc(size);
  179. return addr;
  180. }
  181. static int __meminit init_section_page_ext(unsigned long pfn, int nid)
  182. {
  183. struct mem_section *section;
  184. struct page_ext *base;
  185. unsigned long table_size;
  186. section = __pfn_to_section(pfn);
  187. if (section->page_ext)
  188. return 0;
  189. table_size = sizeof(struct page_ext) * PAGES_PER_SECTION;
  190. base = alloc_page_ext(table_size, nid);
  191. /*
  192. * The value stored in section->page_ext is (base - pfn)
  193. * and it does not point to the memory block allocated above,
  194. * causing kmemleak false positives.
  195. */
  196. kmemleak_not_leak(base);
  197. if (!base) {
  198. pr_err("page ext allocation failure\n");
  199. return -ENOMEM;
  200. }
  201. /*
  202. * The passed "pfn" may not be aligned to SECTION. For the calculation
  203. * we need to apply a mask.
  204. */
  205. pfn &= PAGE_SECTION_MASK;
  206. section->page_ext = base - pfn;
  207. total_usage += table_size;
  208. return 0;
  209. }
  210. #ifdef CONFIG_MEMORY_HOTPLUG
  211. static void free_page_ext(void *addr)
  212. {
  213. if (is_vmalloc_addr(addr)) {
  214. vfree(addr);
  215. } else {
  216. struct page *page = virt_to_page(addr);
  217. size_t table_size;
  218. table_size = sizeof(struct page_ext) * PAGES_PER_SECTION;
  219. BUG_ON(PageReserved(page));
  220. free_pages_exact(addr, table_size);
  221. }
  222. }
  223. static void __free_page_ext(unsigned long pfn)
  224. {
  225. struct mem_section *ms;
  226. struct page_ext *base;
  227. ms = __pfn_to_section(pfn);
  228. if (!ms || !ms->page_ext)
  229. return;
  230. base = ms->page_ext + pfn;
  231. free_page_ext(base);
  232. ms->page_ext = NULL;
  233. }
  234. static int __meminit online_page_ext(unsigned long start_pfn,
  235. unsigned long nr_pages,
  236. int nid)
  237. {
  238. unsigned long start, end, pfn;
  239. int fail = 0;
  240. start = SECTION_ALIGN_DOWN(start_pfn);
  241. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  242. if (nid == -1) {
  243. /*
  244. * In this case, "nid" already exists and contains valid memory.
  245. * "start_pfn" passed to us is a pfn which is an arg for
  246. * online__pages(), and start_pfn should exist.
  247. */
  248. nid = pfn_to_nid(start_pfn);
  249. VM_BUG_ON(!node_state(nid, N_ONLINE));
  250. }
  251. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  252. if (!pfn_present(pfn))
  253. continue;
  254. fail = init_section_page_ext(pfn, nid);
  255. }
  256. if (!fail)
  257. return 0;
  258. /* rollback */
  259. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  260. __free_page_ext(pfn);
  261. return -ENOMEM;
  262. }
  263. static int __meminit offline_page_ext(unsigned long start_pfn,
  264. unsigned long nr_pages, int nid)
  265. {
  266. unsigned long start, end, pfn;
  267. start = SECTION_ALIGN_DOWN(start_pfn);
  268. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  269. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  270. __free_page_ext(pfn);
  271. return 0;
  272. }
  273. static int __meminit page_ext_callback(struct notifier_block *self,
  274. unsigned long action, void *arg)
  275. {
  276. struct memory_notify *mn = arg;
  277. int ret = 0;
  278. switch (action) {
  279. case MEM_GOING_ONLINE:
  280. ret = online_page_ext(mn->start_pfn,
  281. mn->nr_pages, mn->status_change_nid);
  282. break;
  283. case MEM_OFFLINE:
  284. offline_page_ext(mn->start_pfn,
  285. mn->nr_pages, mn->status_change_nid);
  286. break;
  287. case MEM_CANCEL_ONLINE:
  288. offline_page_ext(mn->start_pfn,
  289. mn->nr_pages, mn->status_change_nid);
  290. break;
  291. case MEM_GOING_OFFLINE:
  292. break;
  293. case MEM_ONLINE:
  294. case MEM_CANCEL_OFFLINE:
  295. break;
  296. }
  297. return notifier_from_errno(ret);
  298. }
  299. #endif
  300. void __init page_ext_init(void)
  301. {
  302. unsigned long pfn;
  303. int nid;
  304. if (!invoke_need_callbacks())
  305. return;
  306. for_each_node_state(nid, N_MEMORY) {
  307. unsigned long start_pfn, end_pfn;
  308. start_pfn = node_start_pfn(nid);
  309. end_pfn = node_end_pfn(nid);
  310. /*
  311. * start_pfn and end_pfn may not be aligned to SECTION and the
  312. * page->flags of out of node pages are not initialized. So we
  313. * scan [start_pfn, the biggest section's pfn < end_pfn) here.
  314. */
  315. for (pfn = start_pfn; pfn < end_pfn;
  316. pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
  317. if (!pfn_valid(pfn))
  318. continue;
  319. /*
  320. * Nodes's pfns can be overlapping.
  321. * We know some arch can have a nodes layout such as
  322. * -------------pfn-------------->
  323. * N0 | N1 | N2 | N0 | N1 | N2|....
  324. */
  325. if (pfn_to_nid(pfn) != nid)
  326. continue;
  327. if (init_section_page_ext(pfn, nid))
  328. goto oom;
  329. }
  330. }
  331. hotplug_memory_notifier(page_ext_callback, 0);
  332. pr_info("allocated %ld bytes of page_ext\n", total_usage);
  333. invoke_init_callbacks();
  334. return;
  335. oom:
  336. panic("Out of memory");
  337. }
  338. void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
  339. {
  340. }
  341. #endif