pageattr-test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * self test for change_page_attr.
  3. *
  4. * Clears the a test pte bit on random pages in the direct mapping,
  5. * then reverts and compares page tables forwards and afterwards.
  6. */
  7. #include <linux/bootmem.h>
  8. #include <linux/kthread.h>
  9. #include <linux/random.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/kdebug.h>
  17. /*
  18. * Only print the results of the first pass:
  19. */
  20. static __read_mostly int print = 1;
  21. enum {
  22. NTEST = 400,
  23. #ifdef CONFIG_X86_64
  24. LPS = (1 << PMD_SHIFT),
  25. #elif defined(CONFIG_X86_PAE)
  26. LPS = (1 << PMD_SHIFT),
  27. #else
  28. LPS = (1 << 22),
  29. #endif
  30. GPS = (1<<30)
  31. };
  32. #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
  33. static int pte_testbit(pte_t pte)
  34. {
  35. return pte_flags(pte) & _PAGE_SOFTW1;
  36. }
  37. struct split_state {
  38. long lpg, gpg, spg, exec;
  39. long min_exec, max_exec;
  40. };
  41. static int print_split(struct split_state *s)
  42. {
  43. long i, expected, missed = 0;
  44. int err = 0;
  45. s->lpg = s->gpg = s->spg = s->exec = 0;
  46. s->min_exec = ~0UL;
  47. s->max_exec = 0;
  48. for (i = 0; i < max_pfn_mapped; ) {
  49. unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
  50. unsigned int level;
  51. pte_t *pte;
  52. pte = lookup_address(addr, &level);
  53. if (!pte) {
  54. missed++;
  55. i++;
  56. continue;
  57. }
  58. if (level == PG_LEVEL_1G && sizeof(long) == 8) {
  59. s->gpg++;
  60. i += GPS/PAGE_SIZE;
  61. } else if (level == PG_LEVEL_2M) {
  62. if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
  63. printk(KERN_ERR
  64. "%lx level %d but not PSE %Lx\n",
  65. addr, level, (u64)pte_val(*pte));
  66. err = 1;
  67. }
  68. s->lpg++;
  69. i += LPS/PAGE_SIZE;
  70. } else {
  71. s->spg++;
  72. i++;
  73. }
  74. if (!(pte_val(*pte) & _PAGE_NX)) {
  75. s->exec++;
  76. if (addr < s->min_exec)
  77. s->min_exec = addr;
  78. if (addr > s->max_exec)
  79. s->max_exec = addr;
  80. }
  81. }
  82. if (print) {
  83. printk(KERN_INFO
  84. " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
  85. s->spg, s->lpg, s->gpg, s->exec,
  86. s->min_exec != ~0UL ? s->min_exec : 0,
  87. s->max_exec, missed);
  88. }
  89. expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
  90. if (expected != i) {
  91. printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
  92. max_pfn_mapped, expected);
  93. return 1;
  94. }
  95. return err;
  96. }
  97. static unsigned long addr[NTEST];
  98. static unsigned int len[NTEST];
  99. /* Change the global bit on random pages in the direct mapping */
  100. static int pageattr_test(void)
  101. {
  102. struct split_state sa, sb, sc;
  103. unsigned long *bm;
  104. pte_t *pte, pte0;
  105. int failed = 0;
  106. unsigned int level;
  107. int i, k;
  108. int err;
  109. unsigned long test_addr;
  110. if (print)
  111. printk(KERN_INFO "CPA self-test:\n");
  112. bm = vzalloc((max_pfn_mapped + 7) / 8);
  113. if (!bm) {
  114. printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
  115. return -ENOMEM;
  116. }
  117. failed += print_split(&sa);
  118. for (i = 0; i < NTEST; i++) {
  119. unsigned long pfn = prandom_u32() % max_pfn_mapped;
  120. addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
  121. len[i] = prandom_u32() % 100;
  122. len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
  123. if (len[i] == 0)
  124. len[i] = 1;
  125. pte = NULL;
  126. pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
  127. for (k = 0; k < len[i]; k++) {
  128. pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
  129. if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
  130. !(pte_val(*pte) & _PAGE_PRESENT)) {
  131. addr[i] = 0;
  132. break;
  133. }
  134. if (k == 0) {
  135. pte0 = *pte;
  136. } else {
  137. if (pgprot_val(pte_pgprot(*pte)) !=
  138. pgprot_val(pte_pgprot(pte0))) {
  139. len[i] = k;
  140. break;
  141. }
  142. }
  143. if (test_bit(pfn + k, bm)) {
  144. len[i] = k;
  145. break;
  146. }
  147. __set_bit(pfn + k, bm);
  148. }
  149. if (!addr[i] || !pte || !k) {
  150. addr[i] = 0;
  151. continue;
  152. }
  153. test_addr = addr[i];
  154. err = change_page_attr_set(&test_addr, len[i], PAGE_CPA_TEST, 0);
  155. if (err < 0) {
  156. printk(KERN_ERR "CPA %d failed %d\n", i, err);
  157. failed++;
  158. }
  159. pte = lookup_address(addr[i], &level);
  160. if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
  161. printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
  162. pte ? (u64)pte_val(*pte) : 0ULL);
  163. failed++;
  164. }
  165. if (level != PG_LEVEL_4K) {
  166. printk(KERN_ERR "CPA %lx: unexpected level %d\n",
  167. addr[i], level);
  168. failed++;
  169. }
  170. }
  171. vfree(bm);
  172. failed += print_split(&sb);
  173. for (i = 0; i < NTEST; i++) {
  174. if (!addr[i])
  175. continue;
  176. pte = lookup_address(addr[i], &level);
  177. if (!pte) {
  178. printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
  179. failed++;
  180. continue;
  181. }
  182. test_addr = addr[i];
  183. err = change_page_attr_clear(&test_addr, len[i], PAGE_CPA_TEST, 0);
  184. if (err < 0) {
  185. printk(KERN_ERR "CPA reverting failed: %d\n", err);
  186. failed++;
  187. }
  188. pte = lookup_address(addr[i], &level);
  189. if (!pte || pte_testbit(*pte)) {
  190. printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
  191. addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
  192. failed++;
  193. }
  194. }
  195. failed += print_split(&sc);
  196. if (failed) {
  197. WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
  198. return -EINVAL;
  199. } else {
  200. if (print)
  201. printk(KERN_INFO "ok.\n");
  202. }
  203. return 0;
  204. }
  205. static int do_pageattr_test(void *__unused)
  206. {
  207. while (!kthread_should_stop()) {
  208. schedule_timeout_interruptible(HZ*30);
  209. if (pageattr_test() < 0)
  210. break;
  211. if (print)
  212. print--;
  213. }
  214. return 0;
  215. }
  216. static int start_pageattr_test(void)
  217. {
  218. struct task_struct *p;
  219. p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
  220. if (!IS_ERR(p))
  221. wake_up_process(p);
  222. else
  223. WARN_ON(1);
  224. return 0;
  225. }
  226. device_initcall(start_pageattr_test);