mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Isaku Yamahata <isaku.yamahata@gmail.com>
To: Michael Roth <michael.roth@amd.com>
Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com,
	Paolo Bonzini <pbonzini@redhat.com>,
	erdemaktas@google.com, Sean Christopherson <seanjc@google.com>,
	Sagi Shahar <sagis@google.com>,
	David Matlack <dmatlack@google.com>,
	Kai Huang <kai.huang@intel.com>,
	Zhi Wang <zhi.wang.linux@gmail.com>,
	chen.bo@intel.com, linux-coco@lists.linux.dev,
	Chao Peng <chao.p.peng@linux.intel.com>,
	Ackerley Tng <ackerleytng@google.com>,
	Vishal Annapurve <vannapurve@google.com>
Subject: Re: [RFC PATCH v2 5/6] KVM: Add flags to struct kvm_gfn_range
Date: Wed, 28 Jun 2023 10:05:36 -0700	[thread overview]
Message-ID: <20230628170536.GF3436214@ls.amr.corp.intel.com> (raw)
In-Reply-To: <20230628152132.xyfquj67vhklsvlu@amd.com>

On Wed, Jun 28, 2023 at 10:21:32AM -0500,
Michael Roth <michael.roth@amd.com> wrote:

> On Thu, Jun 22, 2023 at 04:16:29PM -0700, isaku.yamahata@intel.com wrote:
> > From: Isaku Yamahata <isaku.yamahata@intel.com>
> > 
> > Add flags to strut kvm_gfn_range to indicate who triggered the callback
> > and new memory attributes.
> > 
> > TDX needs to know the reason for a callback by kvm_unmap_gfn_range().  mmu
> > notifier, set memory attributes ioctl or KVM gmem callback.  With TDX,
> > zapping a private page from the encrypted page table and adding the page
> > back to the same private GPA results in zeroing the page, and the guest has
> > to accept the page again.  On the change of memory attribute from private
> > to shared, zapping the GPA range irrespective to private-or-shared and
> > expecting the fault doesn't work for TDX.  Instead, zap shared pages only
> > and keep the private pages.  Concretely
> > - If it's from mmu notifier, zap shared pages.
> > - If it's from KVM gmem, zap private pages.
> > - If setting memory attributes to private, zap shared pages.
> > - If setting memory attributes to shared, zap private pages.
> > 
> > Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> > 
> > ---
> > Changes v1 -> v2:
> > - consolidate KVM_GFN_RANGE_FLAGS_GMEM_{PUNCH_HOLE, RELEASE} into
> >   KVM_GFN_RANGE_FLAGS_GMEM.
> > - Update the commit message to describe TDX more.  Drop SEV_SNP.
> > ---
> >  include/linux/kvm_host.h | 10 +++++++++-
> >  virt/kvm/guest_mem.c     |  9 ++++++---
> >  virt/kvm/kvm_main.c      |  4 +++-
> >  3 files changed, 18 insertions(+), 5 deletions(-)
> > 
> > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> > index 1a47cedae8a1..1fe0516fcddf 100644
> > --- a/include/linux/kvm_host.h
> > +++ b/include/linux/kvm_host.h
> > @@ -256,12 +256,20 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
> >  #endif
> >  
> >  #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER
> > +
> > +#define KVM_GFN_RANGE_FLAGS_SET_MEM_ATTR	BIT(0)
> > +#define KVM_GFN_RANGE_FLAGS_GMEM		BIT(1)
> > +
> >  struct kvm_gfn_range {
> >  	struct kvm_memory_slot *slot;
> >  	gfn_t start;
> >  	gfn_t end;
> > -	pte_t pte;
> > +	union {
> > +		pte_t pte;
> > +		u64 attrs;
> > +	};
> >  	bool may_block;
> > +	unsigned int flags;
> >  };
> >  bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range);
> >  bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
> > diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c
> > index cdf2d84683c8..387226136960 100644
> > --- a/virt/kvm/guest_mem.c
> > +++ b/virt/kvm/guest_mem.c
> > @@ -99,7 +99,8 @@ static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index)
> >  }
> >  
> >  static void kvm_gmem_invalidate_begin(struct kvm *kvm, struct kvm_gmem *gmem,
> > -				      pgoff_t start, pgoff_t end)
> > +				      pgoff_t start, pgoff_t end,
> > +				      unsigned int flags)
> >  {
> >  	struct kvm_memory_slot *slot;
> >  	unsigned long index;
> > @@ -118,6 +119,7 @@ static void kvm_gmem_invalidate_begin(struct kvm *kvm, struct kvm_gmem *gmem,
> >  			.slot = slot,
> >  			.pte = __pte(0),
> >  			.may_block = true,
> > +			.flags = flags,
> >  		};
> >  
> >  		kvm_mmu_invalidate_range_add(kvm, gfn_range.start, gfn_range.end);
> > @@ -156,7 +158,8 @@ static long kvm_gmem_punch_hole(struct file *file, loff_t offset, loff_t len)
> >  	 */
> >  	filemap_invalidate_lock(file->f_mapping);
> >  
> > -	kvm_gmem_invalidate_begin(kvm, gmem, start, end);
> > +	kvm_gmem_invalidate_begin(kvm, gmem, start, end,
> > +				  KVM_GFN_RANGE_FLAGS_GMEM);
> 
> Do you anticipate ever needing to pass a different flag via
> kvm_gmem_invalidate_begin()? If not it might make sense to just
> hard-code it rather than passing as a parameter.

I'll update this patch following the suggestion by Sean. [1]
I'll drop the flag. the struct kvm_gfn_range will be

struct kvm_gfn_range {
 struct kvm_memory_slot *slot;
 gfn_t start;
 gfn_t end;
 union {
  struct test_clear_young_metadata *metadata;
  unsigned long attributes;
  pte_t pte;
  unsigned long callback_arg; /* needs a better name */
 };
 bool only_private;
 bool only_shared;
 bool may_block;
};


[1] https://lore.kernel.org/all/ZJX0hk+KpQP0KUyB@google.com/
-- 
Isaku Yamahata <isaku.yamahata@gmail.com>

  reply	other threads:[~2023-06-28 17:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-22 23:16 [RFC PATCH v2 0/6] KVM: guest memory: Misc enhacnement isaku.yamahata
2023-06-22 23:16 ` [RFC PATCH v2 1/6] KVM: selftests: Fix test_add_overlapping_private_memory_regions() isaku.yamahata
2023-06-22 23:16 ` [RFC PATCH v2 2/6] KVM: selftests: Fix guest_memfd() isaku.yamahata
2023-06-22 23:16 ` [RFC PATCH v2 3/6] KVM: x86/mmu: Pass round full 64-bit error code for the KVM page fault isaku.yamahata
2023-06-22 23:28   ` Huang, Kai
2023-06-23  2:54     ` Isaku Yamahata
2023-06-23 17:18       ` Sean Christopherson
2023-06-24  4:15         ` Huang, Kai
2023-06-26 21:48           ` Sean Christopherson
2023-06-22 23:16 ` [RFC PATCH v2 4/6] KVM: x86: Introduce fault type to indicate kvm page fault is private isaku.yamahata
2023-06-23 20:04   ` Sean Christopherson
2023-06-26  1:07     ` Michael Roth
2023-06-26 18:21       ` Sean Christopherson
2023-06-27 23:58         ` Michael Roth
2023-06-28 16:53           ` Sean Christopherson
2023-06-22 23:16 ` [RFC PATCH v2 5/6] KVM: Add flags to struct kvm_gfn_range isaku.yamahata
2023-06-28  6:41   ` Yuan Yao
2023-06-28 17:03     ` Isaku Yamahata
2023-06-28 15:21   ` Michael Roth
2023-06-28 17:05     ` Isaku Yamahata [this message]
2023-06-22 23:16 ` [RFC PATCH v2 6/6] KVM: x86: Add is_vm_type_supported callback isaku.yamahata

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230628170536.GF3436214@ls.amr.corp.intel.com \
    --to=isaku.yamahata@gmail.com \
    --cc=ackerleytng@google.com \
    --cc=chao.p.peng@linux.intel.com \
    --cc=chen.bo@intel.com \
    --cc=dmatlack@google.com \
    --cc=erdemaktas@google.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=sagis@google.com \
    --cc=seanjc@google.com \
    --cc=vannapurve@google.com \
    --cc=zhi.wang.linux@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®