blob: 5f169d5d76a889cf1c489f6f1c8a0f7a40c10c4a [file] [log] [blame]
Andi Kleenfa2d8362008-01-30 13:33:43 +01001/*
2 * self test for change_page_attr.
3 *
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -07004 * Clears the a test pte bit on random pages in the direct mapping,
5 * then reverts and compares page tables forwards and afterwards.
Andi Kleenfa2d8362008-01-30 13:33:43 +01006 */
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>
Paul Gortmaker8f45fe42015-08-24 19:34:54 -040011#include <linux/init.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010012#include <linux/mm.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100013#include <linux/vmalloc.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010014
Andi Kleenfa2d8362008-01-30 13:33:43 +010015#include <asm/cacheflush.h>
16#include <asm/pgtable.h>
17#include <asm/kdebug.h>
18
Ingo Molnar971a52d2008-02-06 22:39:45 +010019/*
20 * Only print the results of the first pass:
21 */
22static __read_mostly int print = 1;
23
Andi Kleenfa2d8362008-01-30 13:33:43 +010024enum {
Ingo Molnar971a52d2008-02-06 22:39:45 +010025 NTEST = 400,
Andi Kleenfa2d8362008-01-30 13:33:43 +010026#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010027 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010028#elif defined(CONFIG_X86_PAE)
Ingo Molnar851339b2008-01-30 13:33:43 +010029 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010030#else
Ingo Molnar851339b2008-01-30 13:33:43 +010031 LPS = (1 << 22),
Andi Kleenfa2d8362008-01-30 13:33:43 +010032#endif
Ingo Molnar851339b2008-01-30 13:33:43 +010033 GPS = (1<<30)
Andi Kleenfa2d8362008-01-30 13:33:43 +010034};
35
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -070036#define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -070037
38static int pte_testbit(pte_t pte)
39{
Mel Gormanc46a7c82014-06-04 16:06:30 -070040 return pte_flags(pte) & _PAGE_SOFTW1;
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -070041}
42
Andi Kleenfa2d8362008-01-30 13:33:43 +010043struct split_state {
44 long lpg, gpg, spg, exec;
45 long min_exec, max_exec;
46};
47
Ingo Molnar971a52d2008-02-06 22:39:45 +010048static int print_split(struct split_state *s)
Andi Kleenfa2d8362008-01-30 13:33:43 +010049{
Andi Kleenfa2d8362008-01-30 13:33:43 +010050 long i, expected, missed = 0;
51 int err = 0;
52
53 s->lpg = s->gpg = s->spg = s->exec = 0;
54 s->min_exec = ~0UL;
55 s->max_exec = 0;
Thomas Gleixnerf87519e2008-01-30 13:34:05 +010056 for (i = 0; i < max_pfn_mapped; ) {
Ingo Molnar851339b2008-01-30 13:33:43 +010057 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
Harvey Harrison93809be2008-02-01 17:49:43 +010058 unsigned int level;
Andi Kleenfa2d8362008-01-30 13:33:43 +010059 pte_t *pte;
Andi Kleenfa2d8362008-01-30 13:33:43 +010060
Ingo Molnar851339b2008-01-30 13:33:43 +010061 pte = lookup_address(addr, &level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010062 if (!pte) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010063 missed++;
64 i++;
65 continue;
66 }
67
Ingo Molnar86f03982008-01-30 13:34:09 +010068 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010069 s->gpg++;
70 i += GPS/PAGE_SIZE;
Ingo Molnar86f03982008-01-30 13:34:09 +010071 } else if (level == PG_LEVEL_2M) {
Andrea Arcangeli18699732013-04-11 15:36:09 +020072 if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
Ingo Molnar851339b2008-01-30 13:33:43 +010073 printk(KERN_ERR
74 "%lx level %d but not PSE %Lx\n",
75 addr, level, (u64)pte_val(*pte));
Andi Kleenfa2d8362008-01-30 13:33:43 +010076 err = 1;
77 }
78 s->lpg++;
79 i += LPS/PAGE_SIZE;
80 } else {
81 s->spg++;
82 i++;
83 }
84 if (!(pte_val(*pte) & _PAGE_NX)) {
85 s->exec++;
Ingo Molnar851339b2008-01-30 13:33:43 +010086 if (addr < s->min_exec)
87 s->min_exec = addr;
88 if (addr > s->max_exec)
89 s->max_exec = addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +010090 }
91 }
Ingo Molnar971a52d2008-02-06 22:39:45 +010092 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);
98 }
Ingo Molnar851339b2008-01-30 13:33:43 +010099
Andi Kleenfa2d8362008-01-30 13:33:43 +0100100 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
101 if (expected != i) {
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100102 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
103 max_pfn_mapped, expected);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100104 return 1;
105 }
106 return err;
107}
108
Ingo Molnar971a52d2008-02-06 22:39:45 +0100109static unsigned long addr[NTEST];
110static unsigned int len[NTEST];
Andi Kleenfa2d8362008-01-30 13:33:43 +0100111
112/* Change the global bit on random pages in the direct mapping */
Ingo Molnar971a52d2008-02-06 22:39:45 +0100113static int pageattr_test(void)
Andi Kleenfa2d8362008-01-30 13:33:43 +0100114{
Andi Kleenfa2d8362008-01-30 13:33:43 +0100115 struct split_state sa, sb, sc;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100116 unsigned long *bm;
Ingo Molnar851339b2008-01-30 13:33:43 +0100117 pte_t *pte, pte0;
118 int failed = 0;
Harvey Harrison93809be2008-02-01 17:49:43 +0100119 unsigned int level;
Ingo Molnar851339b2008-01-30 13:33:43 +0100120 int i, k;
121 int err;
Shaohua Liab7e7922008-08-21 10:46:21 +0800122 unsigned long test_addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100123
Ingo Molnar971a52d2008-02-06 22:39:45 +0100124 if (print)
125 printk(KERN_INFO "CPA self-test:\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100126
Joe Perchesc4d017f2011-05-28 10:36:22 -0700127 bm = vzalloc((max_pfn_mapped + 7) / 8);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100128 if (!bm) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100129 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100130 return -ENOMEM;
131 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100132
133 failed += print_split(&sa);
Ingo Molnar851339b2008-01-30 13:33:43 +0100134
Andi Kleenfa2d8362008-01-30 13:33:43 +0100135 for (i = 0; i < NTEST; i++) {
Akinobu Mita56a1ba52013-04-29 16:21:27 -0700136 unsigned long pfn = prandom_u32() % max_pfn_mapped;
Ingo Molnar851339b2008-01-30 13:33:43 +0100137
Andi Kleenfa2d8362008-01-30 13:33:43 +0100138 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
Akinobu Mita56a1ba52013-04-29 16:21:27 -0700139 len[i] = prandom_u32() % 100;
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100140 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
Ingo Molnar851339b2008-01-30 13:33:43 +0100141
Andi Kleenfa2d8362008-01-30 13:33:43 +0100142 if (len[i] == 0)
143 len[i] = 1;
144
145 pte = NULL;
146 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
Ingo Molnar851339b2008-01-30 13:33:43 +0100147
Andi Kleenfa2d8362008-01-30 13:33:43 +0100148 for (k = 0; k < len[i]; k++) {
149 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
Thomas Gleixner64f351d2008-02-04 16:48:08 +0100150 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
151 !(pte_val(*pte) & _PAGE_PRESENT)) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100152 addr[i] = 0;
153 break;
154 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100155 if (k == 0) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100156 pte0 = *pte;
Ingo Molnar851339b2008-01-30 13:33:43 +0100157 } else {
158 if (pgprot_val(pte_pgprot(*pte)) !=
Andi Kleenfa2d8362008-01-30 13:33:43 +0100159 pgprot_val(pte_pgprot(pte0))) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100160 len[i] = k;
161 break;
162 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100163 }
164 if (test_bit(pfn + k, bm)) {
165 len[i] = k;
166 break;
167 }
168 __set_bit(pfn + k, bm);
169 }
170 if (!addr[i] || !pte || !k) {
171 addr[i] = 0;
172 continue;
173 }
174
Shaohua Liab7e7922008-08-21 10:46:21 +0800175 test_addr = addr[i];
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -0700176 err = change_page_attr_set(&test_addr, len[i], PAGE_CPA_TEST, 0);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100177 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100178 printk(KERN_ERR "CPA %d failed %d\n", i, err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100179 failed++;
180 }
181
182 pte = lookup_address(addr[i], &level);
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -0700183 if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100184 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100185 pte ? (u64)pte_val(*pte) : 0ULL);
186 failed++;
187 }
Ingo Molnar86f03982008-01-30 13:34:09 +0100188 if (level != PG_LEVEL_4K) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100189 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
190 addr[i], level);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100191 failed++;
192 }
193
194 }
195 vfree(bm);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100196
197 failed += print_split(&sb);
198
Andi Kleenfa2d8362008-01-30 13:33:43 +0100199 for (i = 0; i < NTEST; i++) {
200 if (!addr[i])
201 continue;
202 pte = lookup_address(addr[i], &level);
203 if (!pte) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100204 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100205 failed++;
206 continue;
207 }
Shaohua Liab7e7922008-08-21 10:46:21 +0800208 test_addr = addr[i];
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -0700209 err = change_page_attr_clear(&test_addr, len[i], PAGE_CPA_TEST, 0);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100210 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100211 printk(KERN_ERR "CPA reverting failed: %d\n", err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100212 failed++;
213 }
214 pte = lookup_address(addr[i], &level);
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -0700215 if (!pte || pte_testbit(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100216 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
217 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100218 failed++;
219 }
220
221 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100222
223 failed += print_split(&sc);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100224
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100225 if (failed) {
Arjan van de Ven875e40b2008-07-30 12:26:26 -0700226 WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
Ingo Molnar971a52d2008-02-06 22:39:45 +0100227 return -EINVAL;
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100228 } else {
Ingo Molnar971a52d2008-02-06 22:39:45 +0100229 if (print)
230 printk(KERN_INFO "ok.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100231 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100232
233 return 0;
234}
Ingo Molnar971a52d2008-02-06 22:39:45 +0100235
236static int do_pageattr_test(void *__unused)
237{
238 while (!kthread_should_stop()) {
239 schedule_timeout_interruptible(HZ*30);
240 if (pageattr_test() < 0)
241 break;
242 if (print)
243 print--;
244 }
245 return 0;
246}
247
248static int start_pageattr_test(void)
249{
250 struct task_struct *p;
251
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);
257
258 return 0;
259}
Paul Gortmaker8f45fe42015-08-24 19:34:54 -0400260device_initcall(start_pageattr_test);