Skip to contentSkip to conversation

futex: adjust a futex timeout with a per-timens offset

PATCH Applied Andrei Vagin 4 participants 7 messages Oct 15, 2020 – Oct 16, 2020

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 futex: adjust a futex timeout with a per-timens offset

For all commands except FUTEX_WAIT, timeout is interpreted as an absolute value. This absolute value is inside the task's time namespace and has to be converted to the host's time.

Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")
---
 kernel/futex.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/futex.c +3 -0
index a5876694a60e..9ff2b8c5a506 100644
@@ -39,6 +39,7 @@
 #include <linux/freezer.h>
 #include <linux/memblock.h>
 #include <linux/fault-inject.h>
+#include <linux/time_namespace.h>
 
 #include <asm/futex.h>
 
@@ -3797,6 +3798,8 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
 		t = timespec64_to_ktime(ts);
 		if (cmd == FUTEX_WAIT)
 			t = ktime_add_safe(ktime_get(), t);
+		else if (!(cmd & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	/*
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 selftests/timens: Add a test for futex()

Output on success:

 $ ./futex
 1..1
 ok 1 futex
 # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/testing/selftests/timens/Makefile |   2 +-
 tools/testing/selftests/timens/futex.c  | 107 ++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/timens/futex.c
tools/testing/selftests/timens/Makefile +1 -1
index b4fd9a934654..3a5936cc10ab 100644
@@ -1,4 +1,4 @@
-TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec
+TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec futex
 TEST_GEN_PROGS_EXTENDED := gettime_perf
 
 CFLAGS := -Wall -Werror -pthread
tools/testing/selftests/timens/futex.c new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
2.26.2
merge bot Applied
#2 lore raw

[tip: timers/urgent] selftests/timens: Add a test for futex()

The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     4cdf42596216e08051c0ccc4c896dcb8b2d22f10
Gitweb:        https://git.kernel.org/tip/4cdf42596216e08051c0ccc4c896dcb8b2d22f10
Author:        Andrei Vagin <[email protected]>
AuthorDate:    Thu, 15 Oct 2020 00:29:09 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 15 Oct 2020 11:24:04 +02:00

selftests/timens: Add a test for futex()

Output on success:

 $ ./futex
 1..1
 ok 1 futex
 # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
---
 tools/testing/selftests/timens/Makefile |   2 +-
 tools/testing/selftests/timens/futex.c  | 107 +++++++++++++++++++++++-
 2 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/timens/futex.c
tools/testing/selftests/timens/Makefile +1 -1
index b4fd9a9..3a5936c 100644
@@ -1,4 +1,4 @@
-TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec
+TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec futex
 TEST_GEN_PROGS_EXTENDED := gettime_perf
 
 CFLAGS := -Wall -Werror -pthread
tools/testing/selftests/timens/futex.c new file +107 -0

107 lines not on this page · raw message

merge bot Applied
#3 lore raw

[tip: timers/urgent] futex: Adjust futex absolute timeouts with per-timens offset

In reply to Andrei Vagin
The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     06764291690f8650a9f96dea42cc0dd4138d47d5
Gitweb:        https://git.kernel.org/tip/06764291690f8650a9f96dea42cc0dd4138d47d5
Author:        Andrei Vagin <[email protected]>
AuthorDate:    Thu, 15 Oct 2020 00:29:08 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 15 Oct 2020 11:24:04 +02:00

futex: Adjust futex absolute timeouts with per-timens offset

For all commands except FUTEX_WAIT, timeout is interpreted as an absolute value. This absolute value is inside the task's time namespace and has to be converted to the host's time.

Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")
---
 kernel/futex.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/futex.c +3 -0
index a587669..9ff2b8c 100644
@@ -39,6 +39,7 @@
 #include <linux/freezer.h>
 #include <linux/memblock.h>
 #include <linux/fault-inject.h>
+#include <linux/time_namespace.h>
 
 #include <asm/futex.h>
 
@@ -3797,6 +3798,8 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
 		t = timespec64_to_ktime(ts);
 		if (cmd == FUTEX_WAIT)
 			t = ktime_add_safe(ktime_get(), t);
+		else if (!(cmd & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	/*
#4 lore raw
In reply to Andrei Vagin
Quoted 9 lines · Andrei Vagin

For all commands except FUTEX_WAIT, timeout is interpreted as an absolute value. This absolute value is inside the task's time namespace and has to be converted to the host's time.

Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")

kernel/futex.c SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, · line 3802

Quoted 5 lines
kernel/futex.c +2 -0
@@ -3797,6 +3798,8 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
 		t = timespec64_to_ktime(ts);
 		if (cmd == FUTEX_WAIT)
 			t = ktime_add_safe(ktime_get(), t);
+		else if (!(cmd & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);

Err, it probably should be
: else if (!(op & FUTEX_CLOCK_REALTIME))

And there's also
: SYSCALL_DEFINE6(futex_time32, ...)
which wants to be patched.

Thanks,
Dmitry

#5 lore raw
Quoted 5 lines · Dmitry Safonov
Quoted 4 lines · Andrei Vagin

For all commands except FUTEX_WAIT, timeout is interpreted as an absolute value. This absolute value is inside the task's time namespace and has to be converted to the host's time.

Quoted 13 lines

kernel/futex.c SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, · line 3802

Quoted 10 lines
Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")
kernel/futex.c +2 -0
@@ -3797,6 +3798,8 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
 		t = timespec64_to_ktime(ts);
 		if (cmd == FUTEX_WAIT)
 			t = ktime_add_safe(ktime_get(), t);
+		else if (!(cmd & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);

Err, it probably should be
: else if (!(op & FUTEX_CLOCK_REALTIME))

Duh, you are right. I stared at it for a while and did not spot it.

Quoted 3 lines

And there's also
: SYSCALL_DEFINE6(futex_time32, ...)
which wants to be patched.

Indeed. I zapped the commits.

Thanks,

tglx

#6 lore raw
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Dmitry Safonov
Quoted 4 lines · Andrei Vagin

For all commands except FUTEX_WAIT, timeout is interpreted as an absolute value. This absolute value is inside the task's time namespace and has to be converted to the host's time.

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")
(quoted diff) +0 -0
@@ -3797,6 +3798,8 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
Quoted 8 lines

kernel/futex.c SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, · line 3802

Quoted 5 lines
kernel/futex.c +2 -0
 		t = timespec64_to_ktime(ts);
 		if (cmd == FUTEX_WAIT)
 			t = ktime_add_safe(ktime_get(), t);
+		else if (!(cmd & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);

Err, it probably should be
: else if (!(op & FUTEX_CLOCK_REALTIME))

Dmitry, thank you for catching this.

Quoted 8 lines

Duh, you are right. I stared at it for a while and did not spot it.

Quoted 3 lines

And there's also
: SYSCALL_DEFINE6(futex_time32, ...)
which wants to be patched.

Indeed. I zapped the commits.

I sent a new version. This time, I extended the test to check FUTEX_CLOCK_REALTIME and I compiled and run the compat version. Everything works as it should be.

Thanks,
Andrei