Skip to content

Commit 5a590f3

Browse files
avaginKAGA-KOKO
authored andcommitted
posix-clocks: Wire up clock_gettime() with timens offsets
Adjust monotonic and boottime clocks with per-timens offsets. As the result a process inside time namespace will see timers and clocks corrected to offsets that were set when the namespace was created Note that applications usually go through vDSO to get time, which is not yet adjusted. Further changes will complete time namespace virtualisation with vDSO support. Co-developed-by: Dmitry Safonov <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 198fa44 commit 5a590f3

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

kernel/time/alarmtimer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/freezer.h>
2727
#include <linux/compat.h>
2828
#include <linux/module.h>
29+
#include <linux/time_namespace.h>
2930

3031
#include "posix-timers.h"
3132

@@ -886,6 +887,12 @@ static struct platform_driver alarmtimer_driver = {
886887
}
887888
};
888889

890+
static void get_boottime_timespec(struct timespec64 *tp)
891+
{
892+
ktime_get_boottime_ts64(tp);
893+
timens_add_boottime(tp);
894+
}
895+
889896
/**
890897
* alarmtimer_init - Initialize alarm timer code
891898
*
@@ -906,7 +913,7 @@ static int __init alarmtimer_init(void)
906913
alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64,
907914
alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
908915
alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
909-
alarm_bases[ALARM_BOOTTIME].get_timespec = ktime_get_boottime_ts64;
916+
alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
910917
for (i = 0; i < ALARM_NUMTYPE; i++) {
911918
timerqueue_init_head(&alarm_bases[i].timerqueue);
912919
spin_lock_init(&alarm_bases[i].lock);

kernel/time/posix-stubs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/ktime.h>
1515
#include <linux/timekeeping.h>
1616
#include <linux/posix-timers.h>
17+
#include <linux/time_namespace.h>
1718
#include <linux/compat.h>
1819

1920
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
@@ -77,9 +78,11 @@ int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
7778
break;
7879
case CLOCK_MONOTONIC:
7980
ktime_get_ts64(tp);
81+
timens_add_monotonic(tp);
8082
break;
8183
case CLOCK_BOOTTIME:
8284
ktime_get_boottime_ts64(tp);
85+
timens_add_boottime(tp);
8386
break;
8487
default:
8588
return -EINVAL;

kernel/time/posix-timers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/hashtable.h>
3131
#include <linux/compat.h>
3232
#include <linux/nospec.h>
33+
#include <linux/time_namespace.h>
3334

3435
#include "timekeeping.h"
3536
#include "posix-timers.h"
@@ -195,6 +196,7 @@ static int posix_clock_realtime_adj(const clockid_t which_clock,
195196
static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
196197
{
197198
ktime_get_ts64(tp);
199+
timens_add_monotonic(tp);
198200
return 0;
199201
}
200202

@@ -209,6 +211,7 @@ static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
209211
static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
210212
{
211213
ktime_get_raw_ts64(tp);
214+
timens_add_monotonic(tp);
212215
return 0;
213216
}
214217

@@ -223,6 +226,7 @@ static int posix_get_monotonic_coarse(clockid_t which_clock,
223226
struct timespec64 *tp)
224227
{
225228
ktime_get_coarse_ts64(tp);
229+
timens_add_monotonic(tp);
226230
return 0;
227231
}
228232

@@ -235,6 +239,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *
235239
static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
236240
{
237241
ktime_get_boottime_ts64(tp);
242+
timens_add_boottime(tp);
238243
return 0;
239244
}
240245

0 commit comments

Comments
 (0)