powerpc/kvm: Save Timebase Offset to fix sched_clock() while running guest code.
Subsystems (2)
PATCH 1/1 powerpc/kvm: Save Timebase Offset to fix sched_clock() while running guest code.
Before guest entry, TBU40 register is changed to reflect guest timebase. After exitting guest, the register is reverted to it's original value.
If one tries to get the timestamp from host between those changes, it will present an incorrect value.
An example would be trying to add a tracepoint in kvmppc_guest_entry_inject_int(), which depending on last tracepoint acquired could actually cause the host to crash.
Save the Timebase Offset to PACA and use it on sched_clock() to always get the correct timestamp.
--- arch/powerpc/include/asm/kvm_book3s_asm.h | 1 + arch/powerpc/kernel/asm-offsets.c | 1 + arch/powerpc/kernel/time.c | 3 ++- arch/powerpc/kvm/book3s_hv.c | 2 ++ arch/powerpc/kvm/book3s_hv_rmhandlers.S | 2 ++ 5 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_book3s_asm.h +1 -0
@@ -131,6 +131,7 @@ struct kvmppc_host_state {u64 cfar;u64 ppr;u64 host_fscr; +u64 tb_offset;/* Timebase offset: keeps correct timebase while on guest */ #endif };
arch/powerpc/kernel/asm-offsets.c +1 -0
@@ -706,6 +706,7 @@ int main(void)HSTATE_FIELD (HSTATE_CFAR , cfar);HSTATE_FIELD (HSTATE_PPR , ppr);HSTATE_FIELD (HSTATE_HOST_FSCR , host_fscr); +HSTATE_FIELD (HSTATE_TB_OFFSET , tb_offset);#endif /* CONFIG_PPC_BOOK3S_64 */ #else /* CONFIG_PPC_BOOK3S */
arch/powerpc/kernel/time.c +2 -1
@@ -699,7 +699,8 @@ EXPORT_SYMBOL_GPL(tb_to_ns);*/ notrace unsigned long long sched_clock (void ) { -return mulhdu (get_tb () - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; +return mulhdu (get_tb () - boot_tb - local_paca->kvm_hstate.tb_offset, tb_to_ns_scale) + << tb_to_ns_shift; }
arch/powerpc/kvm/book3s_hv.c +2 -0
@@ -3491,6 +3491,7 @@ static int kvmhv_load_hv_regs_and_go(struct kvm_vcpu *vcpu, u64 time_limit,if ((tb &0xffffff ) < (new_tb &0xffffff ))mtspr (SPRN_TBU40 , new_tb +0x1000000 ); vc->tb_offset_applied = vc->tb_offset; + local_paca->kvm_hstate.tb_offset = vc->tb_offset; }if (vc->pcr)
@@ -3594,6 +3595,7 @@ static int kvmhv_load_hv_regs_and_go(struct kvm_vcpu *vcpu, u64 time_limit,if ((tb &0xffffff ) < (new_tb &0xffffff ))mtspr (SPRN_TBU40 , new_tb +0x1000000 ); vc->tb_offset_applied =0 ; + local_paca->kvm_hstate.tb_offset =0 ; }mtspr (SPRN_HDEC ,0x7fffffff );
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -0
@@ -632,6 +632,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300) cmpdi r8,0 beq 37f std r8, VCORE_TB_OFFSET_APPL(r5) + std r8, HSTATE_TB_OFFSET(r13) mftb r6 /* current host timebase */ add r8,r8,r6 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
@@ -1907,6 +1908,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) beq 17f li r0, 0 std r0, VCORE_TB_OFFSET_APPL(r5) + std r0, HSTATE_TB_OFFSET(r13) mftb r6 /* current guest timebase */ subf r8,r8,r6 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
Signature · 1 line
2.29.2
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Leonardo,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.11-rc6 next-20210125]
[cannot apply to paulus-powerpc/kvm-ppc-next]
[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/Leonardo-Bras/powerpc-kvm-Save-Timebase-Offset-to-fix-sched_clock-while-running-guest-code/20210205-111818 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc64-randconfig-s031-20210204 (attached as .config) compiler: powerpc-linux-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-215-g0fb77bb6-dirty
# https://github.com/0day-ci/linux/commit/bdbc4170501017eddf182ed24be15a76106a613e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Leonardo-Bras/powerpc-kvm-Save-Timebase-Offset-to-fix-sched_clock-while-running-guest-code/20210205-111818 git checkout bdbc4170501017eddf182ed24be15a76106a613e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpc64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/powerpc/kernel/time.c: In function 'sched_clock':
>> arch/powerpc/kernel/time.c:703:37: error: 'local_paca' undeclared (first use in this function); did you mean 'local_lock'?
703 | return mulhdu(get_tb() - boot_tb - local_paca->kvm_hstate.tb_offset, tb_to_ns_scale)
| ^~~~~~~~~~
| local_lock
arch/powerpc/kernel/time.c:703:37: note: each undeclared identifier is reported only once for each function it appears in
arch/powerpc/kernel/time.c:705:1: error: control reaches end of non-void function [-Werror=return-type]
705 | }
| ^cc1: all warnings being treated as errors
vim +703 arch/powerpc/kernel/time.c
693
694 /*
695 * Scheduler clock - returns current time in nanosec units.
696 *
697 * Note: mulhdu(a, b) (multiply high double unsigned) returns
698 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
699 * are 64-bit unsigned numbers.
700 */
701 notrace unsigned long long sched_clock(void)
702 {
> 703 return mulhdu(get_tb() - boot_tb - local_paca->kvm_hstate.tb_offset, tb_to_ns_scale)
704 << tb_to_ns_shift;
705 }
706
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Leonardo,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.11-rc6 next-20210125]
[cannot apply to paulus-powerpc/kvm-ppc-next]
[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/Leonardo-Bras/powerpc-kvm-Save-Timebase-Offset-to-fix-sched_clock-while-running-guest-code/20210205-111818 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-ps3_defconfig (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/bdbc4170501017eddf182ed24be15a76106a613e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Leonardo-Bras/powerpc-kvm-Save-Timebase-Offset-to-fix-sched_clock-while-running-guest-code/20210205-111818 git checkout bdbc4170501017eddf182ed24be15a76106a613e
# 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 errors (new ones prefixed by >>):
In file included from arch/powerpc/include/asm/delay.h:7,
from arch/powerpc/include/asm/io.h:33,
from include/linux/io.h:13,
from include/linux/irq.h:20,
from arch/powerpc/include/asm/hardirq.h:6,
from include/linux/hardirq.h:10,
from include/linux/interrupt.h:11,
from arch/powerpc/kernel/time.c:38:
arch/powerpc/kernel/time.c: In function 'sched_clock':
>> arch/powerpc/kernel/time.c:703:47: error: 'struct paca_struct' has no member named 'kvm_hstate'
703 | return mulhdu(get_tb() - boot_tb - local_paca->kvm_hstate.tb_offset, tb_to_ns_scale)
| ^~
arch/powerpc/include/asm/time.h:89:61: note: in definition of macro 'mulhdu'
89 | ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
| ^vim +703 arch/powerpc/kernel/time.c
693
694 /*
695 * Scheduler clock - returns current time in nanosec units.
696 *
697 * Note: mulhdu(a, b) (multiply high double unsigned) returns
698 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
699 * are 64-bit unsigned numbers.
700 */
701 notrace unsigned long long sched_clock(void)
702 {
> 703 return mulhdu(get_tb() - boot_tb - local_paca->kvm_hstate.tb_offset, tb_to_ns_scale)
704 << tb_to_ns_shift;
705 }
706
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]