Skip to content

Commit cd77548

Browse files
author
Christopher Friedt
committed
tests: posix: stress test for pthread_create and pthread_join
Recently, a race condition was discovered in `pthread_create()` and `pthread_join()` that would trigger an assertion in `kernel/sched.c` of the form below. ``` aborted _current back from dead ``` Add a test that stresses the pthread implementation in order to more easily reproduce the failure. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 56b335a commit cd77548

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2023, Meta
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cmake_minimum_required(VERSION 3.20.0)
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(pthread_pressure)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE ${app_sources})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) 2023, Meta
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
source "Kconfig.zephyr"
6+
7+
config TEST_NUM_CPUS
8+
int "Number of CPUs to use in parallel"
9+
range 1 MP_NUM_CPUS
10+
default MP_NUM_CPUS
11+
help
12+
The number of parallel threads to run during the test. The test
13+
thread itself yields so that all cores have some probability of
14+
causing racey behaviour.
15+
16+
config TEST_DURATION_S
17+
int "Number of seconds to run the test"
18+
range 1 21600
19+
default 29
20+
help
21+
Duration for the test, in seconds. The range has a reblatively high
22+
upper bound because we should expect that pthread_create() and
23+
pthread_join() are stable enough to run for an arbitrarily long
24+
period of time without encountering any race conditions.
25+
26+
Some exceptions apply, notably Qemu SMP targets.
27+
28+
config TEST_DELAY_US
29+
int "Microseconds to delay between pthread join and create"
30+
default 0
31+
help
32+
If there is a race condition, a value of zero here should
33+
cause a crash.
34+
35+
config TEST_STACK_SIZE
36+
int "Size of each thread stack in this test"
37+
default 2048 if !64_BIT
38+
default 4096 if 64_BIT
39+
help
40+
The minimal stack size required to run a no-op thread.
41+
42+
config TEST_KTHREADS
43+
bool "Test k_threads"
44+
default n
45+
help
46+
Run tests for k_threads
47+
48+
config TEST_PTHREADS
49+
bool "Test pthreads"
50+
default y
51+
help
52+
Run tests for pthreads
53+
54+
config TEST_EXTRA_ASSERTIONS
55+
bool "Add extra assertions into the hot path"
56+
help
57+
On Qemu SMP targets, this can potentially lead to "scheduler noise"
58+
leaking in from the host system, which can cause the test to fail.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_NEW_API=y
3+
CONFIG_POSIX_API=y
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/*
2+
* Copyright (c) 2023, Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <pthread.h>
8+
9+
#include <zephyr/sys/util.h>
10+
#include <zephyr/ztest.h>
11+
12+
#define STACK_SIZE K_THREAD_STACK_LEN(CONFIG_TEST_STACK_SIZE)
13+
14+
/* update interval for printing stats */
15+
#if CONFIG_TEST_DURATION_S >= 60
16+
#define UPDATE_INTERVAL_S 10
17+
#elif CONFIG_TEST_DURATION_S >= 30
18+
#define UPDATE_INTERVAL_S 5
19+
#else
20+
#define UPDATE_INTERVAL_S 1
21+
#endif
22+
23+
/* 32 threads is mainly a limitation of find_lsb_set() */
24+
#define NUM_THREADS MIN(32, MIN(CONFIG_TEST_NUM_CPUS, CONFIG_MAX_PTHREAD_COUNT))
25+
26+
typedef int (*create_fn)(int i);
27+
typedef int (*join_fn)(int i);
28+
29+
static void *setup(void);
30+
static void before(void *fixture);
31+
32+
/* bitmask of available threads */
33+
static bool alive[NUM_THREADS];
34+
35+
/* array of thread stacks */
36+
static K_THREAD_STACK_ARRAY_DEFINE(thread_stacks, NUM_THREADS, STACK_SIZE);
37+
38+
static struct k_thread k_threads[NUM_THREADS];
39+
static size_t counters[NUM_THREADS];
40+
41+
static void print_stats(uint64_t now, uint64_t end)
42+
{
43+
printk("now (ms): %llu end (ms): %llu\n", now, end);
44+
for (int i = 0; i < NUM_THREADS; ++i) {
45+
printk("Thread %d created and joined %zu times\n", i, counters[i]);
46+
}
47+
}
48+
49+
static void test_create_join_common(const char *tag, create_fn create, join_fn join)
50+
{
51+
int i;
52+
__unused int ret;
53+
uint64_t now_ms = k_uptime_get();
54+
const uint64_t end_ms = now_ms + MSEC_PER_SEC * CONFIG_TEST_DURATION_S;
55+
uint64_t update_ms = now_ms + MSEC_PER_SEC * UPDATE_INTERVAL_S;
56+
57+
printk("BOARD: %s\n", CONFIG_BOARD);
58+
printk("NUM_THREADS: %u\n", NUM_THREADS);
59+
printk("TEST_NUM_CPUS: %u\n", CONFIG_TEST_NUM_CPUS);
60+
printk("TEST_DURATION_S: %u\n", CONFIG_TEST_DURATION_S);
61+
printk("TEST_DELAY_US: %u\n", CONFIG_TEST_DELAY_US);
62+
63+
for (i = 0; i < NUM_THREADS; ++i) {
64+
/* spawn thread i */
65+
ret = create(i);
66+
#ifdef CONFIG_EXTRA_ASSERTIONS
67+
zassert_ok(ret, "%s_create(%d)[%zu] failed: %d", tag, i, counters[i], ret);
68+
#endif
69+
}
70+
71+
do {
72+
#ifndef CONFIG_SMP
73+
/* allow the test thread to be swapped-out */
74+
k_yield();
75+
#endif
76+
77+
for (i = 0; i < NUM_THREADS; ++i) {
78+
if (alive[i]) {
79+
ret = join(i);
80+
#ifdef CONFIG_EXTRA_ASSERTIONS
81+
zassert_ok(ret, "%s_join(%d)[%zu] failed: %d", tag, i, counters[i],
82+
ret);
83+
#endif
84+
alive[i] = false;
85+
86+
/* update counter i after each (create,join) pair */
87+
++counters[i];
88+
89+
#if CONFIG_TEST_DELAY_US > 0
90+
/* success with 0 delay means we are ~raceless */
91+
k_busy_wait(CONFIG_TEST_DELAY_US);
92+
#endif
93+
94+
/* re-spawn thread i */
95+
ret = create(i);
96+
#ifdef CONFIG_TEST_EXTRA_ASSERTIONS
97+
zassert_ok(ret, "%s_create(%d)[%zu] failed: %d", tag, i,
98+
counters[i], ret);
99+
#endif
100+
}
101+
}
102+
103+
/* are we there yet? */
104+
now_ms = k_uptime_get();
105+
106+
/* dump some stats periodically */
107+
if (now_ms > update_ms) {
108+
update_ms += MSEC_PER_SEC * UPDATE_INTERVAL_S;
109+
110+
/* at this point, we should have seen many context switches */
111+
for (i = 0; i < NUM_THREADS; ++i) {
112+
#ifdef CONFIG_TEST_EXTRA_ASSERTIONS
113+
zassert_true(counters[i] > 0, "%s %d was never scheduled", tag, i);
114+
#endif
115+
}
116+
117+
print_stats(now_ms, end_ms);
118+
}
119+
} while (end_ms > now_ms);
120+
121+
print_stats(now_ms, end_ms);
122+
}
123+
124+
/*
125+
* Wrappers for k_threads
126+
*/
127+
128+
static void k_thread_fun(void *arg1, void *arg2, void *arg3)
129+
{
130+
int i = POINTER_TO_INT(arg1);
131+
132+
alive[i] = true;
133+
}
134+
135+
static int k_thread_create_wrapper(int i)
136+
{
137+
k_thread_create(&k_threads[i], thread_stacks[i], STACK_SIZE, k_thread_fun,
138+
INT_TO_POINTER(i), NULL, NULL, K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
139+
K_NO_WAIT);
140+
141+
return 0;
142+
}
143+
144+
static int k_thread_join_wrapper(int i)
145+
{
146+
return k_thread_join(&k_threads[i], K_FOREVER);
147+
}
148+
149+
ZTEST(pthread_pressure, test_k_thread_create_join)
150+
{
151+
if (IS_ENABLED(CONFIG_TEST_KTHREADS)) {
152+
test_create_join_common("k_thread", k_thread_create_wrapper, k_thread_join_wrapper);
153+
} else {
154+
ztest_test_skip();
155+
}
156+
}
157+
158+
/*
159+
* Wrappers for pthreads
160+
*/
161+
162+
static pthread_t pthreads[NUM_THREADS];
163+
static pthread_attr_t pthread_attrs[NUM_THREADS];
164+
165+
static void *pthread_fun(void *arg)
166+
{
167+
k_thread_fun(arg, NULL, NULL);
168+
return NULL;
169+
}
170+
171+
static int pthread_create_wrapper(int i)
172+
{
173+
return pthread_create(&pthreads[i], &pthread_attrs[i], pthread_fun, INT_TO_POINTER(i));
174+
}
175+
176+
static int pthread_join_wrapper(int i)
177+
{
178+
return pthread_join(pthreads[i], NULL);
179+
}
180+
181+
ZTEST(pthread_pressure, test_pthread_create_join)
182+
{
183+
if (IS_ENABLED(CONFIG_TEST_PTHREADS)) {
184+
test_create_join_common("pthread", pthread_create_wrapper, pthread_join_wrapper);
185+
} else {
186+
ztest_test_skip();
187+
}
188+
}
189+
190+
/*
191+
* Test suite / fixture
192+
*/
193+
194+
ZTEST_SUITE(pthread_pressure, NULL, setup, before, NULL, NULL);
195+
196+
static void *setup(void)
197+
{
198+
if (IS_ENABLED(CONFIG_TEST_PTHREADS)) {
199+
const struct sched_param param = {
200+
.sched_priority = sched_get_priority_max(SCHED_FIFO),
201+
};
202+
203+
/* setup pthread stacks */
204+
for (int i = 0; i < NUM_THREADS; ++i) {
205+
zassert_ok(pthread_attr_init(&pthread_attrs[i]));
206+
zassert_ok(pthread_attr_setstack(&pthread_attrs[i], thread_stacks[i],
207+
STACK_SIZE));
208+
zassert_ok(pthread_attr_setschedpolicy(&pthread_attrs[i], SCHED_FIFO));
209+
zassert_ok(pthread_attr_setschedparam(&pthread_attrs[i], &param));
210+
}
211+
}
212+
213+
return NULL;
214+
}
215+
216+
static void before(void *fixture)
217+
{
218+
ARG_UNUSED(before);
219+
220+
for (int i = 0; i < NUM_THREADS; ++i) {
221+
counters[i] = 0;
222+
}
223+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
common:
2+
arch_exclude:
3+
- posix
4+
tags: posix
5+
min_ram: 64
6+
integration_platforms:
7+
- qemu_riscv64_smp
8+
tests:
9+
portability.posix.pthread_pressure:
10+
extra_configs:
11+
- CONFIG_NEWLIB_LIBC=n
12+
portability.posix.pthread_pressure.newlib:
13+
filter: TOOLCHAIN_HAS_NEWLIB == 1
14+
extra_configs:
15+
- CONFIG_NEWLIB_LIBC=y
16+
portability.posix.pthread_pressure.tls:
17+
filter: CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE and
18+
CONFIG_TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE
19+
extra_configs:
20+
- CONFIG_NEWLIB_LIBC=n
21+
- CONFIG_THREAD_LOCAL_STORAGE=y
22+
- CONFIG_MAIN_STACK_SIZE=4096
23+
portability.posix.pthread_pressure.tls.newlib:
24+
filter: TOOLCHAIN_HAS_NEWLIB == 1 and CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE and
25+
CONFIG_TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE
26+
extra_configs:
27+
- CONFIG_NEWLIB_LIBC=y
28+
- CONFIG_THREAD_LOCAL_STORAGE=y
29+
- CONFIG_MAIN_STACK_SIZE=4096
30+
portability.posix.pthread_pressure.picolibc:
31+
tags: picolibc
32+
filter: CONFIG_PICOLIBC_SUPPORTED
33+
extra_configs:
34+
- CONFIG_PICOLIBC=y

0 commit comments

Comments
 (0)