page_ext.c 13 KB

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