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