1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * self test for change_page_attr.
4 *
5 * Clears the a test pte bit on random pages in the direct mapping,
6 * then reverts and compares page tables forwards and afterwards.
7 */
8 #include <linux/memblock.h>
9 #include <linux/kthread.h>
10 #include <linux/random.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/vmalloc.h>
15
16 #include <asm/cacheflush.h>
17 #include <asm/pgtable.h>
18 #include <asm/kdebug.h>
19
20 /*
21 * Only print the results of the first pass:
22 */
23 static __read_mostly int print = 1;
24
25 enum {
26 NTEST = 3 * 100,
27 NPAGES = 100,
28 #ifdef CONFIG_X86_64
29 LPS = (1 << PMD_SHIFT),
30 #elif defined(CONFIG_X86_PAE)
31 LPS = (1 << PMD_SHIFT),
32 #else
33 LPS = (1 << 22),
34 #endif
35 GPS = (1<<30)
36 };
37
38 #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
39
pte_testbit(pte_t pte)40 static int pte_testbit(pte_t pte)
41 {
42 return pte_flags(pte) & _PAGE_SOFTW1;
43 }
44
45 struct split_state {
46 long lpg, gpg, spg, exec;
47 long min_exec, max_exec;
48 };
49
print_split(struct split_state * s)50 static int print_split(struct split_state *s)
51 {
52 long i, expected, missed = 0;
53 int err = 0;
54
55 s->lpg = s->gpg = s->spg = s->exec = 0;
56 s->min_exec = ~0UL;
57 s->max_exec = 0;
58 for (i = 0; i < max_pfn_mapped; ) {
59 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
60 unsigned int level;
61 pte_t *pte;
62
63 pte = lookup_address(addr, &level);
64 if (!pte) {
65 missed++;
66 i++;
67 continue;
68 }
69
70 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
71 s->gpg++;
72 i += GPS/PAGE_SIZE;
73 } else if (level == PG_LEVEL_2M) {
74 if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
75 printk(KERN_ERR
76 "%lx level %d but not PSE %Lx\n",
77 addr, level, (u64)pte_val(*pte));
78 err = 1;
79 }
80 s->lpg++;
81 i += LPS/PAGE_SIZE;
82 } else {
83 s->spg++;
84 i++;
85 }
86 if (!(pte_val(*pte) & _PAGE_NX)) {
87 s->exec++;
88 if (addr < s->min_exec)
89 s->min_exec = addr;
90 if (addr > s->max_exec)
91 s->max_exec = addr;
92 }
93 }
94 if (print) {
95 printk(KERN_INFO
96 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
97 s->spg, s->lpg, s->gpg, s->exec,
98 s->min_exec != ~0UL ? s->min_exec : 0,
99 s->max_exec, missed);
100 }
101
102 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
103 if (expected != i) {
104 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
105 max_pfn_mapped, expected);
106 return 1;
107 }
108 return err;
109 }
110
111 static unsigned long addr[NTEST];
112 static unsigned int len[NTEST];
113
114 static struct page *pages[NPAGES];
115 static unsigned long addrs[NPAGES];
116
117 /* Change the global bit on random pages in the direct mapping */
pageattr_test(void)118 static int pageattr_test(void)
119 {
120 struct split_state sa, sb, sc;
121 unsigned long *bm;
122 pte_t *pte, pte0;
123 int failed = 0;
124 unsigned int level;
125 int i, k;
126 int err;
127
128 if (print)
129 printk(KERN_INFO "CPA self-test:\n");
130
131 bm = vzalloc((max_pfn_mapped + 7) / 8);
132 if (!bm) {
133 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
134 return -ENOMEM;
135 }
136
137 failed += print_split(&sa);
138
139 for (i = 0; i < NTEST; i++) {
140 unsigned long pfn = prandom_u32() % max_pfn_mapped;
141
142 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
143 len[i] = prandom_u32() % NPAGES;
144 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
145
146 if (len[i] == 0)
147 len[i] = 1;
148
149 pte = NULL;
150 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
151
152 for (k = 0; k < len[i]; k++) {
153 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
154 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
155 !(pte_val(*pte) & _PAGE_PRESENT)) {
156 addr[i] = 0;
157 break;
158 }
159 if (k == 0) {
160 pte0 = *pte;
161 } else {
162 if (pgprot_val(pte_pgprot(*pte)) !=
163 pgprot_val(pte_pgprot(pte0))) {
164 len[i] = k;
165 break;
166 }
167 }
168 if (test_bit(pfn + k, bm)) {
169 len[i] = k;
170 break;
171 }
172 __set_bit(pfn + k, bm);
173 addrs[k] = addr[i] + k*PAGE_SIZE;
174 pages[k] = pfn_to_page(pfn + k);
175 }
176 if (!addr[i] || !pte || !k) {
177 addr[i] = 0;
178 continue;
179 }
180
181 switch (i % 3) {
182 case 0:
183 err = change_page_attr_set(&addr[i], len[i], PAGE_CPA_TEST, 0);
184 break;
185
186 case 1:
187 err = change_page_attr_set(addrs, len[1], PAGE_CPA_TEST, 1);
188 break;
189
190 case 2:
191 err = cpa_set_pages_array(pages, len[i], PAGE_CPA_TEST);
192 break;
193 }
194
195
196 if (err < 0) {
197 printk(KERN_ERR "CPA %d failed %d\n", i, err);
198 failed++;
199 }
200
201 pte = lookup_address(addr[i], &level);
202 if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
203 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
204 pte ? (u64)pte_val(*pte) : 0ULL);
205 failed++;
206 }
207 if (level != PG_LEVEL_4K) {
208 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
209 addr[i], level);
210 failed++;
211 }
212
213 }
214 vfree(bm);
215
216 failed += print_split(&sb);
217
218 for (i = 0; i < NTEST; i++) {
219 if (!addr[i])
220 continue;
221 pte = lookup_address(addr[i], &level);
222 if (!pte) {
223 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
224 failed++;
225 continue;
226 }
227 err = change_page_attr_clear(&addr[i], len[i], PAGE_CPA_TEST, 0);
228 if (err < 0) {
229 printk(KERN_ERR "CPA reverting failed: %d\n", err);
230 failed++;
231 }
232 pte = lookup_address(addr[i], &level);
233 if (!pte || pte_testbit(*pte)) {
234 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
235 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
236 failed++;
237 }
238
239 }
240
241 failed += print_split(&sc);
242
243 if (failed) {
244 WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
245 return -EINVAL;
246 } else {
247 if (print)
248 printk(KERN_INFO "ok.\n");
249 }
250
251 return 0;
252 }
253
do_pageattr_test(void * __unused)254 static int do_pageattr_test(void *__unused)
255 {
256 while (!kthread_should_stop()) {
257 schedule_timeout_interruptible(HZ*30);
258 if (pageattr_test() < 0)
259 break;
260 if (print)
261 print--;
262 }
263 return 0;
264 }
265
start_pageattr_test(void)266 static int start_pageattr_test(void)
267 {
268 struct task_struct *p;
269
270 p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
271 if (!IS_ERR(p))
272 wake_up_process(p);
273 else
274 WARN_ON(1);
275
276 return 0;
277 }
278 device_initcall(start_pageattr_test);
279