Skip to contentSkip to conversation

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

PATCH Applied Andrei Vagin 3 participants 5 messages Oct 15, 2020 – Oct 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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")

---

v2:
* check FUTEX_CLOCK_REALTIME properly
* fix futex_time32 too

 kernel/futex.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/futex.c +5 -0
index a5876694a60e..32056d2d4171 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	/*
@@ -3989,6 +3992,8 @@ SYSCALL_DEFINE6(futex_time32, 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Signature · 1 line
2.26.2
#1 lore raw

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

Output on success:
1..2
ok 1 futex with the 0 clockid
ok 2 futex with the 1 clockid
# Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0

---
v2: extend the test to check FUTEX_CLOCK_REALTIME

 tools/testing/selftests/timens/Makefile |   2 +-
 tools/testing/selftests/timens/futex.c  | 110 ++++++++++++++++++++++++
 2 files changed, 111 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 +110 -0

110 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:     a4fd8414659bf470e2146b352574bbd274e54b7a
Gitweb:        https://git.kernel.org/tip/a4fd8414659bf470e2146b352574bbd274e54b7a
Author:        Andrei Vagin <[email protected]>
AuthorDate:    Thu, 15 Oct 2020 09:00:20 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 20 Oct 2020 17:02:57 +02:00

selftests/timens: Add a test for futex()

Output on success:
1..2
ok 1 futex with the 0 clockid
ok 2 futex with the 1 clockid
# Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 tools/testing/selftests/timens/Makefile |   2 +-
 tools/testing/selftests/timens/futex.c  | 110 +++++++++++++++++++++++-
 2 files changed, 111 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 +110 -0

110 lines not on this page · raw message

Reviewed-by on v2 1/2
#3 lore raw
In reply to Andrei Vagin
Quoted 8 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")
Quoted 32 lines

---

v2:
* check FUTEX_CLOCK_REALTIME properly
* fix futex_time32 too

 kernel/futex.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/futex.c +3 -0
index a5876694a60e..32056d2d4171 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	/*

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

Quoted 8 lines
kernel/futex.c +2 -0
@@ -3989,6 +3992,8 @@ SYSCALL_DEFINE6(futex_time32, 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||

Thanks,
Dmitry

merge bot Applied
#4 lore raw

[tip: timers/urgent] futex: Adjust absolute futex timeouts with per time namespace 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:     c2f7d08cccf4af2ce6992feaabb9e68e4ae0bff3
Gitweb:        https://git.kernel.org/tip/c2f7d08cccf4af2ce6992feaabb9e68e4ae0bff3
Author:        Andrei Vagin <[email protected]>
AuthorDate:    Thu, 15 Oct 2020 09:00:19 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 20 Oct 2020 17:02:57 +02:00

futex: Adjust absolute futex timeouts with per time namespace offset

For all commands except FUTEX_WAIT, the 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 | 5 +++++
 1 file changed, 5 insertions(+)
kernel/futex.c +5 -0
index 680854d..be68ac0 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	/*
@@ -3989,6 +3992,8 @@ SYSCALL_DEFINE6(futex_time32, 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 (!(op & FUTEX_CLOCK_REALTIME))
+			t = timens_ktime_to_host(CLOCK_MONOTONIC, t);
 		tp = &t;
 	}
 	if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||