Linux 4.6-rc6
[linux/fpc-iii.git] / arch / x86 / mm / pageattr-test.c
blob5f169d5d76a889cf1c489f6f1c8a0f7a40c10c4a
1 /*
2 * self test for change_page_attr.
4 * Clears the a test pte bit on random pages in the direct mapping,
5 * then reverts and compares page tables forwards and afterwards.
6 */
7 #include <linux/bootmem.h>
8 #include <linux/kthread.h>
9 #include <linux/random.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <linux/vmalloc.h>
15 #include <asm/cacheflush.h>
16 #include <asm/pgtable.h>
17 #include <asm/kdebug.h>
20 * Only print the results of the first pass:
22 static __read_mostly int print = 1;
24 enum {
25 NTEST = 400,
26 #ifdef CONFIG_X86_64
27 LPS = (1 << PMD_SHIFT),
28 #elif defined(CONFIG_X86_PAE)
29 LPS = (1 << PMD_SHIFT),
30 #else
31 LPS = (1 << 22),
32 #endif
33 GPS = (1<<30)
36 #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
38 static int pte_testbit(pte_t pte)
40 return pte_flags(pte) & _PAGE_SOFTW1;
43 struct split_state {
44 long lpg, gpg, spg, exec;
45 long min_exec, max_exec;
48 static int print_split(struct split_state *s)
50 long i, expected, missed = 0;
51 int err = 0;
53 s->lpg = s->gpg = s->spg = s->exec = 0;
54 s->min_exec = ~0UL;
55 s->max_exec = 0;
56 for (i = 0; i < max_pfn_mapped; ) {
57 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
58 unsigned int level;
59 pte_t *pte;
61 pte = lookup_address(addr, &level);
62 if (!pte) {
63 missed++;
64 i++;
65 continue;
68 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
69 s->gpg++;
70 i += GPS/PAGE_SIZE;
71 } else if (level == PG_LEVEL_2M) {
72 if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
73 printk(KERN_ERR
74 "%lx level %d but not PSE %Lx\n",
75 addr, level, (u64)pte_val(*pte));
76 err = 1;
78 s->lpg++;
79 i += LPS/PAGE_SIZE;
80 } else {
81 s->spg++;
82 i++;
84 if (!(pte_val(*pte) & _PAGE_NX)) {
85 s->exec++;
86 if (addr < s->min_exec)
87 s->min_exec = addr;
88 if (addr > s->max_exec)
89 s->max_exec = addr;
92 if (print) {
93 printk(KERN_INFO
94 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
95 s->spg, s->lpg, s->gpg, s->exec,
96 s->min_exec != ~0UL ? s->min_exec : 0,
97 s->max_exec, missed);
100 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
101 if (expected != i) {
102 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
103 max_pfn_mapped, expected);
104 return 1;
106 return err;
109 static unsigned long addr[NTEST];
110 static unsigned int len[NTEST];
112 /* Change the global bit on random pages in the direct mapping */
113 static int pageattr_test(void)
115 struct split_state sa, sb, sc;
116 unsigned long *bm;
117 pte_t *pte, pte0;
118 int failed = 0;
119 unsigned int level;
120 int i, k;
121 int err;
122 unsigned long test_addr;
124 if (print)
125 printk(KERN_INFO "CPA self-test:\n");
127 bm = vzalloc((max_pfn_mapped + 7) / 8);
128 if (!bm) {
129 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
130 return -ENOMEM;
133 failed += print_split(&sa);
135 for (i = 0; i < NTEST; i++) {
136 unsigned long pfn = prandom_u32() % max_pfn_mapped;
138 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
139 len[i] = prandom_u32() % 100;
140 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
142 if (len[i] == 0)
143 len[i] = 1;
145 pte = NULL;
146 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
148 for (k = 0; k < len[i]; k++) {
149 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
150 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
151 !(pte_val(*pte) & _PAGE_PRESENT)) {
152 addr[i] = 0;
153 break;
155 if (k == 0) {
156 pte0 = *pte;
157 } else {
158 if (pgprot_val(pte_pgprot(*pte)) !=
159 pgprot_val(pte_pgprot(pte0))) {
160 len[i] = k;
161 break;
164 if (test_bit(pfn + k, bm)) {
165 len[i] = k;
166 break;
168 __set_bit(pfn + k, bm);
170 if (!addr[i] || !pte || !k) {
171 addr[i] = 0;
172 continue;
175 test_addr = addr[i];
176 err = change_page_attr_set(&test_addr, len[i], PAGE_CPA_TEST, 0);
177 if (err < 0) {
178 printk(KERN_ERR "CPA %d failed %d\n", i, err);
179 failed++;
182 pte = lookup_address(addr[i], &level);
183 if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
184 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
185 pte ? (u64)pte_val(*pte) : 0ULL);
186 failed++;
188 if (level != PG_LEVEL_4K) {
189 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
190 addr[i], level);
191 failed++;
195 vfree(bm);
197 failed += print_split(&sb);
199 for (i = 0; i < NTEST; i++) {
200 if (!addr[i])
201 continue;
202 pte = lookup_address(addr[i], &level);
203 if (!pte) {
204 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
205 failed++;
206 continue;
208 test_addr = addr[i];
209 err = change_page_attr_clear(&test_addr, len[i], PAGE_CPA_TEST, 0);
210 if (err < 0) {
211 printk(KERN_ERR "CPA reverting failed: %d\n", err);
212 failed++;
214 pte = lookup_address(addr[i], &level);
215 if (!pte || pte_testbit(*pte)) {
216 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
217 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
218 failed++;
223 failed += print_split(&sc);
225 if (failed) {
226 WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
227 return -EINVAL;
228 } else {
229 if (print)
230 printk(KERN_INFO "ok.\n");
233 return 0;
236 static int do_pageattr_test(void *__unused)
238 while (!kthread_should_stop()) {
239 schedule_timeout_interruptible(HZ*30);
240 if (pageattr_test() < 0)
241 break;
242 if (print)
243 print--;
245 return 0;
248 static int start_pageattr_test(void)
250 struct task_struct *p;
252 p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
253 if (!IS_ERR(p))
254 wake_up_process(p);
255 else
256 WARN_ON(1);
258 return 0;
260 device_initcall(start_pageattr_test);