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