HostMem: Add mechanism to opt in kvm guest memfd via MachineState

Add a new member "guest_memfd" to memory backends. When it's set
to true, it enables RAM_GUEST_MEMFD in ram_flags, thus private kvm
guest_memfd will be allocated during RAMBlock allocation.

Memory backend's @guest_memfd is wired with @require_guest_memfd
field of MachineState. It avoid looking up the machine in phymem.c.

MachineState::require_guest_memfd is supposed to be set by any VMs
that requires KVM guest memfd as private memory, e.g., TDX VM.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Message-ID: <20240320083945.991426-8-michael.roth@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Xiaoyao Li 2024-03-20 03:39:03 -05:00 committed by Paolo Bonzini
parent bd3bcf6962
commit 37662d85b0
7 changed files with 12 additions and 0 deletions

View File

@ -85,6 +85,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
ram_flags |= fb->readonly ? RAM_READONLY_FD : 0; ram_flags |= fb->readonly ? RAM_READONLY_FD : 0;
ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0; ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
ram_flags |= fb->is_pmem ? RAM_PMEM : 0; ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
ram_flags |= RAM_NAMED_FILE; ram_flags |= RAM_NAMED_FILE;
return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,

View File

@ -55,6 +55,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
name = host_memory_backend_get_name(backend); name = host_memory_backend_get_name(backend);
ram_flags = backend->share ? RAM_SHARED : 0; ram_flags = backend->share ? RAM_SHARED : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name, return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
backend->size, ram_flags, fd, 0, errp); backend->size, ram_flags, fd, 0, errp);
} }

View File

@ -30,6 +30,7 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
name = host_memory_backend_get_name(backend); name = host_memory_backend_get_name(backend);
ram_flags = backend->share ? RAM_SHARED : 0; ram_flags = backend->share ? RAM_SHARED : 0;
ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend), return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend),
name, backend->size, name, backend->size,
ram_flags, errp); ram_flags, errp);

View File

@ -277,6 +277,7 @@ static void host_memory_backend_init(Object *obj)
/* TODO: convert access to globals to compat properties */ /* TODO: convert access to globals to compat properties */
backend->merge = machine_mem_merge(machine); backend->merge = machine_mem_merge(machine);
backend->dump = machine_dump_guest_core(machine); backend->dump = machine_dump_guest_core(machine);
backend->guest_memfd = machine_require_guest_memfd(machine);
backend->reserve = true; backend->reserve = true;
backend->prealloc_threads = machine->smp.cpus; backend->prealloc_threads = machine->smp.cpus;
} }

View File

@ -1201,6 +1201,11 @@ bool machine_mem_merge(MachineState *machine)
return machine->mem_merge; return machine->mem_merge;
} }
bool machine_require_guest_memfd(MachineState *machine)
{
return machine->require_guest_memfd;
}
static char *cpu_slot_to_string(const CPUArchId *cpu) static char *cpu_slot_to_string(const CPUArchId *cpu)
{ {
GString *s = g_string_new(NULL); GString *s = g_string_new(NULL);

View File

@ -36,6 +36,7 @@ bool machine_usb(MachineState *machine);
int machine_phandle_start(MachineState *machine); int machine_phandle_start(MachineState *machine);
bool machine_dump_guest_core(MachineState *machine); bool machine_dump_guest_core(MachineState *machine);
bool machine_mem_merge(MachineState *machine); bool machine_mem_merge(MachineState *machine);
bool machine_require_guest_memfd(MachineState *machine);
HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine); HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
void machine_set_cpu_numa_node(MachineState *machine, void machine_set_cpu_numa_node(MachineState *machine,
const CpuInstanceProperties *props, const CpuInstanceProperties *props,
@ -370,6 +371,7 @@ struct MachineState {
char *dt_compatible; char *dt_compatible;
bool dump_guest_core; bool dump_guest_core;
bool mem_merge; bool mem_merge;
bool require_guest_memfd;
bool usb; bool usb;
bool usb_disabled; bool usb_disabled;
char *firmware; char *firmware;

View File

@ -74,6 +74,7 @@ struct HostMemoryBackend {
uint64_t size; uint64_t size;
bool merge, dump, use_canonical_path; bool merge, dump, use_canonical_path;
bool prealloc, is_mapped, share, reserve; bool prealloc, is_mapped, share, reserve;
bool guest_memfd;
uint32_t prealloc_threads; uint32_t prealloc_threads;
ThreadContext *prealloc_context; ThreadContext *prealloc_context;
DECLARE_BITMAP(host_nodes, MAX_NODES + 1); DECLARE_BITMAP(host_nodes, MAX_NODES + 1);