Skip to content

Commit 66bce7a

Browse files
broonieakpm00
authored andcommitted
selftests/mm: fix test result reporting in gup_longterm
The kselftest framework uses the string logged when a test result is reported as the unique identifier for a test, using it to track test results between runs. The gup_longterm test fails to follow this pattern, it runs a single test function repeatedly with various parameters but each result report is a string logging an error message which is fixed between runs. Since the code already logs each test uniquely before it starts refactor to also print this to a buffer, then use that name as the test result. This isn't especially pretty but is relatively straightforward and is a great help to tooling. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Shuah Khan <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3f2d9a9 commit 66bce7a

File tree

1 file changed

+94
-56
lines changed

1 file changed

+94
-56
lines changed

tools/testing/selftests/mm/gup_longterm.c

Lines changed: 94 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,48 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
9393
__fsword_t fs_type = get_fs_type(fd);
9494
bool should_work;
9595
char *mem;
96+
int result = KSFT_PASS;
9697
int ret;
9798

99+
if (fd < 0) {
100+
result = KSFT_FAIL;
101+
goto report;
102+
}
103+
98104
if (ftruncate(fd, size)) {
99105
if (errno == ENOENT) {
100106
skip_test_dodgy_fs("ftruncate()");
101107
} else {
102-
ksft_test_result_fail("ftruncate() failed (%s)\n", strerror(errno));
108+
ksft_print_msg("ftruncate() failed (%s)\n",
109+
strerror(errno));
110+
result = KSFT_FAIL;
111+
goto report;
103112
}
104113
return;
105114
}
106115

107116
if (fallocate(fd, 0, 0, size)) {
108-
if (size == pagesize)
109-
ksft_test_result_fail("fallocate() failed (%s)\n", strerror(errno));
110-
else
111-
ksft_test_result_skip("need more free huge pages\n");
112-
return;
117+
if (size == pagesize) {
118+
ksft_print_msg("fallocate() failed (%s)\n", strerror(errno));
119+
result = KSFT_FAIL;
120+
} else {
121+
ksft_print_msg("need more free huge pages\n");
122+
result = KSFT_SKIP;
123+
}
124+
goto report;
113125
}
114126

115127
mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
116128
shared ? MAP_SHARED : MAP_PRIVATE, fd, 0);
117129
if (mem == MAP_FAILED) {
118-
if (size == pagesize || shared)
119-
ksft_test_result_fail("mmap() failed (%s)\n", strerror(errno));
120-
else
121-
ksft_test_result_skip("need more free huge pages\n");
122-
return;
130+
if (size == pagesize || shared) {
131+
ksft_print_msg("mmap() failed (%s)\n", strerror(errno));
132+
result = KSFT_FAIL;
133+
} else {
134+
ksft_print_msg("need more free huge pages\n");
135+
result = KSFT_SKIP;
136+
}
137+
goto report;
123138
}
124139

125140
/* Fault in the page such that GUP-fast can pin it directly. */
@@ -134,7 +149,8 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
134149
*/
135150
ret = mprotect(mem, size, PROT_READ);
136151
if (ret) {
137-
ksft_test_result_fail("mprotect() failed (%s)\n", strerror(errno));
152+
ksft_print_msg("mprotect() failed (%s)\n", strerror(errno));
153+
result = KSFT_FAIL;
138154
goto munmap;
139155
}
140156
/* FALLTHROUGH */
@@ -147,12 +163,14 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
147163
type == TEST_TYPE_RW_FAST;
148164

149165
if (gup_fd < 0) {
150-
ksft_test_result_skip("gup_test not available\n");
166+
ksft_print_msg("gup_test not available\n");
167+
result = KSFT_SKIP;
151168
break;
152169
}
153170

154171
if (rw && shared && fs_is_unknown(fs_type)) {
155-
ksft_test_result_skip("Unknown filesystem\n");
172+
ksft_print_msg("Unknown filesystem\n");
173+
result = KSFT_SKIP;
156174
return;
157175
}
158176
/*
@@ -169,14 +187,19 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
169187
args.flags |= rw ? PIN_LONGTERM_TEST_FLAG_USE_WRITE : 0;
170188
ret = ioctl(gup_fd, PIN_LONGTERM_TEST_START, &args);
171189
if (ret && errno == EINVAL) {
172-
ksft_test_result_skip("PIN_LONGTERM_TEST_START failed (EINVAL)n");
190+
ksft_print_msg("PIN_LONGTERM_TEST_START failed (EINVAL)n");
191+
result = KSFT_SKIP;
173192
break;
174193
} else if (ret && errno == EFAULT) {
175-
ksft_test_result(!should_work, "Should have failed\n");
194+
if (should_work)
195+
result = KSFT_FAIL;
196+
else
197+
result = KSFT_PASS;
176198
break;
177199
} else if (ret) {
178-
ksft_test_result_fail("PIN_LONGTERM_TEST_START failed (%s)\n",
179-
strerror(errno));
200+
ksft_print_msg("PIN_LONGTERM_TEST_START failed (%s)\n",
201+
strerror(errno));
202+
result = KSFT_FAIL;
180203
break;
181204
}
182205

@@ -189,7 +212,10 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
189212
* some previously unsupported filesystems, we might want to
190213
* perform some additional tests for possible data corruptions.
191214
*/
192-
ksft_test_result(should_work, "Should have worked\n");
215+
if (should_work)
216+
result = KSFT_PASS;
217+
else
218+
result = KSFT_FAIL;
193219
break;
194220
}
195221
#ifdef LOCAL_CONFIG_HAVE_LIBURING
@@ -199,17 +225,19 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
199225

200226
/* io_uring always pins pages writable. */
201227
if (shared && fs_is_unknown(fs_type)) {
202-
ksft_test_result_skip("Unknown filesystem\n");
203-
return;
228+
ksft_print_msg("Unknown filesystem\n");
229+
result = KSFT_SKIP;
230+
goto report;
204231
}
205232
should_work = !shared ||
206233
fs_supports_writable_longterm_pinning(fs_type);
207234

208235
/* Skip on errors, as we might just lack kernel support. */
209236
ret = io_uring_queue_init(1, &ring, 0);
210237
if (ret < 0) {
211-
ksft_test_result_skip("io_uring_queue_init() failed (%s)\n",
212-
strerror(-ret));
238+
ksft_print_msg("io_uring_queue_init() failed (%s)\n",
239+
strerror(-ret));
240+
result = KSFT_SKIP;
213241
break;
214242
}
215243
/*
@@ -222,17 +250,28 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
222250
/* Only new kernels return EFAULT. */
223251
if (ret && (errno == ENOSPC || errno == EOPNOTSUPP ||
224252
errno == EFAULT)) {
225-
ksft_test_result(!should_work, "Should have failed (%s)\n",
226-
strerror(errno));
253+
if (should_work) {
254+
ksft_print_msg("Should have failed (%s)\n",
255+
strerror(errno));
256+
result = KSFT_FAIL;
257+
} else {
258+
result = KSFT_PASS;
259+
}
227260
} else if (ret) {
228261
/*
229262
* We might just lack support or have insufficient
230263
* MEMLOCK limits.
231264
*/
232-
ksft_test_result_skip("io_uring_register_buffers() failed (%s)\n",
233-
strerror(-ret));
265+
ksft_print_msg("io_uring_register_buffers() failed (%s)\n",
266+
strerror(-ret));
267+
result = KSFT_SKIP;
234268
} else {
235-
ksft_test_result(should_work, "Should have worked\n");
269+
if (should_work) {
270+
result = KSFT_PASS;
271+
} else {
272+
ksft_print_msg("Should have worked\n");
273+
result = KSFT_FAIL;
274+
}
236275
io_uring_unregister_buffers(&ring);
237276
}
238277

@@ -246,6 +285,8 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared)
246285

247286
munmap:
248287
munmap(mem, size);
288+
report:
289+
log_test_result(result);
249290
}
250291

251292
typedef void (*test_fn)(int fd, size_t size);
@@ -254,13 +295,11 @@ static void run_with_memfd(test_fn fn, const char *desc)
254295
{
255296
int fd;
256297

257-
ksft_print_msg("[RUN] %s ... with memfd\n", desc);
298+
log_test_start("%s ... with memfd", desc);
258299

259300
fd = memfd_create("test", 0);
260-
if (fd < 0) {
261-
ksft_test_result_fail("memfd_create() failed (%s)\n", strerror(errno));
262-
return;
263-
}
301+
if (fd < 0)
302+
ksft_print_msg("memfd_create() failed (%s)\n", strerror(errno));
264303

265304
fn(fd, pagesize);
266305
close(fd);
@@ -271,46 +310,46 @@ static void run_with_tmpfile(test_fn fn, const char *desc)
271310
FILE *file;
272311
int fd;
273312

274-
ksft_print_msg("[RUN] %s ... with tmpfile\n", desc);
313+
log_test_start("%s ... with tmpfile", desc);
275314

276315
file = tmpfile();
277316
if (!file) {
278-
ksft_test_result_fail("tmpfile() failed (%s)\n", strerror(errno));
279-
return;
280-
}
281-
282-
fd = fileno(file);
283-
if (fd < 0) {
284-
ksft_test_result_fail("fileno() failed (%s)\n", strerror(errno));
285-
goto close;
317+
ksft_print_msg("tmpfile() failed (%s)\n", strerror(errno));
318+
fd = -1;
319+
} else {
320+
fd = fileno(file);
321+
if (fd < 0) {
322+
ksft_print_msg("fileno() failed (%s)\n", strerror(errno));
323+
}
286324
}
287325

288326
fn(fd, pagesize);
289-
close:
290-
fclose(file);
327+
328+
if (file)
329+
fclose(file);
291330
}
292331

293332
static void run_with_local_tmpfile(test_fn fn, const char *desc)
294333
{
295334
char filename[] = __FILE__"_tmpfile_XXXXXX";
296335
int fd;
297336

298-
ksft_print_msg("[RUN] %s ... with local tmpfile\n", desc);
337+
log_test_start("%s ... with local tmpfile", desc);
299338

300339
fd = mkstemp(filename);
301-
if (fd < 0) {
302-
ksft_test_result_fail("mkstemp() failed (%s)\n", strerror(errno));
303-
return;
304-
}
340+
if (fd < 0)
341+
ksft_print_msg("mkstemp() failed (%s)\n", strerror(errno));
305342

306343
if (unlink(filename)) {
307-
ksft_test_result_fail("unlink() failed (%s)\n", strerror(errno));
308-
goto close;
344+
ksft_print_msg("unlink() failed (%s)\n", strerror(errno));
345+
close(fd);
346+
fd = -1;
309347
}
310348

311349
fn(fd, pagesize);
312-
close:
313-
close(fd);
350+
351+
if (fd >= 0)
352+
close(fd);
314353
}
315354

316355
static void run_with_memfd_hugetlb(test_fn fn, const char *desc,
@@ -319,15 +358,14 @@ static void run_with_memfd_hugetlb(test_fn fn, const char *desc,
319358
int flags = MFD_HUGETLB;
320359
int fd;
321360

322-
ksft_print_msg("[RUN] %s ... with memfd hugetlb (%zu kB)\n", desc,
361+
log_test_start("%s ... with memfd hugetlb (%zu kB)", desc,
323362
hugetlbsize / 1024);
324363

325364
flags |= __builtin_ctzll(hugetlbsize) << MFD_HUGE_SHIFT;
326365

327366
fd = memfd_create("test", flags);
328367
if (fd < 0) {
329-
ksft_test_result_skip("memfd_create() failed (%s)\n", strerror(errno));
330-
return;
368+
ksft_print_msg("memfd_create() failed (%s)\n", strerror(errno));
331369
}
332370

333371
fn(fd, hugetlbsize);

0 commit comments

Comments
 (0)