Skip to content

Commit 19a4ff5

Browse files
compudjIngo Molnar
authored andcommitted
selftests, sched/membarrier: Add multi-threaded test
membarrier commands cover very different code paths if they are in a single-threaded vs multi-threaded process. Therefore, exercise both scenarios in the kernel selftests to increase coverage of this selftest. Signed-off-by: Mathieu Desnoyers <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Chris Metcalf <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Eric W. Biederman <[email protected]> Cc: Kirill Tkhai <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Paul E. McKenney <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Russell King - ARM Linux admin <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 227a4aa commit 19a4ff5

File tree

5 files changed

+124
-21
lines changed

5 files changed

+124
-21
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
membarrier_test
1+
membarrier_test_multi_thread
2+
membarrier_test_single_thread

tools/testing/selftests/membarrier/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
CFLAGS += -g -I../../../../usr/include/
3+
LDLIBS += -lpthread
34

4-
TEST_GEN_PROGS := membarrier_test
5+
TEST_GEN_PROGS := membarrier_test_single_thread \
6+
membarrier_test_multi_thread
57

68
include ../lib.mk
7-

tools/testing/selftests/membarrier/membarrier_test.c renamed to tools/testing/selftests/membarrier/membarrier_test_impl.h

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// SPDX-License-Identifier: GPL-2.0
1+
/* SPDX-License-Identifier: GPL-2.0 */
22
#define _GNU_SOURCE
33
#include <linux/membarrier.h>
44
#include <syscall.h>
55
#include <stdio.h>
66
#include <errno.h>
77
#include <string.h>
8+
#include <pthread.h>
89

910
#include "../kselftest.h"
1011

@@ -223,7 +224,7 @@ static int test_membarrier_global_expedited_success(void)
223224
return 0;
224225
}
225226

226-
static int test_membarrier(void)
227+
static int test_membarrier_fail(void)
227228
{
228229
int status;
229230

@@ -233,10 +234,27 @@ static int test_membarrier(void)
233234
status = test_membarrier_flags_fail();
234235
if (status)
235236
return status;
236-
status = test_membarrier_global_success();
237+
status = test_membarrier_private_expedited_fail();
237238
if (status)
238239
return status;
239-
status = test_membarrier_private_expedited_fail();
240+
status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0);
241+
if (status < 0) {
242+
ksft_test_result_fail("sys_membarrier() failed\n");
243+
return status;
244+
}
245+
if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) {
246+
status = test_membarrier_private_expedited_sync_core_fail();
247+
if (status)
248+
return status;
249+
}
250+
return 0;
251+
}
252+
253+
static int test_membarrier_success(void)
254+
{
255+
int status;
256+
257+
status = test_membarrier_global_success();
240258
if (status)
241259
return status;
242260
status = test_membarrier_register_private_expedited_success();
@@ -251,9 +269,6 @@ static int test_membarrier(void)
251269
return status;
252270
}
253271
if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) {
254-
status = test_membarrier_private_expedited_sync_core_fail();
255-
if (status)
256-
return status;
257272
status = test_membarrier_register_private_expedited_sync_core_success();
258273
if (status)
259274
return status;
@@ -300,14 +315,3 @@ static int test_membarrier_query(void)
300315
ksft_test_result_pass("sys_membarrier available\n");
301316
return 0;
302317
}
303-
304-
int main(int argc, char **argv)
305-
{
306-
ksft_print_header();
307-
ksft_set_plan(13);
308-
309-
test_membarrier_query();
310-
test_membarrier();
311-
312-
return ksft_exit_pass();
313-
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
3+
#include <linux/membarrier.h>
4+
#include <syscall.h>
5+
#include <stdio.h>
6+
#include <errno.h>
7+
#include <string.h>
8+
#include <pthread.h>
9+
10+
#include "membarrier_test_impl.h"
11+
12+
static int thread_ready, thread_quit;
13+
static pthread_mutex_t test_membarrier_thread_mutex =
14+
PTHREAD_MUTEX_INITIALIZER;
15+
static pthread_cond_t test_membarrier_thread_cond =
16+
PTHREAD_COND_INITIALIZER;
17+
18+
void *test_membarrier_thread(void *arg)
19+
{
20+
pthread_mutex_lock(&test_membarrier_thread_mutex);
21+
thread_ready = 1;
22+
pthread_cond_broadcast(&test_membarrier_thread_cond);
23+
pthread_mutex_unlock(&test_membarrier_thread_mutex);
24+
25+
pthread_mutex_lock(&test_membarrier_thread_mutex);
26+
while (!thread_quit)
27+
pthread_cond_wait(&test_membarrier_thread_cond,
28+
&test_membarrier_thread_mutex);
29+
pthread_mutex_unlock(&test_membarrier_thread_mutex);
30+
31+
return NULL;
32+
}
33+
34+
static int test_mt_membarrier(void)
35+
{
36+
int i;
37+
pthread_t test_thread;
38+
39+
pthread_create(&test_thread, NULL,
40+
test_membarrier_thread, NULL);
41+
42+
pthread_mutex_lock(&test_membarrier_thread_mutex);
43+
while (!thread_ready)
44+
pthread_cond_wait(&test_membarrier_thread_cond,
45+
&test_membarrier_thread_mutex);
46+
pthread_mutex_unlock(&test_membarrier_thread_mutex);
47+
48+
test_membarrier_fail();
49+
50+
test_membarrier_success();
51+
52+
pthread_mutex_lock(&test_membarrier_thread_mutex);
53+
thread_quit = 1;
54+
pthread_cond_broadcast(&test_membarrier_thread_cond);
55+
pthread_mutex_unlock(&test_membarrier_thread_mutex);
56+
57+
pthread_join(test_thread, NULL);
58+
59+
return 0;
60+
}
61+
62+
int main(int argc, char **argv)
63+
{
64+
ksft_print_header();
65+
ksft_set_plan(13);
66+
67+
test_membarrier_query();
68+
69+
/* Multi-threaded */
70+
test_mt_membarrier();
71+
72+
return ksft_exit_pass();
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
3+
#include <linux/membarrier.h>
4+
#include <syscall.h>
5+
#include <stdio.h>
6+
#include <errno.h>
7+
#include <string.h>
8+
#include <pthread.h>
9+
10+
#include "membarrier_test_impl.h"
11+
12+
int main(int argc, char **argv)
13+
{
14+
ksft_print_header();
15+
ksft_set_plan(13);
16+
17+
test_membarrier_query();
18+
19+
test_membarrier_fail();
20+
21+
test_membarrier_success();
22+
23+
return ksft_exit_pass();
24+
}

0 commit comments

Comments
 (0)