Skip to contentSkip to conversation

selftests/bpf: fix nanosleep for real this time

PATCH Superseded Andrii Nakryiko 3 participants 3 messages Mar 13, 2020 – Mar 14, 2020

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Fixes 4e1fd25d19e8 ("selftests/bpf: Fix usleep() implementation")
---
 tools/testing/selftests/bpf/test_progs.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/test_progs.c +6 -10
index f85a06512541..6956d722a463 100644
@@ -35,16 +35,12 @@ struct prog_test_def {
  */
 int usleep(useconds_t usec)
 {
-	struct timespec ts;
-
-	if (usec > 999999) {
-		ts.tv_sec = usec / 1000000;
-		ts.tv_nsec = usec % 1000000;
-	} else {
-		ts.tv_sec = 0;
-		ts.tv_nsec = usec;
-	}
-	return nanosleep(&ts, NULL);
+	struct timespec ts = {
+		.tv_sec = usec / 1000000,
+		.tv_nsec = usec % 1000000,
+	};
+
+	return syscall(__NR_nanosleep, &ts, NULL);
 }
 
 static bool should_run(struct test_selector *sel, int num, const char *name)
Signature · 1 line
2.17.1
#1 lore raw
Quoted 24 lines · Andrii Nakryiko

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.

Fixes 4e1fd25d19e8 ("selftests/bpf: Fix usleep() implementation")
---
 tools/testing/selftests/bpf/test_progs.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/test_progs.c +0 -3
index f85a06512541..6956d722a463 100644
@@ -35,16 +35,12 @@ struct prog_test_def {
  */
 int usleep(useconds_t usec)
 {
-	struct timespec ts;
-
-	if (usec > 999999) {

tools/testing/selftests/bpf/test_progs.c struct prog_test_def { · line 40

Quoted 10 lines
tools/testing/selftests/bpf/test_progs.c +3 -7
-		ts.tv_sec = usec / 1000000;
-		ts.tv_nsec = usec % 1000000;
-	} else {
-		ts.tv_sec = 0;
-		ts.tv_nsec = usec;
-	}
-	return nanosleep(&ts, NULL);
+	struct timespec ts = {
+		.tv_sec = usec / 1000000,
+		.tv_nsec = usec % 1000000,

usec is in micro and tv_nsec is in nano?

Quoted 5 lines
tools/testing/selftests/bpf/test_progs.c +3 -0
+	};
+
+	return syscall(__NR_nanosleep, &ts, NULL);
 }
 
#2 lore raw
Quoted 29 lines · Martin KaFai Lau
Quoted 27 lines · Andrii Nakryiko

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.

Fixes 4e1fd25d19e8 ("selftests/bpf: Fix usleep() implementation")
---
 tools/testing/selftests/bpf/test_progs.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/test_progs.c +0 -6
index f85a06512541..6956d722a463 100644
@@ -35,16 +35,12 @@ struct prog_test_def {
  */
 int usleep(useconds_t usec)
 {
-     struct timespec ts;
-
-     if (usec > 999999) {
-             ts.tv_sec = usec / 1000000;
-             ts.tv_nsec = usec % 1000000;
-     } else {
Quoted 9 lines

tools/testing/selftests/bpf/test_progs.c struct prog_test_def { · line 40

Quoted 7 lines
tools/testing/selftests/bpf/test_progs.c +3 -4
-             ts.tv_sec = 0;
-             ts.tv_nsec = usec;
-     }
-     return nanosleep(&ts, NULL);
+     struct timespec ts = {
+             .tv_sec = usec / 1000000,
+             .tv_nsec = usec % 1000000,

usec is in micro and tv_nsec is in nano?

Yes, this is implementation of usleep() (microsecond sleep), so usec is microseconds. We call nanosleep internally, though, which accepts seconds and nanoseconds units. Did I mess up math here?

But either way, sending v2, there is another place we explicitly are calling nanosleep as well, fixing that one as well.

Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/test_progs.c +3 -0
+     };
+
+     return syscall(__NR_nanosleep, &ts, NULL);
 }