Currently, when setting memory attributes, KVM provides no guarantees about the memory contents.
Introduce default handlers for applying memory content modes, which different architectures should override. These handlers will be used later to apply memory content modes during set memory attributes requests. Signed-off-by: Ackerley Tng <[email protected]> --- include/linux/kvm_host.h | 9 +++++++++ virt/kvm/guest_memfd.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1ea14c66fc82e..bcb81e871fd37 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -740,6 +740,15 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm) return flags; } + +u64 kvm_arch_gmem_supported_content_modes(struct kvm *kvm); +int kvm_gmem_apply_content_mode_zero(struct folio *folio); +int kvm_arch_gmem_apply_content_mode_zero(struct kvm *kvm, + struct folio *folio); +int kvm_arch_gmem_apply_content_mode_preserve(struct kvm *kvm, + struct folio *folio); +int kvm_arch_gmem_apply_content_mode_unspecified(struct kvm *kvm, + struct folio *folio); #endif #ifndef kvm_arch_has_readonly_mem diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index c4f6bdad6289e..f23acbca28e54 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -638,6 +638,42 @@ static int kvm_gmem_mas_preallocate(struct ma_state *mas, u64 attributes, return mas_preallocate(mas, xa_mk_value(attributes), GFP_KERNEL); } +u64 __weak kvm_arch_gmem_supported_content_modes(struct kvm *kvm) +{ + /* Architectures must override with supported modes. */ + return 0; +} + +static u64 kvm_gmem_supported_content_modes(struct kvm *kvm) +{ + return kvm_arch_gmem_supported_content_modes(kvm); +} + +int kvm_gmem_apply_content_mode_zero(struct folio *folio) +{ + folio_zero_segment(folio, 0, folio_size(folio)); + + return 0; +} + +int __weak kvm_arch_gmem_apply_content_mode_unspecified(struct kvm *kvm, + struct folio *folio) +{ + return 0; +} + +int __weak kvm_arch_gmem_apply_content_mode_zero(struct kvm *kvm, + struct folio *folio) +{ + return kvm_gmem_apply_content_mode_zero(folio); +} + +int __weak kvm_arch_gmem_apply_content_mode_preserve(struct kvm *kvm, + struct folio *folio) +{ + return -EOPNOTSUPP; +} + static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, size_t nr_pages, uint64_t attrs, pgoff_t *err_index) -- 2.53.0.851.ga537e3e6e9-goog
