Re: [PATCH v10 9/9] KVM: Enable and expose KVM_MEM_PRIVATE

From: Isaku Yamahata
Date: Wed Mar 22 2023 - 20:41:42 EST


On Wed, Mar 08, 2023 at 03:40:26PM +0800,
Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx> wrote:

> On Wed, Mar 08, 2023 at 12:13:24AM +0000, Ackerley Tng wrote:
> > Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx> writes:
> >
> > > On Sat, Jan 14, 2023 at 12:01:01AM +0000, Sean Christopherson wrote:
> > > > On Fri, Dec 02, 2022, Chao Peng wrote:
> > > ...
> > > > Strongly prefer to use similar logic to existing code that detects wraps:
> >
> > > > mem->restricted_offset + mem->memory_size < mem->restricted_offset
> >
> > > > This is also where I'd like to add the "gfn is aligned to offset"
> > > > check, though
> > > > my brain is too fried to figure that out right now.
> >
> > > Used count_trailing_zeros() for this TODO, unsure we have other better
> > > approach.
> >
> > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > > index afc8c26fa652..fd34c5f7cd2f 100644
> > > --- a/virt/kvm/kvm_main.c
> > > +++ b/virt/kvm/kvm_main.c
> > > @@ -56,6 +56,7 @@
> > > #include <asm/processor.h>
> > > #include <asm/ioctl.h>
> > > #include <linux/uaccess.h>
> > > +#include <linux/count_zeros.h>
> >
> > > #include "coalesced_mmio.h"
> > > #include "async_pf.h"
> > > @@ -2087,6 +2088,19 @@ static bool kvm_check_memslot_overlap(struct
> > > kvm_memslots *slots, int id,
> > > return false;
> > > }
> >
> > > +/*
> > > + * Return true when ALIGNMENT(offset) >= ALIGNMENT(gpa).
> > > + */
> > > +static bool kvm_check_rmem_offset_alignment(u64 offset, u64 gpa)
> > > +{
> > > + if (!offset)
> > > + return true;
> > > + if (!gpa)
> > > + return false;
> > > +
> > > + return !!(count_trailing_zeros(offset) >= count_trailing_zeros(gpa));

This check doesn't work expected. For example, offset = 2GB, gpa=4GB
this check fails.
I come up with the following.