page_ext.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/mmzone.h>
  4. #include <linux/memblock.h>
  5. #include <linux/page_ext.h>
  6. #include <linux/memory.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/kmemleak.h>
  9. #include <linux/page_owner.h>
  10. #include <linux/page_idle.h>
  11. #include <linux/page_table_check.h>
  12. #include <linux/rcupdate.h>
  13. #include <linux/pgalloc_tag.h>
  14. /*
  15. * struct page extension
  16. *
  17. * This is the feature to manage memory for extended data per page.
  18. *
  19. * Until now, we must modify struct page itself to store extra data per page.
  20. * This requires rebuilding the kernel and it is really time consuming process.
  21. * And, sometimes, rebuild is impossible due to third party module dependency.
  22. * At last, enlarging struct page could cause un-wanted system behaviour change.
  23. *
  24. * This feature is intended to overcome above mentioned problems. This feature
  25. * allocates memory for extended data per page in certain place rather than
  26. * the struct page itself. This memory can be accessed by the accessor
  27. * functions provided by this code. During the boot process, it checks whether
  28. * allocation of huge chunk of memory is needed or not. If not, it avoids
  29. * allocating memory at all. With this advantage, we can include this feature
  30. * into the kernel in default and can avoid rebuild and solve related problems.
  31. *
  32. * To help these things to work well, there are two callbacks for clients. One
  33. * is the need callback which is mandatory if user wants to avoid useless
  34. * memory allocation at boot-time. The other is optional, init callback, which
  35. * is used to do proper initialization after memory is allocated.
  36. *
  37. * The need callback is used to decide whether extended memory allocation is
  38. * needed or not. Sometimes users want to deactivate some features in this
  39. * boot and extra memory would be unnecessary. In this case, to avoid
  40. * allocating huge chunk of memory, each clients represent their need of
  41. * extra memory through the need callback. If one of the need callbacks
  42. * returns true, it means that someone needs extra memory so that
  43. * page extension core should allocates memory for page extension. If
  44. * none of need callbacks return true, memory isn't needed at all in this boot
  45. * and page extension core can skip to allocate memory. As result,
  46. * none of memory is wasted.
  47. *
  48. * When need callback returns true, page_ext checks if there is a request for
  49. * extra memory through size in struct page_ext_operations. If it is non-zero,
  50. * extra space is allocated for each page_ext entry and offset is returned to
  51. * user through offset in struct page_ext_operations.
  52. *
  53. * The init callback is used to do proper initialization after page extension
  54. * is completely initialized. In sparse memory system, extra memory is
  55. * allocated some time later than memmap is allocated. In other words, lifetime
  56. * of memory for page extension isn't same with memmap for struct page.
  57. * Therefore, clients can't store extra data until page extension is
  58. * initialized, even if pages are allocated and used freely. This could
  59. * cause inadequate state of extra data per page, so, to prevent it, client
  60. * can utilize this callback to initialize the state of it correctly.
  61. */
  62. #ifdef CONFIG_SPARSEMEM
  63. #define PAGE_EXT_INVALID (0x1)
  64. #endif
  65. #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
  66. static bool need_page_idle(void)
  67. {
  68. return true;
  69. }
  70. static struct page_ext_operations page_idle_ops __initdata = {
  71. .need = need_page_idle,
  72. .need_shared_flags = true,
  73. };
  74. #endif
  75. static struct page_ext_operations *page_ext_ops[] __initdata = {
  76. #ifdef CONFIG_PAGE_OWNER
  77. &page_owner_ops,
  78. #endif
  79. #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
  80. &page_idle_ops,
  81. #endif
  82. #ifdef CONFIG_MEM_ALLOC_PROFILING
  83. &page_alloc_tagging_ops,
  84. #endif
  85. #ifdef CONFIG_PAGE_TABLE_CHECK
  86. &page_table_check_ops,
  87. #endif
  88. };
  89. unsigned long page_ext_size;
  90. static unsigned long total_usage;
  91. #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
  92. /*
  93. * To ensure correct allocation tagging for pages, page_ext should be available
  94. * before the first page allocation. Otherwise early task stacks will be
  95. * allocated before page_ext initialization and missing tags will be flagged.
  96. */
  97. bool early_page_ext __meminitdata = true;
  98. #else
  99. bool early_page_ext __meminitdata;
  100. #endif
  101. static int __init setup_early_page_ext(char *str)
  102. {
  103. early_page_ext = true;
  104. return 0;
  105. }
  106. early_param("early_page_ext", setup_early_page_ext);
  107. static bool __init invoke_need_callbacks(void)
  108. {
  109. int i;
  110. int entries = ARRAY_SIZE(page_ext_ops);
  111. bool need = false;
  112. for (i = 0; i < entries; i++) {
  113. if (page_ext_ops[i]->need()) {
  114. if (page_ext_ops[i]->need_shared_flags) {
  115. page_ext_size = sizeof(struct page_ext);
  116. break;
  117. }
  118. }
  119. }
  120. for (i = 0; i < entries; i++) {
  121. if (page_ext_ops[i]->need()) {
  122. page_ext_ops[i]->offset = page_ext_size;
  123. page_ext_size += page_ext_ops[i]->size;
  124. need = true;
  125. }
  126. }
  127. return need;
  128. }
  129. static void __init invoke_init_callbacks(void)
  130. {
  131. int i;
  132. int entries = ARRAY_SIZE(page_ext_ops);
  133. for (i = 0; i < entries; i++) {
  134. if (page_ext_ops[i]->init)
  135. page_ext_ops[i]->init();
  136. }
  137. }
  138. static inline struct page_ext *get_entry(void *base, unsigned long index)
  139. {
  140. return base + page_ext_size * index;
  141. }
  142. #ifndef CONFIG_SPARSEMEM
  143. void __init page_ext_init_flatmem_late(void)
  144. {
  145. invoke_init_callbacks();
  146. }
  147. void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
  148. {
  149. pgdat->node_page_ext = NULL;
  150. }
  151. static struct page_ext *lookup_page_ext(const struct page *page)
  152. {
  153. unsigned long pfn = page_to_pfn(page);
  154. unsigned long index;
  155. struct page_ext *base;
  156. WARN_ON_ONCE(!rcu_read_lock_held());
  157. base = NODE_DATA(page_to_nid(page))->node_page_ext;
  158. /*
  159. * The sanity checks the page allocator does upon freeing a
  160. * page can reach here before the page_ext arrays are
  161. * allocated when feeding a range of pages to the allocator
  162. * for the first time during bootup or memory hotplug.
  163. */
  164. if (unlikely(!base))
  165. return NULL;
  166. index = pfn - round_down(node_start_pfn(page_to_nid(page)),
  167. MAX_ORDER_NR_PAGES);
  168. return get_entry(base, index);
  169. }
  170. static int __init alloc_node_page_ext(int nid)
  171. {
  172. struct page_ext *base;
  173. unsigned long table_size;
  174. unsigned long nr_pages;
  175. nr_pages = NODE_DATA(nid)->node_spanned_pages;
  176. if (!nr_pages)
  177. return 0;
  178. /*
  179. * Need extra space if node range is not aligned with
  180. * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
  181. * checks buddy's status, range could be out of exact node range.
  182. */
  183. if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
  184. !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
  185. nr_pages += MAX_ORDER_NR_PAGES;
  186. table_size = page_ext_size * nr_pages;
  187. base = memblock_alloc_try_nid(
  188. table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  189. MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  190. if (!base)
  191. return -ENOMEM;
  192. NODE_DATA(nid)->node_page_ext = base;
  193. total_usage += table_size;
  194. memmap_boot_pages_add(DIV_ROUND_UP(table_size, PAGE_SIZE));
  195. return 0;
  196. }
  197. void __init page_ext_init_flatmem(void)
  198. {
  199. int nid, fail;
  200. if (!invoke_need_callbacks())
  201. return;
  202. for_each_online_node(nid) {
  203. fail = alloc_node_page_ext(nid);
  204. if (fail)
  205. goto fail;
  206. }
  207. pr_info("allocated %ld bytes of page_ext\n", total_usage);
  208. return;
  209. fail:
  210. pr_crit("allocation of page_ext failed.\n");
  211. panic("Out of memory");
  212. }
  213. #else /* CONFIG_SPARSEMEM */
  214. static bool page_ext_invalid(struct page_ext *page_ext)
  215. {
  216. return !page_ext || (((unsigned long)page_ext & PAGE_EXT_INVALID) == PAGE_EXT_INVALID);
  217. }
  218. static struct page_ext *lookup_page_ext(const struct page *page)
  219. {
  220. unsigned long pfn = page_to_pfn(page);
  221. struct mem_section *section = __pfn_to_section(pfn);
  222. struct page_ext *page_ext = READ_ONCE(section->page_ext);
  223. WARN_ON_ONCE(!rcu_read_lock_held());
  224. /*
  225. * The sanity checks the page allocator does upon freeing a
  226. * page can reach here before the page_ext arrays are
  227. * allocated when feeding a range of pages to the allocator
  228. * for the first time during bootup or memory hotplug.
  229. */
  230. if (page_ext_invalid(page_ext))
  231. return NULL;
  232. return get_entry(page_ext, pfn);
  233. }
  234. static void *__meminit alloc_page_ext(size_t size, int nid)
  235. {
  236. gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
  237. void *addr = NULL;
  238. addr = alloc_pages_exact_nid(nid, size, flags);
  239. if (addr)
  240. kmemleak_alloc(addr, size, 1, flags);
  241. else
  242. addr = vzalloc_node(size, nid);
  243. if (addr)
  244. memmap_pages_add(DIV_ROUND_UP(size, PAGE_SIZE));
  245. return addr;
  246. }
  247. static int __meminit init_section_page_ext(unsigned long pfn, int nid)
  248. {
  249. struct mem_section *section;
  250. struct page_ext *base;
  251. unsigned long table_size;
  252. section = __pfn_to_section(pfn);
  253. if (section->page_ext)
  254. return 0;
  255. table_size = page_ext_size * PAGES_PER_SECTION;
  256. base = alloc_page_ext(table_size, nid);
  257. /*
  258. * The value stored in section->page_ext is (base - pfn)
  259. * and it does not point to the memory block allocated above,
  260. * causing kmemleak false positives.
  261. */
  262. kmemleak_not_leak(base);
  263. if (!base) {
  264. pr_err("page ext allocation failure\n");
  265. return -ENOMEM;
  266. }
  267. /*
  268. * The passed "pfn" may not be aligned to SECTION. For the calculation
  269. * we need to apply a mask.
  270. */
  271. pfn &= PAGE_SECTION_MASK;
  272. section->page_ext = (void *)base - page_ext_size * pfn;
  273. total_usage += table_size;
  274. return 0;
  275. }
  276. static void free_page_ext(void *addr)
  277. {
  278. size_t table_size;
  279. struct page *page;
  280. table_size = page_ext_size * PAGES_PER_SECTION;
  281. memmap_pages_add(-1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
  282. if (is_vmalloc_addr(addr)) {
  283. vfree(addr);
  284. } else {
  285. page = virt_to_page(addr);
  286. BUG_ON(PageReserved(page));
  287. kmemleak_free(addr);
  288. free_pages_exact(addr, table_size);
  289. }
  290. }
  291. static void __free_page_ext(unsigned long pfn)
  292. {
  293. struct mem_section *ms;
  294. struct page_ext *base;
  295. ms = __pfn_to_section(pfn);
  296. if (!ms || !ms->page_ext)
  297. return;
  298. base = READ_ONCE(ms->page_ext);
  299. /*
  300. * page_ext here can be valid while doing the roll back
  301. * operation in online_page_ext().
  302. */
  303. if (page_ext_invalid(base))
  304. base = (void *)base - PAGE_EXT_INVALID;
  305. WRITE_ONCE(ms->page_ext, NULL);
  306. base = get_entry(base, pfn);
  307. free_page_ext(base);
  308. }
  309. static void __invalidate_page_ext(unsigned long pfn)
  310. {
  311. struct mem_section *ms;
  312. void *val;
  313. ms = __pfn_to_section(pfn);
  314. if (!ms || !ms->page_ext)
  315. return;
  316. val = (void *)ms->page_ext + PAGE_EXT_INVALID;
  317. WRITE_ONCE(ms->page_ext, val);
  318. }
  319. static int __meminit online_page_ext(unsigned long start_pfn,
  320. unsigned long nr_pages,
  321. int nid)
  322. {
  323. unsigned long start, end, pfn;
  324. int fail = 0;
  325. start = SECTION_ALIGN_DOWN(start_pfn);
  326. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  327. if (nid == NUMA_NO_NODE) {
  328. /*
  329. * In this case, "nid" already exists and contains valid memory.
  330. * "start_pfn" passed to us is a pfn which is an arg for
  331. * online__pages(), and start_pfn should exist.
  332. */
  333. nid = pfn_to_nid(start_pfn);
  334. VM_BUG_ON(!node_online(nid));
  335. }
  336. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
  337. fail = init_section_page_ext(pfn, nid);
  338. if (!fail)
  339. return 0;
  340. /* rollback */
  341. end = pfn - PAGES_PER_SECTION;
  342. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  343. __free_page_ext(pfn);
  344. return -ENOMEM;
  345. }
  346. static void __meminit offline_page_ext(unsigned long start_pfn,
  347. unsigned long nr_pages)
  348. {
  349. unsigned long start, end, pfn;
  350. start = SECTION_ALIGN_DOWN(start_pfn);
  351. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  352. /*
  353. * Freeing of page_ext is done in 3 steps to avoid
  354. * use-after-free of it:
  355. * 1) Traverse all the sections and mark their page_ext
  356. * as invalid.
  357. * 2) Wait for all the existing users of page_ext who
  358. * started before invalidation to finish.
  359. * 3) Free the page_ext.
  360. */
  361. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  362. __invalidate_page_ext(pfn);
  363. synchronize_rcu();
  364. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  365. __free_page_ext(pfn);
  366. }
  367. static int __meminit page_ext_callback(struct notifier_block *self,
  368. unsigned long action, void *arg)
  369. {
  370. struct memory_notify *mn = arg;
  371. int ret = 0;
  372. switch (action) {
  373. case MEM_GOING_ONLINE:
  374. ret = online_page_ext(mn->start_pfn,
  375. mn->nr_pages, mn->status_change_nid);
  376. break;
  377. case MEM_OFFLINE:
  378. offline_page_ext(mn->start_pfn,
  379. mn->nr_pages);
  380. break;
  381. case MEM_CANCEL_ONLINE:
  382. offline_page_ext(mn->start_pfn,
  383. mn->nr_pages);
  384. break;
  385. case MEM_GOING_OFFLINE:
  386. break;
  387. case MEM_ONLINE:
  388. case MEM_CANCEL_OFFLINE:
  389. break;
  390. }
  391. return notifier_from_errno(ret);
  392. }
  393. void __init page_ext_init(void)
  394. {
  395. unsigned long pfn;
  396. int nid;
  397. if (!invoke_need_callbacks())
  398. return;
  399. for_each_node_state(nid, N_MEMORY) {
  400. unsigned long start_pfn, end_pfn;
  401. start_pfn = node_start_pfn(nid);
  402. end_pfn = node_end_pfn(nid);
  403. /*
  404. * start_pfn and end_pfn may not be aligned to SECTION and the
  405. * page->flags of out of node pages are not initialized. So we
  406. * scan [start_pfn, the biggest section's pfn < end_pfn) here.
  407. */
  408. for (pfn = start_pfn; pfn < end_pfn;
  409. pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
  410. if (!pfn_valid(pfn))
  411. continue;
  412. /*
  413. * Nodes's pfns can be overlapping.
  414. * We know some arch can have a nodes layout such as
  415. * -------------pfn-------------->
  416. * N0 | N1 | N2 | N0 | N1 | N2|....
  417. */
  418. if (pfn_to_nid(pfn) != nid)
  419. continue;
  420. if (init_section_page_ext(pfn, nid))
  421. goto oom;
  422. cond_resched();
  423. }
  424. }
  425. hotplug_memory_notifier(page_ext_callback, DEFAULT_CALLBACK_PRI);
  426. pr_info("allocated %ld bytes of page_ext\n", total_usage);
  427. invoke_init_callbacks();
  428. return;
  429. oom:
  430. panic("Out of memory");
  431. }
  432. void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
  433. {
  434. }
  435. #endif
  436. /**
  437. * page_ext_get() - Get the extended information for a page.
  438. * @page: The page we're interested in.
  439. *
  440. * Ensures that the page_ext will remain valid until page_ext_put()
  441. * is called.
  442. *
  443. * Return: NULL if no page_ext exists for this page.
  444. * Context: Any context. Caller may not sleep until they have called
  445. * page_ext_put().
  446. */
  447. struct page_ext *page_ext_get(const struct page *page)
  448. {
  449. struct page_ext *page_ext;
  450. rcu_read_lock();
  451. page_ext = lookup_page_ext(page);
  452. if (!page_ext) {
  453. rcu_read_unlock();
  454. return NULL;
  455. }
  456. return page_ext;
  457. }
  458. /**
  459. * page_ext_put() - Working with page extended information is done.
  460. * @page_ext: Page extended information received from page_ext_get().
  461. *
  462. * The page extended information of the page may not be valid after this
  463. * function is called.
  464. *
  465. * Return: None.
  466. * Context: Any context with corresponding page_ext_get() is called.
  467. */
  468. void page_ext_put(struct page_ext *page_ext)
  469. {
  470. if (unlikely(!page_ext))
  471. return;
  472. rcu_read_unlock();
  473. }