blob: 75f1b109aae8fe6711d12b440df3743f38150ce3 [file] [log] [blame]
Andi Kleenfa2d8362008-01-30 13:33:43 +01001/*
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 Molnar851339b2008-01-30 13:33:43 +01007#include <linux/bootmem.h>
Ingo Molnar971a52d2008-02-06 22:39:45 +01008#include <linux/kthread.h>
Andi Kleenfa2d8362008-01-30 13:33:43 +01009#include <linux/random.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010012#include <linux/mm.h>
13
Andi Kleenfa2d8362008-01-30 13:33:43 +010014#include <asm/cacheflush.h>
15#include <asm/pgtable.h>
16#include <asm/kdebug.h>
17
Ingo Molnar971a52d2008-02-06 22:39:45 +010018/*
19 * Only print the results of the first pass:
20 */
21static __read_mostly int print = 1;
22
Andi Kleenfa2d8362008-01-30 13:33:43 +010023enum {
Ingo Molnar971a52d2008-02-06 22:39:45 +010024 NTEST = 400,
Andi Kleenfa2d8362008-01-30 13:33:43 +010025#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010026 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010027#elif defined(CONFIG_X86_PAE)
Ingo Molnar851339b2008-01-30 13:33:43 +010028 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010029#else
Ingo Molnar851339b2008-01-30 13:33:43 +010030 LPS = (1 << 22),
Andi Kleenfa2d8362008-01-30 13:33:43 +010031#endif
Ingo Molnar851339b2008-01-30 13:33:43 +010032 GPS = (1<<30)
Andi Kleenfa2d8362008-01-30 13:33:43 +010033};
34
Andi Kleenfa2d8362008-01-30 13:33:43 +010035struct split_state {
36 long lpg, gpg, spg, exec;
37 long min_exec, max_exec;
38};
39
Ingo Molnar971a52d2008-02-06 22:39:45 +010040static int print_split(struct split_state *s)
Andi Kleenfa2d8362008-01-30 13:33:43 +010041{
Andi Kleenfa2d8362008-01-30 13:33:43 +010042 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 Gleixnerf87519e2008-01-30 13:34:05 +010048 for (i = 0; i < max_pfn_mapped; ) {
Ingo Molnar851339b2008-01-30 13:33:43 +010049 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
Harvey Harrison93809be2008-02-01 17:49:43 +010050 unsigned int level;
Andi Kleenfa2d8362008-01-30 13:33:43 +010051 pte_t *pte;
Andi Kleenfa2d8362008-01-30 13:33:43 +010052
Ingo Molnar851339b2008-01-30 13:33:43 +010053 pte = lookup_address(addr, &level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010054 if (!pte) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010055 missed++;
56 i++;
57 continue;
58 }
59
Ingo Molnar86f03989d2008-01-30 13:34:09 +010060 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010061 s->gpg++;
62 i += GPS/PAGE_SIZE;
Ingo Molnar86f03989d2008-01-30 13:34:09 +010063 } else if (level == PG_LEVEL_2M) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010064 if (!(pte_val(*pte) & _PAGE_PSE)) {
Ingo Molnar851339b2008-01-30 13:33:43 +010065 printk(KERN_ERR
66 "%lx level %d but not PSE %Lx\n",
67 addr, level, (u64)pte_val(*pte));
Andi Kleenfa2d8362008-01-30 13:33:43 +010068 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 Molnar851339b2008-01-30 13:33:43 +010078 if (addr < s->min_exec)
79 s->min_exec = addr;
80 if (addr > s->max_exec)
81 s->max_exec = addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +010082 }
83 }
Ingo Molnar971a52d2008-02-06 22:39:45 +010084 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 Molnar851339b2008-01-30 13:33:43 +010091
Andi Kleenfa2d8362008-01-30 13:33:43 +010092 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
93 if (expected != i) {
Thomas Gleixnerf87519e2008-01-30 13:34:05 +010094 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
95 max_pfn_mapped, expected);
Andi Kleenfa2d8362008-01-30 13:33:43 +010096 return 1;
97 }
98 return err;
99}
100
Ingo Molnar971a52d2008-02-06 22:39:45 +0100101static unsigned long addr[NTEST];
102static unsigned int len[NTEST];
Andi Kleenfa2d8362008-01-30 13:33:43 +0100103
104/* Change the global bit on random pages in the direct mapping */
Ingo Molnar971a52d2008-02-06 22:39:45 +0100105static int pageattr_test(void)
Andi Kleenfa2d8362008-01-30 13:33:43 +0100106{
Andi Kleenfa2d8362008-01-30 13:33:43 +0100107 struct split_state sa, sb, sc;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100108 unsigned long *bm;
Ingo Molnar851339b2008-01-30 13:33:43 +0100109 pte_t *pte, pte0;
110 int failed = 0;
Harvey Harrison93809be2008-02-01 17:49:43 +0100111 unsigned int level;
Ingo Molnar851339b2008-01-30 13:33:43 +0100112 int i, k;
113 int err;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100114
Ingo Molnar971a52d2008-02-06 22:39:45 +0100115 if (print)
116 printk(KERN_INFO "CPA self-test:\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100117
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100118 bm = vmalloc((max_pfn_mapped + 7) / 8);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100119 if (!bm) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100120 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100121 return -ENOMEM;
122 }
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100123 memset(bm, 0, (max_pfn_mapped + 7) / 8);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100124
125 failed += print_split(&sa);
126 srandom32(100);
Ingo Molnar851339b2008-01-30 13:33:43 +0100127
Andi Kleenfa2d8362008-01-30 13:33:43 +0100128 for (i = 0; i < NTEST; i++) {
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100129 unsigned long pfn = random32() % max_pfn_mapped;
Ingo Molnar851339b2008-01-30 13:33:43 +0100130
Andi Kleenfa2d8362008-01-30 13:33:43 +0100131 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
132 len[i] = random32() % 100;
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100133 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
Ingo Molnar851339b2008-01-30 13:33:43 +0100134
Andi Kleenfa2d8362008-01-30 13:33:43 +0100135 if (len[i] == 0)
136 len[i] = 1;
137
138 pte = NULL;
139 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
Ingo Molnar851339b2008-01-30 13:33:43 +0100140
Andi Kleenfa2d8362008-01-30 13:33:43 +0100141 for (k = 0; k < len[i]; k++) {
142 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
Thomas Gleixner64f351d2008-02-04 16:48:08 +0100143 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
144 !(pte_val(*pte) & _PAGE_PRESENT)) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100145 addr[i] = 0;
146 break;
147 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100148 if (k == 0) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100149 pte0 = *pte;
Ingo Molnar851339b2008-01-30 13:33:43 +0100150 } else {
151 if (pgprot_val(pte_pgprot(*pte)) !=
Andi Kleenfa2d8362008-01-30 13:33:43 +0100152 pgprot_val(pte_pgprot(pte0))) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100153 len[i] = k;
154 break;
155 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100156 }
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 Molnar86f03989d2008-01-30 13:34:09 +0100168 err = change_page_attr_clear(addr[i], len[i],
Thomas Gleixner72932c72008-01-30 13:34:08 +0100169 __pgprot(_PAGE_GLOBAL));
Andi Kleenfa2d8362008-01-30 13:33:43 +0100170 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100171 printk(KERN_ERR "CPA %d failed %d\n", i, err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100172 failed++;
173 }
174
175 pte = lookup_address(addr[i], &level);
176 if (!pte || pte_global(*pte) || pte_huge(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100177 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100178 pte ? (u64)pte_val(*pte) : 0ULL);
179 failed++;
180 }
Ingo Molnar86f03989d2008-01-30 13:34:09 +0100181 if (level != PG_LEVEL_4K) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100182 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
183 addr[i], level);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100184 failed++;
185 }
186
187 }
188 vfree(bm);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100189
190 failed += print_split(&sb);
191
Andi Kleenfa2d8362008-01-30 13:33:43 +0100192 for (i = 0; i < NTEST; i++) {
193 if (!addr[i])
194 continue;
195 pte = lookup_address(addr[i], &level);
196 if (!pte) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100197 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100198 failed++;
199 continue;
200 }
Ingo Molnar86f03989d2008-01-30 13:34:09 +0100201 err = change_page_attr_set(addr[i], len[i],
Thomas Gleixner72932c72008-01-30 13:34:08 +0100202 __pgprot(_PAGE_GLOBAL));
Andi Kleenfa2d8362008-01-30 13:33:43 +0100203 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100204 printk(KERN_ERR "CPA reverting failed: %d\n", err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100205 failed++;
206 }
207 pte = lookup_address(addr[i], &level);
208 if (!pte || !pte_global(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100209 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
210 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100211 failed++;
212 }
213
214 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100215
216 failed += print_split(&sc);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100217
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100218 if (failed) {
Ingo Molnar971a52d2008-02-06 22:39:45 +0100219 printk(KERN_ERR "NOT PASSED. Please report.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100220 WARN_ON(1);
Ingo Molnar971a52d2008-02-06 22:39:45 +0100221 return -EINVAL;
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100222 } else {
Ingo Molnar971a52d2008-02-06 22:39:45 +0100223 if (print)
224 printk(KERN_INFO "ok.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100225 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100226
227 return 0;
228}
Ingo Molnar971a52d2008-02-06 22:39:45 +0100229
230static 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
242static 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
255module_init(start_pageattr_test);