linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Enabling htimer support for CLOCK_MONOTONIC_RAW
@ 2016-01-15 17:41 Marc Zyngier
  2016-01-15 17:41 ` [PATCH 1/3] hrtimer: Add " Marc Zyngier
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Marc Zyngier @ 2016-01-15 17:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel, Tomasz Nowicki, Christoffer Dall

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(-)

-- 
2.1.4

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

* [PATCH 1/3] hrtimer: Add support for CLOCK_MONOTONIC_RAW
  2016-01-15 17:41 [PATCH 0/3] Enabling htimer support for CLOCK_MONOTONIC_RAW Marc Zyngier
@ 2016-01-15 17:41 ` Marc Zyngier
  2016-01-27 11:42   ` [tip:timers/core] " tip-bot for Marc Zyngier
  2016-01-15 17:41 ` [PATCH 2/3] hrtimer: Catch illegal clockids Marc Zyngier
  2016-01-15 17:41 ` [PATCH 3/3] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW Marc Zyngier
  2 siblings, 1 reply; 10+ messages in thread
From: Marc Zyngier @ 2016-01-15 17:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel, Tomasz Nowicki, Christoffer Dall

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...).

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
---
 include/linux/hrtimer.h |  1 +
 kernel/time/hrtimer.c   | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index 76dd4f0..a6d64af 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -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,
 };
 
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 435b885..a125f22 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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;
-- 
2.1.4

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

* [PATCH 2/3] hrtimer: Catch illegal clockids
  2016-01-15 17:41 [PATCH 0/3] Enabling htimer support for CLOCK_MONOTONIC_RAW Marc Zyngier
  2016-01-15 17:41 ` [PATCH 1/3] hrtimer: Add " Marc Zyngier
@ 2016-01-15 17:41 ` Marc Zyngier
  2016-01-27 11:43   ` [tip:timers/core] " tip-bot for Marc Zyngier
  2017-02-18 10:01   ` [tip:timers/core] hrtimer: Catch invalid clockids again tip-bot for Marc Zyngier
  2016-01-15 17:41 ` [PATCH 3/3] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW Marc Zyngier
  2 siblings, 2 replies; 10+ messages in thread
From: Marc Zyngier @ 2016-01-15 17:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel, Tomasz Nowicki, Christoffer Dall

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.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
---
 kernel/time/hrtimer.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index a125f22..cb0fe70 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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;
 }
 
 /*
-- 
2.1.4

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

* [PATCH 3/3] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
  2016-01-15 17:41 [PATCH 0/3] Enabling htimer support for CLOCK_MONOTONIC_RAW Marc Zyngier
  2016-01-15 17:41 ` [PATCH 1/3] hrtimer: Add " Marc Zyngier
  2016-01-15 17:41 ` [PATCH 2/3] hrtimer: Catch illegal clockids Marc Zyngier
@ 2016-01-15 17:41 ` Marc Zyngier
  2016-01-27 11:43   ` [tip:timers/core] " tip-bot for Marc Zyngier
  2 siblings, 1 reply; 10+ messages in thread
From: Marc Zyngier @ 2016-01-15 17:41 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel, Tomasz Nowicki, Christoffer Dall

In order to avoid NTP messing with the guest timer behind our back,
use the new and improved monotonic raw version of the hrtimers.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
---
 virt/kvm/arm/arch_timer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 69bca18..97c5815 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -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;
 }
 
-- 
2.1.4

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

* [tip:timers/core] hrtimer: Add support for CLOCK_MONOTONIC_RAW
  2016-01-15 17:41 ` [PATCH 1/3] hrtimer: Add " Marc Zyngier
@ 2016-01-27 11:42   ` tip-bot for Marc Zyngier
  2016-03-02 11:30     ` Marc Zyngier
  0 siblings, 1 reply; 10+ messages in thread
From: tip-bot for Marc Zyngier @ 2016-01-27 11:42 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, mingo, marc.zyngier, linux-kernel, christoffer.dall, tn, tglx

Commit-ID:  9c808765e88efb6fa6af7e2206ef89512f1840a7
Gitweb:     http://git.kernel.org/tip/9c808765e88efb6fa6af7e2206ef89512f1840a7
Author:     Marc Zyngier <marc.zyngier@arm.com>
AuthorDate: Fri, 15 Jan 2016 17:41:08 +0000
Committer:  Thomas Gleixner <tglx@linutronix.de>
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...).

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Cc: Tomasz Nowicki <tn@semihalf.com>
Cc: Christoffer Dall <christoffer.dall@linaro.org>
Link: http://lkml.kernel.org/r/1452879670-16133-2-git-send-email-marc.zyngier@arm.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/hrtimer.h |  1 +
 kernel/time/hrtimer.c   | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index 76dd4f0..a6d64af 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -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,
 };
 
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 435b885..a125f22 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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;

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

* [tip:timers/core] hrtimer: Catch illegal clockids
  2016-01-15 17:41 ` [PATCH 2/3] hrtimer: Catch illegal clockids Marc Zyngier
@ 2016-01-27 11:43   ` tip-bot for Marc Zyngier
  2017-02-18 10:01   ` [tip:timers/core] hrtimer: Catch invalid clockids again tip-bot for Marc Zyngier
  1 sibling, 0 replies; 10+ messages in thread
From: tip-bot for Marc Zyngier @ 2016-01-27 11:43 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: marc.zyngier, christoffer.dall, hpa, linux-kernel, tn, tglx, mingo

Commit-ID:  9006a01829a50cfd6bbd4980910ed46e895e93d7
Gitweb:     http://git.kernel.org/tip/9006a01829a50cfd6bbd4980910ed46e895e93d7
Author:     Marc Zyngier <marc.zyngier@arm.com>
AuthorDate: Fri, 15 Jan 2016 17:41:09 +0000
Committer:  Thomas Gleixner <tglx@linutronix.de>
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.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Cc: Tomasz Nowicki <tn@semihalf.com>
Cc: Christoffer Dall <christoffer.dall@linaro.org>
Link: http://lkml.kernel.org/r/1452879670-16133-3-git-send-email-marc.zyngier@arm.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/hrtimer.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index a125f22..cb0fe70 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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;
 }
 
 /*

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

* [tip:timers/core] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW
  2016-01-15 17:41 ` [PATCH 3/3] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW Marc Zyngier
@ 2016-01-27 11:43   ` tip-bot for Marc Zyngier
  0 siblings, 0 replies; 10+ messages in thread
From: tip-bot for Marc Zyngier @ 2016-01-27 11:43 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: christoffer.dall, marc.zyngier, linux-kernel, tn, hpa, mingo, tglx

Commit-ID:  a6e707ddbdf150bd1c2a5c0eccc55abdc62a0039
Gitweb:     http://git.kernel.org/tip/a6e707ddbdf150bd1c2a5c0eccc55abdc62a0039
Author:     Marc Zyngier <marc.zyngier@arm.com>
AuthorDate: Fri, 15 Jan 2016 17:41:10 +0000
Committer:  Thomas Gleixner <tglx@linutronix.de>
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.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Cc: Tomasz Nowicki <tn@semihalf.com>
Cc: Christoffer Dall <christoffer.dall@linaro.org>
Link: http://lkml.kernel.org/r/1452879670-16133-4-git-send-email-marc.zyngier@arm.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 virt/kvm/arm/arch_timer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 69bca18..97c5815 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -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;
 }
 

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

* Re: [tip:timers/core] hrtimer: Add support for CLOCK_MONOTONIC_RAW
  2016-01-27 11:42   ` [tip:timers/core] " tip-bot for Marc Zyngier
@ 2016-03-02 11:30     ` Marc Zyngier
  2016-03-03 10:27       ` [tip:timers/core] hrtimer: Revert CLOCK_MONOTONIC_RAW support tip-bot for Thomas Gleixner
  0 siblings, 1 reply; 10+ messages in thread
From: Marc Zyngier @ 2016-03-02 11:30 UTC (permalink / raw)
  To: mingo, linux-kernel, hpa, tglx, tn, christoffer.dall, linux-tip-commits

Thomas,

On 27/01/16 11:42, tip-bot for Marc Zyngier wrote:
> Commit-ID:  9c808765e88efb6fa6af7e2206ef89512f1840a7
> Gitweb:     http://git.kernel.org/tip/9c808765e88efb6fa6af7e2206ef89512f1840a7
> Author:     Marc Zyngier <marc.zyngier@arm.com>
> AuthorDate: Fri, 15 Jan 2016 17:41:08 +0000
> Committer:  Thomas Gleixner <tglx@linutronix.de>
> 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...).
> 
> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
> Cc: Tomasz Nowicki <tn@semihalf.com>
> Cc: Christoffer Dall <christoffer.dall@linaro.org>
> Link: http://lkml.kernel.org/r/1452879670-16133-2-git-send-email-marc.zyngier@arm.com
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

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.
-- 
Jazz is not dead. It just smells funny...

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

* [tip:timers/core] hrtimer: Revert CLOCK_MONOTONIC_RAW support
  2016-03-02 11:30     ` Marc Zyngier
@ 2016-03-03 10:27       ` tip-bot for Thomas Gleixner
  0 siblings, 0 replies; 10+ messages in thread
From: tip-bot for Thomas Gleixner @ 2016-03-03 10:27 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: linux-kernel, mingo, hpa, tglx, marc.zyngier

Commit-ID:  82e88ff1ea948d83125a8aaa7c9809f03ccc500f
Gitweb:     http://git.kernel.org/tip/82e88ff1ea948d83125a8aaa7c9809f03ccc500f
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Thu, 3 Mar 2016 11:11:12 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
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.

Reported-by: Marc Zyngier <marc.zyngier@arm.com>
Link: http://lkml.kernel.org/r/56D6CEF0.8060607@arm.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/hrtimer.h   |  1 -
 kernel/time/hrtimer.c     | 18 ++----------------
 virt/kvm/arm/arch_timer.c |  4 ++--
 3 files changed, 4 insertions(+), 19 deletions(-)

diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index a6d64af..76dd4f0 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -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,
 };
 
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index cb0fe70..435b885 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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;
diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 97c5815..69bca18 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -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;
 }
 

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

* [tip:timers/core] hrtimer: Catch invalid clockids again
  2016-01-15 17:41 ` [PATCH 2/3] hrtimer: Catch illegal clockids Marc Zyngier
  2016-01-27 11:43   ` [tip:timers/core] " tip-bot for Marc Zyngier
@ 2017-02-18 10:01   ` tip-bot for Marc Zyngier
  1 sibling, 0 replies; 10+ messages in thread
From: tip-bot for Marc Zyngier @ 2017-02-18 10:01 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, linux-kernel, tglx, marc.zyngier, mingo, christoffer.dall, tn

Commit-ID:  336a9cde10d641e70bac67d90ae91b3190c3edca
Gitweb:     http://git.kernel.org/tip/336a9cde10d641e70bac67d90ae91b3190c3edca
Author:     Marc Zyngier <marc.zyngier@arm.com>
AuthorDate: Fri, 15 Jan 2016 17:41:09 +0000
Committer:  Thomas Gleixner <tglx@linutronix.de>
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 ]

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Cc: Tomasz Nowicki <tn@semihalf.com>
Cc: Christoffer Dall <christoffer.dall@linaro.org>
Link: http://lkml.kernel.org/r/1452879670-16133-3-git-send-email-marc.zyngier@arm.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/hrtimer.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index edabde6..8e11d8d 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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)
 {

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

end of thread, other threads:[~2017-02-18 10:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-15 17:41 [PATCH 0/3] Enabling htimer support for CLOCK_MONOTONIC_RAW Marc Zyngier
2016-01-15 17:41 ` [PATCH 1/3] hrtimer: Add " Marc Zyngier
2016-01-27 11:42   ` [tip:timers/core] " tip-bot for Marc Zyngier
2016-03-02 11:30     ` Marc Zyngier
2016-03-03 10:27       ` [tip:timers/core] hrtimer: Revert CLOCK_MONOTONIC_RAW support tip-bot for Thomas Gleixner
2016-01-15 17:41 ` [PATCH 2/3] hrtimer: Catch illegal clockids Marc Zyngier
2016-01-27 11:43   ` [tip:timers/core] " tip-bot for Marc Zyngier
2017-02-18 10:01   ` [tip:timers/core] hrtimer: Catch invalid clockids again tip-bot for Marc Zyngier
2016-01-15 17:41 ` [PATCH 3/3] KVM: arm/arm64: timer: Switch to CLOCK_MONOTONIC_RAW Marc Zyngier
2016-01-27 11:43   ` [tip:timers/core] " tip-bot for Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).