[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 18/26] kvm: Introduce support for memory_attributes
From: |
Paolo Bonzini |
Subject: |
[PATCH 18/26] kvm: Introduce support for memory_attributes |
Date: |
Fri, 22 Mar 2024 19:11:08 +0100 |
From: Xiaoyao Li <xiaoyao.li@intel.com>
Introduce the helper functions to set the attributes of a range of
memory to private or shared.
This is necessary to notify KVM the private/shared attribute of each gpa
range. KVM needs the information to decide the GPA needs to be mapped at
hva-based shared memory or guest_memfd based private memory.
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Message-ID: <20240320083945.991426-11-michael.roth@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/sysemu/kvm.h | 4 ++++
accel/kvm/kvm-all.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
index 54f4d83a370..bda309d5ffa 100644
--- a/include/sysemu/kvm.h
+++ b/include/sysemu/kvm.h
@@ -536,4 +536,8 @@ void kvm_mark_guest_state_protected(void);
* reported for the VM.
*/
bool kvm_hwpoisoned_mem(void);
+
+int kvm_set_memory_attributes_private(hwaddr start, hwaddr size);
+int kvm_set_memory_attributes_shared(hwaddr start, hwaddr size);
+
#endif
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 4ac3cf1c9ef..36e39fd6514 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -91,6 +91,7 @@ bool kvm_msi_use_devid;
static bool kvm_has_guest_debug;
static int kvm_sstep_flags;
static bool kvm_immediate_exit;
+static uint64_t kvm_supported_memory_attributes;
static hwaddr kvm_max_slot_size = ~0;
static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -1266,6 +1267,35 @@ void kvm_set_max_memslot_size(hwaddr max_slot_size)
kvm_max_slot_size = max_slot_size;
}
+static int kvm_set_memory_attributes(hwaddr start, hwaddr size, uint64_t attr)
+{
+ struct kvm_memory_attributes attrs;
+ int r;
+
+ assert((attr & kvm_supported_memory_attributes) == attr);
+ attrs.attributes = attr;
+ attrs.address = start;
+ attrs.size = size;
+ attrs.flags = 0;
+
+ r = kvm_vm_ioctl(kvm_state, KVM_SET_MEMORY_ATTRIBUTES, &attrs);
+ if (r) {
+ error_report("failed to set memory (0x%lx+%#zx) with attr 0x%lx error
'%s'",
+ start, size, attr, strerror(errno));
+ }
+ return r;
+}
+
+int kvm_set_memory_attributes_private(hwaddr start, hwaddr size)
+{
+ return kvm_set_memory_attributes(start, size,
KVM_MEMORY_ATTRIBUTE_PRIVATE);
+}
+
+int kvm_set_memory_attributes_shared(hwaddr start, hwaddr size)
+{
+ return kvm_set_memory_attributes(start, size, 0);
+}
+
/* Called with KVMMemoryListener.slots_lock held */
static void kvm_set_phys_mem(KVMMemoryListener *kml,
MemoryRegionSection *section, bool add)
@@ -2382,6 +2412,7 @@ static int kvm_init(MachineState *ms)
goto err;
}
+ kvm_supported_memory_attributes = kvm_check_extension(s,
KVM_CAP_MEMORY_ATTRIBUTES);
kvm_immediate_exit = kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT);
s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
--
2.44.0
- Re: [PATCH 11/26] runstate: skip initial CPU reset if reset is not actually possible, (continued)
- [PATCH 09/26] [HACK] linux-headers: Update headers for 6.8 + kvm-coco-queue + SNP, Paolo Bonzini, 2024/03/22
- [PATCH 12/26] KVM: track whether guest state is encrypted, Paolo Bonzini, 2024/03/22
- [PATCH 13/26] KVM: remove kvm_arch_cpu_check_are_resettable, Paolo Bonzini, 2024/03/22
- [PATCH 14/26] target/i386: introduce x86-confidential-guest, Paolo Bonzini, 2024/03/22
- [PATCH 16/26] target/i386: SEV: use KVM_SEV_INIT2 if possible, Paolo Bonzini, 2024/03/22
- [PATCH 17/26] trace/kvm: Split address space and slot id in trace_kvm_set_user_memory(), Paolo Bonzini, 2024/03/22
- [PATCH 20/26] kvm: Enable KVM_SET_USER_MEMORY_REGION2 for memslot, Paolo Bonzini, 2024/03/22
- [PATCH 18/26] kvm: Introduce support for memory_attributes,
Paolo Bonzini <=
- [PATCH 19/26] RAMBlock: Add support of KVM private guest memfd, Paolo Bonzini, 2024/03/22
- [PATCH 15/26] target/i386: Implement mc->kvm_type() to get VM type, Paolo Bonzini, 2024/03/22
- [PATCH 21/26] kvm/memory: Make memory type private by default if it has guest memfd backend, Paolo Bonzini, 2024/03/22
- [PATCH 22/26] HostMem: Add mechanism to opt in kvm guest memfd via MachineState, Paolo Bonzini, 2024/03/22
- [PATCH 23/26] RAMBlock: make guest_memfd require uncoordinated discard, Paolo Bonzini, 2024/03/22