kernel: Introduce Time Namespace
v5 cover kernel: Introduce Time Namespace
Discussions around time namespace are there for a long time. The first attempt to implement it was in 2006 by Jeff Dike. From that time, the topic appears on and off in various discussions.
There are two main use cases for time namespaces:
1. change date and time inside a container;
2. adjust clocks for a container restored from a checkpoint.
“It seems like this might be one of the last major obstacles keeping migration from being used in production systems, given that not all containers and connections can be migrated as long as a time dependency is capable of messing it up.” (by github.com/dav-ell)
The kernel provides access to several clocks: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME. Last two clocks are monotonous, but the start points for them are not defined and are different for each system. When a container is migrated from one node to another, all clocks have to be restored into consistent states; in other words, they have to continue running from the same points where they have been dumped.
The main idea of this patch set is adding per-namespace offsets for system clocks. When a process in a non-root time namespace requests time of a clock, a namespace offset is added to the current value of this clock and the sum is returned.
All offsets are placed on a separate page, this allows us to map it as part of VVAR into user processes and use offsets from VDSO calls.
Now offsets are implemented for CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks.
v4..v5 Changes:
* Rebased over generic vdso (already in master)
* Addressing review comments by Thomas Gleixner
(thanks much for your time and patience):
- Dropping timens prefix from subjects (it's not a subsystem)
- Keeping commit messages in a neutral technical form
- Splitting unreasonably large patches
- Document code with missing comments
- Dropped dead code that's not compiled with !CONFIG_TIME_NS
* Updated performance results [here, at the bottom]
* Split vdso jump tables patch
* Allow unshare() with many threads: it's safe until fork()/clone(), where we check for CLONE_THREADS
* Add missed check in setns() for CLONE_VM | CLONE_THREADS
* Fixed compilation with !CONFIG_UTS_NS
* Add a plan in selftests (prevents new warning "Planned tests != run tests")
* Set jump table section address & size to (-1UL) just in case if there is no such section while running vdso2c (and WARN() on boot in such case)
[v1..v4 Changelog is at the very bottom here]
Our performance measurements show that the price of VDSO's clock_gettime() in a child time namespace is about 8% with a hot CPU cache and about 90% with a cold CPU cache. There is no performance regression for host processes outside time namespace on those tests.
We wrote two small benchmarks. The first one gettime_perf.c calls clock_gettime() in a loop for 3 seconds. It shows us performance with a hot CPU cache (more clock_gettime() cycles - the better):
| before | CONFIG_TIME_NS=n | host | inside timens
--------------------------------------------------------------
| 161822960 | 161147792 | 160187142 | 150584782
| 161891728 | 161489804 | 159914989 | 150417019
| 161891770 | 161098734 | 160123179 | 150601277
| 161687686 | 161114738 | 159874249 | 150243276
| 161247151 | 161411636 | 159096392 | 149637536
--------------------------------------------------------------
avg | 161708259 | 161252540 | 159839190 | 150296778
diff % | 100 | 99.7 | 98.8 | 92.9-------------------------------------------------------------
stdev % | 0.2 | 0.1 | 0.1 | 0.2
The gettime_perf_cold test does 10K iterations. In each iteration, it drops cpu caches for vdso pages, clflush is used for this, then it runs rdtsc(); clock_gettime; rdtsc(); and prints the number of tsc cycles.
(lesser tsc per cycle - the better):
| before | CONFIG_TIME_NS=n | host | inside timens
--------------------------------------------------------------
tsc | 434 | 433 | 437 | 477 stdev(tsc) | 5 | 5 | 5 | 3 diff (%) | 1 | 1 | 100.1 | 109
vdsotest results: https://gist.github.com/avagin/f290afb8b721ae0522a561d585f34de0
The numbers gathered on Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz.
v5 on github (if someone prefers git pull or git log):
https://github.com/0x7f454c46/linux/tree/timens-v5
v4: https://lkml.kernel.org/r/[email protected]
v3: https://lkml.kernel.org/r/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]/
RFC: https://lkml.kernel.org/r/[email protected]/
v3..v4 Changes:
* CLOCKE_NEWTIME is unshare()-only flag now (CLON_PIDFD took previous value)
* Addressing Jann Horn's feedback - we don't allow CLONE_THREAD or CLONE_VM together with CLONE_NEWTIME (thanks for spotting!)
* Addressing issues found by Thomas - removed unmaintainable CLOCK_TIMENS and introduced another call back into k_clock to get ktime instead of getting timespec and converting it (Patch 03)
* Renaming timens_offsets members to omit _offset postfix (thanks Cyrill for the suggestion)
* Suggestions, renaming and making code more maintainable from Thomas's feedback (thanks much!)
* Fixing out-of-bounds and other issues in procfs file (kudos Jann Horn)
* vdso_fault() can be called on a remote task by /proc/$pid/mem or process_vm_readv() - addressed by adding a slow-path with searching for owner's namespace (thanks for spotting this unobvious issue, Jann)
* Other nits by Jann Horn
v2..v3: Major changes:
* Simplify two VDSO images by using static_branch() in vclock_gettime() Removes unwanted conflicts with generic VDSO movement patches and simplifies things by dropping too invasive linker magic.
As an alternative to static_branch() we tested an attempt to introduce home-made dynamic patching called retcalls:
https://github.com/0x7f454c46/linux/commit/4cc0180f6d65
Considering some theoretical problems with toolchains, we decided to go with long well-tested nop-patching in static_branch(). Though, it was needed to provide backend for relative code.
* address Thomas' comments.
* add sanity checks for offsets:
- the current clock time in a namespace has to be in [0, KTIME_MAX / 2). KTIME_MAX is divided by two here to be sure that the KTIME_MAX limit is still unreachable.
v1..v2: There are two major changes:
* Two versions of the VDSO library to avoid a performance penalty for host tasks outside time namespace (as suggested by Andy and Thomas).
As it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.
Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation VDSO code.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
* Allow to set clock offsets for a namespace only before any processes appear in it.
Now a time namespace looks similar to a pid namespace in a way how it is
created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace.
This scheme allows to create a new time namespaces, set clock offsets and then populate the namespace with processes.
Andrei Vagin (25):
ns: Introduce Time Namespace
timens: Add timens_offsets
posix-clocks: Rename the clock_get() into clock_get_timespec()
posix-clocks: Rename *_clock_get() functions into
*_clock_get_timespec()
alarmtimer: Rename gettime() callback to get_ktime()
alarmtimer: Provide get_timespec() callback
posix-clocks: Introduce clock_get_ktime() callback
posix-timers: Use clock_get_ktime() in common_timer_get()
posix-clocks: Introduce CLOCK_MONOTONIC time namespace offsets
posix-clocks: Introduce CLOCK_BOOTTIME time namespace offset
kernel: Add do_timens_ktime_to_host() helper
timerfd: Make timerfd_settime() time namespace aware
posix-timers: Make timer_settime() time namespace aware
alarmtimer: Make nanosleep time namespace aware
hrtimers: Prepare hrtimer_nanosleep() for time namespaces
posix-timers: Make clock_nanosleep() time namespace aware x86/vdso: Add offsets page in vvar
vdso: Introduce vdso_static_branch_unlikely()
x86/vdso: Enable static branches for the timens vdso fs/proc: Introduce /proc/pid/timens_offsets selftest/timens: Add a test for timerfd
selftest/timens: Add a test for clock_nanosleep() selftest/timens: Add timer offsets test
selftests/timens: Add a simple perf test for clock_gettime() selftest/timens: Check that a right vdso is mapped after fork and exec
Dmitry Safonov (12):
fd/proc: Respect boottime inside time namespace for /proc/uptime x86/vdso2c: Correct err messages on file opening x86/vdso2c: Convert iterator to unsigned x86/vdso/Makefile: Add vobjs32
x86/vdso: Restrict splitting VVAR VMA
x86/vdso: Rename vdso_image {.data=>.text}
x86/vdso: Allocate timens vdso
x86/vdso: Switch image on setns()/clone()
x86/vdso2c: Process jump tables
posix-clocks: Add align for timens_offsets selftest/timens: Add Time Namespace test for supported clocks selftest/timens: Add procfs selftest
MAINTAINERS | 3 + arch/Kconfig | 5 + arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 15 +- arch/x86/entry/vdso/vdso-layout.lds.S | 10 +- arch/x86/entry/vdso/vdso2c.c | 7 +- arch/x86/entry/vdso/vdso2c.h | 22 +- arch/x86/entry/vdso/vma.c | 177 +++++++- arch/x86/include/asm/jump_label.h | 14 + arch/x86/include/asm/vdso.h | 14 +- arch/x86/kernel/jump_label.c | 14 + fs/proc/base.c | 95 +++++ fs/proc/namespaces.c | 4 + fs/proc/uptime.c | 3 + fs/timerfd.c | 3 + include/linux/hrtimer.h | 2 +- include/linux/jump_label.h | 8 + include/linux/nsproxy.h | 2 + include/linux/posix-timers.h | 3 + include/linux/proc_ns.h | 3 + include/linux/time_namespace.h | 114 ++++++ include/linux/timens_offsets.h | 18 + include/linux/user_namespace.h | 1 + include/uapi/linux/sched.h | 6 + init/Kconfig | 9 + kernel/Makefile | 1 + kernel/fork.c | 16 +- kernel/nsproxy.c | 41 +- kernel/time/alarmtimer.c | 68 +++- kernel/time/hrtimer.c | 8 +- kernel/time/posix-clock.c | 8 +- kernel/time/posix-cpu-timers.c | 32 +- kernel/time/posix-stubs.c | 15 +- kernel/time/posix-timers.c | 89 ++-- kernel/time/posix-timers.h | 7 +- kernel/time_namespace.c | 385 ++++++++++++++++++ lib/vdso/gettimeofday.c | 53 +++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/timens/.gitignore | 8 + tools/testing/selftests/timens/Makefile | 12 + .../selftests/timens/clock_nanosleep.c | 102 +++++ tools/testing/selftests/timens/config | 1 + tools/testing/selftests/timens/exec.c | 93 +++++ tools/testing/selftests/timens/gettime_perf.c | 101 +++++ .../selftests/timens/gettime_perf_cold.c | 160 ++++++++ tools/testing/selftests/timens/log.h | 26 ++ tools/testing/selftests/timens/procfs.c | 144 +++++++ tools/testing/selftests/timens/timens.c | 185 +++++++++ tools/testing/selftests/timens/timens.h | 63 +++ tools/testing/selftests/timens/timer.c | 118 ++++++ tools/testing/selftests/timens/timerfd.c | 129 ++++++ 51 files changed, 2304 insertions(+), 115 deletions(-) create mode 100644 include/linux/time_namespace.h create mode 100644 include/linux/timens_offsets.h create mode 100644 kernel/time_namespace.c create mode 100644 tools/testing/selftests/timens/.gitignore create mode 100644 tools/testing/selftests/timens/Makefile create mode 100644 tools/testing/selftests/timens/clock_nanosleep.c create mode 100644 tools/testing/selftests/timens/config create mode 100644 tools/testing/selftests/timens/exec.c create mode 100644 tools/testing/selftests/timens/gettime_perf.c create mode 100644 tools/testing/selftests/timens/gettime_perf_cold.c create mode 100644 tools/testing/selftests/timens/log.h create mode 100644 tools/testing/selftests/timens/procfs.c create mode 100644 tools/testing/selftests/timens/timens.c create mode 100644 tools/testing/selftests/timens/timens.h create mode 100644 tools/testing/selftests/timens/timer.c create mode 100644 tools/testing/selftests/timens/timerfd.c
Signature · 1 line
2.22.0
PATCH v5 1/37 ns: Introduce Time Namespace
From: Andrei Vagin <[email protected]>
Time Namespace isolates clock values.
The kernel provides access to several clocks CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, etc.
CLOCK_REALTIME
System-wide clock that measures real (i.e., wall-clock) time.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_BOOTTIME
Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
For many users, the time namespace means the ability to changes date and time in a container (CLOCK_REALTIME).
But in a context of the checkpoint/restore functionality, monotonic and bootime clocks become interesting. Both clocks are monotonic with unspecified staring points. These clocks are widely used to measure time slices and set timers. After restoring or migrating processes, we have to guarantee that they never go backward. In an ideal case, the behavior of these clocks should be the same as for a case when a whole system is suspended. All this means that we need to be able to set CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks, what can be done by adding per-namespace offsets for clocks.
A time namespace is similar to a pid namespace in a way how it is
created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace.
This scheme allows setting clock offsets for a namespace, before any processes appear in it.
All available clone flags have been used, so CLONE_NEWTIME uses the highest bit of CSIGNAL. It means that we can use it with the unshare system call only. Rith now, this works for us, because time namespace offsets can be set only when a new time namespace is not populated. In a future, we will have the clone3 system call [1] which will allow to use the CSIGNAL mask for clone flags.
[1]: httmps://lkml.kernel.org/r/[email protected]
--- MAINTAINERS | 2 + fs/proc/namespaces.c | 4 + include/linux/nsproxy.h | 2 + include/linux/proc_ns.h | 3 + include/linux/time_namespace.h | 69 +++++++++++ include/linux/user_namespace.h | 1 + include/uapi/linux/sched.h | 6 + init/Kconfig | 7 ++ kernel/Makefile | 1 + kernel/fork.c | 16 ++- kernel/nsproxy.c | 41 +++++-- kernel/time_namespace.c | 217 +++++++++++++++++++++++++++++++++ 12 files changed, 359 insertions(+), 10 deletions(-) create mode 100644 include/linux/time_namespace.h create mode 100644 kernel/time_namespace.c
MAINTAINERS +2 -0
@@ -12836,6 +12836,8 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core S: Maintained F: fs/timerfd.c F: include/linux/timer* +F: include/linux/time_namespace.h +F: kernel/time_namespace.c F: kernel/time/*timer* POWER MANAGEMENT CORE
fs/proc/namespaces.c +4 -0
@@ -33,6 +33,10 @@ static const struct proc_ns_operations *ns_entries[] = {#ifdef CONFIG_CGROUPS &cgroupns_operations,#endif +#ifdef CONFIG_TIME_NS + &timens_operations, + &timens_for_children_operations, +#endif }; static const char *proc_ns_get_link(struct dentry *dentry,
include/linux/nsproxy.h +2 -0
@@ -35,6 +35,8 @@ struct nsproxy {struct mnt_namespace *mnt_ns;struct pid_namespace *pid_ns_for_children;struct net *net_ns; +struct time_namespace *time_ns; +struct time_namespace *time_ns_for_children;struct cgroup_namespace *cgroup_ns; };extern struct nsproxy init_nsproxy;
include/linux/proc_ns.h +3 -0
@@ -32,6 +32,8 @@ extern const struct proc_ns_operations pidns_for_children_operations;extern const struct proc_ns_operations userns_operations;extern const struct proc_ns_operations mntns_operations;extern const struct proc_ns_operations cgroupns_operations; +extern const struct proc_ns_operations timens_operations; +extern const struct proc_ns_operations timens_for_children_operations;/* * We always define these enumerators
@@ -43,6 +45,7 @@ enum {PROC_USER_INIT_INO =0xEFFFFFFDU ,PROC_PID_INIT_INO =0xEFFFFFFCU ,PROC_CGROUP_INIT_INO =0xEFFFFFFBU , + PROC_TIME_INIT_INO = 0xEFFFFFFAU, };#ifdef CONFIG_PROC_FS
include/linux/time_namespace.h new file +69 -0
69 lines not on this page · raw message
include/linux/user_namespace.h +1 -0
@@ -45,6 +45,7 @@ enum ucount_type {UCOUNT_NET_NAMESPACES ,UCOUNT_MNT_NAMESPACES ,UCOUNT_CGROUP_NAMESPACES , +UCOUNT_TIME_NAMESPACES , #ifdef CONFIG_INOTIFY_USERUCOUNT_INOTIFY_INSTANCES , UCOUNT_INOTIFY_WATCHES,
include/uapi/linux/sched.h +6 -0
@@ -33,6 +33,12 @@#define CLONE_NEWNET 0x40000000/* New network namespace */ #define CLONE_IO 0x80000000/* Clone io context */ +/* +* cloning flags intersect with CSIGNAL so can be used with unshare and clone3 +* syscalls only: +*/ +#define CLONE_NEWTIME 0x00000080/* New time namespace */ +/* * Arguments for the clone3 syscall */
init/Kconfig +7 -0
@@ -1069,6 +1069,13 @@ config UTS_NS In this namespace tasks see different info provided with the uname() system call +config TIME_NS + bool "TIME namespace" + default y + help + In this namespace boottime and monotonic clocks can be set. + The time will keep going with the same pace. + config IPC_NS bool "IPC namespace" depends on (SYSVIPC || POSIX_MQUEUE)
kernel/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/ obj-$(CONFIG_UTS_NS) += utsname.o +obj-$(CONFIG_TIME_NS) += time_namespace.o obj-$(CONFIG_USER_NS) += user_namespace.o obj-$(CONFIG_PID_NS) += pid_namespace.o obj-$(CONFIG_IKCONFIG) += configs.o
kernel/fork.c +13 -3
@@ -1777,6 +1777,7 @@ static __latent_entropy struct task_struct *copy_process(struct multiprocess_signals delayed;struct file *pidfile =NULL ;u64 clone_flags = args->flags; +struct nsproxy *nsp = current->nsproxy;/* * Don't allow sharing the root directory with processes in a different
@@ -1819,8 +1820,16 @@ static __latent_entropy struct task_struct *copy_process(*/ if (clone_flags &CLONE_THREAD ) {if ((clone_flags & (CLONE_NEWUSER |CLONE_NEWPID )) || - (task_active_pid_ns (current) != - current->nsproxy->pid_ns_for_children)) + (task_active_pid_ns (current) != nsp->pid_ns_for_children)) +return ERR_PTR (-EINVAL ); + } + +/* +* If the new process will be in a different time namespace +* do not allow it to share VM or a thread group with the forking task. +*/ +if (clone_flags & (CLONE_THREAD |CLONE_VM )) { +if (nsp->time_ns != nsp->time_ns_for_children)return ERR_PTR (-EINVAL ); }
@@ -2707,7 +2716,8 @@ static int check_unshare_flags(unsigned long unshare_flags)if (unshare_flags & ~(CLONE_THREAD |CLONE_FS |CLONE_NEWNS |CLONE_SIGHAND |CLONE_VM |CLONE_FILES |CLONE_SYSVSEM |CLONE_NEWUTS |CLONE_NEWIPC |CLONE_NEWNET | -CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP )) +CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP | +CLONE_NEWTIME ))return -EINVAL ;/* * Not implemented, but pretend it works if there is nothing
kernel/nsproxy.c +34 -7
88 lines not on this page · raw message
kernel/time_namespace.c new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 2/37 timens: Add timens_offsets
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Introduce offsets for time namespace. They will contain an adjustment needed to convert clocks to/from host's.
Allocate one page for each time namespace that will be premapped into userspace among vvar pages.
--- MAINTAINERS | 1 + include/linux/time_namespace.h | 1 + include/linux/timens_offsets.h | 8 ++++++++ kernel/time_namespace.c | 14 ++++++++++++-- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 include/linux/timens_offsets.h
MAINTAINERS +1 -0
@@ -12837,6 +12837,7 @@ S: Maintained F: fs/timerfd.c F: include/linux/timer* F: include/linux/time_namespace.h +F: include/linux/timens_offsets.h F: kernel/time_namespace.c F: kernel/time/*timer*
include/linux/time_namespace.h +1 -0
@@ -8,6 +8,7 @@#include <linux/nsproxy.h> #include <linux/ns_common.h> #include <linux/err.h> +#include <linux/timens_offsets.h> struct user_namespace ;extern struct user_namespace init_user_ns;
include/linux/timens_offsets.h new file +8 -0
@@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_TIME_OFFSETS_H +#define _LINUX_TIME_OFFSETS_H + +struct timens_offsets { +}; + +#endif
kernel/time_namespace.c +12 -2
@@ -14,6 +14,7 @@#include <linux/slab.h> #include <linux/cred.h> #include <linux/err.h> +#include <linux/mm.h> static struct ucounts *inc_time_namespaces (struct user_namespace *ns) {
@@ -47,6 +48,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns, {struct time_namespace *ns;struct ucounts *ucounts; +struct page *page;int err; err = -ENOSPC ;
@@ -59,15 +61,22 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,if (!ns) goto fail_dec; + page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!page) + goto fail_free; + ns->offsets =page_address (page); +BUILD_BUG_ON (sizeof (*ns->offsets) >PAGE_SIZE ); + err =ns_alloc_inum (&ns->ns);if (err) - goto fail_free; + goto fail_page; ns->ucounts = ucounts; ns->ns.ops = &timens_operations; ns->user_ns =get_user_ns (user_ns);return ns; - +fail_page: +free_page ((unsigned long )ns->offsets); fail_free:kfree (ns); fail_dec:
@@ -95,6 +104,7 @@ void free_time_ns(struct kref *kref)struct time_namespace *ns; ns = container_of(kref, struct time_namespace, kref); +free_page ((unsigned long )ns->offsets);dec_time_namespaces (ns->ucounts);put_user_ns (ns->user_ns);ns_free_inum (&ns->ns);
Signature · 1 line
2.22.0
PATCH v5 3/37 posix-clocks: Rename the clock_get() into clock_get_timespec()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format, rather than in (struct timespec).
Rename clock_get() callback into clock_get_timespec() as a preparation for introducing clock_get_ktime().
--- kernel/time/alarmtimer.c | 4 ++-- kernel/time/posix-clock.c | 8 ++++---- kernel/time/posix-cpu-timers.c | 32 ++++++++++++++++---------------- kernel/time/posix-timers.c | 22 +++++++++++----------- kernel/time/posix-timers.h | 4 ++-- 5 files changed, 35 insertions(+), 35 deletions(-)
kernel/time/alarmtimer.c +2 -2
@@ -644,7 +644,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp }/** -* alarm_clock_get - posix clock_get interface +* alarm_clock_get - posix clock_get_timespec interface * @which_clock: clockid * @tp: timespec to fill. *
@@ -824,7 +824,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, - .clock_get = alarm_clock_get, + .clock_get_timespec = alarm_clock_get, .timer_create = alarm_timer_create, .timer_set = common_timer_set, .timer_del = common_timer_del,
kernel/time/posix-clock.c +4 -4
@@ -315,8 +315,8 @@ static int pc_clock_settime(clockid_t id, const struct timespec64 *ts) }const struct k_clock clock_posix_dynamic = { - .clock_getres = pc_clock_getres, - .clock_set = pc_clock_settime, - .clock_get = pc_clock_gettime, - .clock_adj = pc_clock_adjtime, + .clock_getres = pc_clock_getres, + .clock_set = pc_clock_settime, + .clock_get_timespec = pc_clock_gettime, + .clock_adj = pc_clock_adjtime, };
kernel/time/posix-cpu-timers.c +16 -16
@@ -1417,26 +1417,26 @@ static int thread_cpu_timer_create(struct k_itimer *timer) }const struct k_clock clock_posix_cpu = { - .clock_getres = posix_cpu_clock_getres, - .clock_set = posix_cpu_clock_set, - .clock_get = posix_cpu_clock_get, - .timer_create = posix_cpu_timer_create, - .nsleep = posix_cpu_nsleep, - .timer_set = posix_cpu_timer_set, - .timer_del = posix_cpu_timer_del, - .timer_get = posix_cpu_timer_get, - .timer_rearm = posix_cpu_timer_rearm, + .clock_getres = posix_cpu_clock_getres, + .clock_set = posix_cpu_clock_set, + .clock_get_timespec = posix_cpu_clock_get, + .timer_create = posix_cpu_timer_create, + .nsleep = posix_cpu_nsleep, + .timer_set = posix_cpu_timer_set, + .timer_del = posix_cpu_timer_del, + .timer_get = posix_cpu_timer_get, + .timer_rearm = posix_cpu_timer_rearm, };const struct k_clock clock_process = { - .clock_getres = process_cpu_clock_getres, - .clock_get = process_cpu_clock_get, - .timer_create = process_cpu_timer_create, - .nsleep = process_cpu_nsleep, + .clock_getres = process_cpu_clock_getres, + .clock_get_timespec = process_cpu_clock_get, + .timer_create = process_cpu_timer_create, + .nsleep = process_cpu_nsleep, };const struct k_clock clock_thread = { - .clock_getres = thread_cpu_clock_getres, - .clock_get = thread_cpu_clock_get, - .timer_create = thread_cpu_timer_create, + .clock_getres = thread_cpu_clock_getres, + .clock_get_timespec = thread_cpu_clock_get, + .timer_create = thread_cpu_timer_create, };
kernel/time/posix-timers.c +11 -11
@@ -667,7 +667,7 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)* The timespec64 based conversion is suboptimal, but it's not * worth to implement yet another callback. */ - kc->clock_get (timr->it_clock, &ts64); + kc->clock_get_timespec (timr->it_clock, &ts64); now =timespec64_to_ktime (ts64);/*
@@ -781,7 +781,7 @@ static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,* Posix magic: Relative CLOCK_REALTIME timers are not affected by * clock modifications, so they become CLOCK_MONOTONIC based under the * hood. See hrtimer_init(). Update timr->kclock, so the generic -* functions which use timr->kclock->clock_get() work. +* functions which use timr->kclock->clock_get_timespec() work. * * Note: it_clock stays unmodified, because the next timer_set() might * use ABSTIME, so it needs to switch back.
@@ -1032,7 +1032,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,if (!kc)return -EINVAL ; - error = kc->clock_get (which_clock, &kernel_tp); + error = kc->clock_get_timespec (which_clock, &kernel_tp);if (!error &&put_timespec64 (&kernel_tp, tp)) error = -EFAULT ;
@@ -1114,7 +1114,7 @@ SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,if (!kc)return -EINVAL ; - err = kc->clock_get (which_clock, &ts); + err = kc->clock_get_timespec (which_clock, &ts);if (!err &&put_old_timespec32 (&ts, tp)) err = -EFAULT ;
@@ -1226,7 +1226,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_clock_realtime_get, + .clock_get_timespec = posix_clock_realtime_get, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1243,7 +1243,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_ktime_get_ts, + .clock_get_timespec = posix_ktime_get_ts, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1258,22 +1258,22 @@ static const struct k_clock clock_monotonic = {static const struct k_clock clock_monotonic_raw = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_monotonic_raw, + .clock_get_timespec = posix_get_monotonic_raw, };static const struct k_clock clock_realtime_coarse = { .clock_getres = posix_get_coarse_res, - .clock_get = posix_get_realtime_coarse, + .clock_get_timespec = posix_get_realtime_coarse, };static const struct k_clock clock_monotonic_coarse = { .clock_getres = posix_get_coarse_res, - .clock_get = posix_get_monotonic_coarse, + .clock_get_timespec = posix_get_monotonic_coarse, };static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_tai, + .clock_get_timespec = posix_get_tai, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1288,7 +1288,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_boottime, + .clock_get_timespec = posix_get_boottime, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
kernel/time/posix-timers.h +2 -2
@@ -6,8 +6,8 @@ struct k_clock { struct timespec64 *tp);int (*clock_set)(const clockid_t which_clock,const struct timespec64 *tp); -int (*clock_get)(const clockid_t which_clock, -struct timespec64 *tp); +int (*clock_get_timespec)(const clockid_t which_clock, +struct timespec64 *tp);int (*clock_adj)(const clockid_t which_clock,struct __kernel_timex *tx);int (*timer_create)(struct k_itimer *timer); int (*nsleep)(const clockid_t which_clock, int flags,
Signature · 1 line
2.22.0
PATCH v5 4/37 posix-clocks: Rename *_clock_get() functions into *_clock_get_timespec()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format in (struct k_clock).
As a preparation ground for introducing clock_get_ktime(), the original callback clock_get() was renamed into clock_get_timespec(). Reflect the renaming into callbacks realizations.
--- kernel/time/alarmtimer.c | 6 +++--- kernel/time/posix-timers.c | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-)
kernel/time/alarmtimer.c +3 -3
@@ -644,13 +644,13 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp }/** -* alarm_clock_get - posix clock_get_timespec interface +* alarm_clock_get_timespec - posix clock_get_timespec interface * @which_clock: clockid * @tp: timespec to fill. * * Provides the underlying alarm base time. */ -static int alarm_clock_get (clockid_t which_clock,struct timespec64 *tp) +static int alarm_clock_get_timespec (clockid_t which_clock,struct timespec64 *tp) {struct alarm_base *base = &alarm_bases[clock2alarm (which_clock)];
@@ -824,7 +824,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, - .clock_get_timespec = alarm_clock_get, + .clock_get_timespec = alarm_clock_get_timespec, .timer_create = alarm_timer_create, .timer_set = common_timer_set, .timer_del = common_timer_del,
kernel/time/posix-timers.c +9 -8
@@ -165,7 +165,8 @@ static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) }/* Get clock_realtime */ -static int posix_clock_realtime_get (clockid_t which_clock,struct timespec64 *tp) +static int +posix_clock_realtime_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_real_ts64 (tp);return 0 ;
@@ -187,7 +188,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,/* * Get monotonic time for posix timers */ -static int posix_ktime_get_ts (clockid_t which_clock,struct timespec64 *tp) +static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;
@@ -222,13 +223,13 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *return 0 ; } -static int posix_get_boottime (const clockid_t which_clock,struct timespec64 *tp) +static int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp);return 0 ; } -static int posix_get_tai (clockid_t which_clock,struct timespec64 *tp) +static int posix_get_tai_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_clocktai_ts64 (tp);return 0 ;
@@ -1226,7 +1227,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_clock_realtime_get, + .clock_get_timespec = posix_clock_realtime_get_timespec, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1243,7 +1244,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_ktime_get_ts, + .clock_get_timespec = posix_get_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1273,7 +1274,7 @@ static const struct k_clock clock_monotonic_coarse = {static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_get_tai, + .clock_get_timespec = posix_get_tai_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1288,7 +1289,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_get_boottime, + .clock_get_timespec = posix_get_boottime_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
Signature · 1 line
2.22.0
PATCH v5 5/37 alarmtimer: Rename gettime() callback to get_ktime()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a tasks time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
struct alarm_base needs to follow the same name convention, so rename .gettime() callback into get_ktime() as a preparation for introducing get_timespec().
--- kernel/time/alarmtimer.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
kernel/time/alarmtimer.c +17 -17
@@ -36,13 +36,13 @@* struct alarm_base - Alarm timer bases * @lock: Lock for syncrhonized access to the base * @timerqueue: Timerqueue head managing the list of events -* @gettime: Function to read the time correlating to the base +* @get_ktime: Function to read the time correlating to the base * @base_clockid: clockid for the base */ static struct alarm_base {spinlock_t lock;struct timerqueue_head timerqueue; -ktime_t (*gettime)(void ); +ktime_t (*get_ktime)(void );clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ];
@@ -207,7 +207,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)spin_unlock_irqrestore (&base->lock, flags);if (alarm->function) - restart = alarm->function (alarm, base->gettime ()); + restart = alarm->function (alarm, base->get_ktime ());spin_lock_irqsave (&base->lock, flags);if (restart !=ALARMTIMER_NORESTART ) {
@@ -217,7 +217,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) }spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_fired (alarm, base->gettime ()); +trace_alarmtimer_fired (alarm, base->get_ktime ());return ret; }
@@ -225,7 +225,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)ktime_t alarm_expires_remaining (const struct alarm *alarm) {struct alarm_base *base = &alarm_bases[alarm->type]; -return ktime_sub (alarm->node.expires, base->gettime ()); +return ktime_sub (alarm->node.expires, base->get_ktime ()); }EXPORT_SYMBOL_GPL (alarm_expires_remaining);
@@ -270,7 +270,7 @@ static int alarmtimer_suspend(struct device *dev)spin_unlock_irqrestore (&base->lock, flags);if (!next)continue ; - delta =ktime_sub (next->expires, base->gettime ()); + delta =ktime_sub (next->expires, base->get_ktime ());if (!min || (delta < min)) { expires = next->expires; min = delta;
@@ -364,7 +364,7 @@ void alarm_start(struct alarm *alarm, ktime_t start)hrtimer_start (&alarm->timer, alarm->node.expires,HRTIMER_MODE_ABS );spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_start (alarm, base->gettime ()); +trace_alarmtimer_start (alarm, base->get_ktime ()); }EXPORT_SYMBOL_GPL (alarm_start);
@@ -377,7 +377,7 @@ void alarm_start_relative(struct alarm *alarm, ktime_t start) {struct alarm_base *base = &alarm_bases[alarm->type]; - start =ktime_add_safe (start, base->gettime ()); + start =ktime_add_safe (start, base->get_ktime ());alarm_start (alarm, start); }EXPORT_SYMBOL_GPL (alarm_start_relative);
@@ -414,7 +414,7 @@ int alarm_try_to_cancel(struct alarm *alarm)alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_cancel (alarm, base->gettime ()); +trace_alarmtimer_cancel (alarm, base->get_ktime ());return ret; }EXPORT_SYMBOL_GPL (alarm_try_to_cancel);
@@ -474,7 +474,7 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) {struct alarm_base *base = &alarm_bases[alarm->type]; -return alarm_forward (alarm, base->gettime (), interval); +return alarm_forward (alarm, base->get_ktime (), interval); }EXPORT_SYMBOL_GPL (alarm_forward_now);
@@ -500,7 +500,7 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)return ; } - delta =ktime_sub (absexp, base->gettime ()); + delta =ktime_sub (absexp, base->get_ktime ());spin_lock_irqsave (&freezer_delta_lock, flags);if (!freezer_delta || (delta < freezer_delta)) {
@@ -619,7 +619,7 @@ static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,struct alarm_base *base = &alarm_bases[alarm->type];if (!absolute) - expires =ktime_add_safe (expires, base->gettime ()); + expires =ktime_add_safe (expires, base->get_ktime ());if (sigev_none) alarm->node.expires = expires;else
@@ -657,7 +657,7 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpif (!alarmtimer_get_rtcdev ())return -EINVAL ; - *tp =ktime_to_timespec64 (base->gettime ()); + *tp =ktime_to_timespec64 (base->get_ktime ());return 0 ; }
@@ -734,7 +734,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct timespec64 rmt;ktime_t rem; - rem =ktime_sub (absexp, alarm_bases[type].gettime ()); + rem =ktime_sub (absexp, alarm_bases[type].get_ktime ());if (rem <=0 )return 0 ;
@@ -803,7 +803,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags, exp =timespec64_to_ktime (*tsreq);/* Convert (if necessary) to absolute time */ if (flags !=TIMER_ABSTIME ) { -ktime_t now = alarm_bases[type].gettime (); +ktime_t now = alarm_bases[type].get_ktime (); exp =ktime_add_safe (now, exp); }
@@ -868,9 +868,9 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; - alarm_bases[ALARM_REALTIME ].gettime = &ktime_get_real; + alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; alarm_bases[ALARM_BOOTTIME ].base_clockid =CLOCK_BOOTTIME ; - alarm_bases[ALARM_BOOTTIME ].gettime = &ktime_get_boottime; + alarm_bases[ALARM_BOOTTIME ].get_ktime = &ktime_get_boottime;for (i =0 ; i <ALARM_NUMTYPE ; i++) {timerqueue_init_head (&alarm_bases[i].timerqueue);spin_lock_init (&alarm_bases[i].lock);
Signature · 1 line
2.22.0
PATCH v5 6/37 alarmtimer: Provide get_timespec() callback
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
Wire up alarm bases with get_timespec().
--- include/linux/posix-timers.h | 3 +++ kernel/time/alarmtimer.c | 8 ++++++-- kernel/time/posix-timers.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-)
include/linux/posix-timers.h +3 -0
@@ -127,4 +127,7 @@ void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new);void posixtimer_rearm (struct kernel_siginfo *info); + +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp); +int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp);#endif
kernel/time/alarmtimer.c +6 -2
@@ -37,12 +37,15 @@* @lock: Lock for syncrhonized access to the base * @timerqueue: Timerqueue head managing the list of events * @get_ktime: Function to read the time correlating to the base +* @get_timespec: Function to read the namespace time correlating to the base * @base_clockid: clockid for the base */ static struct alarm_base {spinlock_t lock;struct timerqueue_head timerqueue;ktime_t (*get_ktime)(void ); +int (*get_timespec)(const clockid_t which_clock, +struct timespec64 *tp);clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ];
@@ -657,8 +660,7 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpif (!alarmtimer_get_rtcdev ())return -EINVAL ; - *tp =ktime_to_timespec64 (base->get_ktime ()); -return 0 ; +return base->get_timespec (base->base_clockid, tp); }/**
@@ -869,8 +871,10 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; + alarm_bases[ALARM_REALTIME ].get_timespec = posix_get_timespec, alarm_bases[ALARM_BOOTTIME ].base_clockid =CLOCK_BOOTTIME ; alarm_bases[ALARM_BOOTTIME ].get_ktime = &ktime_get_boottime; + alarm_bases[ALARM_BOOTTIME ].get_timespec = posix_get_boottime_timespec;for (i =0 ; i <ALARM_NUMTYPE ; i++) {timerqueue_init_head (&alarm_bases[i].timerqueue);spin_lock_init (&alarm_bases[i].lock);
kernel/time/posix-timers.c +2 -2
@@ -188,7 +188,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,/* * Get monotonic time for posix timers */ -static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;
@@ -223,7 +223,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *return 0 ; } -static int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) +int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp);return 0 ;
Signature · 1 line
2.22.0
PATCH v5 7/37 posix-clocks: Introduce clock_get_ktime() callback
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The callsite in common_timer_get() has already a comment:
/*
* The timespec64 based conversion is suboptimal, but it's not
* worth to implement yet another callback.
*/
kc->clock_get(timr->it_clock, &ts64);
now = timespec64_to_ktime(ts64);The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format.
--- kernel/time/alarmtimer.c | 19 ++++++++++++++++++- kernel/time/posix-timers.c | 26 +++++++++++++++++++++++++- kernel/time/posix-timers.h | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +18 -1
@@ -651,7 +651,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp* @which_clock: clockid * @tp: timespec to fill. * -* Provides the underlying alarm base time. +* Provides the underlying alarm base time in a tasks time namespace. */ static int alarm_clock_get_timespec (clockid_t which_clock,struct timespec64 *tp) {
@@ -663,6 +663,22 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpreturn base->get_timespec (base->base_clockid, tp); } +/** +* alarm_clock_get_ktime - posix clock_get_ktime interface +* @which_clock: clockid +* +* Provides the underlying alarm base time in the root namespace. +*/ +static ktime_t alarm_clock_get_ktime (clockid_t which_clock) +{ +struct alarm_base *base = &alarm_bases[clock2alarm (which_clock)]; + +if (!alarmtimer_get_rtcdev ()) +return -EINVAL ; + +return base->get_ktime (); +} +/** * alarm_timer_create - posix timer_create interface * @new_timer: k_itimer pointer to manage
@@ -826,6 +842,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, + .clock_get_ktime = alarm_clock_get_ktime, .clock_get_timespec = alarm_clock_get_timespec, .timer_create = alarm_timer_create, .timer_set = common_timer_set,
kernel/time/posix-timers.c +25 -1
@@ -172,6 +172,11 @@ posix_clock_realtime_get_timespec(clockid_t which_clock, struct timespec64 *tp)return 0 ; } +static ktime_t posix_clock_realtime_get_ktime (clockid_t which_clock) +{ +return ktime_get_real (); +} +/* Set clock_realtime */ static int posix_clock_realtime_set (const clockid_t which_clock,const struct timespec64 *tp)
@@ -194,6 +199,11 @@ int posix_get_timespec(clockid_t which_clock, struct timespec64 *tp)return 0 ; } +static ktime_t posix_get_ktime (clockid_t which_clock) +{ +return ktime_get (); +} +/* * Get monotonic-raw time for posix timers */
@@ -229,12 +239,22 @@ int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *return 0 ; } +static ktime_t posix_get_boottime_ktime (const clockid_t which_clock) +{ +return ktime_get_boottime (); +} +static int posix_get_tai_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_clocktai_ts64 (tp);return 0 ; } +static ktime_t posix_get_tai_ktime (clockid_t which_clock) +{ +return ktime_get_clocktai (); +} +static int posix_get_hrtimer_res (clockid_t which_clock,struct timespec64 *tp) { tp->tv_sec =0 ;
@@ -782,7 +802,7 @@ static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,* Posix magic: Relative CLOCK_REALTIME timers are not affected by * clock modifications, so they become CLOCK_MONOTONIC based under the * hood. See hrtimer_init(). Update timr->kclock, so the generic -* functions which use timr->kclock->clock_get_timespec() work. +* functions which use timr->kclock->clock_get_*() work. * * Note: it_clock stays unmodified, because the next timer_set() might * use ABSTIME, so it needs to switch back.
@@ -1228,6 +1248,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_clock_realtime_get_timespec, + .clock_get_ktime = posix_clock_realtime_get_ktime, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1245,6 +1266,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_get_timespec, + .clock_get_ktime = posix_get_ktime, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1274,6 +1296,7 @@ static const struct k_clock clock_monotonic_coarse = {static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, + .clock_get_ktime = posix_get_tai_ktime, .clock_get_timespec = posix_get_tai_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create,
@@ -1289,6 +1312,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, + .clock_get_ktime = posix_get_boottime_ktime, .clock_get_timespec = posix_get_boottime_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create,
kernel/time/posix-timers.h +3 -0
@@ -6,8 +6,11 @@ struct k_clock { struct timespec64 *tp);int (*clock_set)(const clockid_t which_clock,const struct timespec64 *tp); +/* Returns the clock value in the current time namespace. */ int (*clock_get_timespec)(const clockid_t which_clock,struct timespec64 *tp); +/* Returns the clock value in the root time namespace. */ +ktime_t (*clock_get_ktime)(const clockid_t which_clock);int (*clock_adj)(const clockid_t which_clock,struct __kernel_timex *tx);int (*timer_create)(struct k_itimer *timer); int (*nsleep)(const clockid_t which_clock, int flags,
Signature · 1 line
2.22.0
PATCH v5 8/37 posix-timers: Use clock_get_ktime() in common_timer_get()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Now, when the clock_get_ktime() callback exists, the suboptimal timespec64-based conversion can be removed from common_timer_get().
--- kernel/time/posix-timers.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/time/posix-timers.c +1 -7
@@ -666,7 +666,6 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) {const struct k_clock *kc = timr->kclock;ktime_t now, remaining, iv; -struct timespec64 ts64;bool sig_none; sig_none = timr->it_sigev_notify ==SIGEV_NONE ;
@@ -684,12 +683,7 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)return ; } -/* -* The timespec64 based conversion is suboptimal, but it's not -* worth to implement yet another callback. -*/ - kc->clock_get_timespec (timr->it_clock, &ts64); - now =timespec64_to_ktime (ts64); + now = kc->clock_get_ktime (timr->it_clock);/* * When a requeue is pending or this is a SIGEV_NONE timer move the
Signature · 1 line
2.22.0
PATCH v5 9/37 posix-clocks: Introduce CLOCK_MONOTONIC time namespace offsets
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Add monotonic time virtualisation for time namespace. Introduce timespec for monotionic clock into timens offsets and wire clock_gettime() syscall.
--- include/linux/time_namespace.h | 8 ++++++++ include/linux/timens_offsets.h | 1 + kernel/time/posix-stubs.c | 2 ++ kernel/time/posix-timers.c | 4 ++++ 4 files changed, 15 insertions(+)
include/linux/time_namespace.h +8 -0
@@ -40,6 +40,13 @@ static inline void put_time_ns(struct time_namespace *ns) kref_put(&ns->kref, free_time_ns); } +static inline void timens_add_monotonic (struct timespec64 *ts) +{ +struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + +if (ns_offsets) + *ts =timespec64_add (*ts, ns_offsets->monotonic); +}#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns)
@@ -65,6 +72,7 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsreturn 0 ; } +static inline void timens_add_monotonic (struct timespec64 *ts) {}#endif #endif /* _LINUX_TIMENS_H */
include/linux/timens_offsets.h +1 -0
@@ -3,6 +3,7 @@#define _LINUX_TIME_OFFSETS_Hstruct timens_offsets { +struct timespec64 monotonic; };#endif
kernel/time/posix-stubs.c +2 -0
@@ -14,6 +14,7 @@#include <linux/ktime.h> #include <linux/timekeeping.h> #include <linux/posix-timers.h> +#include <linux/time_namespace.h> #include <linux/compat.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
@@ -77,6 +78,7 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)break ;case CLOCK_MONOTONIC :ktime_get_ts64 (tp); +timens_add_monotonic (tp);break ;case CLOCK_BOOTTIME :ktime_get_boottime_ts64 (tp);
kernel/time/posix-timers.c +4 -0
@@ -30,6 +30,7 @@#include <linux/hashtable.h> #include <linux/compat.h> #include <linux/nospec.h> +#include <linux/time_namespace.h> #include "timekeeping.h" #include "posix-timers.h"
@@ -196,6 +197,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
@@ -210,6 +212,7 @@ static ktime_t posix_get_ktime(clockid_t which_clock)static int posix_get_monotonic_raw (clockid_t which_clock,struct timespec64 *tp) {ktime_get_raw_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
@@ -224,6 +227,7 @@ static int posix_get_monotonic_coarse(clockid_t which_clock, struct timespec64 *tp) {ktime_get_coarse_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
Signature · 1 line
2.22.0
PATCH v5 10/37 posix-clocks: Introduce CLOCK_BOOTTIME time namespace offset
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Adds boottime virtualisation for time namespace. Introduce timespec for boottime clock into timens offsets and wire clock_gettime() syscall.
--- include/linux/time_namespace.h | 9 +++++++++ include/linux/timens_offsets.h | 1 + kernel/time/alarmtimer.c | 1 + kernel/time/posix-stubs.c | 1 + kernel/time/posix-timers.c | 1 + 5 files changed, 13 insertions(+)
include/linux/time_namespace.h +9 -0
@@ -48,6 +48,14 @@ static inline void timens_add_monotonic(struct timespec64 *ts) *ts = timespec64_add(*ts, ns_offsets->monotonic); } +static inline void timens_add_boottime (struct timespec64 *ts) +{ +struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + +if (ns_offsets) + *ts =timespec64_add (*ts, ns_offsets->boottime); +} +#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns) {
@@ -73,6 +81,7 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *ts }static inline void timens_add_monotonic (struct timespec64 *ts) {} +static inline void timens_add_boottime (struct timespec64 *ts) {}#endif #endif /* _LINUX_TIMENS_H */
include/linux/timens_offsets.h +1 -0
@@ -4,6 +4,7 @@struct timens_offsets {struct timespec64 monotonic; +struct timespec64 boottime; };#endif
kernel/time/alarmtimer.c +1 -0
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/time_namespace.h> #include "posix-timers.h"
kernel/time/posix-stubs.c +1 -0
@@ -82,6 +82,7 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)break ;case CLOCK_BOOTTIME :ktime_get_boottime_ts64 (tp); +timens_add_boottime (tp);break ;default :return -EINVAL ;
kernel/time/posix-timers.c +1 -0
@@ -240,6 +240,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp); +timens_add_boottime (tp);return 0 ; }
Signature · 1 line
2.22.0
PATCH v5 11/37 kernel: Add do_timens_ktime_to_host() helper
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The helper subtracts namespace's clock offset from the given time and checks that the result is in [0, KTIME_MAX].
--- include/linux/time_namespace.h | 17 ++++++++++++++ kernel/time_namespace.c | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
include/linux/time_namespace.h +17 -0
@@ -56,6 +56,18 @@ static inline void timens_add_boottime(struct timespec64 *ts) *ts = timespec64_add(*ts, ns_offsets->boottime); } +ktime_t do_timens_ktime_to_host (clockid_t clockid,ktime_t tim, +struct timens_offsets *offsets); +static inline ktime_t timens_ktime_to_host (clockid_t clockid,ktime_t tim) +{ +struct timens_offsets *offsets = current->nsproxy->time_ns->offsets; + +if (!offsets) +return tim; + +return do_timens_ktime_to_host (clockid, tim, offsets); +} +#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns) {
@@ -82,6 +94,11 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsstatic inline void timens_add_monotonic (struct timespec64 *ts) {}static inline void timens_add_boottime (struct timespec64 *ts) {} + +static inline ktime_t timens_ktime_to_host (clockid_t clockid,ktime_t tim) +{ +return tim; +}#endif #endif /* _LINUX_TIMENS_H */
kernel/time_namespace.c +43 -0
49 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 12/37 timerfd: Make timerfd_settime() time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
timerfd_settime() accepts an absolute value of the experation time if TFD_TIMER_ABSTIME is specified. This value is in task's time namespace and has to be converted to the host's time namespace.
--- fs/timerfd.c | 3 +++ 1 file changed, 3 insertions(+)
fs/timerfd.c +3 -0
@@ -26,6 +26,7 @@#include <linux/syscalls.h> #include <linux/compat.h> #include <linux/rcupdate.h> +#include <linux/time_namespace.h> struct timerfd_ctx { union {
@@ -196,6 +197,8 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags, }if (texp !=0 ) { +if (flags &TFD_TIMER_ABSTIME ) + texp =timens_ktime_to_host (clockid, texp);if (isalarm (ctx)) {if (flags &TFD_TIMER_ABSTIME )alarm_start (&ctx->t.alarm, texp);
Signature · 1 line
2.22.0
PATCH v5 13/37 posix-timers: Make timer_settime() time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Wire timer_settime() syscall into time namespace virtualization.
sys_timer_settime() calls the ktime->timer_set() callback. Right now, common_timer_set() is the only implementation for the callback.
There user-supplied timer's value is converted from timespec64 to ktime and then timens_ktime_to_host() can be used to convert namespace's time to the host time.
Inside a time namespace kernel's time differ on a fixed offset from a user-supplied, but only absolute values (TIMER_ABSTIME) must be converted.
--- kernel/time/posix-timers.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/time/posix-timers.c +2 -0
@@ -857,6 +857,8 @@ int common_timer_set(struct k_itimer *timr, int flags, timr->it_interval =timespec64_to_ktime (new_setting->it_interval); expires =timespec64_to_ktime (new_setting->it_value); +if (flags &TIMER_ABSTIME ) + expires =timens_ktime_to_host (timr->it_clock, expires); sigev_none = timr->it_sigev_notify ==SIGEV_NONE ; kc->timer_arm (timr, expires, flags &TIMER_ABSTIME , sigev_none);
Signature · 1 line
2.22.0
PATCH v5 14/37 alarmtimer: Make nanosleep time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
clock_nanosleep() accepts absolute values of expiration time when TIMER_ABSTIME flag is set. This absolute value is inside the task's time namespace, and has to be converted to the host's time.
--- kernel/time/alarmtimer.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/time/alarmtimer.c +2 -0
@@ -825,6 +825,8 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,ktime_t now = alarm_bases[type].get_ktime (); exp =ktime_add_safe (now, exp); + }else { + exp =timens_ktime_to_host (which_clock, exp); } ret =alarmtimer_do_nsleep (&alarm, exp, type);
Signature · 1 line
2.22.0
PATCH v5 15/37 hrtimers: Prepare hrtimer_nanosleep() for time namespaces
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
clock_nanosleep() accepts absolute values of expiration time when TIMER_ABSTIME flag is set. This absolute value is inside the task's time namespace, and has to be converted to the host's time.
There is timens_ktime_to_host() helper for converting time, but it accepts ktime argument.
As a preparation, make hrtimer_nanosleep() accept a clock value in ktime instead of timespec64.
--- include/linux/hrtimer.h | 2 +- kernel/time/hrtimer.c | 8 ++++---- kernel/time/posix-stubs.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)
include/linux/hrtimer.h +1 -1
@@ -459,7 +459,7 @@ static inline u64 hrtimer_forward_now(struct hrtimer *timer,/* Precise sleep: */ extern int nanosleep_copyout (struct restart_block *,struct timespec64 *); -extern long hrtimer_nanosleep (const struct timespec64 *rqtp, +extern long hrtimer_nanosleep (ktime_t rqtp,const enum hrtimer_mode mode,const clockid_t clockid);
kernel/time/hrtimer.c +4 -4
@@ -1716,7 +1716,7 @@ static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)return ret; } -long hrtimer_nanosleep (const struct timespec64 *rqtp, +long hrtimer_nanosleep (ktime_t rqtp,const enum hrtimer_mode mode,const clockid_t clockid) {struct restart_block *restart;
@@ -1729,7 +1729,7 @@ long hrtimer_nanosleep(const struct timespec64 *rqtp, slack =0 ;hrtimer_init_on_stack (&t.timer, clockid, mode); -hrtimer_set_expires_range_ns (&t.timer,timespec64_to_ktime (*rqtp), slack); +hrtimer_set_expires_range_ns (&t.timer, rqtp, slack); ret =do_nanosleep (&t, mode);if (ret != -ERESTART_RESTARTBLOCK ) goto out;
@@ -1764,7 +1764,7 @@ SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp, current->restart_block.nanosleep.type = rmtp ?TT_NATIVE :TT_NONE ; current->restart_block.nanosleep.rmtp = rmtp; - return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); + return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, CLOCK_MONOTONIC); }#endif
@@ -1784,7 +1784,7 @@ SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp, current->restart_block.nanosleep.type = rmtp ?TT_COMPAT :TT_NONE ; current->restart_block.nanosleep.compat_rmtp = rmtp; - return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); + return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, CLOCK_MONOTONIC); }#endif
kernel/time/posix-stubs.c +2 -2
@@ -147,7 +147,7 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, rmtp =NULL ; current->restart_block.nanosleep.type = rmtp ?TT_NATIVE :TT_NONE ; current->restart_block.nanosleep.rmtp = rmtp; -return hrtimer_nanosleep (&t, flags &TIMER_ABSTIME ? +return hrtimer_nanosleep (timespec64_to_ktime (t), flags &TIMER_ABSTIME ?HRTIMER_MODE_ABS :HRTIMER_MODE_REL , which_clock); }
@@ -233,7 +233,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, rmtp =NULL ; current->restart_block.nanosleep.type = rmtp ?TT_COMPAT :TT_NONE ; current->restart_block.nanosleep.compat_rmtp = rmtp; -return hrtimer_nanosleep (&t, flags &TIMER_ABSTIME ? +return hrtimer_nanosleep (timespec64_to_ktime (t), flags &TIMER_ABSTIME ?HRTIMER_MODE_ABS :HRTIMER_MODE_REL , which_clock); }
Signature · 1 line
2.22.0
PATCH v5 16/37 posix-timers: Make clock_nanosleep() time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
clock_nanosleep() accepts absolute values of expiration time, if the TIMER_ABSTIME flag is set. This value is in the task time namespace, which has to be converted to the host time namespace.
--- kernel/time/posix-stubs.c | 12 ++++++++++-- kernel/time/posix-timers.c | 21 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-)
kernel/time/posix-stubs.c +10 -2
@@ -129,6 +129,7 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, struct __kernel_timespec __user *, rmtp) {struct timespec64 t; +ktime_t texp;switch (which_clock) {case CLOCK_REALTIME :
@@ -147,7 +148,10 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, rmtp =NULL ; current->restart_block.nanosleep.type = rmtp ?TT_NATIVE :TT_NONE ; current->restart_block.nanosleep.rmtp = rmtp; -return hrtimer_nanosleep (timespec64_to_ktime (t), flags &TIMER_ABSTIME ? + texp =timespec64_to_ktime (t); +if (flags &TIMER_ABSTIME ) + texp =timens_ktime_to_host (clockid, texp); +return hrtimer_nanosleep (texp, flags &TIMER_ABSTIME ?HRTIMER_MODE_ABS :HRTIMER_MODE_REL , which_clock); }
@@ -215,6 +219,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, struct old_timespec32 __user *, rmtp) {struct timespec64 t; +ktime texp;switch (which_clock) {case CLOCK_REALTIME :
@@ -233,7 +238,10 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, rmtp =NULL ; current->restart_block.nanosleep.type = rmtp ?TT_COMPAT :TT_NONE ; current->restart_block.nanosleep.compat_rmtp = rmtp; -return hrtimer_nanosleep (timespec64_to_ktime (t), flags &TIMER_ABSTIME ? + texp =timespec64_to_ktime (t); +if (flags &TIMER_ABSTIME ) + texp =timens_ktime_to_host (clockid, texp); +return hrtimer_nanosleep (texp, flags &TIMER_ABSTIME ?HRTIMER_MODE_ABS :HRTIMER_MODE_REL , which_clock); }
kernel/time/posix-timers.c +18 -3
@@ -1187,7 +1187,22 @@ SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,static int common_nsleep (const clockid_t which_clock,int flags,const struct timespec64 *rqtp) { -return hrtimer_nanosleep (rqtp, flags &TIMER_ABSTIME ? +ktime_t texp =timespec64_to_ktime (*rqtp); + +return hrtimer_nanosleep (texp, flags &TIMER_ABSTIME ? +HRTIMER_MODE_ABS :HRTIMER_MODE_REL , + which_clock); +} + +static int common_nsleep_timens (const clockid_t which_clock,int flags, +const struct timespec64 *rqtp) +{ +ktime_t texp =timespec64_to_ktime (*rqtp); + +if (flags &TIMER_ABSTIME ) + texp =timens_ktime_to_host (which_clock, texp); + +return hrtimer_nanosleep (texp, flags &TIMER_ABSTIME ?HRTIMER_MODE_ABS :HRTIMER_MODE_REL , which_clock); }
@@ -1268,7 +1283,7 @@ static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_get_timespec, .clock_get_ktime = posix_get_ktime, - .nsleep = common_nsleep, + .nsleep = common_nsleep_timens, .timer_create = common_timer_create, .timer_set = common_timer_set, .timer_get = common_timer_get,
@@ -1315,7 +1330,7 @@ static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, .clock_get_ktime = posix_get_boottime_ktime, .clock_get_timespec = posix_get_boottime_timespec, - .nsleep = common_nsleep, + .nsleep = common_nsleep_timens, .timer_create = common_timer_create, .timer_set = common_timer_set, .timer_get = common_timer_get,
Signature · 1 line
2.22.0
PATCH v5 17/37 fd/proc: Respect boottime inside time namespace for /proc/uptime
In reply to Dmitry Safonov--- fs/proc/uptime.c | 3 +++ 1 file changed, 3 insertions(+)
fs/proc/uptime.c +3 -0
@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/seq_file.h> #include <linux/time.h> +#include <linux/time_namespace.h> #include <linux/kernel_stat.h> static int uptime_proc_show (struct seq_file *m,void *v)
@@ -20,6 +21,8 @@ static int uptime_proc_show(struct seq_file *m, void *v) nsec += (__force u64 )kcpustat_cpu (i).cpustat[CPUTIME_IDLE ];ktime_get_boottime_ts64 (&uptime); +timens_add_boottime (&uptime); + idle.tv_sec =div_u64_rem (nsec,NSEC_PER_SEC , &rem); idle.tv_nsec = rem; seq_printf(m, "%lu.%02lu %lu.%02lu\n",
Signature · 1 line
2.22.0
PATCH v5 18/37 x86/vdso2c: Correct err messages on file opening
In reply to Dmitry Safonoverr() message in main() is misleading: it should print outfilename, which is argv[3], not argv[2].
Correct error messages to be more precise about what failed and for which file.
--- arch/x86/entry/vdso/vdso2c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/entry/vdso/vdso2c.c +2 -2
@@ -184,7 +184,7 @@ static void map_input(const char *name, void **addr, size_t *len, int prot)int fd =open (name,O_RDONLY );if (fd ==-1 ) -err (1 ,"%s" , name); +err (1 ,"open(%s)" , name); tmp_len =lseek (fd,0 ,SEEK_END );if (tmp_len == (off_t)-1 )
@@ -237,7 +237,7 @@ int main(int argc, char **argv) outfilename = argv[3 ]; outfile =fopen (outfilename,"w" );if (!outfile) -err (1 ,"%s" , argv[2 ]); +err (1 ,"fopen(%s)" , outfilename);go (raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
Signature · 1 line
2.22.0
PATCH v5 19/37 x86/vdso2c: Convert iterator to unsigned
In reply to Dmitry Safonovi and j are used everywhere with unsigned types. Cleanup and prettify the code a bit.
Introduce syms_nr for readability and as a preparation for allocating an array of vDSO entries that will be needed for creating two vdso .so's:
one for host tasks and another for processes inside time namespace.
--- arch/x86/entry/vdso/vdso2c.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/entry/vdso/vdso2c.h +4 -5
@@ -13,7 +13,7 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len,unsigned long load_size =-1 ;/* Work around bogus warning */ unsigned long mapping_size;ELF (Ehdr) *hdr = (ELF (Ehdr) *)raw_addr; -int i; +unsigned int i, syms_nr;unsigned long j;ELF (Shdr) *symtab_hdr =NULL , *strtab_hdr, *secstrings_hdr, *alt_sec =NULL ;
@@ -86,11 +86,10 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len, strtab_hdr = raw_addr +GET_LE (&hdr->e_shoff) +GET_LE (&hdr->e_shentsize) *GET_LE (&symtab_hdr->sh_link); + syms_nr =GET_LE (&symtab_hdr->sh_size) /GET_LE (&symtab_hdr->sh_entsize);/* Walk the symbol table */ -for (i =0 ; - i <GET_LE (&symtab_hdr->sh_size) /GET_LE (&symtab_hdr->sh_entsize); - i++) { -int k; +for (i =0 ; i < syms_nr; i++) { +unsigned int k;ELF (Sym) *sym = raw_addr +GET_LE (&symtab_hdr->sh_offset) +GET_LE (&symtab_hdr->sh_entsize) * i; const char *sym_name = raw_addr +
Signature · 1 line
2.22.0
PATCH v5 20/37 x86/vdso/Makefile: Add vobjs32
In reply to Dmitry SafonovTreat ia32/i386 objects in array the same As for 64-bit vdso objects. This is a preparation ground to avoid code duplication on introduction timens vdso.
--- arch/x86/entry/vdso/Makefile | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
arch/x86/entry/vdso/Makefile +5 -10
@@ -24,6 +24,8 @@ VDSO32-$(CONFIG_IA32_EMULATION) := y # files to link into the vdso vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o +vobjs32-y := vdso32/note.o vdso32/system_call.o vdso32/sigreturn.o +vobjs32-y += vdso32/vclock_gettime.o # files to link into kernel obj-y += vma.o
@@ -37,10 +39,12 @@ vdso_img-$(VDSO32-y) += 32 obj-$(VDSO32-y) += vdso32-setup.o vobjs := $(foreach F,$(vobjs-y),$(obj)/$F) +vobjs32 := $(foreach F,$(vobjs32-y),$(obj)/$F) $(obj)/vdso.o: $(obj)/vdso.so targets += vdso.lds $(vobjs-y) +targets += vdso32/vdso32.lds $(vobjs32-y) # Build the vDSO image C files and link them in. vdso_img_objs := $(vdso_img-y:%=vdso-image-%.o)
@@ -131,10 +135,6 @@ $(obj)/vdsox32.so.dbg: $(obj)/vdsox32.lds $(vobjx32s) FORCE CPPFLAGS_vdso32.lds = $(CPPFLAGS_vdso.lds) VDSO_LDFLAGS_vdso32.lds = -m elf_i386 -soname linux-gate.so.1 -targets += vdso32/vdso32.lds -targets += vdso32/note.o vdso32/system_call.o vdso32/sigreturn.o -targets += vdso32/vclock_gettime.o - KBUILD_AFLAGS_32 := $(filter-out -m64,$(KBUILD_AFLAGS)) -DBUILD_VDSO $(obj)/vdso32.so.dbg: KBUILD_AFLAGS = $(KBUILD_AFLAGS_32) $(obj)/vdso32.so.dbg: asflags-$(CONFIG_X86_64) += -m32
@@ -159,12 +159,7 @@ endif $(obj)/vdso32.so.dbg: KBUILD_CFLAGS = $(KBUILD_CFLAGS_32) -$(obj)/vdso32.so.dbg: FORCE \ - $(obj)/vdso32/vdso32.lds \ - $(obj)/vdso32/vclock_gettime.o \ - $(obj)/vdso32/note.o \ - $(obj)/vdso32/system_call.o \ - $(obj)/vdso32/sigreturn.o +$(obj)/vdso32.so.dbg: $(obj)/vdso32/vdso32.lds $(vobjs32) FORCE $(call if_changed,vdso_and_check) #
Signature · 1 line
2.22.0
PATCH v5 21/37 x86/vdso: Restrict splitting VVAR VMA
In reply to Dmitry SafonovAlthough, time namespace can work with VVAR VMA split, it seems worth to forbid splitting VVAR resulting in stricter ABI and reducing amount of corner-cases to consider while working further on VDSO.
I don't think there is any use-case for partial mremap() of vvar, but if there is any - this patch can be easily reverted.
--- arch/x86/entry/vdso/vma.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/entry/vdso/vma.c +13 -0
@@ -84,6 +84,18 @@ static int vdso_mremap(const struct vm_special_mapping *sm,return 0 ; } +static int vvar_mremap (const struct vm_special_mapping *sm, +struct vm_area_struct *new_vma) +{ +unsigned long new_size = new_vma->vm_end - new_vma->vm_start; +const struct vdso_image *image = new_vma->vm_mm->context.vdso_image; + +if (new_size != -image->sym_vvar_start) +return -EINVAL ; + +return 0 ; +} +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm,struct vm_area_struct *vma,struct vm_fault *vmf) {
@@ -136,6 +148,7 @@ static const struct vm_special_mapping vdso_mapping = {static const struct vm_special_mapping vvar_mapping = { .name ="[vvar]" , .fault = vvar_fault, + .mremap = vvar_mremap, };/*
Signature · 1 line
2.22.0
PATCH v5 22/37 x86/vdso: Rename vdso_image {.data=>.text}
In reply to Dmitry SafonovTo avoid any confusion with VVAR.
--- arch/x86/entry/vdso/vdso2c.h | 2 +- arch/x86/entry/vdso/vma.c | 6 +++--- arch/x86/include/asm/vdso.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/entry/vdso/vdso2c.h +1 -1
@@ -158,7 +158,7 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len,fprintf (outfile,"\n};\n\n" );fprintf (outfile,"const struct vdso_image %s = {\n" , image_name); -fprintf (outfile,"\t.data = raw_data,\n" ); +fprintf (outfile,"\t.text = raw_data,\n" );fprintf (outfile,"\t.size = %lu,\n" , mapping_size);if (alt_sec) { fprintf(outfile, "\t.alt = %lu,\n",
arch/x86/entry/vdso/vma.c +3 -3
@@ -32,8 +32,8 @@ void __init init_vdso_image(const struct vdso_image *image) {BUG_ON (image->size %PAGE_SIZE !=0 ); -apply_alternatives ((struct alt_instr *)(image->data + image->alt), - (struct alt_instr *)(image->data + image->alt + +apply_alternatives ((struct alt_instr *)(image->text + image->alt), + (struct alt_instr *)(image->text + image->alt + image->alt_len)); }
@@ -47,7 +47,7 @@ static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,if (!image || (vmf->pgoff <<PAGE_SHIFT ) >= image->size)return VM_FAULT_SIGBUS ; - vmf->page =virt_to_page (image->data + (vmf->pgoff <<PAGE_SHIFT )); + vmf->page =virt_to_page (image->text + (vmf->pgoff <<PAGE_SHIFT ));get_page (vmf->page);return 0 ; }
arch/x86/include/asm/vdso.h +1 -1
@@ -11,7 +11,7 @@#include <linux/mm_types.h> struct vdso_image { -void *data; +void *text;unsigned long size;/* Always a multiple of PAGE_SIZE */ unsigned long alt, alt_len;
Signature · 1 line
2.22.0
PATCH v5 23/37 x86/vdso: Add offsets page in vvar
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
As modern applications fetch time from VDSO without entering the kernel, it's needed to provide offsets for userspace code inside time namespace.
A page for timens offsets is allocated on time namespace construction. Put that page into VVAR for tasks inside timens and zero page for host processes.
As VDSO code is already optimized as much as possible in terms of speed, any new if-condition in VDSO code is undesirable; the goal is to provide two .so(s), as was originally suggested by Andy and Thomas:
- for host tasks with optimized-out clk_to_ns() without any penalty
- for processes inside timens with clk_to_ns()
For this purpose, define clk_to_ns() under CONFIG_TIME_NS.
To eliminate any performance regression, clk_to_ns() will be called under static_branch with follow-up patches, that adds support for patching vdso.
VDSO mappings are platform-specific, add Kconfig dependency for arch.
--- arch/Kconfig | 5 +++ arch/x86/Kconfig | 1 + arch/x86/entry/vdso/vdso-layout.lds.S | 9 ++++- arch/x86/entry/vdso/vdso2c.c | 3 ++ arch/x86/entry/vdso/vma.c | 12 +++++++ arch/x86/include/asm/vdso.h | 1 + init/Kconfig | 1 + lib/vdso/gettimeofday.c | 47 +++++++++++++++++++++++++++ 8 files changed, 78 insertions(+), 1 deletion(-)
arch/Kconfig +5 -0
@@ -729,6 +729,11 @@ config HAVE_ARCH_NVRAM_OPS config ISA_BUS_API def_bool ISA +config ARCH_HAS_VDSO_TIME_NS + bool + help + VDSO can add time-ns offsets without entering kernel. + # # ABI hall of shame #
arch/x86/Kconfig +1 -0
@@ -81,6 +81,7 @@ config X86 select ARCH_HAS_STRICT_MODULE_RWX select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_HAS_VDSO_TIME_NS select ARCH_HAVE_NMI_SAFE_CMPXCHG select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI select ARCH_MIGHT_HAVE_PC_PARPORT
arch/x86/entry/vdso/vdso-layout.lds.S +8 -1
@@ -7,6 +7,12 @@ * This script controls its layout. */ +#ifdef CONFIG_TIME_NS +# define TIMENS_SZ PAGE_SIZE +#else +# define TIMENS_SZ 0 +#endif + SECTIONS { /*
@@ -16,7 +22,7 @@ SECTIONS * segment. */ - vvar_start = . - 3 * PAGE_SIZE; + vvar_start = . - (3 * PAGE_SIZE + TIMENS_SZ); vvar_page = vvar_start; /* Place all vvars at the offsets in asm/vvar.h. */
@@ -28,6 +34,7 @@ SECTIONS pvclock_page = vvar_start + PAGE_SIZE; hvclock_page = vvar_start + 2 * PAGE_SIZE; + timens_page = vvar_start + 3 * PAGE_SIZE; . = SIZEOF_HEADERS;
arch/x86/entry/vdso/vdso2c.c +3 -0
@@ -75,12 +75,14 @@ enum { sym_vvar_page, sym_pvclock_page, sym_hvclock_page, + sym_timens_page, };const int special_pages[] = { sym_vvar_page, sym_pvclock_page, sym_hvclock_page, + sym_timens_page, }; struct vdso_sym {
@@ -93,6 +95,7 @@ struct vdso_sym required_syms[] = { [sym_vvar_page] = {"vvar_page", true}, [sym_pvclock_page] = {"pvclock_page", true}, [sym_hvclock_page] = {"hvclock_page", true}, + [sym_timens_page] = {"timens_page", true}, {"VDSO32_NOTE_MASK", true}, {"__kernel_vsyscall", true}, {"__kernel_sigreturn", true},
arch/x86/entry/vdso/vma.c +12 -0
@@ -14,6 +14,7 @@#include <linux/elf.h> #include <linux/cpu.h> #include <linux/ptrace.h> +#include <linux/time_namespace.h> #include <asm/pvclock.h> #include <asm/vgtod.h> #include <asm/proto.h>
@@ -23,6 +24,7 @@#include <asm/desc.h> #include <asm/cpufeature.h> #include <clocksource/hyperv_timer.h> +#include <asm/page.h> #if defined(CONFIG_X86_64 )unsigned int __read_mostly vdso64_enabled =1 ;
@@ -135,6 +137,16 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,if (tsc_pg &&vclock_was_used (VCLOCK_HVCLOCK ))return vmf_insert_pfn (vma, vmf->address,vmalloc_to_pfn (tsc_pg)); + }else if (sym_offset == image->sym_timens_page) { +struct time_namespace *ns = current->nsproxy->time_ns; +unsigned long pfn; + +if (!ns->offsets) + pfn =page_to_pfn (ZERO_PAGE (0 )); +else + pfn =page_to_pfn (virt_to_page (ns->offsets)); + +return vmf_insert_pfn (vma, vmf->address, pfn); }return VM_FAULT_SIGBUS ;
arch/x86/include/asm/vdso.h +1 -0
@@ -21,6 +21,7 @@ struct vdso_image {long sym_vvar_page;long sym_pvclock_page;long sym_hvclock_page; +long sym_timens_page;long sym_VDSO32_NOTE_MASK;long sym___kernel_sigreturn;long sym___kernel_rt_sigreturn;
init/Kconfig +1 -0
@@ -1071,6 +1071,7 @@ config UTS_NS config TIME_NS bool "TIME namespace" + depends on ARCH_HAS_VDSO_TIME_NS default y help In this namespace boottime and monotonic clocks can be set.
lib/vdso/gettimeofday.c +47 -0
71 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 24/37 x86/vdso: Allocate timens vdso
In reply to Dmitry SafonovAs it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.
Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation vdso code.
The propose is to allocate a second vdso code with dynamically patched out (disabled by static_branch) timens code on boot time.
Allocate another vdso and copy original code.
--- arch/x86/entry/vdso/vdso2c.h | 2 +- arch/x86/entry/vdso/vma.c | 113 +++++++++++++++++++++++++++++++++-- arch/x86/include/asm/vdso.h | 9 +-- 3 files changed, 114 insertions(+), 10 deletions(-)
arch/x86/entry/vdso/vdso2c.h +1 -1
@@ -157,7 +157,7 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len, }fprintf (outfile,"\n};\n\n" ); -fprintf (outfile,"const struct vdso_image %s = {\n" , image_name); +fprintf (outfile,"struct vdso_image %s __ro_after_init = {\n" , image_name);fprintf (outfile,"\t.text = raw_data,\n" );fprintf (outfile,"\t.size = %lu,\n" , mapping_size);if (alt_sec) {
arch/x86/entry/vdso/vma.c +108 -5
147 lines not on this page · raw message
arch/x86/include/asm/vdso.h +5 -4
@@ -12,6 +12,7 @@struct vdso_image {void *text; +void *text_timens;unsigned long size;/* Always a multiple of PAGE_SIZE */ unsigned long alt, alt_len;
@@ -30,18 +31,18 @@ struct vdso_image { };#ifdef CONFIG_X86_64 -extern const struct vdso_image vdso_image_64; +extern struct vdso_image vdso_image_64;#endif #ifdef CONFIG_X86_X32 -extern const struct vdso_image vdso_image_x32; +extern struct vdso_image vdso_image_x32;#endif #if definedCONFIG_X86_32 || definedCONFIG_COMPAT -extern const struct vdso_image vdso_image_32; +extern struct vdso_image vdso_image_32;#endif -extern void __init init_vdso_image (const struct vdso_image *image); +extern void __init init_vdso_image (struct vdso_image *image);extern int map_vdso_once (const struct vdso_image *image,unsigned long addr);
Signature · 1 line
2.22.0
PATCH v5 25/37 x86/vdso: Switch image on setns()/clone()
In reply to Dmitry SafonovAs it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.
Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation vdso code.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
Whenever a user does setns() or unshare(CLONE_TIMENS) followed by clone(), change VDSO image in mm and zap VVAR/VDSO page tables. They will be re-faulted with corresponding image and VVAR offsets.
--- arch/x86/entry/vdso/vma.c | 23 +++++++++++++++++++++++ arch/x86/include/asm/vdso.h | 1 + kernel/time_namespace.c | 11 +++++++++++ 3 files changed, 35 insertions(+)
arch/x86/entry/vdso/vma.c +23 -0
@@ -25,6 +25,7 @@#include <asm/cpufeature.h> #include <clocksource/hyperv_timer.h> #include <asm/page.h> +#include <asm/tlb.h> #if defined(CONFIG_X86_64 )unsigned int __read_mostly vdso64_enabled =1 ;
@@ -266,6 +267,28 @@ static const struct vm_special_mapping vvar_mapping = { .mremap = vvar_mremap, }; +#ifdef CONFIG_TIME_NS +int vdso_join_timens (struct task_struct *task) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (down_write_killable (&mm->mmap_sem)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, &vvar_mapping) || +vma_is_special_mapping (vma, &vdso_mapping)) +zap_page_range (vma, vma->vm_start, size); + } + +up_write (&mm->mmap_sem); +return 0 ; +} +#endif +/* * Add vdso and vvar mappings to current process. * @image - blob to map
arch/x86/include/asm/vdso.h +1 -0
@@ -45,6 +45,7 @@ extern struct vdso_image vdso_image_32;extern void __init init_vdso_image (struct vdso_image *image);extern int map_vdso_once (const struct vdso_image *image,unsigned long addr); +extern int vdso_join_timens (struct task_struct *task);#endif /* __ASSEMBLER__ */
kernel/time_namespace.c +11 -0
@@ -15,6 +15,7 @@#include <linux/cred.h> #include <linux/err.h> #include <linux/mm.h> +#include <asm/vdso.h> ktime_t do_timens_ktime_to_host (clockid_t clockid,ktime_t tim,struct timens_offsets *ns_offsets)
@@ -199,6 +200,7 @@ static void timens_put(struct ns_common *ns)static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) {struct time_namespace *ns =to_time_ns (new); +int ret;if (!thread_group_empty (current))return -EINVAL ;
@@ -207,6 +209,10 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new) !ns_capable(current_user_ns(), CAP_SYS_ADMIN))return -EPERM ; + ret =vdso_join_timens (current); +if (ret) +return ret; +get_time_ns (ns);get_time_ns (ns);put_time_ns (nsproxy->time_ns);
@@ -221,10 +227,15 @@ int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk) {struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;struct time_namespace *ns =to_time_ns (nsc); +int ret;if (nsproxy->time_ns == nsproxy->time_ns_for_children)return 0 ; + ret =vdso_join_timens (tsk); +if (ret) +return ret; +get_time_ns (ns);put_time_ns (nsproxy->time_ns); nsproxy->time_ns = ns;
Signature · 1 line
2.22.0
PATCH v5 26/37 vdso: Introduce vdso_static_branch_unlikely()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
As it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
Introduce vdso_static_branch_unlikely(), which is similar to static_branch_unlikely(); alias it with timens_static_branch_unlikely() under CONFIG_TIME_NS.
The timens code in vdso will look like this:
if (timens_static_branch_unlikely()) {
clk_to_ns(clk, ts);}
The version of vdso which is compiled from sources will never execute clk_to_ns(). And then we can patch the 'no-op' in the straight-line codepath with a 'jump' instruction to the out-of-line true branch and get the timens version of the vdso library.
--- arch/x86/include/asm/jump_label.h | 14 ++++++++++++++ lib/vdso/gettimeofday.c | 10 ++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-)
arch/x86/include/asm/jump_label.h +14 -0
@@ -53,6 +53,20 @@ static __always_inline bool arch_static_branch_jump(struct static_key *key, boolreturn true; } +static __always_inline bool vdso_static_branch_unlikely(void) +{ + asm_volatile_goto("1:\n\t" + ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + "2: .word 1b - 2b, %l[l_yes] - 2b\n\t" + ".popsection\n\t" + : : : : l_yes); + +return false; +l_yes: +return true; +} +#else /* __ASSEMBLY__ */ .macro STATIC_JUMP_IF_TRUE target, key, def
lib/vdso/gettimeofday.c +8 -2
@@ -8,6 +8,7 @@#include <linux/kernel.h> #include <linux/hrtimer_defs.h> #include <linux/timens_offsets.h> +#include <linux/jump_label.h> #include <vdso/datapage.h> #include <vdso/helpers.h>
@@ -43,6 +44,8 @@ u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult) extern u8 timens_page __attribute__((visibility("hidden"))); +#define timens_static_branch_unlikely vdso_static_branch_unlikely + notrace static __always_inline void clk_to_ns(clockid_t clk, struct __kernel_timespec *ts) {struct timens_offsets *timens = (struct timens_offsets *) &timens_page;
@@ -79,6 +82,7 @@ notrace static __always_inline void clk_to_ns(clockid_t clk, struct __kernel_tim }#else notrace static __always_inline void clk_to_ns(clockid_t clk, struct __kernel_timespec *ts) {} +notrace static __always_inline bool timens_static_branch_unlikely(void) { return false; }#endif static int do_hres(const struct vdso_data *vd, clockid_t clk,
@@ -108,7 +112,8 @@ static int do_hres(const struct vdso_data *vd, clockid_t clk, ts->tv_sec = sec +__iter_div_u64_rem (ns,NSEC_PER_SEC , &ns); ts->tv_nsec = ns; -clk_to_ns (clk, ts); +if (timens_static_branch_unlikely ()) +clk_to_ns (clk, ts);return 0 ; }
@@ -125,7 +130,8 @@ static void do_coarse(const struct vdso_data *vd, clockid_t clk, ts->tv_nsec = vdso_ts->nsec; }while (unlikely (vdso_read_retry (vd, seq))); -clk_to_ns (clk, ts); +if (timens_static_branch_unlikely ()) +clk_to_ns (clk, ts); } static __maybe_unused int
Signature · 1 line
2.22.0
PATCH v5 27/37 x86/vdso2c: Process jump tables
In reply to Dmitry SafonovAs it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside time namespace with clk_to_ns() that subtracts offsets from host's time.
The timens code in vdso looks like this:
if (timens_static_branch()) {
clk_to_ns(clk, ts);
}Static branch mechanism adds a __jump_table section into vdso. Vdso's linker script drops all unwanted sections in compile time.
Preserve __jump_table section and add it into (struct vdso_image), as it's needed for enabling (patching) static branches that are present on vdso.
--- arch/x86/entry/vdso/vdso-layout.lds.S | 1 + arch/x86/entry/vdso/vdso2c.h | 9 ++++++++- arch/x86/include/asm/vdso.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/vdso-layout.lds.S +1 -0
@@ -45,6 +45,7 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + __jump_table : { *(__jump_table) } :text .dynamic : { *(.dynamic) } :text :dynamic
arch/x86/entry/vdso/vdso2c.h +8 -1
@@ -14,7 +14,7 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len,unsigned long mapping_size;ELF (Ehdr) *hdr = (ELF (Ehdr) *)raw_addr;unsigned int i, syms_nr; -unsigned long j; +unsigned long j, jump_table_addr =-1UL , jump_table_size =-1UL ;ELF (Shdr) *symtab_hdr =NULL , *strtab_hdr, *secstrings_hdr, *alt_sec =NULL ;ELF (Dyn) *dyn =0 , *dyn_end =0 ;
@@ -78,6 +78,10 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len,if (!strcmp (secstrings +GET_LE (&sh->sh_name),".altinstructions" )) alt_sec = sh; +if (!strcmp (secstrings +GET_LE (&sh->sh_name),"__jump_table" )) { + jump_table_addr =GET_LE (&sh->sh_offset); + jump_table_size =GET_LE (&sh->sh_size); + } }if (!symtab_hdr)
@@ -166,6 +170,9 @@ static void BITSFUNC(go)(void *raw_addr, size_t raw_len,fprintf (outfile,"\t.alt_len = %lu,\n" , (unsigned long )GET_LE (&alt_sec->sh_size)); } +fprintf (outfile,"\t.jump_table = %luUL,\n" , jump_table_addr); +fprintf (outfile,"\t.jump_table_len = %luUL,\n" , jump_table_size); +for (i =0 ; i <NSYMS ; i++) {if (required_syms[i].export && syms[i]) fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
arch/x86/include/asm/vdso.h +1 -0
@@ -16,6 +16,7 @@ struct vdso_image {unsigned long size;/* Always a multiple of PAGE_SIZE */ unsigned long alt, alt_len; +unsigned long jump_table, jump_table_len;long sym_vvar_start;/* Negative offset to the vvar area */
Signature · 1 line
2.22.0
PATCH v5 28/37 x86/vdso: Enable static branches for the timens vdso
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
As it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
The timens code in vdso looks like this:
if (timens_static_branch_unlikely()) {
clk_to_ns(clk, ts);
}This static branch is disabled by default. And the code generated consist of a single atomic 'no-op' instruction, in the straight-line code path.
Enable static branches in the timens vdso: the 'no-op' instruction gets replaced with a 'jump' instruction to the out-of-line true branch.
--- arch/x86/entry/vdso/vma.c | 30 ++++++++++++++++++++++++------ arch/x86/kernel/jump_label.c | 14 ++++++++++++++ include/linux/jump_label.h | 8 ++++++++ init/Kconfig | 1 + 4 files changed, 47 insertions(+), 6 deletions(-)
arch/x86/entry/vdso/vma.c +24 -6
@@ -15,6 +15,7 @@#include <linux/cpu.h> #include <linux/ptrace.h> #include <linux/time_namespace.h> +#include <linux/jump_label.h> #include <asm/pvclock.h> #include <asm/vgtod.h> #include <asm/proto.h>
@@ -31,20 +32,37 @@unsigned int __read_mostly vdso64_enabled =1 ;#endif -void __init init_vdso_image (struct vdso_image *image) +#ifdef CONFIG_TIME_NS +static __init void init_timens (struct vdso_image *image) { -BUG_ON (image->size %PAGE_SIZE !=0 ); +struct vdso_jump_entry *entries; +unsigned long entries_nr; + +if (WARN_ON (image->jump_table ==-1UL )) +return ; -apply_alternatives ((struct alt_instr *)(image->text + image->alt), - (struct alt_instr *)(image->text + image->alt + - image->alt_len)); -#ifdef CONFIG_TIME_NS image->text_timens =vmalloc_32 (image->size);if (WARN_ON (image->text_timens ==NULL ))return ;memcpy (image->text_timens, image->text, image->size); + + entries = image->text_timens + image->jump_table; + entries_nr = image->jump_table_len /sizeof (struct vdso_jump_entry ); +apply_vdso_jump_labels (entries, entries_nr); +} +#else +static inline void init_timens (struct vdso_image *image) {}#endif + +void __init init_vdso_image (struct vdso_image *image) +{ +BUG_ON (image->size %PAGE_SIZE !=0 ); + +apply_alternatives ((struct alt_instr *)(image->text + image->alt), + (struct alt_instr *)(image->text + image->alt + + image->alt_len)); +init_timens (image); }struct linux_binprm ;
arch/x86/kernel/jump_label.c +14 -0
@@ -24,6 +24,20 @@ union jump_code_union { } __attribute__((packed)); }; +__init void apply_vdso_jump_labels (struct vdso_jump_entry *ent,unsigned long nr) +{ +while (nr--) { +void *code_addr = (void *)ent + ent->code; +union jump_code_union jmp; + + jmp.jump =0xe9 ;/* JMP rel32 */ + jmp.offset = ent->target - ent->code -JUMP_LABEL_NOP_SIZE ; +memcpy (code_addr, &jmp,JUMP_LABEL_NOP_SIZE ); + + ent++; + } +} +static void bug_at (unsigned char *ip,int line) {/*
include/linux/jump_label.h +8 -0
@@ -125,6 +125,11 @@ struct jump_entry {long key;// key may be far away from the core kernel under KASLR }; +struct vdso_jump_entry { +u16 code; +u16 target; +}; +static inline unsigned long jump_entry_code (const struct jump_entry *entry) {return (unsigned long )&entry->code + entry->code;
@@ -229,6 +234,9 @@ extern void static_key_enable(struct static_key *key);extern void static_key_disable (struct static_key *key);extern void static_key_enable_cpuslocked (struct static_key *key);extern void static_key_disable_cpuslocked (struct static_key *key); +extern void apply_vdso_jump_labels (struct vdso_jump_entry *ent, +unsigned long nr); +/* * We should be using ATOMIC_INIT() for initializing .enabled, but
init/Kconfig +1 -0
@@ -1072,6 +1072,7 @@ config UTS_NS config TIME_NS bool "TIME namespace" depends on ARCH_HAS_VDSO_TIME_NS + depends on JUMP_LABEL default y help In this namespace boottime and monotonic clocks can be set.
Signature · 1 line
2.22.0
PATCH v5 29/37 posix-clocks: Add align for timens_offsets
In reply to Dmitry SafonovAlign offsets so that time namespace will work for ia32 applications on x86_64 host.
--- include/linux/timens_offsets.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
include/linux/timens_offsets.h +10 -2
@@ -2,9 +2,17 @@#ifndef _LINUX_TIME_OFFSETS_H#define _LINUX_TIME_OFFSETS_H +/* +* Time offsets need align as they're placed on VVAR page, +* which is used by x86_64 and ia32 VDSO code. +* On ia32 offset::tv_sec (u64) has align(4), so re-align offsets +* to the same positions as 64-bit offsets. +* On 64-bit big-endian systems VDSO should convert to timespec64 +* to timespec because of a padding occurring between the fields. +*/ struct timens_offsets { -struct timespec64 monotonic; -struct timespec64 boottime; +struct timespec64 monotonic__aligned(8) ; +struct timespec64 boottime__aligned(8) ; };#endif
Signature · 1 line
2.22.0
PATCH v5 30/37 fs/proc: Introduce /proc/pid/timens_offsets
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
API to set time namespace offsets for children processes, i.e.:
echo "clockid off_ses off_nsec" > /proc/self/timens_offsets
--- fs/proc/base.c | 95 ++++++++++++++++++++++++++++++ include/linux/time_namespace.h | 10 ++++ kernel/time_namespace.c | 104 +++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+)
fs/proc/base.c +95 -0
@@ -94,6 +94,7 @@#include <linux/sched/debug.h> #include <linux/sched/stat.h> #include <linux/posix-timers.h> +#include <linux/time_namespace.h> #include <trace/events/oom.h> #include "internal.h" #include "fd.h"
@@ -1533,6 +1534,97 @@ static const struct file_operations proc_pid_sched_autogroup_operations = {#endif /* CONFIG_SCHED_AUTOGROUP */ +#ifdef CONFIG_TIME_NS +static int timens_offsets_show (struct seq_file *m,void *v) +{ +struct task_struct *p; + + p =get_proc_task (file_inode (m->file)); +if (!p) +return -ESRCH ; +proc_timens_show_offsets (p, m); + +put_task_struct (p); + +return 0 ; +} + +static ssize_t +timens_offsets_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct inode *inode =file_inode (file); +struct proc_timens_offset offsets[2 ]; +char *kbuf =NULL , *pos, *next_line; +struct task_struct *p; +int ret, noffsets; + +/* Only allow < page size writes at the beginning of the file */ +if ((*ppos !=0 ) || (count >=PAGE_SIZE )) +return -EINVAL ; + +/* Slurp in the user data */ + kbuf =memdup_user_nul (buf, count); +if (IS_ERR (kbuf)) +return PTR_ERR (kbuf); + +/* Parse the user data */ + ret = -EINVAL ; + noffsets =0 ; +for (pos = kbuf; pos; pos = next_line) { +struct proc_timens_offset *off = &offsets[noffsets]; +int err; + +/* Find the end of line and ensure we don't look past it */ + next_line =strchr (pos,'\n' ); +if (next_line) { + *next_line ='\0' ; + next_line++; +if (*next_line =='\0' ) + next_line =NULL ; + } + + err =sscanf (pos,"%u %lld %lu" , &off->clockid, + &off->val.tv_sec, &off->val.tv_nsec); +if (err !=3 || off->val.tv_nsec >=NSEC_PER_SEC ) + goto out; + noffsets++; +if (noffsets ==ARRAY_SIZE (offsets)) { +if (next_line) + count = next_line - kbuf; +break ; + } + } + + ret = -ESRCH ; + p =get_proc_task (inode); +if (!p) + goto out; + ret =proc_timens_set_offset (file, p, offsets, noffsets); +put_task_struct (p); +if (ret) + goto out; + + ret = count; +out: +kfree (kbuf); +return ret; +} + +static int timens_offsets_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, timens_offsets_show, inode); +} + +static const struct file_operations proc_timens_offsets_operations = { + .open = timens_offsets_open, + .read = seq_read, + .write = timens_offsets_write, + .llseek = seq_lseek, + .release = single_release, +}; +#endif /* CONFIG_TIME_NS */ +static ssize_t comm_write (struct file *file,const char __user *buf,size_t count,loff_t *offset) {
@@ -3015,6 +3107,9 @@ static const struct pid_entry tgid_base_stuff[] = {#endif #ifdef CONFIG_SCHED_AUTOGROUP REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), +#endif +#ifdef CONFIG_TIME_NS +REG ("timens_offsets" ,S_IRUGO |S_IWUSR , proc_timens_offsets_operations), #endifREG ("comm" ,S_IRUGO |S_IWUSR , proc_pid_set_comm_operations), #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
include/linux/time_namespace.h +10 -0
@@ -40,6 +40,16 @@ static inline void put_time_ns(struct time_namespace *ns)kref_put (&ns->kref, free_time_ns); } +extern void proc_timens_show_offsets (struct task_struct *p,struct seq_file *m); + +struct proc_timens_offset { +int clockid; +struct timespec64 val; +}; + +extern int proc_timens_set_offset (struct file *file,struct task_struct *p, +struct proc_timens_offset *offsets,int n); +static inline void timens_add_monotonic (struct timespec64 *ts) {struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets;
kernel/time_namespace.c +104 -0
116 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 31/37 selftest/timens: Add Time Namespace test for supported clocks
In reply to Dmitry SafonovA test to check that all supported clocks work on host and inside a new time namespace. Use both ways to get time: through VDSO and by entering the kernel with implicit syscall.
Introduce a new timens directory in selftests framework for the next timens tests.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 5 + tools/testing/selftests/timens/config | 1 + tools/testing/selftests/timens/log.h | 26 +++ tools/testing/selftests/timens/timens.c | 185 ++++++++++++++++++++++ tools/testing/selftests/timens/timens.h | 63 ++++++++ 7 files changed, 282 insertions(+) create mode 100644 tools/testing/selftests/timens/.gitignore create mode 100644 tools/testing/selftests/timens/Makefile create mode 100644 tools/testing/selftests/timens/config create mode 100644 tools/testing/selftests/timens/log.h create mode 100644 tools/testing/selftests/timens/timens.c create mode 100644 tools/testing/selftests/timens/timens.h
tools/testing/selftests/Makefile +1 -0
@@ -47,6 +47,7 @@ TARGETS += splice TARGETS += static_keys TARGETS += sync TARGETS += sysctl +TARGETS += timens ifneq (1, $(quicktest)) TARGETS += timers endif
tools/testing/selftests/timens/.gitignore new file +1 -0
@@ -0,0 +1 @@ +timens
tools/testing/selftests/timens/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +TEST_GEN_PROGS := timens + +CFLAGS := -Wall -Werror + +include ../lib.mk
tools/testing/selftests/timens/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_TIME_NS=y
tools/testing/selftests/timens/log.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __SELFTEST_TIMENS_LOG_H__ +#define __SELFTEST_TIMENS_LOG_H__ + +#define pr_msg (fmt, lvl, ...) \ + ksft_print_msg("[%s] (%s:%d)\t" fmt "\n", \ + lvl, __FILE__, __LINE__, ##__VA_ARGS__) + +#define pr_p (func, fmt, ...) func(fmt ": %m", ##__VA_ARGS__) + +#define pr_err (fmt, ...) \ + ({ \ + ksft_test_result_error(fmt "\n", ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_fail (fmt, ...) \ + ({ \ + ksft_test_result_fail(fmt, ##__VA_ARGS__); \ + -1; \ + }) + +#define pr_perror (fmt, ...) pr_p(pr_err, fmt, ##__VA_ARGS__) + +#endif
tools/testing/selftests/timens/timens.c new file +185 -0
185 lines not on this page · raw message
tools/testing/selftests/timens/timens.h new file +63 -0
63 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 32/37 selftest/timens: Add a test for timerfd
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Check that timerfd_create() takes into account clock offsets.
--- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/timerfd.c | 129 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/timerfd.c
tools/testing/selftests/timens/.gitignore +1 -0
@@ -1 +1,2 @@ timens +timerfd
tools/testing/selftests/timens/Makefile +1 -1
@@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens +TEST_GEN_PROGS := timens timerfd CFLAGS := -Wall -Werror
tools/testing/selftests/timens/timerfd.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 33/37 selftest/timens: Add a test for clock_nanosleep()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Check that clock_nanosleep() takes into account clock offsets.
--- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- .../selftests/timens/clock_nanosleep.c | 102 ++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/clock_nanosleep.c
tools/testing/selftests/timens/.gitignore +1 -0
@@ -1,2 +1,3 @@ +clock_nanosleep timens timerfd
tools/testing/selftests/timens/Makefile +1 -1
@@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd +TEST_GEN_PROGS := timens timerfd clock_nanosleep CFLAGS := -Wall -Werror
tools/testing/selftests/timens/clock_nanosleep.c new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 34/37 selftest/timens: Add procfs selftest
In reply to Dmitry SafonovCheck that /proc/uptime is correct inside a new time namespace.
--- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/procfs.c | 144 ++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/procfs.c
tools/testing/selftests/timens/.gitignore +1 -0
@@ -1,3 +1,4 @@ clock_nanosleep +procfs timens timerfd
tools/testing/selftests/timens/Makefile +1 -1
@@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd clock_nanosleep +TEST_GEN_PROGS := timens timerfd clock_nanosleep procfs CFLAGS := -Wall -Werror
tools/testing/selftests/timens/procfs.c new file +144 -0
144 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 35/37 selftest/timens: Add timer offsets test
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Check that timer_create() takes into account clock offsets.
--- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 3 +- tools/testing/selftests/timens/timer.c | 118 ++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/timer.c
tools/testing/selftests/timens/.gitignore +1 -0
@@ -1,4 +1,5 @@ clock_nanosleep procfs timens +timer timerfd
tools/testing/selftests/timens/Makefile +2 -1
@@ -1,5 +1,6 @@ -TEST_GEN_PROGS := timens timerfd clock_nanosleep procfs +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs CFLAGS := -Wall -Werror +LDFLAGS := -lrt include ../lib.mk
tools/testing/selftests/timens/timer.c new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 36/37 selftests/timens: Add a simple perf test for clock_gettime()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
--- tools/testing/selftests/timens/.gitignore | 2 + tools/testing/selftests/timens/Makefile | 10 +- tools/testing/selftests/timens/gettime_perf.c | 101 +++++++++++ .../selftests/timens/gettime_perf_cold.c | 160 ++++++++++++++++++ 4 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/timens/gettime_perf.c create mode 100644 tools/testing/selftests/timens/gettime_perf_cold.c
tools/testing/selftests/timens/.gitignore +2 -0
@@ -1,4 +1,6 @@ clock_nanosleep +gettime_perf +gettime_perf_cold procfs timens timer
tools/testing/selftests/timens/Makefile +8 -2
@@ -1,6 +1,12 @@ -TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs gettime_perf + +uname_M := $(shell uname -m 2>/dev/null || echo not) +ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/) +ifeq ($(ARCH),x86_64) +TEST_GEN_PROGS += gettime_perf_cold +endif CFLAGS := -Wall -Werror -LDFLAGS := -lrt +LDFLAGS := -lrt -ldl include ../lib.mk
tools/testing/selftests/timens/gettime_perf.c new file +101 -0
101 lines not on this page · raw message
tools/testing/selftests/timens/gettime_perf_cold.c new file +160 -0
160 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 37/37 selftest/timens: Check that a right vdso is mapped after fork and exec
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
--- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/exec.c | 93 +++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/exec.c
tools/testing/selftests/timens/.gitignore +1 -0
@@ -1,4 +1,5 @@ clock_nanosleep +exec gettime_perf gettime_perf_cold procfs
tools/testing/selftests/timens/Makefile +1 -1
@@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs gettime_perf +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs gettime_perf exec uname_M := $(shell uname -m 2>/dev/null || echo not) ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/)
tools/testing/selftests/timens/exec.c new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.22.0
v5 cover kernel: Introduce Time Namespace
In reply to Dmitry SafonovDiscussions around time namespace are there for a long time. The first attempt to implement it was in 2006 by Jeff Dike. From that time, the topic appears on and off in various discussions.
There are two main use cases for time namespaces:
1. change date and time inside a container;
2. adjust clocks for a container restored from a checkpoint.
“It seems like this might be one of the last major obstacles keeping migration from being used in production systems, given that not all containers and connections can be migrated as long as a time dependency is capable of messing it up.” (by github.com/dav-ell)
The kernel provides access to several clocks: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME. Last two clocks are monotonous, but the start points for them are not defined and are different for each system. When a container is migrated from one node to another, all clocks have to be restored into consistent states; in other words, they have to continue running from the same points where they have been dumped.
The main idea of this patch set is adding per-namespace offsets for system clocks. When a process in a non-root time namespace requests time of a clock, a namespace offset is added to the current value of this clock and the sum is returned.
All offsets are placed on a separate page, this allows us to map it as part of VVAR into user processes and use offsets from VDSO calls.
Now offsets are implemented for CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks.
v4..v5 Changes:
* Rebased over generic vdso (already in master)
* Addressing review comments by Thomas Gleixner
(thanks much for your time and patience):
- Dropping timens prefix from subjects (it's not a subsystem)
- Keeping commit messages in a neutral technical form
- Splitting unreasonably large patches
- Document code with missing comments
- Dropped dead code that's not compiled with !CONFIG_TIME_NS
* Updated performance results [here, at the bottom]
* Split vdso jump tables patch
* Allow unshare() with many threads: it's safe until fork()/clone(), where we check for CLONE_THREADS
* Add missed check in setns() for CLONE_VM | CLONE_THREADS
* Fixed compilation with !CONFIG_UTS_NS
* Add a plan in selftests (prevents new warning "Planned tests != run tests")
* Set jump table section address & size to (-1UL) just in case if there is no such section while running vdso2c (and WARN() on boot in such case)
[v1..v4 Changelog is at the very bottom here]
Our performance measurements show that the price of VDSO's clock_gettime() in a child time namespace is about 8% with a hot CPU cache and about 90% with a cold CPU cache. There is no performance regression for host processes outside time namespace on those tests.
We wrote two small benchmarks. The first one gettime_perf.c calls clock_gettime() in a loop for 3 seconds. It shows us performance with a hot CPU cache (more clock_gettime() cycles - the better):
| before | CONFIG_TIME_NS=n | host | inside timens
--------------------------------------------------------------
| 161822960 | 161147792 | 160187142 | 150584782
| 161891728 | 161489804 | 159914989 | 150417019
| 161891770 | 161098734 | 160123179 | 150601277
| 161687686 | 161114738 | 159874249 | 150243276
| 161247151 | 161411636 | 159096392 | 149637536
--------------------------------------------------------------
avg | 161708259 | 161252540 | 159839190 | 150296778
diff % | 100 | 99.7 | 98.8 | 92.9-------------------------------------------------------------
stdev % | 0.2 | 0.1 | 0.1 | 0.2
The gettime_perf_cold test does 10K iterations. In each iteration, it drops cpu caches for vdso pages, clflush is used for this, then it runs rdtsc(); clock_gettime; rdtsc(); and prints the number of tsc cycles.
(lesser tsc per cycle - the better):
| before | CONFIG_TIME_NS=n | host | inside timens
--------------------------------------------------------------
tsc | 434 | 433 | 437 | 477 stdev(tsc) | 5 | 5 | 5 | 3 diff (%) | 1 | 1 | 100.1 | 109
vdsotest results: https://gist.github.com/avagin/f290afb8b721ae0522a561d585f34de0
The numbers gathered on Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz.
v5 on github (if someone prefers git pull or git log):
https://github.com/0x7f454c46/linux/tree/timens-v5
v4: https://lkml.kernel.org/r/[email protected]
v3: https://lkml.kernel.org/r/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]/
RFC: https://lkml.kernel.org/r/[email protected]/
v3..v4 Changes:
* CLOCKE_NEWTIME is unshare()-only flag now (CLON_PIDFD took previous value)
* Addressing Jann Horn's feedback - we don't allow CLONE_THREAD or CLONE_VM together with CLONE_NEWTIME (thanks for spotting!)
* Addressing issues found by Thomas - removed unmaintainable CLOCK_TIMENS and introduced another call back into k_clock to get ktime instead of getting timespec and converting it (Patch 03)
* Renaming timens_offsets members to omit _offset postfix (thanks Cyrill for the suggestion)
* Suggestions, renaming and making code more maintainable from Thomas's feedback (thanks much!)
* Fixing out-of-bounds and other issues in procfs file (kudos Jann Horn)
* vdso_fault() can be called on a remote task by /proc/$pid/mem or process_vm_readv() - addressed by adding a slow-path with searching for owner's namespace (thanks for spotting this unobvious issue, Jann)
* Other nits by Jann Horn
v2..v3: Major changes:
* Simplify two VDSO images by using static_branch() in vclock_gettime() Removes unwanted conflicts with generic VDSO movement patches and simplifies things by dropping too invasive linker magic.
As an alternative to static_branch() we tested an attempt to introduce home-made dynamic patching called retcalls:
https://github.com/0x7f454c46/linux/commit/4cc0180f6d65
Considering some theoretical problems with toolchains, we decided to go with long well-tested nop-patching in static_branch(). Though, it was needed to provide backend for relative code.
* address Thomas' comments.
* add sanity checks for offsets:
- the current clock time in a namespace has to be in [0, KTIME_MAX / 2). KTIME_MAX is divided by two here to be sure that the KTIME_MAX limit is still unreachable.
v1..v2: There are two major changes:
* Two versions of the VDSO library to avoid a performance penalty for host tasks outside time namespace (as suggested by Andy and Thomas).
As it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.
Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation VDSO code.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
* Allow to set clock offsets for a namespace only before any processes appear in it.
Now a time namespace looks similar to a pid namespace in a way how it is
created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace.
This scheme allows to create a new time namespaces, set clock offsets and then populate the namespace with processes.
Andrei Vagin (25):
ns: Introduce Time Namespace
timens: Add timens_offsets
posix-clocks: Rename the clock_get() into clock_get_timespec()
posix-clocks: Rename *_clock_get() functions into
*_clock_get_timespec()
alarmtimer: Rename gettime() callback to get_ktime()
alarmtimer: Provide get_timespec() callback
posix-clocks: Introduce clock_get_ktime() callback
posix-timers: Use clock_get_ktime() in common_timer_get()
posix-clocks: Introduce CLOCK_MONOTONIC time namespace offsets
posix-clocks: Introduce CLOCK_BOOTTIME time namespace offset
kernel: Add do_timens_ktime_to_host() helper
timerfd: Make timerfd_settime() time namespace aware
posix-timers: Make timer_settime() time namespace aware
alarmtimer: Make nanosleep time namespace aware
hrtimers: Prepare hrtimer_nanosleep() for time namespaces
posix-timers: Make clock_nanosleep() time namespace aware x86/vdso: Add offsets page in vvar
vdso: Introduce vdso_static_branch_unlikely()
x86/vdso: Enable static branches for the timens vdso fs/proc: Introduce /proc/pid/timens_offsets selftest/timens: Add a test for timerfd
selftest/timens: Add a test for clock_nanosleep() selftest/timens: Add timer offsets test
selftests/timens: Add a simple perf test for clock_gettime() selftest/timens: Check that a right vdso is mapped after fork and exec
Dmitry Safonov (12):
fd/proc: Respect boottime inside time namespace for /proc/uptime x86/vdso2c: Correct err messages on file opening x86/vdso2c: Convert iterator to unsigned x86/vdso/Makefile: Add vobjs32
x86/vdso: Restrict splitting VVAR VMA
x86/vdso: Rename vdso_image {.data=>.text}
x86/vdso: Allocate timens vdso
x86/vdso: Switch image on setns()/clone()
x86/vdso2c: Process jump tables
posix-clocks: Add align for timens_offsets selftest/timens: Add Time Namespace test for supported clocks selftest/timens: Add procfs selftest
MAINTAINERS | 3 + arch/Kconfig | 5 + arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 15 +- arch/x86/entry/vdso/vdso-layout.lds.S | 10 +- arch/x86/entry/vdso/vdso2c.c | 7 +- arch/x86/entry/vdso/vdso2c.h | 22 +- arch/x86/entry/vdso/vma.c | 177 +++++++- arch/x86/include/asm/jump_label.h | 14 + arch/x86/include/asm/vdso.h | 14 +- arch/x86/kernel/jump_label.c | 14 + fs/proc/base.c | 95 +++++ fs/proc/namespaces.c | 4 + fs/proc/uptime.c | 3 + fs/timerfd.c | 3 + include/linux/hrtimer.h | 2 +- include/linux/jump_label.h | 8 + include/linux/nsproxy.h | 2 + include/linux/posix-timers.h | 3 + include/linux/proc_ns.h | 3 + include/linux/time_namespace.h | 114 ++++++ include/linux/timens_offsets.h | 18 + include/linux/user_namespace.h | 1 + include/uapi/linux/sched.h | 6 + init/Kconfig | 9 + kernel/Makefile | 1 + kernel/fork.c | 16 +- kernel/nsproxy.c | 41 +- kernel/time/alarmtimer.c | 68 +++- kernel/time/hrtimer.c | 8 +- kernel/time/posix-clock.c | 8 +- kernel/time/posix-cpu-timers.c | 32 +- kernel/time/posix-stubs.c | 15 +- kernel/time/posix-timers.c | 89 ++-- kernel/time/posix-timers.h | 7 +- kernel/time_namespace.c | 385 ++++++++++++++++++ lib/vdso/gettimeofday.c | 53 +++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/timens/.gitignore | 8 + tools/testing/selftests/timens/Makefile | 12 + .../selftests/timens/clock_nanosleep.c | 102 +++++ tools/testing/selftests/timens/config | 1 + tools/testing/selftests/timens/exec.c | 93 +++++ tools/testing/selftests/timens/gettime_perf.c | 101 +++++ .../selftests/timens/gettime_perf_cold.c | 160 ++++++++ tools/testing/selftests/timens/log.h | 26 ++ tools/testing/selftests/timens/procfs.c | 144 +++++++ tools/testing/selftests/timens/timens.c | 185 +++++++++ tools/testing/selftests/timens/timens.h | 63 +++ tools/testing/selftests/timens/timer.c | 118 ++++++ tools/testing/selftests/timens/timerfd.c | 129 ++++++ 51 files changed, 2304 insertions(+), 115 deletions(-) create mode 100644 include/linux/time_namespace.h create mode 100644 include/linux/timens_offsets.h create mode 100644 kernel/time_namespace.c create mode 100644 tools/testing/selftests/timens/.gitignore create mode 100644 tools/testing/selftests/timens/Makefile create mode 100644 tools/testing/selftests/timens/clock_nanosleep.c create mode 100644 tools/testing/selftests/timens/config create mode 100644 tools/testing/selftests/timens/exec.c create mode 100644 tools/testing/selftests/timens/gettime_perf.c create mode 100644 tools/testing/selftests/timens/gettime_perf_cold.c create mode 100644 tools/testing/selftests/timens/log.h create mode 100644 tools/testing/selftests/timens/procfs.c create mode 100644 tools/testing/selftests/timens/timens.c create mode 100644 tools/testing/selftests/timens/timens.h create mode 100644 tools/testing/selftests/timens/timer.c create mode 100644 tools/testing/selftests/timens/timerfd.c
Signature · 1 line
2.22.0
PATCH v5 1/37 ns: Introduce Time Namespace
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Time Namespace isolates clock values.
The kernel provides access to several clocks CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, etc.
CLOCK_REALTIME
System-wide clock that measures real (i.e., wall-clock) time.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_BOOTTIME
Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
For many users, the time namespace means the ability to changes date and time in a container (CLOCK_REALTIME).
But in a context of the checkpoint/restore functionality, monotonic and bootime clocks become interesting. Both clocks are monotonic with unspecified staring points. These clocks are widely used to measure time slices and set timers. After restoring or migrating processes, we have to guarantee that they never go backward. In an ideal case, the behavior of these clocks should be the same as for a case when a whole system is suspended. All this means that we need to be able to set CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks, what can be done by adding per-namespace offsets for clocks.
A time namespace is similar to a pid namespace in a way how it is
created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace.
This scheme allows setting clock offsets for a namespace, before any processes appear in it.
All available clone flags have been used, so CLONE_NEWTIME uses the highest bit of CSIGNAL. It means that we can use it with the unshare system call only. Rith now, this works for us, because time namespace offsets can be set only when a new time namespace is not populated. In a future, we will have the clone3 system call [1] which will allow to use the CSIGNAL mask for clone flags.
[1]: httmps://lkml.kernel.org/r/[email protected]
--- MAINTAINERS | 2 + fs/proc/namespaces.c | 4 + include/linux/nsproxy.h | 2 + include/linux/proc_ns.h | 3 + include/linux/time_namespace.h | 69 +++++++++++ include/linux/user_namespace.h | 1 + include/uapi/linux/sched.h | 6 + init/Kconfig | 7 ++ kernel/Makefile | 1 + kernel/fork.c | 16 ++- kernel/nsproxy.c | 41 +++++-- kernel/time_namespace.c | 217 +++++++++++++++++++++++++++++++++ 12 files changed, 359 insertions(+), 10 deletions(-) create mode 100644 include/linux/time_namespace.h create mode 100644 kernel/time_namespace.c
MAINTAINERS +2 -0
@@ -12836,6 +12836,8 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core S: Maintained F: fs/timerfd.c F: include/linux/timer* +F: include/linux/time_namespace.h +F: kernel/time_namespace.c F: kernel/time/*timer* POWER MANAGEMENT CORE
fs/proc/namespaces.c +4 -0
@@ -33,6 +33,10 @@ static const struct proc_ns_operations *ns_entries[] = {#ifdef CONFIG_CGROUPS &cgroupns_operations,#endif +#ifdef CONFIG_TIME_NS + &timens_operations, + &timens_for_children_operations, +#endif }; static const char *proc_ns_get_link(struct dentry *dentry,
include/linux/nsproxy.h +2 -0
@@ -35,6 +35,8 @@ struct nsproxy {struct mnt_namespace *mnt_ns;struct pid_namespace *pid_ns_for_children;struct net *net_ns; +struct time_namespace *time_ns; +struct time_namespace *time_ns_for_children;struct cgroup_namespace *cgroup_ns; };extern struct nsproxy init_nsproxy;
include/linux/proc_ns.h +3 -0
@@ -32,6 +32,8 @@ extern const struct proc_ns_operations pidns_for_children_operations;extern const struct proc_ns_operations userns_operations;extern const struct proc_ns_operations mntns_operations;extern const struct proc_ns_operations cgroupns_operations; +extern const struct proc_ns_operations timens_operations; +extern const struct proc_ns_operations timens_for_children_operations;/* * We always define these enumerators
@@ -43,6 +45,7 @@ enum {PROC_USER_INIT_INO =0xEFFFFFFDU ,PROC_PID_INIT_INO =0xEFFFFFFCU ,PROC_CGROUP_INIT_INO =0xEFFFFFFBU , + PROC_TIME_INIT_INO = 0xEFFFFFFAU, };#ifdef CONFIG_PROC_FS
include/linux/time_namespace.h new file +69 -0
69 lines not on this page · raw message
include/linux/user_namespace.h +1 -0
@@ -45,6 +45,7 @@ enum ucount_type {UCOUNT_NET_NAMESPACES ,UCOUNT_MNT_NAMESPACES ,UCOUNT_CGROUP_NAMESPACES , +UCOUNT_TIME_NAMESPACES , #ifdef CONFIG_INOTIFY_USERUCOUNT_INOTIFY_INSTANCES , UCOUNT_INOTIFY_WATCHES,
include/uapi/linux/sched.h +6 -0
@@ -33,6 +33,12 @@#define CLONE_NEWNET 0x40000000/* New network namespace */ #define CLONE_IO 0x80000000/* Clone io context */ +/* +* cloning flags intersect with CSIGNAL so can be used with unshare and clone3 +* syscalls only: +*/ +#define CLONE_NEWTIME 0x00000080/* New time namespace */ +/* * Arguments for the clone3 syscall */
init/Kconfig +7 -0
@@ -1069,6 +1069,13 @@ config UTS_NS In this namespace tasks see different info provided with the uname() system call +config TIME_NS + bool "TIME namespace" + default y + help + In this namespace boottime and monotonic clocks can be set. + The time will keep going with the same pace. + config IPC_NS bool "IPC namespace" depends on (SYSVIPC || POSIX_MQUEUE)
kernel/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/ obj-$(CONFIG_UTS_NS) += utsname.o +obj-$(CONFIG_TIME_NS) += time_namespace.o obj-$(CONFIG_USER_NS) += user_namespace.o obj-$(CONFIG_PID_NS) += pid_namespace.o obj-$(CONFIG_IKCONFIG) += configs.o
kernel/fork.c +13 -3
@@ -1777,6 +1777,7 @@ static __latent_entropy struct task_struct *copy_process(struct multiprocess_signals delayed;struct file *pidfile =NULL ;u64 clone_flags = args->flags; +struct nsproxy *nsp = current->nsproxy;/* * Don't allow sharing the root directory with processes in a different
@@ -1819,8 +1820,16 @@ static __latent_entropy struct task_struct *copy_process(*/ if (clone_flags &CLONE_THREAD ) {if ((clone_flags & (CLONE_NEWUSER |CLONE_NEWPID )) || - (task_active_pid_ns (current) != - current->nsproxy->pid_ns_for_children)) + (task_active_pid_ns (current) != nsp->pid_ns_for_children)) +return ERR_PTR (-EINVAL ); + } + +/* +* If the new process will be in a different time namespace +* do not allow it to share VM or a thread group with the forking task. +*/ +if (clone_flags & (CLONE_THREAD |CLONE_VM )) { +if (nsp->time_ns != nsp->time_ns_for_children)return ERR_PTR (-EINVAL ); }
@@ -2707,7 +2716,8 @@ static int check_unshare_flags(unsigned long unshare_flags)if (unshare_flags & ~(CLONE_THREAD |CLONE_FS |CLONE_NEWNS |CLONE_SIGHAND |CLONE_VM |CLONE_FILES |CLONE_SYSVSEM |CLONE_NEWUTS |CLONE_NEWIPC |CLONE_NEWNET | -CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP )) +CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP | +CLONE_NEWTIME ))return -EINVAL ;/* * Not implemented, but pretend it works if there is nothing
kernel/nsproxy.c +34 -7
88 lines not on this page · raw message
kernel/time_namespace.c new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 2/37 timens: Add timens_offsets
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Introduce offsets for time namespace. They will contain an adjustment needed to convert clocks to/from host's.
Allocate one page for each time namespace that will be premapped into userspace among vvar pages.
--- MAINTAINERS | 1 + include/linux/time_namespace.h | 1 + include/linux/timens_offsets.h | 8 ++++++++ kernel/time_namespace.c | 14 ++++++++++++-- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 include/linux/timens_offsets.h
MAINTAINERS +1 -0
@@ -12837,6 +12837,7 @@ S: Maintained F: fs/timerfd.c F: include/linux/timer* F: include/linux/time_namespace.h +F: include/linux/timens_offsets.h F: kernel/time_namespace.c F: kernel/time/*timer*
include/linux/time_namespace.h +1 -0
@@ -8,6 +8,7 @@#include <linux/nsproxy.h> #include <linux/ns_common.h> #include <linux/err.h> +#include <linux/timens_offsets.h> struct user_namespace ;extern struct user_namespace init_user_ns;
include/linux/timens_offsets.h new file +8 -0
@@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_TIME_OFFSETS_H +#define _LINUX_TIME_OFFSETS_H + +struct timens_offsets { +}; + +#endif
kernel/time_namespace.c +12 -2
@@ -14,6 +14,7 @@#include <linux/slab.h> #include <linux/cred.h> #include <linux/err.h> +#include <linux/mm.h> static struct ucounts *inc_time_namespaces (struct user_namespace *ns) {
@@ -47,6 +48,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns, {struct time_namespace *ns;struct ucounts *ucounts; +struct page *page;int err; err = -ENOSPC ;
@@ -59,15 +61,22 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,if (!ns) goto fail_dec; + page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!page) + goto fail_free; + ns->offsets =page_address (page); +BUILD_BUG_ON (sizeof (*ns->offsets) >PAGE_SIZE ); + err =ns_alloc_inum (&ns->ns);if (err) - goto fail_free; + goto fail_page; ns->ucounts = ucounts; ns->ns.ops = &timens_operations; ns->user_ns =get_user_ns (user_ns);return ns; - +fail_page: +free_page ((unsigned long )ns->offsets); fail_free:kfree (ns); fail_dec:
@@ -95,6 +104,7 @@ void free_time_ns(struct kref *kref)struct time_namespace *ns; ns = container_of(kref, struct time_namespace, kref); +free_page ((unsigned long )ns->offsets);dec_time_namespaces (ns->ucounts);put_user_ns (ns->user_ns);ns_free_inum (&ns->ns);
Signature · 1 line
2.22.0
PATCH v5 3/37 posix-clocks: Rename the clock_get() into clock_get_timespec()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format, rather than in (struct timespec).
Rename clock_get() callback into clock_get_timespec() as a preparation for introducing clock_get_ktime().
--- kernel/time/alarmtimer.c | 4 ++-- kernel/time/posix-clock.c | 8 ++++---- kernel/time/posix-cpu-timers.c | 32 ++++++++++++++++---------------- kernel/time/posix-timers.c | 22 +++++++++++----------- kernel/time/posix-timers.h | 4 ++-- 5 files changed, 35 insertions(+), 35 deletions(-)
kernel/time/alarmtimer.c +2 -2
@@ -644,7 +644,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp }/** -* alarm_clock_get - posix clock_get interface +* alarm_clock_get - posix clock_get_timespec interface * @which_clock: clockid * @tp: timespec to fill. *
@@ -824,7 +824,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, - .clock_get = alarm_clock_get, + .clock_get_timespec = alarm_clock_get, .timer_create = alarm_timer_create, .timer_set = common_timer_set, .timer_del = common_timer_del,
kernel/time/posix-clock.c +4 -4
@@ -315,8 +315,8 @@ static int pc_clock_settime(clockid_t id, const struct timespec64 *ts) }const struct k_clock clock_posix_dynamic = { - .clock_getres = pc_clock_getres, - .clock_set = pc_clock_settime, - .clock_get = pc_clock_gettime, - .clock_adj = pc_clock_adjtime, + .clock_getres = pc_clock_getres, + .clock_set = pc_clock_settime, + .clock_get_timespec = pc_clock_gettime, + .clock_adj = pc_clock_adjtime, };
kernel/time/posix-cpu-timers.c +16 -16
@@ -1417,26 +1417,26 @@ static int thread_cpu_timer_create(struct k_itimer *timer) }const struct k_clock clock_posix_cpu = { - .clock_getres = posix_cpu_clock_getres, - .clock_set = posix_cpu_clock_set, - .clock_get = posix_cpu_clock_get, - .timer_create = posix_cpu_timer_create, - .nsleep = posix_cpu_nsleep, - .timer_set = posix_cpu_timer_set, - .timer_del = posix_cpu_timer_del, - .timer_get = posix_cpu_timer_get, - .timer_rearm = posix_cpu_timer_rearm, + .clock_getres = posix_cpu_clock_getres, + .clock_set = posix_cpu_clock_set, + .clock_get_timespec = posix_cpu_clock_get, + .timer_create = posix_cpu_timer_create, + .nsleep = posix_cpu_nsleep, + .timer_set = posix_cpu_timer_set, + .timer_del = posix_cpu_timer_del, + .timer_get = posix_cpu_timer_get, + .timer_rearm = posix_cpu_timer_rearm, };const struct k_clock clock_process = { - .clock_getres = process_cpu_clock_getres, - .clock_get = process_cpu_clock_get, - .timer_create = process_cpu_timer_create, - .nsleep = process_cpu_nsleep, + .clock_getres = process_cpu_clock_getres, + .clock_get_timespec = process_cpu_clock_get, + .timer_create = process_cpu_timer_create, + .nsleep = process_cpu_nsleep, };const struct k_clock clock_thread = { - .clock_getres = thread_cpu_clock_getres, - .clock_get = thread_cpu_clock_get, - .timer_create = thread_cpu_timer_create, + .clock_getres = thread_cpu_clock_getres, + .clock_get_timespec = thread_cpu_clock_get, + .timer_create = thread_cpu_timer_create, };
kernel/time/posix-timers.c +11 -11
@@ -667,7 +667,7 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)* The timespec64 based conversion is suboptimal, but it's not * worth to implement yet another callback. */ - kc->clock_get (timr->it_clock, &ts64); + kc->clock_get_timespec (timr->it_clock, &ts64); now =timespec64_to_ktime (ts64);/*
@@ -781,7 +781,7 @@ static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,* Posix magic: Relative CLOCK_REALTIME timers are not affected by * clock modifications, so they become CLOCK_MONOTONIC based under the * hood. See hrtimer_init(). Update timr->kclock, so the generic -* functions which use timr->kclock->clock_get() work. +* functions which use timr->kclock->clock_get_timespec() work. * * Note: it_clock stays unmodified, because the next timer_set() might * use ABSTIME, so it needs to switch back.
@@ -1032,7 +1032,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,if (!kc)return -EINVAL ; - error = kc->clock_get (which_clock, &kernel_tp); + error = kc->clock_get_timespec (which_clock, &kernel_tp);if (!error &&put_timespec64 (&kernel_tp, tp)) error = -EFAULT ;
@@ -1114,7 +1114,7 @@ SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,if (!kc)return -EINVAL ; - err = kc->clock_get (which_clock, &ts); + err = kc->clock_get_timespec (which_clock, &ts);if (!err &&put_old_timespec32 (&ts, tp)) err = -EFAULT ;
@@ -1226,7 +1226,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_clock_realtime_get, + .clock_get_timespec = posix_clock_realtime_get, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1243,7 +1243,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_ktime_get_ts, + .clock_get_timespec = posix_ktime_get_ts, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1258,22 +1258,22 @@ static const struct k_clock clock_monotonic = {static const struct k_clock clock_monotonic_raw = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_monotonic_raw, + .clock_get_timespec = posix_get_monotonic_raw, };static const struct k_clock clock_realtime_coarse = { .clock_getres = posix_get_coarse_res, - .clock_get = posix_get_realtime_coarse, + .clock_get_timespec = posix_get_realtime_coarse, };static const struct k_clock clock_monotonic_coarse = { .clock_getres = posix_get_coarse_res, - .clock_get = posix_get_monotonic_coarse, + .clock_get_timespec = posix_get_monotonic_coarse, };static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_tai, + .clock_get_timespec = posix_get_tai, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1288,7 +1288,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, - .clock_get = posix_get_boottime, + .clock_get_timespec = posix_get_boottime, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
kernel/time/posix-timers.h +2 -2
@@ -6,8 +6,8 @@ struct k_clock { struct timespec64 *tp);int (*clock_set)(const clockid_t which_clock,const struct timespec64 *tp); -int (*clock_get)(const clockid_t which_clock, -struct timespec64 *tp); +int (*clock_get_timespec)(const clockid_t which_clock, +struct timespec64 *tp);int (*clock_adj)(const clockid_t which_clock,struct __kernel_timex *tx);int (*timer_create)(struct k_itimer *timer); int (*nsleep)(const clockid_t which_clock, int flags,
Signature · 1 line
2.22.0
PATCH v5 4/37 posix-clocks: Rename *_clock_get() functions into *_clock_get_timespec()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format in (struct k_clock).
As a preparation ground for introducing clock_get_ktime(), the original callback clock_get() was renamed into clock_get_timespec(). Reflect the renaming into callbacks realizations.
--- kernel/time/alarmtimer.c | 6 +++--- kernel/time/posix-timers.c | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-)
kernel/time/alarmtimer.c +3 -3
@@ -644,13 +644,13 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp }/** -* alarm_clock_get - posix clock_get_timespec interface +* alarm_clock_get_timespec - posix clock_get_timespec interface * @which_clock: clockid * @tp: timespec to fill. * * Provides the underlying alarm base time. */ -static int alarm_clock_get (clockid_t which_clock,struct timespec64 *tp) +static int alarm_clock_get_timespec (clockid_t which_clock,struct timespec64 *tp) {struct alarm_base *base = &alarm_bases[clock2alarm (which_clock)];
@@ -824,7 +824,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, - .clock_get_timespec = alarm_clock_get, + .clock_get_timespec = alarm_clock_get_timespec, .timer_create = alarm_timer_create, .timer_set = common_timer_set, .timer_del = common_timer_del,
kernel/time/posix-timers.c +9 -8
@@ -165,7 +165,8 @@ static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) }/* Get clock_realtime */ -static int posix_clock_realtime_get (clockid_t which_clock,struct timespec64 *tp) +static int +posix_clock_realtime_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_real_ts64 (tp);return 0 ;
@@ -187,7 +188,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,/* * Get monotonic time for posix timers */ -static int posix_ktime_get_ts (clockid_t which_clock,struct timespec64 *tp) +static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;
@@ -222,13 +223,13 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *return 0 ; } -static int posix_get_boottime (const clockid_t which_clock,struct timespec64 *tp) +static int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp);return 0 ; } -static int posix_get_tai (clockid_t which_clock,struct timespec64 *tp) +static int posix_get_tai_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_clocktai_ts64 (tp);return 0 ;
@@ -1226,7 +1227,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_clock_realtime_get, + .clock_get_timespec = posix_clock_realtime_get_timespec, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1243,7 +1244,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_ktime_get_ts, + .clock_get_timespec = posix_get_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1273,7 +1274,7 @@ static const struct k_clock clock_monotonic_coarse = {static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_get_tai, + .clock_get_timespec = posix_get_tai_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1288,7 +1289,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_get_boottime, + .clock_get_timespec = posix_get_boottime_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
Signature · 1 line
2.22.0
PATCH v5 5/37 alarmtimer: Rename gettime() callback to get_ktime()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a tasks time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
struct alarm_base needs to follow the same name convention, so rename .gettime() callback into get_ktime() as a preparation for introducing get_timespec().
--- kernel/time/alarmtimer.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
kernel/time/alarmtimer.c +17 -17
@@ -36,13 +36,13 @@* struct alarm_base - Alarm timer bases * @lock: Lock for syncrhonized access to the base * @timerqueue: Timerqueue head managing the list of events -* @gettime: Function to read the time correlating to the base +* @get_ktime: Function to read the time correlating to the base * @base_clockid: clockid for the base */ static struct alarm_base {spinlock_t lock;struct timerqueue_head timerqueue; -ktime_t (*gettime)(void ); +ktime_t (*get_ktime)(void );clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ];
@@ -207,7 +207,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)spin_unlock_irqrestore (&base->lock, flags);if (alarm->function) - restart = alarm->function (alarm, base->gettime ()); + restart = alarm->function (alarm, base->get_ktime ());spin_lock_irqsave (&base->lock, flags);if (restart !=ALARMTIMER_NORESTART ) {
@@ -217,7 +217,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) }spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_fired (alarm, base->gettime ()); +trace_alarmtimer_fired (alarm, base->get_ktime ());return ret; }
@@ -225,7 +225,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)ktime_t alarm_expires_remaining (const struct alarm *alarm) {struct alarm_base *base = &alarm_bases[alarm->type]; -return ktime_sub (alarm->node.expires, base->gettime ()); +return ktime_sub (alarm->node.expires, base->get_ktime ()); }EXPORT_SYMBOL_GPL (alarm_expires_remaining);
@@ -270,7 +270,7 @@ static int alarmtimer_suspend(struct device *dev)spin_unlock_irqrestore (&base->lock, flags);if (!next)continue ; - delta =ktime_sub (next->expires, base->gettime ()); + delta =ktime_sub (next->expires, base->get_ktime ());if (!min || (delta < min)) { expires = next->expires; min = delta;
@@ -364,7 +364,7 @@ void alarm_start(struct alarm *alarm, ktime_t start)hrtimer_start (&alarm->timer, alarm->node.expires,HRTIMER_MODE_ABS );spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_start (alarm, base->gettime ()); +trace_alarmtimer_start (alarm, base->get_ktime ()); }EXPORT_SYMBOL_GPL (alarm_start);
@@ -377,7 +377,7 @@ void alarm_start_relative(struct alarm *alarm, ktime_t start) {struct alarm_base *base = &alarm_bases[alarm->type]; - start =ktime_add_safe (start, base->gettime ()); + start =ktime_add_safe (start, base->get_ktime ());alarm_start (alarm, start); }EXPORT_SYMBOL_GPL (alarm_start_relative);
@@ -414,7 +414,7 @@ int alarm_try_to_cancel(struct alarm *alarm)alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags); -trace_alarmtimer_cancel (alarm, base->gettime ()); +trace_alarmtimer_cancel (alarm, base->get_ktime ());return ret; }EXPORT_SYMBOL_GPL (alarm_try_to_cancel);
@@ -474,7 +474,7 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) {struct alarm_base *base = &alarm_bases[alarm->type]; -return alarm_forward (alarm, base->gettime (), interval); +return alarm_forward (alarm, base->get_ktime (), interval); }EXPORT_SYMBOL_GPL (alarm_forward_now);
@@ -500,7 +500,7 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)return ; } - delta =ktime_sub (absexp, base->gettime ()); + delta =ktime_sub (absexp, base->get_ktime ());spin_lock_irqsave (&freezer_delta_lock, flags);if (!freezer_delta || (delta < freezer_delta)) {
@@ -619,7 +619,7 @@ static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,struct alarm_base *base = &alarm_bases[alarm->type];if (!absolute) - expires =ktime_add_safe (expires, base->gettime ()); + expires =ktime_add_safe (expires, base->get_ktime ());if (sigev_none) alarm->node.expires = expires;else
@@ -657,7 +657,7 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpif (!alarmtimer_get_rtcdev ())return -EINVAL ; - *tp =ktime_to_timespec64 (base->gettime ()); + *tp =ktime_to_timespec64 (base->get_ktime ());return 0 ; }
@@ -734,7 +734,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct timespec64 rmt;ktime_t rem; - rem =ktime_sub (absexp, alarm_bases[type].gettime ()); + rem =ktime_sub (absexp, alarm_bases[type].get_ktime ());if (rem <=0 )return 0 ;
@@ -803,7 +803,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags, exp =timespec64_to_ktime (*tsreq);/* Convert (if necessary) to absolute time */ if (flags !=TIMER_ABSTIME ) { -ktime_t now = alarm_bases[type].gettime (); +ktime_t now = alarm_bases[type].get_ktime (); exp =ktime_add_safe (now, exp); }
@@ -868,9 +868,9 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; - alarm_bases[ALARM_REALTIME ].gettime = &ktime_get_real; + alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; alarm_bases[ALARM_BOOTTIME ].base_clockid =CLOCK_BOOTTIME ; - alarm_bases[ALARM_BOOTTIME ].gettime = &ktime_get_boottime; + alarm_bases[ALARM_BOOTTIME ].get_ktime = &ktime_get_boottime;for (i =0 ; i <ALARM_NUMTYPE ; i++) {timerqueue_init_head (&alarm_bases[i].timerqueue);spin_lock_init (&alarm_bases[i].lock);
Signature · 1 line
2.22.0
PATCH v5 6/37 alarmtimer: Provide get_timespec() callback
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
Wire up alarm bases with get_timespec().
--- include/linux/posix-timers.h | 3 +++ kernel/time/alarmtimer.c | 8 ++++++-- kernel/time/posix-timers.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-)
include/linux/posix-timers.h +3 -0
@@ -127,4 +127,7 @@ void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new);void posixtimer_rearm (struct kernel_siginfo *info); + +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp); +int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp);#endif
kernel/time/alarmtimer.c +6 -2
@@ -37,12 +37,15 @@* @lock: Lock for syncrhonized access to the base * @timerqueue: Timerqueue head managing the list of events * @get_ktime: Function to read the time correlating to the base +* @get_timespec: Function to read the namespace time correlating to the base * @base_clockid: clockid for the base */ static struct alarm_base {spinlock_t lock;struct timerqueue_head timerqueue;ktime_t (*get_ktime)(void ); +int (*get_timespec)(const clockid_t which_clock, +struct timespec64 *tp);clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ];
@@ -657,8 +660,7 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpif (!alarmtimer_get_rtcdev ())return -EINVAL ; - *tp =ktime_to_timespec64 (base->get_ktime ()); -return 0 ; +return base->get_timespec (base->base_clockid, tp); }/**
@@ -869,8 +871,10 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; + alarm_bases[ALARM_REALTIME ].get_timespec = posix_get_timespec, alarm_bases[ALARM_BOOTTIME ].base_clockid =CLOCK_BOOTTIME ; alarm_bases[ALARM_BOOTTIME ].get_ktime = &ktime_get_boottime; + alarm_bases[ALARM_BOOTTIME ].get_timespec = posix_get_boottime_timespec;for (i =0 ; i <ALARM_NUMTYPE ; i++) {timerqueue_init_head (&alarm_bases[i].timerqueue);spin_lock_init (&alarm_bases[i].lock);
kernel/time/posix-timers.c +2 -2
@@ -188,7 +188,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,/* * Get monotonic time for posix timers */ -static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;
@@ -223,7 +223,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *return 0 ; } -static int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) +int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp);return 0 ;
Signature · 1 line
2.22.0
PATCH v5 7/37 posix-clocks: Introduce clock_get_ktime() callback
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The callsite in common_timer_get() has already a comment:
/*
* The timespec64 based conversion is suboptimal, but it's not
* worth to implement yet another callback.
*/
kc->clock_get(timr->it_clock, &ts64);
now = timespec64_to_ktime(ts64);The upcoming support for time namespaces requires to have access to:
- The time in a task's time namespace for sys_clock_gettime()
- The time in the root name space for common_timer_get()
That adds a valid reason to finally implement a separate callback which returns the time in ktime_t format.
--- kernel/time/alarmtimer.c | 19 ++++++++++++++++++- kernel/time/posix-timers.c | 26 +++++++++++++++++++++++++- kernel/time/posix-timers.h | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +18 -1
@@ -651,7 +651,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp* @which_clock: clockid * @tp: timespec to fill. * -* Provides the underlying alarm base time. +* Provides the underlying alarm base time in a tasks time namespace. */ static int alarm_clock_get_timespec (clockid_t which_clock,struct timespec64 *tp) {
@@ -663,6 +663,22 @@ static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tpreturn base->get_timespec (base->base_clockid, tp); } +/** +* alarm_clock_get_ktime - posix clock_get_ktime interface +* @which_clock: clockid +* +* Provides the underlying alarm base time in the root namespace. +*/ +static ktime_t alarm_clock_get_ktime (clockid_t which_clock) +{ +struct alarm_base *base = &alarm_bases[clock2alarm (which_clock)]; + +if (!alarmtimer_get_rtcdev ()) +return -EINVAL ; + +return base->get_ktime (); +} +/** * alarm_timer_create - posix timer_create interface * @new_timer: k_itimer pointer to manage
@@ -826,6 +842,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,const struct k_clock alarm_clock = { .clock_getres = alarm_clock_getres, + .clock_get_ktime = alarm_clock_get_ktime, .clock_get_timespec = alarm_clock_get_timespec, .timer_create = alarm_timer_create, .timer_set = common_timer_set,
kernel/time/posix-timers.c +25 -1
@@ -172,6 +172,11 @@ posix_clock_realtime_get_timespec(clockid_t which_clock, struct timespec64 *tp)return 0 ; } +static ktime_t posix_clock_realtime_get_ktime (clockid_t which_clock) +{ +return ktime_get_real (); +} +/* Set clock_realtime */ static int posix_clock_realtime_set (const clockid_t which_clock,const struct timespec64 *tp)
@@ -194,6 +199,11 @@ int posix_get_timespec(clockid_t which_clock, struct timespec64 *tp)return 0 ; } +static ktime_t posix_get_ktime (clockid_t which_clock) +{ +return ktime_get (); +} +/* * Get monotonic-raw time for posix timers */
@@ -229,12 +239,22 @@ int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *return 0 ; } +static ktime_t posix_get_boottime_ktime (const clockid_t which_clock) +{ +return ktime_get_boottime (); +} +static int posix_get_tai_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_clocktai_ts64 (tp);return 0 ; } +static ktime_t posix_get_tai_ktime (clockid_t which_clock) +{ +return ktime_get_clocktai (); +} +static int posix_get_hrtimer_res (clockid_t which_clock,struct timespec64 *tp) { tp->tv_sec =0 ;
@@ -782,7 +802,7 @@ static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,* Posix magic: Relative CLOCK_REALTIME timers are not affected by * clock modifications, so they become CLOCK_MONOTONIC based under the * hood. See hrtimer_init(). Update timr->kclock, so the generic -* functions which use timr->kclock->clock_get_timespec() work. +* functions which use timr->kclock->clock_get_*() work. * * Note: it_clock stays unmodified, because the next timer_set() might * use ABSTIME, so it needs to switch back.
@@ -1228,6 +1248,7 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_clock_realtime_get_timespec, + .clock_get_ktime = posix_clock_realtime_get_ktime, .clock_set = posix_clock_realtime_set, .clock_adj = posix_clock_realtime_adj, .nsleep = common_nsleep,
@@ -1245,6 +1266,7 @@ static const struct k_clock clock_realtime = {static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_get_timespec, + .clock_get_ktime = posix_get_ktime, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set,
@@ -1274,6 +1296,7 @@ static const struct k_clock clock_monotonic_coarse = {static const struct k_clock clock_tai = { .clock_getres = posix_get_hrtimer_res, + .clock_get_ktime = posix_get_tai_ktime, .clock_get_timespec = posix_get_tai_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create,
@@ -1289,6 +1312,7 @@ static const struct k_clock clock_tai = {static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, + .clock_get_ktime = posix_get_boottime_ktime, .clock_get_timespec = posix_get_boottime_timespec, .nsleep = common_nsleep, .timer_create = common_timer_create,
kernel/time/posix-timers.h +3 -0
@@ -6,8 +6,11 @@ struct k_clock { struct timespec64 *tp);int (*clock_set)(const clockid_t which_clock,const struct timespec64 *tp); +/* Returns the clock value in the current time namespace. */ int (*clock_get_timespec)(const clockid_t which_clock,struct timespec64 *tp); +/* Returns the clock value in the root time namespace. */ +ktime_t (*clock_get_ktime)(const clockid_t which_clock);int (*clock_adj)(const clockid_t which_clock,struct __kernel_timex *tx);int (*timer_create)(struct k_itimer *timer); int (*nsleep)(const clockid_t which_clock, int flags,
Signature · 1 line
2.22.0
PATCH v5 8/37 posix-timers: Use clock_get_ktime() in common_timer_get()
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Now, when the clock_get_ktime() callback exists, the suboptimal timespec64-based conversion can be removed from common_timer_get().
--- kernel/time/posix-timers.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/time/posix-timers.c +1 -7
@@ -666,7 +666,6 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) {const struct k_clock *kc = timr->kclock;ktime_t now, remaining, iv; -struct timespec64 ts64;bool sig_none; sig_none = timr->it_sigev_notify ==SIGEV_NONE ;
@@ -684,12 +683,7 @@ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)return ; } -/* -* The timespec64 based conversion is suboptimal, but it's not -* worth to implement yet another callback. -*/ - kc->clock_get_timespec (timr->it_clock, &ts64); - now =timespec64_to_ktime (ts64); + now = kc->clock_get_ktime (timr->it_clock);/* * When a requeue is pending or this is a SIGEV_NONE timer move the
Signature · 1 line
2.22.0
PATCH v5 9/37 posix-clocks: Introduce CLOCK_MONOTONIC time namespace offsets
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Add monotonic time virtualisation for time namespace. Introduce timespec for monotionic clock into timens offsets and wire clock_gettime() syscall.
--- include/linux/time_namespace.h | 8 ++++++++ include/linux/timens_offsets.h | 1 + kernel/time/posix-stubs.c | 2 ++ kernel/time/posix-timers.c | 4 ++++ 4 files changed, 15 insertions(+)
include/linux/time_namespace.h +8 -0
@@ -40,6 +40,13 @@ static inline void put_time_ns(struct time_namespace *ns) kref_put(&ns->kref, free_time_ns); } +static inline void timens_add_monotonic (struct timespec64 *ts) +{ +struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + +if (ns_offsets) + *ts =timespec64_add (*ts, ns_offsets->monotonic); +}#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns)
@@ -65,6 +72,7 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsreturn 0 ; } +static inline void timens_add_monotonic (struct timespec64 *ts) {}#endif #endif /* _LINUX_TIMENS_H */
include/linux/timens_offsets.h +1 -0
@@ -3,6 +3,7 @@#define _LINUX_TIME_OFFSETS_Hstruct timens_offsets { +struct timespec64 monotonic; };#endif
kernel/time/posix-stubs.c +2 -0
@@ -14,6 +14,7 @@#include <linux/ktime.h> #include <linux/timekeeping.h> #include <linux/posix-timers.h> +#include <linux/time_namespace.h> #include <linux/compat.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
@@ -77,6 +78,7 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)break ;case CLOCK_MONOTONIC :ktime_get_ts64 (tp); +timens_add_monotonic (tp);break ;case CLOCK_BOOTTIME :ktime_get_boottime_ts64 (tp);
kernel/time/posix-timers.c +4 -0
@@ -30,6 +30,7 @@#include <linux/hashtable.h> #include <linux/compat.h> #include <linux/nospec.h> +#include <linux/time_namespace.h> #include "timekeeping.h" #include "posix-timers.h"
@@ -196,6 +197,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
@@ -210,6 +212,7 @@ static ktime_t posix_get_ktime(clockid_t which_clock)static int posix_get_monotonic_raw (clockid_t which_clock,struct timespec64 *tp) {ktime_get_raw_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
@@ -224,6 +227,7 @@ static int posix_get_monotonic_coarse(clockid_t which_clock, struct timespec64 *tp) {ktime_get_coarse_ts64 (tp); +timens_add_monotonic (tp);return 0 ; }
Signature · 1 line
2.22.0
PATCH v5 10/37 posix-clocks: Introduce CLOCK_BOOTTIME time namespace offset
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Adds boottime virtualisation for time namespace. Introduce timespec for boottime clock into timens offsets and wire clock_gettime() syscall.
--- include/linux/time_namespace.h | 9 +++++++++ include/linux/timens_offsets.h | 1 + kernel/time/alarmtimer.c | 1 + kernel/time/posix-stubs.c | 1 + kernel/time/posix-timers.c | 1 + 5 files changed, 13 insertions(+)
include/linux/time_namespace.h +9 -0
@@ -48,6 +48,14 @@ static inline void timens_add_monotonic(struct timespec64 *ts) *ts = timespec64_add(*ts, ns_offsets->monotonic); } +static inline void timens_add_boottime (struct timespec64 *ts) +{ +struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + +if (ns_offsets) + *ts =timespec64_add (*ts, ns_offsets->boottime); +} +#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns) {
@@ -73,6 +81,7 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *ts }static inline void timens_add_monotonic (struct timespec64 *ts) {} +static inline void timens_add_boottime (struct timespec64 *ts) {}#endif #endif /* _LINUX_TIMENS_H */
include/linux/timens_offsets.h +1 -0
@@ -4,6 +4,7 @@struct timens_offsets {struct timespec64 monotonic; +struct timespec64 boottime; };#endif
kernel/time/alarmtimer.c +1 -0
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/time_namespace.h> #include "posix-timers.h"
kernel/time/posix-stubs.c +1 -0
@@ -82,6 +82,7 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)break ;case CLOCK_BOOTTIME :ktime_get_boottime_ts64 (tp); +timens_add_boottime (tp);break ;default :return -EINVAL ;
kernel/time/posix-timers.c +1 -0
@@ -240,6 +240,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *int posix_get_boottime_timespec (const clockid_t which_clock,struct timespec64 *tp) {ktime_get_boottime_ts64 (tp); +timens_add_boottime (tp);return 0 ; }
Signature · 1 line
2.22.0
PATCH v5 11/37 kernel: Add do_timens_ktime_to_host() helper
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
The helper subtracts namespace's clock offset from the given time and checks that the result is in [0, KTIME_MAX].
--- include/linux/time_namespace.h | 17 ++++++++++++++ kernel/time_namespace.c | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
include/linux/time_namespace.h +17 -0
@@ -56,6 +56,18 @@ static inline void timens_add_boottime(struct timespec64 *ts) *ts = timespec64_add(*ts, ns_offsets->boottime); } +ktime_t do_timens_ktime_to_host (clockid_t clockid,ktime_t tim, +struct timens_offsets *offsets); +static inline ktime_t timens_ktime_to_host (clockid_t clockid,ktime_t tim) +{ +struct timens_offsets *offsets = current->nsproxy->time_ns->offsets; + +if (!offsets) +return tim; + +return do_timens_ktime_to_host (clockid, tim, offsets); +} +#else static inline struct time_namespace *get_time_ns (struct time_namespace *ns) {
@@ -82,6 +94,11 @@ static inline int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsstatic inline void timens_add_monotonic (struct timespec64 *ts) {}static inline void timens_add_boottime (struct timespec64 *ts) {} + +static inline ktime_t timens_ktime_to_host (clockid_t clockid,ktime_t tim) +{ +return tim; +}#endif #endif /* _LINUX_TIMENS_H */
kernel/time_namespace.c +43 -0
49 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v5 12/37 timerfd: Make timerfd_settime() time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
timerfd_settime() accepts an absolute value of the experation time if TFD_TIMER_ABSTIME is specified. This value is in task's time namespace and has to be converted to the host's time namespace.
--- fs/timerfd.c | 3 +++ 1 file changed, 3 insertions(+)
fs/timerfd.c +3 -0
@@ -26,6 +26,7 @@#include <linux/syscalls.h> #include <linux/compat.h> #include <linux/rcupdate.h> +#include <linux/time_namespace.h> struct timerfd_ctx { union {
@@ -196,6 +197,8 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags, }if (texp !=0 ) { +if (flags &TFD_TIMER_ABSTIME ) + texp =timens_ktime_to_host (clockid, texp);if (isalarm (ctx)) {if (flags &TFD_TIMER_ABSTIME )alarm_start (&ctx->t.alarm, texp);
Signature · 1 line
2.22.0
PATCH v5 13/37 posix-timers: Make timer_settime() time namespace aware
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Wire timer_settime() syscall into time namespace virtualization.
sys_timer_settime() calls the ktime->timer_set() callback. Right now, common_timer_set() is the only implementation for the callback.
There user-supplied timer's value is converted from timespec64 to ktime and then timens_ktime_to_host() can be used to convert namespace's time to the host time.
Inside a time namespace kernel's time differ on a fixed offset from a user-supplied, but only absolute values (TIMER_ABSTIME) must be converted.
--- kernel/time/posix-timers.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/time/posix-timers.c +2 -0
@@ -857,6 +857,8 @@ int common_timer_set(struct k_itimer *timr, int flags, timr->it_interval =timespec64_to_ktime (new_setting->it_interval); expires =timespec64_to_ktime (new_setting->it_value); +if (flags &TIMER_ABSTIME ) + expires =timens_ktime_to_host (timr->it_clock, expires); sigev_none = timr->it_sigev_notify ==SIGEV_NONE ; kc->timer_arm (timr, expires, flags &TIMER_ABSTIME , sigev_none);
Signature · 1 line
2.22.0
Sorry for spamming this twice.
I did git send-email --to [email protected] /tmp/timens/ --cc 'Dmitry Safonov <[email protected]>' /tmp/timens/
And didn't notice that I've put the same folder ^^ twice :(
Pardon again.
Quoted 4 lines · Dmitry Safonov
Discussions around time namespace are there for a long time. The first attempt to implement it was in 2006 by Jeff Dike. From that time, the topic appears on and off in various discussions.
[..]
Thanks,
Dmitry
Andy Lutomirski
maintainerRe: [PATCHv5 28/37] x86/vdso: Enable static branches for the timens vdso
In reply to Dmitry SafonovQuoted 10 lines · Dmitry Safonov
From: Andrei Vagin <[email protected]>
As it has been discussed on timens RFC, adding a new conditional branch
if (inside_time_ns)on VDSO for all processes is undesirable.Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace withclk_to_ns()that subtracts offsets from host's time.
Quoted 5 lines
The timens code in vdso looks like this:
if (timens_static_branch_unlikely()) { clk_to_ns(clk, ts); }
I'm confused. Now we effectively have *three* versions: the vDSO without timens, and vDSO with timens but with it switched off, and the vDSO with timens on. This seems like too much.
What you need is, IMO, a static-branch-ish thing that is per mm. This has a fundamental problem that the vDSO can be modified using FOLL_FORCE. Perhaps any CoW of the vDSO should implicitly switch the static branch on, which at least gives some degree of correctness even if it's a bit surprising.
--Andy
Andy Lutomirski
maintainerRe: [PATCHv5 23/37] x86/vdso: Add offsets page in vvar
In reply to Dmitry SafonovQuoted 147 lines · Dmitry Safonov
From: Andrei Vagin <[email protected]>
As modern applications fetch time from VDSO without entering the kernel, it's needed to provide offsets for userspace code inside time namespace.
A page for timens offsets is allocated on time namespace construction. Put that page into VVAR for tasks inside timens and zero page for host processes.
As VDSO code is already optimized as much as possible in terms of speed, any new if-condition in VDSO code is undesirable; the goal is to provide two .so(s), as was originally suggested by Andy and Thomas:
- for host tasks with optimized-outclk_to_ns()without any penalty
- for processes inside timens withclk_to_ns()
For this purpose, defineclk_to_ns()underCONFIG_TIME_NS.To eliminate any performance regression,
clk_to_ns()will be called understatic_branchwith follow-up patches, that adds support for patching vdso.VDSO mappings are platform-specific, add
Kconfigdependency for arch.Signed-off-by Andrei Vagin <[email protected]>Co-developed-by Dmitry Safonov <[email protected]>Signed-off-by Dmitry Safonov <[email protected]>--- arch/Kconfig | 5 +++ arch/x86/Kconfig | 1 + arch/x86/entry/vdso/vdso-layout.lds.S | 9 ++++- arch/x86/entry/vdso/vdso2c.c | 3 ++ arch/x86/entry/vdso/vma.c | 12 +++++++ arch/x86/include/asm/vdso.h | 1 + init/Kconfig | 1 + lib/vdso/gettimeofday.c | 47 +++++++++++++++++++++++++++ 8 files changed, 78 insertions(+), 1 deletion(-)arch/Kconfig +5
-0@@ -729,6 +729,11 @@ config HAVE_ARCH_NVRAM_OPS config ISA_BUS_API def_bool ISA +config ARCH_HAS_VDSO_TIME_NS + bool + help + VDSO can add time-ns offsets without entering kernel. + # # ABI hall of shame #arch/x86/Kconfig +1
-0@@ -81,6 +81,7 @@ config X86 select ARCH_HAS_STRICT_MODULE_RWX select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_HAS_VDSO_TIME_NS select ARCH_HAVE_NMI_SAFE_CMPXCHG select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI select ARCH_MIGHT_HAVE_PC_PARPORTarch/x86/entry/vdso/vdso-layout.lds.S +8
-1@@ -7,6 +7,12 @@ * This script controls its layout. */ +#ifdef CONFIG_TIME_NS +# define TIMENS_SZ PAGE_SIZE +#else +# define TIMENS_SZ 0 +#endif + SECTIONS { /*@@ -16,7 +22,7 @@ SECTIONS * segment. */ - vvar_start = . - 3 * PAGE_SIZE; + vvar_start = . - (3 * PAGE_SIZE + TIMENS_SZ); vvar_page = vvar_start; /* Place all vvars at the offsets in asm/vvar.h. */@@ -28,6 +34,7 @@ SECTIONS pvclock_page = vvar_start + PAGE_SIZE; hvclock_page = vvar_start + 2 * PAGE_SIZE; + timens_page = vvar_start + 3 * PAGE_SIZE; . = SIZEOF_HEADERS;arch/x86/entry/vdso/vdso2c.c +3
-0@@ -75,12 +75,14 @@ enum { sym_vvar_page, sym_pvclock_page, sym_hvclock_page, + sym_timens_page, };const int special_pages[] = { sym_vvar_page, sym_pvclock_page, sym_hvclock_page, + sym_timens_page, }; struct vdso_sym {@@ -93,6 +95,7 @@ struct vdso_sym required_syms[] = { [sym_vvar_page] = {"vvar_page", true}, [sym_pvclock_page] = {"pvclock_page", true}, [sym_hvclock_page] = {"hvclock_page", true}, + [sym_timens_page] = {"timens_page", true}, {"VDSO32_NOTE_MASK", true}, {"__kernel_vsyscall", true}, {"__kernel_sigreturn", true},arch/x86/entry/vdso/vma.c +2
-0@@ -14,6 +14,7 @@#include <linux/elf.h> #include <linux/cpu.h> #include <linux/ptrace.h> +#include <linux/time_namespace.h> #include <asm/pvclock.h> #include <asm/vgtod.h> #include <asm/proto.h> @@ -23,6 +24,7 @@#include <asm/desc.h> #include <asm/cpufeature.h> #include <clocksource/hyperv_timer.h> +#include <asm/page.h> #if defined(CONFIG_X86_64 )unsigned int __read_mostly vdso64_enabled =1 ;
arch/x86/entry/vdso/vma.c static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, · line 141
Quoted 5 lines
arch/x86/entry/vdso/vma.c +2
-0@@ -135,6 +137,16 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,if (tsc_pg &&vclock_was_used (VCLOCK_HVCLOCK ))return vmf_insert_pfn (vma, vmf->address,vmalloc_to_pfn (tsc_pg)); + }else if (sym_offset == image->sym_timens_page) { +struct time_namespace *ns = current->nsproxy->time_ns;
What, if anything, guarantees that all tasks in the mm share the same timens?
--Andy
Andy Lutomirski
maintainerRe: [PATCHv5 21/37] x86/vdso: Restrict splitting VVAR VMA
In reply to Dmitry SafonovQuoted 7 lines · Dmitry Safonov
Although, time namespace can work with VVAR VMA split, it seems worth to forbid splitting VVAR resulting in stricter ABI and reducing amount of corner-cases to consider while working further on VDSO.
I don't think there is any use-case for partial
mremap()of vvar, but if there is any - this patch can be easily reverted.
Seems reasonable to me.
Andy Lutomirski
maintainerRe: [PATCHv5 01/37] ns: Introduce Time Namespace
In reply to Dmitry Safonovkernel/time_namespace.c line 151
Quoted 11 lines · Dmitry Safonov
From: Andrei Vagin <[email protected]>
Time Namespace isolates clock values.
kernel/time_namespace.c new file +6
-0+static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) +{ +struct time_namespace *ns =to_time_ns (new); + +if (!thread_group_empty (current)) +return -EINVAL ;
You also need to check for other users of the mm.
kernel/time_namespace.c line 163
Quoted 12 lines
kernel/time_namespace.c new file +12
-0+ +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || + !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) +return -EPERM ; + +get_time_ns (ns); +get_time_ns (ns); +put_time_ns (nsproxy->time_ns); +put_time_ns (nsproxy->time_ns_for_children); + nsproxy->time_ns = ns; + nsproxy->time_ns_for_children = ns; + ns->initialized = true;
I really really wish that setns() took an explicit flag for "change now" or "change for children", since the semantics are different. Oh well.
Andy Lutomirski
maintainerRe: [PATCHv5 25/37] x86/vdso: Switch image on setns()/clone()
In reply to Dmitry SafonovQuoted 14 lines · Dmitry Safonov
As it has been discussed on timens RFC, adding a new conditional branch
if (inside_time_ns)on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.arch/x86/entry/vdso/vma.c +6
-0+#ifdef CONFIG_TIME_NS +int vdso_join_timens (struct task_struct *task) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; +
arch/x86/entry/vdso/vma.c static const struct vm_special_mapping vvar_mapping = { · line 285
Quoted 10 lines
arch/x86/entry/vdso/vma.c +10
-0+if (down_write_killable (&mm->mmap_sem)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, &vvar_mapping) || +vma_is_special_mapping (vma, &vdso_mapping)) +zap_page_range (vma, vma->vm_start, size); + }
This is, unfortunately, fundamentally buggy. If any thread is in the vDSO or has the vDSO on the stack (due to a signal, for example), this will crash it. I can think of three solutions:
1. Say that you can't setns() if you have other mms and ignore the signal issue. Anything with green threads will disapprove. It's also rather gross.
2. Make it so that you can flip the static branch safely. As in my other email, you'll need to deal with CoW somehow,
3. Make it so that you can't change timens, or at least that you can't turn timens on or off, without execve() or fork().
BTW, that static branch probably needs to be aligned to a cache line or something similar to avoid all the nastiness with trying to poke text that might be concurrently executing. This will be a mess.
[email protected]
reviewerRe: [PATCHv5 25/37] x86/vdso: Switch image on setns()/clone()
Quoted 39 lines · Andy Lutomirski
Quoted 2 lines · Dmitry Safonov
As it has been discussed on timens RFC, adding a new conditional
branch
Quoted 2 lines
if (inside_time_ns)on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor maymispredict
Quoted 10 lines
the jump. Also there are instruction cache lines wasted on cmp/jmp.
arch/x86/entry/vdso/vma.c +6
-0+#ifdef CONFIG_TIME_NS +int vdso_join_timens (struct task_struct *task) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; +arch/x86/entry/vdso/vma.c static const struct vm_special_mapping vvar_mapping = { · line 285
Quoted 10 lines
arch/x86/entry/vdso/vma.c +10
-0+if (down_write_killable (&mm->mmap_sem)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, &vvar_mapping) || +vma_is_special_mapping (vma, &vdso_mapping)) +zap_page_range (vma, vma->vm_start, size); + }This is, unfortunately, fundamentally buggy. If any thread is in the vDSO or has the vDSO on the stack (due to a signal, for example), this will crash it. I can think of three solutions:
1. Say that you can't
setns()if you have other mms and ignore the signal issue. Anything with green threads will disapprove. It's also rather gross.2. Make it so that you can flip the static branch safely. As in my other email, you'll need to deal with CoW somehow,
Quoted 6 lines
3. Make it so that you can't change timens, or at least that you can't turn timens on or off, without
execve()orfork().BTW, that static branch probably needs to be aligned to a cache line or something similar to avoid all the nastiness with trying to poke text that might be concurrently executing. This will be a mess.
Since we are talking about different physical addresses I believe we should be okay as long as they don't cross page boundaries, and even if they do it can be managed with proper page invalidation sequencing – it's not like the problems of having to deal with XMC on live pages like in the kernel.
Still, you really need each instruction sequence to be present, with the only difference being specific patch sites.
Any fundamental reason this can't be strictly data driven? Seems odd to me if it couldn't, but I might be missing something obvious.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCHv5 28/37] x86/vdso: Enable static branches for the timens vdso
In reply to Andy LutomirskiQuoted 10 lines · Andy Lutomirski
Quoted 9 lines · Dmitry Safonov
As it has been discussed on timens RFC, adding a new conditional branch
if (inside_time_ns)on VDSO for all processes is undesirable.Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace withclk_to_ns()that subtracts offsets from host's time.The timens code in vdso looks like this:
Quoted 7 lines
Quoted 3 lines
if (timens_static_branch_unlikely()) { clk_to_ns(clk, ts); }I'm confused. Now we effectively have *three* versions: the vDSO without timens, and vDSO with timens but with it switched off, and the vDSO with timens on. This seems like too much.
The problem is that if you have a single VDSO, then omce one process joins a time namespace _ALL_ other processes get an extra conditional with at least one extra cache line as a bonus.
This has been discussed at Plumbers last year and the agreement was to create VDSO plain (no namespace support) and VDSO extra (namespace support).
The performance hit of the conditional + one etra cache line for tasks which do not use a time name space is measurable.
That also avoids the whole mess of dealing with the static branch being flipped. The time name space property is determined on fork/exec _before_ anything can touch the VDSO and depending on it the approriate version of the VDSO is loaded.
Thanks,
tglx
Andy Lutomirski
maintainerRe: [PATCHv5 25/37] x86/vdso: Switch image on setns()/clone()
In reply to [email protected]Quoted 48 lines · On Wed, Jul 31, 2019 at 11:09 PM <[email protected]> wrote:
Quoted 39 lines · Andy Lutomirski
Quoted 2 lines · Dmitry Safonov
As it has been discussed on timens RFC, adding a new conditional
branch
Quoted 2 lines
if (inside_time_ns)on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor maymispredict
Quoted 10 lines
the jump. Also there are instruction cache lines wasted on cmp/jmp.
arch/x86/entry/vdso/vma.c +6
-0+#ifdef CONFIG_TIME_NS +int vdso_join_timens (struct task_struct *task) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; +arch/x86/entry/vdso/vma.c static const struct vm_special_mapping vvar_mapping = { · line 285
Quoted 10 lines
arch/x86/entry/vdso/vma.c +10
-0+if (down_write_killable (&mm->mmap_sem)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, &vvar_mapping) || +vma_is_special_mapping (vma, &vdso_mapping)) +zap_page_range (vma, vma->vm_start, size); + }This is, unfortunately, fundamentally buggy. If any thread is in the vDSO or has the vDSO on the stack (due to a signal, for example), this will crash it. I can think of three solutions:
1. Say that you can't
setns()if you have other mms and ignore the signal issue. Anything with green threads will disapprove. It's also rather gross.2. Make it so that you can flip the static branch safely. As in my other email, you'll need to deal with CoW somehow,
Quoted 6 lines
3. Make it so that you can't change timens, or at least that you can't turn timens on or off, without
execve()orfork().BTW, that static branch probably needs to be aligned to a cache line or something similar to avoid all the nastiness with trying to poke text that might be concurrently executing. This will be a mess.
Quoted 5 lines
Since we are talking about different physical addresses I believe we should be okay as long as they don't cross page boundaries, and even if they do it can be managed with proper page invalidation sequencing – it's not like the problems of having to deal with XMC on live pages like in the kernel.
Still, you really need each instruction sequence to be present, with the only difference being specific patch sites.
Any fundamental reason this can't be strictly data driven? Seems odd to me if it couldn't, but I might be missing something obvious.
I think it can be. There are at least two places where vDSO slow paths could hook without affecting fast paths: vclock_mode and the low bit of the sequence number.
Re: [PATCHv5 01/37] ns: Introduce Time Namespace
In reply to Andy LutomirskiHi Andy,
Thank you for the review,
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Dmitry Safonov
From: Andrei Vagin <[email protected]>
Time Namespace isolates clock values.
Quoted 8 lines
kernel/time_namespace.c line 151
Quoted 6 lines
kernel/time_namespace.c new file +6
-0+static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) +{ +struct time_namespace *ns =to_time_ns (new); + +if (!thread_group_empty (current)) +return -EINVAL ;You also need to check for other users of the mm.
Oops. It seems like, if the check was
if (!current_is_single_threaded())
return -EUSERS;
instead of thread_group_empty(current), it would address the concerns from 23/37 and 25/37 patches, too?
Quoted 18 lines
kernel/time_namespace.c line 163
Quoted 13 lines
kernel/time_namespace.c new file +12
-0+ +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || + !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) +return -EPERM ; + +get_time_ns (ns); +get_time_ns (ns); +put_time_ns (nsproxy->time_ns); +put_time_ns (nsproxy->time_ns_for_children); + nsproxy->time_ns = ns; + nsproxy->time_ns_for_children = ns; + ns->initialized = true;I really really wish that
setns()took an explicit flag for "change now" or "change for children", since the semantics are different. Oh well.
Thanks,
Dmitry
PATCH v6 1/37 ns: Introduce Time Namespace
In reply to Dmitry SafonovFrom: Andrei Vagin <[email protected]>
Time Namespace isolates clock values.
The kernel provides access to several clocks CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, etc.
CLOCK_REALTIME
System-wide clock that measures real (i.e., wall-clock) time.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_BOOTTIME
Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
For many users, the time namespace means the ability to changes date and time in a container (CLOCK_REALTIME).
But in a context of the checkpoint/restore functionality, monotonic and bootime clocks become interesting. Both clocks are monotonic with unspecified staring points. These clocks are widely used to measure time slices and set timers. After restoring or migrating processes, we have to guarantee that they never go backward. In an ideal case, the behavior of these clocks should be the same as for a case when a whole system is suspended. All this means that we need to be able to set CLOCK_MONOTONIC and CLOCK_BOOTTIME clocks, what can be done by adding per-namespace offsets for clocks.
A time namespace is similar to a pid namespace in a way how it is
created: unshare(CLONE_NEWTIME) system call creates a new time namespace, but doesn't set it to the current process. Then all children of the process will be born in the new time namespace, or a process can use the setns() system call to join a namespace.
This scheme allows setting clock offsets for a namespace, before any processes appear in it.
All available clone flags have been used, so CLONE_NEWTIME uses the highest bit of CSIGNAL. It means that we can use it with the unshare system call only. Rith now, this works for us, because time namespace offsets can be set only when a new time namespace is not populated. In a future, we will have the clone3 system call [1] which will allow to use the CSIGNAL mask for clone flags.
[1]: httmps://lkml.kernel.org/r/[email protected]
---
v5..v6 Change:
Used current_is_single_threaded() instead of thread_group_empty(). Changed errno code to something more grepabble (EUSERS).
MAINTAINERS | 2 + fs/proc/namespaces.c | 4 + include/linux/nsproxy.h | 2 + include/linux/proc_ns.h | 3 + include/linux/time_namespace.h | 69 +++++++++++ include/linux/user_namespace.h | 1 + include/uapi/linux/sched.h | 6 + init/Kconfig | 7 ++ kernel/Makefile | 1 + kernel/fork.c | 16 ++- kernel/nsproxy.c | 41 +++++-- kernel/time_namespace.c | 217 +++++++++++++++++++++++++++++++++ 12 files changed, 359 insertions(+), 10 deletions(-) create mode 100644 include/linux/time_namespace.h create mode 100644 kernel/time_namespace.c
MAINTAINERS +2 -0
@@ -12841,6 +12841,8 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core S: Maintained F: fs/timerfd.c F: include/linux/timer* +F: include/linux/time_namespace.h +F: kernel/time_namespace.c F: kernel/time/*timer* POWER MANAGEMENT CORE
fs/proc/namespaces.c +4 -0
@@ -33,6 +33,10 @@ static const struct proc_ns_operations *ns_entries[] = {#ifdef CONFIG_CGROUPS &cgroupns_operations,#endif +#ifdef CONFIG_TIME_NS + &timens_operations, + &timens_for_children_operations, +#endif }; static const char *proc_ns_get_link(struct dentry *dentry,
include/linux/nsproxy.h +2 -0
@@ -35,6 +35,8 @@ struct nsproxy {struct mnt_namespace *mnt_ns;struct pid_namespace *pid_ns_for_children;struct net *net_ns; +struct time_namespace *time_ns; +struct time_namespace *time_ns_for_children;struct cgroup_namespace *cgroup_ns; };extern struct nsproxy init_nsproxy;
include/linux/proc_ns.h +3 -0
@@ -32,6 +32,8 @@ extern const struct proc_ns_operations pidns_for_children_operations;extern const struct proc_ns_operations userns_operations;extern const struct proc_ns_operations mntns_operations;extern const struct proc_ns_operations cgroupns_operations; +extern const struct proc_ns_operations timens_operations; +extern const struct proc_ns_operations timens_for_children_operations;/* * We always define these enumerators
@@ -43,6 +45,7 @@ enum {PROC_USER_INIT_INO =0xEFFFFFFDU ,PROC_PID_INIT_INO =0xEFFFFFFCU ,PROC_CGROUP_INIT_INO =0xEFFFFFFBU , + PROC_TIME_INIT_INO = 0xEFFFFFFAU, };#ifdef CONFIG_PROC_FS
include/linux/time_namespace.h new file +69 -0
69 lines not on this page · raw message
include/linux/user_namespace.h +1 -0
@@ -45,6 +45,7 @@ enum ucount_type {UCOUNT_NET_NAMESPACES ,UCOUNT_MNT_NAMESPACES ,UCOUNT_CGROUP_NAMESPACES , +UCOUNT_TIME_NAMESPACES , #ifdef CONFIG_INOTIFY_USERUCOUNT_INOTIFY_INSTANCES , UCOUNT_INOTIFY_WATCHES,
include/uapi/linux/sched.h +6 -0
@@ -33,6 +33,12 @@#define CLONE_NEWNET 0x40000000/* New network namespace */ #define CLONE_IO 0x80000000/* Clone io context */ +/* +* cloning flags intersect with CSIGNAL so can be used with unshare and clone3 +* syscalls only: +*/ +#define CLONE_NEWTIME 0x00000080/* New time namespace */ +/* * Arguments for the clone3 syscall */
init/Kconfig +7 -0
@@ -1069,6 +1069,13 @@ config UTS_NS In this namespace tasks see different info provided with the uname() system call +config TIME_NS + bool "TIME namespace" + default y + help + In this namespace boottime and monotonic clocks can be set. + The time will keep going with the same pace. + config IPC_NS bool "IPC namespace" depends on (SYSVIPC || POSIX_MQUEUE)
kernel/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/ obj-$(CONFIG_UTS_NS) += utsname.o +obj-$(CONFIG_TIME_NS) += time_namespace.o obj-$(CONFIG_USER_NS) += user_namespace.o obj-$(CONFIG_PID_NS) += pid_namespace.o obj-$(CONFIG_IKCONFIG) += configs.o
kernel/fork.c +13 -3
@@ -1777,6 +1777,7 @@ static __latent_entropy struct task_struct *copy_process(struct multiprocess_signals delayed;struct file *pidfile =NULL ;u64 clone_flags = args->flags; +struct nsproxy *nsp = current->nsproxy;/* * Don't allow sharing the root directory with processes in a different
@@ -1819,8 +1820,16 @@ static __latent_entropy struct task_struct *copy_process(*/ if (clone_flags &CLONE_THREAD ) {if ((clone_flags & (CLONE_NEWUSER |CLONE_NEWPID )) || - (task_active_pid_ns (current) != - current->nsproxy->pid_ns_for_children)) + (task_active_pid_ns (current) != nsp->pid_ns_for_children)) +return ERR_PTR (-EINVAL ); + } + +/* +* If the new process will be in a different time namespace +* do not allow it to share VM or a thread group with the forking task. +*/ +if (clone_flags & (CLONE_THREAD |CLONE_VM )) { +if (nsp->time_ns != nsp->time_ns_for_children)return ERR_PTR (-EINVAL ); }
@@ -2707,7 +2716,8 @@ static int check_unshare_flags(unsigned long unshare_flags)if (unshare_flags & ~(CLONE_THREAD |CLONE_FS |CLONE_NEWNS |CLONE_SIGHAND |CLONE_VM |CLONE_FILES |CLONE_SYSVSEM |CLONE_NEWUTS |CLONE_NEWIPC |CLONE_NEWNET | -CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP )) +CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP | +CLONE_NEWTIME ))return -EINVAL ;/* * Not implemented, but pretend it works if there is nothing
kernel/nsproxy.c +34 -7
88 lines not on this page · raw message
kernel/time_namespace.c new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v6 25/37 x86/vdso: Switch image on setns()/clone()
In reply to Dmitry SafonovAs it has been discussed on timens RFC, adding a new conditional branch if (inside_time_ns) on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp.
Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation vdso code.
Addressing those problems, there are two versions of VDSO's .so:
for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time.
Whenever a user does setns() or unshare(CLONE_TIMENS) followed by clone(), change VDSO image in mm and zap VVAR/VDSO page tables. They will be re-faulted with corresponding image and VVAR offsets.
---
v5..v6 Change:
Rebased over current_is_single_threaded() change in the first patch.
arch/x86/entry/vdso/vma.c | 23 +++++++++++++++++++++++ arch/x86/include/asm/vdso.h | 1 + kernel/time_namespace.c | 11 +++++++++++ 3 files changed, 35 insertions(+)
arch/x86/entry/vdso/vma.c +23 -0
@@ -25,6 +25,7 @@#include <asm/cpufeature.h> #include <clocksource/hyperv_timer.h> #include <asm/page.h> +#include <asm/tlb.h> #if defined(CONFIG_X86_64 )unsigned int __read_mostly vdso64_enabled =1 ;
@@ -266,6 +267,28 @@ static const struct vm_special_mapping vvar_mapping = { .mremap = vvar_mremap, }; +#ifdef CONFIG_TIME_NS +int vdso_join_timens (struct task_struct *task) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (down_write_killable (&mm->mmap_sem)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, &vvar_mapping) || +vma_is_special_mapping (vma, &vdso_mapping)) +zap_page_range (vma, vma->vm_start, size); + } + +up_write (&mm->mmap_sem); +return 0 ; +} +#endif +/* * Add vdso and vvar mappings to current process. * @image - blob to map
arch/x86/include/asm/vdso.h +1 -0
@@ -45,6 +45,7 @@ extern struct vdso_image vdso_image_32;extern void __init init_vdso_image (struct vdso_image *image);extern int map_vdso_once (const struct vdso_image *image,unsigned long addr); +extern int vdso_join_timens (struct task_struct *task);#endif /* __ASSEMBLER__ */
kernel/time_namespace.c +11 -0
@@ -15,6 +15,7 @@#include <linux/cred.h> #include <linux/err.h> #include <linux/mm.h> +#include <asm/vdso.h> ktime_t do_timens_ktime_to_host (clockid_t clockid,ktime_t tim,struct timens_offsets *ns_offsets)
@@ -199,6 +200,7 @@ static void timens_put(struct ns_common *ns)static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) {struct time_namespace *ns =to_time_ns (new); +int ret;if (!current_is_single_threaded ())return -EUSERS ;
@@ -207,6 +209,10 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new) !ns_capable(current_user_ns(), CAP_SYS_ADMIN))return -EPERM ; + ret =vdso_join_timens (current); +if (ret) +return ret; +get_time_ns (ns);get_time_ns (ns);put_time_ns (nsproxy->time_ns);
@@ -221,10 +227,15 @@ int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk) {struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;struct time_namespace *ns =to_time_ns (nsc); +int ret;if (nsproxy->time_ns == nsproxy->time_ns_for_children)return 0 ; + ret =vdso_join_timens (tsk); +if (ret) +return ret; +get_time_ns (ns);put_time_ns (nsproxy->time_ns); nsproxy->time_ns = ns;
Signature · 1 line
2.22.0
Re: [PATCHv5 04/37] posix-clocks: Rename *_clock_get() functions into *_clock_get_timespec()
In reply to Dmitry Safonovkernel/time/posix-timers.c static const struct k_clock clock_realtime = { · line 1247
Quoted 4 lines · Dmitry Safonov
static const struct
k_clockclock_monotonic= {kernel/time/posix-timers.c +1
-1.clock_getres = posix_get_hrtimer_res, - .clock_get_timespec = posix_ktime_get_ts, + .clock_get_timespec = posix_get_timespec,
posix_get_monotonic_timespec
Please.
Re: [PATCHv5 06/37] alarmtimer: Provide get_timespec() callback
In reply to Dmitry Safonovkernel/time/alarmtimer.c static int __init alarmtimer_init(void) · line 874
Quoted 6 lines · Dmitry Safonov
/**
kernel/time/alarmtimer.c +1
-0@@ -869,8 +871,10 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; + alarm_bases[ALARM_REALTIME].get_timespec = posix_get_timespec,
That's just wrong:
kernel/time/posix-timers.c static int posix_clock_realtime_adj(const clockid_t which_clock, · line 194
Quoted 8 lines
kernel/time/posix-timers.c +1
-1/* * Get monotonic time for posix timers */ -static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;
Using a proper function name would have avoided this.
Re: [PATCHv5 09/37] posix-clocks: Introduce CLOCK_MONOTONIC time namespace offsets
In reply to Dmitry Safonovinclude/linux/time_namespace.h static inline void put_time_ns(struct time_namespace *ns) · line 49
Quoted 8 lines · Dmitry Safonov
include/linux/time_namespace.h +7
-0+static inline void timens_add_monotonic (struct timespec64 *ts) +{ +struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + +if (ns_offsets) + *ts =timespec64_add (*ts, ns_offsets->monotonic); +}
This helper is not posix timer specific and should be introduced either in the name space patches or in a separate patch,
Thanks
tglx
Re: [PATCHv5 06/37] alarmtimer: Provide get_timespec() callback
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
kernel/time/alarmtimer.c static int __init alarmtimer_init(void) · line 874
Quoted 6 lines · Dmitry Safonov
/**
kernel/time/alarmtimer.c +1
-0@@ -869,8 +871,10 @@ static int __init alarmtimer_init(void)/* Initialize alarm bases */ alarm_bases[ALARM_REALTIME ].base_clockid =CLOCK_REALTIME ; alarm_bases[ALARM_REALTIME ].get_ktime = &ktime_get_real; + alarm_bases[ALARM_REALTIME].get_timespec = posix_get_timespec,That's just wrong:
Quoted 10 lines
kernel/time/posix-timers.c static int posix_clock_realtime_adj(const clockid_t which_clock, · line 194
Quoted 8 lines
kernel/time/posix-timers.c +1
-1/* * Get monotonic time for posix timers */ -static int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) +int posix_get_timespec (clockid_t which_clock,struct timespec64 *tp) {ktime_get_ts64 (tp);return 0 ;Using a proper function name would have avoided this.
You are right. Will fix. Thanks!