linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Elliot Berman <quic_eberman@quicinc.com>
To: David Hildenbrand <david@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Sean Christopherson <seanjc@google.com>,
	Fuad Tabba <tabba@google.com>,
	Ackerley Tng <ackerleytng@google.com>,
	Mike Rapoport <rppt@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	Matthew Wilcox <willy@infradead.org>,
	James Gowans <jgowans@amazon.com>,
	<linux-fsdevel@vger.kernel.org>, <kvm@vger.kernel.org>,
	<linux-coco@lists.linux.dev>, <linux-arm-msm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>
Subject: Re: [PATCH RFC v3 1/2] KVM: guest_memfd: Convert .free_folio() to .release_folio()
Date: Fri, 15 Nov 2024 12:13:59 -0800	[thread overview]
Message-ID: <20241115121119110-0800.eberman@hu-eberman-lv.qualcomm.com> (raw)
In-Reply-To: <d2147b7c-bb2e-4434-aa10-40cacac43d4f@redhat.com>

On Fri, Nov 15, 2024 at 11:58:59AM +0100, David Hildenbrand wrote:
> On 15.11.24 11:58, David Hildenbrand wrote:
> > On 13.11.24 23:34, Elliot Berman wrote:
> > > When guest_memfd becomes a library, a callback will need to be made to
> > > the owner (KVM SEV) to transition pages back to hypervisor-owned/shared
> > > state. This is currently being done as part of .free_folio() address
> > > space op, but this callback shouldn't assume that the mapping still
> > > exists. guest_memfd library will need the mapping to still exist to look
> > > up its operations table.
> > 
> > I assume you mean, that the mapping is no longer set for the folio (it
> > sure still exists, because we are getting a callback from it :) )?
> > 
> > Staring at filemap_remove_folio(), this is exactly what happens:
> > 
> > We remember folio->mapping, call __filemap_remove_folio(), and then call
> > filemap_free_folio() where we zap folio->mapping via page_cache_delete().
> > 
> > Maybe it's easier+cleaner to also forward the mapping to the
> > free_folio() callback, just like we do with filemap_free_folio()? Would
> > that help?
> > 
> > CCing Willy if that would be reasonable extension of the free_folio
> > callback.
> > 

I like this approach too. It would avoid the checks we have to do in the
invalidate_folio() callback and is cleaner.

- Elliot

> > > 
> > > .release_folio() and .invalidate_folio() address space ops can serve the
> > > same purpose here. The key difference between release_folio() and
> > > free_folio() is whether the mapping is still valid at time of the
> > > callback. This approach was discussed in the link in the footer, but not
> > > taken because free_folio() was easier to implement.
> > > 
> > > Link: https://lore.kernel.org/kvm/20231016115028.996656-1-michael.roth@amd.com/
> > > Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
> > > ---
> > >    virt/kvm/guest_memfd.c | 19 ++++++++++++++++---
> > >    1 file changed, 16 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> > > index 47a9f68f7b247f4cba0c958b4c7cd9458e7c46b4..13f83ad8a4c26ba82aca4f2684f22044abb4bc19 100644
> > > --- a/virt/kvm/guest_memfd.c
> > > +++ b/virt/kvm/guest_memfd.c
> > > @@ -358,22 +358,35 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol
> > >    }
> > >    #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
> > > -static void kvm_gmem_free_folio(struct folio *folio)
> > > +static bool kvm_gmem_release_folio(struct folio *folio, gfp_t gfp)
> > >    {
> > >    	struct page *page = folio_page(folio, 0);
> > >    	kvm_pfn_t pfn = page_to_pfn(page);
> > >    	int order = folio_order(folio);
> > >    	kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order));
> > > +
> > > +	return true;
> > > +}
> > > +
> > > +static void kvm_gmem_invalidate_folio(struct folio *folio, size_t offset,
> > > +				      size_t len)
> > > +{
> > > +	WARN_ON_ONCE(offset != 0);
> > > +	WARN_ON_ONCE(len != folio_size(folio));
> > > +
> > > +	if (offset == 0 && len == folio_size(folio))
> > > +		filemap_release_folio(folio, 0);
> > >    }
> > >    #endif
> > >    static const struct address_space_operations kvm_gmem_aops = {
> > >    	.dirty_folio = noop_dirty_folio,
> > > -	.migrate_folio	= kvm_gmem_migrate_folio,
> > > +	.migrate_folio = kvm_gmem_migrate_folio,
> > >    	.error_remove_folio = kvm_gmem_error_folio,
> > >    #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
> > > -	.free_folio = kvm_gmem_free_folio,
> > > +	.release_folio = kvm_gmem_release_folio,
> > > +	.invalidate_folio = kvm_gmem_invalidate_folio,
> > >    #endif
> > >    };
> > > 
> > 
> > 
> 
> 
> -- 
> Cheers,
> 
> David / dhildenb
> 


  reply	other threads:[~2024-11-15 20:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-13 22:34 [PATCH RFC v3 0/2] mm: Refactor KVM guest_memfd to introduce guestmem library Elliot Berman
2024-11-13 22:34 ` [PATCH RFC v3 1/2] KVM: guest_memfd: Convert .free_folio() to .release_folio() Elliot Berman
2024-11-15 10:58   ` David Hildenbrand
2024-11-15 10:58     ` David Hildenbrand
2024-11-15 20:13       ` Elliot Berman [this message]
2024-11-18  9:38         ` David Hildenbrand
2024-11-13 22:34 ` [PATCH RFC v3 2/2] mm: guestmem: Convert address_space operations to guestmem library Elliot Berman
2024-11-15 10:31 ` [PATCH RFC v3 0/2] mm: Refactor KVM guest_memfd to introduce " David Hildenbrand

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=20241115121119110-0800.eberman@hu-eberman-lv.qualcomm.com \
    --to=quic_eberman@quicinc.com \
    --cc=ackerleytng@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=hpa@zytor.com \
    --cc=jgowans@amazon.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pbonzini@redhat.com \
    --cc=rppt@kernel.org \
    --cc=seanjc@google.com \
    --cc=tabba@google.com \
    --cc=willy@infradead.org \
    /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