pageattr-test.c 5.0 KB
Newer Older
1 2 3 4 5 6
/*
 * self test for change_page_attr.
 *
 * Clears the global bit on random pages in the direct mapping, then reverts
 * and compares page tables forwards and afterwards.
 */
7
#include <linux/bootmem.h>
8 9 10
#include <linux/random.h>
#include <linux/kernel.h>
#include <linux/init.h>
11 12
#include <linux/mm.h>

13 14 15 16 17
#include <asm/cacheflush.h>
#include <asm/pgtable.h>
#include <asm/kdebug.h>

enum {
18
	NTEST			= 400,
T
Thomas Gleixner 已提交
19
	LOWEST_LEVEL		= PG_LEVEL_4K,
20
#ifdef CONFIG_X86_64
21
	LPS			= (1 << PMD_SHIFT),
22
#elif defined(CONFIG_X86_PAE)
23
	LPS			= (1 << PMD_SHIFT),
24
#else
25
	LPS			= (1 << 22),
26
#endif
27
	GPS			= (1<<30)
28 29 30
};

#ifdef CONFIG_X86_64
31 32
# include <asm/proto.h>
# define max_mapped		end_pfn_map
33
#else
34
# define max_mapped		max_low_pfn
35 36 37 38 39 40 41 42 43 44
#endif

struct split_state {
	long lpg, gpg, spg, exec;
	long min_exec, max_exec;
};

static __init int print_split(struct split_state *s)
{
	long i, expected, missed = 0;
45
	int printed = 0;
46 47 48 49 50 51
	int err = 0;

	s->lpg = s->gpg = s->spg = s->exec = 0;
	s->min_exec = ~0UL;
	s->max_exec = 0;
	for (i = 0; i < max_mapped; ) {
52
		unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
53 54 55
		int level;
		pte_t *pte;

56
		pte = lookup_address(addr, &level);
57 58
		if (!pte) {
			if (!printed) {
59 60 61
				dump_pagetable(addr);
				printk(KERN_INFO "CPA %lx no pte level %d\n",
					addr, level);
62 63 64 65 66 67 68 69 70 71 72 73
				printed = 1;
			}
			missed++;
			i++;
			continue;
		}

		if (level == 2 && sizeof(long) == 8) {
			s->gpg++;
			i += GPS/PAGE_SIZE;
		} else if (level != LOWEST_LEVEL) {
			if (!(pte_val(*pte) & _PAGE_PSE)) {
74 75 76
				printk(KERN_ERR
					"%lx level %d but not PSE %Lx\n",
					addr, level, (u64)pte_val(*pte));
77 78 79 80 81 82 83 84 85 86
				err = 1;
			}
			s->lpg++;
			i += LPS/PAGE_SIZE;
		} else {
			s->spg++;
			i++;
		}
		if (!(pte_val(*pte) & _PAGE_NX)) {
			s->exec++;
87 88 89 90
			if (addr < s->min_exec)
				s->min_exec = addr;
			if (addr > s->max_exec)
				s->max_exec = addr;
91 92
		}
	}
93 94
	printk(KERN_INFO
		"CPA mapping 4k %lu large %lu gb %lu x %lu[%lx-%lx] miss %lu\n",
95 96
		s->spg, s->lpg, s->gpg, s->exec,
		s->min_exec != ~0UL ? s->min_exec : 0, s->max_exec, missed);
97

98 99
	expected = (s->gpg*GPS + s->lpg*LPS)/PAGE_SIZE + s->spg + missed;
	if (expected != i) {
100
		printk(KERN_ERR "CPA max_mapped %lu but expected %lu\n",
101 102 103 104 105 106
			max_mapped, expected);
		return 1;
	}
	return err;
}

107 108
static unsigned long __initdata addr[NTEST];
static unsigned int __initdata len[NTEST];
109 110 111 112

/* Change the global bit on random pages in the direct mapping */
static __init int exercise_pageattr(void)
{
113 114
	struct split_state sa, sb, sc;
	unsigned long *bm;
115
	pte_t *pte, pte0;
116
	int failed = 0;
117
	int level;
118
	int i, k;
119 120
	int err;

121
	printk(KERN_INFO "CPA exercising pageattr\n");
122 123 124

	bm = vmalloc((max_mapped + 7) / 8);
	if (!bm) {
125
		printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
126 127 128 129 130 131
		return -ENOMEM;
	}
	memset(bm, 0, (max_mapped + 7) / 8);

	failed += print_split(&sa);
	srandom32(100);
132

133 134
	for (i = 0; i < NTEST; i++) {
		unsigned long pfn = random32() % max_mapped;
135

136 137 138
		addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
		len[i] = random32() % 100;
		len[i] = min_t(unsigned long, len[i], max_mapped - pfn - 1);
139

140 141 142 143 144
		if (len[i] == 0)
			len[i] = 1;

		pte = NULL;
		pte0 = pfn_pte(0, __pgprot(0)); /* shut gcc up */
145

146 147 148 149 150 151
		for (k = 0; k < len[i]; k++) {
			pte = lookup_address(addr[i] + k*PAGE_SIZE, &level);
			if (!pte || pgprot_val(pte_pgprot(*pte)) == 0) {
				addr[i] = 0;
				break;
			}
152
			if (k == 0) {
153
				pte0 = *pte;
154 155
			} else {
				if (pgprot_val(pte_pgprot(*pte)) !=
156
					pgprot_val(pte_pgprot(pte0))) {
157 158 159
					len[i] = k;
					break;
				}
160 161 162 163 164 165 166 167 168 169 170 171
			}
			if (test_bit(pfn + k, bm)) {
				len[i] = k;
				break;
			}
			__set_bit(pfn + k, bm);
		}
		if (!addr[i] || !pte || !k) {
			addr[i] = 0;
			continue;
		}

A
Andi Kleen 已提交
172
		err = change_page_attr_addr(addr[i], len[i],
173 174
			    pte_pgprot(pte_clrhuge(pte_clrglobal(pte0))));
		if (err < 0) {
175
			printk(KERN_ERR "CPA %d failed %d\n", i, err);
176 177 178 179 180
			failed++;
		}

		pte = lookup_address(addr[i], &level);
		if (!pte || pte_global(*pte) || pte_huge(*pte)) {
181
			printk(KERN_ERR "CPA %lx: bad pte %Lx\n", addr[i],
182 183 184 185
				pte ? (u64)pte_val(*pte) : 0ULL);
			failed++;
		}
		if (level != LOWEST_LEVEL) {
186 187
			printk(KERN_ERR "CPA %lx: unexpected level %d\n",
				addr[i], level);
188 189 190 191 192 193 194 195 196
			failed++;
		}

	}
	vfree(bm);
	global_flush_tlb();

	failed += print_split(&sb);

197
	printk(KERN_INFO "CPA reverting everything\n");
198 199 200 201 202
	for (i = 0; i < NTEST; i++) {
		if (!addr[i])
			continue;
		pte = lookup_address(addr[i], &level);
		if (!pte) {
203
			printk(KERN_ERR "CPA lookup of %lx failed\n", addr[i]);
204 205 206
			failed++;
			continue;
		}
A
Andi Kleen 已提交
207
		err = change_page_attr_addr(addr[i], len[i],
208 209
					  pte_pgprot(pte_mkglobal(*pte)));
		if (err < 0) {
210
			printk(KERN_ERR "CPA reverting failed: %d\n", err);
211 212 213 214
			failed++;
		}
		pte = lookup_address(addr[i], &level);
		if (!pte || !pte_global(*pte)) {
215 216
			printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
				addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
217 218 219 220 221 222 223 224
			failed++;
		}

	}
	global_flush_tlb();

	failed += print_split(&sc);

I
Ingo Molnar 已提交
225
	if (failed) {
226
		printk(KERN_ERR "CPA selftests NOT PASSED. Please report.\n");
I
Ingo Molnar 已提交
227 228
		WARN_ON(1);
	} else {
229
		printk(KERN_INFO "CPA selftests PASSED\n");
I
Ingo Molnar 已提交
230
	}
231 232 233 234

	return 0;
}
module_init(exercise_pageattr);