Re: [RFC 16/33] KVM: x86/mmu: Expose R/W/X flags during memory fault exits
From: Maxim Levitsky
Date: Tue Nov 28 2023 - 02:36:41 EST
On Wed, 2023-11-08 at 11:17 +0000, Nicolas Saenz Julienne wrote:
> Include the fault's read, write and execute status when exiting to
> user-space.
>
> Signed-off-by: Nicolas Saenz Julienne <nsaenz@xxxxxxxxxx>
> ---
> arch/x86/kvm/mmu/mmu.c | 4 ++--
> include/linux/kvm_host.h | 9 +++++++--
> include/uapi/linux/kvm.h | 6 ++++++
> 3 files changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 4e02d506cc25..feca077c0210 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -4300,8 +4300,8 @@ static inline u8 kvm_max_level_for_order(int order)
> static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
> struct kvm_page_fault *fault)
> {
> - kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT,
> - PAGE_SIZE, fault->write, fault->exec,
> + kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, PAGE_SIZE,
> + fault->write, fault->exec, fault->user,
> fault->is_private);
> }
>
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index 71e1e8cf8936..631fd532c97a 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -2367,14 +2367,19 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)
> static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
> gpa_t gpa, gpa_t size,
> bool is_write, bool is_exec,
> - bool is_private)
> + bool is_read, bool is_private)
It almost feels like there is a need for a struct to hold all of those parameters.
> {
> vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT;
> vcpu->run->memory_fault.gpa = gpa;
> vcpu->run->memory_fault.size = size;
>
> - /* RWX flags are not (yet) defined or communicated to userspace. */
> vcpu->run->memory_fault.flags = 0;
> + if (is_read)
> + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_READ;
> + if (is_write)
> + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_WRITE;
> + if (is_exec)
> + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_EXECUTE;
> if (is_private)
> vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE;
> }
> diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
> index 03f5c08fd7aa..0ddffb8b0c99 100644
> --- a/include/uapi/linux/kvm.h
> +++ b/include/uapi/linux/kvm.h
> @@ -533,7 +533,13 @@ struct kvm_run {
> } notify;
> /* KVM_EXIT_MEMORY_FAULT */
> struct {
> +#define KVM_MEMORY_EXIT_FLAG_READ (1ULL << 0)
> +#define KVM_MEMORY_EXIT_FLAG_WRITE (1ULL << 1)
> +#define KVM_MEMORY_EXIT_FLAG_EXECUTE (1ULL << 2)
> #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3)
> +#define KVM_MEMORY_EXIT_NO_ACCESS \
> + (KVM_MEMORY_EXIT_FLAG_NR | KVM_MEMORY_EXIT_FLAG_NW | \
> + KVM_MEMORY_EXIT_FLAG_NX)
> __u64 flags;
> __u64 gpa;
> __u64 size;
I don't think that KVM_MEMORY_EXIT_FLAG_NR, KVM_MEMORY_EXIT_FLAG_NW, KVM_MEMORY_EXIT_FLAG_NX are defined anywhere.
Also why KVM_MEMORY_EXIT_NO_ACCESS is needed - userspace can infer it from the lack of other access flags.
Best regards,
Maxim Levitsky