[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v3 10/70] kvm: handle KVM_EXIT_MEMORY_FAULT
|
From: |
Daniel P . Berrangé |
|
Subject: |
Re: [PATCH v3 10/70] kvm: handle KVM_EXIT_MEMORY_FAULT |
|
Date: |
Wed, 15 Nov 2023 10:42:24 +0000 |
|
User-agent: |
Mutt/2.2.10 (2023-03-25) |
On Wed, Nov 15, 2023 at 02:14:19AM -0500, Xiaoyao Li wrote:
> From: Chao Peng <chao.p.peng@linux.intel.com>
>
> Currently only KVM_MEMORY_EXIT_FLAG_PRIVATE in flags is valid when
> KVM_EXIT_MEMORY_FAULT happens. It indicates userspace needs to do
> the memory conversion on the RAMBlock to turn the memory into desired
> attribute, i.e., private/shared.
>
> Note, KVM_EXIT_MEMORY_FAULT makes sense only when the RAMBlock has
> guest_memfd memory backend.
>
> Note, KVM_EXIT_MEMORY_FAULT returns with -EFAULT, so special handling is
> added.
>
> Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
> Co-developed-by: Xiaoyao Li <xiaoyao.li@intel.com>
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
> accel/kvm/kvm-all.c | 76 +++++++++++++++++++++++++++++++++++++++------
> 1 file changed, 66 insertions(+), 10 deletions(-)
>
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 76e2404d54d2..58abbcb6926e 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -2902,6 +2902,50 @@ static void kvm_eat_signals(CPUState *cpu)
> } while (sigismember(&chkset, SIG_IPI));
> }
>
> +static int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
> +{
> + MemoryRegionSection section;
> + ram_addr_t offset;
> + RAMBlock *rb;
> + void *addr;
> + int ret = -1;
> +
> + section = memory_region_find(get_system_memory(), start, size);
> + if (!section.mr) {
> + return ret;
> + }
> +
> + if (memory_region_has_guest_memfd(section.mr)) {
> + if (to_private) {
> + ret = kvm_set_memory_attributes_private(start, size);
> + } else {
> + ret = kvm_set_memory_attributes_shared(start, size);
> + }
> +
> + if (ret) {
> + memory_region_unref(section.mr);
> + return ret;
> + }
> +
> + addr = memory_region_get_ram_ptr(section.mr) +
> + section.offset_within_region;
> + rb = qemu_ram_block_from_host(addr, false, &offset);
> + /*
> + * With KVM_SET_MEMORY_ATTRIBUTES by kvm_set_memory_attributes(),
> + * operation on underlying file descriptor is only for releasing
> + * unnecessary pages.
> + */
> + ram_block_convert_range(rb, offset, size, to_private);
> + } else {
> + warn_report("Convert non guest_memfd backed memory region "
> + "(0x%"HWADDR_PRIx" ,+ 0x%"HWADDR_PRIx") to %s",
> + start, size, to_private ? "private" : "shared");
Again, if you're returning '-1' to indicate error, then
using warn_report is wrong, it should be error_report.
warn_report is for when you return success, indicating
the problem was non-fatal.
> + }
> +
> + memory_region_unref(section.mr);
> + return ret;
> +}
> +
> int kvm_cpu_exec(CPUState *cpu)
> {
> struct kvm_run *run = cpu->kvm_run;
> @@ -2969,18 +3013,20 @@ int kvm_cpu_exec(CPUState *cpu)
> ret = EXCP_INTERRUPT;
> break;
> }
> - fprintf(stderr, "error: kvm run failed %s\n",
> - strerror(-run_ret));
> + if (!(run_ret == -EFAULT && run->exit_reason ==
> KVM_EXIT_MEMORY_FAULT)) {
> + fprintf(stderr, "error: kvm run failed %s\n",
> + strerror(-run_ret));
> #ifdef TARGET_PPC
> - if (run_ret == -EBUSY) {
> - fprintf(stderr,
> - "This is probably because your SMT is enabled.\n"
> - "VCPU can only run on primary threads with all "
> - "secondary threads offline.\n");
> - }
> + if (run_ret == -EBUSY) {
> + fprintf(stderr,
> + "This is probably because your SMT is enabled.\n"
> + "VCPU can only run on primary threads with all "
> + "secondary threads offline.\n");
> + }
> #endif
> - ret = -1;
> - break;
> + ret = -1;
> + break;
> + }
> }
>
> trace_kvm_run_exit(cpu->cpu_index, run->exit_reason);
> @@ -3067,6 +3113,16 @@ int kvm_cpu_exec(CPUState *cpu)
> break;
> }
> break;
> + case KVM_EXIT_MEMORY_FAULT:
> + if (run->memory_fault.flags & ~KVM_MEMORY_EXIT_FLAG_PRIVATE) {
> + error_report("KVM_EXIT_MEMORY_FAULT: Unknown flag 0x%"
> PRIx64,
> + (uint64_t)run->memory_fault.flags);
> + ret = -1;
> + break;
> + }
> + ret = kvm_convert_memory(run->memory_fault.gpa,
> run->memory_fault.size,
> + run->memory_fault.flags &
> KVM_MEMORY_EXIT_FLAG_PRIVATE);
> + break;
> default:
> DPRINTF("kvm_arch_handle_exit\n");
> ret = kvm_arch_handle_exit(cpu, run);
> --
> 2.34.1
>
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
- [PATCH v3 07/70] physmem: Relax the alignment check of host_startaddr in ram_block_discard_range(), (continued)
- [PATCH v3 07/70] physmem: Relax the alignment check of host_startaddr in ram_block_discard_range(), Xiaoyao Li, 2023/11/15
- [PATCH v3 09/70] physmem: Introduce ram_block_convert_range() for page conversion, Xiaoyao Li, 2023/11/15
- [PATCH v3 06/70] kvm: Introduce support for memory_attributes, Xiaoyao Li, 2023/11/15
- [PATCH v3 08/70] physmem: replace function name with __func__ in ram_block_discard_range(), Xiaoyao Li, 2023/11/15
- [PATCH v3 10/70] kvm: handle KVM_EXIT_MEMORY_FAULT, Xiaoyao Li, 2023/11/15
- Re: [PATCH v3 10/70] kvm: handle KVM_EXIT_MEMORY_FAULT,
Daniel P . Berrangé <=
- [PATCH v3 12/70] *** HACK *** linux-headers: Update headers to pull in TDX API changes, Xiaoyao Li, 2023/11/15
- [PATCH v3 13/70] i386: Introduce tdx-guest object, Xiaoyao Li, 2023/11/15
- [PATCH v3 14/70] target/i386: Implement mc->kvm_type() to get VM type, Xiaoyao Li, 2023/11/15
- [PATCH v3 15/70] target/i386: Parse TDX vm type, Xiaoyao Li, 2023/11/15
- [PATCH v3 11/70] trace/kvm: Add trace for page convertion between shared and private, Xiaoyao Li, 2023/11/15
- [PATCH v3 16/70] target/i386: Introduce kvm_confidential_guest_init(), Xiaoyao Li, 2023/11/15
- [PATCH v3 17/70] i386/tdx: Implement tdx_kvm_init() to initialize TDX VM context, Xiaoyao Li, 2023/11/15