Skip to contentSkip to conversation

KVM guest: prevent tracing recursion with kvmclock

PATCH In review Avi Kivity 3 participants 5 messages Nov 15, 2011

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 KVM guest: prevent tracing recursion with kvmclock

Stop tracing when we read the clock, since tracing will also want to read the clock, and recurse indefinitely.

Based on a similar patch for Xen from Jeremy Fitzhardinge.

---
 arch/x86/kernel/kvmclock.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +3 -2
index c1a0188..44842d7 100644
@@ -74,9 +74,10 @@ static cycle_t kvm_clock_read(void)
 	struct pvclock_vcpu_time_info *src;
 	cycle_t ret;
 
-	src = &get_cpu_var(hv_clock);
+	preempt_disable_notrace();
+	src = &__get_cpu_var(hv_clock);
 	ret = pvclock_clocksource_read(src);
-	put_cpu_var(hv_clock);
+	preempt_enable_notrace();
 	return ret;
 }
 
Signature · 1 line
1.7.7.1
Tested-by on v1 1/1
#1 lore raw
Quoted 6 lines · Avi Kivity

Stop tracing when we read the clock, since tracing will also want to read the clock, and recurse indefinitely.

Based on a similar patch for Xen from Jeremy Fitzhardinge.

Quoted 23 lines
---
 arch/x86/kernel/kvmclock.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +3 -2
index c1a0188..44842d7 100644
@@ -74,9 +74,10 @@ static cycle_t kvm_clock_read(void)
 	struct pvclock_vcpu_time_info *src;
 	cycle_t ret;
 
-	src = &get_cpu_var(hv_clock);
+	preempt_disable_notrace();
+	src = &__get_cpu_var(hv_clock);
 	ret = pvclock_clocksource_read(src);
-	put_cpu_var(hv_clock);
+	preempt_enable_notrace();
 	return ret;
 }
 
Signature · 1 line
1.7.7.1

--
Gleb.

Acked-by on v1 1/1
#2 lore raw
In reply to Avi Kivity
Quoted 2 lines · Avi Kivity

Stop tracing when we read the clock, since tracing will also want to read the clock, and recurse indefinitely.

I would rephrase the above. You don't actually stop tracing, you just don't trace the preempt disable. I would reword that to something like:

Prevent tracing of preempt_disable() in get_cpu_var() in kvm_clock_read(). When CONFIG_DEBUG_PREEMPT is enabled, preempt_disable/enable() are traced and this causes the function_graph tracer to go into an infinite recursion. By open coding the preempt_disable() around the get_cpu_var(), we can use the notrace version which prevents preempt_disable/enable() from being traced and prevents the recursion.

Something like the above.

Quoted 4 lines

Based on a similar patch for Xen from Jeremy Fitzhardinge.

This was exactly my first thought, but I was thinking it may be better to have a get_cpu_var_notrace() than to have to open code this stuff. Maybe there's not that many users that open code is not an issue. I'll still want to add that recursion protection with the warn on though.

-- Steve

Quoted 21 lines
---
 arch/x86/kernel/kvmclock.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +3 -2
index c1a0188..44842d7 100644
@@ -74,9 +74,10 @@ static cycle_t kvm_clock_read(void)
 	struct pvclock_vcpu_time_info *src;
 	cycle_t ret;
 
-	src = &get_cpu_var(hv_clock);
+	preempt_disable_notrace();
+	src = &__get_cpu_var(hv_clock);
 	ret = pvclock_clocksource_read(src);
-	put_cpu_var(hv_clock);
+	preempt_enable_notrace();
 	return ret;
 }
 
author
#3 lore raw
Quoted 15 lines · Steven Rostedt
Quoted 2 lines · Avi Kivity

Stop tracing when we read the clock, since tracing will also want to read the clock, and recurse indefinitely.

I would rephrase the above. You don't actually stop tracing, you just don't trace the preempt disable. I would reword that to something like:

Prevent tracing of preempt_disable() in get_cpu_var() in kvm_clock_read(). When CONFIG_DEBUG_PREEMPT is enabled, preempt_disable/enable() are traced and this causes the function_graph tracer to go into an infinite recursion. By open coding the preempt_disable() around the get_cpu_var(), we can use the notrace version which prevents preempt_disable/enable() from being traced and prevents the recursion.

Quoted 1 line

Something like the above.

Thanks, I adopted your wording.

Quoted 10 lines
Quoted 4 lines

Based on a similar patch for Xen from Jeremy Fitzhardinge.

This was exactly my first thought, but I was thinking it may be better to have a get_cpu_var_notrace() than to have to open code this stuff. Maybe there's not that many users that open code is not an issue. I'll still want to add that recursion protection with the warn on though.

What about function traces? Will any noninlined calls cause the same problem?

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 3 lines · Avi Kivity

What about function traces? Will any noninlined calls cause the same problem?

Both function_graph and function tracing have recursion protection around the users of the call. function tracing is much lighter weight than function_graph tracing. The problem here was that function_graph tracing does some accounting, and calls the clock handler outside the recursion protection. I need to add recursion protection around the accounting too. But I still want to warn when it happens as recursion protection protects from crashing, but recursion is still bad because it still goes through a bit of arch specific setup before the recursion is detected, slowing function_graph tracing down much more than function tracing when it recurses.

-- Steve