debug_vm_pgtable.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This kernel test validates architecture page table helpers and
  4. * accessors and helps in verifying their continued compliance with
  5. * expected generic MM semantics.
  6. *
  7. * Copyright (C) 2019 ARM Ltd.
  8. *
  9. * Author: Anshuman Khandual <[email protected]>
  10. */
  11. #define pr_fmt(fmt) "debug_vm_pgtable: [%-25s]: " fmt, __func__
  12. #include <linux/gfp.h>
  13. #include <linux/highmem.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kconfig.h>
  17. #include <linux/mm.h>
  18. #include <linux/mman.h>
  19. #include <linux/mm_types.h>
  20. #include <linux/module.h>
  21. #include <linux/pfn_t.h>
  22. #include <linux/printk.h>
  23. #include <linux/pgtable.h>
  24. #include <linux/random.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/swap.h>
  27. #include <linux/swapops.h>
  28. #include <linux/start_kernel.h>
  29. #include <linux/sched/mm.h>
  30. #include <linux/io.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/pgalloc.h>
  33. #include <asm/tlbflush.h>
  34. /*
  35. * Please refer Documentation/mm/arch_pgtable_helpers.rst for the semantics
  36. * expectations that are being validated here. All future changes in here
  37. * or the documentation need to be in sync.
  38. */
  39. #define VMFLAGS (VM_READ|VM_WRITE|VM_EXEC)
  40. /*
  41. * On s390 platform, the lower 4 bits are used to identify given page table
  42. * entry type. But these bits might affect the ability to clear entries with
  43. * pxx_clear() because of how dynamic page table folding works on s390. So
  44. * while loading up the entries do not change the lower 4 bits. It does not
  45. * have affect any other platform. Also avoid the 62nd bit on ppc64 that is
  46. * used to mark a pte entry.
  47. */
  48. #define S390_SKIP_MASK GENMASK(3, 0)
  49. #if __BITS_PER_LONG == 64
  50. #define PPC64_SKIP_MASK GENMASK(62, 62)
  51. #else
  52. #define PPC64_SKIP_MASK 0x0
  53. #endif
  54. #define ARCH_SKIP_MASK (S390_SKIP_MASK | PPC64_SKIP_MASK)
  55. #define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
  56. #define RANDOM_NZVALUE GENMASK(7, 0)
  57. struct pgtable_debug_args {
  58. struct mm_struct *mm;
  59. struct vm_area_struct *vma;
  60. pgd_t *pgdp;
  61. p4d_t *p4dp;
  62. pud_t *pudp;
  63. pmd_t *pmdp;
  64. pte_t *ptep;
  65. p4d_t *start_p4dp;
  66. pud_t *start_pudp;
  67. pmd_t *start_pmdp;
  68. pgtable_t start_ptep;
  69. unsigned long vaddr;
  70. pgprot_t page_prot;
  71. pgprot_t page_prot_none;
  72. bool is_contiguous_page;
  73. unsigned long pud_pfn;
  74. unsigned long pmd_pfn;
  75. unsigned long pte_pfn;
  76. unsigned long fixed_pgd_pfn;
  77. unsigned long fixed_p4d_pfn;
  78. unsigned long fixed_pud_pfn;
  79. unsigned long fixed_pmd_pfn;
  80. unsigned long fixed_pte_pfn;
  81. };
  82. static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx)
  83. {
  84. pgprot_t prot = vm_get_page_prot(idx);
  85. pte_t pte = pfn_pte(args->fixed_pte_pfn, prot);
  86. unsigned long val = idx, *ptr = &val;
  87. pr_debug("Validating PTE basic (%pGv)\n", ptr);
  88. /*
  89. * This test needs to be executed after the given page table entry
  90. * is created with pfn_pte() to make sure that vm_get_page_prot(idx)
  91. * does not have the dirty bit enabled from the beginning. This is
  92. * important for platforms like arm64 where (!PTE_RDONLY) indicate
  93. * dirty bit being set.
  94. */
  95. WARN_ON(pte_dirty(pte_wrprotect(pte)));
  96. WARN_ON(!pte_same(pte, pte));
  97. WARN_ON(!pte_young(pte_mkyoung(pte_mkold(pte))));
  98. WARN_ON(!pte_dirty(pte_mkdirty(pte_mkclean(pte))));
  99. WARN_ON(!pte_write(pte_mkwrite(pte_wrprotect(pte))));
  100. WARN_ON(pte_young(pte_mkold(pte_mkyoung(pte))));
  101. WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte))));
  102. WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
  103. WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte))));
  104. WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte))));
  105. }
  106. static void __init pte_advanced_tests(struct pgtable_debug_args *args)
  107. {
  108. struct page *page;
  109. pte_t pte;
  110. /*
  111. * Architectures optimize set_pte_at by avoiding TLB flush.
  112. * This requires set_pte_at to be not used to update an
  113. * existing pte entry. Clear pte before we do set_pte_at
  114. *
  115. * flush_dcache_page() is called after set_pte_at() to clear
  116. * PG_arch_1 for the page on ARM64. The page flag isn't cleared
  117. * when it's released and page allocation check will fail when
  118. * the page is allocated again. For architectures other than ARM64,
  119. * the unexpected overhead of cache flushing is acceptable.
  120. */
  121. page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
  122. if (!page)
  123. return;
  124. pr_debug("Validating PTE advanced\n");
  125. pte = pfn_pte(args->pte_pfn, args->page_prot);
  126. set_pte_at(args->mm, args->vaddr, args->ptep, pte);
  127. flush_dcache_page(page);
  128. ptep_set_wrprotect(args->mm, args->vaddr, args->ptep);
  129. pte = ptep_get(args->ptep);
  130. WARN_ON(pte_write(pte));
  131. ptep_get_and_clear(args->mm, args->vaddr, args->ptep);
  132. pte = ptep_get(args->ptep);
  133. WARN_ON(!pte_none(pte));
  134. pte = pfn_pte(args->pte_pfn, args->page_prot);
  135. pte = pte_wrprotect(pte);
  136. pte = pte_mkclean(pte);
  137. set_pte_at(args->mm, args->vaddr, args->ptep, pte);
  138. flush_dcache_page(page);
  139. pte = pte_mkwrite(pte);
  140. pte = pte_mkdirty(pte);
  141. ptep_set_access_flags(args->vma, args->vaddr, args->ptep, pte, 1);
  142. pte = ptep_get(args->ptep);
  143. WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
  144. ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
  145. pte = ptep_get(args->ptep);
  146. WARN_ON(!pte_none(pte));
  147. pte = pfn_pte(args->pte_pfn, args->page_prot);
  148. pte = pte_mkyoung(pte);
  149. set_pte_at(args->mm, args->vaddr, args->ptep, pte);
  150. flush_dcache_page(page);
  151. ptep_test_and_clear_young(args->vma, args->vaddr, args->ptep);
  152. pte = ptep_get(args->ptep);
  153. WARN_ON(pte_young(pte));
  154. ptep_get_and_clear_full(args->mm, args->vaddr, args->ptep, 1);
  155. }
  156. static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
  157. {
  158. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
  159. if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
  160. return;
  161. pr_debug("Validating PTE saved write\n");
  162. WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
  163. WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
  164. }
  165. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  166. static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx)
  167. {
  168. pgprot_t prot = vm_get_page_prot(idx);
  169. unsigned long val = idx, *ptr = &val;
  170. pmd_t pmd;
  171. if (!has_transparent_hugepage())
  172. return;
  173. pr_debug("Validating PMD basic (%pGv)\n", ptr);
  174. pmd = pfn_pmd(args->fixed_pmd_pfn, prot);
  175. /*
  176. * This test needs to be executed after the given page table entry
  177. * is created with pfn_pmd() to make sure that vm_get_page_prot(idx)
  178. * does not have the dirty bit enabled from the beginning. This is
  179. * important for platforms like arm64 where (!PTE_RDONLY) indicate
  180. * dirty bit being set.
  181. */
  182. WARN_ON(pmd_dirty(pmd_wrprotect(pmd)));
  183. WARN_ON(!pmd_same(pmd, pmd));
  184. WARN_ON(!pmd_young(pmd_mkyoung(pmd_mkold(pmd))));
  185. WARN_ON(!pmd_dirty(pmd_mkdirty(pmd_mkclean(pmd))));
  186. WARN_ON(!pmd_write(pmd_mkwrite(pmd_wrprotect(pmd))));
  187. WARN_ON(pmd_young(pmd_mkold(pmd_mkyoung(pmd))));
  188. WARN_ON(pmd_dirty(pmd_mkclean(pmd_mkdirty(pmd))));
  189. WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd))));
  190. WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd))));
  191. WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd))));
  192. /*
  193. * A huge page does not point to next level page table
  194. * entry. Hence this must qualify as pmd_bad().
  195. */
  196. WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
  197. }
  198. static void __init pmd_advanced_tests(struct pgtable_debug_args *args)
  199. {
  200. struct page *page;
  201. pmd_t pmd;
  202. unsigned long vaddr = args->vaddr;
  203. if (!has_transparent_hugepage())
  204. return;
  205. page = (args->pmd_pfn != ULONG_MAX) ? pfn_to_page(args->pmd_pfn) : NULL;
  206. if (!page)
  207. return;
  208. /*
  209. * flush_dcache_page() is called after set_pmd_at() to clear
  210. * PG_arch_1 for the page on ARM64. The page flag isn't cleared
  211. * when it's released and page allocation check will fail when
  212. * the page is allocated again. For architectures other than ARM64,
  213. * the unexpected overhead of cache flushing is acceptable.
  214. */
  215. pr_debug("Validating PMD advanced\n");
  216. /* Align the address wrt HPAGE_PMD_SIZE */
  217. vaddr &= HPAGE_PMD_MASK;
  218. pgtable_trans_huge_deposit(args->mm, args->pmdp, args->start_ptep);
  219. pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
  220. set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
  221. flush_dcache_page(page);
  222. pmdp_set_wrprotect(args->mm, vaddr, args->pmdp);
  223. pmd = READ_ONCE(*args->pmdp);
  224. WARN_ON(pmd_write(pmd));
  225. pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
  226. pmd = READ_ONCE(*args->pmdp);
  227. WARN_ON(!pmd_none(pmd));
  228. pmd = pfn_pmd(args->pmd_pfn, args->page_prot);
  229. pmd = pmd_wrprotect(pmd);
  230. pmd = pmd_mkclean(pmd);
  231. set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
  232. flush_dcache_page(page);
  233. pmd = pmd_mkwrite(pmd);
  234. pmd = pmd_mkdirty(pmd);
  235. pmdp_set_access_flags(args->vma, vaddr, args->pmdp, pmd, 1);
  236. pmd = READ_ONCE(*args->pmdp);
  237. WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
  238. pmdp_huge_get_and_clear_full(args->vma, vaddr, args->pmdp, 1);
  239. pmd = READ_ONCE(*args->pmdp);
  240. WARN_ON(!pmd_none(pmd));
  241. pmd = pmd_mkhuge(pfn_pmd(args->pmd_pfn, args->page_prot));
  242. pmd = pmd_mkyoung(pmd);
  243. set_pmd_at(args->mm, vaddr, args->pmdp, pmd);
  244. flush_dcache_page(page);
  245. pmdp_test_and_clear_young(args->vma, vaddr, args->pmdp);
  246. pmd = READ_ONCE(*args->pmdp);
  247. WARN_ON(pmd_young(pmd));
  248. /* Clear the pte entries */
  249. pmdp_huge_get_and_clear(args->mm, vaddr, args->pmdp);
  250. pgtable_trans_huge_withdraw(args->mm, args->pmdp);
  251. }
  252. static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
  253. {
  254. pmd_t pmd;
  255. if (!has_transparent_hugepage())
  256. return;
  257. pr_debug("Validating PMD leaf\n");
  258. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  259. /*
  260. * PMD based THP is a leaf entry.
  261. */
  262. pmd = pmd_mkhuge(pmd);
  263. WARN_ON(!pmd_leaf(pmd));
  264. }
  265. static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
  266. {
  267. pmd_t pmd;
  268. if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
  269. return;
  270. if (!has_transparent_hugepage())
  271. return;
  272. pr_debug("Validating PMD saved write\n");
  273. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
  274. WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
  275. WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
  276. }
  277. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  278. static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
  279. {
  280. pgprot_t prot = vm_get_page_prot(idx);
  281. unsigned long val = idx, *ptr = &val;
  282. pud_t pud;
  283. if (!has_transparent_hugepage())
  284. return;
  285. pr_debug("Validating PUD basic (%pGv)\n", ptr);
  286. pud = pfn_pud(args->fixed_pud_pfn, prot);
  287. /*
  288. * This test needs to be executed after the given page table entry
  289. * is created with pfn_pud() to make sure that vm_get_page_prot(idx)
  290. * does not have the dirty bit enabled from the beginning. This is
  291. * important for platforms like arm64 where (!PTE_RDONLY) indicate
  292. * dirty bit being set.
  293. */
  294. WARN_ON(pud_dirty(pud_wrprotect(pud)));
  295. WARN_ON(!pud_same(pud, pud));
  296. WARN_ON(!pud_young(pud_mkyoung(pud_mkold(pud))));
  297. WARN_ON(!pud_dirty(pud_mkdirty(pud_mkclean(pud))));
  298. WARN_ON(pud_dirty(pud_mkclean(pud_mkdirty(pud))));
  299. WARN_ON(!pud_write(pud_mkwrite(pud_wrprotect(pud))));
  300. WARN_ON(pud_write(pud_wrprotect(pud_mkwrite(pud))));
  301. WARN_ON(pud_young(pud_mkold(pud_mkyoung(pud))));
  302. WARN_ON(pud_dirty(pud_wrprotect(pud_mkclean(pud))));
  303. WARN_ON(!pud_dirty(pud_wrprotect(pud_mkdirty(pud))));
  304. if (mm_pmd_folded(args->mm))
  305. return;
  306. /*
  307. * A huge page does not point to next level page table
  308. * entry. Hence this must qualify as pud_bad().
  309. */
  310. WARN_ON(!pud_bad(pud_mkhuge(pud)));
  311. }
  312. static void __init pud_advanced_tests(struct pgtable_debug_args *args)
  313. {
  314. struct page *page;
  315. unsigned long vaddr = args->vaddr;
  316. pud_t pud;
  317. if (!has_transparent_hugepage())
  318. return;
  319. page = (args->pud_pfn != ULONG_MAX) ? pfn_to_page(args->pud_pfn) : NULL;
  320. if (!page)
  321. return;
  322. /*
  323. * flush_dcache_page() is called after set_pud_at() to clear
  324. * PG_arch_1 for the page on ARM64. The page flag isn't cleared
  325. * when it's released and page allocation check will fail when
  326. * the page is allocated again. For architectures other than ARM64,
  327. * the unexpected overhead of cache flushing is acceptable.
  328. */
  329. pr_debug("Validating PUD advanced\n");
  330. /* Align the address wrt HPAGE_PUD_SIZE */
  331. vaddr &= HPAGE_PUD_MASK;
  332. pud = pfn_pud(args->pud_pfn, args->page_prot);
  333. set_pud_at(args->mm, vaddr, args->pudp, pud);
  334. flush_dcache_page(page);
  335. pudp_set_wrprotect(args->mm, vaddr, args->pudp);
  336. pud = READ_ONCE(*args->pudp);
  337. WARN_ON(pud_write(pud));
  338. #ifndef __PAGETABLE_PMD_FOLDED
  339. pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
  340. pud = READ_ONCE(*args->pudp);
  341. WARN_ON(!pud_none(pud));
  342. #endif /* __PAGETABLE_PMD_FOLDED */
  343. pud = pfn_pud(args->pud_pfn, args->page_prot);
  344. pud = pud_wrprotect(pud);
  345. pud = pud_mkclean(pud);
  346. set_pud_at(args->mm, vaddr, args->pudp, pud);
  347. flush_dcache_page(page);
  348. pud = pud_mkwrite(pud);
  349. pud = pud_mkdirty(pud);
  350. pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
  351. pud = READ_ONCE(*args->pudp);
  352. WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
  353. #ifndef __PAGETABLE_PMD_FOLDED
  354. pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
  355. pud = READ_ONCE(*args->pudp);
  356. WARN_ON(!pud_none(pud));
  357. #endif /* __PAGETABLE_PMD_FOLDED */
  358. pud = pfn_pud(args->pud_pfn, args->page_prot);
  359. pud = pud_mkyoung(pud);
  360. set_pud_at(args->mm, vaddr, args->pudp, pud);
  361. flush_dcache_page(page);
  362. pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
  363. pud = READ_ONCE(*args->pudp);
  364. WARN_ON(pud_young(pud));
  365. pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
  366. }
  367. static void __init pud_leaf_tests(struct pgtable_debug_args *args)
  368. {
  369. pud_t pud;
  370. if (!has_transparent_hugepage())
  371. return;
  372. pr_debug("Validating PUD leaf\n");
  373. pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
  374. /*
  375. * PUD based THP is a leaf entry.
  376. */
  377. pud = pud_mkhuge(pud);
  378. WARN_ON(!pud_leaf(pud));
  379. }
  380. #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  381. static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
  382. static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
  383. static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
  384. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  385. #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
  386. static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
  387. static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
  388. static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
  389. static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
  390. static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
  391. static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
  392. static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
  393. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  394. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  395. static void __init pmd_huge_tests(struct pgtable_debug_args *args)
  396. {
  397. pmd_t pmd;
  398. if (!arch_vmap_pmd_supported(args->page_prot))
  399. return;
  400. pr_debug("Validating PMD huge\n");
  401. /*
  402. * X86 defined pmd_set_huge() verifies that the given
  403. * PMD is not a populated non-leaf entry.
  404. */
  405. WRITE_ONCE(*args->pmdp, __pmd(0));
  406. WARN_ON(!pmd_set_huge(args->pmdp, __pfn_to_phys(args->fixed_pmd_pfn), args->page_prot));
  407. WARN_ON(!pmd_clear_huge(args->pmdp));
  408. pmd = READ_ONCE(*args->pmdp);
  409. WARN_ON(!pmd_none(pmd));
  410. }
  411. static void __init pud_huge_tests(struct pgtable_debug_args *args)
  412. {
  413. pud_t pud;
  414. if (!arch_vmap_pud_supported(args->page_prot))
  415. return;
  416. pr_debug("Validating PUD huge\n");
  417. /*
  418. * X86 defined pud_set_huge() verifies that the given
  419. * PUD is not a populated non-leaf entry.
  420. */
  421. WRITE_ONCE(*args->pudp, __pud(0));
  422. WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
  423. WARN_ON(!pud_clear_huge(args->pudp));
  424. pud = READ_ONCE(*args->pudp);
  425. WARN_ON(!pud_none(pud));
  426. }
  427. #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
  428. static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
  429. static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
  430. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
  431. static void __init p4d_basic_tests(struct pgtable_debug_args *args)
  432. {
  433. p4d_t p4d;
  434. pr_debug("Validating P4D basic\n");
  435. memset(&p4d, RANDOM_NZVALUE, sizeof(p4d_t));
  436. WARN_ON(!p4d_same(p4d, p4d));
  437. }
  438. static void __init pgd_basic_tests(struct pgtable_debug_args *args)
  439. {
  440. pgd_t pgd;
  441. pr_debug("Validating PGD basic\n");
  442. memset(&pgd, RANDOM_NZVALUE, sizeof(pgd_t));
  443. WARN_ON(!pgd_same(pgd, pgd));
  444. }
  445. #ifndef __PAGETABLE_PUD_FOLDED
  446. static void __init pud_clear_tests(struct pgtable_debug_args *args)
  447. {
  448. pud_t pud = READ_ONCE(*args->pudp);
  449. if (mm_pmd_folded(args->mm))
  450. return;
  451. pr_debug("Validating PUD clear\n");
  452. pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
  453. WRITE_ONCE(*args->pudp, pud);
  454. pud_clear(args->pudp);
  455. pud = READ_ONCE(*args->pudp);
  456. WARN_ON(!pud_none(pud));
  457. }
  458. static void __init pud_populate_tests(struct pgtable_debug_args *args)
  459. {
  460. pud_t pud;
  461. if (mm_pmd_folded(args->mm))
  462. return;
  463. pr_debug("Validating PUD populate\n");
  464. /*
  465. * This entry points to next level page table page.
  466. * Hence this must not qualify as pud_bad().
  467. */
  468. pud_populate(args->mm, args->pudp, args->start_pmdp);
  469. pud = READ_ONCE(*args->pudp);
  470. WARN_ON(pud_bad(pud));
  471. }
  472. #else /* !__PAGETABLE_PUD_FOLDED */
  473. static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
  474. static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
  475. #endif /* PAGETABLE_PUD_FOLDED */
  476. #ifndef __PAGETABLE_P4D_FOLDED
  477. static void __init p4d_clear_tests(struct pgtable_debug_args *args)
  478. {
  479. p4d_t p4d = READ_ONCE(*args->p4dp);
  480. if (mm_pud_folded(args->mm))
  481. return;
  482. pr_debug("Validating P4D clear\n");
  483. p4d = __p4d(p4d_val(p4d) | RANDOM_ORVALUE);
  484. WRITE_ONCE(*args->p4dp, p4d);
  485. p4d_clear(args->p4dp);
  486. p4d = READ_ONCE(*args->p4dp);
  487. WARN_ON(!p4d_none(p4d));
  488. }
  489. static void __init p4d_populate_tests(struct pgtable_debug_args *args)
  490. {
  491. p4d_t p4d;
  492. if (mm_pud_folded(args->mm))
  493. return;
  494. pr_debug("Validating P4D populate\n");
  495. /*
  496. * This entry points to next level page table page.
  497. * Hence this must not qualify as p4d_bad().
  498. */
  499. pud_clear(args->pudp);
  500. p4d_clear(args->p4dp);
  501. p4d_populate(args->mm, args->p4dp, args->start_pudp);
  502. p4d = READ_ONCE(*args->p4dp);
  503. WARN_ON(p4d_bad(p4d));
  504. }
  505. static void __init pgd_clear_tests(struct pgtable_debug_args *args)
  506. {
  507. pgd_t pgd = READ_ONCE(*(args->pgdp));
  508. if (mm_p4d_folded(args->mm))
  509. return;
  510. pr_debug("Validating PGD clear\n");
  511. pgd = __pgd(pgd_val(pgd) | RANDOM_ORVALUE);
  512. WRITE_ONCE(*args->pgdp, pgd);
  513. pgd_clear(args->pgdp);
  514. pgd = READ_ONCE(*args->pgdp);
  515. WARN_ON(!pgd_none(pgd));
  516. }
  517. static void __init pgd_populate_tests(struct pgtable_debug_args *args)
  518. {
  519. pgd_t pgd;
  520. if (mm_p4d_folded(args->mm))
  521. return;
  522. pr_debug("Validating PGD populate\n");
  523. /*
  524. * This entry points to next level page table page.
  525. * Hence this must not qualify as pgd_bad().
  526. */
  527. p4d_clear(args->p4dp);
  528. pgd_clear(args->pgdp);
  529. pgd_populate(args->mm, args->pgdp, args->start_p4dp);
  530. pgd = READ_ONCE(*args->pgdp);
  531. WARN_ON(pgd_bad(pgd));
  532. }
  533. #else /* !__PAGETABLE_P4D_FOLDED */
  534. static void __init p4d_clear_tests(struct pgtable_debug_args *args) { }
  535. static void __init pgd_clear_tests(struct pgtable_debug_args *args) { }
  536. static void __init p4d_populate_tests(struct pgtable_debug_args *args) { }
  537. static void __init pgd_populate_tests(struct pgtable_debug_args *args) { }
  538. #endif /* PAGETABLE_P4D_FOLDED */
  539. static void __init pte_clear_tests(struct pgtable_debug_args *args)
  540. {
  541. struct page *page;
  542. pte_t pte = pfn_pte(args->pte_pfn, args->page_prot);
  543. page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
  544. if (!page)
  545. return;
  546. /*
  547. * flush_dcache_page() is called after set_pte_at() to clear
  548. * PG_arch_1 for the page on ARM64. The page flag isn't cleared
  549. * when it's released and page allocation check will fail when
  550. * the page is allocated again. For architectures other than ARM64,
  551. * the unexpected overhead of cache flushing is acceptable.
  552. */
  553. pr_debug("Validating PTE clear\n");
  554. #ifndef CONFIG_RISCV
  555. pte = __pte(pte_val(pte) | RANDOM_ORVALUE);
  556. #endif
  557. set_pte_at(args->mm, args->vaddr, args->ptep, pte);
  558. flush_dcache_page(page);
  559. barrier();
  560. ptep_clear(args->mm, args->vaddr, args->ptep);
  561. pte = ptep_get(args->ptep);
  562. WARN_ON(!pte_none(pte));
  563. }
  564. static void __init pmd_clear_tests(struct pgtable_debug_args *args)
  565. {
  566. pmd_t pmd = READ_ONCE(*args->pmdp);
  567. pr_debug("Validating PMD clear\n");
  568. pmd = __pmd(pmd_val(pmd) | RANDOM_ORVALUE);
  569. WRITE_ONCE(*args->pmdp, pmd);
  570. pmd_clear(args->pmdp);
  571. pmd = READ_ONCE(*args->pmdp);
  572. WARN_ON(!pmd_none(pmd));
  573. }
  574. static void __init pmd_populate_tests(struct pgtable_debug_args *args)
  575. {
  576. pmd_t pmd;
  577. pr_debug("Validating PMD populate\n");
  578. /*
  579. * This entry points to next level page table page.
  580. * Hence this must not qualify as pmd_bad().
  581. */
  582. pmd_populate(args->mm, args->pmdp, args->start_ptep);
  583. pmd = READ_ONCE(*args->pmdp);
  584. WARN_ON(pmd_bad(pmd));
  585. }
  586. static void __init pte_special_tests(struct pgtable_debug_args *args)
  587. {
  588. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  589. if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
  590. return;
  591. pr_debug("Validating PTE special\n");
  592. WARN_ON(!pte_special(pte_mkspecial(pte)));
  593. }
  594. static void __init pte_protnone_tests(struct pgtable_debug_args *args)
  595. {
  596. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);
  597. if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
  598. return;
  599. pr_debug("Validating PTE protnone\n");
  600. WARN_ON(!pte_protnone(pte));
  601. WARN_ON(!pte_present(pte));
  602. }
  603. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  604. static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
  605. {
  606. pmd_t pmd;
  607. if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
  608. return;
  609. if (!has_transparent_hugepage())
  610. return;
  611. pr_debug("Validating PMD protnone\n");
  612. pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
  613. WARN_ON(!pmd_protnone(pmd));
  614. WARN_ON(!pmd_present(pmd));
  615. }
  616. #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
  617. static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
  618. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  619. #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
  620. static void __init pte_devmap_tests(struct pgtable_debug_args *args)
  621. {
  622. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  623. pr_debug("Validating PTE devmap\n");
  624. WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
  625. }
  626. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  627. static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
  628. {
  629. pmd_t pmd;
  630. if (!has_transparent_hugepage())
  631. return;
  632. pr_debug("Validating PMD devmap\n");
  633. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  634. WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
  635. }
  636. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  637. static void __init pud_devmap_tests(struct pgtable_debug_args *args)
  638. {
  639. pud_t pud;
  640. if (!has_transparent_hugepage())
  641. return;
  642. pr_debug("Validating PUD devmap\n");
  643. pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
  644. WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
  645. }
  646. #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  647. static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
  648. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  649. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  650. static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
  651. static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
  652. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  653. #else
  654. static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
  655. static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
  656. static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
  657. #endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */
  658. static void __init pte_soft_dirty_tests(struct pgtable_debug_args *args)
  659. {
  660. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  661. if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
  662. return;
  663. pr_debug("Validating PTE soft dirty\n");
  664. WARN_ON(!pte_soft_dirty(pte_mksoft_dirty(pte)));
  665. WARN_ON(pte_soft_dirty(pte_clear_soft_dirty(pte)));
  666. }
  667. static void __init pte_swap_soft_dirty_tests(struct pgtable_debug_args *args)
  668. {
  669. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  670. if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
  671. return;
  672. pr_debug("Validating PTE swap soft dirty\n");
  673. WARN_ON(!pte_swp_soft_dirty(pte_swp_mksoft_dirty(pte)));
  674. WARN_ON(pte_swp_soft_dirty(pte_swp_clear_soft_dirty(pte)));
  675. }
  676. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  677. static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args)
  678. {
  679. pmd_t pmd;
  680. if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
  681. return;
  682. if (!has_transparent_hugepage())
  683. return;
  684. pr_debug("Validating PMD soft dirty\n");
  685. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  686. WARN_ON(!pmd_soft_dirty(pmd_mksoft_dirty(pmd)));
  687. WARN_ON(pmd_soft_dirty(pmd_clear_soft_dirty(pmd)));
  688. }
  689. static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args)
  690. {
  691. pmd_t pmd;
  692. if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) ||
  693. !IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION))
  694. return;
  695. if (!has_transparent_hugepage())
  696. return;
  697. pr_debug("Validating PMD swap soft dirty\n");
  698. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  699. WARN_ON(!pmd_swp_soft_dirty(pmd_swp_mksoft_dirty(pmd)));
  700. WARN_ON(pmd_swp_soft_dirty(pmd_swp_clear_soft_dirty(pmd)));
  701. }
  702. #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
  703. static void __init pmd_soft_dirty_tests(struct pgtable_debug_args *args) { }
  704. static void __init pmd_swap_soft_dirty_tests(struct pgtable_debug_args *args) { }
  705. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  706. static void __init pte_swap_exclusive_tests(struct pgtable_debug_args *args)
  707. {
  708. #ifdef __HAVE_ARCH_PTE_SWP_EXCLUSIVE
  709. pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  710. pr_debug("Validating PTE swap exclusive\n");
  711. pte = pte_swp_mkexclusive(pte);
  712. WARN_ON(!pte_swp_exclusive(pte));
  713. pte = pte_swp_clear_exclusive(pte);
  714. WARN_ON(pte_swp_exclusive(pte));
  715. #endif /* __HAVE_ARCH_PTE_SWP_EXCLUSIVE */
  716. }
  717. static void __init pte_swap_tests(struct pgtable_debug_args *args)
  718. {
  719. swp_entry_t swp;
  720. pte_t pte;
  721. pr_debug("Validating PTE swap\n");
  722. pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);
  723. swp = __pte_to_swp_entry(pte);
  724. pte = __swp_entry_to_pte(swp);
  725. WARN_ON(args->fixed_pte_pfn != pte_pfn(pte));
  726. }
  727. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  728. static void __init pmd_swap_tests(struct pgtable_debug_args *args)
  729. {
  730. swp_entry_t swp;
  731. pmd_t pmd;
  732. if (!has_transparent_hugepage())
  733. return;
  734. pr_debug("Validating PMD swap\n");
  735. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  736. swp = __pmd_to_swp_entry(pmd);
  737. pmd = __swp_entry_to_pmd(swp);
  738. WARN_ON(args->fixed_pmd_pfn != pmd_pfn(pmd));
  739. }
  740. #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
  741. static void __init pmd_swap_tests(struct pgtable_debug_args *args) { }
  742. #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
  743. static void __init swap_migration_tests(struct pgtable_debug_args *args)
  744. {
  745. struct page *page;
  746. swp_entry_t swp;
  747. if (!IS_ENABLED(CONFIG_MIGRATION))
  748. return;
  749. /*
  750. * swap_migration_tests() requires a dedicated page as it needs to
  751. * be locked before creating a migration entry from it. Locking the
  752. * page that actually maps kernel text ('start_kernel') can be real
  753. * problematic. Lets use the allocated page explicitly for this
  754. * purpose.
  755. */
  756. page = (args->pte_pfn != ULONG_MAX) ? pfn_to_page(args->pte_pfn) : NULL;
  757. if (!page)
  758. return;
  759. pr_debug("Validating swap migration\n");
  760. /*
  761. * make_[readable|writable]_migration_entry() expects given page to
  762. * be locked, otherwise it stumbles upon a BUG_ON().
  763. */
  764. __SetPageLocked(page);
  765. swp = make_writable_migration_entry(page_to_pfn(page));
  766. WARN_ON(!is_migration_entry(swp));
  767. WARN_ON(!is_writable_migration_entry(swp));
  768. swp = make_readable_migration_entry(swp_offset(swp));
  769. WARN_ON(!is_migration_entry(swp));
  770. WARN_ON(is_writable_migration_entry(swp));
  771. swp = make_readable_migration_entry(page_to_pfn(page));
  772. WARN_ON(!is_migration_entry(swp));
  773. WARN_ON(is_writable_migration_entry(swp));
  774. __ClearPageLocked(page);
  775. }
  776. #ifdef CONFIG_HUGETLB_PAGE
  777. static void __init hugetlb_basic_tests(struct pgtable_debug_args *args)
  778. {
  779. struct page *page;
  780. pte_t pte;
  781. pr_debug("Validating HugeTLB basic\n");
  782. /*
  783. * Accessing the page associated with the pfn is safe here,
  784. * as it was previously derived from a real kernel symbol.
  785. */
  786. page = pfn_to_page(args->fixed_pmd_pfn);
  787. pte = mk_huge_pte(page, args->page_prot);
  788. WARN_ON(!huge_pte_dirty(huge_pte_mkdirty(pte)));
  789. WARN_ON(!huge_pte_write(huge_pte_mkwrite(huge_pte_wrprotect(pte))));
  790. WARN_ON(huge_pte_write(huge_pte_wrprotect(huge_pte_mkwrite(pte))));
  791. #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
  792. pte = pfn_pte(args->fixed_pmd_pfn, args->page_prot);
  793. WARN_ON(!pte_huge(pte_mkhuge(pte)));
  794. #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  795. }
  796. #else /* !CONFIG_HUGETLB_PAGE */
  797. static void __init hugetlb_basic_tests(struct pgtable_debug_args *args) { }
  798. #endif /* CONFIG_HUGETLB_PAGE */
  799. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  800. static void __init pmd_thp_tests(struct pgtable_debug_args *args)
  801. {
  802. pmd_t pmd;
  803. if (!has_transparent_hugepage())
  804. return;
  805. pr_debug("Validating PMD based THP\n");
  806. /*
  807. * pmd_trans_huge() and pmd_present() must return positive after
  808. * MMU invalidation with pmd_mkinvalid(). This behavior is an
  809. * optimization for transparent huge page. pmd_trans_huge() must
  810. * be true if pmd_page() returns a valid THP to avoid taking the
  811. * pmd_lock when others walk over non transhuge pmds (i.e. there
  812. * are no THP allocated). Especially when splitting a THP and
  813. * removing the present bit from the pmd, pmd_trans_huge() still
  814. * needs to return true. pmd_present() should be true whenever
  815. * pmd_trans_huge() returns true.
  816. */
  817. pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
  818. WARN_ON(!pmd_trans_huge(pmd_mkhuge(pmd)));
  819. #ifndef __HAVE_ARCH_PMDP_INVALIDATE
  820. WARN_ON(!pmd_trans_huge(pmd_mkinvalid(pmd_mkhuge(pmd))));
  821. WARN_ON(!pmd_present(pmd_mkinvalid(pmd_mkhuge(pmd))));
  822. #endif /* __HAVE_ARCH_PMDP_INVALIDATE */
  823. }
  824. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  825. static void __init pud_thp_tests(struct pgtable_debug_args *args)
  826. {
  827. pud_t pud;
  828. if (!has_transparent_hugepage())
  829. return;
  830. pr_debug("Validating PUD based THP\n");
  831. pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
  832. WARN_ON(!pud_trans_huge(pud_mkhuge(pud)));
  833. /*
  834. * pud_mkinvalid() has been dropped for now. Enable back
  835. * these tests when it comes back with a modified pud_present().
  836. *
  837. * WARN_ON(!pud_trans_huge(pud_mkinvalid(pud_mkhuge(pud))));
  838. * WARN_ON(!pud_present(pud_mkinvalid(pud_mkhuge(pud))));
  839. */
  840. }
  841. #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  842. static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
  843. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  844. #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
  845. static void __init pmd_thp_tests(struct pgtable_debug_args *args) { }
  846. static void __init pud_thp_tests(struct pgtable_debug_args *args) { }
  847. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  848. static unsigned long __init get_random_vaddr(void)
  849. {
  850. unsigned long random_vaddr, random_pages, total_user_pages;
  851. total_user_pages = (TASK_SIZE - FIRST_USER_ADDRESS) / PAGE_SIZE;
  852. random_pages = get_random_long() % total_user_pages;
  853. random_vaddr = FIRST_USER_ADDRESS + random_pages * PAGE_SIZE;
  854. return random_vaddr;
  855. }
  856. static void __init destroy_args(struct pgtable_debug_args *args)
  857. {
  858. struct page *page = NULL;
  859. /* Free (huge) page */
  860. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  861. IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
  862. has_transparent_hugepage() &&
  863. args->pud_pfn != ULONG_MAX) {
  864. if (args->is_contiguous_page) {
  865. free_contig_range(args->pud_pfn,
  866. (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
  867. } else {
  868. page = pfn_to_page(args->pud_pfn);
  869. __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
  870. }
  871. args->pud_pfn = ULONG_MAX;
  872. args->pmd_pfn = ULONG_MAX;
  873. args->pte_pfn = ULONG_MAX;
  874. }
  875. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  876. has_transparent_hugepage() &&
  877. args->pmd_pfn != ULONG_MAX) {
  878. if (args->is_contiguous_page) {
  879. free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
  880. } else {
  881. page = pfn_to_page(args->pmd_pfn);
  882. __free_pages(page, HPAGE_PMD_ORDER);
  883. }
  884. args->pmd_pfn = ULONG_MAX;
  885. args->pte_pfn = ULONG_MAX;
  886. }
  887. if (args->pte_pfn != ULONG_MAX) {
  888. page = pfn_to_page(args->pte_pfn);
  889. __free_pages(page, 0);
  890. args->pte_pfn = ULONG_MAX;
  891. }
  892. /* Free page table entries */
  893. if (args->start_ptep) {
  894. pte_free(args->mm, args->start_ptep);
  895. mm_dec_nr_ptes(args->mm);
  896. }
  897. if (args->start_pmdp) {
  898. pmd_free(args->mm, args->start_pmdp);
  899. mm_dec_nr_pmds(args->mm);
  900. }
  901. if (args->start_pudp) {
  902. pud_free(args->mm, args->start_pudp);
  903. mm_dec_nr_puds(args->mm);
  904. }
  905. if (args->start_p4dp)
  906. p4d_free(args->mm, args->start_p4dp);
  907. /* Free vma and mm struct */
  908. if (args->vma)
  909. vm_area_free(args->vma);
  910. if (args->mm)
  911. mmdrop(args->mm);
  912. }
  913. static struct page * __init
  914. debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
  915. {
  916. struct page *page = NULL;
  917. #ifdef CONFIG_CONTIG_ALLOC
  918. if (order >= MAX_ORDER) {
  919. page = alloc_contig_pages((1 << order), GFP_KERNEL,
  920. first_online_node, NULL);
  921. if (page) {
  922. args->is_contiguous_page = true;
  923. return page;
  924. }
  925. }
  926. #endif
  927. if (order < MAX_ORDER)
  928. page = alloc_pages(GFP_KERNEL, order);
  929. return page;
  930. }
  931. static int __init init_args(struct pgtable_debug_args *args)
  932. {
  933. struct page *page = NULL;
  934. phys_addr_t phys;
  935. int ret = 0;
  936. /*
  937. * Initialize the debugging data.
  938. *
  939. * vm_get_page_prot(VM_NONE) or vm_get_page_prot(VM_SHARED|VM_NONE)
  940. * will help create page table entries with PROT_NONE permission as
  941. * required for pxx_protnone_tests().
  942. */
  943. memset(args, 0, sizeof(*args));
  944. args->vaddr = get_random_vaddr();
  945. args->page_prot = vm_get_page_prot(VMFLAGS);
  946. args->page_prot_none = vm_get_page_prot(VM_NONE);
  947. args->is_contiguous_page = false;
  948. args->pud_pfn = ULONG_MAX;
  949. args->pmd_pfn = ULONG_MAX;
  950. args->pte_pfn = ULONG_MAX;
  951. args->fixed_pgd_pfn = ULONG_MAX;
  952. args->fixed_p4d_pfn = ULONG_MAX;
  953. args->fixed_pud_pfn = ULONG_MAX;
  954. args->fixed_pmd_pfn = ULONG_MAX;
  955. args->fixed_pte_pfn = ULONG_MAX;
  956. /* Allocate mm and vma */
  957. args->mm = mm_alloc();
  958. if (!args->mm) {
  959. pr_err("Failed to allocate mm struct\n");
  960. ret = -ENOMEM;
  961. goto error;
  962. }
  963. args->vma = vm_area_alloc(args->mm);
  964. if (!args->vma) {
  965. pr_err("Failed to allocate vma\n");
  966. ret = -ENOMEM;
  967. goto error;
  968. }
  969. /*
  970. * Allocate page table entries. They will be modified in the tests.
  971. * Lets save the page table entries so that they can be released
  972. * when the tests are completed.
  973. */
  974. args->pgdp = pgd_offset(args->mm, args->vaddr);
  975. args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
  976. if (!args->p4dp) {
  977. pr_err("Failed to allocate p4d entries\n");
  978. ret = -ENOMEM;
  979. goto error;
  980. }
  981. args->start_p4dp = p4d_offset(args->pgdp, 0UL);
  982. WARN_ON(!args->start_p4dp);
  983. args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
  984. if (!args->pudp) {
  985. pr_err("Failed to allocate pud entries\n");
  986. ret = -ENOMEM;
  987. goto error;
  988. }
  989. args->start_pudp = pud_offset(args->p4dp, 0UL);
  990. WARN_ON(!args->start_pudp);
  991. args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
  992. if (!args->pmdp) {
  993. pr_err("Failed to allocate pmd entries\n");
  994. ret = -ENOMEM;
  995. goto error;
  996. }
  997. args->start_pmdp = pmd_offset(args->pudp, 0UL);
  998. WARN_ON(!args->start_pmdp);
  999. if (pte_alloc(args->mm, args->pmdp)) {
  1000. pr_err("Failed to allocate pte entries\n");
  1001. ret = -ENOMEM;
  1002. goto error;
  1003. }
  1004. args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
  1005. WARN_ON(!args->start_ptep);
  1006. /*
  1007. * PFN for mapping at PTE level is determined from a standard kernel
  1008. * text symbol. But pfns for higher page table levels are derived by
  1009. * masking lower bits of this real pfn. These derived pfns might not
  1010. * exist on the platform but that does not really matter as pfn_pxx()
  1011. * helpers will still create appropriate entries for the test. This
  1012. * helps avoid large memory block allocations to be used for mapping
  1013. * at higher page table levels in some of the tests.
  1014. */
  1015. phys = __pa_symbol(&start_kernel);
  1016. args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
  1017. args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
  1018. args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
  1019. args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
  1020. args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
  1021. WARN_ON(!pfn_valid(args->fixed_pte_pfn));
  1022. /*
  1023. * Allocate (huge) pages because some of the tests need to access
  1024. * the data in the pages. The corresponding tests will be skipped
  1025. * if we fail to allocate (huge) pages.
  1026. */
  1027. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  1028. IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
  1029. has_transparent_hugepage()) {
  1030. page = debug_vm_pgtable_alloc_huge_page(args,
  1031. HPAGE_PUD_SHIFT - PAGE_SHIFT);
  1032. if (page) {
  1033. args->pud_pfn = page_to_pfn(page);
  1034. args->pmd_pfn = args->pud_pfn;
  1035. args->pte_pfn = args->pud_pfn;
  1036. return 0;
  1037. }
  1038. }
  1039. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  1040. has_transparent_hugepage()) {
  1041. page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
  1042. if (page) {
  1043. args->pmd_pfn = page_to_pfn(page);
  1044. args->pte_pfn = args->pmd_pfn;
  1045. return 0;
  1046. }
  1047. }
  1048. page = alloc_pages(GFP_KERNEL, 0);
  1049. if (page)
  1050. args->pte_pfn = page_to_pfn(page);
  1051. return 0;
  1052. error:
  1053. destroy_args(args);
  1054. return ret;
  1055. }
  1056. static int __init debug_vm_pgtable(void)
  1057. {
  1058. struct pgtable_debug_args args;
  1059. spinlock_t *ptl = NULL;
  1060. int idx, ret;
  1061. pr_info("Validating architecture page table helpers\n");
  1062. ret = init_args(&args);
  1063. if (ret)
  1064. return ret;
  1065. /*
  1066. * Iterate over each possible vm_flags to make sure that all
  1067. * the basic page table transformation validations just hold
  1068. * true irrespective of the starting protection value for a
  1069. * given page table entry.
  1070. *
  1071. * Protection based vm_flags combinatins are always linear
  1072. * and increasing i.e starting from VM_NONE and going upto
  1073. * (VM_SHARED | READ | WRITE | EXEC).
  1074. */
  1075. #define VM_FLAGS_START (VM_NONE)
  1076. #define VM_FLAGS_END (VM_SHARED | VM_EXEC | VM_WRITE | VM_READ)
  1077. for (idx = VM_FLAGS_START; idx <= VM_FLAGS_END; idx++) {
  1078. pte_basic_tests(&args, idx);
  1079. pmd_basic_tests(&args, idx);
  1080. pud_basic_tests(&args, idx);
  1081. }
  1082. /*
  1083. * Both P4D and PGD level tests are very basic which do not
  1084. * involve creating page table entries from the protection
  1085. * value and the given pfn. Hence just keep them out from
  1086. * the above iteration for now to save some test execution
  1087. * time.
  1088. */
  1089. p4d_basic_tests(&args);
  1090. pgd_basic_tests(&args);
  1091. pmd_leaf_tests(&args);
  1092. pud_leaf_tests(&args);
  1093. pte_savedwrite_tests(&args);
  1094. pmd_savedwrite_tests(&args);
  1095. pte_special_tests(&args);
  1096. pte_protnone_tests(&args);
  1097. pmd_protnone_tests(&args);
  1098. pte_devmap_tests(&args);
  1099. pmd_devmap_tests(&args);
  1100. pud_devmap_tests(&args);
  1101. pte_soft_dirty_tests(&args);
  1102. pmd_soft_dirty_tests(&args);
  1103. pte_swap_soft_dirty_tests(&args);
  1104. pmd_swap_soft_dirty_tests(&args);
  1105. pte_swap_exclusive_tests(&args);
  1106. pte_swap_tests(&args);
  1107. pmd_swap_tests(&args);
  1108. swap_migration_tests(&args);
  1109. pmd_thp_tests(&args);
  1110. pud_thp_tests(&args);
  1111. hugetlb_basic_tests(&args);
  1112. /*
  1113. * Page table modifying tests. They need to hold
  1114. * proper page table lock.
  1115. */
  1116. args.ptep = pte_offset_map_lock(args.mm, args.pmdp, args.vaddr, &ptl);
  1117. pte_clear_tests(&args);
  1118. pte_advanced_tests(&args);
  1119. pte_unmap_unlock(args.ptep, ptl);
  1120. ptl = pmd_lock(args.mm, args.pmdp);
  1121. pmd_clear_tests(&args);
  1122. pmd_advanced_tests(&args);
  1123. pmd_huge_tests(&args);
  1124. pmd_populate_tests(&args);
  1125. spin_unlock(ptl);
  1126. ptl = pud_lock(args.mm, args.pudp);
  1127. pud_clear_tests(&args);
  1128. pud_advanced_tests(&args);
  1129. pud_huge_tests(&args);
  1130. pud_populate_tests(&args);
  1131. spin_unlock(ptl);
  1132. spin_lock(&(args.mm->page_table_lock));
  1133. p4d_clear_tests(&args);
  1134. pgd_clear_tests(&args);
  1135. p4d_populate_tests(&args);
  1136. pgd_populate_tests(&args);
  1137. spin_unlock(&(args.mm->page_table_lock));
  1138. destroy_args(&args);
  1139. return 0;
  1140. }
  1141. late_initcall(debug_vm_pgtable);