v10
2026-08-08
guest_memfd就地转换 · v10
KVM guest_memfd 私有内存补丁系列 v10(41 个):加入 per-gmem 属性、in-place private/shared 转换及通用检查接口。
原始补丁链接
子 Patch(41)
1/41
KVM: guest_memfd: Use kvm_mem_is_private() when populating guest_memfd memory
guest_memfd populate 路径改用 kvm_mem_is_private() 替代手写等价判断,简化当前逻辑并避免日后引入 range-based gmem 查找 API;无功能变化。
2/41
KVM: guest_memfd: Introduce per-gmem attributes, use to guard user mappings
为 guest_memfd 引入 per-inode 的 maple tree 属性跟踪,支持 private/shared 原位转换;用 filemap invalidation lock 保护,避免新增锁带来的死锁复杂度。
3/41
KVM: Rename KVM_GENERIC_MEMORY_ATTRIBUTES to KVM_VM_MEMORY_ATTRIBUTES
将 KVM_GENERIC_MEMORY_ATTRIBUTES 改名为 KVM_VM_MEMORY_ATTRIBUTES,明确其表示 per-VM 属性,为后续 guest_memfd 支持内存属性做准备;纯改名无功能变化。
4/41
KVM: Enumerate support for PRIVATE memory iff kvm_arch_has_private_mem is defined
仅当架构定义 kvm_arch_has_private_mem() 时才上报 KVM_MEMORY_ATTRIBUTE_PRIVATE,避免 guest_memfd 属性通用化后 arm64 等系统级误报;与 CONFIG_KVM_VM_MEMORY_ATTRIBUTES 解耦。
5/41
KVM: Rename memory attribute APIs to prepare for in-place gmem conversion
重命名内存属性 API,加入 vm_ 前缀以便与后续 guest_memfd 属性区分;kvm_vm_set_mem_attributes() 改为 kvm_set_vm_mem_attributes(),trace 函数同步更新;无功能变化。
6/41
KVM: Provide generic interface for checking memory private/shared status
新增 kvm_mem_is_private() 静态调用接口,运行时选择 guest_memfd 或 VM 属性实现;原基于 VM 属性的检查改名 kvm_vm_mem_is_private(),为后续实现选择做准备。
7/41
KVM: guest_memfd: Stub in ability to enable in-place shared<=>private conversion
加入 gmem_in_place_conversion 全局开关(后续暴露为模块参数),启用时把 __kvm_mem_is_private 静态调用接到 guest_memfd 版本,使 gmem 成为 private/shared 唯一判定源。
8/41
KVM: Consolidate private memory and guest_memfd ifdeffery in kvm_host.h
整理 kvm_host.h 中 kvm_arch_has_private_mem 桩和 guest_memfd 声明,复用现有 #ifdef 使相关代码聚集;无功能变化。
9/41
KVM: guest_memfd: Filter both shared and private when invalidating
guest_memfd 失效时同时设置 shared/private 过滤位,因为转换后同一区间可能混合两种页,只按初始状态过滤会漏;让 zapping 逻辑自行遍历属性,简化处理。
10/41
KVM: guest_memfd: Add base support for KVM_SET_MEMORY_ATTRIBUTES2
新增 KVM_SET_MEMORY_ATTRIBUTES2 ioctl,KVM 对 payload 改为可写以支持部分/增量转换,出错时记录失败偏移;新结构带 padding,并引入 KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES 通告。
11/41
KVM: guest_memfd: Ensure pages are not in use before conversion
On Fri, Aug 07, 2026 at 02:52:50PM -0700, Ackerley Tng via B4 Relay wrote: > From: Ackerley Tng ackerleytng@google.com > > When converting memory to private in guest_memfd, it is necessary to ensur
12/41
KVM: guest_memfd: Call arch make_shared callback for to-shared conversion
private 转 shared 时遍历受影响 folio,调用 kvm_arch_gmem_make_shared() 清理平台私有状态/加密元数据;在 MMU 失效开始后调用以阻止并发页故障;不实现 make-private,因 SNP 偏好 fault 时再 private。
13/41
KVM: guest_memfd: Return early if range already has requested attributes
新增区间属性检查函数,若目标区间所有页已具有请求属性则提前返回,避免多余转换和 unmap 操作;优化 KVM_SET_MEMORY_ATTRIBUTES2 路径。
14/41
mm/gup: factor out LRU cache draining for folio into lru_cache_drain_for_folio()
将 LRU cache 冲刷逻辑抽取为 lru_cache_drain_for_folio(),供 guest_memfd 转换前移除 per-CPU LRU cache 中的额外 folio 引用;保留两次预期 refcount 检查以降低并发漏检风险。
15/41
KVM: guest_memfd: Handle lru_add fbatch refcounts during conversion safety check
转换安全检查中处理 lru_add fbatch 导致的额外 refcount:先排空 per-CPU fbatch 再判断是否仅剩 guest_memfd 引用;若已 DMA pinned 或仍映射则提前失败;引入 drain 状态避免重复排空。
16/41
KVM: guest_memfd: Zero page while getting pfn
将 folio 初始化/清零逻辑移入 __kvm_gmem_get_pfn(),使 gmem_populate 获取页面时也清零,避免未初始化主机内存泄漏给 guest;用 uptodate 保证页生命周期内最多清零一次。
17/41
KVM: SEV: Make 'uaddr' parameter optional for KVM_SEV_SNP_LAUNCH_UPDATE
KVM_SEV_SNP_LAUNCH_UPDATE 的 uaddr 在启用 in-place 转换时可为 NULL,表示直接从已写好的 guest_memfd 页原位填充;per-VM 属性模式仍强制非 NULL。
18/41
KVM: TDX: Make source page optional for KVM_TDX_INIT_MEM_REGION
TDX 的 init_mem_region 允许无源页:启用 gmem_in_place_conversion 时,tdx_gmem_post_populate 直接对目标 PFN 执行 TDH.MEM.PAGE.ADD 原位填充;否则拒绝,避免支持已废弃路径或隐式零页优化。
19/41
KVM: Move KVM_VM_MEMORY_ATTRIBUTES config definition to x86
将 KVM_VM_MEMORY_ATTRIBUTES Kconfig 移到 x86,明确 per-VM 属性仅限 x86/CoCo,阻止其他架构使用已弃用的 per-VM 追踪;公共代码保留便于回退。
20/41
KVM: Let userspace disable per-VM mem attributes, enable per-gmem attributes
将 gmem_in_place_conversion 暴露为模块参数(仅 CONFIG_KVM_VM_MEMORY_ATTRIBUTES=y 时),并让该 Kconfig 可被选择;vm_memory_attributes 参数设为只读防不一致,kvm_arch_has_private_mem 改由平台配置决定。
21/41
KVM: guest_memfd: Enable INIT_SHARED on guest_memfd for x86 Coco VMs
From: Sean Christopherson seanjc@google.com Now that guest_memfd supports tracking private vs. shared within gmem itself, allow userspace to specify INIT_SHARED on a guest_memfd instance for x86 Co
22/41
KVM: selftests: Create gmem fd before "regular" fd when adding memslot
selftests 添加 memslot 时先创建/dup guest_memfd,再创建普通 backing file,为将来 guest_memfd 支持 mmap 后让同一 fd 同时作为普通 fd 做准备;调整顺序无功能影响。
23/41
KVM: selftests: Rename guest_memfd{,_offset} to gmem_{fd,offset}
From: Sean Christopherson seanjc@google.com Rename local variables and function parameters for the guest memory file descriptor and its offset to use a “gmem_” prefix instead of “guest_memfd_”. No
24/41
KVM: selftests: Add support for mmap() on guest_memfd in core library
From: Sean Christopherson seanjc@google.com Accept gmem_flags in vm_mem_add() to be able to create a guest_memfd within vm_mem_add(). When vm_mem_add() is used to set up a guest_memfd for a memslo
25/41
KVM: selftests: Add selftests global for guest memory attributes capability
From: Sean Christopherson seanjc@google.com Add a global variable, kvm_has_gmem_attributes, to make the result of checking for KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES available to all tests. kvm_has
26/41
KVM: selftests: Add helpers for calling ioctls on guest_memfd
From: Sean Christopherson seanjc@google.com Add helper functions to kvm_util.h to support calling ioctls, specifically KVM_SET_MEMORY_ATTRIBUTES2, on a guest_memfd file descriptor. Introduce gmem_
27/41
KVM: selftests: Test basic single-page conversion flow
新增 x86 selftest 验证 guest_memfd 单页 private→shared→private 基础转换流程,并搭建可扩展的转换测试基础设施;使用 KVM_X86_SW_PROTECTED_VM 作为测试载体。
28/41
KVM: selftests: Test conversion flow when INIT_SHARED
From: Ackerley Tng ackerleytng@google.com Add a test case to verify that conversions between private and shared memory work correctly when the memory is initially created as shared. Co-developed-b
29/41
KVM: selftests: Test conversion precision in guest_memfd
From: Ackerley Tng ackerleytng@google.com The existing guest_memfd conversion tests only use single-page memory regions. This provides no coverage for multi-page guest_memfd objects, specifically w
30/41
KVM: selftests: Test conversion before allocation
From: Ackerley Tng ackerleytng@google.com Add two test cases to the guest_memfd conversions selftest to cover the scenario where a conversion is requested before any memory has been allocated in th
31/41
KVM: selftests: Convert with allocated folios in different layouts
From: Ackerley Tng ackerleytng@google.com Add a guest_memfd selftest to verify that memory conversions work correctly with allocated folios in different layouts. By iterating through which pages a
32/41
KVM: selftests: Test that truncation does not change shared/private status
From: Ackerley Tng ackerleytng@google.com Add a test to verify that deallocating a page in a guest memfd region via fallocate() with FALLOC_FL_PUNCH_HOLE does not alter the shared or private status
33/41
KVM: selftests: Test that shared/private status is consistent across processes
新增跨进程一致性测试:fork 子进程后两进程交替访问共享区间,验证 guest_memfd 的 shared/private 状态在所有进程一致,且某进程映射的 shared 页在转换后从所有进程 unmap。
34/41
KVM: selftests: Add helpers to pin pages with CONFIG_GUP_TEST
From: Ackerley Tng ackerleytng@google.com Add helper functions to allow KVM selftests to pin memory using CONFIG_GUP_TEST. This is useful for testing scenarios where some page has an increased refc
35/41
KVM: selftests: Test conversion with elevated page refcount
From: Ackerley Tng ackerleytng@google.com Add a selftest to verify that converting a shared guest_memfd page to a private page fails if the page has an elevated reference count. When KVM converts
36/41
KVM: selftests: Reset shared memory after hole-punching
From: Ackerley Tng ackerleytng@google.com private_mem_conversions_test used to reset the shared memory that was used for the test to an initial pattern at the end of each test iteration. Then, it w
37/41
KVM: selftests: Provide function to look up guest_memfd details from gpa
From: Ackerley Tng ackerleytng@google.com Introduce a new helper, kvm_gpa_to_guest_memfd(), to find the guest_memfd-related details of a memory region that contains a given guest physical address (
38/41
KVM: selftests: Provide common function to set memory attributes
From: Sean Christopherson seanjc@google.com Introduce vm_mem_set_memory_attributes(), which handles setting of memory attributes for a range of guest physical addresses, regardless of whether the a
39/41
KVM: selftests: Make TEST_EXPECT_SIGBUS thread-safe
From: Ackerley Tng ackerleytng@google.com The TEST_EXPECT_SIGBUS macro is not thread-safe as it uses a global sigjmp_buf and installs a global SIGBUS signal handler. If multiple threads execute the
40/41
KVM: selftests: Update private_mem_conversions_test to mmap() guest_memfd
From: Ackerley Tng ackerleytng@google.com Update the private memory conversions selftest to also test conversions that are done “in-place” via per-guest_memfd memory attributes. In-place conversion
41/41
KVM: selftests: Update private memory exits test to work with per-gmem attributes
From: Sean Christopherson seanjc@google.com Skip setting memory to private in the private memory exits test when using per-gmem memory attributes, as memory is initialized to private by default for