Skip to content

Commit 4107890

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Fix nanosleep for real this time
Amazingly, some libc implementations don't call __NR_nanosleep syscall from their nanosleep() APIs. Hammer it down with explicit syscall() call and never get back to it again. Also simplify code for timespec initialization. I verified that nanosleep is called w/ printk and in exactly same Linux image that is used in Travis CI. So it should both sleep and call correct syscall. v1->v2: - math is too hard, fix usec -> nsec convertion (Martin); - test_vmlinux has explicit nanosleep() call, convert that one as well. Fixes: 4e1fd25 ("selftests/bpf: Fix usleep() implementation") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent cc9864a commit 4107890

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

tools/testing/selftests/bpf/prog_tests/vmlinux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void nsleep()
1111
{
1212
struct timespec ts = { .tv_nsec = MY_TV_NSEC };
1313

14-
(void)nanosleep(&ts, NULL);
14+
(void)syscall(__NR_nanosleep, &ts, NULL);
1515
}
1616

1717
void test_vmlinux(void)

tools/testing/selftests/bpf/test_progs.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ struct prog_test_def {
3535
*/
3636
int usleep(useconds_t usec)
3737
{
38-
struct timespec ts;
39-
40-
if (usec > 999999) {
41-
ts.tv_sec = usec / 1000000;
42-
ts.tv_nsec = usec % 1000000;
43-
} else {
44-
ts.tv_sec = 0;
45-
ts.tv_nsec = usec;
46-
}
47-
return nanosleep(&ts, NULL);
38+
struct timespec ts = {
39+
.tv_sec = usec / 1000000,
40+
.tv_nsec = (usec % 1000000) * 1000,
41+
};
42+
43+
return syscall(__NR_nanosleep, &ts, NULL);
4844
}
4945

5046
static bool should_run(struct test_selector *sel, int num, const char *name)

0 commit comments

Comments
 (0)