Enabling htimer support for CLOCK_MONOTONIC_RAW
cover Enabling htimer support for CLOCK_MONOTONIC_RAW
In some (admitedly rare) cases, a kernel subsystem wants to obtain a timer that is not affected by NTP whatsoever. An example of this is the KVM/ARM timer, which uses a hrtimer to back a vcpu timer when the vcpu is not running. If we let this backup timer being corrected by NTP, the guest ends up being woken up at the wrong time, and confusion follows.
This series adds a new hrtimer base (HRTIMER_BASE_MONOTONIC_RAW), which gets used when CLOCK_MONOTONIC_RAW is passed to hrtimer_init, making its behaviour very similar to that of the posix timers.
It also adds a new check for illegal values passed to hrtimer_init (it seems way too easy to confuse the hrtimer code by giving it a posix timer clockid). The last patch simply switches the KVM/ARM timer to CLOCK_MONOTONIC_RAW.
Please note that I only have a limited understanding of how the hrtimer subsystem actually works, so I'd appreciate any comment on how this could otherwise be done.
It has been (very lightly) tested on arm64.
Marc Zyngier (3):
hrtimer: Add support for CLOCK_MONOTONIC_RAW
hrtimer: Catch illegal clockids
KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
include/linux/hrtimer.h | 1 + kernel/time/hrtimer.c | 18 ++++++++++++++++-- virt/kvm/arm/arch_timer.c | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/3 hrtimer: Add support for CLOCK_MONOTONIC_RAW
The KVM/ARM timer implementation arms a hrtimer when a vcpu is blocked (usually because it is waiting for an interrupt) while its timer is going to kick in the future.
It is essential that this timer doesn't get adjusted, or the guest will end up being woken-up at the wrong time (NTP running on the host seems to confuse the hell out of some guests).
In order to allow this, let's add CLOCK_MONOTONIC_RAW support to hrtimer (it is so far only supported for posix timers). It also has the (limited) benefit of fixing de0421d53bfb ("mac80211_hwsim:
shuffle code to prepare for dynamic radios"), which already uses this functionnality without realizing wasn't implemented (just being lucky...).
--- include/linux/hrtimer.h | 1 + kernel/time/hrtimer.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-)
include/linux/hrtimer.h +1 -0
@@ -151,6 +151,7 @@ enum hrtimer_base_type {HRTIMER_BASE_REALTIME ,HRTIMER_BASE_BOOTTIME ,HRTIMER_BASE_TAI , +HRTIMER_BASE_MONOTONIC_RAW , HRTIMER_MAX_CLOCK_BASES, };
kernel/time/hrtimer.c +10 -1
@@ -90,12 +90,18 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = .clockid = CLOCK_TAI, .get_time = &ktime_get_clocktai, }, + { + .index = HRTIMER_BASE_MONOTONIC_RAW, + .clockid = CLOCK_MONOTONIC_RAW, + .get_time = &ktime_get_raw, + }, } };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , + [CLOCK_MONOTONIC_RAW ] =HRTIMER_BASE_MONOTONIC_RAW , [CLOCK_BOOTTIME ] =HRTIMER_BASE_BOOTTIME , [CLOCK_TAI ] =HRTIMER_BASE_TAI , };
@@ -1268,7 +1274,10 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)if (!(active &0x01 ))continue ; - basenow =ktime_add (now, base->offset); +if (unlikely (base->index ==HRTIMER_BASE_MONOTONIC_RAW )) + basenow =ktime_get_raw (); +else + basenow =ktime_add (now, base->offset);while ((node =timerqueue_getnext (&base->active))) {struct hrtimer *timer;
Signature · 1 line
2.1.4
tip-bot for Marc Zyngier
merge bot[tip:timers/core] hrtimer: Add support for CLOCK_MONOTONIC_RAW
hrtimer: Add support for CLOCK_MONOTONIC_RAW
Commit-ID: 9c808765e88efb6fa6af7e2206ef89512f1840a7 Gitweb: http://git.kernel.org/tip/9c808765e88efb6fa6af7e2206ef89512f1840a7 Author: Marc Zyngier <[email protected]> AuthorDate: Fri, 15 Jan 2016 17:41:08 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 27 Jan 2016 12:38:04 +0100
hrtimer: Add support for CLOCK_MONOTONIC_RAW
The KVM/ARM timer implementation arms a hrtimer when a vcpu is blocked (usually because it is waiting for an interrupt) while its timer is going to kick in the future.
It is essential that this timer doesn't get adjusted, or the guest will end up being woken-up at the wrong time (NTP running on the host seems to confuse the hell out of some guests).
In order to allow this, let's add CLOCK_MONOTONIC_RAW support to hrtimer (it is so far only supported for posix timers). It also has the (limited) benefit of fixing de0421d53bfb ("mac80211_hwsim:
shuffle code to prepare for dynamic radios"), which already uses this functionnality without realizing wasn't implemented (just being lucky...).
--- include/linux/hrtimer.h | 1 + kernel/time/hrtimer.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-)
include/linux/hrtimer.h +1 -0
@@ -151,6 +151,7 @@ enum hrtimer_base_type {HRTIMER_BASE_REALTIME ,HRTIMER_BASE_BOOTTIME ,HRTIMER_BASE_TAI , +HRTIMER_BASE_MONOTONIC_RAW , HRTIMER_MAX_CLOCK_BASES, };
kernel/time/hrtimer.c +10 -1
@@ -90,12 +90,18 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = .clockid = CLOCK_TAI, .get_time = &ktime_get_clocktai, }, + { + .index = HRTIMER_BASE_MONOTONIC_RAW, + .clockid = CLOCK_MONOTONIC_RAW, + .get_time = &ktime_get_raw, + }, } };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , + [CLOCK_MONOTONIC_RAW ] =HRTIMER_BASE_MONOTONIC_RAW , [CLOCK_BOOTTIME ] =HRTIMER_BASE_BOOTTIME , [CLOCK_TAI ] =HRTIMER_BASE_TAI , };
@@ -1268,7 +1274,10 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)if (!(active &0x01 ))continue ; - basenow =ktime_add (now, base->offset); +if (unlikely (base->index ==HRTIMER_BASE_MONOTONIC_RAW )) + basenow =ktime_get_raw (); +else + basenow =ktime_add (now, base->offset);while ((node =timerqueue_getnext (&base->active))) {struct hrtimer *timer;
Re: [tip:timers/core] hrtimer: Add support for CLOCK_MONOTONIC_RAW
Thomas,
Quoted 24 lines · tip-bot for Marc Zyngier
Commit-ID: 9c808765e88efb6fa6af7e2206ef89512f1840a7 Gitweb: http://git.kernel.org/tip/9c808765e88efb6fa6af7e2206ef89512f1840a7 Author: Marc Zyngier <[email protected]> AuthorDate: Fri, 15 Jan 2016 17:41:08 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 27 Jan 2016 12:38:04 +0100hrtimer: Add support for
CLOCK_MONOTONIC_RAWThe KVM/ARM timer implementation arms a hrtimer when a vcpu is blocked (usually because it is waiting for an interrupt) while its timer is going to kick in the future.
It is essential that this timer doesn't get adjusted, or the guest will end up being woken-up at the wrong time (NTP running on the host seems to confuse the hell out of some guests).
In order to allow this, let's add
CLOCK_MONOTONIC_RAWsupport to hrtimer (it is so far only supported for posix timers). It also has the (limited) benefit of fixingde0421d53bfb("mac80211_hwsim:
shuffle code to prepare for dynamic radios"), which already uses this functionnality without realizing wasn't implemented (just being lucky...).
Quoted 5 lines
Signed-off-by Marc Zyngier <[email protected]>Cc Tomasz Nowicki <[email protected]>Cc Christoffer Dall <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>
I'm afraid this patch is terminally broken - I managed to make this fall over pretty easily (hint to self: __hrtimer_get_next_event needs some serious thoughts to deal with MONOTONIC_RAW, and so does hrtimer_forward at the very least).
Can you please drop (or revert):
a6e707d KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW 9006a01 hrtimer: Catch illegal clockids
9c80876 hrtimer: Add support for CLOCK_MONOTONIC_RAW
Thanks, and sorry for the noise.
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
tip-bot for Thomas Gleixner
merge bot[tip:timers/core] hrtimer: Revert CLOCK_MONOTONIC_RAW support
hrtimer: Revert CLOCK_MONOTONIC_RAW support
Commit-ID: 82e88ff1ea948d83125a8aaa7c9809f03ccc500f Gitweb: http://git.kernel.org/tip/82e88ff1ea948d83125a8aaa7c9809f03ccc500f Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 3 Mar 2016 11:11:12 +0100 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 3 Mar 2016 11:11:12 +0100
hrtimer: Revert CLOCK_MONOTONIC_RAW support
Revert commits:a6e707ddbdf1: KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW 9006a01829a5: hrtimer: Catch illegal clockids 9c808765e88e: hrtimer: Add support for CLOCK_MONOTONIC_RAW
Marc found out, that there are fundamental issues with that patch series because __hrtimer_get_next_event() and hrtimer_forward() need support for CLOCK_MONOTONIC_RAW. Nothing which is easily fixed, so revert the whole lot.
--- include/linux/hrtimer.h | 1 - kernel/time/hrtimer.c | 18 ++---------------- virt/kvm/arm/arch_timer.c | 4 ++-- 3 files changed, 4 insertions(+), 19 deletions(-)
include/linux/hrtimer.h +0 -1
@@ -151,7 +151,6 @@ enum hrtimer_base_type {HRTIMER_BASE_REALTIME ,HRTIMER_BASE_BOOTTIME ,HRTIMER_BASE_TAI , -HRTIMER_BASE_MONOTONIC_RAW , HRTIMER_MAX_CLOCK_BASES, };
kernel/time/hrtimer.c +2 -16
@@ -90,30 +90,19 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = .clockid = CLOCK_TAI, .get_time = &ktime_get_clocktai, }, - { - .index = HRTIMER_BASE_MONOTONIC_RAW, - .clockid = CLOCK_MONOTONIC_RAW, - .get_time = &ktime_get_raw, - }, } };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { -/* Make sure we catch unsupported clockids */ - [0 ...MAX_CLOCKS -1 ] =HRTIMER_MAX_CLOCK_BASES , - [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , - [CLOCK_MONOTONIC_RAW ] =HRTIMER_BASE_MONOTONIC_RAW , [CLOCK_BOOTTIME ] =HRTIMER_BASE_BOOTTIME , [CLOCK_TAI ] =HRTIMER_BASE_TAI , };static inline int hrtimer_clockid_to_base (clockid_t clock_id) { -int base = hrtimer_clock_to_base_table[clock_id]; -BUG_ON (base ==HRTIMER_MAX_CLOCK_BASES ); -return base; +return hrtimer_clock_to_base_table[clock_id]; }/*
@@ -1279,10 +1268,7 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)if (!(active &0x01 ))continue ; -if (unlikely (base->index ==HRTIMER_BASE_MONOTONIC_RAW )) - basenow =ktime_get_raw (); -else - basenow =ktime_add (now, base->offset); + basenow =ktime_add (now, base->offset);while ((node =timerqueue_getnext (&base->active))) {struct hrtimer *timer;
virt/kvm/arm/arch_timer.c +2 -2
@@ -48,7 +48,7 @@ static bool timer_is_armed(struct arch_timer_cpu *timer)static void timer_arm (struct arch_timer_cpu *timer,u64 ns) { timer->armed = true; -hrtimer_start (&timer->timer,ktime_add_ns (ktime_get_raw (), ns), +hrtimer_start (&timer->timer,ktime_add_ns (ktime_get (), ns),HRTIMER_MODE_ABS ); }
@@ -308,7 +308,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;INIT_WORK (&timer->expired, kvm_timer_inject_irq_work); -hrtimer_init (&timer->timer,CLOCK_MONOTONIC_RAW ,HRTIMER_MODE_ABS ); +hrtimer_init (&timer->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); timer->timer.function = kvm_timer_expire; }
PATCH 2/3 hrtimer: Catch illegal clockids
In reply to Marc ZyngierIt is way too easy to take any random clockid and feed it to the hrtimer subsystem. At best, it gets mapped to a monotonic base, but it would be better to just catch illegal values as early as possible.
This patch does exactly that, mapping illegal clockids to an illegal base index, and panicing when we detect the illegal condition.
--- kernel/time/hrtimer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/time/hrtimer.c +6 -1
@@ -99,6 +99,9 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { +/* Make sure we catch unsupported clockids */ + [0 ...MAX_CLOCKS -1 ] =HRTIMER_MAX_CLOCK_BASES , + [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , [CLOCK_MONOTONIC_RAW ] =HRTIMER_BASE_MONOTONIC_RAW ,
@@ -108,7 +111,9 @@ static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {static inline int hrtimer_clockid_to_base (clockid_t clock_id) { -return hrtimer_clock_to_base_table[clock_id]; +int base = hrtimer_clock_to_base_table[clock_id]; +BUG_ON (base ==HRTIMER_MAX_CLOCK_BASES ); +return base; }/*
Signature · 1 line
2.1.4
tip-bot for Marc Zyngier
merge bot[tip:timers/core] hrtimer: Catch illegal clockids
hrtimer: Catch illegal clockids
Commit-ID: 9006a01829a50cfd6bbd4980910ed46e895e93d7 Gitweb: http://git.kernel.org/tip/9006a01829a50cfd6bbd4980910ed46e895e93d7 Author: Marc Zyngier <[email protected]> AuthorDate: Fri, 15 Jan 2016 17:41:09 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 27 Jan 2016 12:38:04 +0100
hrtimer: Catch illegal clockids
It is way too easy to take any random clockid and feed it to the hrtimer subsystem. At best, it gets mapped to a monotonic base, but it would be better to just catch illegal values as early as possible.
This patch does exactly that, mapping illegal clockids to an illegal base index, and panicing when we detect the illegal condition.
--- kernel/time/hrtimer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/time/hrtimer.c +6 -1
@@ -99,6 +99,9 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { +/* Make sure we catch unsupported clockids */ + [0 ...MAX_CLOCKS -1 ] =HRTIMER_MAX_CLOCK_BASES , + [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , [CLOCK_MONOTONIC_RAW ] =HRTIMER_BASE_MONOTONIC_RAW ,
@@ -108,7 +111,9 @@ static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {static inline int hrtimer_clockid_to_base (clockid_t clock_id) { -return hrtimer_clock_to_base_table[clock_id]; +int base = hrtimer_clock_to_base_table[clock_id]; +BUG_ON (base ==HRTIMER_MAX_CLOCK_BASES ); +return base; }/*
tip-bot for Marc Zyngier
merge bot[tip:timers/core] hrtimer: Catch invalid clockids again
In reply to Marc Zyngierhrtimer: Catch invalid clockids again
Commit-ID: 336a9cde10d641e70bac67d90ae91b3190c3edca Gitweb: http://git.kernel.org/tip/336a9cde10d641e70bac67d90ae91b3190c3edca Author: Marc Zyngier <[email protected]> AuthorDate: Fri, 15 Jan 2016 17:41:09 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Sat, 18 Feb 2017 10:58:39 +0100
hrtimer: Catch invalid clockids again
commit 82e88ff1ea94 ("hrtimer: Revert CLOCK_MONOTONIC_RAW support") removed unfortunately a sanity check in the hrtimer code which was part of that MONOTONIC_RAW patch series.
It would have caught the bogus usage of CLOCK_MONOTONIC_RAW in the wireless code. So bring it back.
It is way too easy to take any random clockid and feed it to the hrtimer subsystem. At best, it gets mapped to a monotonic base, but it would be better to just catch illegal values as early as possible.
Detect invalid clockids, map them to CLOCK_MONOTONIC and emit a warning.
[ tglx: Replaced the BUG by a WARN and gracefully map to CLOCK_MONOTONIC ]
--- kernel/time/hrtimer.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
kernel/time/hrtimer.c +15 -5
@@ -94,17 +94,15 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = };static const int hrtimer_clock_to_base_table[MAX_CLOCKS ] = { +/* Make sure we catch unsupported clockids */ + [0 ...MAX_CLOCKS -1 ] =HRTIMER_MAX_CLOCK_BASES , + [CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME , [CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC , [CLOCK_BOOTTIME ] =HRTIMER_BASE_BOOTTIME , [CLOCK_TAI ] =HRTIMER_BASE_TAI , }; -static inline int hrtimer_clockid_to_base (clockid_t clock_id) -{ -return hrtimer_clock_to_base_table[clock_id]; -} -/* * Functions and macros which are different for UP/SMP systems are kept in a * single place
@@ -1081,6 +1079,18 @@ u64 hrtimer_get_next_event(void) }#endif +static inline int hrtimer_clockid_to_base (clockid_t clock_id) +{ +if (likely (clock_id <MAX_CLOCKS )) { +int base = hrtimer_clock_to_base_table[clock_id]; + +if (likely (base !=HRTIMER_MAX_CLOCK_BASES )) +return base; + } +WARN (1 ,"Invalid clockid %d. Using MONOTONIC\n" , clock_id); +return HRTIMER_BASE_MONOTONIC ; +} +static void __hrtimer_init (struct hrtimer *timer,clockid_t clock_id,enum hrtimer_mode mode) {
PATCH 3/3 KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
In reply to Marc ZyngierIn order to avoid NTP messing with the guest timer behind our back, use the new and improved monotonic raw version of the hrtimers.
--- virt/kvm/arm/arch_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/arm/arch_timer.c +2 -2
@@ -48,7 +48,7 @@ static bool timer_is_armed(struct arch_timer_cpu *timer)static void timer_arm (struct arch_timer_cpu *timer,u64 ns) { timer->armed = true; -hrtimer_start (&timer->timer,ktime_add_ns (ktime_get (), ns), +hrtimer_start (&timer->timer,ktime_add_ns (ktime_get_raw (), ns),HRTIMER_MODE_ABS ); }
@@ -308,7 +308,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;INIT_WORK (&timer->expired, kvm_timer_inject_irq_work); -hrtimer_init (&timer->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); +hrtimer_init (&timer->timer,CLOCK_MONOTONIC_RAW ,HRTIMER_MODE_ABS ); timer->timer.function = kvm_timer_expire; }
Signature · 1 line
2.1.4
tip-bot for Marc Zyngier
merge bot[tip:timers/core] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
Commit-ID: a6e707ddbdf150bd1c2a5c0eccc55abdc62a0039 Gitweb: http://git.kernel.org/tip/a6e707ddbdf150bd1c2a5c0eccc55abdc62a0039 Author: Marc Zyngier <[email protected]> AuthorDate: Fri, 15 Jan 2016 17:41:10 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Wed, 27 Jan 2016 12:38:04 +0100
KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
In order to avoid NTP messing with the guest timer behind our back, use the new and improved monotonic raw version of the hrtimers.
--- virt/kvm/arm/arch_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/arm/arch_timer.c +2 -2
@@ -48,7 +48,7 @@ static bool timer_is_armed(struct arch_timer_cpu *timer)static void timer_arm (struct arch_timer_cpu *timer,u64 ns) { timer->armed = true; -hrtimer_start (&timer->timer,ktime_add_ns (ktime_get (), ns), +hrtimer_start (&timer->timer,ktime_add_ns (ktime_get_raw (), ns),HRTIMER_MODE_ABS ); }
@@ -308,7 +308,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;INIT_WORK (&timer->expired, kvm_timer_inject_irq_work); -hrtimer_init (&timer->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); +hrtimer_init (&timer->timer,CLOCK_MONOTONIC_RAW ,HRTIMER_MODE_ABS ); timer->timer.function = kvm_timer_expire; }