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