Re: [PATCH] KVM: VMX: Add a wrapper for reading INVPCID/INVEPT/INVVPID type

From: Vipin Sharma
Date: Tue Nov 02 2021 - 14:13:35 EST


Sorry for the late reply.

On Thu, Oct 14, 2021 at 10:05 AM Jim Mattson <jmattson@xxxxxxxxxx> wrote:
>
> On Thu, Oct 14, 2021 at 9:54 AM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
> >
> > On Mon, Oct 11, 2021, Jim Mattson wrote:
> > > On Mon, Oct 11, 2021 at 1:23 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
> > > >
> > > > On Mon, Oct 11, 2021, Vipin Sharma wrote:
> > > > > - if (type > 3) {
> > > > > + if (type > INVPCID_TYPE_MAX) {
> > > >
> > > > Hrm, I don't love this because it's not auto-updating in the unlikely chance that
> > > > a new type is added. I definitely don't like open coding '3' either. What about
> > > > going with a verbose option of
> > > >
> > > > if (type != INVPCID_TYPE_INDIV_ADDR &&
> > > > type != INVPCID_TYPE_SINGLE_CTXT &&
> > > > type != INVPCID_TYPE_ALL_INCL_GLOBAL &&
> > > > type != INVPCID_TYPE_ALL_NON_GLOBAL) {
> > > > kvm_inject_gp(vcpu, 0);
> > > > return 1;
> > > > }
> > >
> > > Better, perhaps, to introduce a new function, valid_invpcid_type(),
> > > and squirrel away the ugliness there?
> >

I might not have understood your auto-updating concern correctly, can
I change these macros to an enum like:

enum INVPCID_TYPE {
INVPCID_TYPE_INDIV_ADDR,
INVPCID_TYPE_SINGLE_CTXT,
INVPCID_TYPE_ALL_INCL_GLOBAL,
INVPCID_TYPE_ALL_NON_GLOBAL,
INVPCID_TYPE_MAX,
};

My check in the condition will be then "if (type >= INVPCID_TYPE_MAX) {}"
This way if there is a new type added, max will be auto updated. Will
this answers your concern?

> > Oh, yeah, definitely. I missed that SVM's invpcid_interception() has the same
> > open-coded check.
> >
> > Alternatively, could we handle the invalid type in the main switch statement? I
> > don't see anything in the SDM or APM that architecturally _requires_ the type be
> > checked before reading the INVPCID descriptor. Hardware may operate that way,
> > but that's uArch specific behavior unless there's explicit documentation.
>
> Right. INVVPID and INVEPT are explicitly documented to check the type
> first, but INVPCID is not.

It seems to me that I can move type > 3 check to kvm_handle_invpcid()
switch statement. I can replace BUG() in that switch statement with
kvm_inject_gp for the default case, I won't even need INVPCID_TYPE_MAX
in this case.

If you are fine with this approach then I will send out a patch where
invalid type is handled in kvm_handle_invpcid() switch statement.

Thanks
Vipin