blob: a25588ad75efef495c4abb9778439da2f7e64a5d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andi Kleenfa2d8362008-01-30 13:33:43 +01002/*
3 * self test for change_page_attr.
4 *
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -07005 * Clears the a test pte bit on random pages in the direct mapping,
6 * then reverts and compares page tables forwards and afterwards.
Andi Kleenfa2d8362008-01-30 13:33:43 +01007 */
Ingo Molnar851339b2008-01-30 13:33:43 +01008#include <linux/bootmem.h>
Ingo Molnar971a52d2008-02-06 22:39:45 +01009#include <linux/kthread.h>
Andi Kleenfa2d8362008-01-30 13:33:43 +010010#include <linux/random.h>
11#include <linux/kernel.h>
Paul Gortmaker8f45fe42015-08-24 19:34:54 -040012#include <linux/init.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010013#include <linux/mm.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100014#include <linux/vmalloc.h>
Ingo Molnar851339b2008-01-30 13:33:43 +010015
Andi Kleenfa2d8362008-01-30 13:33:43 +010016#include <asm/cacheflush.h>
17#include <asm/pgtable.h>
18#include <asm/kdebug.h>
19
Ingo Molnar971a52d2008-02-06 22:39:45 +010020/*
21 * Only print the results of the first pass:
22 */
23static __read_mostly int print = 1;
24
Andi Kleenfa2d8362008-01-30 13:33:43 +010025enum {
Ingo Molnar971a52d2008-02-06 22:39:45 +010026 NTEST = 400,
Andi Kleenfa2d8362008-01-30 13:33:43 +010027#ifdef CONFIG_X86_64
Ingo Molnar851339b2008-01-30 13:33:43 +010028 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010029#elif defined(CONFIG_X86_PAE)
Ingo Molnar851339b2008-01-30 13:33:43 +010030 LPS = (1 << PMD_SHIFT),
Andi Kleenfa2d8362008-01-30 13:33:43 +010031#else
Ingo Molnar851339b2008-01-30 13:33:43 +010032 LPS = (1 << 22),
Andi Kleenfa2d8362008-01-30 13:33:43 +010033#endif
Ingo Molnar851339b2008-01-30 13:33:43 +010034 GPS = (1<<30)
Andi Kleenfa2d8362008-01-30 13:33:43 +010035};
36
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -070037#define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -070038
39static int pte_testbit(pte_t pte)
40{
Mel Gormanc46a7c82014-06-04 16:06:30 -070041 return pte_flags(pte) & _PAGE_SOFTW1;
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -070042}
43
Andi Kleenfa2d8362008-01-30 13:33:43 +010044struct split_state {
45 long lpg, gpg, spg, exec;
46 long min_exec, max_exec;
47};
48
Ingo Molnar971a52d2008-02-06 22:39:45 +010049static int print_split(struct split_state *s)
Andi Kleenfa2d8362008-01-30 13:33:43 +010050{
Andi Kleenfa2d8362008-01-30 13:33:43 +010051 long i, expected, missed = 0;
52 int err = 0;
53
54 s->lpg = s->gpg = s->spg = s->exec = 0;
55 s->min_exec = ~0UL;
56 s->max_exec = 0;
Thomas Gleixnerf87519e2008-01-30 13:34:05 +010057 for (i = 0; i < max_pfn_mapped; ) {
Ingo Molnar851339b2008-01-30 13:33:43 +010058 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
Harvey Harrison93809be2008-02-01 17:49:43 +010059 unsigned int level;
Andi Kleenfa2d8362008-01-30 13:33:43 +010060 pte_t *pte;
Andi Kleenfa2d8362008-01-30 13:33:43 +010061
Ingo Molnar851339b2008-01-30 13:33:43 +010062 pte = lookup_address(addr, &level);
Andi Kleenfa2d8362008-01-30 13:33:43 +010063 if (!pte) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010064 missed++;
65 i++;
66 continue;
67 }
68
Ingo Molnar86f03982008-01-30 13:34:09 +010069 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
Andi Kleenfa2d8362008-01-30 13:33:43 +010070 s->gpg++;
71 i += GPS/PAGE_SIZE;
Ingo Molnar86f03982008-01-30 13:34:09 +010072 } else if (level == PG_LEVEL_2M) {
Andrea Arcangeli18699732013-04-11 15:36:09 +020073 if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
Ingo Molnar851339b2008-01-30 13:33:43 +010074 printk(KERN_ERR
75 "%lx level %d but not PSE %Lx\n",
76 addr, level, (u64)pte_val(*pte));
Andi Kleenfa2d8362008-01-30 13:33:43 +010077 err = 1;
78 }
79 s->lpg++;
80 i += LPS/PAGE_SIZE;
81 } else {
82 s->spg++;
83 i++;
84 }
85 if (!(pte_val(*pte) & _PAGE_NX)) {
86 s->exec++;
Ingo Molnar851339b2008-01-30 13:33:43 +010087 if (addr < s->min_exec)
88 s->min_exec = addr;
89 if (addr > s->max_exec)
90 s->max_exec = addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +010091 }
92 }
Ingo Molnar971a52d2008-02-06 22:39:45 +010093 if (print) {
94 printk(KERN_INFO
95 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
96 s->spg, s->lpg, s->gpg, s->exec,
97 s->min_exec != ~0UL ? s->min_exec : 0,
98 s->max_exec, missed);
99 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100100
Andi Kleenfa2d8362008-01-30 13:33:43 +0100101 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
102 if (expected != i) {
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100103 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
104 max_pfn_mapped, expected);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100105 return 1;
106 }
107 return err;
108}
109
Ingo Molnar971a52d2008-02-06 22:39:45 +0100110static unsigned long addr[NTEST];
111static unsigned int len[NTEST];
Andi Kleenfa2d8362008-01-30 13:33:43 +0100112
113/* Change the global bit on random pages in the direct mapping */
Ingo Molnar971a52d2008-02-06 22:39:45 +0100114static int pageattr_test(void)
Andi Kleenfa2d8362008-01-30 13:33:43 +0100115{
Andi Kleenfa2d8362008-01-30 13:33:43 +0100116 struct split_state sa, sb, sc;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100117 unsigned long *bm;
Ingo Molnar851339b2008-01-30 13:33:43 +0100118 pte_t *pte, pte0;
119 int failed = 0;
Harvey Harrison93809be2008-02-01 17:49:43 +0100120 unsigned int level;
Ingo Molnar851339b2008-01-30 13:33:43 +0100121 int i, k;
122 int err;
Shaohua Liab7e7922008-08-21 10:46:21 +0800123 unsigned long test_addr;
Andi Kleenfa2d8362008-01-30 13:33:43 +0100124
Ingo Molnar971a52d2008-02-06 22:39:45 +0100125 if (print)
126 printk(KERN_INFO "CPA self-test:\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100127
Joe Perchesc4d017f2011-05-28 10:36:22 -0700128 bm = vzalloc((max_pfn_mapped + 7) / 8);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100129 if (!bm) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100130 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
Andi Kleenfa2d8362008-01-30 13:33:43 +0100131 return -ENOMEM;
132 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100133
134 failed += print_split(&sa);
Ingo Molnar851339b2008-01-30 13:33:43 +0100135
Andi Kleenfa2d8362008-01-30 13:33:43 +0100136 for (i = 0; i < NTEST; i++) {
Akinobu Mita56a1ba52013-04-29 16:21:27 -0700137 unsigned long pfn = prandom_u32() % max_pfn_mapped;
Ingo Molnar851339b2008-01-30 13:33:43 +0100138
Andi Kleenfa2d8362008-01-30 13:33:43 +0100139 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
Akinobu Mita56a1ba52013-04-29 16:21:27 -0700140 len[i] = prandom_u32() % 100;
Thomas Gleixnerf87519e2008-01-30 13:34:05 +0100141 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
Ingo Molnar851339b2008-01-30 13:33:43 +0100142
Andi Kleenfa2d8362008-01-30 13:33:43 +0100143 if (len[i] == 0)
144 len[i] = 1;
145
146 pte = NULL;
147 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
Ingo Molnar851339b2008-01-30 13:33:43 +0100148
Andi Kleenfa2d8362008-01-30 13:33:43 +0100149 for (k = 0; k < len[i]; k++) {
150 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
Thomas Gleixner64f351d2008-02-04 16:48:08 +0100151 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
152 !(pte_val(*pte) & _PAGE_PRESENT)) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100153 addr[i] = 0;
154 break;
155 }
Ingo Molnar851339b2008-01-30 13:33:43 +0100156 if (k == 0) {
Andi Kleenfa2d8362008-01-30 13:33:43 +0100157 pte0 = *pte;
Ingo Molnar851339b2008-01-30 13:33:43 +0100158 } else {
159 if (pgprot_val(pte_pgprot(*pte)) !=
Andi Kleenfa2d8362008-01-30 13:33:43 +0100160 pgprot_val(pte_pgprot(pte0))) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100161 len[i] = k;
162 break;
163 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100164 }
165 if (test_bit(pfn + k, bm)) {
166 len[i] = k;
167 break;
168 }
169 __set_bit(pfn + k, bm);
170 }
171 if (!addr[i] || !pte || !k) {
172 addr[i] = 0;
173 continue;
174 }
175
Shaohua Liab7e7922008-08-21 10:46:21 +0800176 test_addr = addr[i];
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -0700177 err = change_page_attr_set(&test_addr, len[i], PAGE_CPA_TEST, 0);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100178 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100179 printk(KERN_ERR "CPA %d failed %d\n", i, err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100180 failed++;
181 }
182
183 pte = lookup_address(addr[i], &level);
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -0700184 if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100185 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
Andi Kleenfa2d8362008-01-30 13:33:43 +0100186 pte ? (u64)pte_val(*pte) : 0ULL);
187 failed++;
188 }
Ingo Molnar86f03982008-01-30 13:34:09 +0100189 if (level != PG_LEVEL_4K) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100190 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
191 addr[i], level);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100192 failed++;
193 }
194
195 }
196 vfree(bm);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100197
198 failed += print_split(&sb);
199
Andi Kleenfa2d8362008-01-30 13:33:43 +0100200 for (i = 0; i < NTEST; i++) {
201 if (!addr[i])
202 continue;
203 pte = lookup_address(addr[i], &level);
204 if (!pte) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100205 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100206 failed++;
207 continue;
208 }
Shaohua Liab7e7922008-08-21 10:46:21 +0800209 test_addr = addr[i];
Jeremy Fitzhardinge110e0352008-08-28 13:58:39 -0700210 err = change_page_attr_clear(&test_addr, len[i], PAGE_CPA_TEST, 0);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100211 if (err < 0) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100212 printk(KERN_ERR "CPA reverting failed: %d\n", err);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100213 failed++;
214 }
215 pte = lookup_address(addr[i], &level);
Jeremy Fitzhardinge5a654ba2008-07-01 16:46:37 -0700216 if (!pte || pte_testbit(*pte)) {
Ingo Molnar851339b2008-01-30 13:33:43 +0100217 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
218 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100219 failed++;
220 }
221
222 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100223
224 failed += print_split(&sc);
Andi Kleenfa2d8362008-01-30 13:33:43 +0100225
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100226 if (failed) {
Arjan van de Ven875e40b2008-07-30 12:26:26 -0700227 WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
Ingo Molnar971a52d2008-02-06 22:39:45 +0100228 return -EINVAL;
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100229 } else {
Ingo Molnar971a52d2008-02-06 22:39:45 +0100230 if (print)
231 printk(KERN_INFO "ok.\n");
Ingo Molnar55ce29b2008-01-30 13:33:58 +0100232 }
Andi Kleenfa2d8362008-01-30 13:33:43 +0100233
234 return 0;
235}
Ingo Molnar971a52d2008-02-06 22:39:45 +0100236
237static int do_pageattr_test(void *__unused)
238{
239 while (!kthread_should_stop()) {
240 schedule_timeout_interruptible(HZ*30);
241 if (pageattr_test() < 0)
242 break;
243 if (print)
244 print--;
245 }
246 return 0;
247}
248
249static int start_pageattr_test(void)
250{
251 struct task_struct *p;
252
253 p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
254 if (!IS_ERR(p))
255 wake_up_process(p);
256 else
257 WARN_ON(1);
258
259 return 0;
260}
Paul Gortmaker8f45fe42015-08-24 19:34:54 -0400261device_initcall(start_pageattr_test);