Skip to content

RFC Reduce scope of vcpu state at hyp by refactoring out state hyp needs

Fuad Tabba kvm kvmarm linux-arm-kernel 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest30 patches 5 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/30KVM: arm64: placeholder to check if VM is protectedarch/arm64/include/asm/kvm_host.h+5 -01 reply
2/30Temporarily disable unused variable warningMakefile+1 -10 replies
3/30Coccinelle scripts for refactoringcocci_refactor/add_ctxt.cocci cocci_refactor/add_hypstate.cocci cocci_refactor/hyp_ctxt.cocci +12 more+870 -00 replies
4/30KVM: arm64: remove unused parameters and asm offsetsarch/arm64/include/asm/kvm_hyp.h arch/arm64/kernel/asm-offsets.c arch/arm64/kvm/hyp/nvhe/switch.c +1 more+7 -80 replies
5/30KVM: arm64: add accessors for kvm_cpu_contextarch/arm64/include/asm/kvm_emulate.h arch/arm64/include/asm/kvm_host.h arch/arm64/kvm/hyp/exception.c+94 -201 reply
6/30KVM: arm64: COCCI: use_ctxt_access.cocci: use kvm_cpu_context accessorsarch/arm64/kvm/fpsimd.c arch/arm64/kvm/guest.c arch/arm64/kvm/hyp/include/hyp/switch.h +2 more+30 -300 replies
7/30KVM: arm64: COCCI: add_ctxt.cocci use_ctxt.cocci: reduce scope of functions to kvm_cpu_ctxtarch/arm64/kvm/hyp/aarch32.c arch/arm64/kvm/hyp/exception.c arch/arm64/kvm/hyp/include/hyp/adjust_pc.h +7 more+155 -900 replies
8/30KVM: arm64: add hypervisor state accessorsarch/arm64/include/asm/kvm_host.h+7 -00 replies
9/30KVM: arm64: COCCI: vcpu_hyp_accessors.cocci: use accessors for hypervisor state vcpu variablesarch/arm64/include/asm/kvm_emulate.h arch/arm64/kvm/arm.c arch/arm64/kvm/debug.c +13 more+97 -970 replies
10/30KVM: arm64: Add accessors for hypervisor state in kvm_vcpu_archarch/arm64/include/asm/kvm_emulate.h arch/arm64/include/asm/kvm_host.h+181 -391 reply
11/30KVM: arm64: create and use a new vcpu_hyp_state structarch/arm64/include/asm/kvm_host.h arch/arm64/kernel/asm-offsets.c+21 -161 reply
12/30KVM: arm64: COCCI: add_hypstate.cocci use_hypstate.cocci: Reduce scope of functions to hyp_statearch/arm64/include/asm/kvm_hyp.h arch/arm64/kvm/hyp/aarch32.c arch/arm64/kvm/hyp/exception.c +8 more+112 -531 reply
13/30KVM: arm64: change function parameters to use kvm_cpu_ctxt and hyp_statearch/arm64/include/asm/kvm_emulate.h arch/arm64/kvm/hyp/aarch32.c arch/arm64/kvm/hyp/exception.c +6 more+39 -390 replies
14/30KVM: arm64: reduce scope of vgic v2arch/arm64/include/asm/kvm_hyp.h arch/arm64/kvm/hyp/include/hyp/switch.h arch/arm64/kvm/hyp/nvhe/switch.c +2 more+14 -150 replies
15/30KVM: arm64: COCCI: vgic3_cpu.cocci: reduce scope of vgic v3arch/arm64/kvm/hyp/vgic-v3-sr.c+137 -1100 replies
16/30KVM: arm64: reduce scope of vgic_v3 access parametersarch/arm64/include/asm/kvm_hyp.h arch/arm64/kvm/hyp/include/hyp/switch.h arch/arm64/kvm/hyp/vgic-v3-sr.c+8 -70 replies
17/30KVM: arm64: access __hyp_running_vcpu via accessors onlyarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_host.h arch/arm64/kernel/asm-offsets.c +3 more+41 -130 replies
18/30KVM: arm64: reduce scope of __guest_exit to only depend on kvm_cpu_contextarch/arm64/kvm/hyp/entry.S arch/arm64/kvm/hyp/hyp-entry.S+6 -90 replies
19/30KVM: arm64: change calls of get_loaded_vcpu to get_loaded_vcpu_ctxtarch/arm64/kvm/hyp/entry.S arch/arm64/kvm/hyp/nvhe/host.S+3 -30 replies
20/30KVM: arm64: add __hyp_running_ctxt and __hyp_running_hypsarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_host.h arch/arm64/kernel/asm-offsets.c +1 more+32 -40 replies
21/30KVM: arm64: transition code to __hyp_running_ctxt and __hyp_running_hypsarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_host.h arch/arm64/kernel/asm-offsets.c +4 more+14 -360 replies
22/30KVM: arm64: reduce scope of __guest_enter to depend only on kvm_cpu_ctxtarch/arm64/include/asm/kvm_hyp.h arch/arm64/kvm/hyp/entry.S arch/arm64/kvm/hyp/nvhe/switch.c +1 more+13 -90 replies
23/30KVM: arm64: COCCI: remove_unused.cocci: remove unused ctxt and hypstate variablesarch/arm64/kvm/hyp/exception.c arch/arm64/kvm/hyp/include/hyp/switch.h arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +3 more+6 -170 replies
24/30KVM: arm64: remove unused functionsarch/arm64/kvm/hyp/exception.c+0 -150 replies
25/30KVM: arm64: separate kvm_run() for protected VMsarch/arm64/kvm/hyp/nvhe/switch.c+116 -30 replies
26/30KVM: arm64: pVM activate_traps to use vcpu_ctxt and vcpu_hyp_statearch/arm64/kvm/hyp/nvhe/switch.c+23 -120 replies
27/30KVM: arm64: remove unsupported pVM featuresarch/arm64/kvm/hyp/include/hyp/switch.h arch/arm64/kvm/hyp/nvhe/switch.c+3 -380 replies
28/30KVM: arm64: reduce scope of pVM fixup_guest_exit to hyp_state and kvm_cpu_ctxtarch/arm64/kvm/hyp/include/hyp/switch.h arch/arm64/kvm/hyp/nvhe/switch.c arch/arm64/kvm/hyp/vhe/switch.c+22 -110 replies
29/30Remove Coccinelle scripts added for refactoringcocci_refactor/add_ctxt.cocci cocci_refactor/add_hypstate.cocci cocci_refactor/hyp_ctxt.cocci +12 more+0 -8700 replies
30/30Re-enable warningsMakefile+1 -10 replies

Reviews trailers per patch

1 of 30 patches has a Reviewed-by or Acked-by.

PatchWill Deacon
1/30A-b
2/30
3/30
4/30
5/30
6/30
7/30
8/30
9/30
10/30
11/30
12/30
13/30
14/30
15/30
16/30
17/30
18/30
19/30
20/30
21/30
22/30
23/30
24/30
25/30
26/30
27/30
28/30
29/30
30/30

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [RFC PATCH v1 00/30] Reduce scope of vcpu state at hyp by refactoring out state hyp needs

Hi,

This is a prolog to a series where we try to maintain virtual machine and vcpu state for protected VMs at the hypervisor [1].

The main issue is that in KVM, the VM state (struct kvm) and the vcpu state (struct kvm_vcpu) are created by the host and are always accessible by it. For protected VMs (pKVM [2]), only the hypervisor should have access to their state and not trust the host to access it. Therefore, the hypervisor should maintain a copy of VM state for all protected VMs to use that is not accessible by the host.

The problem with using and with maintaining a copy of the existing kvm_vcpu struct at the hypervisor is that it's big. Depending on the configuration, it is in the order of 10kB (ymmv) per vcpu. Whereas most of what it needs to run a VM is the kvm_cpu_ctxt and some hyp-related registers and flags, which amount to less than 2kB. Many of the functions use the vcpu struct when all they access is kvm_cpu_ctxt. Other functions only need that as well as a few hypervisor state variables. Moreover, we would like to use the existing code, rather than write new code for protected VMs that use new or special structures.

This patch series reduces the scope of the functions that only need kvm_cpu_ctxt to just that. It also takes out the few elements that are relevant to the hypervisor from kvm_vcpu_arch into a new structure, vcpu_hyp_state. This allows the remainder of the series to reduce the scope of everything accessed by the hypervisor, at least for protected VMs, to kvm_cpu_ctxt and vcpu_hyp_state (and maybe vgic if supported for protected VMs).

This series uses coccinelle semantic patches [3] as much as possible when changes are made repetitively across many files. All patches that use coccinelle are prefixed with COCCI.

Based on Linux 5.13-rc6.

Cheers,
/fuad

[1] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/el2-state-cocci-out

[2] Once complete, protected KVM adds the ability to create protected VMs. These protected VMs are protected from the host Linux kernel (and from other VMs), where the host does not have access to guest memory,even if compromised. Normal (nVHE) guests can still be created and run in parallel with protected VMs. Their functionality should not be affected.

For protected VMs, the host should not even have access to a protected guest's state or anything that would enable it to manipulate it (e.g., vcpu register context and el2 system registers); only hyp would have that access. If the host could access that state, then it might be able to get around the protection provided. Therefore, anything that is sensitive and that would require such access needs to happen at hyp, hence the code in nvhe running only at hyp.

For more details about pKVM, please refer to Will's talk at KVM Forum 2020:
https://mirrors.edge.kernel.org/pub/linux/kernel/people/will/slides/kvmforum-2020-edited.pdf
https://www.youtube.com/watch?v=edqJSzsDRxk

[3] https://coccinelle.gitlabpages.inria.fr/website/

Fuad Tabba (30):
KVM: arm64: placeholder to check if VM is protected [DONOTMERGE] Temporarily disable unused variable warning [DONOTMERGE] Coccinelle scripts for refactoring
KVM: arm64: remove unused parameters and asm offsets
KVM: arm64: add accessors for kvm_cpu_context
KVM: arm64: COCCI: use_ctxt_access.cocci: use kvm_cpu_context
accessors
KVM: arm64: COCCI: add_ctxt.cocci use_ctxt.cocci: reduce scope of
functions to kvm_cpu_ctxt
KVM: arm64: add hypervisor state accessors
KVM: arm64: COCCI: vcpu_hyp_accessors.cocci: use accessors for
hypervisor state vcpu variables
KVM: arm64: Add accessors for hypervisor state in kvm_vcpu_arch
KVM: arm64: create and use a new vcpu_hyp_state struct
KVM: arm64: COCCI: add_hypstate.cocci use_hypstate.cocci: Reduce scope
of functions to hyp_state
KVM: arm64: change function parameters to use kvm_cpu_ctxt and
hyp_state
KVM: arm64: reduce scope of vgic v2
KVM: arm64: COCCI: vgic3_cpu.cocci: reduce scope of vgic v3
KVM: arm64: reduce scope of vgic_v3 access parameters
KVM: arm64: access __hyp_running_vcpu via accessors only
KVM: arm64: reduce scope of __guest_exit to only depend on
kvm_cpu_context
KVM: arm64: change calls of get_loaded_vcpu to get_loaded_vcpu_ctxt
KVM: arm64: add __hyp_running_ctxt and __hyp_running_hyps
KVM: arm64: transition code to __hyp_running_ctxt and
__hyp_running_hyps
KVM: arm64: reduce scope of __guest_enter to depend only on
kvm_cpu_ctxt
KVM: arm64: COCCI: remove_unused.cocci: remove unused ctxt and
hypstate variables
KVM: arm64: remove unused functions
KVM: arm64: separate kvm_run() for protected VMs
KVM: arm64: pVM activate_traps to use vcpu_ctxt and vcpu_hyp_state
KVM: arm64: remove unsupported pVM features
KVM: arm64: reduce scope of pVM fixup_guest_exit to hyp_state and
kvm_cpu_ctxt
[DONOTMERGE] Remove Coccinelle scripts added for refactoring [DONOTMERGE] Re-enable warnings

 arch/arm64/include/asm/kvm_asm.h           |  33 ++-
 arch/arm64/include/asm/kvm_emulate.h       | 292 ++++++++++++++++-----
 arch/arm64/include/asm/kvm_host.h          | 110 ++++++--
 arch/arm64/include/asm/kvm_hyp.h           |  14 +-
 arch/arm64/kernel/asm-offsets.c            |   7 +-
 arch/arm64/kvm/arm.c                       |   2 +-
 arch/arm64/kvm/debug.c                     |  28 +-
 arch/arm64/kvm/fpsimd.c                    |  22 +-
 arch/arm64/kvm/guest.c                     |  30 +--
 arch/arm64/kvm/handle_exit.c               |   8 +-
 arch/arm64/kvm/hyp/aarch32.c               |  26 +-
 arch/arm64/kvm/hyp/entry.S                 |  23 +-
 arch/arm64/kvm/hyp/exception.c             | 113 ++++----
 arch/arm64/kvm/hyp/hyp-entry.S             |   8 +-
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h |  26 +-
 arch/arm64/kvm/hyp/include/hyp/debug-sr.h  |   6 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h    | 101 ++++---
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h |  43 +--
 arch/arm64/kvm/hyp/nvhe/debug-sr.c         |   8 +-
 arch/arm64/kvm/hyp/nvhe/host.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/switch.c           | 155 ++++++++---
 arch/arm64/kvm/hyp/nvhe/timer-sr.c         |   4 +-
 arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c   |  32 ++-
 arch/arm64/kvm/hyp/vgic-v3-sr.c            | 242 +++++++++++------
 arch/arm64/kvm/hyp/vhe/switch.c            |  40 +--
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c         |   3 +-
 arch/arm64/kvm/inject_fault.c              |  10 +-
 arch/arm64/kvm/reset.c                     |  16 +-
 arch/arm64/kvm/sys_regs.c                  |   4 +-
 29 files changed, 951 insertions(+), 459 deletions(-)

base-commit: 6d53b3be3b9be497fbe054f35154f508deac729c

Signature · 6 lines
2.33.0.685.g46640cef36-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm