Skip to contentSkip to conversation

KVM: Fix tick-based vtime accounting on x86

RFC PATCH Superseded Sean Christopherson 3 participants 15 messages Apr 13, 2021 – Apr 14, 2021

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: Fix tick-based vtime accounting on x86

This is an alternative to Wanpeng's series[*] to fix tick-based accounting on x86. The approach for fixing the bug is identical: defer accounting until after tick IRQs are handled. The difference is purely in how the context tracking and vtime code is refactored in order to give KVM the hooks it needs to fix the x86 bug.

x86 compile tested only, hence the RFC. If folks like the direction and there are no unsolvable issues, I'll cross-compile, properly test on x86, and post an "official" series.

Sean Christopherson (7):
sched/vtime: Move guest enter/exit vtime accounting to separate
helpers
context_tracking: Move guest enter/exit logic to standalone helpers
context_tracking: Consolidate guest enter/exit wrappers
context_tracking: KVM: Move guest enter/exit wrappers to KVM's domain
KVM: Move vtime accounting of guest exit to separate helper
KVM: x86: Consolidate guest enter/exit logic to common helpers
KVM: x86: Defer tick-based accounting 'til after IRQ handling

 arch/x86/kvm/svm/svm.c           |  39 +-----------
 arch/x86/kvm/vmx/vmx.c           |  39 +-----------
 arch/x86/kvm/x86.c               |   8 +++
 arch/x86/kvm/x86.h               |  48 +++++++++++++++
 include/linux/context_tracking.h | 100 ++++++++-----------------------
 include/linux/kvm_host.h         |  50 ++++++++++++++++
 include/linux/vtime.h            |  45 ++++++++++++--
 7 files changed, 175 insertions(+), 154 deletions(-)
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
author maintainer
#1 lore raw

RFC PATCH 1/7 sched/vtime: Move guest enter/exit vtime accounting to separate helpers

Provide separate helpers for guest enter/exit vtime accounting instead of open coding the logic within the context tracking code. This will allow KVM x86 to handle vtime accounting slightly differently when using tick- based accounting.

Opportunstically delete the vtime_account_kernel() stub now that all callers are wrapped with CONFIG_VIRT_CPU_ACCOUNTING_NATIVE=y.

No functional change intended.

---
 include/linux/context_tracking.h | 17 +++---------
 include/linux/vtime.h            | 45 +++++++++++++++++++++++++++++---
 2 files changed, 45 insertions(+), 17 deletions(-)
include/linux/context_tracking.h +4 -13
index bceb06498521..58f9a7251d3b 100644
@@ -102,16 +102,12 @@ extern void context_tracking_init(void);
 static inline void context_tracking_init(void) { }
 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
 
-
 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
 /* must be called with irqs disabled */
 static __always_inline void guest_enter_irqoff(void)
 {
 	instrumentation_begin();
-	if (vtime_accounting_enabled_this_cpu())
-		vtime_guest_enter(current);
-	else
-		current->flags |= PF_VCPU;
+	vtime_account_guest_enter();
 	instrumentation_end();
 
 	if (context_tracking_enabled())
@@ -137,10 +133,7 @@ static __always_inline void guest_exit_irqoff(void)
 		__context_tracking_exit(CONTEXT_GUEST);
 
 	instrumentation_begin();
-	if (vtime_accounting_enabled_this_cpu())
-		vtime_guest_exit(current);
-	else
-		current->flags &= ~PF_VCPU;
+	vtime_account_guest_exit();
 	instrumentation_end();
 }
 
@@ -153,8 +146,7 @@ static __always_inline void guest_enter_irqoff(void)
 	 * to flush.
 	 */
 	instrumentation_begin();
-	vtime_account_kernel(current);
-	current->flags |= PF_VCPU;
+	vtime_account_guest_enter();
 	rcu_virt_note_context_switch(smp_processor_id());
 	instrumentation_end();
 }
@@ -163,8 +155,7 @@ static __always_inline void guest_exit_irqoff(void)
 {
 	instrumentation_begin();
 	/* Flush the guest cputime we spent on the guest */
-	vtime_account_kernel(current);
-	current->flags &= ~PF_VCPU;
+	vtime_account_guest_exit();
 	instrumentation_end();
 }
 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
include/linux/vtime.h +41 -4

74 lines not on this page · raw message

Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#2 lore raw

Re: [RFC PATCH 1/7] sched/vtime: Move guest enter/exit vtime accounting to separate helpers

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sean,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next linus/master v5.12-rc7 next-20210413] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
config: powerpc-allyesconfig (attached as .config)
compiler: powerpc64-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c68e2a1489e453b217384eb985d04dd6784c3b53
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106 git checkout c68e2a1489e453b217384eb985d04dd6784c3b53
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=powerpc

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

In file included from include/linux/hardirq.h:9,
from include/linux/interrupt.h:11, from include/linux/kernel_stat.h:9, from include/linux/cgroup.h:26, from include/linux/memcontrol.h:13, from include/linux/swap.h:9,
from include/linux/suspend.h:5, from arch/powerpc/kernel/asm-offsets.c:23:
include/linux/vtime.h: In function 'vtime_account_guest_enter':

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel'; did you mean 'vtime_account_guest_enter'? [-Werror=implicit-function-declaration]
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~
         |  vtime_account_guest_enter
   include/linux/vtime.h: At top level:
>> include/linux/vtime.h:103:13: warning: conflicting types for 'vtime_account_kernel'
     103 | extern void vtime_account_kernel(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~~~~
   include/linux/vtime.h:25:2: note: previous implicit declaration of 'vtime_account_kernel' was here
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
--
In file included from include/linux/hardirq.h:9,
from include/linux/interrupt.h:11, from include/linux/kernel_stat.h:9, from include/linux/cgroup.h:26, from include/linux/memcontrol.h:13, from include/linux/swap.h:9,
from include/linux/suspend.h:5, from arch/powerpc/kernel/asm-offsets.c:23:
include/linux/vtime.h: In function 'vtime_account_guest_enter':

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel'; did you mean 'vtime_account_guest_enter'? [-Werror=implicit-function-declaration]
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~
         |  vtime_account_guest_enter
   include/linux/vtime.h: At top level:
>> include/linux/vtime.h:103:13: warning: conflicting types for 'vtime_account_kernel'
     103 | extern void vtime_account_kernel(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~~~~
   include/linux/vtime.h:25:2: note: previous implicit declaration of 'vtime_account_kernel' was here
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
--
In file included from include/linux/hardirq.h:9,
from include/linux/interrupt.h:11,
from include/linux/kernel_stat.h:9,
from include/linux/cgroup.h:26,
from include/linux/memcontrol.h:13,
from include/linux/swap.h:9,
from include/linux/suspend.h:5,
from arch/powerpc/kernel/asm-offsets.c:23:
include/linux/vtime.h: In function 'vtime_account_guest_enter':

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel'; did you mean 'vtime_account_guest_enter'? [-Werror=implicit-function-declaration]
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~
         |  vtime_account_guest_enter
   include/linux/vtime.h: At top level:
>> include/linux/vtime.h:103:13: warning: conflicting types for 'vtime_account_kernel'
     103 | extern void vtime_account_kernel(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~~~~
   include/linux/vtime.h:25:2: note: previous implicit declaration of 'vtime_account_kernel' was here
      25 |  vtime_account_kernel(current);
         |  ^~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +25 include/linux/vtime.h

    22	
    23	static __always_inline void vtime_account_guest_enter(void)
    24	{
  > 25		vtime_account_kernel(current);
    26		current->flags |= PF_VCPU;
    27	}
    28	
    29	static __always_inline void vtime_account_guest_exit(void)
    30	{
    31	
    32	}
    33	
    34	#elif defined(CONFIG_VIRT_CPU_ACCOUNTING_GEN)
    35	
    36	/*
    37	 * Checks if vtime is enabled on some CPU. Cputime readers want to be careful
    38	 * in that case and compute the tickless cputime.
    39	 * For now vtime state is tied to context tracking. We might want to decouple
    40	 * those later if necessary.
    41	 */
    42	static inline bool vtime_accounting_enabled(void)
    43	{
    44		return context_tracking_enabled();
    45	}
    46	
    47	static inline bool vtime_accounting_enabled_cpu(int cpu)
    48	{
    49		return context_tracking_enabled_cpu(cpu);
    50	}
    51	
    52	static inline bool vtime_accounting_enabled_this_cpu(void)
    53	{
    54		return context_tracking_enabled_this_cpu();
    55	}
    56	
    57	extern void vtime_task_switch_generic(struct task_struct *prev);
    58	
    59	static inline void vtime_task_switch(struct task_struct *prev)
    60	{
    61		if (vtime_accounting_enabled_this_cpu())
    62			vtime_task_switch_generic(prev);
    63	}
    64	
    65	static __always_inline void vtime_account_guest_enter(void)
    66	{
    67		if (vtime_accounting_enabled_this_cpu())
    68			vtime_guest_enter(current);
    69		else
    70			current->flags |= PF_VCPU;
    71	}
    72	
    73	static __always_inline void vtime_account_guest_exit(void)
    74	{
    75		if (vtime_accounting_enabled_this_cpu())
    76			vtime_guest_exit(current);
    77		else
    78			current->flags &= ~PF_VCPU;
    79	}
    80	
    81	
    82	#else /* !CONFIG_VIRT_CPU_ACCOUNTING */
    83	
    84	static inline bool vtime_accounting_enabled_this_cpu(void) { return false; }
    85	static inline void vtime_task_switch(struct task_struct *prev) { }
    86	
    87	static __always_inline void vtime_account_guest_enter(void)
    88	{
    89		current->flags |= PF_VCPU;
    90	}
    91	
    92	static __always_inline void vtime_account_guest_exit(void)
    93	{
    94		current->flags &= ~PF_VCPU;
    95	}
    96	
    97	#endif
    98	
    99	/*
   100	 * Common vtime APIs
   101	 */
   102	#ifdef CONFIG_VIRT_CPU_ACCOUNTING
 > 103	extern void vtime_account_kernel(struct task_struct *tsk);

104 extern void vtime_account_idle(struct task_struct *tsk);
105 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
106

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [RFC PATCH 1/7] sched/vtime: Move guest enter/exit vtime accounting to separate helpers

In reply to Sean Christopherson
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sean,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next linus/master v5.12-rc7 next-20210413] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
config: powerpc64-randconfig-r023-20210413 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 9829f5e6b1bca9b61efc629770d28bb9014dec45)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install powerpc64 cross compiling tool for clang build
# apt-get install binutils-powerpc64-linux-gnu
# https://github.com/0day-ci/linux/commit/c68e2a1489e453b217384eb985d04dd6784c3b53
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106 git checkout c68e2a1489e453b217384eb985d04dd6784c3b53
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=powerpc64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:

>> include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_enter(current);
                   ^
>> include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_exit(current);
                   ^
>> include/linux/vtime.h:111:13: error: conflicting types for 'vtime_guest_enter'
   extern void vtime_guest_enter(struct task_struct *tsk);
               ^
   include/linux/vtime.h:68:3: note: previous implicit declaration is here
                   vtime_guest_enter(current);
                   ^
>> include/linux/vtime.h:112:13: error: conflicting types for 'vtime_guest_exit'
   extern void vtime_guest_exit(struct task_struct *tsk);
               ^
   include/linux/vtime.h:76:3: note: previous implicit declaration is here
                   vtime_guest_exit(current);
                   ^

4 errors generated.
--
In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:

>> include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_enter(current);
                   ^
>> include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_exit(current);
                   ^
>> include/linux/vtime.h:111:13: error: conflicting types for 'vtime_guest_enter'
   extern void vtime_guest_enter(struct task_struct *tsk);
               ^
   include/linux/vtime.h:68:3: note: previous implicit declaration is here
                   vtime_guest_enter(current);
                   ^
>> include/linux/vtime.h:112:13: error: conflicting types for 'vtime_guest_exit'
   extern void vtime_guest_exit(struct task_struct *tsk);
               ^
   include/linux/vtime.h:76:3: note: previous implicit declaration is here
                   vtime_guest_exit(current);
                   ^

4 errors generated.
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/vtime_guest_enter +68 include/linux/vtime.h

    64	
    65	static __always_inline void vtime_account_guest_enter(void)
    66	{
    67		if (vtime_accounting_enabled_this_cpu())
  > 68			vtime_guest_enter(current);
    69		else
    70			current->flags |= PF_VCPU;
    71	}
    72	
    73	static __always_inline void vtime_account_guest_exit(void)
    74	{
    75		if (vtime_accounting_enabled_this_cpu())
  > 76			vtime_guest_exit(current);
    77		else
    78			current->flags &= ~PF_VCPU;
    79	}
    80	
    81	
    82	#else /* !CONFIG_VIRT_CPU_ACCOUNTING */
    83	
    84	static inline bool vtime_accounting_enabled_this_cpu(void) { return false; }
    85	static inline void vtime_task_switch(struct task_struct *prev) { }
    86	
    87	static __always_inline void vtime_account_guest_enter(void)
    88	{
    89		current->flags |= PF_VCPU;
    90	}
    91	
    92	static __always_inline void vtime_account_guest_exit(void)
    93	{
    94		current->flags &= ~PF_VCPU;
    95	}
    96	
    97	#endif
    98	
    99	/*
   100	 * Common vtime APIs
   101	 */
   102	#ifdef CONFIG_VIRT_CPU_ACCOUNTING
   103	extern void vtime_account_kernel(struct task_struct *tsk);
   104	extern void vtime_account_idle(struct task_struct *tsk);
   105	#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
   106	
   107	#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
   108	extern void arch_vtime_task_switch(struct task_struct *tsk);
   109	extern void vtime_user_enter(struct task_struct *tsk);
   110	extern void vtime_user_exit(struct task_struct *tsk);
 > 111	extern void vtime_guest_enter(struct task_struct *tsk);
 > 112	extern void vtime_guest_exit(struct task_struct *tsk);

113 extern void vtime_init_idle(struct task_struct *tsk, int cpu);
114 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */
115 static inline void vtime_user_enter(struct task_struct *tsk) { }
116 static inline void vtime_user_exit(struct task_struct *tsk) { }
117 static inline void vtime_guest_enter(struct task_struct *tsk) { }
118 static inline void vtime_guest_exit(struct task_struct *tsk) { }
119 static inline void vtime_init_idle(struct task_struct *tsk, int cpu) { }
120 #endif
121

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

Re: [RFC PATCH 1/7] sched/vtime: Move guest enter/exit vtime accounting to separate helpers

In reply to Sean Christopherson
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sean,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next linus/master v5.12-rc7 next-20210413] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
config: powerpc-randconfig-r021-20210413 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 9829f5e6b1bca9b61efc629770d28bb9014dec45)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install powerpc cross compiling tool for clang build
# apt-get install binutils-powerpc-linux-gnu
# https://github.com/0day-ci/linux/commit/c68e2a1489e453b217384eb985d04dd6784c3b53
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106 git checkout c68e2a1489e453b217384eb985d04dd6784c3b53
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=powerpc

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel' [-Werror,-Wimplicit-function-declaration]
           vtime_account_kernel(current);
           ^
>> include/linux/vtime.h:103:13: error: conflicting types for 'vtime_account_kernel'
   extern void vtime_account_kernel(struct task_struct *tsk);
               ^
   include/linux/vtime.h:25:2: note: previous implicit declaration is here
           vtime_account_kernel(current);
           ^

2 errors generated.
--
In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel' [-Werror,-Wimplicit-function-declaration]
           vtime_account_kernel(current);
           ^
>> include/linux/vtime.h:103:13: error: conflicting types for 'vtime_account_kernel'
   extern void vtime_account_kernel(struct task_struct *tsk);
               ^
   include/linux/vtime.h:25:2: note: previous implicit declaration is here
           vtime_account_kernel(current);
           ^

2 errors generated.
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
--
In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:

>> include/linux/vtime.h:25:2: error: implicit declaration of function 'vtime_account_kernel' [-Werror,-Wimplicit-function-declaration]
           vtime_account_kernel(current);
           ^
>> include/linux/vtime.h:103:13: error: conflicting types for 'vtime_account_kernel'
   extern void vtime_account_kernel(struct task_struct *tsk);
               ^
   include/linux/vtime.h:25:2: note: previous implicit declaration is here
           vtime_account_kernel(current);
           ^

2 errors generated.
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/vtime_account_kernel +25 include/linux/vtime.h

    22	
    23	static __always_inline void vtime_account_guest_enter(void)
    24	{
  > 25		vtime_account_kernel(current);
    26		current->flags |= PF_VCPU;
    27	}
    28	
    29	static __always_inline void vtime_account_guest_exit(void)
    30	{
    31	
    32	}
    33	
    34	#elif defined(CONFIG_VIRT_CPU_ACCOUNTING_GEN)
    35	
    36	/*
    37	 * Checks if vtime is enabled on some CPU. Cputime readers want to be careful
    38	 * in that case and compute the tickless cputime.
    39	 * For now vtime state is tied to context tracking. We might want to decouple
    40	 * those later if necessary.
    41	 */
    42	static inline bool vtime_accounting_enabled(void)
    43	{
    44		return context_tracking_enabled();
    45	}
    46	
    47	static inline bool vtime_accounting_enabled_cpu(int cpu)
    48	{
    49		return context_tracking_enabled_cpu(cpu);
    50	}
    51	
    52	static inline bool vtime_accounting_enabled_this_cpu(void)
    53	{
    54		return context_tracking_enabled_this_cpu();
    55	}
    56	
    57	extern void vtime_task_switch_generic(struct task_struct *prev);
    58	
    59	static inline void vtime_task_switch(struct task_struct *prev)
    60	{
    61		if (vtime_accounting_enabled_this_cpu())
    62			vtime_task_switch_generic(prev);
    63	}
    64	
    65	static __always_inline void vtime_account_guest_enter(void)
    66	{
    67		if (vtime_accounting_enabled_this_cpu())
    68			vtime_guest_enter(current);
    69		else
    70			current->flags |= PF_VCPU;
    71	}
    72	
    73	static __always_inline void vtime_account_guest_exit(void)
    74	{
    75		if (vtime_accounting_enabled_this_cpu())
    76			vtime_guest_exit(current);
    77		else
    78			current->flags &= ~PF_VCPU;
    79	}
    80	
    81	
    82	#else /* !CONFIG_VIRT_CPU_ACCOUNTING */
    83	
    84	static inline bool vtime_accounting_enabled_this_cpu(void) { return false; }
    85	static inline void vtime_task_switch(struct task_struct *prev) { }
    86	
    87	static __always_inline void vtime_account_guest_enter(void)
    88	{
    89		current->flags |= PF_VCPU;
    90	}
    91	
    92	static __always_inline void vtime_account_guest_exit(void)
    93	{
    94		current->flags &= ~PF_VCPU;
    95	}
    96	
    97	#endif
    98	
    99	/*
   100	 * Common vtime APIs
   101	 */
   102	#ifdef CONFIG_VIRT_CPU_ACCOUNTING
 > 103	extern void vtime_account_kernel(struct task_struct *tsk);

104 extern void vtime_account_idle(struct task_struct *tsk);
105 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
106

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#5 lore raw

RFC PATCH 2/7 context_tracking: Move guest enter/exit logic to standalone helpers

In reply to Sean Christopherson

Move guest enter/exit context tracking to standalone helpers, so that the existing wrappers can be moved under KVM.

No functional change intended.

---
 include/linux/context_tracking.h | 43 +++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 17 deletions(-)
include/linux/context_tracking.h +26 -17
index 58f9a7251d3b..89a1a5ccb2ab 100644
@@ -71,6 +71,30 @@ static inline void exception_exit(enum ctx_state prev_ctx)
 	}
 }
 
+static __always_inline void context_tracking_guest_enter_irqoff(void)
+{
+	if (context_tracking_enabled())
+		__context_tracking_enter(CONTEXT_GUEST);
+
+	/* KVM does not hold any references to rcu protected data when it
+	 * switches CPU into a guest mode. In fact switching to a guest mode
+	 * is very similar to exiting to userspace from rcu point of view. In
+	 * addition CPU may stay in a guest mode for quite a long time (up to
+	 * one time slice). Lets treat guest mode as quiescent state, just like
+	 * we do with user-mode execution.
+	 */
+	if (!context_tracking_enabled_this_cpu()) {
+		instrumentation_begin();
+		rcu_virt_note_context_switch(smp_processor_id());
+		instrumentation_end();
+	}
+}
+
+static __always_inline void context_tracking_guest_exit_irqoff(void)
+{
+	if (context_tracking_enabled())
+		__context_tracking_exit(CONTEXT_GUEST);
+}
 
 /**
  * ct_state() - return the current context tracking state if known
@@ -110,27 +134,12 @@ static __always_inline void guest_enter_irqoff(void)
 	vtime_account_guest_enter();
 	instrumentation_end();
 
-	if (context_tracking_enabled())
-		__context_tracking_enter(CONTEXT_GUEST);
-
-	/* KVM does not hold any references to rcu protected data when it
-	 * switches CPU into a guest mode. In fact switching to a guest mode
-	 * is very similar to exiting to userspace from rcu point of view. In
-	 * addition CPU may stay in a guest mode for quite a long time (up to
-	 * one time slice). Lets treat guest mode as quiescent state, just like
-	 * we do with user-mode execution.
-	 */
-	if (!context_tracking_enabled_this_cpu()) {
-		instrumentation_begin();
-		rcu_virt_note_context_switch(smp_processor_id());
-		instrumentation_end();
-	}
+	context_tracking_guest_enter_irqoff();
 }
 
 static __always_inline void guest_exit_irqoff(void)
 {
-	if (context_tracking_enabled())
-		__context_tracking_exit(CONTEXT_GUEST);
+	context_tracking_guest_exit_irqoff();
 
 	instrumentation_begin();
 	vtime_account_guest_exit();
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
author maintainer
#6 lore raw

RFC PATCH 3/7 context_tracking: Consolidate guest enter/exit wrappers

In reply to Sean Christopherson

Consolidate the guest enter/exit wrappers by providing stubs for the context tracking helpers as necessary. This will allow moving the wrappers under KVM without having to bleed too many #ifdefs into the soon-to-be KVM code.

No functional change intended.

---
 include/linux/context_tracking.h | 65 ++++++++++++++------------------
 1 file changed, 29 insertions(+), 36 deletions(-)
include/linux/context_tracking.h +29 -36
index 89a1a5ccb2ab..ded56aed539a 100644
@@ -76,18 +76,7 @@ static __always_inline void context_tracking_guest_enter_irqoff(void)
 	if (context_tracking_enabled())
 		__context_tracking_enter(CONTEXT_GUEST);
 
-	/* KVM does not hold any references to rcu protected data when it
-	 * switches CPU into a guest mode. In fact switching to a guest mode
-	 * is very similar to exiting to userspace from rcu point of view. In
-	 * addition CPU may stay in a guest mode for quite a long time (up to
-	 * one time slice). Lets treat guest mode as quiescent state, just like
-	 * we do with user-mode execution.
-	 */
-	if (!context_tracking_enabled_this_cpu()) {
-		instrumentation_begin();
-		rcu_virt_note_context_switch(smp_processor_id());
-		instrumentation_end();
-	}
+	return context_tracking_enabled_this_cpu();
 }
 
 static __always_inline void context_tracking_guest_exit_irqoff(void)
@@ -116,6 +105,17 @@ static inline void user_exit_irqoff(void) { }
 static inline enum ctx_state exception_enter(void) { return 0; }
 static inline void exception_exit(enum ctx_state prev_ctx) { }
 static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
+
+static __always_inline bool context_tracking_guest_enter_irqoff(void)
+{
+	return false;
+}
+
+static __always_inline void context_tracking_guest_exit_irqoff(void)
+{
+
+}
+
 #endif /* !CONFIG_CONTEXT_TRACKING */
 
 #define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond))
@@ -126,48 +126,41 @@ extern void context_tracking_init(void);
 static inline void context_tracking_init(void) { }
 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
 
-#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
 /* must be called with irqs disabled */
 static __always_inline void guest_enter_irqoff(void)
 {
+	/*
+	 * This is running in ioctl context so its safe to assume that it's the
+	 * stime pending cputime to flush.
+	 */
 	instrumentation_begin();
 	vtime_account_guest_enter();
 	instrumentation_end();
 
-	context_tracking_guest_enter_irqoff();
+	/*
+	 * KVM does not hold any references to rcu protected data when it
+	 * switches CPU into a guest mode. In fact switching to a guest mode
+	 * is very similar to exiting to userspace from rcu point of view. In
+	 * addition CPU may stay in a guest mode for quite a long time (up to
+	 * one time slice). Lets treat guest mode as quiescent state, just like
+	 * we do with user-mode execution.
+	 */
+	if (!context_tracking_guest_enter_irqoff()) {
+		instrumentation_begin();
+		rcu_virt_note_context_switch(smp_processor_id());
+		instrumentation_end();
+	}
 }
 
 static __always_inline void guest_exit_irqoff(void)
 {
 	context_tracking_guest_exit_irqoff();
 
-	instrumentation_begin();
-	vtime_account_guest_exit();
-	instrumentation_end();
-}
-
-#else
-static __always_inline void guest_enter_irqoff(void)
-{
-	/*
-	 * This is running in ioctl context so its safe
-	 * to assume that it's the stime pending cputime
-	 * to flush.
-	 */
-	instrumentation_begin();
-	vtime_account_guest_enter();
-	rcu_virt_note_context_switch(smp_processor_id());
-	instrumentation_end();
-}
-
-static __always_inline void guest_exit_irqoff(void)
-{
 	instrumentation_begin();
 	/* Flush the guest cputime we spent on the guest */
 	vtime_account_guest_exit();
 	instrumentation_end();
 }
-#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
 
 static inline void guest_exit(void)
 {
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#7 lore raw

Re: [RFC PATCH 3/7] context_tracking: Consolidate guest enter/exit wrappers

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sean,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next linus/master v5.12-rc7 next-20210413] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
config: powerpc64-randconfig-r023-20210413 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 9829f5e6b1bca9b61efc629770d28bb9014dec45)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install powerpc64 cross compiling tool for clang build
# apt-get install binutils-powerpc64-linux-gnu
# https://github.com/0day-ci/linux/commit/3f339890e27d8d4673de753b889fc5025413330d
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106 git checkout 3f339890e27d8d4673de753b889fc5025413330d
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=powerpc64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:
include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror,-Wimplicit-function-declaration]

                   vtime_guest_enter(current);
                   ^
   include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_exit(current);
                   ^
   include/linux/vtime.h:111:13: error: conflicting types for 'vtime_guest_enter'
   extern void vtime_guest_enter(struct task_struct *tsk);
               ^
   include/linux/vtime.h:68:3: note: previous implicit declaration is here
                   vtime_guest_enter(current);
                   ^
   include/linux/vtime.h:112:13: error: conflicting types for 'vtime_guest_exit'
   extern void vtime_guest_exit(struct task_struct *tsk);
               ^
   include/linux/vtime.h:76:3: note: previous implicit declaration is here
                   vtime_guest_exit(current);
                   ^
   In file included from arch/powerpc/kernel/asm-offsets.c:38:
   In file included from arch/powerpc/include/asm/dbell.h:17:
   In file included from arch/powerpc/include/asm/kvm_ppc.h:19:
   In file included from include/linux/kvm_host.h:25:
>> include/linux/context_tracking.h:79:2: error: void function 'context_tracking_guest_enter_irqoff' should not return a value [-Wreturn-type]
           return context_tracking_enabled_this_cpu();
           ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/context_tracking.h:148:6: error: invalid argument type 'void' to unary expression
           if (!context_tracking_guest_enter_irqoff()) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6 errors generated.
--
In file included from arch/powerpc/kernel/asm-offsets.c:23:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:9:
include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror,-Wimplicit-function-declaration]

                   vtime_guest_enter(current);
                   ^
   include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror,-Wimplicit-function-declaration]
                   vtime_guest_exit(current);
                   ^
   include/linux/vtime.h:111:13: error: conflicting types for 'vtime_guest_enter'
   extern void vtime_guest_enter(struct task_struct *tsk);
               ^
   include/linux/vtime.h:68:3: note: previous implicit declaration is here
                   vtime_guest_enter(current);
                   ^
   include/linux/vtime.h:112:13: error: conflicting types for 'vtime_guest_exit'
   extern void vtime_guest_exit(struct task_struct *tsk);
               ^
   include/linux/vtime.h:76:3: note: previous implicit declaration is here
                   vtime_guest_exit(current);
                   ^
   In file included from arch/powerpc/kernel/asm-offsets.c:38:
   In file included from arch/powerpc/include/asm/dbell.h:17:
   In file included from arch/powerpc/include/asm/kvm_ppc.h:19:
   In file included from include/linux/kvm_host.h:25:
>> include/linux/context_tracking.h:79:2: error: void function 'context_tracking_guest_enter_irqoff' should not return a value [-Wreturn-type]
           return context_tracking_enabled_this_cpu();
           ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/context_tracking.h:148:6: error: invalid argument type 'void' to unary expression
           if (!context_tracking_guest_enter_irqoff()) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6 errors generated.
make[2]: *** [scripts/Makefile.build:116: arch/powerpc/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/context_tracking_guest_enter_irqoff +79 include/linux/context_tracking.h

    73	
    74	static __always_inline void context_tracking_guest_enter_irqoff(void)
    75	{
    76		if (context_tracking_enabled())
    77			__context_tracking_enter(CONTEXT_GUEST);
    78	
  > 79		return context_tracking_enabled_this_cpu();
    80	}
    81	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#8 lore raw

RFC PATCH 4/7 context_tracking: KVM: Move guest enter/exit wrappers to KVM's domain

In reply to Sean Christopherson

Move the guest enter/exit wrappers to kvm_host.h so that KVM can manage its context tracking vs. vtime accounting without bleeding too many KVM details into the context tracking code.

No functional change intended.

---
 include/linux/context_tracking.h | 45 --------------------------------
 include/linux/kvm_host.h         | 45 ++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 45 deletions(-)
include/linux/context_tracking.h +0 -45
index ded56aed539a..2f4538380a8d 100644
@@ -126,49 +126,4 @@ extern void context_tracking_init(void);
 static inline void context_tracking_init(void) { }
 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
 
-/* must be called with irqs disabled */
-static __always_inline void guest_enter_irqoff(void)
-{
-	/*
-	 * This is running in ioctl context so its safe to assume that it's the
-	 * stime pending cputime to flush.
-	 */
-	instrumentation_begin();
-	vtime_account_guest_enter();
-	instrumentation_end();
-
-	/*
-	 * KVM does not hold any references to rcu protected data when it
-	 * switches CPU into a guest mode. In fact switching to a guest mode
-	 * is very similar to exiting to userspace from rcu point of view. In
-	 * addition CPU may stay in a guest mode for quite a long time (up to
-	 * one time slice). Lets treat guest mode as quiescent state, just like
-	 * we do with user-mode execution.
-	 */
-	if (!context_tracking_guest_enter_irqoff()) {
-		instrumentation_begin();
-		rcu_virt_note_context_switch(smp_processor_id());
-		instrumentation_end();
-	}
-}
-
-static __always_inline void guest_exit_irqoff(void)
-{
-	context_tracking_guest_exit_irqoff();
-
-	instrumentation_begin();
-	/* Flush the guest cputime we spent on the guest */
-	vtime_account_guest_exit();
-	instrumentation_end();
-}
-
-static inline void guest_exit(void)
-{
-	unsigned long flags;
-
-	local_irq_save(flags);
-	guest_exit_irqoff();
-	local_irq_restore(flags);
-}
-
 #endif
include/linux/kvm_host.h +45 -0

51 lines not on this page · raw message

Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#9 lore raw

Re: [RFC PATCH 4/7] context_tracking: KVM: Move guest enter/exit wrappers to KVM's domain

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Sean,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next linus/master v5.12-rc7 next-20210413] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
config: arm64-randconfig-r033-20210413 (attached as .config)
compiler: aarch64-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/812e089ce79e0e9d5a7b8063fbdb4496174f30f5
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-Fix-tick-based-vtime-accounting-on-x86/20210414-023106 git checkout 812e089ce79e0e9d5a7b8063fbdb4496174f30f5
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from include/linux/hardirq.h:9,

                    from include/linux/kvm_host.h:7,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/vtime.h: In function 'vtime_account_guest_enter':
   include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror=implicit-function-declaration]
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h: In function 'vtime_account_guest_exit':
   include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror=implicit-function-declaration]
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   include/linux/vtime.h: At top level:
   include/linux/vtime.h:111:13: warning: conflicting types for 'vtime_guest_enter'
     111 | extern void vtime_guest_enter(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:68:3: note: previous implicit declaration of 'vtime_guest_enter' was here
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:112:13: warning: conflicting types for 'vtime_guest_exit'
     112 | extern void vtime_guest_exit(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~
   include/linux/vtime.h:76:3: note: previous implicit declaration of 'vtime_guest_exit' was here
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   In file included from include/linux/kvm_host.h:25,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/context_tracking.h: In function 'context_tracking_guest_enter_irqoff':
   include/linux/context_tracking.h:79:9: error: 'return' with a value, in function returning void [-Werror=return-type]
      79 |  return context_tracking_enabled_this_cpu();
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/context_tracking.h:74:29: note: declared here
      74 | static __always_inline void context_tracking_guest_enter_irqoff(void)
         |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/kvm_host.h: In function 'guest_enter_irqoff':
>> include/linux/kvm_host.h:354:6: error: invalid use of void expression
     354 |  if (!context_tracking_guest_enter_irqoff()) {
         |      ^
   cc1: some warnings being treated as errors
--
   In file included from include/linux/hardirq.h:9,
                    from include/linux/kvm_host.h:7,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/vtime.h: In function 'vtime_account_guest_enter':
   include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror=implicit-function-declaration]
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h: In function 'vtime_account_guest_exit':
   include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror=implicit-function-declaration]
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   include/linux/vtime.h: At top level:
   include/linux/vtime.h:111:13: warning: conflicting types for 'vtime_guest_enter'
     111 | extern void vtime_guest_enter(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:68:3: note: previous implicit declaration of 'vtime_guest_enter' was here
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:112:13: warning: conflicting types for 'vtime_guest_exit'
     112 | extern void vtime_guest_exit(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~
   include/linux/vtime.h:76:3: note: previous implicit declaration of 'vtime_guest_exit' was here
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   In file included from include/linux/kvm_host.h:25,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/context_tracking.h: In function 'context_tracking_guest_enter_irqoff':
   include/linux/context_tracking.h:79:9: error: 'return' with a value, in function returning void [-Werror=return-type]
      79 |  return context_tracking_enabled_this_cpu();
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/context_tracking.h:74:29: note: declared here
      74 | static __always_inline void context_tracking_guest_enter_irqoff(void)
         |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/kvm_host.h: In function 'guest_enter_irqoff':
>> include/linux/kvm_host.h:354:6: error: invalid use of void expression
     354 |  if (!context_tracking_guest_enter_irqoff()) {
         |      ^

cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:116: arch/arm64/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
--
In file included from include/linux/hardirq.h:9,

                    from include/linux/kvm_host.h:7,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/vtime.h: In function 'vtime_account_guest_enter':
   include/linux/vtime.h:68:3: error: implicit declaration of function 'vtime_guest_enter' [-Werror=implicit-function-declaration]
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h: In function 'vtime_account_guest_exit':
   include/linux/vtime.h:76:3: error: implicit declaration of function 'vtime_guest_exit' [-Werror=implicit-function-declaration]
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   include/linux/vtime.h: At top level:
   include/linux/vtime.h:111:13: warning: conflicting types for 'vtime_guest_enter'
     111 | extern void vtime_guest_enter(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:68:3: note: previous implicit declaration of 'vtime_guest_enter' was here
      68 |   vtime_guest_enter(current);
         |   ^~~~~~~~~~~~~~~~~
   include/linux/vtime.h:112:13: warning: conflicting types for 'vtime_guest_exit'
     112 | extern void vtime_guest_exit(struct task_struct *tsk);
         |             ^~~~~~~~~~~~~~~~
   include/linux/vtime.h:76:3: note: previous implicit declaration of 'vtime_guest_exit' was here
      76 |   vtime_guest_exit(current);
         |   ^~~~~~~~~~~~~~~~
   In file included from include/linux/kvm_host.h:25,
                    from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/context_tracking.h: In function 'context_tracking_guest_enter_irqoff':
   include/linux/context_tracking.h:79:9: error: 'return' with a value, in function returning void [-Werror=return-type]
      79 |  return context_tracking_enabled_this_cpu();
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/context_tracking.h:74:29: note: declared here
      74 | static __always_inline void context_tracking_guest_enter_irqoff(void)
         |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from arch/arm64/kernel/asm-offsets.c:14:
   include/linux/kvm_host.h: In function 'guest_enter_irqoff':
>> include/linux/kvm_host.h:354:6: error: invalid use of void expression
     354 |  if (!context_tracking_guest_enter_irqoff()) {
         |      ^

cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:116: arch/arm64/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1235: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:215: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +354 include/linux/kvm_host.h

334
335 /* must be called with irqs disabled */
336 static __always_inline void guest_enter_irqoff(void)
337 {
338 /*
339 * This is running in ioctl context so its safe to assume that it's the
340 * stime pending cputime to flush.
341 */
342 instrumentation_begin();
343 vtime_account_guest_enter();
344 instrumentation_end();
345
346 /*
347 * KVM does not hold any references to rcu protected data when it
348 * switches CPU into a guest mode. In fact switching to a guest mode
349 * is very similar to exiting to userspace from rcu point of view. In
350 * addition CPU may stay in a guest mode for quite a long time (up to
351 * one time slice). Lets treat guest mode as quiescent state, just like
352 * we do with user-mode execution.
353 */

 > 354		if (!context_tracking_guest_enter_irqoff()) {

355 instrumentation_begin();
356 rcu_virt_note_context_switch(smp_processor_id());
357 instrumentation_end();
358 }
359 }
360

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#10 lore raw

RFC PATCH 5/7 KVM: Move vtime accounting of guest exit to separate helper

In reply to Sean Christopherson

Provide a standalone helper for guest exit vtime accounting so that x86 can defer tick-based accounting until the appropriate time, while still updating context tracking immediately after VM-Exit.

No functional change intended.

---
 include/linux/kvm_host.h | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
include/linux/kvm_host.h +8 -3
index 444d5f0225cb..20604bfae5a8 100644
@@ -358,16 +358,21 @@ static __always_inline void guest_enter_irqoff(void)
 	}
 }
 
-static __always_inline void guest_exit_irqoff(void)
+static __always_inline void kvm_vtime_account_guest_exit(void)
 {
-	context_tracking_guest_exit_irqoff();
-
 	instrumentation_begin();
 	/* Flush the guest cputime we spent on the guest */
 	vtime_account_guest_exit();
 	instrumentation_end();
 }
 
+static __always_inline void guest_exit_irqoff(void)
+{
+	context_tracking_guest_exit_irqoff();
+
+	kvm_vtime_account_guest_exit();
+}
+
 static inline void guest_exit(void)
 {
 	unsigned long flags;
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
author maintainer
#11 lore raw

RFC PATCH 6/7 KVM: x86: Consolidate guest enter/exit logic to common helpers

In reply to Sean Christopherson

Move the enter/exit logic in {svm,vmx}_vcpu_enter_exit() to common helpers. In addition to deduplicating code, this will allow tweaking the vtime accounting in the VM-Exit path without splitting logic across x86, VMX, and SVM.

No functional change intended.

---
 arch/x86/kvm/svm/svm.c | 39 ++----------------------------------
 arch/x86/kvm/vmx/vmx.c | 39 ++----------------------------------
 arch/x86/kvm/x86.h     | 45 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 74 deletions(-)
arch/x86/kvm/svm/svm.c +2 -37
index 48b396f33bee..0677595d07e5 100644
@@ -3713,25 +3713,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
-	/*
-	 * VMENTER enables interrupts (host state), but the kernel state is
-	 * interrupts disabled when this is invoked. Also tell RCU about
-	 * it. This is the same logic as for exit_to_user_mode().
-	 *
-	 * This ensures that e.g. latency analysis on the host observes
-	 * guest mode as interrupt enabled.
-	 *
-	 * guest_enter_irqoff() informs context tracking about the
-	 * transition to guest mode and if enabled adjusts RCU state
-	 * accordingly.
-	 */
-	instrumentation_begin();
-	trace_hardirqs_on_prepare();
-	lockdep_hardirqs_on_prepare(CALLER_ADDR0);
-	instrumentation_end();
-
-	guest_enter_irqoff();
-	lockdep_hardirqs_on(CALLER_ADDR0);
+	kvm_guest_enter_irqoff();
 
 	if (sev_es_guest(vcpu->kvm)) {
 		__svm_sev_es_vcpu_run(svm->vmcb_pa);
@@ -3745,24 +3727,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
 		vmload(__sme_page_pa(sd->save_area));
 	}
 
-	/*
-	 * VMEXIT disables interrupts (host state), but tracing and lockdep
-	 * have them in state 'on' as recorded before entering guest mode.
-	 * Same as enter_from_user_mode().
-	 *
-	 * guest_exit_irqoff() restores host context and reinstates RCU if
-	 * enabled and required.
-	 *
-	 * This needs to be done before the below as native_read_msr()
-	 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
-	 * into world and some more.
-	 */
-	lockdep_hardirqs_off(CALLER_ADDR0);
-	guest_exit_irqoff();
-
-	instrumentation_begin();
-	trace_hardirqs_off_finish();
-	instrumentation_end();
+	kvm_guest_exit_irqoff();
 }
 
 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +2 -37
index c05e6e2854b5..19b0e25bf598 100644
@@ -6600,25 +6600,7 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
 static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
 					struct vcpu_vmx *vmx)
 {
-	/*
-	 * VMENTER enables interrupts (host state), but the kernel state is
-	 * interrupts disabled when this is invoked. Also tell RCU about
-	 * it. This is the same logic as for exit_to_user_mode().
-	 *
-	 * This ensures that e.g. latency analysis on the host observes
-	 * guest mode as interrupt enabled.
-	 *
-	 * guest_enter_irqoff() informs context tracking about the
-	 * transition to guest mode and if enabled adjusts RCU state
-	 * accordingly.
-	 */
-	instrumentation_begin();
-	trace_hardirqs_on_prepare();
-	lockdep_hardirqs_on_prepare(CALLER_ADDR0);
-	instrumentation_end();
-
-	guest_enter_irqoff();
-	lockdep_hardirqs_on(CALLER_ADDR0);
+	kvm_guest_enter_irqoff();
 
 	/* L1D Flush includes CPU buffer clear to mitigate MDS */
 	if (static_branch_unlikely(&vmx_l1d_should_flush))
@@ -6634,24 +6616,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
 
 	vcpu->arch.cr2 = native_read_cr2();
 
-	/*
-	 * VMEXIT disables interrupts (host state), but tracing and lockdep
-	 * have them in state 'on' as recorded before entering guest mode.
-	 * Same as enter_from_user_mode().
-	 *
-	 * guest_exit_irqoff() restores host context and reinstates RCU if
-	 * enabled and required.
-	 *
-	 * This needs to be done before the below as native_read_msr()
-	 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
-	 * into world and some more.
-	 */
-	lockdep_hardirqs_off(CALLER_ADDR0);
-	guest_exit_irqoff();
-
-	instrumentation_begin();
-	trace_hardirqs_off_finish();
-	instrumentation_end();
+	kvm_guest_exit_irqoff();
 }
 
 static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.h +45 -0

51 lines not on this page · raw message

Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
author maintainer
#12 lore raw

RFC PATCH 7/7 KVM: x86: Defer tick-based accounting 'til after IRQ handling

In reply to Sean Christopherson

When using tick-based accounting, defer the call to account guest time until after servicing any IRQ(s) that happened in the guest (or immediately after VM-Exit). When using tick-based accounting, time is accounted to the guest when PF_VCPU is set when the tick IRQ handler runs. The current approach of unconditionally accounting time in kvm_guest_exit_irqoff() prevents IRQs that occur in the guest from ever being processed with PF_VCPU set, since PF_VCPU ends up being set only during the relatively short VM-Enter sequence, which runs entirely with IRQs disabled.

Fixes 87fa7f3e98a131 ("x86/kvm: Move context tracking where it belongs")
Cc Thomas Gleixner <[email protected]>
Cc Michael Tokarev <[email protected]>
---
 arch/x86/kvm/x86.c | 8 ++++++++
 arch/x86/kvm/x86.h | 9 ++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +8 -0
index 16fb39503296..096bbf50b7a9 100644
@@ -9230,6 +9230,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	local_irq_disable();
 	kvm_after_interrupt(vcpu);
 
+	/*
+	 * When using tick-based account, wait until after servicing IRQs to
+	 * account guest time so that any ticks that occurred while running the
+	 * guest are properly accounted to the guest.
+	 */
+	if (!IS_ENABLED(CONFIG_VIRT_CPU_ACCOUNTING_GEN))
+		kvm_vtime_account_guest_exit();
+
 	if (lapic_in_kernel(vcpu)) {
 		s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
 		if (delta != S64_MIN) {
arch/x86/kvm/x86.h +6 -3
index 74ef92f47db8..039a7d585925 100644
@@ -38,15 +38,18 @@ static __always_inline void kvm_guest_exit_irqoff(void)
 	 * have them in state 'on' as recorded before entering guest mode.
 	 * Same as enter_from_user_mode().
 	 *
-	 * guest_exit_irqoff() restores host context and reinstates RCU if
-	 * enabled and required.
+	 * context_tracking_guest_exit_irqoff() restores host context and
+	 * reinstates RCU if enabled and required.
 	 *
 	 * This needs to be done before the below as native_read_msr()
 	 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
 	 * into world and some more.
 	 */
 	lockdep_hardirqs_off(CALLER_ADDR0);
-	guest_exit_irqoff();
+	context_tracking_guest_exit_irqoff();
+
+	if (IS_ENABLED(CONFIG_VIRT_CPU_ACCOUNTING_GEN))
+		kvm_vtime_account_guest_exit();
 
 	instrumentation_begin();
 	trace_hardirqs_off_finish();
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#13 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

This is an alternative to Wanpeng's series[*] to fix tick-based accounting on x86. The approach for fixing the bug is identical: defer accounting until after tick IRQs are handled. The difference is purely in how the context tracking and vtime code is refactored in order to give KVM the hooks it needs to fix the x86 bug.

x86 compile tested only, hence the RFC. If folks like the direction and there are no unsolvable issues, I'll cross-compile, properly test on x86, and post an "official" series.

I like the final outcome of this, but we really want a small set of patches first which actually fix the bug and is easy to backport and then the larger consolidation on top.

Can you sort that out with Wanpeng please?

Thanks,

tglx

author maintainer
#14 lore raw
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Sean Christopherson

This is an alternative to Wanpeng's series[*] to fix tick-based accounting on x86. The approach for fixing the bug is identical: defer accounting until after tick IRQs are handled. The difference is purely in how the context tracking and vtime code is refactored in order to give KVM the hooks it needs to fix the x86 bug.

x86 compile tested only, hence the RFC. If folks like the direction and there are no unsolvable issues, I'll cross-compile, properly test on x86, and post an "official" series.

Quoted 5 lines

I like the final outcome of this, but we really want a small set of patches first which actually fix the bug and is easy to backport and then the larger consolidation on top.

Can you sort that out with Wanpeng please?

Will do.