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

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

18 19 20 21 22
/*
 * Only print the results of the first pass:
 */
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)
33 34
};

35 36 37 38 39 40 41
#define PAGE_TESTBIT	__pgprot(_PAGE_UNUSED1)

static int pte_testbit(pte_t pte)
{
	return pte_flags(pte) & _PAGE_UNUSED1;
}

42 43 44 45 46
struct split_state {
	long lpg, gpg, spg, exec;
	long min_exec, max_exec;
};

47
static int print_split(struct split_state *s)
48 49 50 51 52 53 54
{
	long i, expected, missed = 0;
	int err = 0;

	s->lpg = s->gpg = s->spg = s->exec = 0;
	s->min_exec = ~0UL;
	s->max_exec = 0;
T
Thomas Gleixner 已提交
55
	for (i = 0; i < max_pfn_mapped; ) {
56
		unsigned long addr = (unsigned long)__va(i << PAGE_SHIFT);
57
		unsigned int level;
58 59
		pte_t *pte;

60
		pte = lookup_address(addr, &level);
61 62 63 64 65 66
		if (!pte) {
			missed++;
			i++;
			continue;
		}

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

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

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

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

122 123
	if (print)
		printk(KERN_INFO "CPA self-test:\n");
124

T
Thomas Gleixner 已提交
125
	bm = vmalloc((max_pfn_mapped + 7) / 8);
126
	if (!bm) {
127
		printk(KERN_ERR "CPA Cannot vmalloc bitmap\n");
128 129
		return -ENOMEM;
	}
T
Thomas Gleixner 已提交
130
	memset(bm, 0, (max_pfn_mapped + 7) / 8);
131 132 133

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

135
	for (i = 0; i < NTEST; i++) {
T
Thomas Gleixner 已提交
136
		unsigned long pfn = random32() % max_pfn_mapped;
137

138 139
		addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
		len[i] = random32() % 100;
T
Thomas Gleixner 已提交
140
		len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
141

142 143 144 145 146
		if (len[i] == 0)
			len[i] = 1;

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

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

175
		err = change_page_attr_set(addr[i], len[i], PAGE_TESTBIT);
176
		if (err < 0) {
177
			printk(KERN_ERR "CPA %d failed %d\n", i, err);
178 179 180 181
			failed++;
		}

		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 185 186
				pte ? (u64)pte_val(*pte) : 0ULL);
			failed++;
		}
I
Ingo Molnar 已提交
187
		if (level != PG_LEVEL_4K) {
188 189
			printk(KERN_ERR "CPA %lx: unexpected level %d\n",
				addr[i], level);
190 191 192 193 194 195 196 197 198 199 200 201 202
			failed++;
		}

	}
	vfree(bm);

	failed += print_split(&sb);

	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;
		}
207
		err = change_page_attr_clear(addr[i], len[i], PAGE_TESTBIT);
208
		if (err < 0) {
209
			printk(KERN_ERR "CPA reverting failed: %d\n", err);
210 211 212
			failed++;
		}
		pte = lookup_address(addr[i], &level);
213
		if (!pte || pte_testbit(*pte)) {
214 215
			printk(KERN_ERR "CPA %lx: bad pte after revert %Lx\n",
				addr[i], pte ? (u64)pte_val(*pte) : 0ULL);
216 217 218 219 220 221 222
			failed++;
		}

	}

	failed += print_split(&sc);

I
Ingo Molnar 已提交
223
	if (failed) {
224
		WARN(1, KERN_ERR "NOT PASSED. Please report.\n");
225
		return -EINVAL;
I
Ingo Molnar 已提交
226
	} else {
227 228
		if (print)
			printk(KERN_INFO "ok.\n");
I
Ingo Molnar 已提交
229
	}
230 231 232

	return 0;
}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

static int do_pageattr_test(void *__unused)
{
	while (!kthread_should_stop()) {
		schedule_timeout_interruptible(HZ*30);
		if (pageattr_test() < 0)
			break;
		if (print)
			print--;
	}
	return 0;
}

static int start_pageattr_test(void)
{
	struct task_struct *p;

	p = kthread_create(do_pageattr_test, NULL, "pageattr-test");
	if (!IS_ERR(p))
		wake_up_process(p);
	else
		WARN_ON(1);

	return 0;
}

module_init(start_pageattr_test);