From dded44bcfd81401485c28c1adfe67415ea2fad2f Mon Sep 17 00:00:00 2001 From: Quentin Perret Date: Tue, 26 Oct 2021 12:07:14 +0100 Subject: [PATCH] ANDROID: BACKPORT: KVM: arm64: Use guest VMID as owner id We currently track page-ownership in nVHE protected mode with a rather coarse granularity -- all guests share a unique owner id. But a finer grain tracking will be useful soon, to e.g. handle host stage-2 faults caused by an access to guest memory. To prepare the ground for this, let's use the guest VMIDs as owner ids, hence allowing to distinguish between all of them. This only works since the pKVM EL2 hypervisor guarantees the stability of the VMIDs for the entire lifetime of a guest VM. This will need some rework when/if we attempt to run more than 255 guests concurrently in protected mode as we'll have to handle VMID rollovers, but there is no clear need for now, so let's keep it simple to start. Signed-off-by: Quentin Perret [willdeacon@: Update constants in mem_protect.h] Bug: 209580772 Change-Id: I5c5c8061617d7dc481ae5e25a0391b306aabbd8c Signed-off-by: Will Deacon --- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 3 +-- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index fa83c2fa7b9d..e6f93ac38b6a 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -53,8 +53,7 @@ extern struct host_kvm host_kvm; typedef u32 pkvm_id; static const pkvm_id pkvm_host_id = 0; -static const pkvm_id pkvm_hyp_id = 1; -static const pkvm_id pkvm_guest_id = 2; +static const pkvm_id pkvm_hyp_id = (1 << 16); extern unsigned long hyp_nr_cpus; diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 062258f91fa3..da75c35ef4e8 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -29,6 +29,12 @@ struct host_kvm host_kvm; static struct hyp_pool host_s2_pool; +static pkvm_id pkvm_guest_id(struct kvm_vcpu *vcpu) +{ + return vcpu->arch.hw_mmu->vmid.vmid; + +} + static DEFINE_PER_CPU(struct kvm_shadow_vm *, __current_vm); #define current_vm (*this_cpu_ptr(&__current_vm)) @@ -687,6 +693,8 @@ static pkvm_id completer_owner_id(const struct pkvm_mem_transition *tx) return pkvm_host_id; case PKVM_ID_HYP: return pkvm_hyp_id; + case PKVM_ID_GUEST: + return pkvm_guest_id(tx->completer.guest.vcpu); default: WARN_ON(1); return -1;