From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Shivank Garg <shivankg@amd.com>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Jan Kara <jack@suse.cz>,
Andrew Morton <akpm@linux-foundation.org>,
Vlastimil Babka <vbabka@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Brendan Jackman <jackmanb@google.com>,
Johannes Weiner <hannes@cmpxchg.org>, Zi Yan <ziy@nvidia.com>,
Matthew Brost <matthew.brost@intel.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Rakie Kim <rakie.kim@sk.com>, Byungchul Park <byungchul@sk.com>,
Gregory Price <gourry@gourry.net>,
Ying Huang <ying.huang@linux.alibaba.com>,
Alistair Popple <apopple@nvidia.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>,
Chao Peng <chao.p.peng@linux.intel.com>,
Nikunj A Dadhania <nikunj@amd.com>,
Ira Weiny <ira.weiny@intel.com>,
Michael Roth <michael.roth@amd.com>,
Pankaj Gupta <pankaj.gupta@amd.com>,
Ackerley Tng <ackerleytng@google.com>,
Fuad Tabba <tabba@google.com>,
Sean Christopherson <seanjc@google.com>,
Vishal Annapurve <vannapurve@google.com>,
Nikita Kalyazin <nikita.kalyazin@linux.dev>,
Patrick Roy <patrick.roy@linux.dev>,
Pratik Sampat <prsampat@amd.com>,
Ashish Kalra <Ashish.Kalra@amd.com>
Cc: linux-fsdevel@vger.kernel.org, linux-coco@lists.linux.dev,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH RFC 2/3] KVM: guest_memfd: support folio migration for non-confidential VMs
Date: Mon, 15 Jun 2026 20:35:21 +0200 [thread overview]
Message-ID: <1f74f3b9-fda5-40e1-9170-5861ebac9ca8@kernel.org> (raw)
In-Reply-To: <20260611-shivank-gmem-migrate-v1-2-2d266bfc6f95@amd.com>
On 6/11/26 15:05, Shivank Garg wrote:
> guest_memfd folios are currently marked unmmovable, so the kernel
> cannot perform NUMA-balancing, memory compaction, etc.
> This is unavoidable for confidential VMs (SEV-SNP, TDX),
> since memory is encrypted and copying it need firmware assistance.
> However, for non-cofidential VMs (like firecracker), we can migrate
> the folios.
>
> Mark non-confidential VMs as movable and implement
> kvm_gmem_migrate_folio() using filemap_migrate_folio().
>
> This lays the ground work for migrating cofidential guest_memfd
> later. Once the firmware-assisted copying support is available,
> those VMs can be made movable. The confidential folio content can
> be copied separately, and the destination folio can be marked with
> FOLIO_CONTENT_COPIED so __migrate_folio() skips the host-side
> folio_mc_copy().
>
> Signed-off-by: Shivank Garg <shivankg@amd.com>
> ---
> virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 45 insertions(+), 5 deletions(-)
>
> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index 806a42f0e031a1c7729f53c786316d2502532553..e4470106fc7792f328bce5275419683328c8b4ab 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c
> @@ -487,13 +487,45 @@ static struct file_operations kvm_gmem_fops = {
> .fallocate = kvm_gmem_fallocate,
> };
>
> +#ifdef CONFIG_MIGRATION
> static int kvm_gmem_migrate_folio(struct address_space *mapping,
> struct folio *dst, struct folio *src,
> enum migrate_mode mode)
> {
> - WARN_ON_ONCE(1);
> - return -EINVAL;
> + struct inode *inode = mapping->host;
> + pgoff_t start, end;
> + int ret;
> +
> + if (!filemap_invalidate_trylock_shared(mapping))
> + return -EAGAIN;
> +
> + start = src->index;
> + end = start + folio_nr_pages(src);
> +
> + kvm_gmem_invalidate_begin(inode, start, end);
> +
> + /*
> + * For non-confidential guest_memfd the folio is host-readable,
> + * so filemap_migrate_folio() can copy the contents itself via
> + * folio_mc_copy().
> + *
> + * This is also the hook point for confidential VMs (SEV-SNP, TDX) once
> + * they are made movable: the host cannot copy encrypted/private memory,
> + * so a firmware-assisted copy would run here.
> + * Idea: https://lore.kernel.org/r/20260428155043.39251-8-shivankg@amd.com
> + * Mark the @dst->migrate_info field with FOLIO_CONTENT_COPIED, so
> + * __migrate_folio() skip folio_mc_copy() for confidential VMs.
> + */
> + ret = filemap_migrate_folio(mapping, dst, src, mode);
> +
> + kvm_gmem_invalidate_end(inode, start, end);
> +
> + filemap_invalidate_unlock_shared(mapping);
> + return ret;
> }
> +#else
> +#define kvm_gmem_migrate_folio NULL
> +#endif
>
> static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folio)
> {
> @@ -592,9 +624,17 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
> inode->i_size = size;
> mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
> mapping_set_inaccessible(inode->i_mapping);
> - mapping_set_unmovable(inode->i_mapping);
> - /* Unmovable mappings are supposed to be marked unevictable as well. */
> - WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping));
> +
> + /*
> + * Confidential VMs (SEV-SNP, TDX) bind encryption to the physical
> + * address and require firmware assisted copy, so their folios cannot
> + * be migrated yet.
> + */
> + if (kvm_arch_has_private_mem(kvm)) {
> + mapping_set_unmovable(inode->i_mapping);
> + /* Unmovable mappings are supposed to be marked unevictable as well. */
> + WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping));
We would still want our movable mappings to be flagged unevictable.
> + }
>
As discussed, for guest_memfd instances that support page migration, we would
want to also allocate the pages in for guest_memfd as GFP_HIGHUSER_MOVABLE.
That is, handle the mapping_set_gfp_mask() call as well.
It will unlock access to areas reserved for movable allocations (CMA/
ZONE_MOVABLE) and properly let the page allocator group pages by mobility
(MOVABLE vs. UNMOVABLE vs. RECLAIMABLE).
--
Cheers,
David
next prev parent reply other threads:[~2026-06-15 18:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-11 13:05 [PATCH RFC 0/3] KVM: guest_memfd: " Shivank Garg
2026-06-11 13:05 ` [PATCH RFC 1/3] mm: split AS_UNMOVABLE back out of AS_INACCESSIBLE Shivank Garg
2026-06-11 13:05 ` [PATCH RFC 2/3] KVM: guest_memfd: support folio migration for non-confidential VMs Shivank Garg
2026-06-15 18:35 ` David Hildenbrand (Arm) [this message]
2026-06-11 13:05 ` [PATCH RFC 3/3] KVM: selftests: exercise guest_memfd folio migration Shivank Garg
2026-06-15 10:43 ` [PATCH RFC 0/3] KVM: guest_memfd: folio migration for non-confidential VMs Alexandru Elisei
2026-06-15 11:04 ` Alexandru Elisei
2026-06-15 17:39 ` Sean Christopherson
2026-06-15 18:24 ` David Hildenbrand (Arm)
2026-06-16 18:09 ` Ackerley Tng
2026-06-17 10:34 ` David Hildenbrand (Arm)
2026-06-17 10:17 ` Garg, Shivank
2026-06-15 18:30 ` David Hildenbrand (Arm)
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=1f74f3b9-fda5-40e1-9170-5861ebac9ca8@kernel.org \
--to=david@kernel.org \
--cc=Ashish.Kalra@amd.com \
--cc=ackerleytng@google.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=byungchul@sk.com \
--cc=chao.p.peng@linux.intel.com \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=ira.weiny@intel.com \
--cc=jack@suse.cz \
--cc=jackmanb@google.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=matthew.brost@intel.com \
--cc=mhocko@suse.com \
--cc=michael.roth@amd.com \
--cc=nikita.kalyazin@linux.dev \
--cc=nikunj@amd.com \
--cc=pankaj.gupta@amd.com \
--cc=patrick.roy@linux.dev \
--cc=pbonzini@redhat.com \
--cc=prsampat@amd.com \
--cc=rakie.kim@sk.com \
--cc=seanjc@google.com \
--cc=shivankg@amd.com \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=tabba@google.com \
--cc=vannapurve@google.com \
--cc=vbabka@kernel.org \
--cc=willy@infradead.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.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®