mm: revert "page-writeback.c: subtract min_free_kbytes from dirtyable memory"
[linux/fpc-iii.git] / arch / x86 / mm / pageattr-test.c
blobd0b1773d9d2efb1b24ff9ed0da0d2bbea999fc7e
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>
14 #include <asm/cacheflush.h>
15 #include <asm/pgtable.h>
16 #include <asm/kdebug.h>
19 * Only print the results of the first pass:
21 static __read_mostly int print = 1;
23 enum {
24 NTEST = 400,
25 #ifdef CONFIG_X86_64
26 LPS = (1 << PMD_SHIFT),
27 #elif defined(CONFIG_X86_PAE)
28 LPS = (1 << PMD_SHIFT),
29 #else
30 LPS = (1 << 22),
31 #endif
32 GPS = (1<<30)
35 #define PAGE_CPA_TEST __pgprot(_PAGE_CPA_TEST)
37 static int pte_testbit(pte_t pte)
39 return pte_flags(pte) & _PAGE_UNUSED1;
42 struct split_state {
43 long lpg, gpg, spg, exec;
44 long min_exec, max_exec;
47 static int print_split(struct split_state *s)
49 long i, expected, missed = 0;
50 int err = 0;
52 s->lpg = s->gpg = s->spg = s->exec = 0;
53 s->min_exec = ~0UL;
54 s->max_exec = 0;
55 for (i = 0; i < max_pfn_mapped; ) {
56 unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
57 unsigned int level;
58 pte_t *pte;
60 pte = lookup_address(addr, &level);
61 if (!pte) {
62 missed++;
63 i++;
64 continue;
67 if (level == PG_LEVEL_1G && sizeof(long) == 8) {
68 s->gpg++;
69 i += GPS/PAGE_SIZE;
70 } else if (level == PG_LEVEL_2M) {
71 if ((pte_val(*pte) & _PAGE_PRESENT) && !(pte_val(*pte) & _PAGE_PSE)) {
72 printk(KERN_ERR
73 "%lx level %d but not PSE %Lx\n",
74 addr, level, (u64)pte_val(*pte));
75 err = 1;
77 s->lpg++;
78 i += LPS/PAGE_SIZE;
79 } else {
80 s->spg++;
81 i++;
83 if (!(pte_val(*pte) & _PAGE_NX)) {
84 s->exec++;
85 if (addr < s->min_exec)
86 s->min_exec = addr;
87 if (addr > s->max_exec)
88 s->max_exec = addr;
91 if (print) {
92 printk(KERN_INFO
93 " 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
94 s->spg, s->lpg, s->gpg, s->exec,
95 s->min_exec != ~0UL ? s->min_exec : 0,
96 s->max_exec, missed);
99 expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
100 if (expected != i) {
101 printk(KERN_ERR "CPA max_pfn_mapped %lu but expected %lu\n",
102 max_pfn_mapped, expected);
103 return 1;
105 return err;
108 static unsigned long addr[NTEST];
109 static unsigned int len[NTEST];
111 /* Change the global bit on random pages in the direct mapping */
112 static int pageattr_test(void)
114 struct split_state sa, sb, sc;
115 unsigned long *bm;
116 pte_t *pte, pte0;
117 int failed = 0;
118 unsigned int level;
119 int i, k;
120 int err;
121 unsigned long test_addr;
123 if (print)
124 printk(KERN_INFO "CPA self-test:\n");
126 bm = vzalloc((max_pfn_mapped + 7) / 8);
127 if (!bm) {
128 printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
129 return -ENOMEM;
132 failed += print_split(&sa);
134 for (i = 0; i < NTEST; i++) {
135 unsigned long pfn = prandom_u32() % max_pfn_mapped;
137 addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
138 len[i] = prandom_u32() % 100;
139 len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
141 if (len[i] == 0)
142 len[i] = 1;
144 pte = NULL;
145 pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
147 for (k = 0; k < len[i]; k++) {
148 pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
149 if (!pte || pgprot_val(pte_pgprot(*pte)) == 0 ||
150 !(pte_val(*pte) & _PAGE_PRESENT)) {
151 addr[i] = 0;
152 break;
154 if (k == 0) {
155 pte0 = *pte;
156 } else {
157 if (pgprot_val(pte_pgprot(*pte)) !=
158 pgprot_val(pte_pgprot(pte0))) {
159 len[i] = k;
160 break;
163 if (test_bit(pfn + k, bm)) {
164 len[i] = k;
165 break;
167 __set_bit(pfn + k, bm);
169 if (!addr[i] || !pte || !k) {
170 addr[i] = 0;
171 continue;
174 test_addr = addr[i];
175 err = change_page_attr_set(&test_addr, len[i], PAGE_CPA_TEST, 0);
176 if (err < 0) {
177 printk(KERN_ERR "CPA %d failed %d\n", i, err);
178 failed++;
181 pte = lookup_address(addr[i], &level);
182 if (!pte || !pte_testbit(*pte) || pte_huge(*pte)) {
183 printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
184 pte ? (u64)pte_val(*pte) : 0ULL);
185 failed++;
187 if (level != PG_LEVEL_4K) {
188 printk(KERN_ERR "CPA %lx: unexpected level %d\n",
189 addr[i], level);
190 failed++;
194 vfree(bm);
196 failed += print_split(&sb);
198 for (i = 0; i < NTEST; i++) {
199 if (!addr[i])
200 continue;
201 pte = lookup_address(addr[i], &level);
202 if (!pte) {
203 printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
204 failed++;
205 continue;
207 test_addr = addr[i];
208 err = change_page_attr_clear(&test_addr, len[i], PAGE_CPA_TEST, 0);
209 if (err < 0) {
210 printk(KERN_ERR "CPA reverting failed: %d\n", err);
211 failed++;
213 pte = lookup_address(addr[i], &level);
214 if (!pte || pte_testbit(*pte)) {
215 printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
216 addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
217 failed++;
222 failed += print_split(&sc);
224 if (failed) {
225 WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
226 return -EINVAL;
227 } else {
228 if (print)
229 printk(KERN_INFO "ok.\n");
232 return 0;
235 static int do_pageattr_test(void *__unused)
237 while (!kthread_should_stop()) {
238 schedule_timeout_interruptible(HZ*30);
239 if (pageattr_test() < 0)
240 break;
241 if (print)
242 print--;
244 return 0;
247 static int start_pageattr_test(void)
249 struct task_struct *p;
251 p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
252 if (!IS_ERR(p))
253 wake_up_process(p);
254 else
255 WARN_ON(1);
257 return 0;
260 module_init(start_pageattr_test);