v13 2026-09-11

guest_memfd就地转换 · v13

KVM guest_memfd 系列优化转换开销,重命名内存属性 API,并按架构支持枚举私有内存。

原始补丁链接

子 Patch(44)

1/44
KVM: guest_memfd: Optimize away conversion overheads via dead-code elimination
新增 kvm_arch_has_gmem_convert() 条件调用 arch 转换钩子,靠死代码消除省去未编译架构开销,为原地转换减少 guest_memfd 元数据开销。
2/44
KVM: guest_memfd: Use kvm_mem_is_private() when populating guest_memfd memory
在 kvm_gmem_populate 中用 kvm_mem_is_private() 替代手写属性判断,简化代码,避免后续为 gmem 区间查询另加 API。
3/44
KVM: guest_memfd: Introduce per-gmem attributes, use to guard user mappings
用受 filemap_invalidate_lock 保护的 maple tree 记录 guest_memfd 每页私有/共享属性,替换 per-VM 判定,并在 kvm_gmem_get_pfn 提前 folio_put 支持原地转换。
4/44
KVM: Rename KVM_GENERIC_MEMORY_ATTRIBUTES to KVM_VM_MEMORY_ATTRIBUTES
将 KVM_GENERIC_MEMORY_ATTRIBUTES 重命名为 KVM_VM_MEMORY_ATTRIBUTES,明确它只针对 per-VM 属性,为后续 guest_memfd 独立属性解耦 Kconfig。
5/44
KVM: Enumerate support for PRIVATE memory iff kvm_arch_has_private_mem is defined
用 kvm_arch_has_private_mem 是否定义决定是否上报 KVM_MEMORY_ATTRIBUTE_PRIVATE,解除与 KVM_VM_MEMORY_ATTRIBUTES 耦合,避免 arm64 误报并准备 per-gmem 属性。
6/44
KVM: Rename memory attribute APIs to prepare for in-place gmem conversion
把 VM 级内存属性 API 统一改名为 kvm_vm 并同步 trace,区分未来 guest_memfd 属性来源,为原地私有/共享转换做准备。
7/44
KVM: Rename kvm_mem_is_private() to kvm_is_private_gfn()
把 kvm_mem_is_private() 改为 kvm_is_private_gfn(),统一 vm/gmem 前缀,便于后续同时存在 VM 与 guest_memfd 两种私有 GFN 查询。
8/44
KVM: Provide generic interface for checking memory private/shared status
用 static_call 引入通用 kvm_is_private_gfn(),运行时可在 VM 属性与 guest_memfd 查询间切换,原检查改名 kvm_vm_is_private_gfn。
9/44
KVM: guest_memfd: Stub in ability to enable in-place shared<=>private conversion
为 guest_memfd 原地私有/共享转换加入全局开关桩,开启后把 __kvm_is_private_gfn 静态调用指向 gmem 查询,使 gmem 成为私有状态唯一权威。
10/44
KVM: Consolidate private memory and guest_memfd ifdeffery in kvm_host.h
将 kvm_arch_has_private_mem() 桩与 guest_memfd 声明移到 kvm_host.h 现有 #ifdef 区域,减少重复条件编译并聚集私有内存相关定义。
11/44
KVM: guest_memfd: Invalidate both SHARED and PRIVATE mappings for in-place conversions
原地转换开启时,移除 guest_memfd folio 时同时失效共享和私有映射,并让 helper 返回映射过滤条件,避免 PUNCH_HOLE 误 zapping 无关共享映射。
12/44
KVM: guest_memfd: Always fault from guest_memfd if in-place conversion is enabled
in-place 转换开启时设 KVM_MEMSLOT_GMEM_ONLY,使客户机共享/私有页都从 guest_memfd 取,简化 stage2 失效;userspace_addr 仅作普通访问。
13/44
KVM: guest_memfd: Pass mapping type filter to invalidation helper
给 guest_memfd 失效辅助函数增加映射类型过滤参数,由调用者指定 shared/private/both,为转换时只 zap 目标状态不同的映射做准备。
14/44
KVM: guest_memfd: Add base support for KVM_SET_MEMORY_ATTRIBUTES2
新增 KVM_SET_MEMORY_ATTRIBUTES2 与更大 attributes2 结构,允许内核写回失败 offset 以支持部分原地转换,并用新 capability 通告。
15/44
KVM: guest_memfd: Ensure pages are not in use before conversion
转换前检查 guest_memfd 页引用,先排空 lru_add 与 mlock fbatch 消除已知伪引用,若仍有 DMA pin 等额外引用则拒绝转换,防止用户态继续写私有内存。
16/44
KVM: guest_memfd: Call arch make_shared callback for to-shared conversion
私有转共享时在失效保护下立即调用 arch make_shared 更新硬件元数据;共享转私有留到取 PFN 时按需处理,避免遍历 memslot 并省去无回调开销。
17/44
KVM: guest_memfd: Return early if range already has requested attributes
新增区间属性检查,在设置内存属性时若请求范围已全为目标属性就提前返回,减少无意义的转换与失效工作。
18/44
KVM: guest_memfd: Zero page while getting pfn
把 zeroing/初始化移入 __kvm_gmem_get_pfn,使 populate 路径也先清零,防止原地转换将未初始化宿主页暴露给客户机;用 uptodate 保证只清零一次。
19/44
KVM: SEV: Make 'uaddr' parameter optional for KVM_SEV_SNP_LAUNCH_UPDATE
允许 KVM_SEV_SNP_LAUNCH_UPDATE 在 in-place 模式下 uaddr 为 NULL,直接从 guest_memfd 转换已有数据;per-VM 私有跟踪时仍强制提供源地址。
20/44
KVM: TDX: Make source page optional for KVM_TDX_INIT_MEM_REGION
From: Ackerley Tng ackerleytng@google.com Update tdx_gmem_post_populate() to handle cases where userspace requests “no source page”. To handle “no source page”, populate (perform TDH.MEM.PAGE.ADD)
21/44
KVM: Move KVM_VM_MEMORY_ATTRIBUTES config definition to x86
From: Sean Christopherson seanjc@google.com Bury KVM_VM_MEMORY_ATTRIBUTES in x86 to discourage other architectures from adding support for per-VM memory attributes, because tracking private vs. sha
22/44
KVM: Let userspace disable per-VM mem attributes, enable per-gmem attributes
From: Sean Christopherson seanjc@google.com Allow the user to disable KVM_VM_MEMORY_ATTRIBUTES even when KVM supports PRIVATE and SHARED attributes, and expose gmem_in_place_conversion as a module
23/44
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
24/44
KVM: selftests: Create gmem fd before "regular" fd when adding memslot
From: Sean Christopherson seanjc@google.com When adding a memslot associated with a guest_memfd instance, create/dup the guest_memfd before creating the “normal” backing file. This will allow dup'
25/44
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
26/44
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
27/44
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
28/44
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_
29/44
KVM: selftests: Test basic single-page conversion flow
From: Ackerley Tng ackerleytng@google.com Add a selftest for the guest_memfd memory attribute conversion ioctls. The test starts the guest_memfd as all-private (the default state), and verifies the
30/44
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
31/44
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
32/44
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
33/44
KVM: selftests: Convert with allocated folios in different layouts
[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 alloc
34/44
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
35/44
KVM: selftests: Test that shared/private status is consistent across processes
From: Sean Christopherson seanjc@google.com Add a test to verify that a guest_memfd’s shared/private status is consistent across processes, and that any shared pages previously mapped in any proces
36/44
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 creating test scenarios where some page has an increase
37/44
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
38/44
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
39/44
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 (
40/44
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
41/44
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
42/44
KVM: selftests: Set up page size and alignment independently for guest_memfd
From: Ackerley Tng ackerleytng@google.com Currently, vm_mem_add derives the backing source page size, alignment padding, and mmap size from the backing source type upfront before checking if guest_
43/44
KVM: selftests: Update private_mem_conversions_test for in-place conversions
From: Ackerley Tng ackerleytng@google.com Update private_mem_conversions_test for in-place conversions. In-place conversions support is detected in selftests with kvm_has_gmem_attributes. With in-
44/44
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