diff mbox series

[v3,2/6] KVM: x86: add force_intercept_exceptions_mask

Message ID 20210811122927.900604-3-mlevitsk@redhat.com (mailing list archive)
State New, archived
Headers show
Series KVM: my debug patch queue | expand

Commit Message

Maxim Levitsky Aug. 11, 2021, 12:29 p.m. UTC
This parameter will be used by VMX and SVM code to force
interception of a set of exceptions, given by a bitmask
for guest debug and/or kvm debug.

This is based on an idea first shown here:
https://patchwork.kernel.org/project/kvm/patch/20160301192822.GD22677@pd.tnic/

CC: Borislav Petkov <bp@suse.de>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
 arch/x86/kvm/x86.c | 3 +++
 arch/x86/kvm/x86.h | 2 ++
 2 files changed, 5 insertions(+)

Comments

Sean Christopherson Sept. 2, 2021, 4:56 p.m. UTC | #1
Assuming this hasn't been abandoned...

On Wed, Aug 11, 2021, Maxim Levitsky wrote:
> This parameter will be used by VMX and SVM code to force
> interception of a set of exceptions, given by a bitmask
> for guest debug and/or kvm debug.
> 
> This is based on an idea first shown here:
> https://patchwork.kernel.org/project/kvm/patch/20160301192822.GD22677@pd.tnic/
> 
> CC: Borislav Petkov <bp@suse.de>
> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> ---
>  arch/x86/kvm/x86.c | 3 +++
>  arch/x86/kvm/x86.h | 2 ++
>  2 files changed, 5 insertions(+)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index fdc0c18339fb..092e2fad3c0d 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -184,6 +184,9 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
>  int __read_mostly pi_inject_timer = -1;
>  module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
>  
> +uint force_intercept_exceptions_mask;
> +module_param(force_intercept_exceptions_mask, uint, S_IRUGO | S_IWUSR);

Use octal permissions.  This also can't be a simple writable param, at least not
without a well-documented disclaimer, as there's no guarantee a vCPU will update
its exception bitmap in a timely fashion.  An alternative to a module param would
be to extend/add a per-VM ioctl(), e.g. maybe KVM_SET_GUEST_DEBUG?  The downside
of an ioctl() is that it would require userspace enabling :-/
Maxim Levitsky Feb. 8, 2022, 2:34 p.m. UTC | #2
On Thu, 2021-09-02 at 16:56 +0000, Sean Christopherson wrote:
> Assuming this hasn't been abandoned...
> 
> On Wed, Aug 11, 2021, Maxim Levitsky wrote:
> > This parameter will be used by VMX and SVM code to force
> > interception of a set of exceptions, given by a bitmask
> > for guest debug and/or kvm debug.
> > 
> > This is based on an idea first shown here:
> > https://patchwork.kernel.org/project/kvm/patch/20160301192822.GD22677@pd.tnic/
> > 
> > CC: Borislav Petkov <bp@suse.de>
> > Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> > ---
> >  arch/x86/kvm/x86.c | 3 +++
> >  arch/x86/kvm/x86.h | 2 ++
> >  2 files changed, 5 insertions(+)
> > 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index fdc0c18339fb..092e2fad3c0d 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -184,6 +184,9 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
> >  int __read_mostly pi_inject_timer = -1;
> >  module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
> >  
> > +uint force_intercept_exceptions_mask;
> > +module_param(force_intercept_exceptions_mask, uint, S_IRUGO | S_IWUSR);
> 
> Use octal permissions.  This also can't be a simple writable param, at least not
> without a well-documented disclaimer, as there's no guarantee a vCPU will update
> its exception bitmap in a timely fashion.  An alternative to a module param would
> be to extend/add a per-VM ioctl(), e.g. maybe KVM_SET_GUEST_DEBUG?  The downside
> of an ioctl() is that it would require userspace enabling :-/
> 

All other module params in this file use macros for permissions, that is why
I used them too.

I'll add a comment with a disclaimer here - this is only for debug.
I strongly don't want to have this as ioctl as that will indeed need qemu patches,
not to mention things like unit tests and which don't even always use qemu.

Or I can make this parameter read-only. I don't mind reloading kvm module when
I change this parameter.


Best regards,
	Maxim Levitsky

PS: Forgot to send this mail, it was in my drafts folder.
Sean Christopherson March 8, 2022, 11:37 p.m. UTC | #3
On Tue, Feb 08, 2022, Maxim Levitsky wrote:
> On Thu, 2021-09-02 at 16:56 +0000, Sean Christopherson wrote:
> > Assuming this hasn't been abandoned...
> > 
> > On Wed, Aug 11, 2021, Maxim Levitsky wrote:
> > > This parameter will be used by VMX and SVM code to force
> > > interception of a set of exceptions, given by a bitmask
> > > for guest debug and/or kvm debug.
> > > 
> > > This is based on an idea first shown here:
> > > https://patchwork.kernel.org/project/kvm/patch/20160301192822.GD22677@pd.tnic/
> > > 
> > > CC: Borislav Petkov <bp@suse.de>
> > > Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> > > ---
> > >  arch/x86/kvm/x86.c | 3 +++
> > >  arch/x86/kvm/x86.h | 2 ++
> > >  2 files changed, 5 insertions(+)
> > > 
> > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > > index fdc0c18339fb..092e2fad3c0d 100644
> > > --- a/arch/x86/kvm/x86.c
> > > +++ b/arch/x86/kvm/x86.c
> > > @@ -184,6 +184,9 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
> > >  int __read_mostly pi_inject_timer = -1;
> > >  module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
> > >  
> > > +uint force_intercept_exceptions_mask;
> > > +module_param(force_intercept_exceptions_mask, uint, S_IRUGO | S_IWUSR);
> > 
> > Use octal permissions.  This also can't be a simple writable param, at least not
> > without a well-documented disclaimer, as there's no guarantee a vCPU will update
> > its exception bitmap in a timely fashion.  An alternative to a module param would
> > be to extend/add a per-VM ioctl(), e.g. maybe KVM_SET_GUEST_DEBUG?  The downside
> > of an ioctl() is that it would require userspace enabling :-/
> > 
> 
> All other module params in this file use macros for permissions, that is why
> I used them too.
> 
> I'll add a comment with a disclaimer here - this is only for debug.
> I strongly don't want to have this as ioctl as that will indeed need qemu patches,
> not to mention things like unit tests and which don't even always use qemu.
> 
> Or I can make this parameter read-only. I don't mind reloading kvm module when
> I change this parameter.

Oh!  We can force an update, a la nx_huge_pages, where the setter loops through
all VMs and does a kvm_make_all_cpus_request() to instruct vCPUs to update their
bitmaps.  Requires a new request, but that doesn't seem like a huge deal, and it
might help pave the way for adding more debug hooks for developers.

The param should also be "unsafe".

E.g. something like

static const struct kernel_param_ops force_ex_intercepts_ops = {
	.set = set_force_exception_intercepts,
	.get = get_force_exception_intercepts,
};
module_param_cb_unsafe(force_exception_intercepts, &force_ex_intercepts_ops,
		       &force_exception_intercepts, 0644);
Maxim Levitsky March 9, 2022, 12:31 p.m. UTC | #4
On Tue, 2022-03-08 at 23:37 +0000, Sean Christopherson wrote:
> On Tue, Feb 08, 2022, Maxim Levitsky wrote:
> > On Thu, 2021-09-02 at 16:56 +0000, Sean Christopherson wrote:
> > > Assuming this hasn't been abandoned...
> > > 
> > > On Wed, Aug 11, 2021, Maxim Levitsky wrote:
> > > > This parameter will be used by VMX and SVM code to force
> > > > interception of a set of exceptions, given by a bitmask
> > > > for guest debug and/or kvm debug.
> > > > 
> > > > This is based on an idea first shown here:
> > > > https://patchwork.kernel.org/project/kvm/patch/20160301192822.GD22677@pd.tnic/
> > > > 
> > > > CC: Borislav Petkov <bp@suse.de>
> > > > Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> > > > ---
> > > >  arch/x86/kvm/x86.c | 3 +++
> > > >  arch/x86/kvm/x86.h | 2 ++
> > > >  2 files changed, 5 insertions(+)
> > > > 
> > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > > > index fdc0c18339fb..092e2fad3c0d 100644
> > > > --- a/arch/x86/kvm/x86.c
> > > > +++ b/arch/x86/kvm/x86.c
> > > > @@ -184,6 +184,9 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
> > > >  int __read_mostly pi_inject_timer = -1;
> > > >  module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
> > > >  
> > > > +uint force_intercept_exceptions_mask;
> > > > +module_param(force_intercept_exceptions_mask, uint, S_IRUGO | S_IWUSR);
> > > 
> > > Use octal permissions.  This also can't be a simple writable param, at least not
> > > without a well-documented disclaimer, as there's no guarantee a vCPU will update
> > > its exception bitmap in a timely fashion.  An alternative to a module param would
> > > be to extend/add a per-VM ioctl(), e.g. maybe KVM_SET_GUEST_DEBUG?  The downside
> > > of an ioctl() is that it would require userspace enabling :-/
> > > 
> > 
> > All other module params in this file use macros for permissions, that is why
> > I used them too.
> > 
> > I'll add a comment with a disclaimer here - this is only for debug.
> > I strongly don't want to have this as ioctl as that will indeed need qemu patches,
> > not to mention things like unit tests and which don't even always use qemu.
> > 
> > Or I can make this parameter read-only. I don't mind reloading kvm module when
> > I change this parameter.
> 
> Oh!  We can force an update, a la nx_huge_pages, where the setter loops through
> all VMs and does a kvm_make_all_cpus_request() to instruct vCPUs to update their
> bitmaps.  Requires a new request, but that doesn't seem like a huge deal, and it
> might help pave the way for adding more debug hooks for developers.

Question: is it worth it? Since I am very busy with various things, this feature,
beeing just small debug help which I used once in a while doesn't get much time from me.

I can implement this in the future no doubt.

Best regards,
	Maxim Levitsky

> 
> The param should also be "unsafe".

I didn't knew about unsafe parameters until recently. I can mark it as such,
no objections.

Best regards,
	Maxim Levitsky

> 
> E.g. something like
> 
> static const struct kernel_param_ops force_ex_intercepts_ops = {
> 	.set = set_force_exception_intercepts,
> 	.get = get_force_exception_intercepts,
> };
> module_param_cb_unsafe(force_exception_intercepts, &force_ex_intercepts_ops,
> 		       &force_exception_intercepts, 0644);
>
Paolo Bonzini March 9, 2022, 2:03 p.m. UTC | #5
On 3/9/22 13:31, Maxim Levitsky wrote:
> Question: is it worth it? Since I am very busy with various things, this feature,
> beeing just small debug help which I used once in a while doesn't get much time from me.

I agree it's not very much worth.

Paolo
Sean Christopherson March 9, 2022, 3:40 p.m. UTC | #6
On Wed, Mar 09, 2022, Paolo Bonzini wrote:
> On 3/9/22 13:31, Maxim Levitsky wrote:
> > Question: is it worth it? Since I am very busy with various things, this
> > feature, beeing just small debug help which I used once in a while doesn't
> > get much time from me.
> 
> I agree it's not very much worth.

I don't have a use case, was just trying to find the bottom of my inbox and came
across this thread.
diff mbox series

Patch

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index fdc0c18339fb..092e2fad3c0d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -184,6 +184,9 @@  module_param(force_emulation_prefix, bool, S_IRUGO);
 int __read_mostly pi_inject_timer = -1;
 module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
 
+uint force_intercept_exceptions_mask;
+module_param(force_intercept_exceptions_mask, uint, S_IRUGO | S_IWUSR);
+EXPORT_SYMBOL_GPL(force_intercept_exceptions_mask);
 /*
  * Restoring the host value for MSRs that are only consumed when running in
  * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 7d66d63dc55a..2b59825213c7 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -347,6 +347,8 @@  extern struct static_key kvm_no_apic_vcpu;
 
 extern bool report_ignored_msrs;
 
+extern uint force_intercept_exceptions_mask;
+
 static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
 {
 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,