All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv3 00/27] kernel: Introduce Time Namespace
@ 2019-04-25 16:13 Dmitry Safonov
  2019-04-25 16:13 ` [PATCHv3 01/27] ns: " Dmitry Safonov
                   ` (26 more replies)
  0 siblings, 27 replies; 49+ messages in thread
From: Dmitry Safonov @ 2019-04-25 16:13 UTC (permalink / raw)
  To: linux-kernel
  Cc: Dmitry Safonov, Adrian Reber, Andrei Vagin, Andy Lutomirski,
	Arnd Bergmann, Christian Brauner, Cyrill Gorcunov,
	Dmitry Safonov, Eric W. Biederman, H. Peter Anvin, Ingo Molnar,
	Jeff Dike, Oleg Nesterov, Pavel Emelyanov, Shuah Khan,
	Thomas Gleixner, Vincenzo Frascino, containers, criu, linux-api,
	x86, Andrei Vagin

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.

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.
Link: https://lkml.org/lkml/2018/9/19/950
Link: https://lkml.org/lkml/2019/2/5/867

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.

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
--------|------------|------------------|-------------|-------------
cycles  | 139887013  | 139453003        | 139899785   | 128792458
diff (%)| 100        | 99.7             | 100         | 92

The second one gettime_perf_cold.c calls rdtsc, clock_gettime(), rdtsc
and shows a difference of second and first rdtsc. We call this binary in
a loop 1000 times, get 1000 values and calculate MODE for them.
It should show us performance with a cold CPU cache
(lesser tsc per cycle - the better):

        | before     | CONFIG_TIME_NS=n | host        | inside timens
--------|------------|------------------|-------------|-------------
tsc     | 6748       | 6718             | 6862        | 12682
diff (%)| 100        | 99.6             | 101.7       | 188

The numbers gathered on Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz.

Cc: Adrian Reber <adrian@lisas.de>
Cc: Andrei Vagin <avagin@openvz.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jeff Dike <jdike@addtoit.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Pavel Emelyanov <xemul@virtuozzo.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: containers@lists.linux-foundation.org
Cc: criu@openvz.org
Cc: linux-api@vger.kernel.org
Cc: x86@kernel.org

v2: https://lore.kernel.org/lkml/20190206001107.16488-1-dima@arista.com/
RFC: https://lkml.kernel.org/r/20180919205037.9574-1-dima@arista.com/

Andrei Vagin (16):
  ns: Introduce Time Namespace
  timens: Add timens_offsets
  timens: Introduce CLOCK_MONOTONIC offsets
  timens: Introduce CLOCK_BOOTTIME offset
  timerfd/timens: Take into account ns clock offsets
  posix-timers/timens: Take into account clock offsets
  timens/kernel: Take into account timens clock offsets in
    clock_nanosleep
  x86/vdso: Add offsets page in vvar
  vdso: introduce timens_static_branch
  timens/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
  x86/vdso: Align VDSO functions by CPU L1 cache line
  selftests: Add a simple perf test for clock_gettime()
  selftest/timens: Check that a right vdso is mapped after fork and exec

Dmitry Safonov (11):
  timens: Shift /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()/unshare()/clone()
  timens: 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                  |  16 +-
 arch/x86/entry/vdso/vclock_gettime.c          |  48 +++
 arch/x86/entry/vdso/vdso-layout.lds.S         |  10 +-
 arch/x86/entry/vdso/vdso2c.c                  |   7 +-
 arch/x86/entry/vdso/vdso2c.h                  |  24 +-
 arch/x86/entry/vdso/vma.c                     | 113 ++++++-
 arch/x86/include/asm/jump_label.h             |  14 +
 arch/x86/include/asm/vdso.h                   |  14 +-
 fs/proc/base.c                                | 101 ++++++
 fs/proc/namespaces.c                          |   4 +
 fs/proc/uptime.c                              |   3 +
 fs/timerfd.c                                  |   8 +-
 include/linux/hrtimer.h                       |   1 +
 include/linux/jump_label.h                    |   5 +
 include/linux/nsproxy.h                       |   2 +
 include/linux/proc_ns.h                       |   2 +
 include/linux/time_namespace.h                | 137 ++++++++
 include/linux/timens_offsets.h                |  18 +
 include/linux/user_namespace.h                |   1 +
 include/uapi/linux/sched.h                    |   1 +
 include/uapi/linux/time.h                     |   2 +
 init/Kconfig                                  |   8 +
 kernel/Makefile                               |   1 +
 kernel/fork.c                                 |   3 +-
 kernel/nsproxy.c                              |  41 ++-
 kernel/time/alarmtimer.c                      |  13 +-
 kernel/time/hrtimer.c                         |   3 +
 kernel/time/posix-stubs.c                     |  11 +-
 kernel/time/posix-timers.c                    |  27 +-
 kernel/time_namespace.c                       | 318 ++++++++++++++++++
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/timens/.gitignore     |   8 +
 tools/testing/selftests/timens/Makefile       |  12 +
 .../selftests/timens/clock_nanosleep.c        | 100 ++++++
 tools/testing/selftests/timens/config         |   1 +
 tools/testing/selftests/timens/exec.c         |  91 +++++
 tools/testing/selftests/timens/gettime_perf.c |  74 ++++
 .../selftests/timens/gettime_perf_cold.c      |  63 ++++
 tools/testing/selftests/timens/log.h          |  26 ++
 tools/testing/selftests/timens/procfs.c       | 142 ++++++++
 tools/testing/selftests/timens/timens.c       | 188 +++++++++++
 tools/testing/selftests/timens/timens.h       |  63 ++++
 tools/testing/selftests/timens/timer.c        | 116 +++++++
 tools/testing/selftests/timens/timerfd.c      | 127 +++++++
 47 files changed, 1928 insertions(+), 49 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

-- 
2.21.0


^ permalink raw reply	[flat|nested] 49+ messages in thread

end of thread, other threads:[~2019-05-03  7:00 UTC | newest]

Thread overview: 49+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-25 16:13 [PATCHv3 00/27] kernel: Introduce Time Namespace Dmitry Safonov
2019-04-25 16:13 ` [PATCHv3 01/27] ns: " Dmitry Safonov
2019-04-25 19:10   ` Jann Horn
2019-04-25 19:10     ` Jann Horn
2019-04-25 16:13 ` [PATCHv3 02/27] timens: Add timens_offsets Dmitry Safonov
2019-04-25 16:13 ` [PATCHv3 03/27] timens: Introduce CLOCK_MONOTONIC offsets Dmitry Safonov
2019-04-25 19:52   ` Thomas Gleixner
2019-04-25 16:13 ` [PATCHv3 04/27] timens: Introduce CLOCK_BOOTTIME offset Dmitry Safonov
2019-04-25 20:08   ` Cyrill Gorcunov
2019-04-25 16:13 ` [PATCHv3 05/27] timerfd/timens: Take into account ns clock offsets Dmitry Safonov
2019-04-25 21:28   ` Thomas Gleixner
2019-05-03  7:00     ` Andrei Vagin
2019-04-25 16:13 ` [PATCHv3 06/27] posix-timers/timens: Take into account " Dmitry Safonov
2019-04-25 21:45   ` Thomas Gleixner
2019-04-25 16:13 ` [PATCHv3 07/27] timens/kernel: Take into account timens clock offsets in clock_nanosleep Dmitry Safonov
2019-04-25 16:13 ` [PATCHv3 08/27] timens: Shift /proc/uptime Dmitry Safonov
2019-04-25 16:13 ` [PATCHv3 09/27] x86/vdso2c: Correct err messages on file opening Dmitry Safonov
2019-04-25 16:13 ` [PATCHv3 10/27] x86/vdso2c: Convert iterator to unsigned Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 11/27] x86/vdso/Makefile: Add vobjs32 Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 12/27] x86/vdso: Restrict splitting VVAR VMA Dmitry Safonov
2019-04-25 18:41   ` Jann Horn
2019-04-25 18:41     ` Jann Horn
2019-04-25 18:46     ` Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 13/27] x86/vdso: Rename vdso_image {.data=>.text} Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 14/27] x86/vdso: Add offsets page in vvar Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 15/27] x86/vdso: Allocate timens vdso Dmitry Safonov
2019-04-25 18:32   ` Jann Horn
2019-04-25 18:32     ` Jann Horn
2019-04-25 19:05     ` Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 16/27] x86/vdso: Switch image on setns()/unshare()/clone() Dmitry Safonov
2019-04-25 17:53   ` Jann Horn
2019-04-25 17:53     ` Jann Horn
2019-04-25 18:02     ` Dmitry Safonov
2019-04-25 18:02       ` Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 17/27] vdso: introduce timens_static_branch Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 18/27] timens: Add align for timens_offsets Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 19/27] timens/fs/proc: Introduce /proc/pid/timens_offsets Dmitry Safonov
2019-04-25 18:16   ` Jann Horn
2019-04-25 18:16     ` Jann Horn
2019-05-02  6:08     ` Andrei Vagin
2019-05-02  6:08       ` Andrei Vagin
2019-04-25 16:14 ` [PATCHv3 20/27] selftest/timens: Add Time Namespace test for supported clocks Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 21/27] selftest/timens: Add a test for timerfd Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 22/27] selftest/timens: Add a test for clock_nanosleep() Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 23/27] selftest/timens: Add procfs selftest Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 24/27] selftest/timens: Add timer offsets test Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 25/27] x86/vdso: Align VDSO functions by CPU L1 cache line Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 26/27] selftests: Add a simple perf test for clock_gettime() Dmitry Safonov
2019-04-25 16:14 ` [PATCHv3 27/27] selftest/timens: Check that a right vdso is mapped after fork and exec Dmitry Safonov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.