Re: [RESEND PATCH 2/2] KVM: X86: Expose bus lock debug exception to guest

2021-01-27 Thread Paolo Bonzini

On 27/01/21 01:57, Chenyi Qiang wrote:


What is the status of the patch to introduce 
X86_FEATURE_BUS_LOCK_DETECT (I saw 
https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2389369.html)? 



Paolo


Fenghua sent the v4 patch and pinged x86 maintainers, but still no 
feedback.

https://lore.kernel.org/lkml/ya8bkmyjshkwm...@otcwcpicx3.sc.intel.com/


Ok, please include it when you repost.  Thanks!

Paolo



Re: [RESEND PATCH 2/2] KVM: X86: Expose bus lock debug exception to guest

2021-01-26 Thread Chenyi Qiang




On 1/27/2021 12:33 AM, Paolo Bonzini wrote:

On 08/01/21 19:16, kernel test robot wrote:

Hi Chenyi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v5.11-rc2 next-20210108]
[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]


What is the status of the patch to introduce X86_FEATURE_BUS_LOCK_DETECT 
(I saw 
https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2389369.html)?


Paolo


Fenghua sent the v4 patch and pinged x86 maintainers, but still no feedback.
https://lore.kernel.org/lkml/ya8bkmyjshkwm...@otcwcpicx3.sc.intel.com/







Re: [RESEND PATCH 2/2] KVM: X86: Expose bus lock debug exception to guest

2021-01-26 Thread Paolo Bonzini

On 08/01/21 19:16, kernel test robot wrote:

Hi Chenyi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v5.11-rc2 next-20210108]
[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]


What is the status of the patch to introduce X86_FEATURE_BUS_LOCK_DETECT 
(I saw 
https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2389369.html)?


Paolo



Re: [RESEND PATCH 2/2] KVM: X86: Expose bus lock debug exception to guest

2021-01-08 Thread kernel test robot
Hi Chenyi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v5.11-rc2 next-20210108]
[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/Chenyi-Qiang/Add-KVM-support-for-bus-lock-debug-exception/20210108-144848
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: i386-randconfig-r015-20210108 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
# 
https://github.com/0day-ci/linux/commit/fd0365cd4650a48a76379269b7cebd40ee38e52c
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review 
Chenyi-Qiang/Add-KVM-support-for-bus-lock-debug-exception/20210108-144848
git checkout fd0365cd4650a48a76379269b7cebd40ee38e52c
# save the attached .config to linux build tree
make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot 

All errors (new ones prefixed by >>):

   In file included from arch/x86/kvm/cpuid.c:21:
   arch/x86/kvm/cpuid.c: In function 'kvm_set_cpu_caps':
>> arch/x86/kvm/cpuid.h:98:42: error: 'X86_FEATURE_BUS_LOCK_DETECT' undeclared 
>> (first use in this function); did you mean 'X86_FEATURE_SPLIT_LOCK_DETECT'?
  98 | #define feature_bit(name)  __feature_bit(X86_FEATURE_##name)
 |  ^~~~
   arch/x86/kvm/cpuid.c:55:11: note: in expansion of macro 'feature_bit'
  55 | #define F feature_bit
 |   ^~~
   arch/x86/kvm/cpuid.c:406:3: note: in expansion of macro 'F'
 406 |   F(BUS_LOCK_DETECT)
 |   ^
   arch/x86/kvm/cpuid.h:98:42: note: each undeclared identifier is reported 
only once for each function it appears in
  98 | #define feature_bit(name)  __feature_bit(X86_FEATURE_##name)
 |  ^~~~
   arch/x86/kvm/cpuid.c:55:11: note: in expansion of macro 'feature_bit'
  55 | #define F feature_bit
 |   ^~~
   arch/x86/kvm/cpuid.c:406:3: note: in expansion of macro 'F'
 406 |   F(BUS_LOCK_DETECT)
 |   ^
--
   In file included from include/linux/export.h:43,
from include/linux/linkage.h:7,
from include/linux/fs.h:5,
from include/linux/highmem.h:5,
from arch/x86/kvm/vmx/vmx.c:16:
   arch/x86/kvm/vmx/vmx.c: In function 'vmx_set_msr':
>> arch/x86/kvm/vmx/vmx.c:151:20: error: 'DEBUGCTLMSR_BUS_LOCK_DETECT' 
>> undeclared (first use in this function)
 151 |  DEBUGCTLMSR_BTF | DEBUGCTLMSR_BUS_LOCK_DETECT)
 |^~~
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
  58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : 
__trace_if_value(cond))
 |^~~~
   arch/x86/kvm/vmx/vmx.c:2011:3: note: in expansion of macro 'if'
2011 |   if (data & ~MSR_VMX_SUPPORTED_DEBUGCTL)
 |   ^~
   arch/x86/kvm/vmx/vmx.c:2011:15: note: in expansion of macro 
'MSR_VMX_SUPPORTED_DEBUGCTL'
2011 |   if (data & ~MSR_VMX_SUPPORTED_DEBUGCTL)
 |   ^~
   arch/x86/kvm/vmx/vmx.c:151:20: note: each undeclared identifier is reported 
only once for each function it appears in
 151 |  DEBUGCTLMSR_BTF | DEBUGCTLMSR_BUS_LOCK_DETECT)
 |^~~
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
  58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : 
__trace_if_value(cond))
 |^~~~
   arch/x86/kvm/vmx/vmx.c:2011:3: note: in expansion of macro 'if'
2011 |   if (data & ~MSR_VMX_SUPPORTED_DEBUGCTL)
 |   ^~
   arch/x86/kvm/vmx/vmx.c:2011:15: note: in expansion of macro 
'MSR_VMX_SUPPORTED_DEBUGCTL'
2011 |   if (data & ~MSR_VMX_SUPPORTED_DEBUGCTL)
 |   ^~
>> arch/x86/kvm/vmx/vmx.c:2016:30: error: 'X86_FEATURE_BUS_LOCK_DETECT' 
>> undeclared (first use in this function); did you mean 
>> 'X86_FEATURE_SPLIT_LOCK_DETECT'?
2016 |   !guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
 |  ^~~
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
  58 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : 
__trace_if_value(cond))
 |^~~~
   arch/x86/kvm/vmx/vmx.c:2014:3: note: in expansion of macro 'if'
2014 |   if 

[RESEND PATCH 2/2] KVM: X86: Expose bus lock debug exception to guest

2021-01-07 Thread Chenyi Qiang
Bus lock debug exception is an ability to notify the kernel by an #DB
trap after the instruction acquires a bus lock and is executed when
CPL>0. This allows the kernel to enforce user application throttling or
mitigations.

Existence of bus lock debug exception is enumerated via
CPUID.(EAX=7,ECX=0).ECX[24]. Software can enable these exceptions by
setting bit 2 of the MSR_IA32_DEBUGCTL. Expose the CPUID to guest and
emulate the MSR handling when guest enables it.

Since SVM already has specific handlers of MSR_IA32_DEBUGMSR in the
svm_get/set_msr, move x86 commmon part to VMX and add the bus lock debug
exception support.

Co-developed-by: Xiaoyao Li 
Signed-off-by: Xiaoyao Li 
Signed-off-by: Chenyi Qiang 
---
 arch/x86/kvm/cpuid.c   |  3 ++-
 arch/x86/kvm/vmx/vmx.c | 23 +--
 arch/x86/kvm/x86.c | 16 ++--
 arch/x86/kvm/x86.h |  2 ++
 4 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 13036cf0b912..ea7c593794d2 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -402,7 +402,8 @@ void kvm_set_cpu_caps(void)
F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
-   F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/
+   F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
+   F(BUS_LOCK_DETECT)
);
/* Set LA57 based on hardware capability. */
if (cpuid_ecx(7) & F(LA57))
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 13c9bcc4d9d9..39f28f90bbb4 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -147,6 +147,9 @@ module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);
RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
RTIT_STATUS_BYTECNT))
 
+#define MSR_VMX_SUPPORTED_DEBUGCTL (DEBUGCTLMSR_LBR | \
+   DEBUGCTLMSR_BTF | DEBUGCTLMSR_BUS_LOCK_DETECT)
+
 /*
  * List of MSRs that can be directly passed to the guest.
  * In addition to these x2apic and PT MSRs are handled specially.
@@ -1924,6 +1927,9 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct 
msr_data *msr_info)
!guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
return 1;
goto find_uret_msr;
+   case MSR_IA32_DEBUGCTLMSR:
+   msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
+   break;
default:
find_uret_msr:
msr = vmx_find_uret_msr(vmx, msr_info->index);
@@ -2002,9 +2008,22 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct 
msr_data *msr_info)
VM_EXIT_SAVE_DEBUG_CONTROLS)
get_vmcs12(vcpu)->guest_ia32_debugctl = data;
 
-   ret = kvm_set_msr_common(vcpu, msr_info);
-   break;
+   if (data & ~MSR_VMX_SUPPORTED_DEBUGCTL)
+   return 1;
 
+   if (!msr_info->host_initiated &&
+   (data & DEBUGCTLMSR_BUS_LOCK_DETECT) &&
+   !guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))
+   return 1;
+
+   if (data & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) {
+   if (report_ignored_msrs)
+   vcpu_unimpl(vcpu, "%s: BTF|LBR in 
IA32_DEBUGCTLMSR 0x%llx, nop\n",
+   __func__, data);
+   data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR);
+   }
+   vmcs_write64(GUEST_IA32_DEBUGCTL, data);
+   return 0;
case MSR_IA32_BNDCFGS:
if (!kvm_mpx_supported() ||
(!msr_info->host_initiated &&
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 06de2b9e57f3..d4a601482794 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -116,8 +116,9 @@ EXPORT_SYMBOL_GPL(kvm_x86_ops);
 static bool __read_mostly ignore_msrs = 0;
 module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
 
-static bool __read_mostly report_ignored_msrs = true;
+bool __read_mostly report_ignored_msrs = true;
 module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
+EXPORT_SYMBOL_GPL(report_ignored_msrs);
 
 unsigned int min_timer_period_us = 200;
 module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
@@ -3067,18 +3068,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct 
msr_data *msr_info)
return 1;
}
break;
-   case MSR_IA32_DEBUGCTLMSR:
-   if (!data) {
-   /* We support the non-activated case already */
-   break;
-   } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
-   /* Values other than LBR and BTF are vendor-specific,
-  thus