From: Sean Christopherson <seanjc@google.com>
To: Nikita Kalyazin <kalyazin@amazon.co.uk>
Cc: "pbonzini@redhat.com" <pbonzini@redhat.com>,
"shuah@kernel.org" <shuah@kernel.org>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"linux-kselftest@vger.kernel.org"
<linux-kselftest@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"david@kernel.org" <david@kernel.org>,
"jthoughton@google.com" <jthoughton@google.com>,
"ackerleytng@google.com" <ackerleytng@google.com>,
"vannapurve@google.com" <vannapurve@google.com>,
"jackmanb@google.com" <jackmanb@google.com>,
"patrick.roy@linux.dev" <patrick.roy@linux.dev>,
Jack Thomson <jackabt@amazon.co.uk>,
Takahiro Itazuri <itazur@amazon.co.uk>,
Derek Manwaring <derekmn@amazon.com>,
Marco Cali <xmarcalx@amazon.co.uk>
Subject: Re: [PATCH v7 1/2] KVM: guest_memfd: add generic population via write
Date: Wed, 11 Mar 2026 17:46:27 -0700 [thread overview]
Message-ID: <abIM43oUcfIYMnBc@google.com> (raw)
In-Reply-To: <20251114151828.98165-2-kalyazin@amazon.com>
On Fri, Nov 14, 2025, Nikita Kalyazin wrote:
> ---
> Documentation/virt/kvm/api.rst | 2 ++
> include/linux/kvm_host.h | 2 +-
> include/uapi/linux/kvm.h | 1 +
> virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++
> 4 files changed, 56 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
> index 57061fa29e6a..9541e95fc2ed 100644
> --- a/Documentation/virt/kvm/api.rst
> +++ b/Documentation/virt/kvm/api.rst
> @@ -6448,6 +6448,8 @@ specified via KVM_CREATE_GUEST_MEMFD. Currently defined flags:
> without INIT_SHARED will be marked private).
> Shared memory can be faulted into host userspace
> page tables. Private memory cannot.
> + GUEST_MEMFD_FLAG_WRITE Enable using write() on the guest_memfd file
> + descriptor.
Not the greatest place for it due to limited space, but the page alignment and
shared restrictions should be documented, and this seems to be the best spot.
And whatever we do on a partial copy also needs to be documented. E.g.
GUEST_MEMFD_FLAG_WRITE Enable using write() on the guest_memfd file
descriptor. The start and size of the write
must be page aligned, and all pages must be in
a SHARED state. If the full buffer cannot be
copied for a given page, <something happens>.
> @@ -421,6 +423,53 @@ void kvm_gmem_init(struct module *module)
> kvm_gmem_fops.owner = module;
> }
>
> +static bool kvm_gmem_supports_write(struct inode *inode)
> +{
> + const u64 flags = (u64)inode->i_private;
> +
> + return flags & GUEST_MEMFD_FLAG_WRITE;
> +}
> +
> +static int kvm_gmem_write_begin(const struct kiocb *kiocb,
> + struct address_space *mapping,
> + loff_t pos, unsigned int len,
> + struct folio **folio, void **fsdata)
> +{
> + struct inode *inode = file_inode(kiocb->ki_filp);
> +
> + if (!kvm_gmem_supports_write(inode))
Eh, no need for a helper, especially since flags is now easier to get at:
if (!(GMEM_I(inode)->flags | GUEST_MEMFD_FLAG_WRITE))
return -ENODEV;
I also think we should leave ourselves a safety net for in-place conversion, and
WARN if the gmem instance isn't INIT_SHARED:
if (WARN_ON_ONCE(!(GMEM_I(inode)->flags & GUEST_MEMFD_FLAG_INIT_SHARED)))
return -EBUSY;
That will also provide a good place to actually verify the memory is shared once
in-place conversion comes along.
> + return -ENODEV;
> +
> + if (pos + len > i_size_read(inode))
> + return -EINVAL;
> +
> + if (!IS_ALIGNED(pos, PAGE_SIZE) || !IS_ALIGNED(len, PAGE_SIZE))
> + return -EINVAL;
> +
> + *folio = kvm_gmem_get_folio(inode, pos >> PAGE_SHIFT);
> + if (IS_ERR(*folio))
> + return PTR_ERR(*folio);
> +
> + return 0;
> +}
> +
> +static int kvm_gmem_write_end(const struct kiocb *kiocb,
> + struct address_space *mapping,
> + loff_t pos, unsigned int len,
> + unsigned int copied,
> + struct folio *folio, void *fsdata)
> +{
> + if (!folio_test_uptodate(folio)) {
> + folio_zero_range(folio, copied, len - copied);
Hmm, do we actually want to zero and silently ignore the failure? Given the
intended use case, silently failing here would be a terrible outcome. Would it
makes sense to instead do this?
if (len != copied)
return -EFAULT;
if (!folio_test_uptodate(folio))
folio_mark_uptodate(folio);
folio_unlock(folio);
folio_put(folio);
return copied;
That will cause generic_perform_write() to report -EFAULT if no pages were written,
or IIUC, return the position of the last _full_ page that was written. Then in
the unlikely scenario userspace wants to retry, they can retry starting at the
page that was partially written. That seems like what VMMs generally would want,
not silent failure.
next prev parent reply other threads:[~2026-03-12 0:46 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-14 15:18 [PATCH v7 0/2] KVM: guest_memfd: use write for population Kalyazin, Nikita
2025-11-14 15:18 ` [PATCH v7 1/2] KVM: guest_memfd: add generic population via write Kalyazin, Nikita
2026-03-12 0:46 ` Sean Christopherson [this message]
2025-11-14 15:18 ` [PATCH v7 2/2] KVM: selftests: update guest_memfd write tests Kalyazin, Nikita
2026-03-12 0:55 ` Sean Christopherson
2025-11-14 15:23 ` [PATCH v7 0/2] KVM: guest_memfd: use write for population Nikita Kalyazin
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=abIM43oUcfIYMnBc@google.com \
--to=seanjc@google.com \
--cc=ackerleytng@google.com \
--cc=david@kernel.org \
--cc=derekmn@amazon.com \
--cc=itazur@amazon.co.uk \
--cc=jackabt@amazon.co.uk \
--cc=jackmanb@google.com \
--cc=jthoughton@google.com \
--cc=kalyazin@amazon.co.uk \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=patrick.roy@linux.dev \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
--cc=vannapurve@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