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