futex: adjust a futex timeout with a per-timens offset
Subsystems (2)
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.
---
v2:
* check FUTEX_CLOCK_REALTIME properly
* fix futex_time32 too
kernel/futex.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/futex.c +5 -0
@@ -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
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
@@ -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
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.Cc <[email protected]>Fixes 5a590f35add9 ("posix-clocks: Wire up clock_gettime() with timens offsets")Reported-by Hans van der Laan <[email protected]>Signed-off-by Andrei Vagin <[email protected]>
Quoted 32 lines
---
v2:
* checkFUTEX_CLOCK_REALTIMEproperly
* fixfutex_time32tookernel/futex.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/futex.c +3
-0@@ -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
[tip: timers/urgent] selftests/timens: Add a test for futex()
In reply to Andrei VaginThe 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
@@ -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
[tip: timers/urgent] futex: Adjust absolute futex timeouts with per time namespace offset
In reply to Andrei VaginThe 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.
--- kernel/futex.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/futex.c +5 -0
@@ -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 ||