From: Patrick Roy <roypat@amazon.co.uk>
To: Elliot Berman <quic_eberman@quicinc.com>,
Andrew Morton <akpm@linux-foundation.org>,
Paolo Bonzini <pbonzini@redhat.com>,
"Sean Christopherson" <seanjc@google.com>,
Fuad Tabba <tabba@google.com>,
"David Hildenbrand" <david@redhat.com>, <qperret@google.com>,
Ackerley Tng <ackerleytng@google.com>
Cc: <linux-coco@lists.linux.dev>, <linux-arm-msm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
<kvm@vger.kernel.org>, James Gowans <jgowans@amazon.com>,
"Kalyazin, Nikita" <kalyazin@amazon.co.uk>,
"Manwaring, Derek" <derekmn@amazon.com>,
"Cali, Marco" <xmarcalx@amazon.co.uk>
Subject: Re: [PATCH RFC 4/4] mm: guest_memfd: Add ability for mmap'ing pages
Date: Tue, 6 Aug 2024 16:48:22 +0100 [thread overview]
Message-ID: <6660faa6-4cea-4ddc-a378-ab9da9139ee9@amazon.co.uk> (raw)
In-Reply-To: <20240805-guest-memfd-lib-v1-4-e5a29a4ff5d7@quicinc.com>
On Mon, 2024-08-05 at 19:34 +0100, Elliot Berman wrote:
> Confidential/protected guest virtual machines want to share some memory
> back with the host Linux. For example, virtqueues allow host and
> protected guest to exchange data. In MMU-only isolation of protected
> guest virtual machines, the transition between "shared" and "private"
> can be done in-place without a trusted hypervisor copying pages.
>
> Add support for this feature and allow Linux to mmap host-accessible
> pages. When the owner provides an ->accessible() callback in the
> struct guest_memfd_operations, guest_memfd allows folios to be mapped
> when the ->accessible() callback returns 0.
Wouldn't the set of inaccessible folios always match exactly the set of
folios that have PG_private=1 set? At least guest_memfd instances that
have GUEST_MEMFD_FLAG_NO_DIRECT_MAP set, having folios without direct
map entries marked "accessible" sound like it may cause a lot of mayhem
(as those folios would essentially be secretmem folios, but this time
without the GUP checks). But even more generally, wouldn't tracking
accessibility via PG_private be enough?
> To safely make inaccessible:
>
> ```
> folio = guest_memfd_grab_folio(inode, index, flags);
> r = guest_memfd_make_inaccessible(inode, folio);
> if (r)
> goto err;
>
> hypervisor_does_guest_mapping(folio);
>
> folio_unlock(folio);
> ```
>
> hypervisor_does_s2_mapping(folio) should make it so
> ops->accessible(...) on those folios fails.
>
> The folio lock ensures atomicity.
>
> Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
> ---
> include/linux/guest_memfd.h | 7 ++++
> mm/guest_memfd.c | 81 ++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 87 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h
> index f9e4a27aed67..edcb4ba60cb0 100644
> --- a/include/linux/guest_memfd.h
> +++ b/include/linux/guest_memfd.h
> @@ -16,12 +16,18 @@
> * @invalidate_end: called after invalidate_begin returns success. Optional.
> * @prepare: called before a folio is mapped into the guest address space.
> * Optional.
> + * @accessible: called after prepare returns success and before it's mapped
> + * into the guest address space. Returns 0 if the folio can be
> + * accessed.
> + * Optional. If not present, assumes folios are never accessible.
> * @release: Called when releasing the guest_memfd file. Required.
> */
> struct guest_memfd_operations {
> int (*invalidate_begin)(struct inode *inode, pgoff_t offset, unsigned long nr);
> void (*invalidate_end)(struct inode *inode, pgoff_t offset, unsigned long nr);
> int (*prepare)(struct inode *inode, pgoff_t offset, struct folio *folio);
> + int (*accessible)(struct inode *inode, struct folio *folio,
> + pgoff_t offset, unsigned long nr);
> int (*release)(struct inode *inode);
> };
>
> @@ -48,5 +54,6 @@ struct file *guest_memfd_alloc(const char *name,
> const struct guest_memfd_operations *ops,
> loff_t size, unsigned long flags);
> bool is_guest_memfd(struct file *file, const struct guest_memfd_operations *ops);
> +int guest_memfd_make_inaccessible(struct file *file, struct folio *folio);
>
> #endif
> diff --git a/mm/guest_memfd.c b/mm/guest_memfd.c
> index e9d8cab72b28..6b5609932ca5 100644
> --- a/mm/guest_memfd.c
> +++ b/mm/guest_memfd.c
> @@ -9,6 +9,8 @@
> #include <linux/pagemap.h>
> #include <linux/set_memory.h>
>
> +#include "internal.h"
> +
> static inline int guest_memfd_folio_private(struct folio *folio)
> {
> unsigned long nr_pages = folio_nr_pages(folio);
> @@ -89,7 +91,7 @@ struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags
> goto out_err;
> }
>
> - if (gmem_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP) {
> + if (!ops->accessible && (gmem_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP)) {
> r = guest_memfd_folio_private(folio);
> if (r)
> goto out_err;
> @@ -107,6 +109,82 @@ struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags
> }
> EXPORT_SYMBOL_GPL(guest_memfd_grab_folio);
>
> +int guest_memfd_make_inaccessible(struct file *file, struct folio *folio)
> +{
> + unsigned long gmem_flags = (unsigned long)file->private_data;
> + unsigned long i;
> + int r;
> +
> + unmap_mapping_folio(folio);
> +
> + /**
> + * We can't use the refcount. It might be elevated due to
> + * guest/vcpu trying to access same folio as another vcpu
> + * or because userspace is trying to access folio for same reason
> + *
> + * folio_lock serializes the transitions between (in)accessible
> + */
> + if (folio_maybe_dma_pinned(folio))
> + return -EBUSY;
> +
> + if (gmem_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP) {
> + r = guest_memfd_folio_private(folio);
> + if (r)
> + return r;
> + }
> +
> + return 0;
> +}
> +
> +static vm_fault_t gmem_fault(struct vm_fault *vmf)
> +{
> + struct file *file = vmf->vma->vm_file;
> + struct inode *inode = file_inode(file);
> + const struct guest_memfd_operations *ops = inode->i_private;
> + struct folio *folio;
> + pgoff_t off;
> + int r;
> +
> + folio = guest_memfd_grab_folio(file, vmf->pgoff, GUEST_MEMFD_GRAB_UPTODATE);
> + if (!folio)
> + return VM_FAULT_SIGBUS;
> +
> + off = vmf->pgoff & (folio_nr_pages(folio) - 1);
> + r = ops->accessible(inode, folio, off, 1);
> + if (r) {
This made be stumble at first. I know you say ops->accessible returning
0 means "this is accessible", but if I only look at this if-statement it
reads as "if the folio is accessible, send a SIGBUS", which is not
what's actually happening.
> + folio_unlock(folio);
> + folio_put(folio);
> + return VM_FAULT_SIGBUS;
> + }
> +
> + guest_memfd_folio_clear_private(folio);
> +
> + vmf->page = folio_page(folio, off);
> +
> + return VM_FAULT_LOCKED;
> +}
> +
> +static const struct vm_operations_struct gmem_vm_ops = {
> + .fault = gmem_fault,
> +};
> +
> +static int gmem_mmap(struct file *file, struct vm_area_struct *vma)
> +{
> + const struct guest_memfd_operations *ops = file_inode(file)->i_private;
> +
> + if (!ops->accessible)
> + return -EPERM;
> +
> + /* No support for private mappings to avoid COW. */
> + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) !=
> + (VM_SHARED | VM_MAYSHARE))
> + return -EINVAL;
> +
> + file_accessed(file);
> + vma->vm_ops = &gmem_vm_ops;
> + return 0;
> +}
> +
> static long gmem_punch_hole(struct file *file, loff_t offset, loff_t len)
> {
> struct inode *inode = file_inode(file);
> @@ -220,6 +298,7 @@ static int gmem_release(struct inode *inode, struct file *file)
> static struct file_operations gmem_fops = {
> .open = generic_file_open,
> .llseek = generic_file_llseek,
> + .mmap = gmem_mmap,
> .release = gmem_release,
> .fallocate = gmem_fallocate,
> .owner = THIS_MODULE,
>
> --
> 2.34.1
>
next prev parent reply other threads:[~2024-08-06 15:48 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-05 18:34 [PATCH RFC 0/4] mm: Introduce guest_memfd library Elliot Berman
2024-08-05 18:34 ` [PATCH RFC 1/4] mm: Introduce guest_memfd Elliot Berman
2024-08-06 13:48 ` David Hildenbrand
2024-08-08 18:39 ` Ackerley Tng
2024-08-05 18:34 ` [PATCH RFC 2/4] kvm: Convert to use mm/guest_memfd Elliot Berman
2024-08-05 18:34 ` [PATCH RFC 3/4] mm: guest_memfd: Add option to remove guest private memory from direct map Elliot Berman
2024-08-06 14:08 ` David Hildenbrand
[not found] ` <396fb134-f43e-4263-99a8-cfcef82bfd99@amazon.com>
2024-08-15 19:08 ` Manwaring, Derek
2024-08-06 15:39 ` Patrick Roy
[not found] ` <20240806104702482-0700.eberman@hu-eberman-lv.qualcomm.com>
[not found] ` <a43ae745-9907-425f-b09d-a49405d6bc2d@amazon.co.uk>
[not found] ` <90886a03-ad62-4e98-bc05-63875faa9ccc@amazon.co.uk>
[not found] ` <20240807113514068-0700.eberman@hu-eberman-lv.qualcomm.com>
[not found] ` <7166d51c-7757-44f2-a6f8-36da3e86bf90@amazon.co.uk>
2024-08-08 22:16 ` Elliot Berman
2024-08-09 15:02 ` Patrick Roy
2024-08-19 10:09 ` Mike Rapoport
2024-08-20 16:56 ` Elliot Berman
2024-08-21 14:26 ` Mike Rapoport
2024-08-05 18:34 ` [PATCH RFC 4/4] mm: guest_memfd: Add ability for mmap'ing pages Elliot Berman
2024-08-06 13:51 ` David Hildenbrand
[not found] ` <20240806093625007-0700.eberman@hu-eberman-lv.qualcomm.com>
[not found] ` <a7c5bfc0-1648-4ae1-ba08-e706596e014b@redhat.com>
2024-08-08 21:41 ` Elliot Berman
2024-08-08 21:55 ` David Hildenbrand
2024-08-08 22:26 ` Elliot Berman
2024-08-09 7:16 ` David Hildenbrand
2024-08-15 7:24 ` Fuad Tabba
2024-08-16 9:48 ` David Hildenbrand
2024-08-16 11:19 ` Fuad Tabba
2024-08-16 17:45 ` Ackerley Tng
2024-08-16 18:08 ` David Hildenbrand
2024-08-16 21:52 ` Ackerley Tng
2024-08-16 22:03 ` David Hildenbrand
2024-08-16 23:52 ` Elliot Berman
2024-08-06 15:48 ` Patrick Roy [this message]
2024-08-08 18:51 ` Ackerley Tng
2024-08-08 21:42 ` Elliot Berman
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=6660faa6-4cea-4ddc-a378-ab9da9139ee9@amazon.co.uk \
--to=roypat@amazon.co.uk \
--cc=ackerleytng@google.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=jgowans@amazon.com \
--cc=kalyazin@amazon.co.uk \
--cc=kvm@vger.kernel.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pbonzini@redhat.com \
--cc=qperret@google.com \
--cc=quic_eberman@quicinc.com \
--cc=seanjc@google.com \
--cc=tabba@google.com \
--cc=xmarcalx@amazon.co.uk \
/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