x86: cpa: fix the self-test
[wrt350n-kernel.git] / arch / x86 / mm / pageattr-test.c
blob06353d43f72e073cc01034502d426a37f90d015e
1 /*
2 * self test for change_page_attr.
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>
13 #include <asm/cacheflush.h>
14 #include <asm/pgtable.h>
15 #include <asm/kdebug.h>
17 enum {
18 NTEST = 4000,
19 #ifdef CONFIG_X86_64
20 LPS = (1 << PMD_SHIFT),
21 #elif defined(CONFIG_X86_PAE)
22 LPS = (1 << PMD_SHIFT),
23 #else
24 LPS = (1 << 22),
25 #endif
26 GPS = (1<<30)
29 struct split_state {
30 long lpg, gpg, spg, exec;
31 long min_exec, max_exec;
34 static __init int print_split(struct split_state *s)
36 long i, expected, missed = 0;
37 int printed = 0;
38 int err = 0;
40 s->lpg = s->gpg = s->spg = s->exec = 0;
41 s->min_exec = ~0UL;
42 s->max_exec = 0;
43 for (i = 0; i < max_pfn_mapped; ) {
44 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
45 int level;
46 pte_t *pte;
48 pte = lookup_address(addr, &level);
49 if (!pte) {
50 if (!printed) {
51 dump_pagetable(addr);
52 printk(KERN_INFO "CPA %lx no pte level %d\n",
53 addr, level);
54 printed = 1;
56 missed++;
57 i++;
58 continue;
61 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
62 s->gpg++;
63 i += GPS/PAGE_SIZE;
64 } else if (level == PG_LEVEL_2M) {
65 if (!(pte_val(*pte) & _PAGE_PSE)) {
66 printk(KERN_ERR
67 "%lx level %d but not PSE %Lx\n",
68 addr, level, (u64)pte_val(*pte));
69 err = 1;
71 s->lpg++;
72 i += LPS/PAGE_SIZE;
73 } else {
74 s->spg++;
75 i++;
77 if (!(pte_val(*pte) & _PAGE_NX)) {
78 s->exec++;
79 if (addr < s->min_exec)
80 s->min_exec = addr;
81 if (addr > s->max_exec)
82 s->max_exec = addr;
85 printk(KERN_INFO
86 "CPA mapping 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, s->max_exec, missed);
90 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
91 if (expected != i) {
92 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
93 max_pfn_mapped, expected);
94 return 1;
96 return err;
99 static unsigned long __initdata addr[NTEST];
100 static unsigned int __initdata len[NTEST];
102 /* Change the global bit on random pages in the direct mapping */
103 static __init int exercise_pageattr(void)
105 struct split_state sa, sb, sc;
106 unsigned long *bm;
107 pte_t *pte, pte0;
108 int failed = 0;
109 int level;
110 int i, k;
111 int err;
113 printk(KERN_INFO "CPA exercising pageattr\n");
115 bm = vmalloc((max_pfn_mapped + 7) / 8);
116 if (!bm) {
117 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
118 return -ENOMEM;
120 memset(bm, 0, (max_pfn_mapped + 7) / 8);
122 failed += print_split(&sa);
123 srandom32(100);
125 for (i = 0; i < NTEST; i++) {
126 unsigned long pfn = random32() % max_pfn_mapped;
128 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
129 len[i] = random32() % 100;
130 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
132 if (len[i] == 0)
133 len[i] = 1;
135 pte = NULL;
136 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
138 for (k = 0; k < len[i]; k++) {
139 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
140 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0) {
141 addr[i] = 0;
142 break;
144 if (k == 0) {
145 pte0 = *pte;
146 } else {
147 if (pgprot_val(pte_pgprot(*pte)) !=
148 pgprot_val(pte_pgprot(pte0))) {
149 len[i] = k;
150 break;
153 if (test_bit(pfn + k, bm)) {
154 len[i] = k;
155 break;
157 __set_bit(pfn + k, bm);
159 if (!addr[i] || !pte || !k) {
160 addr[i] = 0;
161 continue;
164 err = change_page_attr_clear(addr[i], len[i],
165 __pgprot(_PAGE_GLOBAL));
166 if (err < 0) {
167 printk(KERN_ERR "CPA %d failed %d\n", i, err);
168 failed++;
171 pte = lookup_address(addr[i], &level);
172 if (!pte || pte_global(*pte) || pte_huge(*pte)) {
173 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
174 pte ? (u64)pte_val(*pte) : 0ULL);
175 failed++;
177 if (level != PG_LEVEL_4K) {
178 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
179 addr[i], level);
180 failed++;
184 vfree(bm);
186 failed += print_split(&sb);
188 printk(KERN_INFO "CPA reverting everything\n");
189 for (i = 0; i < NTEST; i++) {
190 if (!addr[i])
191 continue;
192 pte = lookup_address(addr[i], &level);
193 if (!pte) {
194 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
195 failed++;
196 continue;
198 err = change_page_attr_set(addr[i], len[i],
199 __pgprot(_PAGE_GLOBAL));
200 if (err < 0) {
201 printk(KERN_ERR "CPA reverting failed: %d\n", err);
202 failed++;
204 pte = lookup_address(addr[i], &level);
205 if (!pte || !pte_global(*pte)) {
206 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
207 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
208 failed++;
213 failed += print_split(&sc);
215 if (failed) {
216 printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
217 WARN_ON(1);
218 } else {
219 printk(KERN_INFO "CPA selftests PASSED\n");
222 return 0;
224 module_init(exercise_pageattr);