Debian Codegraph @

tests/alloc-instantiate-race.c

raw symbols SCIP
1 /*
2 * Test rationale:
3 *
4 * This test is designed to detect a kernel allocation race introduced
5 * with hugepage demand-faulting. The problem is that no lock is held
6 * between allocating a hugepage and instantiating it in the
7 * pagetables or page cache index. In between the two, the (huge)
8 * page is cleared, so there's substantial time. Thus two processes
9 * can race instantiating the (same) last available hugepage - one
10 * will fail on the allocation, and thus cause an OOM fault even
11 * though the page it actually wants is being instantiated by the
12 * other racing process.
13 *
14 *
15 * libhugetlbfs - Easy use of Linux hugepages
16 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public License
20 * as published by the Free Software Foundation; either version 2.1 of
21 * the License, or (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with this library; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 */
32
33 #define _GNU_SOURCE
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #include <sys/types.h>
42 #include <sched.h>
43 #include <signal.h>
44 #include <sys/wait.h>
45 #include <pthread.h>
46 #include <linux/unistd.h>
47
48 #include <hugetlbfs.h>
49
50 #include "hugetests.h"
51
52 pid_t gettid(void)
53 {
54 return syscall(__NR_gettid);
55 }
56
57 static long hpage_size;
58 static pid_t child1, child2;
59 static pthread_t thread1, thread2;
60
61 void cleanup(void)
62 {
63 if (child1)
64 kill(child1, SIGKILL);
65 if (child2)
66 kill(child2, SIGKILL);
67 }
68
69 static int one_racer(void *p, int cpu,
70 volatile int *mytrigger, volatile int *othertrigger)
71 {
72 volatile int *pi = p;
73 cpu_set_t cpuset;
74 int err;
75
76 /* Split onto different cpus to encourage the race */
77 CPU_ZERO(&cpuset);
78 CPU_SET(cpu, &cpuset);
79
80 err = sched_setaffinity(gettid(), CPU_SETSIZE/8, &cpuset);
81 if (err != 0)
82 CONFIG("sched_setaffinity(cpu%d): %s", cpu, strerror(errno));
83
84 /* Ready.. */
85 *mytrigger = 1;
86 /* Set.. */
87 while (! *othertrigger)
88 ;
89
90 /* Instantiate! */
91 *pi = 1;
92
93 return 0;
94 }
95
96 static void proc_racer(void *p, int cpu,
97 volatile int *mytrigger, volatile int *othertrigger)
98 {
99 exit(one_racer(p, cpu, mytrigger, othertrigger));
100 }
101
102 struct racer_info {
103 void *p; /* instantiation address */
104 int cpu;
105 int race_type;
106 volatile int *mytrigger;
107 volatile int *othertrigger;
108 int status;
109 };
110
111 static void *thread_racer(void *info)
112 {
113 struct racer_info *ri = info;
114
115 one_racer(ri->p, ri->cpu, ri->mytrigger, ri->othertrigger);
116 return ri;
117 }
118 static void run_race(void *syncarea, int race_type)
119 {
120 volatile int *trigger1, *trigger2;
121 int fd;
122 void *p;
123 int status1, status2;
124 int online_cpus[2], ret;
125
126 check_online_cpus(online_cpus, 2);
127
128 memset(syncarea, 0, sizeof(*trigger1) + sizeof(*trigger2));
129 trigger1 = syncarea;
130 trigger2 = trigger1 + 1;
131
132 /* Get a new file for the final page */
133 fd = hugetlbfs_unlinked_fd();
134 if (fd < 0)
135 FAIL("hugetlbfs_unlinked_fd()");
136
137 verbose_printf("Mapping final page.. ");
138 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, race_type, fd, 0);
139 if (p == MAP_FAILED)
140 FAIL("mmap(): %s", strerror(errno));
141 verbose_printf("%p\n", p);
142
143 if (race_type == MAP_SHARED) {
144 child1 = fork();
145 if (child1 < 0)
146 FAIL("fork(): %s", strerror(errno));
147 if (child1 == 0)
148 proc_racer(p, online_cpus[0], trigger1, trigger2);
149
150 child2 = fork();
151 if (child2 < 0)
152 FAIL("fork(): %s", strerror(errno));
153 if (child2 == 0)
154 proc_racer(p, online_cpus[1], trigger2, trigger1);
155
156 /* wait() calls */
157 ret = waitpid(child1, &status1, 0);
158 if (ret < 0)
159 FAIL("waitpid() child 1: %s", strerror(errno));
160 verbose_printf("Child 1 status: %x\n", status1);
161
162
163 ret = waitpid(child2, &status2, 0);
164 if (ret < 0)
165 FAIL("waitpid() child 2: %s", strerror(errno));
166 verbose_printf("Child 2 status: %x\n", status2);
167
168 if (WIFSIGNALED(status1))
169 FAIL("Child 1 killed by signal %s",
170 strsignal(WTERMSIG(status1)));
171 if (WIFSIGNALED(status2))
172 FAIL("Child 2 killed by signal %s",
173 strsignal(WTERMSIG(status2)));
174
175 status1 = WEXITSTATUS(status1);
176 status2 = WEXITSTATUS(status2);
177 } else {
178 struct racer_info ri1 = {
179 .p = p,
180 .cpu = online_cpus[0],
181 .mytrigger = trigger1,
182 .othertrigger = trigger2,
183 };
184 struct racer_info ri2 = {
185 .p = p,
186 .cpu = online_cpus[1],
187 .mytrigger = trigger2,
188 .othertrigger = trigger1,
189 };
190 void *tret1, *tret2;
191
192 ret = pthread_create(&thread1, NULL, thread_racer, &ri1);
193 if (ret != 0)
194 FAIL("pthread_create() 1: %s\n", strerror(errno));
195
196 ret = pthread_create(&thread2, NULL, thread_racer, &ri2);
197 if (ret != 0)
198 FAIL("pthread_create() 2: %s\n", strerror(errno));
199
200 ret = pthread_join(thread1, &tret1);
201 if (ret != 0)
202 FAIL("pthread_join() 1: %s\n", strerror(errno));
203 if (tret1 != &ri1)
204 FAIL("Thread 1 returned %p not %p, killed?\n",
205 tret1, &ri1);
206 ret = pthread_join(thread2, &tret2);
207 if (ret != 0)
208 FAIL("pthread_join() 2: %s\n", strerror(errno));
209 if (tret2 != &ri2)
210 FAIL("Thread 2 returned %p not %p, killed?\n",
211 tret2, &ri2);
212
213 status1 = ri1.status;
214 status2 = ri2.status;
215 }
216
217 if (status1 != 0)
218 FAIL("Racer 1 terminated with code %d", status1);
219
220 if (status2 != 0)
221 FAIL("Racer 2 terminated with code %d", status2);
222 }
223
224 int main(int argc, char *argv[])
225 {
226 unsigned long totpages;
227 int fd;
228 void *p, *q;
229 unsigned long i;
230 int race_type;
231
232 test_init(argc, argv);
233
234 if (argc != 2)
235 CONFIG("Usage: alloc-instantiate-race <private|shared>");
236
237 hpage_size = check_hugepagesize();
238 totpages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
239
240 if (strcmp(argv[1], "shared") == 0) {
241 race_type = MAP_SHARED;
242 } else if (strcmp(argv[1], "private") == 0) {
243 race_type = MAP_PRIVATE;
244 } else {
245 CONFIG("Usage: alloc-instantiate-race <private|shared>");
246 }
247
248 fd = hugetlbfs_unlinked_fd();
249 if (fd < 0)
250 FAIL("hugetlbfs_unlinked_fd()");
251
252 /* Get a shared normal page for synchronization */
253 verbose_printf("Mapping synchronization area..");
254 q = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
255 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
256 if (q == MAP_FAILED)
257 FAIL("mmap() sync area: %s", strerror(errno));
258 verbose_printf("done\n");
259
260 verbose_printf("Mapping %ld/%ld pages.. ", totpages-1, totpages);
261 p = mmap(NULL, (totpages-1)*hpage_size, PROT_READ|PROT_WRITE,
262 MAP_SHARED, fd, 0);
263 if (p == MAP_FAILED)
264 FAIL("mmap() 1: %s", strerror(errno));
265
266 /* Allocate all save one of the pages up front */
267 verbose_printf("instantiating.. ");
268 for (i = 0; i < (totpages - 1); i++)
269 memset(p + (i * hpage_size), 0, sizeof(int));
270 verbose_printf("done\n");
271
272 run_race(q, race_type);
273
274 PASS();
275 }