summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/mm/hugetlb_fault_after_madv.c
blob: 73b81c632366220c5d706fb506ce708bfb6a9d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-2.0
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

#include "vm_util.h"
#include "../kselftest.h"

#define MMAP_SIZE (1 << 21)
#define INLOOP_ITER 100

char *huge_ptr;

/* Touch the memory while it is being madvised() */
void *touch(void *unused)
{
	char *ptr = (char *)huge_ptr;

	for (int i = 0; i < INLOOP_ITER; i++)
		ptr[0] = '.';

	return NULL;
}

void *madv(void *unused)
{
	usleep(rand() % 10);

	for (int i = 0; i < INLOOP_ITER; i++)
		madvise(huge_ptr, MMAP_SIZE, MADV_DONTNEED);

	return NULL;
}

int main(void)
{
	unsigned long free_hugepages;
	pthread_t thread1, thread2;
	/*
	 * On kernel 6.4, we are able to reproduce the problem with ~1000
	 * interactions
	 */
	int max = 10000;

	srand(getpid());

	free_hugepages = get_free_hugepages();
	if (free_hugepages != 1) {
		ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
			       free_hugepages);
	}

	while (max--) {
		huge_ptr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE,
				MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
				-1, 0);

		if ((unsigned long)huge_ptr == -1)
			ksft_exit_skip("Failed to allocated huge page\n");

		pthread_create(&thread1, NULL, madv, NULL);
		pthread_create(&thread2, NULL, touch, NULL);

		pthread_join(thread1, NULL);
		pthread_join(thread2, NULL);
		munmap(huge_ptr, MMAP_SIZE);
	}

	return KSFT_PASS;
}