Skip to contentSkip to conversation

rcu,nohz,kvm: use RCU extended quiescent state when running KVM guest

PATCH In review [email protected] 4 participants 19 messages Feb 10, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rcu,nohz,kvm: use RCU extended quiescent state when running KVM guest

When running a KVM guest on a system with NOHZ_FULL enabled, and the KVM guest running with idle=poll mode, we still get wakeups of the rcuos/N threads.

This problem has already been solved for user space by telling the RCU subsystem that the CPU is in an extended quiescent state while running user space code.

This patch series extends that code a little bit to make it usable to track KVM guest space, too.

I tested the code by booting a KVM guest with idle=poll, on a system with NOHZ_FULL enabled on most CPUs, and a VCPU thread bound to a CPU. In a 10 second interval, rcuos/N threads on other CPUs got woken up several times, while the rcuos thread on the CPU running the bound and alwasy running VCPU thread never got woken up once.

Thanks to Christian Borntraeger, Paul McKenney, Paulo Bonzini, Frederic Weisbecker, and Will Deacon for reviewing and improving earlier versions of this patch series.

#1 lore raw

PATCH 1/6 rcu,nohz: add context_tracking_user_enter/exit wrapper functions

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

---
 include/linux/context_tracking.h |  2 ++
 kernel/context_tracking.c        | 37 +++++++++++++++++++++++++------------
 2 files changed, 27 insertions(+), 12 deletions(-)
include/linux/context_tracking.h +2 -0
index 37b81bd51ec0..03b9c733eae7 100644
@@ -10,6 +10,8 @@
 #ifdef CONFIG_CONTEXT_TRACKING
 extern void context_tracking_cpu_set(int cpu);
 
+extern void context_tracking_enter(void);
+extern void context_tracking_exit(void);
 extern void context_tracking_user_enter(void);
 extern void context_tracking_user_exit(void);
 extern void __context_tracking_task_switch(struct task_struct *prev,
kernel/context_tracking.c +25 -12
index 937ecdfdf258..bbdc423936e6 100644
@@ -39,15 +39,15 @@ void context_tracking_cpu_set(int cpu)
 }
 
 /**
- * context_tracking_user_enter - Inform the context tracking that the CPU is going to
- *                               enter userspace mode.
+ * context_tracking_enter - Inform the context tracking that the CPU is going
+ *                          enter user or guest space mode.
  *
  * This function must be called right before we switch from the kernel
- * to userspace, when it's guaranteed the remaining kernel instructions
- * to execute won't use any RCU read side critical section because this
- * function sets RCU in extended quiescent state.
+ * to user or guest space, when it's guaranteed the remaining kernel
+ * instructions to execute won't use any RCU read side critical section
+ * because this function sets RCU in extended quiescent state.
  */
-void context_tracking_user_enter(void)
+void context_tracking_enter(void)
 {
 	unsigned long flags;
 
@@ -105,20 +105,27 @@ void context_tracking_user_enter(void)
 	}
 	local_irq_restore(flags);
 }
+NOKPROBE_SYMBOL(context_tracking_enter);
+
+void context_tracking_user_enter(void)
+{
+	context_tracking_enter();
+}
 NOKPROBE_SYMBOL(context_tracking_user_enter);
 
 /**
- * context_tracking_user_exit - Inform the context tracking that the CPU is
- *                              exiting userspace mode and entering the kernel.
+ * context_tracking_exit - Inform the context tracking that the CPU is
+ *                         exiting user or guest mode and entering the kernel.
  *
- * This function must be called after we entered the kernel from userspace
- * before any use of RCU read side critical section. This potentially include
- * any high level kernel code like syscalls, exceptions, signal handling, etc...
+ * This function must be called after we entered the kernel from user or
+ * guest space before any use of RCU read side critical section. This
+ * potentially include any high level kernel code like syscalls, exceptions,
+ * signal handling, etc...
  *
  * This call supports re-entrancy. This way it can be called from any exception
  * handler without needing to know if we came from userspace or not.
  */
-void context_tracking_user_exit(void)
+void context_tracking_exit(void)
 {
 	unsigned long flags;
 
@@ -143,6 +150,12 @@ void context_tracking_user_exit(void)
 	}
 	local_irq_restore(flags);
 }
+NOKPROBE_SYMBOL(context_tracking_exit);
+
+void context_tracking_user_exit(void)
+{
+	context_tracking_exit();
+}
 NOKPROBE_SYMBOL(context_tracking_user_exit);
 
 /**
Signature · 1 line
1.9.3
#2 lore raw

Re: [PATCH 1/6] rcu,nohz: add context_tracking_user_enter/exit wrapper functions

Quoted 8 lines · On Tue, Feb 10, 2015 at 09:41:45AM -0500, [email protected] wrote:

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

This patch alone doesn't make much sense. The changelog says it's about keeping context_tracking_user_*() functions as wrappers but fails to explain to what they wrap, why and what are the new context_tracking_enter/exit functions for.

Perhaps patches 1 and 2 should be merged together into something like:

context_tracking: Generalize context tracking APIs to support user and guest

Do that because we'll also track guest....etc And keep the old user context tracking APIs for now to avoid painful enum parameter support in ARM assembly....

Quoted 65 lines
---
 include/linux/context_tracking.h |  2 ++
 kernel/context_tracking.c        | 37 +++++++++++++++++++++++++------------
 2 files changed, 27 insertions(+), 12 deletions(-)
include/linux/context_tracking.h +2 -0
index 37b81bd51ec0..03b9c733eae7 100644
@@ -10,6 +10,8 @@
 #ifdef CONFIG_CONTEXT_TRACKING
 extern void context_tracking_cpu_set(int cpu);
 
+extern void context_tracking_enter(void);
+extern void context_tracking_exit(void);
 extern void context_tracking_user_enter(void);
 extern void context_tracking_user_exit(void);
 extern void __context_tracking_task_switch(struct task_struct *prev,
kernel/context_tracking.c +14 -11
index 937ecdfdf258..bbdc423936e6 100644
@@ -39,15 +39,15 @@ void context_tracking_cpu_set(int cpu)
 }
 
 /**
- * context_tracking_user_enter - Inform the context tracking that the CPU is going to
- *                               enter userspace mode.
+ * context_tracking_enter - Inform the context tracking that the CPU is going
+ *                          enter user or guest space mode.
  *
  * This function must be called right before we switch from the kernel
- * to userspace, when it's guaranteed the remaining kernel instructions
- * to execute won't use any RCU read side critical section because this
- * function sets RCU in extended quiescent state.
+ * to user or guest space, when it's guaranteed the remaining kernel
+ * instructions to execute won't use any RCU read side critical section
+ * because this function sets RCU in extended quiescent state.
  */
-void context_tracking_user_enter(void)
+void context_tracking_enter(void)
 {
 	unsigned long flags;
 
@@ -105,20 +105,27 @@ void context_tracking_user_enter(void)
 	}
 	local_irq_restore(flags);
 }
+NOKPROBE_SYMBOL(context_tracking_enter);
+
+void context_tracking_user_enter(void)
+{
+	context_tracking_enter();
+}
 NOKPROBE_SYMBOL(context_tracking_user_enter);
 
 /**
- * context_tracking_user_exit - Inform the context tracking that the CPU is
- *                              exiting userspace mode and entering the kernel.
+ * context_tracking_exit - Inform the context tracking that the CPU is
+ *                         exiting user or guest mode and entering the kernel.
  *
- * This function must be called after we entered the kernel from userspace
- * before any use of RCU read side critical section. This potentially include
- * any high level kernel code like syscalls, exceptions, signal handling, etc...

kernel/context_tracking.c void context_tracking_user_enter(void) · line 128

Quoted 10 lines
kernel/context_tracking.c +5 -1
+ * This function must be called after we entered the kernel from user or
+ * guest space before any use of RCU read side critical section. This
+ * potentially include any high level kernel code like syscalls, exceptions,
+ * signal handling, etc...
  *
  * This call supports re-entrancy. This way it can be called from any exception
  * handler without needing to know if we came from userspace or not.
  */
-void context_tracking_user_exit(void)
+void context_tracking_exit(void)

Comment changes are good, thanks!

Quoted 19 lines
kernel/context_tracking.c +6 -0
 {
 	unsigned long flags;
 
@@ -143,6 +150,12 @@ void context_tracking_user_exit(void)
 	}
 	local_irq_restore(flags);
 }
+NOKPROBE_SYMBOL(context_tracking_exit);
+
+void context_tracking_user_exit(void)
+{
+	context_tracking_exit();
+}
 NOKPROBE_SYMBOL(context_tracking_user_exit);
 
 /**
Signature · 1 line
1.9.3
#3 lore raw

Re: [PATCH 1/6] rcu,nohz: add context_tracking_user_enter/exit wrapper functions

Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines · On Tue, Feb 10, 2015 at 09:41:45AM -0500, [email protected] wrote:

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

This patch alone doesn't make much sense.

Agreed, my goal was to keep things super easy to review, to reduce the chance of introducing bugs.

Quoted 10 lines

The changelog says it's about keeping
context_tracking_user_*() functions as wrappers but fails to explain to what they wrap, why and what are the new context_tracking_enter/exit functions for.

Perhaps patches 1 and 2 should be merged together into something like:

context_tracking: Generalize context tracking APIs to support user and guest

Do that because we'll also track guest....etc And keep the old user context tracking APIs for now to avoid painful enum parameter support in ARM assembly....

Can do...

Paul, would you like me to resend the whole series, or just a merged patch that replaces patches 1 & 2?

That is assuming Paul prefers having the patches merged into one :)

#4 lore raw

Re: [PATCH 1/6] rcu,nohz: add context_tracking_user_enter/exit wrapper functions

Quoted 24 lines · Rik van Riel
Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines · On Tue, Feb 10, 2015 at 09:41:45AM -0500, [email protected] wrote:

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

This patch alone doesn't make much sense.

Agreed, my goal was to keep things super easy to review, to reduce the chance of introducing bugs.

Quoted 8 lines

The changelog says it's about keeping
context_tracking_user_*() functions as wrappers but fails to explain to what they wrap, why and what are the new context_tracking_enter/exit functions for.

Perhaps patches 1 and 2 should be merged together into something like:

context_tracking: Generalize context tracking APIs to support user and guest

Quoted 7 lines
Quoted 2 lines

Do that because we'll also track guest....etc And keep the old user context tracking APIs for now to avoid painful enum parameter support in ARM assembly....

Can do...

Paul, would you like me to resend the whole series, or just a merged patch that replaces patches 1 & 2?

I prefer the whole series, as it reduces my opportunity to introduce human error when applying them. ;-)

Quoted 2 lines

That is assuming Paul prefers having the patches merged into one :)

I am OK with that. I will merge the next set with the first two patches merged, people have had sufficient opportunity to review.

Thanx, Paul

#5 lore raw

Re: [PATCH 1/6] rcu,nohz: add context_tracking_user_enter/exit wrapper functions

Quoted 33 lines · Paul E. McKenney
Quoted 24 lines · Rik van Riel
Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines · On Tue, Feb 10, 2015 at 09:41:45AM -0500, [email protected] wrote:

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

This patch alone doesn't make much sense.

Agreed, my goal was to keep things super easy to review, to reduce the chance of introducing bugs.

Quoted 8 lines

The changelog says it's about keeping
context_tracking_user_*() functions as wrappers but fails to explain to what they wrap, why and what are the new context_tracking_enter/exit functions for.

Perhaps patches 1 and 2 should be merged together into something like:

context_tracking: Generalize context tracking APIs to support user and guest

Quoted 7 lines
Quoted 2 lines

Do that because we'll also track guest....etc And keep the old user context tracking APIs for now to avoid painful enum parameter support in ARM assembly....

Can do...

Paul, would you like me to resend the whole series, or just a merged patch that replaces patches 1 & 2?

Quoted 8 lines

I prefer the whole series, as it reduces my opportunity to introduce human error when applying them. ;-)

Quoted 2 lines

That is assuming Paul prefers having the patches merged into one :)

I am OK with that. I will merge the next set with the first two patches merged, people have had sufficient opportunity to review.

BTW, I have a few patches to make on the next cycle to fix a few context tracking related things. And since it's too late to push this series for the current merge window, now I wonder it may be easier if I take these patches. Otherwise you might experience unpleasant rebase conflicts. Is that ok for you?

Thanks.

#6 lore raw

Re: [PATCH 1/6] rcu,nohz: add context_tracking_user_enter/exit wrapper functions

Quoted 40 lines · Frederic Weisbecker
Quoted 39 lines · Paul E. McKenney
Quoted 24 lines · Rik van Riel
Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines · On Tue, Feb 10, 2015 at 09:41:45AM -0500, [email protected] wrote:

From: Rik van Riel <[email protected]>

These wrapper functions allow architecture code (eg. ARM) to keep calling context_tracking_user_enter & context_tracking_user_exit the same way it always has, without error prone tricks like duplicate defines of argument values in assembly code.

This patch alone doesn't make much sense.

Agreed, my goal was to keep things super easy to review, to reduce the chance of introducing bugs.

Quoted 8 lines

The changelog says it's about keeping
context_tracking_user_*() functions as wrappers but fails to explain to what they wrap, why and what are the new context_tracking_enter/exit functions for.

Perhaps patches 1 and 2 should be merged together into something like:

context_tracking: Generalize context tracking APIs to support user and guest

Quoted 7 lines
Quoted 2 lines

Do that because we'll also track guest....etc And keep the old user context tracking APIs for now to avoid painful enum parameter support in ARM assembly....

Can do...

Paul, would you like me to resend the whole series, or just a merged patch that replaces patches 1 & 2?

I prefer the whole series, as it reduces my opportunity to introduce human error when applying them. ;-)

Quoted 2 lines

That is assuming Paul prefers having the patches merged into one :)

Quoted 7 lines
Quoted 2 lines

I am OK with that. I will merge the next set with the first two patches merged, people have had sufficient opportunity to review.

BTW, I have a few patches to make on the next cycle to fix a few context tracking related things. And since it's too late to push this series for the current merge window, now I wonder it may be easier if I take these patches. Otherwise you might experience unpleasant rebase conflicts. Is that ok for you?

Works for me! ;-)

Thanx, Paul

#7 lore raw

PATCH 2/6 rcu,nohz: add state parameter to context_tracking_enter/exit

In reply to [email protected]

From: Rik van Riel <[email protected]>

Add the expected ctx_state as a parameter to context_tracking_enter and context_tracking_exit, allowing the same functions to not just track kernel <> user space switching, but also kernel <> guest transitions.

---
 include/linux/context_tracking.h | 10 +++++-----
 kernel/context_tracking.c        | 14 +++++++-------
 2 files changed, 12 insertions(+), 12 deletions(-)
include/linux/context_tracking.h +5 -5
index 03b9c733eae7..954253283709 100644
@@ -10,8 +10,8 @@
 #ifdef CONFIG_CONTEXT_TRACKING
 extern void context_tracking_cpu_set(int cpu);
 
-extern void context_tracking_enter(void);
-extern void context_tracking_exit(void);
+extern void context_tracking_enter(enum ctx_state state);
+extern void context_tracking_exit(enum ctx_state state);
 extern void context_tracking_user_enter(void);
 extern void context_tracking_user_exit(void);
 extern void __context_tracking_task_switch(struct task_struct *prev,
@@ -37,7 +37,7 @@ static inline enum ctx_state exception_enter(void)
 		return 0;
 
 	prev_ctx = this_cpu_read(context_tracking.state);
-	context_tracking_user_exit();
+	context_tracking_exit(prev_ctx);
 
 	return prev_ctx;
 }
@@ -45,8 +45,8 @@ static inline enum ctx_state exception_enter(void)
 static inline void exception_exit(enum ctx_state prev_ctx)
 {
 	if (context_tracking_is_enabled()) {
-		if (prev_ctx == IN_USER)
-			context_tracking_user_enter();
+		if (prev_ctx != IN_KERNEL)
+			context_tracking_enter(prev_ctx);
 	}
 }
 
kernel/context_tracking.c +7 -7
index bbdc423936e6..38e38aeac8b9 100644
@@ -47,7 +47,7 @@ void context_tracking_cpu_set(int cpu)
  * instructions to execute won't use any RCU read side critical section
  * because this function sets RCU in extended quiescent state.
  */
-void context_tracking_enter(void)
+void context_tracking_enter(enum ctx_state state)
 {
 	unsigned long flags;
 
@@ -75,7 +75,7 @@ void context_tracking_enter(void)
 	WARN_ON_ONCE(!current->mm);
 
 	local_irq_save(flags);
-	if ( __this_cpu_read(context_tracking.state) != IN_USER) {
+	if ( __this_cpu_read(context_tracking.state) != state) {
 		if (__this_cpu_read(context_tracking.active)) {
 			trace_user_enter(0);
 			/*
@@ -101,7 +101,7 @@ void context_tracking_enter(void)
 		 * OTOH we can spare the calls to vtime and RCU when context_tracking.active
 		 * is false because we know that CPU is not tickless.
 		 */
-		__this_cpu_write(context_tracking.state, IN_USER);
+		__this_cpu_write(context_tracking.state, state);
 	}
 	local_irq_restore(flags);
 }
@@ -109,7 +109,7 @@ NOKPROBE_SYMBOL(context_tracking_enter);
 
 void context_tracking_user_enter(void)
 {
-	context_tracking_enter();
+	context_tracking_enter(IN_USER);
 }
 NOKPROBE_SYMBOL(context_tracking_user_enter);
 
@@ -125,7 +125,7 @@ NOKPROBE_SYMBOL(context_tracking_user_enter);
  * This call supports re-entrancy. This way it can be called from any exception
  * handler without needing to know if we came from userspace or not.
  */
-void context_tracking_exit(void)
+void context_tracking_exit(enum ctx_state state)
 {
 	unsigned long flags;
 
@@ -136,7 +136,7 @@ void context_tracking_exit(void)
 		return;
 
 	local_irq_save(flags);
-	if (__this_cpu_read(context_tracking.state) == IN_USER) {
+	if (__this_cpu_read(context_tracking.state) == state) {
 		if (__this_cpu_read(context_tracking.active)) {
 			/*
 			 * We are going to run code that may use RCU. Inform
@@ -154,7 +154,7 @@ NOKPROBE_SYMBOL(context_tracking_exit);
 
 void context_tracking_user_exit(void)
 {
-	context_tracking_exit();
+	context_tracking_exit(IN_USER);
 }
 NOKPROBE_SYMBOL(context_tracking_user_exit);
 
Signature · 1 line
1.9.3
#8 lore raw

PATCH 3/6 nohz: add stub context_tracking_is_enabled

In reply to [email protected]

From: Rik van Riel <[email protected]>

With code elsewhere doing something conditional on whether or not context tracking is enabled, we want a stub function that tells us context tracking is not enabled, when CONFIG_CONTEXT_TRACKING is not set.

---
 include/linux/context_tracking_state.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/context_tracking_state.h +2 -0
index 97a81225d037..72ab10fe1e46 100644
@@ -39,6 +39,8 @@ static inline bool context_tracking_in_user(void)
 #else
 static inline bool context_tracking_in_user(void) { return false; }
 static inline bool context_tracking_active(void) { return false; }
+static inline bool context_tracking_is_enabled(void) { return false; }
+static inline bool context_tracking_cpu_is_enabled(void) { return false; }
 #endif /* CONFIG_CONTEXT_TRACKING */
 
 #endif
Signature · 1 line
1.9.3
#9 lore raw

PATCH 4/6 rcu,nohz: run vtime_user_enter/exit only when state == IN_USER

In reply to [email protected]

From: Rik van Riel <[email protected]>

Only run vtime_user_enter, vtime_user_exit, and the user enter & exit trace points when we are entering or exiting user state, respectively.

The KVM code in guest_enter and guest_exit already take care of calling vtime_guest_enter and vtime_guest_exit, respectively.

The RCU code only distinguishes between "idle" and "not idle or kernel". There should be no need to add an additional (unused) state there.

---
 kernel/context_tracking.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/context_tracking.c +8 -4
index 38e38aeac8b9..0e4e318d5ea4 100644
@@ -77,7 +77,6 @@ void context_tracking_enter(enum ctx_state state)
 	local_irq_save(flags);
 	if ( __this_cpu_read(context_tracking.state) != state) {
 		if (__this_cpu_read(context_tracking.active)) {
-			trace_user_enter(0);
 			/*
 			 * At this stage, only low level arch entry code remains and
 			 * then we'll run in userspace. We can assume there won't be
@@ -85,7 +84,10 @@ void context_tracking_enter(enum ctx_state state)
 			 * user_exit() or rcu_irq_enter(). Let's remove RCU's dependency
 			 * on the tick.
 			 */
-			vtime_user_enter(current);
+			if (state == IN_USER) {
+				trace_user_enter(0);
+				vtime_user_enter(current);
+			}
 			rcu_user_enter();
 		}
 		/*
@@ -143,8 +145,10 @@ void context_tracking_exit(enum ctx_state state)
 			 * RCU core about that (ie: we may need the tick again).
 			 */
 			rcu_user_exit();
-			vtime_user_exit(current);
-			trace_user_exit(0);
+			if (state == IN_USER) {
+				vtime_user_exit(current);
+				trace_user_exit(0);
+			}
 		}
 		__this_cpu_write(context_tracking.state, IN_KERNEL);
 	}
Signature · 1 line
1.9.3
#10 lore raw

PATCH 5/6 nohz,kvm: export context_tracking_user_enter/exit

In reply to [email protected]

From: Rik van Riel <[email protected]>

Export context_tracking_user_enter/exit so it can be used by KVM.

---
 kernel/context_tracking.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/context_tracking.c +2 -0
index 0e4e318d5ea4..5bdf1a342ab3 100644
@@ -108,6 +108,7 @@ void context_tracking_enter(enum ctx_state state)
 	local_irq_restore(flags);
 }
 NOKPROBE_SYMBOL(context_tracking_enter);
+EXPORT_SYMBOL_GPL(context_tracking_enter);
 
 void context_tracking_user_enter(void)
 {
@@ -155,6 +156,7 @@ void context_tracking_exit(enum ctx_state state)
 	local_irq_restore(flags);
 }
 NOKPROBE_SYMBOL(context_tracking_exit);
+EXPORT_SYMBOL_GPL(context_tracking_exit);
 
 void context_tracking_user_exit(void)
 {
Signature · 1 line
1.9.3
#11 lore raw

PATCH 6/6 kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

In reply to [email protected]

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +6 -0
index 954253283709..b65fd1420e53 100644
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
 		vtime_guest_enter(current);
 	else
 		current->flags |= PF_VCPU;
+
+	if (context_tracking_is_enabled())
+		context_tracking_enter(IN_GUEST);
 }
 
 static inline void guest_exit(void)
 {
+	if (context_tracking_is_enabled())
+		context_tracking_exit(IN_GUEST);
+
 	if (vtime_accounting_enabled())
 		vtime_guest_exit(current);
 	else
include/linux/context_tracking_state.h +1 -0
index 72ab10fe1e46..90a7bab8779e 100644
@@ -15,6 +15,7 @@ struct context_tracking {
 	enum ctx_state {
 		IN_KERNEL = 0,
 		IN_USER,
+		IN_GUEST,
 	} state;
 };
 
include/linux/kvm_host.h +2 -1
index 26f106022c88..c7828a6a9614 100644
@@ -772,7 +772,8 @@ static inline void kvm_guest_enter(void)
 	 * one time slice). Lets treat guest mode as quiescent state, just like
 	 * we do with user-mode execution.
 	 */
-	rcu_virt_note_context_switch(smp_processor_id());
+	if (!context_tracking_cpu_is_enabled())
+		rcu_virt_note_context_switch(smp_processor_id());
 }
 
 static inline void kvm_guest_exit(void)
Signature · 1 line
1.9.3
#12 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
  include/linux/context_tracking.h       | 6 ++++++
  include/linux/context_tracking_state.h | 1 +
  include/linux/kvm_host.h               | 3 ++-
  3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
  		vtime_guest_enter(current);
  	else
  		current->flags |= PF_VCPU;
+
+	if (context_tracking_is_enabled())
+		context_tracking_enter(IN_GUEST);

Why the if statement?

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

--Andy

#13 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 39 lines · Andy Lutomirski
Quoted 29 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
  include/linux/context_tracking.h       | 6 ++++++
  include/linux/context_tracking_state.h | 1 +
  include/linux/kvm_host.h               | 3 ++-
  3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
          vtime_guest_enter(current);
      else
          current->flags |= PF_VCPU;
+
+    if (context_tracking_is_enabled())
+        context_tracking_enter(IN_GUEST);

Why the if statement?

Quoted 3 lines

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff.

Guest_enter and guest_exit already do the vtime stuff today.

This patch series adds the rcu stuff, and modifies context_tracking_enter & context_tracking_exit to not do the vtime stuff twice.

Quoted 3 lines

(Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

I am all for making it faster :)

#14 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

In reply to Andy Lutomirski
Quoted 38 lines · Andy Lutomirski
Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
 		vtime_guest_enter(current);
 	else
 		current->flags |= PF_VCPU;
+
+	if (context_tracking_is_enabled())
+		context_tracking_enter(IN_GUEST);

Why the if statement?

Quoted 7 lines

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

It turns out that context_tracking_is_enabled() is a static inline that uses a static_key, so the overhead should be minimal on platforms having a full implementation of static keys.

Thanx, Paul

#15 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 47 lines · Paul E. McKenney
Quoted 38 lines · Andy Lutomirski
Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
            vtime_guest_enter(current);
    else
            current->flags |= PF_VCPU;
+
+    if (context_tracking_is_enabled())
+            context_tracking_enter(IN_GUEST);

Why the if statement?

Quoted 7 lines

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

Quoted 3 lines

It turns out that context_tracking_is_enabled() is a static inline that uses a static_key, so the overhead should be minimal on platforms having a full implementation of static keys.

Shouldn't we just fold that into context_tracking_xyz_enter?

Also, why does the vtime stuff depend on RCU extended quiescent states? To me, they seem mostly orthogonal other than the fact that they hook into the same places.

--Andy

#16 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 49 lines · Andy Lutomirski
Quoted 47 lines · Paul E. McKenney
Quoted 45 lines · Andy Lutomirski
Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
            vtime_guest_enter(current);
    else
            current->flags |= PF_VCPU;
+
+    if (context_tracking_is_enabled())
+            context_tracking_enter(IN_GUEST);

Why the if statement?

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

Quoted 5 lines
Quoted 3 lines

It turns out that context_tracking_is_enabled() is a static inline that uses a static_key, so the overhead should be minimal on platforms having a full implementation of static keys.

Shouldn't we just fold that into context_tracking_xyz_enter?

If I am not getting too confused, Rik did that initially, but it caused some pain for the ARM guys. I don't see a performance downside, at least not for a modern compiler that does a decent job of inlining.

Quoted 3 lines

Also, why does the vtime stuff depend on RCU extended quiescent states? To me, they seem mostly orthogonal other than the fact that they hook into the same places.

I might be missing your point, but...

If there are no scheduling-clock interrupts, then the CPU needs to be in an extended quiescent state, otherwise you will get RCU CPU stall warnings and eventually OOM. Similarly, if there are no scheduling-clock interupts, then you need to compute the vtime stuff based on start times and deltas instead of relying on a scheduling-clock interrupt that never comes. So it isn't that the vtime and RCU stuff are directly related, but rather that they both must take evasive action if there are to be no scheduling-clock interrupts for an extended time period.

Therefore, they really need to key off of the same conditions.

Thanx, Paul

#17 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 54 lines · Paul E. McKenney
Quoted 53 lines · Andy Lutomirski
Quoted 50 lines · Paul E. McKenney
Quoted 38 lines · Andy Lutomirski
Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
            vtime_guest_enter(current);
    else
            current->flags |= PF_VCPU;
+
+    if (context_tracking_is_enabled())
+            context_tracking_enter(IN_GUEST);

Why the if statement?

Quoted 7 lines

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

It turns out that context_tracking_is_enabled() is a static inline that uses a static_key, so the overhead should be minimal on platforms having a full implementation of static keys.

Quoted 5 lines
Quoted 1 line

Shouldn't we just fold that into context_tracking_xyz_enter?

If I am not getting too confused, Rik did that initially, but it caused some pain for the ARM guys. I don't see a performance downside, at least not for a modern compiler that does a decent job of inlining.

It's more of a tidiness issue to me than a performance issue.

Quoted 7 lines
Quoted 4 lines

Also, why does the vtime stuff depend on RCU extended quiescent states? To me, they seem mostly orthogonal other than the fact that they hook into the same places.

I might be missing your point, but...

Quoted 8 lines

If there are no scheduling-clock interrupts, then the CPU needs to be in an extended quiescent state, otherwise you will get RCU CPU stall warnings and eventually OOM. Similarly, if there are no scheduling-clock interupts, then you need to compute the vtime stuff based on start times and deltas instead of relying on a scheduling-clock interrupt that never comes. So it isn't that the vtime and RCU stuff are directly related, but rather that they both must take evasive action if there are to be no scheduling-clock interrupts for an extended time period.

I'm probably missing something, but isn't vtime also used for accurate CPU time stats?

--Andy

#18 lore raw

Re: [PATCH 6/6] kvm,rcu,nohz: use RCU extended quiescent state when running KVM guest

Quoted 56 lines · Andy Lutomirski
Quoted 54 lines · Paul E. McKenney
Quoted 53 lines · Andy Lutomirski
Quoted 50 lines · Paul E. McKenney
Quoted 38 lines · Andy Lutomirski
Quoted 28 lines · On 02/10/2015 06:41 AM, [email protected] wrote:

From: Rik van Riel <[email protected]>

The host kernel is not doing anything while the CPU is executing a KVM guest VCPU, so it can be marked as being in an extended quiescent state, identical to that used when running user space code.

The only exception to that rule is when the host handles an interrupt, which is already handled by the irq code, which calls rcu_irq_enter and rcu_irq_exit.

The guest_enter and guest_exit functions already switch vtime accounting independent of context tracking. Leave those calls where they are, instead of moving them into the context tracking code.

---
 include/linux/context_tracking.h       | 6 ++++++
 include/linux/context_tracking_state.h | 1 +
 include/linux/kvm_host.h               | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/context_tracking.h +0 -0
index 954253283709..b65fd1420e53 100644

include/linux/context_tracking.h static inline void guest_enter(void) · line 85

Quoted 6 lines
include/linux/context_tracking.h +3 -0
@@ -80,10 +80,16 @@ static inline void guest_enter(void)
            vtime_guest_enter(current);
    else
            current->flags |= PF_VCPU;
+
+    if (context_tracking_is_enabled())
+            context_tracking_enter(IN_GUEST);

Why the if statement?

Quoted 7 lines

Also, have you checked how much this hurts guest lightweight entry/exit latency? Context tracking is shockingly expensive for reasons I don't fully understand, but hopefully most of it is the vtime stuff. (Context tracking is *so* expensive that I almost think we should set the performance taint flag if we enable it, assuming that flag ended up getting merged. Also, we should make context tracking faster.)

It turns out that context_tracking_is_enabled() is a static inline that uses a static_key, so the overhead should be minimal on platforms having a full implementation of static keys.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Shouldn't we just fold that into context_tracking_xyz_enter?

If I am not getting too confused, Rik did that initially, but it caused some pain for the ARM guys. I don't see a performance downside, at least not for a modern compiler that does a decent job of inlining.

It's more of a tidiness issue to me than a performance issue.

I feel that the current patch does a good job of optimizing global tidiness.

Quoted 15 lines
Quoted 6 lines
Quoted 3 lines

Also, why does the vtime stuff depend on RCU extended quiescent states? To me, they seem mostly orthogonal other than the fact that they hook into the same places.

I might be missing your point, but...

Quoted 8 lines

If there are no scheduling-clock interrupts, then the CPU needs to be in an extended quiescent state, otherwise you will get RCU CPU stall warnings and eventually OOM. Similarly, if there are no scheduling-clock interupts, then you need to compute the vtime stuff based on start times and deltas instead of relying on a scheduling-clock interrupt that never comes. So it isn't that the vtime and RCU stuff are directly related, but rather that they both must take evasive action if there are to be no scheduling-clock interrupts for an extended time period.

Quoted 2 lines

I'm probably missing something, but isn't vtime also used for accurate CPU time stats?

Right.

In my previous email, I only talked about what happens if there is no scheduling-clock interrupt, and your question is instead about what can happen if the scheduling-clock interrupt is enabled. The accurate CPU time stats are optional if you leave the scheduling clock on during userspace execution, but become mandatory in the nohz_full case where the scheduling clock is disabled across userspace execution. So the accurate CPU time stats are mandatory in the same situation where you have an RCU extended quiescent state.

Thanx, Paul