KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v9 Latest25 patches 39 repliesIn review
- v825 patches 35 repliesSuperseded
- v724 patches 26 repliesSuperseded
Earlier versions · 5
| Version | Posted | Patches | Replies | Status |
|---|---|---|---|---|
| v6 | 25 patches | 60 replies | Superseded | |
| v5 | 27 patches | 16 replies | Superseded | |
| v4 | 28 patches | 53 replies | Superseded | |
| v3 | 29 patches | 18 replies | Superseded | |
| v2 RFC | 58 patches | 38 replies | Superseded |
Changes in v9 from the cover letter
v8: https://lore.kernel.org/all/[email protected]/
- Rebase on iommu/next with the new TLB invalidation rework
- Rename files (Nicolin, Jason)
- Move more IDR parsing logic to common code (Nicolin)
- Move Kconfig and depend on OF (Nicolin)
- IDR0.COHACC must agree with firmware (Nicolin)
- Fix waiting with SEV (Sashiko)
- Drop extra heuristics about carveout size (Nicolin)
- Minor reworks and cleanups
Patches in v9 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/25 | KVM: arm64: Donate MMIO to the hypervisorarch/ arch/ arch/ +1 more | +112 | 0 replies | |
| 2/25 | iommu/arm-smmu-v3: Move Queue and STE functions to headerdrivers/ drivers/ | +63 | 0 replies | |
| 3/25 | iommu/arm-smmu-v3: Move IDR parsing to common functionsdrivers/ drivers/ drivers/ +2 more | +297 | 1 reply | |
| 4/25 | iommu/arm-smmu-v3: Move hitless machinery to common codedrivers/ drivers/ drivers/ +2 more | +254 | 0 replies | |
| 5/25 | iommu/arm-smmu-v3: Move RangeInval to common codedrivers/ drivers/ drivers/ | +162 | 1 reply | |
| 6/25 | KVM: arm64: iommu: Introduce IOMMU driver infrastructurearch/ arch/ arch/ +4 more | +87 | 0 replies | |
| 7/25 | KVM: arm64: iommu: Shadow host stage-2 page tablearch/ arch/ arch/ | +143 | 0 replies | |
| 8/25 | KVM: arm64: iommu: Add memory poolDocumentation/ arch/ arch/ +5 more | +89 | 1 reply | |
| 9/25 | KVM: arm64: iommu: Support DABT for IOMMUarch/ arch/ arch/ | +32 | 0 replies | |
| 10/25 | iommu/arm-smmu-v3-kvm: Add SMMUv3 driverarch/ drivers/ drivers/ +2 more | +161 | 1 reply | |
| 11/25 | iommu/arm-smmu-v3-kvm: Add the kernel driverdrivers/ drivers/ drivers/ | +249 | 10 replies | |
| 12/25 | iommu/arm-smmu-v3-kvm: Probe SMMU HWdrivers/ | +45 | 1 reply | |
| 13/25 | iommu/arm-smmu-v3-kvm: Add MMIO emulationdrivers/ drivers/ | +187 | 5 replies | |
| 14/25 | iommu/arm-smmu-v3-kvm: Shadow the command queuedrivers/ drivers/ drivers/ | +161 | 5 replies | |
| 15/25 | iommu/arm-smmu-v3-kvm: Add CMDQ functionsdrivers/ drivers/ | +127 | 2 replies | |
| 16/25 | iommu/arm-smmu-v3-kvm: Emulate CMDQ for hostdrivers/ | +145 | 3 replies | |
| 17/25 | iommu/arm-smmu-v3-kvm: Shadow stream tabledrivers/ drivers/ drivers/ | +153 | 3 replies | |
| 18/25 | iommu/arm-smmu-v3-kvm: Shadow STEsdrivers/ | +118 | 1 reply | |
| 19/25 | iommu/arm-smmu-v3-kvm: Share other queuesdrivers/ drivers/ | +84 | 1 reply | |
| 20/25 | iommu/arm-smmu-v3-kvm: Emulate GBPAdrivers/ | +29 | 0 replies | |
| 21/25 | iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisorarch/ drivers/ drivers/ +1 more | +106 | 1 reply | |
| 22/25 | iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page tabledrivers/ | +152 | 1 reply | |
| 23/25 | iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBsdrivers/ drivers/ | +155 | 1 reply | |
| 24/25 | iommu/arm-smmu-v3-kvm: Enable nestingdrivers/ | +106 | 1 reply | |
| 25/25 | KVM: arm64: Add documentation for pKVM DMA isolationDocumentation/ | +22 | 0 replies |
Reviews trailers per patch
8 of 25 patches have a Reviewed-by or Acked-by.
| Patch | Nicolin Chen |
|---|---|
| 1/25 | |
| 2/25 | R-b |
| 3/25 | R-b |
| 4/25 | R-b |
| 5/25 | R-b |
| 6/25 | |
| 7/25 | |
| 8/25 | |
| 9/25 | |
| 10/25 | R-b |
| 11/25 | |
| 12/25 | R-b |
| 13/25 | |
| 14/25 | R-b |
| 15/25 | R-b |
| 16/25 | |
| 17/25 | |
| 18/25 | |
| 19/25 | |
| 20/25 | |
| 21/25 | |
| 22/25 | |
| 23/25 | |
| 24/25 | |
| 25/25 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH v9 00/25] KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)
Changes from v8:
v8: https://lore.kernel.org/all/[email protected]/
- Rebase on iommu/next with the new TLB invalidation rework
- Rename files (Nicolin, Jason)
- Move more IDR parsing logic to common code (Nicolin)
- Move Kconfig and depend on OF (Nicolin)
- IDR0.COHACC must agree with firmware (Nicolin)
- Fix waiting with SEV (Sashiko)
- Drop extra heuristics about carveout size (Nicolin)
- Minor reworks and cleanups
===================
I ran Sashiko locally and it was helpful in discovering problems in the series. However, it still shows large number of critical and high severity issues, I went through them and I believe they are false positives, mainly because (in the order of frequently reported):
- It doesn't understand WARNs are fatal in the hypervisor.
- It doesn't understand that a malicious host can DoS the system and pKVM doesn't guarantee availability
- It doesn't understand the SMMUv3 spec and makes stuff up (eg. about CMD_SYC CS field it makes up an non-existent encoding or wrong semantics for the gbpa register)
- It seems to look at one patch at a time and not the whole series, and as the series is written in a way to be bisectable that confuses it.
Design:
=======
Assumptions:
------------
One of the important points, is that this doesn’t emulate the full SMMUv3 architecture, but only the parts used by Linux kernel. That’s why enablement of this (ARM_SMMU_V3_PKVM) depends on (ARM_SMMU_V3=y) so we are sure of the driver behaviour.
Any new change in the driver will likely trigger a WARN_ON ending up in panic, that will require to support also in the hypervisor.
Most notable assumptions:
- Changing of stream table format/size or l2 pointers is not allowed after initialization.
- leaf=0 CFGI is not allowed.
- CFGI_ALL with any value but 31 is not allowed.
- Some commands which are not used are not allowed.
- Values set in ARM_SMMU_CR1 are hardcoded and don't change.
Emulation logic mainly targets:
1) Command Queue
----------------
At boot time, the hypervisor will allocate a shadow command queue (doesn't need to match the host size) which then sets up in HW, then it will trap access to
i) ARM_SMMU_CMDQ_BASE
That can only be written when the cmdq is disabled. Then on enable, the hypervisor will put the host command queue in a shared state to avoid transition into the hypervisor or VMs. It will be unshared with the cmdq is disabled
ii) ARM_SMMU_CMDQ_PROD
Trigger emulation code, where the hypervisor will copy the commands between cons and prod, of the host queue and sanitise them (mostly WARNs if the host is malicious and issuing commands it shouldn't) then eagerly consume them, updating the host cons.
iii) ARM_SMMU_CMDQ_CONS
No much logic, just return the emulated cons + error bits.
2) Stream table
---------------
Similar to the command queue, the first level is allocated at boot with max possible size, then the hypervisor will trap access to:
i) ARM_SMMU_STRTAB_BASE/ARM_SMMU_STRTAB_BASE_CFG: Keep track of the stream table to put it in a shared state.
On CFGI_STE, the hypervisor will read the STE in scope from the host copy, shadow L2 pointers if needed and attach stage-2.
3) GBPA
-------
The hypervisor will set GBPA to abort at boot, then any read from the host will return ABORT and writes are ignored.
If the host tries to clear GBPA, it will look like GBPA is refusing to update and time out.
4) EVTQ and PRIDQ
No shadowing needed for those queues, but the hypervisor needs to keep track of them to put them in a shared state so they can’t be used by the host or the hypervisor.
Bisectibility:
==============
I wrote the patches where most of them are bisectable at run time (so we can run with a prefix of the series till MMIO emulation, cmdq emulation, STE or full nested) that was very helpful in debugging, and I kept it like this to make debugging easier.
Constraints:
============
1) Discovery:
-------------
Only device trees are supported at the moment.
2) Shadow page table
--------------------
Uses page granularity (leaf) for memory, that's because of the lack of split_block_unmap() logic.
Boot and Probe ordering:
=======================
The main SMMUv3 MUST be only bound/probed after KVM fully initialises so it can set up the MMIO emulation.
The KVM SMMUv3 driver is loaded early before KVM init so it can register itself, during that point it will probe all the SMMUs from the platform bus and bind them to the driver.
Then at a later init call it will create an auxiliary device per SMMU, that the main driver will probe. The main driver still relies on this device(parent) for all driver activity.
Future work
===========
1) Sharing page tables will be an interesting optimization, which was discussed at LPC [1]
2) There is currently ongoing work to enable RPM, that will possibly enable/disable the SMMU frequently, we might need some optimizations to avoid re-shadowing the CMDQ/STE unnecessarily.
3) Add support for non-coherent SMMUs.
4) Optimizations (as using block mappings for memory).
Patches overview
=================
The patches are split as follows:
Patches 01: Core hypervisor: Dealing with MMIO and timers. Patches 02-05: Refactoring of io-pgtable-arm and SMMUv3 driver. Patches 06-09: Hypervisor IOMMU core: pagetable management, dabts. Patches 10-25: KVM SMMUv3 code.
Tested on Lenovo IdeaCentre mini X and Qemu.
A development branch can be found at [2]
[1] https://lpc.events/event/20/contributions/2555/
[2] https://android-kvm.googlesource.com/linux/+/refs/heads/kvm-smmu-v9
Jean-Philippe Brucker (1):
iommu/arm-smmu-v3-kvm: Add SMMUv3 driver
Mostafa Saleh (24):
KVM: arm64: Donate MMIO to the hypervisor iommu/arm-smmu-v3: Move Queue and STE functions to header iommu/arm-smmu-v3: Move IDR parsing to common functions iommu/arm-smmu-v3: Move hitless machinery to common code iommu/arm-smmu-v3: Move RangeInval to common code
KVM: arm64: iommu: Introduce IOMMU driver infrastructure
KVM: arm64: iommu: Shadow host stage-2 page table
KVM: arm64: iommu: Add memory pool
KVM: arm64: iommu: Support DABT for IOMMU iommu/arm-smmu-v3-kvm: Add the kernel driver iommu/arm-smmu-v3-kvm: Probe SMMU HW iommu/arm-smmu-v3-kvm: Add MMIO emulation iommu/arm-smmu-v3-kvm: Shadow the command queue iommu/arm-smmu-v3-kvm: Add CMDQ functions iommu/arm-smmu-v3-kvm: Emulate CMDQ for host iommu/arm-smmu-v3-kvm: Shadow stream table iommu/arm-smmu-v3-kvm: Shadow STEs iommu/arm-smmu-v3-kvm: Share other queues iommu/arm-smmu-v3-kvm: Emulate GBPA iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs iommu/arm-smmu-v3-kvm: Enable nesting
KVM: arm64: Add documentation for pKVM DMA isolation
.../admin-guide/kernel-parameters.txt | 7 + Documentation/virt/kvm/arm/pkvm.rst | 23 +- arch/arm64/include/asm/kvm_host.h | 8 + arch/arm64/include/asm/kvm_pgtable.h | 2 + arch/arm64/include/asm/kvm_pkvm.h | 10 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/hyp/include/nvhe/iommu.h | 37 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 10 + arch/arm64/kvm/hyp/nvhe/Makefile | 9 +- arch/arm64/kvm/hyp/nvhe/iommu.c | 157 ++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 143 +- arch/arm64/kvm/hyp/nvhe/setup.c | 19 + arch/arm64/kvm/hyp/pgtable.c | 8 +- arch/arm64/kvm/iommu.c | 68 + arch/arm64/kvm/pkvm.c | 1 + drivers/iommu/arm/Kconfig | 12 + drivers/iommu/arm/arm-smmu-v3/Makefile | 3 +- .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c | 646 ++++++++ .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h | 54 + .../arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c | 227 +++ .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 756 +-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 77 +- .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c | 1425 +++++++++++++++++ .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h | 89 + .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h | 88 + drivers/iommu/io-pgtable-arm.c | 14 +- drivers/iommu/io-pgtable-arm.h | 9 + 28 files changed, 3190 insertions(+), 716 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu.c create mode 100644 arch/arm64/kvm/iommu.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h
base-commit: c233d59828627bfaab750ec5546e81970475f951
Signature · 1 line
2.56.0.176.g3575e65682-goog