summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/mm/hugetlb_madv_vs_map.c
blob: 1d111f42dd59639f16dd688904be0e04b76750b4 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: GPL-2.0
/*
 * A test case that must run on a system with one and only one huge page available.
 *	# echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
 *
 *  Author: Breno Leitao <leitao@debian.org>
 */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "vm_util.h"

#define INLOOP_ITER 100

size_t mmap_size;
char *huge_ptr;

/* Touch the memory while it is being madvised() */
void *touch(void *unused)
{
	for (int i = 0; i < INLOOP_ITER; i++)
		huge_ptr[0] = '.';

	return NULL;
}

void *madv(void *unused)
{
	for (int i = 0; i < INLOOP_ITER; i++)
		madvise(huge_ptr, mmap_size, MADV_DONTNEED);

	return NULL;
}

/*
 * We got here, and there must be no huge page available for mapping
 * The other hugepage should be flipping from used <-> reserved, because
 * of madvise(DONTNEED).
 */
void *map_extra(void *unused)
{
	void *ptr;

	for (int i = 0; i < INLOOP_ITER; i++) {
		ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
			   -1, 0);

		if ((long)ptr != -1) {
			/* Touching the other page now will cause a SIGBUG
			 * huge_ptr[0] = '1';
			 */
			return ptr;
		}
	}

	return NULL;
}

/* During setup in main, the only available page was allocated. This test then
 * starts three threads:
 *
 * - thread1:
 *	* madvise(MADV_DONTNEED) on the allocated huge page
 *  - thread 2:
 *	* Write to the allocated huge page
 *  - thread 3:
 *	* Try to allocated an extra huge page (which must not available)
 *
 *  The test fails if thread3 is able to allocate a page.
 *
 *  Touching the first page after thread3's allocation will raise a SIGBUS
 */
void test_madv_vs_map(void)
{
	pthread_t thread1, thread2, thread3;
	void *ret;

	/*
	 * On kernel 6.7, we are able to reproduce the problem with ~10
	 * interactions
	 */
	int max = 10;

	mmap_size = default_huge_page_size();

	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_fail_perror("Failed to allocate huge page");

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

		pthread_join(thread1, NULL);
		pthread_join(thread2, NULL);
		pthread_join(thread3, &ret);

		if (ret) {
			ksft_test_result_fail("Unexpected huge page allocation\n");
			ksft_finished();
		}

		/* Unmap and restart */
		munmap(huge_ptr, mmap_size);
	}

	ksft_test_result_pass("No unexpected huge page allocations\n");
}

/*  We create a child process, then unmap the page in the parent while the child
 *  waits and verify that there is no underflow of the reserved count. We also
 *  verify that after the child exits, the reserved count is properly restored.
 */
void test_underflow(void)
{
	pid_t pid;
	int pipe_fds[2];
	unsigned long nr_reserved = 0;

	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_fail_perror("Failed to allocate huge page");

	nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
	if (nr_reserved != 1)
		ksft_exit_fail_msg("Unexpected number of reserved pages: %lu, expected 1\n",
				   nr_reserved);

	/* Force the fault to ensure the reservation is consumed */
	*huge_ptr = 0;
	nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
	if (nr_reserved != 0)
		ksft_exit_fail_msg("Unexpected number of reserved pages: %lu, expected 0\n",
				   nr_reserved);

	if (pipe(pipe_fds) != 0)
		ksft_exit_fail_perror("pipe failed");

	pid = fork();
	if (pid < 0)
		ksft_exit_fail_perror("fork failed");

	if (pid == 0) {
		/* Child: Simply wait for the parent */
		char b;

		close(pipe_fds[1]);
		if (read(pipe_fds[0], &b, 1) < 0)
			ksft_perror("child read failed");
		/* Let the parent do the cleanup */
		_exit(0);
	}

	/* Parent */
	close(pipe_fds[0]);

	/* First unmap, this will close the vma */
	if (munmap(huge_ptr, mmap_size) != 0) {
		ksft_perror("munmap failed");
		goto err_cleanup;
	}

	nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
	if (nr_reserved == 0) {
		ksft_test_result_pass("Underflow not present!\n");
	} else {
		ksft_test_result_fail("Unexpected HugePages_Rsvd=%ld after munmap, should be 0\n",
				      nr_reserved);
		goto err_cleanup;
	}
	/* Make the child exit, this should restore HugePages_Rsvd to 0 */
	if (write(pipe_fds[1], &nr_reserved, 1) < 0) {
		/* If write failed, the child is likely already gone */
		ksft_exit_fail_perror("write failed");
	}
	close(pipe_fds[1]);
	if (waitpid(pid, NULL, 0) <= 0)
		ksft_exit_fail_msg("waitpid failed\n");

	nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
	if (nr_reserved == 0)
		ksft_test_result_pass("After the child dies, HugePages_Rsvd is properly set to 0\n");
	else
		ksft_exit_fail_msg("Unexpected HugePages_Rsvd=%ld after the child termination munmap, should be 0\n", nr_reserved);

	return;

err_cleanup:
	if (write(pipe_fds[1], &nr_reserved, 1) < 0)
		ksft_exit_fail_perror("write failed");
	if (waitpid(pid, NULL, 0) <= 0)
		ksft_exit_fail_perror("waitpid failed");

	ksft_exit_fail();
}

int main(void)
{
	ksft_print_header();
	ksft_set_plan(3);

	if (!hugetlb_setup_default_exact(1))
		ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
			       hugetlb_free_default_pages());

	test_madv_vs_map();
	test_underflow();

	ksft_finished();
}