Skip to contentSkip to conversation

KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)

PATCH Superseded Mostafa Saleh 3 participants 86 messages May 1 – Jul 14

Subsystems (6)
Documentation KVM arm64 IOMMU +2 more subsystems

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover KVM: arm64: SMMUv3 driver for pKVM (trap and emulate)

This is v6 of pKVM SMMUv3 support with trap and emulate

v1: Implements full fledged pv interface
https://lore.kernel.org/kvmarm/[email protected]/

v2: Implements full fledged pv interface (+ more features as evtq and s1)
https://lore.kernel.org/kvmarm/[email protected]/

v3: Only DMA isolation (using pv)
https://lore.kernel.org/kvmarm/[email protected]/

v4: Trap and emulate
https://lore.kernel.org/all/[email protected]/

v5: Trap and emulate
https://lore.kernel.org/all/[email protected]/

This series is based on the review feedback on v5 + improvements, most notably:
- Rebase on ToT which includes the newly merged protected VM support!
- Drop non-coherent support to make the patches smaller, this can be added in a later series.
- Re-work the io-pgtable-arm split to rely on iommu-pages [Jason]
- Use the newly added clock for tracing instead of adding new functions in the hypervisor.
- Align nesting support with the upstream driver in terms of supported IPs [Jason]
- Keep STE hiltless updating when possible [Jason]
- Move some of the refactored code to the c file [Jason]
- Add support for evtq and priq tracking
- Add extra hardening checks, handle failures and massively reduce the amount of WARN_ONs and other cleanups.
- Don’t enforce DMA isolation to not regress pKVM booting.

Notes about Sashiko
===================
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 positive, mainly because (in the order of frequently reported):
- It doesn’t understand de-privilege and which data is trusted that the driver populated at boot (keeps complaining about missing checks for zero mmio size..)
- 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.
- Sometimes it complains about code which is not related to the change.

Fuad is currently working on updating review prompts to make it work better with protected KVM [1]

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.
I don’t usually use ACPI, but I can look into adding that later. (not make this series bigger)

1) Shadow page table
--------------------
Uses page granularity (leaf) for memory, that’s because of the lack of split_block_unmap() logic. I am currently looking into the possibility of sharing page tables, if that turned complicated (as expected) it might be worth to re-add this 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. (Check comment in patch 14.

Future work
===========
1) Sharing page tables will be an interesting optimization, but requires dealing with stage-2 page faults (which are handled by the kernel), BBM and possibly more complexity.

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-02: Core hypervisor: Dealing with MMIO and timers. Patches 04-06: Refactoring of io-pgtable-arm and SMMUv3 driver. Patches 07-10: Hypervisor IOMMU core: pagetable management, dabts. Patches 11-25: KVM SMMUv3 code.

Tested on Lenovo IdeaCentre mini X and Qemu.

A development branch can be found at [2]

[1] https://github.com/ftabba/review-prompts/commits/local-arm64-kvm/
[2] https://android-kvm.googlesource.com/linux/+/refs/heads/pkvm-smmu-v6

Jean-Philippe Brucker (1):
iommu/arm-smmu-v3-kvm: Add SMMUv3 driver

Mostafa Saleh (24):
KVM: arm64: Generalize trace clock
KVM: arm64: Donate MMIO to the hypervisor iommu/arm-smmu-v3: Split code with hyp iommu/arm-smmu-v3: Move TLB range invalidation into common code iommu/arm-smmu-v3: Move IDR parsing to common functions iommu/io-pgtable-arm: Rework to use the iommu-pages API
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: Enable nesting
KVM: arm64: Add documentation for pKVM DMA isolation

 .../admin-guide/kernel-parameters.txt         |    4 +
 Documentation/virt/kvm/arm/pkvm.rst           |   19 +-
 arch/arm64/include/asm/kvm_host.h             |    6 +
 arch/arm64/kvm/Makefile                       |    2 +-
 arch/arm64/kvm/hyp/include/nvhe/clock.h       |   11 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |   23 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |    4 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   13 +-
 arch/arm64/kvm/hyp/nvhe/clock.c               |   44 +-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         |  156 ++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  169 ++-
 arch/arm64/kvm/hyp/nvhe/setup.c               |   20 +
 arch/arm64/kvm/hyp/nvhe/trace.c               |    4 +-
 arch/arm64/kvm/hyp/pgtable.c                  |    9 +-
 arch/arm64/kvm/iommu.c                        |   57 +
 arch/arm64/kvm/pkvm.c                         |    1 +
 drivers/iommu/arm/Kconfig                     |    9 +
 drivers/iommu/arm/arm-smmu-v3/Makefile        |    3 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  |  224 +++
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  232 +++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  387 +----
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  150 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 1250 +++++++++++++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |   67 +
 drivers/iommu/io-pgtable-arm.c                |   68 +-
 drivers/iommu/io-pgtable-arm.h                |    6 +
 drivers/iommu/iommu-pages.h                   |   99 ++
 27 files changed, 2668 insertions(+), 369 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/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-kvm.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw

PATCH v6 1/25 KVM: arm64: Generalize trace clock

IOMMU drivers need to track time, mainly for timeouts. Generalize the tracing clock functions in the hypervsior, so they can be used from IOMMU drivers.

1) Make the compilation independent from tracing.

2) As drivers might need to use that quite early, provide default values for the clock data based on cntfrq_el0, the driver can keep using these values without calling hyp_clock_update() as they don't need to sync with the host timers.

This is mainly used for timeouts, so a malicious host can DoS the system or cause premature timeouts which likely end up in hyp panic, that should be acceptable as neither of those would undermine the security guarantees.

---
 arch/arm64/kvm/hyp/include/nvhe/clock.h | 11 ++-----
 arch/arm64/kvm/hyp/nvhe/Makefile        |  4 +--
 arch/arm64/kvm/hyp/nvhe/clock.c         | 44 ++++++++++++++++++++++---
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++
 arch/arm64/kvm/hyp/nvhe/trace.c         |  4 +--
 5 files changed, 51 insertions(+), 17 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/clock.h +3 -8
index 9f429f5c0664..e6a0e43af88d 100644
@@ -5,12 +5,7 @@
 
 #include <asm/kvm_hyp.h>
 
-#ifdef CONFIG_NVHE_EL2_TRACING
-void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
-u64 trace_clock(void);
-#else
-static inline void
-trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) { }
-static inline u64 trace_clock(void) { return 0; }
-#endif
+void hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
+u64 hyp_clock_ns(void);
+int hyp_clock_init(void);
 #endif
arch/arm64/kvm/hyp/nvhe/Makefile +2 -2
index 62cdfbff7562..89d0533921f9 100644
@@ -26,10 +26,10 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
-	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
+	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
-hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o trace.o events.o
+hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += trace.o events.o
 hyp-obj-y += $(lib-objs)
 
 # Path to simple_ring_buffer.c
arch/arm64/kvm/hyp/nvhe/clock.c +39 -5

64 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index d8e5b563fd3d..8041f6e80cd1 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/clock.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
@@ -312,6 +313,10 @@ void __noreturn __pkvm_init_finalise(void)
 	};
 	pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
 
+	ret = hyp_clock_init();
+	if (ret)
+		goto out;
+
 	ret = fix_host_ownership();
 	if (ret)
 		goto out;
arch/arm64/kvm/hyp/nvhe/trace.c +2 -2
index a6ca27b18e15..e30de840e6c2 100644
@@ -35,7 +35,7 @@ static bool hyp_trace_buffer_loaded(struct hyp_trace_buffer *trace_buffer)
 void *tracing_reserve_entry(unsigned long length)
 {
 	return simple_ring_buffer_reserve(this_cpu_ptr(trace_buffer.simple_rbs), length,
-					  trace_clock());
+					  hyp_clock_ns());
 }
 
 void tracing_commit_entry(void)
@@ -285,7 +285,7 @@ void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 	}
 
 	/* ...we can now override the old one and swap. */
-	trace_clock_update(mult, shift, epoch_ns, epoch_cyc);
+	hyp_clock_update(mult, shift, epoch_ns, epoch_cyc);
 }
 
 int __tracing_reset(unsigned int cpu)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#5 lore raw

PATCH v6 2/25 KVM: arm64: Donate MMIO to the hypervisor

In reply to Mostafa Saleh

Add a function to donate MMIO to the hypervisor so IOMMU hypervisor drivers can protect and access the MMIO of IOMMUs.

As donating MMIO is very rare, and we don’t need to encode the full state, it’s reasonable to have a separate function to do this. It will init the host s2 page table with an invalid leaf with the owner ID to prevent the host from mapping the page on faults.

Also, prevent kvm_pgtable_stage2_unmap() from removing owner ID from stage-2 PTEs, as this can be triggered from recycle logic under memory pressure. There is no code relying on this, as all ownership changes is done via kvm_pgtable_stage2_set_owner()

For the error path in IOMMU drivers, add a function to donate MMIO back from hyp to host. However, that leaks the hypervisor virtual address range which should be acceptable as this is quite rare and it matches the behaviour of fix_map/block.

---
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   2 +
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 119 +++++++++++++++++-
 arch/arm64/kvm/hyp/pgtable.c                  |   9 +-
 3 files changed, 121 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
index 3cbfae0e3dda..ff440204d2c7 100644
@@ -36,6 +36,8 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_host_unshare_hyp(u64 pfn);
 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
+int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size, unsigned long *haddr);
+int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size);
 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
arch/arm64/kvm/hyp/nvhe/mem_protect.c +117 -2

140 lines not on this page · raw message

arch/arm64/kvm/hyp/pgtable.c +2 -7
index 0c1defa5fb0f..b64a50f9bfa8 100644
@@ -1159,13 +1159,8 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	kvm_pte_t *childp = NULL;
 	bool need_flush = false;
 
-	if (!kvm_pte_valid(ctx->old)) {
-		if (stage2_pte_is_counted(ctx->old)) {
-			kvm_clear_pte(ctx->ptep);
-			mm_ops->put_page(ctx->ptep);
-		}
-		return 0;
-	}
+	if (!kvm_pte_valid(ctx->old))
+		return stage2_pte_is_counted(ctx->old) ? -EPERM : 0;
 
 	if (kvm_pte_table(ctx->old, ctx->level)) {
 		childp = kvm_pte_follow(ctx->old, mm_ops);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#8 lore raw

PATCH v6 3/25 iommu/arm-smmu-v3: Split code with hyp

In reply to Mostafa Saleh

The KVM SMMUv3 driver would re-use some of the cmdq and ste code inside the hypervisor, move these functions to a new common C file that is shared between the host kernel and the hypervisor.

---
 drivers/iommu/arm/arm-smmu-v3/Makefile        |   2 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 114 +++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 161 ------------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  61 +++++++
 4 files changed, 176 insertions(+), 162 deletions(-)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -1
index 493a659cc66b..c9ce392e6d31 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o
-arm_smmu_v3-y := arm-smmu-v3.o
+arm_smmu_v3-y := arm-smmu-v3.o arm-smmu-v3-common-lib.o
 arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_IOMMUFD) += arm-smmu-v3-iommufd.o
 arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o
 arm_smmu_v3-$(CONFIG_TEGRA241_CMDQV) += tegra241-cmdqv.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c new file +114 -0

114 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -161

197 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +61 -0

67 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#11 lore raw

PATCH v6 4/25 iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh

Range TLB invalidation has a very specific algorithm. Instead of re-writing it for the hypervisor, move it to a function that can be re-used.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 65 ++++--------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 76 +++++++++++++++++++++
 2 files changed, 88 insertions(+), 53 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +12 -53
index cb64f88989f0..c22832d26495 100644
@@ -2362,68 +2362,27 @@ static void arm_smmu_tlb_inv_context(void *cookie)
 	arm_smmu_domain_inv(smmu_domain);
 }
 
+static void __arm_smmu_cmdq_batch_add(void *__opaque,
+				      struct arm_smmu_cmdq_batch *cmds,
+				      struct arm_smmu_cmdq_ent *cmd)
+{
+	struct arm_smmu_device *smmu = (struct arm_smmu_device *)__opaque;
+
+	arm_smmu_cmdq_batch_add(smmu, cmds, cmd);
+}
+
 static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu,
 					  struct arm_smmu_cmdq_batch *cmds,
 					  struct arm_smmu_cmdq_ent *cmd,
 					  unsigned long iova, size_t size,
 					  size_t granule, size_t pgsize)
 {
-	unsigned long end = iova + size, num_pages = 0, tg = pgsize;
-	size_t inv_range = granule;
-
 	if (WARN_ON_ONCE(!size))
 		return;
 
-	if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) {
-		num_pages = size >> tg;
-
-		/* Convert page size of 12,14,16 (log2) to 1,2,3 */
-		cmd->tlbi.tg = (tg - 10) / 2;
-
-		/*
-		 * Determine what level the granule is at. For non-leaf, both
-		 * io-pgtable and SVA pass a nominal last-level granule because
-		 * they don't know what level(s) actually apply, so ignore that
-		 * and leave TTL=0. However for various errata reasons we still
-		 * want to use a range command, so avoid the SVA corner case
-		 * where both scale and num could be 0 as well.
-		 */
-		if (cmd->tlbi.leaf)
-			cmd->tlbi.ttl = 4 - ((ilog2(granule) - 3) / (tg - 3));
-		else if ((num_pages & CMDQ_TLBI_RANGE_NUM_MAX) == 1)
-			num_pages++;
-	}
-
-	while (iova < end) {
-		if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) {
-			/*
-			 * On each iteration of the loop, the range is 5 bits
-			 * worth of the aligned size remaining.
-			 * The range in pages is:
-			 *
-			 * range = (num_pages & (0x1f << __ffs(num_pages)))
-			 */
-			unsigned long scale, num;
-
-			/* Determine the power of 2 multiple number of pages */
-			scale = __ffs(num_pages);
-			cmd->tlbi.scale = scale;
-
-			/* Determine how many chunks of 2^scale size we have */
-			num = (num_pages >> scale) & CMDQ_TLBI_RANGE_NUM_MAX;
-			cmd->tlbi.num = num - 1;
-
-			/* range is num * 2^scale * pgsize */
-			inv_range = num << (scale + tg);
-
-			/* Clear out the lower order bits for the next iteration */
-			num_pages -= num << scale;
-		}
-
-		cmd->tlbi.addr = iova;
-		arm_smmu_cmdq_batch_add(smmu, cmds, cmd);
-		iova += inv_range;
-	}
+	arm_smmu_tlb_inv_build(cmd, iova, size, granule,
+			       pgsize, smmu->features & ARM_SMMU_FEAT_RANGE_INV,
+			       smmu, __arm_smmu_cmdq_batch_add, cmds);
 }
 
 static bool arm_smmu_inv_size_too_big(struct arm_smmu_device *smmu, size_t size,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +76 -0

82 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#21 lore raw

PATCH v6 5/25 iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used from the hypervisor.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 110 +++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 112 +++---------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   5 +
 3 files changed, 130 insertions(+), 97 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +110 -0
index 62744c8548a8..e6dd087e2999 100644
@@ -112,3 +112,113 @@ int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
 
 	return 0;
 }
+
+u32 smmu_idr0_features(u32 reg)
+{
+	u32 features = 0;
+
+	/* 2-level structures */
+	if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL)
+		features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
+
+	if (reg & IDR0_CD2L)
+		features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
+
+	/*
+	 * Translation table endianness.
+	 * We currently require the same endianness as the CPU, but this
+	 * could be changed later by adding a new IO_PGTABLE_QUIRK.
+	 */
+	switch (FIELD_GET(IDR0_TTENDIAN, reg)) {
+	case IDR0_TTENDIAN_MIXED:
+		features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
+		break;
+#ifdef __BIG_ENDIAN
+	case IDR0_TTENDIAN_BE:
+		features |= ARM_SMMU_FEAT_TT_BE;
+		break;
+#else
+	case IDR0_TTENDIAN_LE:
+		features |= ARM_SMMU_FEAT_TT_LE;
+		break;
+#endif
+	}
+
+	/* Boolean feature flags */
+	if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
+		features |= ARM_SMMU_FEAT_PRI;
+
+	if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
+		features |= ARM_SMMU_FEAT_ATS;
+
+	if (reg & IDR0_SEV)
+		features |= ARM_SMMU_FEAT_SEV;
+
+	if (reg & IDR0_MSI)
+		features |= ARM_SMMU_FEAT_MSI;
+
+	if (reg & IDR0_HYP)
+		features |= ARM_SMMU_FEAT_HYP;
+
+	switch (FIELD_GET(IDR0_STALL_MODEL, reg)) {
+	case IDR0_STALL_MODEL_FORCE:
+		features |= ARM_SMMU_FEAT_STALL_FORCE;
+		fallthrough;
+	case IDR0_STALL_MODEL_STALL:
+		features |= ARM_SMMU_FEAT_STALLS;
+	}
+
+	if (reg & IDR0_S1P)
+		features |= ARM_SMMU_FEAT_TRANS_S1;
+
+	if (reg & IDR0_S2P)
+		features |= ARM_SMMU_FEAT_TRANS_S2;
+
+	return features;
+}
+
+u32 smmu_idr3_features(u32 reg)
+{
+	u32 features = 0;
+
+	if (FIELD_GET(IDR3_RIL, reg))
+		features |= ARM_SMMU_FEAT_RANGE_INV;
+	if (FIELD_GET(IDR3_FWB, reg))
+		features |= ARM_SMMU_FEAT_S2FWB;
+
+	return features;
+}
+
+u32 smmu_idr5_to_oas(u32 reg)
+{
+	switch (FIELD_GET(IDR5_OAS, reg)) {
+	case IDR5_OAS_32_BIT:
+		return 32;
+	case IDR5_OAS_36_BIT:
+		return 36;
+	case IDR5_OAS_40_BIT:
+		return 40;
+	case IDR5_OAS_42_BIT:
+		return 42;
+	case IDR5_OAS_44_BIT:
+		return 44;
+	case IDR5_OAS_48_BIT:
+		return 48;
+	case IDR5_OAS_52_BIT:
+		return 52;
+	}
+	return 0;
+}
+
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +15 -97

146 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5 -0
index 7be41dbe5aaa..64618299d03a 100644
@@ -1144,6 +1144,11 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
 				bool sync);
 int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent);
 
+u32 smmu_idr0_features(u32 reg);
+u32 smmu_idr3_features(u32 reg);
+u32 smmu_idr5_to_oas(u32 reg);
+unsigned long smmu_idr5_to_pgsize(u32 reg);
+
 /* Queue functions shared between kernel and hyp. */
 static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
 {
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#31 lore raw

PATCH v6 6/25 iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Mostafa Saleh

To prepare for supporting io-pgtable-arm in the pKVM hypervisor, we need to abstract away standard kernel allocations, frees, virt/phys conversions, and DMA API mapping.

This patch introduces a set of generic wrappers in iommu-pages.h:
- iommu_alloc_data
- iommu_free_data
- iommu_virt_to_phys
- iommu_phys_to_virt
- iommu_pages_dma_map
- iommu_pages_dma_mapping_error
- iommu_pages_dma_unmap

The io-pgtable-arm.c code is updated to universally use these new wrappers instead of standard kernel kmalloc_obj, kfree, virt_to_phys, dma_map_single, etc. This abstraction makes it easy to replace them with hypervisor-specific implementations in a later patch.

---
 drivers/iommu/io-pgtable-arm.c | 37 ++++++++++++++++------------------
 drivers/iommu/iommu-pages.h    | 36 +++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 20 deletions(-)
drivers/iommu/io-pgtable-arm.c +17 -20
index 0208e5897c29..e765021308f9 100644
@@ -15,7 +15,6 @@
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/types.h>
-#include <linux/dma-mapping.h>
 
 #include <asm/barrier.h>
 
@@ -143,7 +142,7 @@
 #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
 
 /* IOPTE accessors */
-#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
+#define iopte_deref(pte, d) iommu_phys_to_virt(iopte_to_paddr(pte, d))
 
 #define iopte_type(pte)					\
 	(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
@@ -245,7 +244,7 @@ static inline bool arm_lpae_concat_mandatory(struct io_pgtable_cfg *cfg,
 
 static dma_addr_t __arm_lpae_dma_addr(void *pages)
 {
-	return (dma_addr_t)virt_to_phys(pages);
+	return (dma_addr_t)iommu_virt_to_phys(pages);
 }
 
 static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
@@ -272,15 +271,15 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 		return NULL;
 
 	if (!cfg->coherent_walk) {
-		dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
-		if (dma_mapping_error(dev, dma))
+		dma = iommu_pages_dma_map(dev, pages, size);
+		if (iommu_pages_dma_mapping_error(dev, dma))
 			goto out_free;
 		/*
 		 * We depend on the IOMMU being able to work with any physical
 		 * address directly, so if the DMA layer suggests otherwise by
 		 * translating or truncating them, that bodes very badly...
 		 */
-		if (dma != virt_to_phys(pages))
+		if (dma != iommu_virt_to_phys(pages))
 			goto out_unmap;
 	}
 
@@ -288,7 +287,7 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 
 out_unmap:
 	dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
-	dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
+	iommu_pages_dma_unmap(dev, dma, size);
 
 out_free:
 	if (cfg->free)
@@ -304,8 +303,7 @@ static void __arm_lpae_free_pages(void *pages, size_t size,
 				  void *cookie)
 {
 	if (!cfg->coherent_walk)
-		dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
-				 size, DMA_TO_DEVICE);
+		iommu_pages_dma_unmap(cfg->iommu_dev, __arm_lpae_dma_addr(pages), size);
 
 	if (cfg->free)
 		cfg->free(cookie, pages, size);
@@ -316,8 +314,7 @@ static void __arm_lpae_free_pages(void *pages, size_t size,
 static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries,
 				struct io_pgtable_cfg *cfg)
 {
-	dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
-				   sizeof(*ptep) * num_entries, DMA_TO_DEVICE);
+	iommu_pages_flush_incoherent(cfg->iommu_dev, ptep, 0, sizeof(*ptep) * num_entries);
 }
 
 static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg, int num_entries)
@@ -395,7 +392,7 @@ static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
 	arm_lpae_iopte old, new;
 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
 
-	new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE;
+	new = paddr_to_iopte(iommu_virt_to_phys(table), data) | ARM_LPAE_PTE_TYPE_TABLE;
 	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
 		new |= ARM_LPAE_PTE_NSTABLE;
 
@@ -616,7 +613,7 @@ static void arm_lpae_free_pgtable(struct io_pgtable *iop)
 	struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
 
 	__arm_lpae_free_pgtable(data, data->start_level, data->pgd);
-	kfree(data);
+	iommu_free_data(data);
 }
 
 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
@@ -930,7 +927,7 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
 	if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
 		return NULL;
 
-	data = kmalloc_obj(*data);
+	data = iommu_alloc_data(sizeof(*data), GFP_KERNEL);
 	if (!data)
 		return NULL;
 
@@ -1053,11 +1050,11 @@ arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
 	wmb();
 
 	/* TTBR */
-	cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
+	cfg->arm_lpae_s1_cfg.ttbr = iommu_virt_to_phys(data->pgd);
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	iommu_free_data(data);
 	return NULL;
 }
 
@@ -1149,11 +1146,11 @@ arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
 	wmb();
 
 	/* VTTBR */
-	cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
+	cfg->arm_lpae_s2_cfg.vttbr = iommu_virt_to_phys(data->pgd);
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	iommu_free_data(data);
 	return NULL;
 }
 
@@ -1223,7 +1220,7 @@ arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
 	/* Ensure the empty pgd is visible before TRANSTAB can be written */
 	wmb();
 
-	cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
+	cfg->arm_mali_lpae_cfg.transtab = iommu_virt_to_phys(data->pgd) |
 					  ARM_MALI_LPAE_TTBR_READ_INNER |
 					  ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
 	if (cfg->coherent_walk)
@@ -1232,7 +1229,7 @@ arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	iommu_free_data(data);
 	return NULL;
 }
 
drivers/iommu/iommu-pages.h +36 -0
index ae9da4f571f6..e1945193ad7f 100644
@@ -7,6 +7,7 @@
 #ifndef __IOMMU_PAGES_H
 #define __IOMMU_PAGES_H
 
+#include <linux/dma-mapping.h>
 #include <linux/iommu.h>
 
 /**
@@ -145,4 +146,39 @@ void iommu_pages_stop_incoherent_list(struct iommu_pages_list *list,
 void iommu_pages_free_incoherent(void *virt, struct device *dma_dev);
 #endif
 
+static inline void *iommu_alloc_data(size_t size, gfp_t gfp)
+{
+	return kmalloc(size, gfp);
+}
+
+static inline void iommu_free_data(void *p)
+{
+	kfree(p);
+}
+
+static inline phys_addr_t iommu_virt_to_phys(void *virt)
+{
+	return virt_to_phys(virt);
+}
+
+static inline void *iommu_phys_to_virt(phys_addr_t phys)
+{
+	return phys_to_virt(phys);
+}
+
+static inline dma_addr_t iommu_pages_dma_map(struct device *dev, void *virt, size_t size)
+{
+	return dma_map_single(dev, virt, size, DMA_TO_DEVICE);
+}
+
+static inline int iommu_pages_dma_mapping_error(struct device *dev, dma_addr_t dma)
+{
+	return dma_mapping_error(dev, dma);
+}
+
+static inline void iommu_pages_dma_unmap(struct device *dev, dma_addr_t dma, size_t size)
+{
+	dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
+}
+
 #endif /* __IOMMU_PAGES_H */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#38 lore raw

PATCH v6 7/25 KVM: arm64: iommu: Introduce IOMMU driver infrastructure

In reply to Mostafa Saleh

To establish DMA isolation, KVM needs an IOMMU driver which provides ops implemented at EL2.

Only one driver can be used and is registered with kvm_iommu_register_driver() by passing pointer to the ops.

This must be called before module_init() which is the point KVM initializes.

---
 arch/arm64/include/asm/kvm_host.h       |  5 +++++
 arch/arm64/kvm/Makefile                 |  2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h | 13 +++++++++++++
 arch/arm64/kvm/hyp/nvhe/Makefile        |  3 ++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c   | 20 +++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++++
 arch/arm64/kvm/iommu.c                  | 26 +++++++++++++++++++++++++
 7 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c
 create mode 100644 arch/arm64/kvm/iommu.c
arch/arm64/include/asm/kvm_host.h +5 -0
index 851f6171751c..52898d2a3ec6 100644
@@ -1733,4 +1733,9 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg
 
 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
+#ifndef __KVM_NVHE_HYPERVISOR__
+struct kvm_iommu_ops;
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops);
+#endif
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Makefile +1 -1
index 59612d2f277c..0ddef54f7434 100644
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 vgic/vgic-v5.o
+	 vgic/vgic-v5.o iommu.o
 
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/hyp/include/nvhe/iommu.h new file +13 -0
new file mode 100644
index 000000000000..1ac70cc28a9e
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ARM64_KVM_NVHE_IOMMU_H__
+#define __ARM64_KVM_NVHE_IOMMU_H__
+
+#include <asm/kvm_host.h>
+
+struct kvm_iommu_ops {
+	int (*init)(void);
+};
+
+int kvm_iommu_init(void);
+
+#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index 89d0533921f9..606c0e1b7bd0 100644
@@ -24,7 +24,8 @@ CFLAGS_switch.nvhe.o += -Wno-override-init
 
 hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
-	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
+	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o \
+	 iommu/iommu.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +20 -0
new file mode 100644
index 000000000000..406c8fb9b3b9
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * IOMMU operations for pKVM
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ */
+#include <nvhe/iommu.h>
+
+/* Only one set of ops supported */
+struct kvm_iommu_ops *kvm_iommu_ops;
+
+
+int kvm_iommu_init(void)
+{
+	/* Keep DMA isolation optional. */
+	if (!kvm_iommu_ops || !kvm_iommu_ops->init)
+		return 0;
+
+	return kvm_iommu_ops->init();
+}
arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index 8041f6e80cd1..1f6b221db9a0 100644
@@ -14,6 +14,7 @@
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -329,6 +330,10 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
+	ret = kvm_iommu_init();
+	if (ret)
+		goto out;
+
 	ret = hyp_ffa_init(ffa_proxy_pages);
 	if (ret)
 		goto out;
arch/arm64/kvm/iommu.c new file +26 -0
new file mode 100644
index 000000000000..f247384fa193
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Mostafa Saleh <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+
+extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+
+static DEFINE_MUTEX(kvm_iommu_reg_lock);
+
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
+{
+	guard(mutex)(&kvm_iommu_reg_lock);
+
+	/* Only protected KVM before de-privilege. */
+	if (!is_protected_kvm_enabled() || is_kvm_arm_initialised())
+		return -EINVAL;
+
+	if (kvm_nvhe_sym(kvm_iommu_ops))
+		return -EBUSY;
+
+	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
+	return 0;
+}
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#41 lore raw

PATCH v6 8/25 KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

There are 3 different ways to add the callback:
1) In the high level memory transitions: (__pkvm_host_donate_hyp(), __pkvm_host_donate_guest()...

2) In Lower level functions covering all transitions
- host_stage2_set_owner_metadata_locked() which covers:
- __pkvm_host_donate_hyp()
- __pkvm_host_donate_guest()
- __pkvm_host_donate_hyp()
- __pkvm_guest_unshare_host()
- host_stage2_set_owner_locked() only for ID_HOST which covers:
- __pkvm_hyp_donate_host()
- __pkvm_host_force_reclaim_page_guest()
- __pkvm_host_reclaim_page_guest()
- __pkvm_guest_share_host()

3) In the lowest level function __host_update_page_state(), which requires only one callback. However, in that case the page state is not enough as we might need to know the old state also.

Option #3 was implemented here.

For some cases, an SMMUv3 may be able to share the same page-table used with the host CPU stage-2 directly.

However, this is too strict and requires changes to the core hypervisor page-table code, plus it would require the hypervisor to handle IOMMU page-faults. This can be added later as an optimization for SMMUV3.

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |   4 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   2 +
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         | 108 +++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  35 ++++++
 4 files changed, 146 insertions(+), 3 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +4 -0
index 1ac70cc28a9e..6277d845cdcf 100644
@@ -3,11 +3,15 @@
 #define __ARM64_KVM_NVHE_IOMMU_H__
 
 #include <asm/kvm_host.h>
+#include <asm/kvm_pgtable.h>
 
 struct kvm_iommu_ops {
 	int (*init)(void);
+	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
 int kvm_iommu_init(void);
 
+int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
+				enum kvm_pgtable_prot prot);
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
index ff440204d2c7..f7faecc3b70a 100644
@@ -54,6 +54,8 @@ int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct
 int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu);
 
 bool addr_is_memory(phys_addr_t phys);
+u64 find_mem_range_from(u64 start, bool *is_memory);
+
 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id);
 int kvm_host_prepare_stage2(void *pgt_pool_base);
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +105 -3

122 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/mem_protect.c +35 -0
index 2fb20a63a417..b54cb72ed88c 100644
@@ -15,6 +15,7 @@
 #include <hyp/fault.h>
 
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;
+
+	/*
+	 * Only update the IOMMU from here, as MMIO can't transition after
+	 * de-privilege, that will need to change when device assignment
+	 * is supported.
+	 * And WARN on failure as we can't unroll at this point.
+	 */
+	WARN_ON(kvm_iommu_host_stage2_idmap(addr, addr + size, prot));
 }
 
 #define KVM_HOST_DONATION_PTE_OWNER_MASK	GENMASK(3, 1)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#61 lore raw

PATCH v6 9/25 KVM: arm64: iommu: Add memory pool

In reply to Mostafa Saleh

IOMMU drivers would require to allocate memory for the shadow page table. Similar to the host stage-2 CPU page table, the IOMMU pool is allocated early from the carveout and it's memory is added in a pool which the IOMMU driver can allocate from and reclaim at run time.

As this is too early for drivers to use init calls, set the number of page allocated from the kernel command line "kvm-arm.hyp_iommu_pages".

Later when the driver registers, it will pass how many pages it needs, and if it was less than what was allocated, it will fail to register.

---
 .../admin-guide/kernel-parameters.txt         |  4 +++
 arch/arm64/include/asm/kvm_host.h             |  3 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |  7 +++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         | 21 +++++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               | 12 ++++++-
 arch/arm64/kvm/iommu.c                        | 33 ++++++++++++++++++-
 arch/arm64/kvm/pkvm.c                         |  1 +
 7 files changed, 76 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
index cf3807641d89..5e49946ff7ed 100644
@@ -3283,6 +3283,10 @@ Kernel parameters
 			trap: set WFI instruction trap
 
 			notrap: clear WFI instruction trap
+	kvm-arm.hyp_iommu_pages=
+			[KVM, ARM, EARLY]
+			Number of pages allocated for the IOMMU pool from the
+			KVM carveout when running in protected mode.
 
 	kvm_cma_resv_ratio=n [PPC,EARLY]
 			Reserves given percentage from system memory area for
arch/arm64/include/asm/kvm_host.h +2 -1
index 52898d2a3ec6..17f4cce86ec3 100644
@@ -1735,7 +1735,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
 struct kvm_iommu_ops;
-int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops);
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops, unsigned int pool_pages);
+unsigned int kvm_iommu_pages(void);
 #endif
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/hyp/include/nvhe/iommu.h +6 -1
index 6277d845cdcf..eba94b4f6050 100644
@@ -10,8 +10,13 @@ struct kvm_iommu_ops {
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
-int kvm_iommu_init(void);
+int kvm_iommu_init(void *pool_base, unsigned int nr_pages);
 
 int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 				enum kvm_pgtable_prot prot);
+
+/* Returns zeroed memory. */
+void *kvm_iommu_donate_pages(u8 order);
+void kvm_iommu_reclaim_pages(void *ptr);
+
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +20 -1
index 1db52bd87c38..53cb5e4b0aac 100644
@@ -15,6 +15,7 @@ struct kvm_iommu_ops *kvm_iommu_ops;
 
 /* Protected by host_mmu.lock */
 static bool kvm_idmap_initialized;
+static struct hyp_pool iommu_pages_pool;
 
 static inline int pkvm_to_iommu_prot(enum kvm_pgtable_prot prot)
 {
@@ -95,7 +96,7 @@ static int kvm_iommu_snapshot_host_stage2(void)
 	return ret;
 }
 
-int kvm_iommu_init(void)
+int kvm_iommu_init(void *pool_base, unsigned int nr_pages)
 {
 	int ret;
 
@@ -103,6 +104,14 @@ int kvm_iommu_init(void)
 	    !kvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)
+		return -ENOMEM;
+
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = kvm_iommu_ops->init();
 	if (ret)
 		return ret;
@@ -120,3 +129,13 @@ int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 
 	return kvm_iommu_ops->host_stage2_idmap(start, end, pkvm_to_iommu_prot(prot));
 }
+
+void *kvm_iommu_donate_pages(u8 order)
+{
+	return hyp_alloc_pages(&iommu_pages_pool, order);
+}
+
+void kvm_iommu_reclaim_pages(void *ptr)
+{
+	hyp_put_page(&iommu_pages_pool, ptr);
+}
arch/arm64/kvm/hyp/nvhe/setup.c +11 -1
index 1f6b221db9a0..215014e42c27 100644
@@ -23,6 +23,9 @@
 
 unsigned long hyp_nr_cpus;
 
+/* See kvm_iommu_pages() */
+unsigned int hyp_kvm_iommu_pages;
+
 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
 			 (unsigned long)__per_cpu_start)
 
@@ -34,6 +37,7 @@ static void *selftest_base;
 static void *ffa_proxy_pages;
 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
 static struct hyp_pool hpool;
+static void *iommu_base;
 
 static int divide_memory_pool(void *virt, unsigned long size)
 {
@@ -71,6 +75,12 @@ static int divide_memory_pool(void *virt, unsigned long size)
 	if (!ffa_proxy_pages)
 		return -ENOMEM;
 
+	if (hyp_kvm_iommu_pages) {
+		iommu_base = hyp_early_alloc_contig(hyp_kvm_iommu_pages);
+		if (!iommu_base)
+			return -ENOMEM;
+	}
+
 	return 0;
 }
 
@@ -330,7 +340,7 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
-	ret = kvm_iommu_init();
+	ret = kvm_iommu_init(iommu_base, hyp_kvm_iommu_pages);
 	if (ret)
 		goto out;
 
arch/arm64/kvm/iommu.c +32 -1
index f247384fa193..213429ceb549 100644
@@ -7,10 +7,11 @@
 #include <linux/kvm_host.h>
 
 extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+extern unsigned int kvm_nvhe_sym(hyp_kvm_iommu_pages);
 
 static DEFINE_MUTEX(kvm_iommu_reg_lock);
 
-int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops, unsigned int pool_pages)
 {
 	guard(mutex)(&kvm_iommu_reg_lock);
 
@@ -21,6 +22,36 @@ int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(kvm_iommu_ops))
 		return -EBUSY;
 
+	/* See kvm_iommu_pages() */
+	if (pool_pages > kvm_nvhe_sym(hyp_kvm_iommu_pages)) {
+		kvm_err("Not enough memory for the IOMMU pool, need 0x%x pages, check kvm-arm.hyp_iommu_pages",
+			pool_pages);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
 	return 0;
 }
+
+unsigned int kvm_iommu_pages(void)
+{
+	/*
+	 * This is used very early during setup_arch() before any initcalls
+	 * or any drivers are registered.
+	 * This value is set by a command line option.
+	 * Later, when the driver is registered, it will pass the number
+	 * pages needed for it's page tables, if it was less that what
+	 * the system has already allocated, the registration will fail.
+	 */
+	return kvm_nvhe_sym(hyp_kvm_iommu_pages);
+}
+
+/* Number of pages to reserve for iommu pool*/
+static int __init early_hyp_iommu_pages(char *arg)
+{
+	if (!arg)
+		return -EINVAL;
+
+	return kstrtouint(arg, 0, &kvm_nvhe_sym(hyp_kvm_iommu_pages));
+}
+early_param("kvm-arm.hyp_iommu_pages", early_hyp_iommu_pages);
arch/arm64/kvm/pkvm.c +1 -0
index 053e4f733e4b..79dd14db4919 100644
@@ -63,6 +63,7 @@ void __init kvm_hyp_reserve(void)
 	hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
 	hyp_mem_pages += pkvm_selftest_pages();
 	hyp_mem_pages += hyp_ffa_proxy_pages();
+	hyp_mem_pages += kvm_iommu_pages();
 
 	/*
 	 * Try to allocate a PMD-aligned region to reduce TLB pressure once
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#66 lore raw

PATCH v6 10/25 KVM: arm64: iommu: Support DABT for IOMMU

In reply to Mostafa Saleh

The pKVM SMMUv3 driver needs to trap and emulate access to the MMIO space of the SMMUv3 to provide emulation for the kernel driver.

Add a handler for DABTs for IOMMU drivers to be able to do so. In case the host causes a data abort, check if it's part of IOMMU emulation first.

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h |  3 ++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c   | 15 +++++++++++++++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c   | 15 +++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +2 -1
index eba94b4f6050..e1b6f16391cc 100644
@@ -8,6 +8,7 @@
 struct kvm_iommu_ops {
 	int (*init)(void);
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
+	bool (*dabt_handler)(struct user_pt_regs *regs, u64 esr, u64 addr);
 };
 
 int kvm_iommu_init(void *pool_base, unsigned int nr_pages);
@@ -18,5 +19,5 @@ int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 /* Returns zeroed memory. */
 void *kvm_iommu_donate_pages(u8 order);
 void kvm_iommu_reclaim_pages(void *ptr);
-
+bool kvm_iommu_host_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr);
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +15 -0
index 53cb5e4b0aac..b1474db016e5 100644
@@ -4,6 +4,10 @@
  *
  * Copyright (C) 2022 Linaro Ltd.
  */
+#include <asm/kvm_hyp.h>
+
+#include <hyp/adjust_pc.h>
+
 #include <linux/iommu.h>
 
 #include <nvhe/iommu.h>
@@ -139,3 +143,14 @@ void kvm_iommu_reclaim_pages(void *ptr)
 {
 	hyp_put_page(&iommu_pages_pool, ptr);
 }
+
+bool kvm_iommu_host_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)
+{
+	if (kvm_iommu_ops && kvm_iommu_ops->dabt_handler &&
+	    kvm_iommu_ops->dabt_handler(regs, esr, addr)) {
+		/* DABT handled by the driver, skip to next instruction. */
+		kvm_skip_host_instr();
+		return true;
+	}
+	return false;
+}
arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -0
index b54cb72ed88c..5c64007dba4d 100644
@@ -788,6 +788,12 @@ static void host_inject_mem_abort(struct kvm_cpu_context *host_ctxt)
 	inject_host_exception(esr);
 }
 
+static bool is_mmio_dabt(u64 esr)
+{
+	return (ESR_ELx_EC(esr) == ESR_ELx_EC_DABT_LOW) &&
+		(esr & ESR_ELx_ISV);
+}
+
 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
 {
 	struct kvm_vcpu_fault_info fault;
@@ -810,6 +816,15 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
 	BUG_ON(!(fault.hpfar_el2 & HPFAR_EL2_NS));
 	addr = FIELD_GET(HPFAR_EL2_FIPA, fault.hpfar_el2) << 12;
 
+	/*
+	 * Emulate data aborts for IOMMU drivers, other access will be denied
+	 * by host_stage2_adjust_range()
+	 */
+	if (is_mmio_dabt(esr) && !addr_is_memory(addr) &&
+	    kvm_iommu_host_dabt_handler(&host_ctxt->regs,
+					esr, addr | FAR_TO_FIPA_OFFSET(fault.far_el2)))
+		return;
+
 	switch (host_stage2_idmap(addr)) {
 	case -EPERM:
 		host_inject_mem_abort(host_ctxt);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#67 lore raw

PATCH v6 11/25 iommu/arm-smmu-v3-kvm: Add SMMUv3 driver

In reply to Mostafa Saleh

From: Jean-Philippe Brucker <[email protected]>

Add the skeleton for an Arm SMMUv3 driver at EL2.

The driver rely on an array of SMMUv3s on the system, where at init it will donate the array and the resources of the SMMUv3s so they can't be changed by the host after de-privilege.

This array will be populated in the next patch.

---
 arch/arm64/kvm/hyp/nvhe/Makefile              |  5 ++
 drivers/iommu/arm/Kconfig                     |  9 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 87 +++++++++++++++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  | 27 ++++++
 4 files changed, 128 insertions(+)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h
arch/arm64/kvm/hyp/nvhe/Makefile +5 -0
index 606c0e1b7bd0..8a75739db947 100644
@@ -33,6 +33,11 @@ hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += trace.o events.o
 hyp-obj-y += $(lib-objs)
 
+HYP_SMMU_V3_DRV_PATH = ../../../../../drivers/iommu/arm/arm-smmu-v3
+
+hyp-obj-$(CONFIG_ARM_SMMU_V3_PKVM) += $(HYP_SMMU_V3_DRV_PATH)/pkvm/arm-smmu-v3.o \
+	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o
+
 # Path to simple_ring_buffer.c
 CFLAGS_trace.nvhe.o += -I$(srctree)/kernel/trace/
 
drivers/iommu/arm/Kconfig +9 -0
index 5fac08b89dee..916f4723238d 100644
@@ -141,3 +141,12 @@ config QCOM_IOMMU
 	select ARM_DMA_USE_IOMMU
 	help
 	  Support for IOMMU on certain Qualcomm SoCs.
+
+config ARM_SMMU_V3_PKVM
+	bool "ARM SMMUv3 support for protected Virtual Machines"
+	depends on KVM && ARM64 && ARM_SMMU_V3=y
+	help
+	  Enable a SMMUv3 driver in the KVM hypervisor, to protect VMs against
+	  memory accesses from devices owned by the host.
+
+	  Say Y here if you intend to enable KVM in protected mode.
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +87 -0

87 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h new file +27 -0
new file mode 100644
index 000000000000..0d9e48b201f5
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __KVM_ARM_SMMU_V3_H
+#define __KVM_ARM_SMMU_V3_H
+
+#include <asm/kvm_asm.h>
+
+/*
+ * Parameters from the trusted host:
+ * @mmio_addr		base address of the SMMU registers
+ * @mmio_size		size of the registers resource
+ *
+ * Other members are filled and used at runtime by the SMMU driver.
+ * @base		Virtual address of SMMU registers
+ */
+struct hyp_arm_smmu_v3_device {
+	phys_addr_t		mmio_addr;
+	size_t			mmio_size;
+	void __iomem		*base;
+};
+
+extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
+#define kvm_hyp_arm_smmu_v3_count kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count)
+
+extern struct hyp_arm_smmu_v3_device *kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus);
+#define kvm_hyp_arm_smmu_v3_smmus kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus)
+
+#endif /* __KVM_ARM_SMMU_V3_H */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#68 lore raw

PATCH v6 12/25 iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh

When KVM runs in protected mode, and CONFIG_ARM_SMMU_V3_PKVM is enabled, it will manage the SMMUv3 HW using trap and emulate and present emulated SMMUs to the host kernel.

In that case, those SMMUs will be on the aux bus, so make it possible to the driver to probe those devices.

Otherwise, everything else is the same as the KVM emulation complies with the architecture,so the driver doesn't need to be modified.

---
 drivers/iommu/arm/arm-smmu-v3/Makefile        |   1 +
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   | 188 ++++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  43 ++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |   2 +
 4 files changed, 234 insertions(+)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -0
index c9ce392e6d31..c3fc5c4a4a1e 100644
@@ -4,5 +4,6 @@ arm_smmu_v3-y := arm-smmu-v3.o arm-smmu-v3-common-lib.o
 arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_IOMMUFD) += arm-smmu-v3-iommufd.o
 arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o
 arm_smmu_v3-$(CONFIG_TEGRA241_CMDQV) += tegra241-cmdqv.o
+arm_smmu_v3-$(CONFIG_ARM_SMMU_V3_PKVM) += arm-smmu-v3-kvm.o
 
 obj-$(CONFIG_ARM_SMMU_V3_KUNIT_TEST) += arm-smmu-v3-test.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +188 -0

188 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +43 -0

55 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +2 -0
index 0d9e48b201f5..744ee2b7f0b4 100644
@@ -8,6 +8,7 @@
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
  * @mmio_size		size of the registers resource
+ * @features		Features of SMMUv3, subset of the main driver
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
@@ -16,6 +17,7 @@ struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
 	size_t			mmio_size;
 	void __iomem		*base;
+	u32			features;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#69 lore raw

PATCH v6 13/25 iommu/arm-smmu-v3-kvm: Probe SMMU HW

In reply to Mostafa Saleh

Probe SMMU features from the IDR register space, most of the logic is common with the kernel.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  6 --
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  6 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 78 +++++++++++++++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |  6 ++
 4 files changed, 90 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -6
index 61e6ab364086..157acde0436d 100644
@@ -4738,12 +4738,6 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
 	return 0;
 }
 
-#define IIDR_IMPLEMENTER_ARM		0x43b
-#define IIDR_PRODUCTID_ARM_MMU_600	0x483
-#define IIDR_PRODUCTID_ARM_MMU_700	0x487
-#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
-#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
-
 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu)
 {
 	u32 reg;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
index 64618299d03a..f904f4d19609 100644
@@ -84,6 +84,12 @@ struct arm_vsmmu;
 #define IIDR_REVISION			GENMASK(15, 12)
 #define IIDR_IMPLEMENTER		GENMASK(11, 0)
 
+#define IIDR_IMPLEMENTER_ARM		0x43b
+#define IIDR_PRODUCTID_ARM_MMU_600	0x483
+#define IIDR_PRODUCTID_ARM_MMU_700	0x487
+#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
+#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
+
 #define ARM_SMMU_AIDR			0x1C
 
 #define ARM_SMMU_CR0			0x20
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +78 -0

98 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +6 -0
index 744ee2b7f0b4..82b84673e85b 100644
@@ -12,12 +12,18 @@
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
+ * @oas			PA size
+ * @pgsize_bitmap	Supported page sizes
+ * @sid_bits		Max number of SID bits supported
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
 	size_t			mmio_size;
 	void __iomem		*base;
 	u32			features;
+	unsigned long		oas;
+	unsigned long		pgsize_bitmap;
+	unsigned int		sid_bits;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#72 lore raw

PATCH v6 14/25 iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Mostafa Saleh

Add data abort handler for the SMMUs, at the moment most registers are just passthrough.
In the next patches CMDQ/STE emulation will be added which inserts logic to some register access.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 143 ++++++++++++++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |  10 ++
 2 files changed, 153 insertions(+)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +143 -0
index d9945db9e102..cce5a51b4656 100644
@@ -8,6 +8,7 @@
 
 #include <nvhe/iommu.h>
 #include <nvhe/mem_protect.h>
+#include <nvhe/trap_handler.h>
 
 #include "arm_smmu_v3.h"
 #include "../arm-smmu-v3.h"
@@ -106,6 +107,7 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 	if (!PAGE_ALIGNED(smmu->mmio_addr | smmu->mmio_size))
 		return -EINVAL;
 
+	hyp_spin_lock_init(&smmu->lock);
 	ret = __pkvm_host_donate_hyp_mmio(smmu->mmio_addr, smmu->mmio_size, &haddr);
 	if (ret)
 		return ret;
@@ -144,6 +146,8 @@ static int smmu_init(void)
 			goto out_reclaim_smmu;
 	}
 
+	BUILD_BUG_ON(sizeof(hyp_spinlock_t) != sizeof(u32));
+
 	return 0;
 
 out_reclaim_smmu:
@@ -153,6 +157,144 @@ static int smmu_init(void)
 	return ret;
 }
 
+static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
+			     struct user_pt_regs *regs,
+			     u64 esr, u32 off)
+{
+	bool is_write = esr & ESR_ELx_WNR;
+	unsigned int len = BIT((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
+	int rd = (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
+	const u64 read_write = -1ULL;
+	const u64 no_access = 0;
+	u64 mask = no_access;
+	const u64 read_only = is_write ? no_access : read_write;
+	bool is_xzr = (rd == 31);
+	u64 val = is_xzr ? 0 : regs->regs[rd];
+
+	switch (off) {
+	case ARM_SMMU_IDR0:
+		if (len != sizeof(u32))
+			break;
+		/* Clear stage-2 support, hide MSI to avoid write back to cmdq */
+		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP);
+		break;
+	/* Passthrough the register access for bisectiblity, handled later */
+	case ARM_SMMU_CMDQ_BASE:
+	case ARM_SMMU_CMDQ_PROD:
+	case ARM_SMMU_CMDQ_CONS:
+	case ARM_SMMU_STRTAB_BASE:
+	case ARM_SMMU_STRTAB_BASE_CFG:
+	case ARM_SMMU_GBPA:
+		mask = read_write;
+		break;
+	case ARM_SMMU_CR0:
+		if (len != sizeof(u32))
+			break;
+		mask = read_write;
+		break;
+	case ARM_SMMU_CR1: {
+		/* Based on Linux implementation */
+		u64 cr1_template = FIELD_PREP(CR1_TABLE_SH, ARM_SMMU_SH_ISH) |
+				FIELD_PREP(CR1_TABLE_OC, CR1_CACHE_WB) |
+				FIELD_PREP(CR1_TABLE_IC, CR1_CACHE_WB) |
+				FIELD_PREP(CR1_QUEUE_SH, ARM_SMMU_SH_ISH) |
+				FIELD_PREP(CR1_QUEUE_OC, CR1_CACHE_WB) |
+				FIELD_PREP(CR1_QUEUE_IC, CR1_CACHE_WB);
+		if (len != sizeof(u32))
+			break;
+		/* Don't mess with shareability/cacheability. */
+		if (is_write) {
+			WARN_ON(val != cr1_template);
+			val = cr1_template;
+		}
+		mask = read_write;
+		break;
+	}
+
+	/* Allowed 32 bit registers. */
+	case ARM_SMMU_EVTQ_PROD + SZ_64K:
+	case ARM_SMMU_EVTQ_CONS + SZ_64K:
+	case ARM_SMMU_EVTQ_IRQ_CFG1:
+	case ARM_SMMU_EVTQ_IRQ_CFG2:
+	case ARM_SMMU_PRIQ_PROD + SZ_64K:
+	case ARM_SMMU_PRIQ_CONS + SZ_64K:
+	case ARM_SMMU_PRIQ_IRQ_CFG1:
+	case ARM_SMMU_PRIQ_IRQ_CFG2:
+	case ARM_SMMU_GERRORN:
+	case ARM_SMMU_GERROR_IRQ_CFG1:
+	case ARM_SMMU_GERROR_IRQ_CFG2:
+	case ARM_SMMU_IRQ_CTRLACK:
+	case ARM_SMMU_IRQ_CTRL:
+	case ARM_SMMU_CR0ACK:
+	case ARM_SMMU_CR2:
+		if (len != sizeof(u32))
+			break;
+		mask = read_write;
+		break;
+	/* Allowed 64 bit registers. */
+	case ARM_SMMU_EVTQ_BASE:
+	case ARM_SMMU_EVTQ_IRQ_CFG0:
+	case ARM_SMMU_PRIQ_BASE:
+	case ARM_SMMU_PRIQ_IRQ_CFG0:
+	case ARM_SMMU_GERROR_IRQ_CFG0:
+		if (len != sizeof(u64))
+			break;
+		mask = read_write;
+		break;
+	/* Allowed RO 32 bit registers. */
+	case ARM_SMMU_IIDR:
+	case ARM_SMMU_IDR5:
+	case ARM_SMMU_IDR3:
+	case ARM_SMMU_IDR1:
+	case ARM_SMMU_GERROR:
+		if (len != sizeof(u32))
+			break;
+		mask = read_only;
+	};
+
+	if (WARN_ON(!mask))
+		goto out_ret;
+
+	if (is_write) {
+		if (len == sizeof(u64))
+			writeq_relaxed(val & mask, smmu->base + off);
+		else
+			writel_relaxed(val & mask, smmu->base + off);
+
+		return true;
+	}
+
+	if (len == sizeof(u64))
+		val = readq_relaxed(smmu->base + off) & mask;
+	else
+		val = readl_relaxed(smmu->base + off) & mask;
+	/*
+	 * Device might be read senstive, so do it but ignore writing
+	 * back for xzr.
+	 */
+	if (!is_xzr)
+		regs->regs[rd] = val;
+
+out_ret:
+	return true;
+}
+
+static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)
+{
+	struct hyp_arm_smmu_v3_device *smmu;
+	bool ret;
+
+	for_each_smmu(smmu) {
+		if (addr < smmu->mmio_addr || addr >= smmu->mmio_addr + smmu->mmio_size)
+			continue;
+		hyp_spin_lock(&smmu->lock);
+		ret = smmu_dabt_device(smmu, regs, esr, addr - smmu->mmio_addr);
+		hyp_spin_unlock(&smmu->lock);
+		return ret;
+	}
+	return false;
+}
+
 static int smmu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, int prot)
 {
 	return 0;
@@ -162,4 +304,5 @@ static int smmu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, int prot)
 struct kvm_iommu_ops smmu_ops = {
 	.init				= smmu_init,
 	.host_stage2_idmap		= smmu_host_stage2_idmap,
+	.dabt_handler			= smmu_dabt_handler,
 };
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +10 -0
index 82b84673e85b..263b0fef262d 100644
@@ -4,6 +4,10 @@
 
 #include <asm/kvm_asm.h>
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#include <nvhe/spinlock.h>
+#endif
+
 /*
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
@@ -15,6 +19,7 @@
  * @oas			PA size
  * @pgsize_bitmap	Supported page sizes
  * @sid_bits		Max number of SID bits supported
+ * @lock		Lock to protect SMMU
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -24,6 +29,11 @@ struct hyp_arm_smmu_v3_device {
 	unsigned long		oas;
 	unsigned long		pgsize_bitmap;
 	unsigned int		sid_bits;
+#ifdef __KVM_NVHE_HYPERVISOR__
+	hyp_spinlock_t		lock;
+#else
+	u32			lock;
+#endif
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#73 lore raw

PATCH v6 15/25 iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Mostafa Saleh

At boot allocate a command queue per SMMU which is used as a shadow by the hypervisor.

The command queue size is 64K which is more than enough, as the hypervisor would consume all the entries per a command queue prod write, which means it can handle up to 4096 at a time.

Then, the host command queue needs to be pinned in a shared state, so it can't be donated to VMs, and avoid tricking the hypervisor into accessing them. This is done each time the command queue is enabled, and undone each time the command queue is disabled.
The hypervisor won’t access the host command queue when it is disabled from the host.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  25 ++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 122 +++++++++++++++++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |   8 ++
 3 files changed, 154 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +25 -0
index 9765d3d636d7..fccbc34de087 100644
@@ -15,6 +15,8 @@
 #include "arm-smmu-v3.h"
 #include "pkvm/arm_smmu_v3.h"
 
+#define SMMU_KVM_CMDQ_ORDER				4
+
 extern struct kvm_iommu_ops kvm_nvhe_sym(smmu_ops);
 
 static size_t				kvm_arm_smmu_count;
@@ -24,6 +26,15 @@ static size_t				kvm_arm_smmu_cur;
 static void kvm_arm_smmu_array_free(void)
 {
 	int order;
+	int i;
+
+	for (i = 0 ; i < kvm_arm_smmu_cur ; ++i) {
+		struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[i];
+
+		if (smmu->cmdq.base_dma)
+			free_pages((unsigned long)phys_to_virt(smmu->cmdq.base_dma),
+				   SMMU_KVM_CMDQ_ORDER);
+	}
 
 	order = get_order(kvm_arm_smmu_count * sizeof(*kvm_arm_smmu_array));
 	free_pages((unsigned long)kvm_arm_smmu_array, order);
@@ -70,6 +81,7 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
 	struct device *dev = &pdev->dev;
 	struct resource *res;
+	void *cmdq_base;
 
 	/* Only device tree, ACPI not supported. */
 	if (!dev->of_node)
@@ -95,6 +107,19 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 	if (of_dma_is_coherent(dev->of_node))
 		smmu->features |= ARM_SMMU_FEAT_COHERENCY;
 
+	/*
+	 * Allocate the shadow command queue, it doesn't have to be the same
+	 * size as the host.
+	 * Only populate base_dma and llq.max_n_shift, the hypervisor will init
+	 * the rest.
+	 */
+	cmdq_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, SMMU_KVM_CMDQ_ORDER);
+	if (!cmdq_base)
+		return -ENOMEM;
+
+	smmu->cmdq.base_dma = virt_to_phys(cmdq_base);
+	smmu->cmdq.llq.max_n_shift = SMMU_KVM_CMDQ_ORDER + PAGE_SHIFT - CMDQ_ENT_SZ_SHIFT;
+
 	kvm_arm_smmu_cur++;
 	return 0;
 }
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +121 -1

169 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +8 -0
index 263b0fef262d..cc1ad4c19845 100644
@@ -8,6 +8,8 @@
 #include <nvhe/spinlock.h>
 #endif
 
+#include "../arm-smmu-v3.h"
+
 /*
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
@@ -20,6 +22,9 @@
  * @pgsize_bitmap	Supported page sizes
  * @sid_bits		Max number of SID bits supported
  * @lock		Lock to protect SMMU
+ * @cmdq		CMDQ as observed by HW
+ * @cmdq_host		Host view of the CMDQ, only q_base and llq used.
+ * @cr0			Last value of CR0
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -34,6 +39,9 @@ struct hyp_arm_smmu_v3_device {
 #else
 	u32			lock;
 #endif
+	struct arm_smmu_queue	cmdq;
+	struct arm_smmu_queue	cmdq_host;
+	u32			cr0;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#74 lore raw

PATCH v6 16/25 iommu/arm-smmu-v3-kvm: Add CMDQ functions

In reply to Mostafa Saleh

Add functions to access the command queue, there are 2 main usage:
- Hypervisor's own commands, as TLB invalidation, would use functions as smmu_send_cmd(), which creates and sends a command.
- Add host commands to the shadow command queue, after being filtered, these will be added with smmu_add_cmd_raw.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  14 ++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 107 ++++++++++++++++++
 2 files changed, 115 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -6
index f904f4d19609..3fc499608d76 100644
@@ -1156,19 +1156,21 @@ u32 smmu_idr5_to_oas(u32 reg);
 unsigned long smmu_idr5_to_pgsize(u32 reg);
 
 /* Queue functions shared between kernel and hyp. */
-static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
+static inline u32 queue_space(struct arm_smmu_ll_queue *q)
 {
-	u32 space, prod, cons;
+	u32 prod, cons;
 
 	prod = Q_IDX(q, q->prod);
 	cons = Q_IDX(q, q->cons);
 
 	if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons))
-		space = (1 << q->max_n_shift) - (prod - cons);
-	else
-		space = cons - prod;
+		return (1 << q->max_n_shift) - (prod - cons);
+	return cons - prod;
+}
 
-	return space >= n;
+static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
+{
+	return queue_space(q) >= n;
 }
 
 static inline bool queue_full(struct arm_smmu_ll_queue *q)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +107 -0

125 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#77 lore raw

PATCH v6 17/25 iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to Mostafa Saleh

Don’t allow access to the command queue from the host:
- ARM_SMMU_CMDQ_BASE: Only allowed to be written when CMDQ is disabled, we use it to keep track of the host command queue base.
Reads return the saved value.
- ARM_SMMU_CMDQ_PROD: Writes trigger command queue emulation which sanitise and filters the whole range. Reads returns the host copy.
- ARM_SMMU_CMDQ_CONS: Writes move the sw copy of the cons, but the host can’t skip commands once submitted. Reads return the emulated value and the error bits in the actual cons.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 128 +++++++++++++++++-
 1 file changed, 124 insertions(+), 4 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +124 -4
index aac455599728..1633a3cf8a3b 100644
@@ -100,7 +100,6 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)
 	return 0;
 }
 
-__maybe_unused
 static bool smmu_cmdq_has_space(struct arm_smmu_queue *cmdq, u32 n)
 {
 	struct arm_smmu_ll_queue *llq = &cmdq->llq;
@@ -351,6 +350,92 @@ static int smmu_init(void)
 	return ret;
 }
 
+static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *command)
+{
+	u64 command0 = le64_to_cpu(command[0]);
+	u64 command1 = le64_to_cpu(command[1]);
+	u64 type = FIELD_GET(CMDQ_0_OP, command0);
+
+	switch (type) {
+	case CMDQ_OP_CFGI_STE:
+		/* TBD: SHADOW_STE*/
+		break;
+	case CMDQ_OP_CFGI_ALL:
+	{
+		/*
+		 * Linux doesn't use range STE invalidation, and only use this
+		 * for CFGI_ALL, which is done on reset and not on an new STE
+		 * being used.
+		 * Although, this is not architectural we rely on the current Linux
+		 * implementation.
+		 */
+		if ((FIELD_GET(CMDQ_CFGI_1_RANGE, command1) != 31))
+			return true;
+		break;
+	}
+	case CMDQ_OP_TLBI_NH_ASID:
+	case CMDQ_OP_TLBI_NH_VA:
+	case 0x13: /* CMD_TLBI_NH_VAA: Not used by Linux */
+	{
+		/* Only allow VMID = 0 */
+		if (FIELD_GET(CMDQ_TLBI_0_VMID, command0) != 0)
+			return true;
+		break;
+	}
+	case 0x10: /* CMD_TLBI_NH_ALL: Not used by Linux */
+	case CMDQ_OP_TLBI_EL2_ALL:
+	case CMDQ_OP_TLBI_EL2_VA:
+	case CMDQ_OP_TLBI_EL2_ASID:
+	case CMDQ_OP_TLBI_S12_VMALL:
+	case CMDQ_OP_TLBI_S2_IPA:
+	case 0x23: /* CMD_TLBI_EL2_VAA: Not used by Linux */
+		return true;
+	case CMDQ_OP_CMD_SYNC:
+		if (FIELD_GET(CMDQ_SYNC_0_CS, command0) == CMDQ_SYNC_0_CS_IRQ) {
+			/* Allow it, but let the host timeout, as this should never happen. */
+			command0 &= ~CMDQ_SYNC_0_CS;
+			command0 |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
+			command1 &= ~CMDQ_SYNC_1_MSIADDR_MASK;
+		}
+		break;
+	}
+
+	return false;
+}
+
+static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u64 *host_cmdq = hyp_phys_to_virt(smmu->cmdq_host.q_base & Q_BASE_ADDR_MASK);
+	bool use_wfe = smmu->features & ARM_SMMU_FEAT_SEV, skip;
+	u64 cmd[CMDQ_ENT_DWORDS];
+	int idx, ret;
+	u32 space;
+
+	if (!is_cmdq_enabled(smmu))
+		return 0;
+
+	space = (1 << (smmu->cmdq_host.llq.max_n_shift)) - queue_space(&smmu->cmdq_host.llq);
+	/* Wait for the command queue to have some space. */
+	ret = smmu_wait(use_wfe, smmu_cmdq_has_space(&smmu->cmdq, space));
+	if (ret)
+		return ret;
+
+	while (space--) {
+		idx = Q_IDX(&smmu->cmdq_host.llq, smmu->cmdq_host.llq.cons);
+		queue_inc_cons(&smmu->cmdq_host.llq);
+
+		memcpy(cmd, &host_cmdq[idx * CMDQ_ENT_DWORDS], CMDQ_ENT_DWORDS << 3);
+		skip = smmu_filter_command(smmu, cmd);
+		if (WARN_ON(skip))
+			continue;
+		smmu_add_cmd_raw(smmu, cmd);
+	}
+
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+
+	return smmu_wait(use_wfe, smmu_cmdq_empty(&smmu->cmdq));
+}
+
 static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
 {
 	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
@@ -388,18 +473,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		/* Clear stage-2 support, hide MSI to avoid write back to cmdq */
 		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP);
 		break;
-	/* Passthrough the register access for bisectiblity, handled later */
 	case ARM_SMMU_CMDQ_BASE:
+		/*
+		 * Although allowed to use smaller size, we rely on the SMMUv3 driver
+		 * using 64-bit store instruction for simplicity.
+		 */
+		if (len != sizeof(u64))
+			break;
 		if (is_write) {
 			/* Not allowed by the architecture */
 			if (WARN_ON(is_cmdq_enabled(smmu)))
 				break;
 			smmu->cmdq_host.q_base = val;
+			goto out_ret;
+		} else {
+			val = smmu->cmdq_host.q_base;
+			goto out_update_regs;
 		}
-		mask = read_write;
-		break;
 	case ARM_SMMU_CMDQ_PROD:
+		if (len != sizeof(u32))
+			break;
+		if (is_write) {
+			smmu->cmdq_host.llq.prod = val;
+			WARN_ON(smmu_emulate_cmdq_insert(smmu));
+			goto out_ret;
+		} else {
+			val = smmu->cmdq_host.llq.prod;
+			goto out_update_regs;
+		}
 	case ARM_SMMU_CMDQ_CONS:
+		if (len != sizeof(u32))
+			break;
+		if (is_write) {
+			if (WARN_ON(is_cmdq_enabled(smmu)))
+				break;
+
+			smmu->cmdq_host.llq.cons = val;
+			goto out_ret;
+		} else {
+			/* Propagate errors back to the host.*/
+			u32 cons = readl_relaxed(smmu->base + ARM_SMMU_CMDQ_CONS);
+
+			val = smmu->cmdq_host.llq.cons | (CMDQ_CONS_ERR & cons);
+			goto out_update_regs;
+		}
+	/* Passthrough the register access for bisectiblity, handled later */
 	case ARM_SMMU_STRTAB_BASE:
 	case ARM_SMMU_STRTAB_BASE_CFG:
 	case ARM_SMMU_GBPA:
@@ -495,6 +613,8 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		val = readq_relaxed(smmu->base + off) & mask;
 	else
 		val = readl_relaxed(smmu->base + off) & mask;
+
+out_update_regs:
 	/*
 	 * Device might be read senstive, so do it but ignore writing
 	 * back for xzr.
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#78 lore raw

PATCH v6 18/25 iommu/arm-smmu-v3-kvm: Shadow stream table

In reply to Mostafa Saleh

Allocate the shadow stream table per SMMU.
We choose the size of that table to be 1MB which is the max size used by host in the case of 2 levels.

All the host writes are still paththrough for bisectibility, that is changed next where CFGI commands will be trapped and used to update the shadow copy hypervisor that will be used by HW.

Similar to the command queue, the host stream table is shared/unshared each time the SMMU is enabled/disabled.

Handling of L2 tables is also done in the next patch when the shadowing is added.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  21 ++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 122 ++++++++++++++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |  10 ++
 3 files changed, 152 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +20 -1
index fccbc34de087..7aec558eea29 100644
@@ -16,6 +16,13 @@
 #include "pkvm/arm_smmu_v3.h"
 
 #define SMMU_KVM_CMDQ_ORDER				4
+/*
+ * Use the max value of L1 the kernel uses, that also covers the worst case
+ * for linear tables as it is mandatory according to the spec to support 2
+ * lvl tables if SIDSIZE >= 7
+ */
+#define SMMU_KVM_STRTAB_ORDER				(get_order(STRTAB_MAX_L1_ENTRIES * \
+							 sizeof(struct arm_smmu_strtab_l1)))
 
 extern struct kvm_iommu_ops kvm_nvhe_sym(smmu_ops);
 
@@ -34,6 +41,9 @@ static void kvm_arm_smmu_array_free(void)
 		if (smmu->cmdq.base_dma)
 			free_pages((unsigned long)phys_to_virt(smmu->cmdq.base_dma),
 				   SMMU_KVM_CMDQ_ORDER);
+		if (smmu->strtab_dma)
+			free_pages((unsigned long)phys_to_virt(smmu->strtab_dma),
+				   SMMU_KVM_STRTAB_ORDER);
 	}
 
 	order = get_order(kvm_arm_smmu_count * sizeof(*kvm_arm_smmu_array));
@@ -80,8 +90,8 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 {
 	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
 	struct device *dev = &pdev->dev;
+	void *cmdq_base, *strtab;
 	struct resource *res;
-	void *cmdq_base;
 
 	/* Only device tree, ACPI not supported. */
 	if (!dev->of_node)
@@ -120,6 +130,15 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 	smmu->cmdq.base_dma = virt_to_phys(cmdq_base);
 	smmu->cmdq.llq.max_n_shift = SMMU_KVM_CMDQ_ORDER + PAGE_SHIFT - CMDQ_ENT_SZ_SHIFT;
 
+	strtab = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, SMMU_KVM_STRTAB_ORDER);
+	if (!strtab) {
+		free_pages((unsigned long)cmdq_base, SMMU_KVM_CMDQ_ORDER);
+		return -ENOMEM;
+	}
+
+	smmu->strtab_dma = virt_to_phys(strtab);
+	smmu->strtab_size = PAGE_SIZE << SMMU_KVM_STRTAB_ORDER;
+
 	kvm_arm_smmu_cur++;
 	return 0;
 }
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +122 -0

177 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +10 -0
index cc1ad4c19845..6a73cf6b8873 100644
@@ -15,6 +15,8 @@
  * @mmio_addr		base address of the SMMU registers
  * @mmio_size		size of the registers resource
  * @features		Features of SMMUv3, subset of the main driver
+ * @strtab_dma		Phys address of stream table
+ * @strtab_size		Stream table size
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
@@ -25,6 +27,9 @@
  * @cmdq		CMDQ as observed by HW
  * @cmdq_host		Host view of the CMDQ, only q_base and llq used.
  * @cr0			Last value of CR0
+ * @host_ste_cfg	Host stream table config
+ * @host_ste_base	Host stream table base
+ * @strtab_cfg		Stream table as seen by HW
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -42,6 +47,11 @@ struct hyp_arm_smmu_v3_device {
 	struct arm_smmu_queue	cmdq;
 	struct arm_smmu_queue	cmdq_host;
 	u32			cr0;
+	dma_addr_t		strtab_dma;
+	size_t			strtab_size;
+	u64			host_ste_cfg;
+	u64			host_ste_base;
+	struct arm_smmu_strtab_cfg strtab_cfg;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#79 lore raw

PATCH v6 19/25 iommu/arm-smmu-v3-kvm: Shadow STEs

In reply to Mostafa Saleh

Add STE emulation, when the host sends the CFGI_STE command.

Copy the STE as is to the shadow owned by the hypervisor, in the next patch, stage-2 page table will be attached.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 114 +++++++++++++++++-
 1 file changed, 108 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +108 -6
index d15c9e5aa998..d92811ef2af5 100644
@@ -23,6 +23,9 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;
 #define strtab_split(smmu)	(FIELD_GET(STRTAB_BASE_CFG_SPLIT, (smmu)->host_ste_cfg))
 #define strtab_l1_size(smmu)	((1UL << (strtab_log2size(smmu) - strtab_split(smmu))) * \
 				 (sizeof(struct arm_smmu_strtab_l1)))
+#define strtab_hyp_base(smmu)	((smmu)->features & ARM_SMMU_FEAT_2_LVL_STRTAB ? \
+				 (u64 *)(smmu)->strtab_cfg.l2.l1tab :\
+				 (u64 *)(smmu)->strtab_cfg.linear.table)
 
 #define for_each_smmu(smmu) \
 	for ((smmu) = kvm_hyp_arm_smmu_v3_smmus; \
@@ -305,6 +308,91 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid,
+				struct arm_smmu_ste *host_ste_out)
+{
+	u64 *host_ste_base = hyp_phys_to_virt(strtab_host_base(smmu));
+	struct arm_smmu_strtab_l1 host_l1_desc;
+	struct arm_smmu_strtab_l2 *l2ptr;
+	phys_addr_t host_l2_tab;
+	int ret;
+
+	host_l1_desc.l2ptr = le64_to_cpu(READ_ONCE(host_ste_base[arm_smmu_strtab_l1_idx(sid)]));
+	if (!(host_l1_desc.l2ptr & STRTAB_L1_DESC_SPAN))
+		return -EINVAL;
+
+	host_l2_tab = host_l1_desc.l2ptr & STRTAB_L1_DESC_L2PTR_MASK;
+	/* Share and pin the table before accessing it. */
+	ret = smmu_share_pages(host_l2_tab, sizeof(struct arm_smmu_strtab_l2));
+	if (ret)
+		return ret;
+
+	l2ptr = hyp_phys_to_virt(host_l2_tab);
+	memcpy(host_ste_out, &l2ptr->stes[arm_smmu_strtab_l2_idx(sid)],
+	       STRTAB_STE_DWORDS << 3);
+	WARN_ON(smmu_unshare_pages(host_l2_tab, sizeof(struct arm_smmu_strtab_l2)));
+	return 0;
+}
+
+static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool leaf)
+{
+	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
+	struct arm_smmu_ste *hyp_ste_ptr, *host_ste_ptr, host_ste_copy;
+	u64 *hyp_ste_base = strtab_hyp_base(smmu);
+	int ret;
+
+	/*
+	 * Linux only uses leaf = 1, when leaf is 0, we need to verify that this
+	 * is a 2 level table and reshadow of l2.
+	 * Also, we rely on Linux only issuing CFGI_STE to attach a device when
+	 * the SMMU is enabled.
+	 */
+	if (!leaf || !is_smmu_enabled(smmu) ||
+		(sid >= (1UL << strtab_log2size(smmu))))
+		return -EINVAL;
+
+	if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)) {
+		struct arm_smmu_ste *hyp_table = (struct arm_smmu_ste *)hyp_ste_base;
+		u64 *host_ste_base = hyp_phys_to_virt(strtab_host_base(smmu));
+		struct arm_smmu_ste *host_table = (struct arm_smmu_ste *)host_ste_base;
+
+		if (sid >= cfg->linear.num_ents)
+			return -E2BIG;
+
+		hyp_ste_ptr = &hyp_table[sid];
+		host_ste_ptr = &host_table[sid];
+	} else {
+		struct arm_smmu_strtab_l1 *l1tab = (struct arm_smmu_strtab_l1 *)hyp_ste_base;
+		u32 l1_idx = arm_smmu_strtab_l1_idx(sid);
+		struct arm_smmu_strtab_l2 *l2ptr;
+
+		if (l1_idx >= cfg->l2.num_l1_ents)
+			return -E2BIG;
+
+		host_ste_ptr = &host_ste_copy;
+		ret = smmu_get_host_l2_ste(smmu, sid, host_ste_ptr);
+		if (ret)
+			return ret;
+
+		if (!l1tab[l1_idx].l2ptr) {
+			struct arm_smmu_strtab_l2 *l2table;
+
+			/* No hypervisor entry, first time the L2 is populated. */
+			l2table = kvm_iommu_donate_pages(get_order(sizeof(*l2table)));
+			if (!l2table)
+				return -ENOMEM;
+			arm_smmu_write_strtab_l1_desc(&l1tab[l1_idx], hyp_virt_to_phys(l2table));
+		}
+		l2ptr = hyp_phys_to_virt(le64_to_cpu(l1tab[l1_idx].l2ptr) &
+				STRTAB_L1_DESC_L2PTR_MASK);
+		hyp_ste_ptr = &l2ptr->stes[arm_smmu_strtab_l2_idx(sid)];
+	}
+
+	memcpy(hyp_ste_ptr->data, host_ste_ptr->data, STRTAB_STE_DWORDS << 3);
+
+	return 0;
+}
+
 static int smmu_init_strtab(struct hyp_arm_smmu_v3_device *smmu)
 {
 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
@@ -419,8 +507,14 @@ static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *comman
 
 	switch (type) {
 	case CMDQ_OP_CFGI_STE:
-		/* TBD: SHADOW_STE*/
+	{
+		u32 sid = FIELD_GET(CMDQ_CFGI_0_SID, command[0]);
+		u32 leaf = FIELD_GET(CMDQ_CFGI_1_LEAF, command[1]);
+
+		if (smmu_reshadow_ste(smmu, sid, leaf))
+			return true;
 		break;
+	}
 	case CMDQ_OP_CFGI_ALL:
 	{
 		/*
@@ -618,25 +712,33 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 			val = smmu->cmdq_host.llq.cons | (CMDQ_CONS_ERR & cons);
 			goto out_update_regs;
 		}
-	/* Passthrough the register access for bisectiblity, handled later */
 	case ARM_SMMU_STRTAB_BASE:
+		if (len != sizeof(u64))
+			break;
 		if (is_write) {
 			/* Must only be written when SMMU_CR0.SMMUEN == 0.*/
 			if (is_smmu_enabled(smmu))
 				break;
 			smmu->host_ste_base = val;
+			goto out_ret;
+		} else {
+			val = smmu->host_ste_base;
+			goto out_update_regs;
 		}
-		mask = read_write;
-		break;
 	case ARM_SMMU_STRTAB_BASE_CFG:
+		if (len != sizeof(u32))
+			break;
 		if (is_write) {
 			/* Must only be written when SMMU_CR0.SMMUEN == 0.*/
 			if (is_smmu_enabled(smmu))
 				break;
 			smmu->host_ste_cfg = val;
+			goto out_ret;
+		} else {
+			val = smmu->host_ste_cfg;
+			goto out_update_regs;
 		}
-		mask = read_write;
-		break;
+	/* Passthrough the register access for bisectiblity, handled later */
 	case ARM_SMMU_GBPA:
 		mask = read_write;
 		break;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#80 lore raw

PATCH v6 20/25 iommu/arm-smmu-v3-kvm: Share other queues

In reply to Mostafa Saleh

Other queues as PRIQ and EVTQ doesn't need to be shadowed. However, we need to make sure they are in a state that disallow them to be donated to the hypervisor or guests. So, keep track of those and share them when they get enabled.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 62 ++++++++++++++++++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h  |  4 ++
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +60 -2
index d92811ef2af5..e258690384f4 100644
@@ -69,6 +69,16 @@ static bool is_smmu_enabled(struct hyp_arm_smmu_v3_device *smmu)
 	return FIELD_GET(CR0_SMMUEN, smmu->cr0);
 }
 
+static bool is_evtq_enabled(struct hyp_arm_smmu_v3_device *smmu)
+{
+	return FIELD_GET(CR0_EVTQEN, smmu->cr0);
+}
+
+static bool is_priq_enabled(struct hyp_arm_smmu_v3_device *smmu)
+{
+	return FIELD_GET(CR0_PRIQEN, smmu->cr0);
+}
+
 /*
  * CMDQ, STE host copies are accessed by the hypervisor, we share them to
  * - Prevent the host from passing protected VM memory.
@@ -647,6 +657,14 @@ static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
 				   cmdq_size(&smmu->cmdq_host)));
 }
 
+static void smmu_emulate_queue(unsigned long q_base, size_t ent_size_shift)
+{
+	phys_addr_t base = q_base & Q_BASE_ADDR_MASK;
+	size_t size = 1UL << (FIELD_GET(Q_BASE_LOG2SIZE, q_base) + ent_size_shift);
+
+	WARN_ON(smmu_share_pages(base ,size));
+}
+
 static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 			     struct user_pt_regs *regs,
 			     u64 esr, u32 off)
@@ -748,12 +766,31 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		if (is_write) {
 			bool last_cmdq_en = is_cmdq_enabled(smmu);
 			bool last_smmu_en = is_smmu_enabled(smmu);
+			bool last_evtq_en = is_evtq_enabled(smmu);
+			bool last_priq_en = is_priq_enabled(smmu);
 
 			smmu->cr0 = val;
 			if (!last_cmdq_en && is_cmdq_enabled(smmu))
 				smmu_emulate_cmdq_enable(smmu);
 			else if (last_cmdq_en && !is_cmdq_enabled(smmu))
 				smmu_emulate_cmdq_disable(smmu);
+
+			/*
+			 * Share PRI and EVTQ to avoid the host using them to write to
+			 * protected memory. However, panic on disable for those queues
+			 * as that is more complicated, unsharing from here can lead to
+			 * use-after-unshare issues, and requires ordering with cr0ack.
+			 * As the host never disable those queues, don't support that.
+			 */
+			if (!last_evtq_en && is_evtq_enabled(smmu))
+				smmu_emulate_queue(smmu->evtq_base, EVTQ_ENT_SZ_SHIFT);
+			else if (last_evtq_en && !is_evtq_enabled(smmu))
+				WARN_ON(1);
+			if (!last_priq_en && is_priq_enabled(smmu))
+				smmu_emulate_queue(smmu->priq_base, PRIQ_ENT_SZ_SHIFT);
+			else if (last_priq_en && !is_priq_enabled(smmu))
+				WARN_ON(1);
+
 			if (!last_smmu_en && is_smmu_enabled(smmu))
 				smmu_emulate_enable(smmu);
 			else if (last_smmu_en && !is_smmu_enabled(smmu))
@@ -779,6 +816,29 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		mask = read_write;
 		break;
 	}
+	case ARM_SMMU_EVTQ_BASE:
+		if (len != sizeof(u64))
+			break;
+
+		if (is_write) {
+			if (is_evtq_enabled(smmu))
+				break;
+			smmu->evtq_base = val;
+		}
+		mask = read_write;
+		break;
+
+	case ARM_SMMU_PRIQ_BASE:
+		if (len != sizeof(u64))
+			break;
+
+		if (is_write) {
+			if (is_priq_enabled(smmu))
+				break;
+			smmu->priq_base = val;
+		}
+		mask = read_write;
+		break;
 
 	/* Allowed 32 bit registers. */
 	case ARM_SMMU_EVTQ_PROD + SZ_64K:
@@ -801,9 +861,7 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		mask = read_write;
 		break;
 	/* Allowed 64 bit registers. */
-	case ARM_SMMU_EVTQ_BASE:
 	case ARM_SMMU_EVTQ_IRQ_CFG0:
-	case ARM_SMMU_PRIQ_BASE:
 	case ARM_SMMU_PRIQ_IRQ_CFG0:
 	case ARM_SMMU_GERROR_IRQ_CFG0:
 		if (len != sizeof(u64))
drivers/iommu/arm/arm-smmu-v3/pkvm/arm_smmu_v3.h +4 -0
index 6a73cf6b8873..e811d51bdfaa 100644
@@ -30,6 +30,8 @@
  * @host_ste_cfg	Host stream table config
  * @host_ste_base	Host stream table base
  * @strtab_cfg		Stream table as seen by HW
+ * @evtq_base		Host evtq base reg
+ * @priq_base		Host priq base reg
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -52,6 +54,8 @@ struct hyp_arm_smmu_v3_device {
 	u64			host_ste_cfg;
 	u64			host_ste_base;
 	struct arm_smmu_strtab_cfg strtab_cfg;
+	unsigned long		evtq_base;
+	unsigned long		priq_base;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#81 lore raw

PATCH v6 21/25 iommu/arm-smmu-v3-kvm: Emulate GBPA

In reply to Mostafa Saleh

The last bit of emulation is GBPA. it must be always set to ABORT, as when the SMMU is disabled it’s not allowed for the host to bypass the SMMU.

That's is done by setting the GBPA to ABORT at init time, and host writes are always ignored and host reads always return ABORT.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 32 +++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +29 -3
index e258690384f4..1ed5ccce7849 100644
@@ -126,6 +126,22 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)
 	return 0;
 }
 
+static int smmu_abort_gbpa(struct hyp_arm_smmu_v3_device *smmu)
+{
+	int ret;
+	u32 reg;
+
+	ret = smmu_wait(false,
+			(readl_relaxed(smmu->base + ARM_SMMU_GBPA) & GBPA_UPDATE) == 0);
+	if (ret)
+		return ret;
+
+	reg = readl_relaxed(smmu->base + ARM_SMMU_GBPA);
+	writel_relaxed(GBPA_UPDATE | GBPA_ABORT | reg, smmu->base + ARM_SMMU_GBPA);
+	return smmu_wait(false,
+			 (readl_relaxed(smmu->base + ARM_SMMU_GBPA) & GBPA_UPDATE) == 0);
+}
+
 static bool smmu_cmdq_has_space(struct arm_smmu_queue *cmdq, u32 n)
 {
 	struct arm_smmu_ll_queue *llq = &cmdq->llq;
@@ -468,6 +484,10 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 	if (ret)
 		goto out_ret;
 
+	ret = smmu_abort_gbpa(smmu);
+	if (ret)
+		goto out_ret;
+
 	return 0;
 
 out_ret:
@@ -756,10 +776,16 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 			val = smmu->host_ste_cfg;
 			goto out_update_regs;
 		}
-	/* Passthrough the register access for bisectiblity, handled later */
 	case ARM_SMMU_GBPA:
-		mask = read_write;
-		break;
+		if (len != sizeof(u32))
+			break;
+
+		/* Ignore write, always read to abort. */
+		if (!is_write) {
+			val = GBPA_ABORT;
+			goto out_update_regs;
+		}
+		goto out_ret;
 	case ARM_SMMU_CR0:
 		if (len != sizeof(u32))
 			break;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#82 lore raw

PATCH v6 22/25 iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

In reply to Mostafa Saleh

To support DMA isolation in pKVM through SMMUv3 nested trap and emulate, io-pgtable-arm needs to be compiled for the hypervisor to create the SMMUs page tables.

Instead of factoring out the kernel-specific code and providing parallel implementations for the hypervisor, we directly utilize and abstract the differences within the iommu-pages API.

This introduces a set of hypervisor-specific wrappers in iommu-pages.h when compiled under __KVM_NVHE_HYPERVISOR__, routing allocations, frees, virt/phys conversions, and DMA API mapping to the appropriate pKVM hypervisor functions (kvm_iommu_donate_pages, etc). The generic kernel definitions are now appropriately excluded in this case.

It also introduces kvm_alloc_io_pgtable_ops at the end of io-pgtable-arm.c to instantiate the page table for pKVM and adds the io-pgtable-arm.o object to the hypervisor Makefile.

---
 arch/arm64/kvm/hyp/nvhe/Makefile |  3 +-
 drivers/iommu/io-pgtable-arm.c   | 31 +++++++++++++++-
 drivers/iommu/io-pgtable-arm.h   |  6 +++
 drivers/iommu/iommu-pages.h      | 63 ++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 3 deletions(-)
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index 8a75739db947..4e9e0f1ed2b5 100644
@@ -36,7 +36,8 @@ hyp-obj-y += $(lib-objs)
 HYP_SMMU_V3_DRV_PATH = ../../../../../drivers/iommu/arm/arm-smmu-v3
 
 hyp-obj-$(CONFIG_ARM_SMMU_V3_PKVM) += $(HYP_SMMU_V3_DRV_PATH)/pkvm/arm-smmu-v3.o \
-	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o
+	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o \
+	$(HYP_SMMU_V3_DRV_PATH)/../../io-pgtable-arm.o
 
 # Path to simple_ring_buffer.c
 CFLAGS_trace.nvhe.o += -I$(srctree)/kernel/trace/
drivers/iommu/io-pgtable-arm.c +29 -2
index e765021308f9..8a0ffea3ae2c 100644
@@ -252,10 +252,14 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 				    void *cookie)
 {
 	struct device *dev = cfg->iommu_dev;
+	int nid = NUMA_NO_NODE;
 	size_t alloc_size;
 	dma_addr_t dma;
 	void *pages;
 
+	if (dev)
+		nid = dev_to_node(dev);
+
 	/*
 	 * For very small starting-level translation tables the HW requires a
 	 * minimum alignment of at least 64 to cover all cases.
@@ -264,8 +268,7 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 	if (cfg->alloc)
 		pages = cfg->alloc(cookie, alloc_size, gfp);
 	else
-		pages = iommu_alloc_pages_node_sz(dev_to_node(dev), gfp,
-						  alloc_size);
+		pages = iommu_alloc_pages_node_sz(nid, gfp, alloc_size);
 
 	if (!pages)
 		return NULL;
@@ -1262,3 +1265,27 @@ struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
 	.alloc	= arm_mali_lpae_alloc_pgtable,
 	.free	= arm_lpae_free_pgtable,
 };
+
+#ifdef __KVM_NVHE_HYPERVISOR__
+#include <nvhe/iommu.h>
+
+struct io_pgtable_ops *kvm_alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
+						struct io_pgtable_cfg *cfg,
+						void *cookie)
+{
+	struct io_pgtable *iop;
+
+	if (fmt != ARM_64_LPAE_S2)
+		return NULL;
+
+	iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
+	if (!iop)
+		return NULL;
+
+	iop->fmt        = fmt;
+	iop->cookie     = cookie;
+	iop->cfg        = *cfg;
+
+	return &iop->ops;
+}
+#endif
drivers/iommu/io-pgtable-arm.h +6 -0
index ba7cfdf7afa0..af3a3f1e765e 100644
@@ -27,4 +27,10 @@
 #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
 #define ARM_LPAE_TCR_PS_52_BIT		0x6ULL
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+struct io_pgtable_ops *kvm_alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
+						struct io_pgtable_cfg *cfg,
+						void *cookie);
+#endif
+
 #endif /* IO_PGTABLE_ARM_H_ */
drivers/iommu/iommu-pages.h +63 -0

73 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#83 lore raw

PATCH v6 23/25 iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

In reply to Mostafa Saleh

Based on the callbacks from the hypervisor, update the SMMUv3 Identity mapped page table.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 197 +++++++++++++++++-
 1 file changed, 195 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +195 -2
index 1ed5ccce7849..b73a2462f0dd 100644
@@ -13,6 +13,9 @@
 
 #include "arm_smmu_v3.h"
 
+#include <linux/io-pgtable.h>
+#include "../../../io-pgtable-arm.h"
+
 size_t __ro_after_init kvm_hyp_arm_smmu_v3_count;
 struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;
 
@@ -59,6 +62,9 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;
 	__ret;								\
 })
 
+/* Protected by host_mmu.lock from core code. */
+static struct io_pgtable *idmap_pgtable;
+
 static bool is_cmdq_enabled(struct hyp_arm_smmu_v3_device *smmu)
 {
 	return FIELD_GET(CR0_CMDQEN, smmu->cr0);
@@ -210,7 +216,6 @@ static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu)
 			 smmu_cmdq_empty(&smmu->cmdq));
 }
 
-__maybe_unused
 static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 			 struct arm_smmu_cmdq_ent *cmd)
 {
@@ -222,6 +227,78 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 	return smmu_sync_cmd(smmu);
 }
 
+static void __smmu_add_cmd(void *__opaque, struct arm_smmu_cmdq_batch *unused,
+			   struct arm_smmu_cmdq_ent *cmd)
+{
+	struct hyp_arm_smmu_v3_device *smmu = (struct hyp_arm_smmu_v3_device *)__opaque;
+
+	WARN_ON(smmu_add_cmd(smmu, cmd));
+}
+
+static int smmu_tlb_inv_range_smmu(struct hyp_arm_smmu_v3_device *smmu,
+				   struct arm_smmu_cmdq_ent *cmd,
+				   unsigned long iova, size_t size, size_t granule)
+{
+	arm_smmu_tlb_inv_build(cmd, iova, size, granule,
+			       PAGE_SHIFT, smmu->features & ARM_SMMU_FEAT_RANGE_INV,
+			       smmu, __smmu_add_cmd, NULL);
+	return smmu_sync_cmd(smmu);
+}
+
+static void smmu_tlb_inv_range(unsigned long iova, size_t size, size_t granule,
+			       bool leaf)
+{
+	struct arm_smmu_cmdq_ent cmd_s1 = {
+		.opcode = CMDQ_OP_TLBI_NH_ALL,
+		.tlbi = {
+			.vmid = 0,
+		},
+	};
+	struct hyp_arm_smmu_v3_device *smmu;
+
+	for_each_smmu(smmu) {
+		struct arm_smmu_cmdq_ent cmd = {
+			.opcode = CMDQ_OP_TLBI_S2_IPA,
+			.tlbi = {
+				.leaf = leaf,
+				.vmid = 0,
+			},
+		};
+
+		hyp_spin_lock(&smmu->lock);
+		/*
+		 * Don't bother if SMMU is disabled, this would be useful for the case
+		 * when RPM is supported to avoid touching the SMMU MMIO when disabled.
+		 * The hypervisor also asserts CMDQEN is enabled before the SMMU is
+		 * enabled. As otherwise the host can prevent the hypervisor from doing
+		 * TLB invalidations.
+		 */
+		if (is_smmu_enabled(smmu)) {
+			WARN_ON(smmu_tlb_inv_range_smmu(smmu, &cmd, iova, size, granule));
+			WARN_ON(smmu_send_cmd(smmu, &cmd_s1));
+		}
+		hyp_spin_unlock(&smmu->lock);
+	}
+}
+
+static void smmu_tlb_flush_walk(unsigned long iova, size_t size,
+				size_t granule, void *cookie)
+{
+	smmu_tlb_inv_range(iova, size, granule, false);
+}
+
+static void smmu_tlb_add_page(struct iommu_iotlb_gather *gather,
+			      unsigned long iova, size_t granule,
+			      void *cookie)
+{
+	smmu_tlb_inv_range(iova, granule, granule, true);
+}
+
+static const struct iommu_flush_ops smmu_tlb_ops = {
+	.tlb_flush_walk = smmu_tlb_flush_walk,
+	.tlb_add_page	= smmu_tlb_add_page,
+};
+
 /* Put the device in a state that can be probed by the host driver. */
 static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu)
 {
@@ -495,6 +572,37 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 	return ret;
 }
 
+static int smmu_init_pgt(void)
+{
+	/* Default values overridden based on SMMUs common features. */
+	struct io_pgtable_cfg cfg = (struct io_pgtable_cfg) {
+		.tlb = &smmu_tlb_ops,
+		.pgsize_bitmap = -1,
+		.ias = 48,
+		.oas = 48,
+		.coherent_walk = true,
+	};
+	struct hyp_arm_smmu_v3_device *smmu;
+	struct io_pgtable_ops *ops;
+
+	for_each_smmu(smmu) {
+		cfg.ias = min(cfg.ias, smmu->oas);
+		cfg.oas = min(cfg.oas, smmu->oas);
+		cfg.pgsize_bitmap &= smmu->pgsize_bitmap;
+		cfg.coherent_walk &= !!(smmu->features & ARM_SMMU_FEAT_COHERENCY);
+	}
+
+	/* At least PAGE_SIZE must be supported by all SMMUs*/
+	if ((cfg.pgsize_bitmap & PAGE_SIZE) == 0)
+		return -EINVAL;
+
+	ops = kvm_alloc_io_pgtable_ops(ARM_64_LPAE_S2, &cfg, NULL);
+	if (!ops)
+		return -ENOMEM;
+	idmap_pgtable = io_pgtable_ops_to_pgtable(ops);
+	return 0;
+}
+
 /* Called while is the host is still trusted. */
 static int smmu_init(void)
 {
@@ -520,7 +628,10 @@ static int smmu_init(void)
 
 	BUILD_BUG_ON(sizeof(hyp_spinlock_t) != sizeof(u32));
 
-	return 0;
+	ret = smmu_init_pgt();
+	if (ret)
+		goto out_reclaim_smmu;
+	return ret;
 
 out_reclaim_smmu:
 	while (smmu != kvm_hyp_arm_smmu_v3_smmus)
@@ -950,8 +1061,90 @@ static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)
 	return false;
 }
 
+static size_t smmu_pgsize_idmap(size_t size, u64 paddr, size_t pgsize_bitmap)
+{
+	size_t pgsizes;
+
+	/* Remove page sizes that are larger than the current size */
+	pgsizes = pgsize_bitmap & GENMASK_ULL(__fls(size), 0);
+
+	/* Remove page sizes that the address is not aligned to. */
+	if (likely(paddr))
+		pgsizes &= GENMASK_ULL(__ffs(paddr), 0);
+
+	WARN_ON(!pgsizes);
+
+	/* Return the largest page size that fits. */
+	return BIT(__fls(pgsizes));
+}
+
 static int smmu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, int prot)
 {
+	size_t pgsize = PAGE_SIZE, pgcount, size;
+	struct io_pgtable *pgtable = idmap_pgtable;
+	int ret = 0;
+
+	end = min(end, BIT(pgtable->cfg.oas));
+	if (start >= end)
+		return 0;
+
+	size = end - start;
+	if (prot) {
+		size_t mapped;
+
+		if (!(prot & IOMMU_MMIO))
+			prot |= IOMMU_CACHE;
+
+		while (size) {
+			mapped = 0;
+			/*
+			 * We handle pages size for memory and MMIO differently:
+			 * - memory: Map everything with PAGE_SIZE, that is guaranteed to
+			 *   find memory as we allocated enough pages to cover the entire
+			 *   memory, we do that as io-pgtable-arm doesn't support
+			 *   split_blk_unmap logic any more, so we can't break blocks once
+			 *   mapped to tables.
+			 * - MMIO: Unlike memory, pKVM allocate 1G to for all MMIO, while
+			 *   the MMIO space can be large, as it is assumed to cover the
+			 *   whole IAS that is not memory, we have to use block mappings,
+			 *   that is fine for MMIO as it is never donated at the moment,
+			 *   so we never need to unmap MMIO at the run time triggereing
+			 *   split block logic.
+			 */
+			if (prot & IOMMU_MMIO)
+				pgsize = smmu_pgsize_idmap(size, start, pgtable->cfg.pgsize_bitmap);
+
+			pgcount = size / pgsize;
+			ret = pgtable->ops.map_pages(&pgtable->ops, start, start,
+						     pgsize, pgcount, prot, 0, &mapped);
+			size -= mapped;
+			start += mapped;
+			/* Map failures doesn't impact security, tolerate it. */
+			if (!mapped || ret)
+				break;
+		}
+	} else {
+		struct iommu_iotlb_gather gather;
+		size_t unmapped;
+
+		while (size) {
+			pgcount = size / pgsize;
+			iommu_iotlb_gather_init(&gather);
+			unmapped = pgtable->ops.unmap_pages(&pgtable->ops, start,
+							    pgsize, pgcount, &gather);
+			size -= unmapped;
+			start += unmapped;
+			if (!unmapped)
+				break;
+		}
+	}
+
+	if (ret)
+		return ret;
+
+	if (WARN_ON(size))
+		return -EINVAL;
+
 	return 0;
 }
 
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#84 lore raw

PATCH v6 24/25 iommu/arm-smmu-v3-kvm: Enable nesting

In reply to Mostafa Saleh

Now, as the hypervisor controls the command queue, stream table, and shadows the stage-2 page table.
Enable stage-2 in case the host puts an STE in bypass or stage-1.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 108 ++++++++++++++++--
 1 file changed, 101 insertions(+), 7 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +101 -7
index b73a2462f0dd..3d727d6dfbf0 100644
@@ -411,6 +411,57 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_attach_stage_2(struct arm_smmu_ste *ste)
+{
+	unsigned long vttbr;
+	unsigned long ts, sl, ic, oc, sh, tg, ps;
+	unsigned long cfg;
+	struct io_pgtable_cfg *pgt_cfg =  &idmap_pgtable->cfg;
+
+	cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ste->data[0]));
+	if (!FIELD_GET(STRTAB_STE_0_V, le64_to_cpu(ste->data[0])) ||
+	    (cfg == STRTAB_STE_0_CFG_ABORT)) {
+		ste->data[2] = 0;
+		ste->data[3] = 0;
+		return 0;
+	}
+	/* S2 is not advertised, that should never be attempted. */
+	if (cfg == STRTAB_STE_0_CFG_NESTED)
+		return -EINVAL;
+	vttbr = pgt_cfg->arm_lpae_s2_cfg.vttbr;
+	ps = pgt_cfg->arm_lpae_s2_cfg.vtcr.ps;
+	tg = pgt_cfg->arm_lpae_s2_cfg.vtcr.tg;
+	sh = pgt_cfg->arm_lpae_s2_cfg.vtcr.sh;
+	oc = pgt_cfg->arm_lpae_s2_cfg.vtcr.orgn;
+	ic = pgt_cfg->arm_lpae_s2_cfg.vtcr.irgn;
+	sl = pgt_cfg->arm_lpae_s2_cfg.vtcr.sl;
+	ts = pgt_cfg->arm_lpae_s2_cfg.vtcr.tsz;
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_SHCFG);
+	ste->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING));
+
+	/* The host shouldn't write dwords 2 and 3, overwrite them. */
+	ste->data[2] = cpu_to_le64(FIELD_PREP(STRTAB_STE_2_VTCR,
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2PS, ps) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2TG, tg) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2SH0, sh) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2OR0, oc) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2IR0, ic) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, sl) |
+				  FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, ts)) |
+		 FIELD_PREP(STRTAB_STE_2_S2VMID, 0) |
+		 STRTAB_STE_2_S2AA64 | STRTAB_STE_2_S2R |
+ #ifdef __BIG_ENDIAN
+		STRTAB_STE_2_S2ENDI |
+#endif
+		STRTAB_STE_2_S2PTW);
+
+	ste->data[3] = cpu_to_le64(vttbr & STRTAB_STE_3_S2TTB_MASK);
+	/* Convert S1 => nested and bypass => S2 */
+	ste->data[0] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_0_CFG, cfg | BIT(1)));
+	return 0;
+}
+
 static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid,
 				struct arm_smmu_ste *host_ste_out)
 {
@@ -440,9 +491,18 @@ static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid,
 static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool leaf)
 {
 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
-	struct arm_smmu_ste *hyp_ste_ptr, *host_ste_ptr, host_ste_copy;
+	struct arm_smmu_ste *hyp_ste_ptr;
 	u64 *hyp_ste_base = strtab_hyp_base(smmu);
-	int ret;
+	struct arm_smmu_ste target = {};
+	struct arm_smmu_cmdq_ent cfgi_cmd = {
+		.opcode	= CMDQ_OP_CFGI_STE,
+		.cfgi	= {
+			.sid	= sid,
+			.leaf	= true,
+		},
+	};
+	bool cur_valid, target_valid;
+	int i, ret;
 
 	/*
 	 * Linux only uses leaf = 1, when leaf is 0, we need to verify that this
@@ -463,7 +523,7 @@ static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool
 			return -E2BIG;
 
 		hyp_ste_ptr = &hyp_table[sid];
-		host_ste_ptr = &host_table[sid];
+		memcpy(target.data, host_table[sid].data, STRTAB_STE_DWORDS << 3);
 	} else {
 		struct arm_smmu_strtab_l1 *l1tab = (struct arm_smmu_strtab_l1 *)hyp_ste_base;
 		u32 l1_idx = arm_smmu_strtab_l1_idx(sid);
@@ -472,8 +532,7 @@ static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool
 		if (l1_idx >= cfg->l2.num_l1_ents)
 			return -E2BIG;
 
-		host_ste_ptr = &host_ste_copy;
-		ret = smmu_get_host_l2_ste(smmu, sid, host_ste_ptr);
+		ret = smmu_get_host_l2_ste(smmu, sid, &target);
 		if (ret)
 			return ret;
 
@@ -491,9 +550,44 @@ static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool
 		hyp_ste_ptr = &l2ptr->stes[arm_smmu_strtab_l2_idx(sid)];
 	}
 
-	memcpy(hyp_ste_ptr->data, host_ste_ptr->data, STRTAB_STE_DWORDS << 3);
 
-	return 0;
+	/*
+	 * Summary of each host emulated state vs real HW.
+	 * |	Host	|	HW	|
+	 * ==============================
+	 * |	V=0	|	V=0	|
+	 * |	Abort	|	Abort	|
+	 * |	Bypass	|	S2	|
+	 * |	S1	|	S1+S2	|
+	 *
+	 * For the host, any V=0 transition is not hitless, all other permutations of
+	 * (abort, bypass, S1) transitions are hitless.
+	 * For the HW state, any V=0 transition is not hitless, as all the S2 config is
+	 * always the same (ttbr, vtcr...), all other transitions should be hitless too.
+	 * However, the host is not trusted, which means that any V=0 <=> V=1 transitions
+	 * we need to enforce writing order of the STE and add CFGI.
+	 */
+	cur_valid = FIELD_GET(STRTAB_STE_0_V, le64_to_cpu(hyp_ste_ptr->data[0]));
+	ret = smmu_attach_stage_2(&target);
+	if (ret)
+		return ret;
+	target_valid = FIELD_GET(STRTAB_STE_0_V, le64_to_cpu(target.data[0]));
+	if (cur_valid && !target_valid) {
+		WRITE_ONCE(hyp_ste_ptr->data[0], target.data[0]);
+		WARN_ON(smmu_send_cmd(smmu, &cfgi_cmd));
+		for (i = 1; i < STRTAB_STE_DWORDS; i++)
+			WRITE_ONCE(hyp_ste_ptr->data[i], target.data[i]);
+	} else if (!cur_valid && target_valid) {
+		for (i = 1; i < STRTAB_STE_DWORDS; i++)
+			WRITE_ONCE(hyp_ste_ptr->data[i], target.data[i]);
+		WARN_ON(smmu_send_cmd(smmu, &cfgi_cmd));
+		WRITE_ONCE(hyp_ste_ptr->data[0], target.data[0]);
+	} else {
+		for (i = 0; i < STRTAB_STE_DWORDS; i++)
+			WRITE_ONCE(hyp_ste_ptr->data[i], target.data[i]);
+	}
+
+	return smmu_send_cmd(smmu, &cfgi_cmd);
 }
 
 static int smmu_init_strtab(struct hyp_arm_smmu_v3_device *smmu)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#85 lore raw

PATCH v6 25/25 KVM: arm64: Add documentation for pKVM DMA isolation

In reply to Mostafa Saleh

Populate the section for DMA isolation in pKVM with the newly added KVM IOMMU and pKVM SMMUv3 driver details.

---
 Documentation/virt/kvm/arm/pkvm.rst | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
Documentation/virt/kvm/arm/pkvm.rst +18 -1
index 514992a79a83..46e5c553646b 100644
@@ -77,7 +77,24 @@ Status: **Unimplemented.**
 DMA isolation using an IOMMU
 ----------------------------
 
-Status: **Unimplemented.**
+Status: Supported for devices behind SMMUv3 supporting dual stages
+of translation.
+
+With ``CONFIG_ARM_SMMU_V3_PKVM``, the hypervisor will take over the SMMUs
+on the system and provide an architectural emulation to the kernel SMMUv3
+driver transparently.
+
+If some devices are not behind an IOMMU or behind another IOMMU architecture,
+DMA isolation is not supported, as a driver must be provided for that.
+
+DMA isolation is enforced by dual stage of translation; similar to the CPU
+where a driver can register their ops through ``kvm_iommu_register_driver``
+and implement ``host_stage2_idmap`` to shadow the CPU page table.
+
+This implementation trusts the systems firmware not to allow the untrusted
+host kernel to bypass the SMMUv3.
+For example by resetting the power. In that case, it is the firmware
+responsibility to save/restore the SMMUv3 state.
 
 Proxying of Trustzone services
 ------------------------------
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#32 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Mostafa Saleh
Quoted 4 lines · Mostafa Saleh

To prepare for supporting io-pgtable-arm in the pKVM hypervisor, we need to abstract away standard kernel allocations, frees, virt/phys conversions, and DMA API mapping.

Quoted 8 lines

This patch introduces a set of generic wrappers in iommu-pages.h:
- iommu_alloc_data
- iommu_free_data
- iommu_virt_to_phys
- iommu_phys_to_virt
- iommu_pages_dma_map
- iommu_pages_dma_mapping_error
- iommu_pages_dma_unmap

Wah? This has nothing to do with iommu pages? This just leaking everything iommu pages abstracted out :(

When I said to use iommu-pages, I meant to use the existing API, not a completely different one.

From an iommu-pages perspective the issue is this code open codes dma_map_single()/etc instead of using the API surface iommu_pages_start_incoherent()

This is annoying to fix beacuse the external allocator messes it up, but I think with some #ifdef you can probably fix it up.

So.. I suggest you update it to use the iommu_pages API, #ifdef out the allocator so the pkvm pkvm doesn't need to deal with it. Then compile a special iommu-pages for the pkvm side presenting the same API.

You should have a pkvm shim header that provides kmalloc/kfree/virt_to_phys in the normal way and just #include that in io-pgtable when doing a pkvm build instead of hacking up all the code.

Jason

#12 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh
Quoted 3 lines · Mostafa Saleh

Range TLB invalidation has a very specific algorithm. Instead of re-writing it for the hypervisor, move it to a function that can be re-used.

I think this is too narrow.

You should start at __arm_smmu_domain_inv_range() and shove all of that callchain into a new file "arm-smmuv3-tlbi.c" which you can then double compile for pkvm.

pkvm would have to present the tlbi description and the invs array which shouldn't be hard for it. Then it will enjoy all the same hypervisor optimizations we are working on for the normal driver.

I am about to send a patch series here for iommupt that significantly alters this. I think it will help your pkvm effort as the invalidation entry point becomes significantly decoupled from the
iommu subsystem:

static void arm_smmu_domain_tlbi_inv(struct arm_smmu_tlbi *tlbi,
struct arm_smmu_invs *invs)

struct arm_smmu_tlbi {
	struct arm_smmu_domain *smmu_domain; // Can be removed 
	unsigned long start;
	unsigned long last;
	u8 leaf_levels_bitmap;
	u8 table_levels_bitmap;
};

Which pkvm should have no trouble invoking. It has to build an invs, but I guess that is pretty simple and done once at boot for pkvm?

Once done all the fiddly bits about building the commands would be shared. There is really no reason this should differ anyhow.

https://github.com/jgunthorpe/linux/commits/iommu_pt_arm64/

cover-letter: Organize SMMUv3 the invalidation flow so iommupt can use it

Jason

#9 lore raw

Re: [PATCH v6 03/25] iommu/arm-smmu-v3: Split code with hyp

In reply to Mostafa Saleh

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c line 17

Quoted 4 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c new file +4 -0
+int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
+{
+	memset(cmd, 0, 1 << CMDQ_ENT_SZ_SHIFT);
+	cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode);

Hopefully I get to it today, but I am going to post a series that will move this logic into the headers while deleting arm_smmu_cmdq_ent. It should be directly usable for pkvm as well to achieve this same split without having to shove a giant inline into a header file.

https://github.com/jgunthorpe/linux/commits/iommu_pt_arm64/

cover-letter: Remove SMMUv3 struct arm_smmu_cmdq_ent

Jason

#22 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

Jason

#70 lore raw

Re: [PATCH v6 13/25] iommu/arm-smmu-v3-kvm: Probe SMMU HW

In reply to Mostafa Saleh
Quoted 22 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -6
index 61e6ab364086..157acde0436d 100644
@@ -4738,12 +4738,6 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
 	return 0;
 }
 
-#define IIDR_IMPLEMENTER_ARM		0x43b
-#define IIDR_PRODUCTID_ARM_MMU_600	0x483
-#define IIDR_PRODUCTID_ARM_MMU_700	0x487
-#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
-#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
-
 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu)
 {
 	u32 reg;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0 -0
index 64618299d03a..f904f4d19609 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_vsmmu; · line 93

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
@@ -84,6 +84,12 @@ struct arm_vsmmu;
 #define IIDR_REVISION			GENMASK(15, 12)
 #define IIDR_IMPLEMENTER		GENMASK(11, 0)
 
+#define IIDR_IMPLEMENTER_ARM		0x43b
+#define IIDR_PRODUCTID_ARM_MMU_600	0x483
+#define IIDR_PRODUCTID_ARM_MMU_700	0x487
+#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
+#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
+
 #define ARM_SMMU_AIDR			0x1C

Lets put these hunks in some earlier patch to migrate out the functions/etc

I think all these pkvm/arm-smmu-v3.c should just be building up the driver.

Quoted 15 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +15 -0
+static bool smmu_nesting_supported(struct hyp_arm_smmu_v3_device *smmu)
+{
+	unsigned int implementer, productid, variant, revision;
+	u32 reg;
+
+	if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1) ||
+	    !(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
+		return false;
+
+	reg = readl_relaxed(smmu->base + ARM_SMMU_IIDR);
+	implementer = FIELD_GET(IIDR_IMPLEMENTER, reg);
+	productid = FIELD_GET(IIDR_PRODUCTID, reg);
+	variant = FIELD_GET(IIDR_VARIANT, reg);
+	revision = FIELD_GET(IIDR_REVISION, reg);
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_vsmmu;

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +10 -0
+	if (implementer != IIDR_IMPLEMENTER_ARM)
+		return true;
+
+	if (productid == IIDR_PRODUCTID_ARM_MMU_600)
+		return variant >= 2;
+	else if (productid == IIDR_PRODUCTID_ARM_MMU_700)
+		return !(variant < 1 || revision < 1);
+
+	return true;
+}

Why not share all this errata stuff with the idr parsing code too?

We already have ARM_SMMU_FEAT_NESTING that has the above calculation.

The two drivers use the same ARM_SMMU_FEAT system, I would expect one chunk of shared code to compute the FEATs, who cares if pkvm doesn't use all of them?

Use the same errata logic and so on to get to the feat bitmap.

Jason

#42 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 2 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

Is there a reason you can't just use the CPU S2 for the iommu?

ie the CCA RMM is doing that, it is how ARM imagined this stuff would work.

Once you start supporting DMA like this you have no choice but to keep a fully populated at all times S2 around, why not use that for the CPU too to avoid faults?

I guess there is a reason, but maybe explain in the commit message?

It sure would be simpler, you wouldn't have to mess with iopgtable at all...

Jason

#10 lore raw

Re: [PATCH v6 03/25] iommu/arm-smmu-v3: Split code with hyp

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c line 17

Quoted 4 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c new file +4 -0
+int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
+{
+	memset(cmd, 0, 1 << CMDQ_ENT_SZ_SHIFT);
+	cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode);

Hopefully I get to it today, but I am going to post a series that will move this logic into the headers while deleting arm_smmu_cmdq_ent. It should be directly usable for pkvm as well to achieve this same split without having to shove a giant inline into a header file.

Quoted 3 lines

https://github.com/jgunthorpe/linux/commits/iommu_pt_arm64/

cover-letter: Remove SMMUv3 struct arm_smmu_cmdq_ent

I see it’s already posted, I will go through it and provide feedback there.

Thanks,
Mostafa

Quoted 2 lines

Jason

#13 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Jason Gunthorpe
Quoted 23 lines · Jason Gunthorpe
Quoted 3 lines · Mostafa Saleh

Range TLB invalidation has a very specific algorithm. Instead of re-writing it for the hypervisor, move it to a function that can be re-used.

I think this is too narrow.

You should start at __arm_smmu_domain_inv_range() and shove all of that callchain into a new file "arm-smmuv3-tlbi.c" which you can then double compile for pkvm.

pkvm would have to present the tlbi description and the invs array which shouldn't be hard for it. Then it will enjoy all the same hypervisor optimizations we are working on for the normal driver.

I am about to send a patch series here for iommupt that significantly alters this. I think it will help your pkvm effort as the invalidation entry point becomes significantly decoupled from the
iommu subsystem:

static void arm_smmu_domain_tlbi_inv(struct arm_smmu_tlbi *tlbi,
struct arm_smmu_invs *invs)

Quoted 8 lines
struct arm_smmu_tlbi {
	struct arm_smmu_domain *smmu_domain; // Can be removed 
	unsigned long start;
	unsigned long last;
	u8 leaf_levels_bitmap;
	u8 table_levels_bitmap;
};

I am not sure if it’s worth it, the hypervisor is much simpler, there is a single page table, it’s locked (also identity mapped), it’s updated on VM boot/teardown only, we don’t even use iotlb_gather at the moment, although possible but I wanted to keep this series as simple as I can then we can add more features later.
So this patch is the least intrusive change, as whatever the main SMMUv3 driver does, the range tlb invalidation logic is the same. But I am happy to experiment with that when posted.

Thanks,
Mostafa

Quoted 11 lines

Which pkvm should have no trouble invoking. It has to build an invs, but I guess that is pretty simple and done once at boot for pkvm?

Once done all the fiddly bits about building the commands would be shared. There is really no reason this should differ anyhow.

https://github.com/jgunthorpe/linux/commits/iommu_pt_arm64/

cover-letter: Organize SMMUv3 the invalidation flow so iommupt can use it

Jason

#23 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Jason Gunthorpe
Quoted 17 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

This way was easier as each function only returns one thing, otherwise we have to pass stuff by address as we can’t pass the smmu struct as it is not shared between the drivers.
But no strong opinion, I can change that.

Thanks,
Mostafa

Quoted 2 lines

Jason

#33 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Jason Gunthorpe
Quoted 24 lines · Jason Gunthorpe
Quoted 4 lines · Mostafa Saleh

To prepare for supporting io-pgtable-arm in the pKVM hypervisor, we need to abstract away standard kernel allocations, frees, virt/phys conversions, and DMA API mapping.

Quoted 8 lines

This patch introduces a set of generic wrappers in iommu-pages.h:
- iommu_alloc_data
- iommu_free_data
- iommu_virt_to_phys
- iommu_phys_to_virt
- iommu_pages_dma_map
- iommu_pages_dma_mapping_error
- iommu_pages_dma_unmap

Wah? This has nothing to do with iommu pages? This just leaking everything iommu pages abstracted out :(

When I said to use iommu-pages, I meant to use the existing API, not a completely different one.

From an iommu-pages perspective the issue is this code open codes dma_map_single()/etc instead of using the API surface iommu_pages_start_incoherent()

Quoted 7 lines

This is annoying to fix beacuse the external allocator messes it up, but I think with some #ifdef you can probably fix it up.

So.. I suggest you update it to use the iommu_pages API, #ifdef out the allocator so the pkvm pkvm doesn't need to deal with it. Then compile a special iommu-pages for the pkvm side presenting the same API.

I see, we still need to leave the DMA-API calls for the custom config, as I am not sure if it can use pages not backed by the vmemmap, I pushed that into a separate function so it’s easily compiled out.

Without this patch, now it looks like:

drivers/iommu/io-pgtable-arm.c +56 -26
index 0208e5897c29..1583b9916b09 100644
@@ -248,26 +248,15 @@ static dma_addr_t __arm_lpae_dma_addr(void *pages)
 	return (dma_addr_t)virt_to_phys(pages);
 }
 
-static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
-				    struct io_pgtable_cfg *cfg,
-				    void *cookie)
+static void *__arm_lpae_cfg_alloc(size_t size, gfp_t gfp,
+				  struct io_pgtable_cfg *cfg,
+				  void *cookie)
 {
 	struct device *dev = cfg->iommu_dev;
-	size_t alloc_size;
 	dma_addr_t dma;
 	void *pages;
 
-	/*
-	 * For very small starting-level translation tables the HW requires a
-	 * minimum alignment of at least 64 to cover all cases.
-	 */
-	alloc_size = max(size, 64);
-	if (cfg->alloc)
-		pages = cfg->alloc(cookie, alloc_size, gfp);
-	else
-		pages = iommu_alloc_pages_node_sz(dev_to_node(dev), gfp,
-						  alloc_size);
-
+	pages = cfg->alloc(cookie, size, gfp);
 	if (!pages)
 		return NULL;
 
@@ -289,26 +278,67 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 out_unmap:
 	dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
 	dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
-
 out_free:
-	if (cfg->free)
-		cfg->free(cookie, pages, size);
-	else
-		iommu_free_pages(pages);
-
+	cfg->free(cookie, pages, size);
 	return NULL;
 }
 
-static void __arm_lpae_free_pages(void *pages, size_t size,
-				  struct io_pgtable_cfg *cfg,
-				  void *cookie)
+static void __arm_lpae_cfg_free(void *pages, size_t size,
+				struct io_pgtable_cfg *cfg,
+				void *cookie)
 {
 	if (!cfg->coherent_walk)
 		dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
 				 size, DMA_TO_DEVICE);
 
-	if (cfg->free)
-		cfg->free(cookie, pages, size);
+	cfg->free(cookie, pages, size);
+}
+
+static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
+				    struct io_pgtable_cfg *cfg,
+				    void *cookie)
+{
+	struct device *dev = cfg->iommu_dev;
+	size_t alloc_size;
+	void *pages;
+
+	/*
+	 * For very small starting-level translation tables the HW requires a
+	 * minimum alignment of at least 64 to cover all cases.
+	 */
+	alloc_size = max(size, 64);
+	if (cfg->alloc)
+		return __arm_lpae_cfg_alloc(alloc_size, gfp, cfg, cookie);
+
+	pages = iommu_alloc_pages_node_sz(dev_to_node(dev), gfp, alloc_size);
+	if (!pages)
+		return NULL;
+
+	if (!cfg->coherent_walk) {
+		int ret = iommu_pages_start_incoherent(pages, dev);
+
+		if (ret) {
+			if (ret == -EOPNOTSUPP)
+				dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
+			iommu_free_pages(pages);
+			return NULL;
+		}
+	}
+
+	return pages;
+}
+
+static void __arm_lpae_free_pages(void *pages, size_t size,
+				  struct io_pgtable_cfg *cfg,
+				  void *cookie)
+{
+	if (cfg->free) {
+		__arm_lpae_cfg_free(pages, size, cfg, cookie);
+		return;
+	}
+
+	if (!cfg->coherent_walk)
+		iommu_pages_free_incoherent(pages, cfg->iommu_dev);
 	else
 		iommu_free_pages(pages);
 }

Thanks,
Mostafa

Quoted 4 lines

You should have a pkvm shim header that provides kmalloc/kfree/virt_to_phys in the normal way and just #include that in io-pgtable when doing a pkvm build instead of hacking up all the code.

Ok, I can do that in another change, but I believe it's better to change the usage in this file to arm_lpae_*(virt_to_phys...) so it's clear which parts are intended for that.

Thanks,
Mostafa

Quoted 2 lines

Jason

#43 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

Is there a reason you can't just use the CPU S2 for the iommu?

ie the CCA RMM is doing that, it is how ARM imagined this stuff would work.

Quoted 8 lines

Once you start supporting DMA like this you have no choice but to keep a fully populated at all times S2 around, why not use that for the CPU too to avoid faults?

I guess there is a reason, but maybe explain in the commit message?

It sure would be simpler, you wouldn't have to mess with iopgtable at all...

Sharing the page table is tricky, this is something I have been thinking about and my plan was to work on it after this series as it has some constraints and would require core KVM changes.

So far this is the list of requirements/changes needed share the stage-2 page table (besides the obvious: same page table format, granularity, endianness...)

1) HW BBM is not supported in the hypervisor page table, that’s because it can generate TLB conflict aborts, which the hypervisor can not handle because of the limited syndrome information. We can rely on FEAT_BBML3 which was newly introduced to work around that, it’s quite niche and not supported in KVM yet or have an allow list similar to the kernel
(as in cpu_supports_bbml2_noabort()) which also limits the number of CPUs that can run this.

2) Handling page faults, devices must be able to stall and let the hypervisor handle the page fault (which has to proxy through the kernel as the hypervisor doesn’t handle interrupts), this includes also IO page faults which are hard to get right from the HW which and may lead to system stability issues or lockups. Alternatively, we can pin the stage-2 pages, that would require some hypercalls, hacks to the driver/IOMMU API and possibly new semantics in the DMA-API for IDENTITY devices as they will still need to pin the pages as they are actually in stage-2 translation and not bypass.

3) SMMUv3 must be coherent.

4) Support BTM/DVM for TLB invalidation, otherwise some hooks are still required (although not io-pgtable-arm)

This is not the complete list, I am sure I will run into more issues when prototyping this.

IMO, 1, 2 are the most tricky parts. It's more work and runs on very limited systems, However, it can be implemented as an optimization) which is my plan.

I am not sure how CCA deals with that, I’d expect they have a lot of constraints on CPUs/SMMUs and DMA capable devices on those systems.

Thanks,
Mostafa

Quoted 2 lines

Jason

#71 lore raw

Re: [PATCH v6 13/25] iommu/arm-smmu-v3-kvm: Probe SMMU HW

In reply to Jason Gunthorpe
Quoted 37 lines · Jason Gunthorpe
Quoted 22 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -6
index 61e6ab364086..157acde0436d 100644
@@ -4738,12 +4738,6 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
 	return 0;
 }
 
-#define IIDR_IMPLEMENTER_ARM		0x43b
-#define IIDR_PRODUCTID_ARM_MMU_600	0x483
-#define IIDR_PRODUCTID_ARM_MMU_700	0x487
-#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
-#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
-
 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu)
 {
 	u32 reg;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0 -0
index 64618299d03a..f904f4d19609 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_vsmmu; · line 93

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
@@ -84,6 +84,12 @@ struct arm_vsmmu;
 #define IIDR_REVISION			GENMASK(15, 12)
 #define IIDR_IMPLEMENTER		GENMASK(11, 0)
 
+#define IIDR_IMPLEMENTER_ARM		0x43b
+#define IIDR_PRODUCTID_ARM_MMU_600	0x483
+#define IIDR_PRODUCTID_ARM_MMU_700	0x487
+#define IIDR_PRODUCTID_ARM_MMU_L1	0x48a
+#define IIDR_PRODUCTID_ARM_MMU_S3	0x498
+
 #define ARM_SMMU_AIDR			0x1C

Lets put these hunks in some earlier patch to migrate out the functions/etc

Quoted 2 lines

I think all these pkvm/arm-smmu-v3.c should just be building up the driver.

Will do.

Quoted 29 lines
Quoted 16 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +15 -0
+static bool smmu_nesting_supported(struct hyp_arm_smmu_v3_device *smmu)
+{
+	unsigned int implementer, productid, variant, revision;
+	u32 reg;
+
+	if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1) ||
+	    !(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
+		return false;
+
+	reg = readl_relaxed(smmu->base + ARM_SMMU_IIDR);
+	implementer = FIELD_GET(IIDR_IMPLEMENTER, reg);
+	productid = FIELD_GET(IIDR_PRODUCTID, reg);
+	variant = FIELD_GET(IIDR_VARIANT, reg);
+	revision = FIELD_GET(IIDR_REVISION, reg);
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_vsmmu;

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +10 -0
+	if (implementer != IIDR_IMPLEMENTER_ARM)
+		return true;
+
+	if (productid == IIDR_PRODUCTID_ARM_MMU_600)
+		return variant >= 2;
+	else if (productid == IIDR_PRODUCTID_ARM_MMU_700)
+		return !(variant < 1 || revision < 1);
+
+	return true;
+}

Why not share all this errata stuff with the idr parsing code too?

Quoted 7 lines

We already have ARM_SMMU_FEAT_NESTING that has the above calculation.

The two drivers use the same ARM_SMMU_FEAT system, I would expect one chunk of shared code to compute the FEATs, who cares if pkvm doesn't use all of them?

Use the same errata logic and so on to get to the feat bitmap.

Makes sense.

Thanks,
Mostafa

Quoted 2 lines

Jason

#14 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh

I am not sure if it’s worth it, the hypervisor is much simpler, there is a single page table, it’s locked (also identity mapped), it’s updated on VM boot/teardown only, we don’t even use iotlb_gather at the moment, although possible but I wanted to keep this series as simple as I can then we can add more features later.
So this patch is the least intrusive change, as whatever the main SMMUv3 driver does, the range tlb invalidation logic is the same. But I am happy to experiment with that when posted.

Okay, then maybe just always push a full invalidation?

I didn't like seeing the range invalidation lifted out, especially how ugly that will turn into after my next series. So, don't use range or use the proper full gather flow seem like the right two options.

I'm not so interested in minimal change, but maximum forward maintainability. It is much easier to manage if you double compile more of the driver exactly the same, and call functions in a fairly normal way vs make special cases and special functions..

Jason

#24 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh
Quoted 26 lines · Mostafa Saleh
Quoted 17 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

Quoted 4 lines

This way was easier as each function only returns one thing, otherwise we have to pass stuff by address as we can’t pass the smmu struct as it is not shared between the drivers.
But no strong opinion, I can change that.

Could you share the struct? With multi-compilation you do get two structs with the same name and different layout, it is OK?

That would give a cleaner setup too because you can just shove all this into a function and have it setup the same struct which is more a copy and paste than break up into little functions?

Jason

#15 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh

I am not sure if it’s worth it, the hypervisor is much simpler, there is a single page table, it’s locked (also identity mapped), it’s updated on VM boot/teardown only, we don’t even use iotlb_gather at the moment, although possible but I wanted to keep this series as simple as I can then we can add more features later.
So this patch is the least intrusive change, as whatever the main SMMUv3 driver does, the range tlb invalidation logic is the same. But I am happy to experiment with that when posted.

Quoted 1 line

Okay, then maybe just always push a full invalidation?

Like full address space invalidation? that will not be optimal and will affect every device on the system, why would we do that if we know the address?

Quoted 9 lines

I didn't like seeing the range invalidation lifted out, especially how ugly that will turn into after my next series. So, don't use range or use the proper full gather flow seem like the right two options.

I'm not so interested in minimal change, but maximum forward maintainability. It is much easier to manage if you double compile more of the driver exactly the same, and call functions in a fairly normal way vs make special cases and special functions..

Let me try with your series, but the core range invalidation shouldn’t be changed often as it is tied to the hardware and wrapping it in a macro is reasonable (just like the CPU tlb invalidation)
The last time this code was changed was in 2023.
I am happy to use higher level functions to improve the driver maintainability, but I don’t see what is the problem at the moment.

Thanks,
Mostafa

Quoted 2 lines

Jason

#25 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Jason Gunthorpe
Quoted 32 lines · Jason Gunthorpe
Quoted 26 lines · Mostafa Saleh
Quoted 17 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

Quoted 4 lines

This way was easier as each function only returns one thing, otherwise we have to pass stuff by address as we can’t pass the smmu struct as it is not shared between the drivers.
But no strong opinion, I can change that.

Quoted 6 lines

Could you share the struct? With multi-compilation you do get two structs with the same name and different layout, it is OK?

That would give a cleaner setup too because you can just shove all this into a function and have it setup the same struct which is more a copy and paste than break up into little functions?

Yes, that’s possible, but I didn't want to contaminate the main struct, also we can’t compile the main one for KVM because of things such as “struct device, struct mutex...”
May we can do:
- struct arm_smmu_device_common: Basic stuff (ioaddress, features, oas...)
- arm_smmu_device: For the main driver which inherits arm_smmu_device_common
- arm_smmu_nested_device for KVM which also inherits arm_smmu_device_common

What do you think?

Thanks,
Mostafa

Quoted 2 lines

Jason

#16 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh
Quoted 12 lines · Mostafa Saleh
Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh

I am not sure if it’s worth it, the hypervisor is much simpler, there is a single page table, it’s locked (also identity mapped), it’s updated on VM boot/teardown only, we don’t even use iotlb_gather at the moment, although possible but I wanted to keep this series as simple as I can then we can add more features later.
So this patch is the least intrusive change, as whatever the main SMMUv3 driver does, the range tlb invalidation logic is the same. But I am happy to experiment with that when posted.

Quoted 5 lines
Quoted 1 line

Okay, then maybe just always push a full invalidation?

Like full address space invalidation? that will not be optimal and will affect every device on the system, why would we do that if we know the address?

Not every device, just wipe the VMID. If you say it is rare then keep it simple. If you need to be narrow then use the proper infastructure with a gather. No reason to make something boutique for pkvm here.

Jason

#26 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh
Quoted 40 lines · Mostafa Saleh
Quoted 32 lines · Jason Gunthorpe
Quoted 26 lines · Mostafa Saleh
Quoted 17 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

Quoted 4 lines

This way was easier as each function only returns one thing, otherwise we have to pass stuff by address as we can’t pass the smmu struct as it is not shared between the drivers.
But no strong opinion, I can change that.

Quoted 6 lines

Could you share the struct? With multi-compilation you do get two structs with the same name and different layout, it is OK?

That would give a cleaner setup too because you can just shove all this into a function and have it setup the same struct which is more a copy and paste than break up into little functions?

Quoted 8 lines

Yes, that’s possible, but I didn't want to contaminate the main struct, also we can’t compile the main one for KVM because of things such as “struct device, struct mutex...”
May we can do:
- struct arm_smmu_device_common: Basic stuff (ioaddress, features, oas...)
- arm_smmu_device: For the main driver which inherits arm_smmu_device_common
- arm_smmu_nested_device for KVM which also inherits arm_smmu_device_common

Less keen on this, it would need more usages to justify it

Quoted 1 line

What do you think?

I was thinking more like

#define arm_smmu arm_pkvm_smmu

Before the pkvm compile of the shared code.

How does pkvm compilation work anyhow? Is it all rolled into a unique link, or do you have to worry about symbol conflicts with the main kernel?

Jason

#17 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Jason Gunthorpe
Quoted 15 lines · Jason Gunthorpe
Quoted 14 lines · Mostafa Saleh
Quoted 12 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh

I am not sure if it’s worth it, the hypervisor is much simpler, there is a single page table, it’s locked (also identity mapped), it’s updated on VM boot/teardown only, we don’t even use iotlb_gather at the moment, although possible but I wanted to keep this series as simple as I can then we can add more features later.
So this patch is the least intrusive change, as whatever the main SMMUv3 driver does, the range tlb invalidation logic is the same. But I am happy to experiment with that when posted.

Okay, then maybe just always push a full invalidation?

Quoted 7 lines
Quoted 3 lines

Like full address space invalidation? that will not be optimal and will affect every device on the system, why would we do that if we know the address?

Not every device, just wipe the VMID. If you say it is rare then keep it simple. If you need to be narrow then use the proper infastructure with a gather. No reason to make something boutique for pkvm here.

But all devices share the same VMID, which impacts all devices on the system, and is not that rare to ignore.

I can add gather support, it’s not that complicated, it is currently supported in Android.
But that doesn’t solve the problem, which is: At some point, whether eagerly from the page table code, through gather sync or a fancy invalidation array, the driver will need to populate a range invalidation command (tg, ttl, scale...) and this logic is better shared with the main driver which is this patch does.

Thanks,
Mostafa

Quoted 2 lines

Jason

#27 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Jason Gunthorpe
Quoted 52 lines · Jason Gunthorpe
Quoted 40 lines · Mostafa Saleh
Quoted 32 lines · Jason Gunthorpe
Quoted 26 lines · Mostafa Saleh
Quoted 17 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent) · line 224

Quoted 13 lines · Mostafa Saleh

Move parsing of IDRs to functions so that it can be re-used

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +12 -0
+unsigned long smmu_idr5_to_pgsize(u32 reg)
+{
+	unsigned long pgsize_bitmap = 0;
+
+	if (reg & IDR5_GRAN64K)
+		pgsize_bitmap |= SZ_64K | SZ_512M;
+	if (reg & IDR5_GRAN16K)
+		pgsize_bitmap |= SZ_16K | SZ_32M;
+	if (reg & IDR5_GRAN4K)
+		pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
+	return pgsize_bitmap;
+}

I think this should include:

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4999

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
+	smmu->oas = smmu_idr5_to_oas(reg);
+	if (smmu->oas == 52)
 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
-		break;

ie it should return the supported page sizes by inspecting all the idrs and don't leave this tricky bit to be open coded..

Quoted 4 lines

This way was easier as each function only returns one thing, otherwise we have to pass stuff by address as we can’t pass the smmu struct as it is not shared between the drivers.
But no strong opinion, I can change that.

Quoted 6 lines

Could you share the struct? With multi-compilation you do get two structs with the same name and different layout, it is OK?

That would give a cleaner setup too because you can just shove all this into a function and have it setup the same struct which is more a copy and paste than break up into little functions?

Quoted 8 lines

Yes, that’s possible, but I didn't want to contaminate the main struct, also we can’t compile the main one for KVM because of things such as “struct device, struct mutex...”
May we can do:
- struct arm_smmu_device_common: Basic stuff (ioaddress, features, oas...)
- arm_smmu_device: For the main driver which inherits arm_smmu_device_common
- arm_smmu_nested_device for KVM which also inherits arm_smmu_device_common

Less keen on this, it would need more usages to justify it

Quoted 7 lines
Quoted 1 line

What do you think?

I was thinking more like

#define arm_smmu arm_pkvm_smmu

Before the pkvm compile of the shared code.

Oh, so we rely on arm_pkvm_smmu and arm_smmu having the same field names and types?

I don’t think that’s better for maintainability, I can look into it if Robin and Will are OK with that.

Quoted 4 lines

How does pkvm compilation work anyhow? Is it all rolled into a unique link, or do you have to worry about symbol conflicts with the main kernel?

All pkvm symbols are prefixed with __kvm_nvhe_ in the main kernel.

Thanks,
Mostafa

Quoted 2 lines

Jason

#34 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh
Quoted 4 lines

So.. I suggest you update it to use the iommu_pages API, #ifdef out the allocator so the pkvm pkvm doesn't need to deal with it. Then compile a special iommu-pages for the pkvm side presenting the same API.

I see, we still need to leave the DMA-API calls for the custom config, as I am not sure if it can use pages not backed by the vmemmap, I pushed that into a separate function so it’s easily compiled out.

Yeah..

Quoted 1 line

Without this patch, now it looks like:

Seems reasonable, and then i'd probably just put something like
#define dma_map(...)
#define dma_umap(...)

To effectively take it out of the pkvm build

Then have a pkvm compile of iommu-pages to provide the functions in a pkvm compatible way. I guess you can't actually fully do this, but can do enough to support this file at least.

Quoted 7 lines
Quoted 3 lines

You should have a pkvm shim header that provides kmalloc/kfree/virt_to_phys in the normal way and just #include that in io-pgtable when doing a pkvm build instead of hacking up all the code.

Ok, I can do that in another change, but I believe it's better to change the usage in this file to arm_lpae_*(virt_to_phys...) so it's clear which parts are intended for that.

IDK, why? virt_to_phys() is part of the iommu-pages API, I'd just leave it.. If you want to narrow it then #define it for pkvm when compiling this file..

Jason

#44 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 4 lines · Mostafa Saleh

So far this is the list of requirements/changes needed share the stage-2 page table (besides the obvious: same page table format, granularity, endianness...)

Quoted 8 lines

1) HW BBM is not supported in the hypervisor page table, that’s because it can generate TLB conflict aborts, which the hypervisor can not handle because of the limited syndrome information. We can rely on FEAT_BBML3 which was newly introduced to work around that, it’s quite niche and not supported in KVM yet or have an allow list similar to the kernel
(as in cpu_supports_bbml2_noabort()) which also limits the number of CPUs that can run this.

Do you think pkvm will need BBM? Hitless replace of a PTE is already a pretty advanced feature and the SMMU has its own support matrix there too. Is it for shared/private conversion?

Quoted 5 lines

2) Handling page faults, devices must be able to stall and let the hypervisor handle the page fault (which has to proxy through the kernel as the hypervisor doesn’t handle interrupts), this includes also IO page faults which are hard to get right from the HW which and may lead to system stability issues or lockups.

No.. once you turn on IO like this you don't have page faults anymore. Everything must be permantently mapped into the SMMU view, it can never be made non-present and you must run without page faults. That's what you have in the io-pgtable constructed table, right?

Quoted 4 lines

Alternatively, we can pin the stage-2 pages, that would require some hypercalls, hacks to the driver/IOMMU API and possibly new semantics in the DMA-API for IDENTITY devices as they will still need to pin the pages as they are actually in stage-2 translation and not bypass.

?? Then how does this series work?

Quoted 1 line

3) SMMUv3 must be coherent.

Yes for sure.

Quoted 2 lines

4) Support BTM/DVM for TLB invalidation, otherwise some hooks are still required (although not io-pgtable-arm)

SW needs to forward invalidations, BTM is rare..

Quoted 3 lines

IMO, 1, 2 are the most tricky parts. It's more work and runs on very limited systems, However, it can be implemented as an optimization) which is my plan.

I think unless you can do it without these HW features (excluding 3) don't bother.

Quoted 2 lines

I am not sure how CCA deals with that, I’d expect they have a lot of constraints on CPUs/SMMUs and DMA capable devices on those systems.

3 is not supported. The entire S2 is permanently mapped and doesn't really change alot at runtime. No page faults, not sure if the RMM private/shard conversion would require BMM..

Jason

#18 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh
Quoted 5 lines · Mostafa Saleh

But that doesn’t solve the problem, which is: At some point, whether eagerly from the page table code, through gather sync or a fancy invalidation array, the driver will need to populate a range invalidation command (tg, ttl, scale...) and this logic is better shared with the main driver which is this patch does.

My point is this patch doesn't share enough. If you do need to issue invalidations then share everything below the top level tlbi entry point and don't try to make a pkvm version of the entire logic just by ripping out the range logic.

There is no reason for pkvm to need a different algorithm here. Especially when you get to supporting ATS and multiple devices and smmus you may as well just use the whole thing.

Which is why I suggested to copy the entire call chain into a shared file

Jason

#28 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh
Quoted 5 lines

I was thinking more like

#define arm_smmu arm_pkvm_smmu

Before the pkvm compile of the shared code.

Oh, so we rely on arm_pkvm_smmu and arm_smmu having the same field names and types?

Just for the shared code, yes.

For instance this patch would require the features field be present in both, and then all you do is directly copy all the smmu->features logic into the shared file and that's it.

Quoted 2 lines

I don’t think that’s better for maintainability, I can look into it if Robin and Will are OK with that.

I think maintainability here means pkvm minimizes how much it changes the code of the main driver.

Copying a bunch of functions into a shared .c file exactly as it is, then compiling the shared file with some #ifdef'ery at is going to be long term better than trying to mangle the whole thing to avoid using any of the core types and not directly share the code, IMHO.

Like my tlbi point for instance, it is not a good idea to rip out a little bit of the invalidation logic then open code a pkvm version. Use the exact driver code. But to do this you need to pass an arm_smmu through to to the pkvm specific queue management functions.

Quoted 5 lines
Quoted 3 lines

How does pkvm compilation work anyhow? Is it all rolled into a unique link, or do you have to worry about symbol conflicts with the main kernel?

All pkvm symbols are prefixed with __kvm_nvhe_ in the main kernel.

So automatically with objcopy or something like that during the build?

Jason

#35 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Jason Gunthorpe
Quoted 28 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh
Quoted 4 lines

So.. I suggest you update it to use the iommu_pages API, #ifdef out the allocator so the pkvm pkvm doesn't need to deal with it. Then compile a special iommu-pages for the pkvm side presenting the same API.

I see, we still need to leave the DMA-API calls for the custom config, as I am not sure if it can use pages not backed by the vmemmap, I pushed that into a separate function so it’s easily compiled out.

Yeah..

Quoted 1 line

Without this patch, now it looks like:

Seems reasonable, and then i'd probably just put something like

   #define dma_map(...)
   #define dma_umap(...)

To effectively take it out of the pkvm build

Then have a pkvm compile of iommu-pages to provide the functions in a pkvm compatible way. I guess you can't actually fully do this, but can do enough to support this file at least.

Quoted 4 lines
Quoted 3 lines

You should have a pkvm shim header that provides kmalloc/kfree/virt_to_phys in the normal way and just #include that in io-pgtable when doing a pkvm build instead of hacking up all the code.

Quoted 7 lines
Quoted 3 lines

Ok, I can do that in another change, but I believe it's better to change the usage in this file to arm_lpae_*(virt_to_phys...) so it's clear which parts are intended for that.

IDK, why? virt_to_phys() is part of the iommu-pages API, I'd just leave it.. If you want to narrow it then #define it for pkvm when compiling this file..

It is not going to be part of the iommu-pages API, I meant in io-pgtable-arm, we will use something arm_lpae_virt_to_phys()... which is then implemented differently for pkvm.

Thanks,
Mostafa

Quoted 2 lines

Jason

#45 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Jason Gunthorpe
Quoted 14 lines · Jason Gunthorpe
Quoted 4 lines · Mostafa Saleh

So far this is the list of requirements/changes needed share the stage-2 page table (besides the obvious: same page table format, granularity, endianness...)

Quoted 8 lines

1) HW BBM is not supported in the hypervisor page table, that’s because it can generate TLB conflict aborts, which the hypervisor can not handle because of the limited syndrome information. We can rely on FEAT_BBML3 which was newly introduced to work around that, it’s quite niche and not supported in KVM yet or have an allow list similar to the kernel
(as in cpu_supports_bbml2_noabort()) which also limits the number of CPUs that can run this.

Quoted 3 lines

Do you think pkvm will need BBM? Hitless replace of a PTE is already a pretty advanced feature and the SMMU has its own support matrix there too. Is it for shared/private conversion?

Yes, we can break block on memory donation which is transfer of ownership to the hypervisor or a guest.

Quoted 7 lines
Quoted 6 lines

2) Handling page faults, devices must be able to stall and let the hypervisor handle the page fault (which has to proxy through the kernel as the hypervisor doesn’t handle interrupts), this includes also IO page faults which are hard to get right from the HW which and may lead to system stability issues or lockups.

Quoted 5 lines

No.. once you turn on IO like this you don't have page faults anymore. Everything must be permantently mapped into the SMMU view, it can never be made non-present and you must run without page faults. That's what you have in the io-pgtable constructed table, right?

Exactly, but the CPU page table doesn’t guarantee that, so we either have to handle page faults in the IOMMU, or completely change how KVM deals with stage-2 if we want to share the page table with the CPU.

Quoted 7 lines
Quoted 5 lines

Alternatively, we can pin the stage-2 pages, that would require some hypercalls, hacks to the driver/IOMMU API and possibly new semantics in the DMA-API for IDENTITY devices as they will still need to pin the pages as they are actually in stage-2 translation and not bypass.

?? Then how does this series work?

This series works fine as it shadows the page table and doesn't share it with the CPU, so it fully populates the address space.

Quoted 8 lines
Quoted 2 lines

3) SMMUv3 must be coherent.

Yes for sure.

Quoted 2 lines

4) Support BTM/DVM for TLB invalidation, otherwise some hooks are still required (although not io-pgtable-arm)

Quoted 8 lines

SW needs to forward invalidations, BTM is rare..

Quoted 3 lines

IMO, 1, 2 are the most tricky parts. It's more work and runs on very limited systems, However, it can be implemented as an optimization) which is my plan.

I think unless you can do it without these HW features (excluding 3) don't bother.

I am looking into this now, but as I mentioned that will be a separate RFC following this one as an optimization for advanced HW.

Thanks,
Mostafa

Quoted 9 lines
Quoted 3 lines

I am not sure how CCA deals with that, I’d expect they have a lot of constraints on CPUs/SMMUs and DMA capable devices on those systems.

3 is not supported. The entire S2 is permanently mapped and doesn't really change alot at runtime. No page faults, not sure if the RMM private/shard conversion would require BMM..

Jason

#19 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 5 lines · Mostafa Saleh

But that doesn’t solve the problem, which is: At some point, whether eagerly from the page table code, through gather sync or a fancy invalidation array, the driver will need to populate a range invalidation command (tg, ttl, scale...) and this logic is better shared with the main driver which is this patch does.

My point is this patch doesn't share enough. If you do need to issue invalidations then share everything below the top level tlbi entry point and don't try to make a pkvm version of the entire logic just by ripping out the range logic.

Quoted 6 lines

There is no reason for pkvm to need a different algorithm here. Especially when you get to supporting ATS and multiple devices and smmus you may as well just use the whole thing.

Which is why I suggested to copy the entire call chain into a shared file

Agh, actually this seires doesn't deal with ATS, which I think is wrong, propably we have to issue CMDQ_OP_ATC_INV for the whole space on every S2 invalidation which has to be done per-SID and as it can't be done by VMID :/ or just hide ATS support from host for now, I will look into more for v7.

But anyway, we don’t have to share any logic, the kernel driver is quite complicated as it is designed for a different use-case. Doing that makes the hypervisor unnecessary complicated and oversharing this logic makes the kernel driver less maintainable IMO.

The hypervisor only needs to share the architecture spec of populating commands.

If that’s a problem. I am happy to re-write the RIL part in the hypervisor. It's not that complicated, but I thought it makes sense to share as it is a HW contract.
Otherwise, we can break this part into some macros that are called by this function and the hypervisor but that's maybe over-engineered.

Thanks,
Mostafa

Quoted 2 lines

Jason

#29 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Jason Gunthorpe
Quoted 20 lines · Jason Gunthorpe
Quoted 8 lines · Mostafa Saleh
Quoted 5 lines

I was thinking more like

#define arm_smmu arm_pkvm_smmu

Before the pkvm compile of the shared code.

Oh, so we rely on arm_pkvm_smmu and arm_smmu having the same field names and types?

Just for the shared code, yes.

For instance this patch would require the features field be present in both, and then all you do is directly copy all the smmu->features logic into the shared file and that's it.

Quoted 2 lines

I don’t think that’s better for maintainability, I can look into it if Robin and Will are OK with that.

Quoted 8 lines

I think maintainability here means pkvm minimizes how much it changes the code of the main driver.

Copying a bunch of functions into a shared .c file exactly as it is, then compiling the shared file with some #ifdef'ery at is going to be long term better than trying to mangle the whole thing to avoid using any of the core types and not directly share the code, IMHO.

There isn't #ifdef'ery at the moment, that's what I was trying to avoid by introducing shared code.
I believe that can be treated as a library as most of the shared code is about HW description (commands, invalidation, IDR probing...)

Quoted 5 lines

Like my tlbi point for instance, it is not a good idea to rip out a little bit of the invalidation logic then open code a pkvm version. Use the exact driver code. But to do this you need to pass an arm_smmu through to to the pkvm specific queue management functions.

What concerns me is how fragile that is, any change in the main struct can easily break the hypervisor, unlike if we have a clear shared code and defined API that is used by 2 entities.
I will think more about this before v7 and see how intrusive it is.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

How does pkvm compilation work anyhow? Is it all rolled into a unique link, or do you have to worry about symbol conflicts with the main kernel?

All pkvm symbols are prefixed with __kvm_nvhe_ in the main kernel.

So automatically with objcopy or something like that during the build?

Exactly: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/kvm/hyp/nvhe/Makefile#n96

Thanks,
Mostafa

Quoted 1 line

Jason

#36 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Mostafa Saleh
Quoted 7 lines · Mostafa Saleh
Quoted 3 lines

IDK, why? virt_to_phys() is part of the iommu-pages API, I'd just leave it.. If you want to narrow it then #define it for pkvm when compiling this file..

It is not going to be part of the iommu-pages API, I meant in io-pgtable-arm, we will use something arm_lpae_virt_to_phys()... which is then implemented differently for pkvm.

Again why? I think the main goal should be to not mess up the normal code.

  #define virt_to_phys pkvm_virt_to_phys

Does that, we should be leaning into this pattern I think, not adding unnecessary churn...

If anything is needed then it should be an iommu_pages function to wrapper virt_to_phys() for use by iommu_pages uses but I'd rather not..

Jason

#46 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 15 lines · Mostafa Saleh
Quoted 14 lines · Jason Gunthorpe
Quoted 12 lines · Mostafa Saleh

So far this is the list of requirements/changes needed share the stage-2 page table (besides the obvious: same page table format, granularity, endianness...)

1) HW BBM is not supported in the hypervisor page table, that’s because it can generate TLB conflict aborts, which the hypervisor can not handle because of the limited syndrome information. We can rely on FEAT_BBML3 which was newly introduced to work around that, it’s quite niche and not supported in KVM yet or have an allow list similar to the kernel
(as in cpu_supports_bbml2_noabort()) which also limits the number of CPUs that can run this.

Quoted 6 lines
Quoted 3 lines

Do you think pkvm will need BBM? Hitless replace of a PTE is already a pretty advanced feature and the SMMU has its own support matrix there too. Is it for shared/private conversion?

Yes, we can break block on memory donation which is transfer of ownership to the hypervisor or a guest.

So you need BBM support on the SMMU too? That is probably a big problem because the SMMU is often mismatched to the CPU :\

Also io-pgtable arm cannot trigger BBM behaviors, so how do you implement it?

Quoted 9 lines
Quoted 5 lines

No.. once you turn on IO like this you don't have page faults anymore. Everything must be permantently mapped into the SMMU view, it can never be made non-present and you must run without page faults. That's what you have in the io-pgtable constructed table, right?

Exactly, but the CPU page table doesn’t guarantee that, so we either have to handle page faults in the IOMMU, or completely change how KVM deals with stage-2 if we want to share the page table with the CPU.

So that's the real explanation, KVM cannot manage the S2 in the right way so you can't share it. RMM/etc are managing the S2 without pointless page faults so they can share it.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Alternatively, we can pin the stage-2 pages, that would require some hypercalls, hacks to the driver/IOMMU API and possibly new semantics in the DMA-API for IDENTITY devices as they will still need to pin the pages as they are actually in stage-2 translation and not bypass.

?? Then how does this series work?

This series works fine as it shadows the page table and doesn't share it with the CPU, so it fully populates the address space.

Which is why it is so weird that KVM is using a partially populated S2 when there is, and must, be a fully populated one for the SMMU. But I understand there are reasons fo rthis.

Jason

#20 lore raw

Re: [PATCH v6 04/25] iommu/arm-smmu-v3: Move TLB range invalidation into common code

In reply to Mostafa Saleh
Quoted 17 lines · Mostafa Saleh
Quoted 16 lines · Jason Gunthorpe
Quoted 5 lines · Mostafa Saleh

But that doesn’t solve the problem, which is: At some point, whether eagerly from the page table code, through gather sync or a fancy invalidation array, the driver will need to populate a range invalidation command (tg, ttl, scale...) and this logic is better shared with the main driver which is this patch does.

My point is this patch doesn't share enough. If you do need to issue invalidations then share everything below the top level tlbi entry point and don't try to make a pkvm version of the entire logic just by ripping out the range logic.

There is no reason for pkvm to need a different algorithm here. Especially when you get to supporting ATS and multiple devices and smmus you may as well just use the whole thing.

Quoted 8 lines
Quoted 2 lines

Which is why I suggested to copy the entire call chain into a shared file

Agh, actually this seires doesn't deal with ATS, which I think is wrong, propably we have to issue CMDQ_OP_ATC_INV for the whole space on every S2 invalidation which has to be done per-SID and as it can't be done by VMID :/ or just hide ATS support from host for now, I will look into more for v7.

Hiding from the host is a fine solution to start with.

Quoted 4 lines

But anyway, we don’t have to share any logic, the kernel driver is quite complicated as it is designed for a different use-case. Doing that makes the hypervisor unnecessary complicated and oversharing this logic makes the kernel driver less maintainable IMO.

invalidation is complicated, you should not try to open code your own version. You really cannot make it any simpler than what is in the driver, just use the code it is already decently modular.

Jason

#30 lore raw

Re: [PATCH v6 05/25] iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh
Quoted 8 lines
Quoted 5 lines

Copying a bunch of functions into a shared .c file exactly as it is, then compiling the shared file with some #ifdef'ery at is going to be long term better than trying to mangle the whole thing to avoid using any of the core types and not directly share the code, IMHO.

There isn't #ifdef'ery at the moment, that's what I was trying to avoid by introducing shared code.

Yeah, I think that may be a bad direction. Ultimately it feels like it will be more burden to maintain this careful split, while some small list of carefully selected #defines will let you reuse alot more with no code changes and I think that is ultimately going to be better.

Quoted 4 lines

What concerns me is how fragile that is, any change in the main struct can easily break the hypervisor, unlike if we have a clear shared code and defined API that is used by 2 entities.
I will think more about this before v7 and see how intrusive it is.

IMHO so long as it is easy to include pkvm in the compilation I see no issue with build testing the pkvm driver when working on smmuv3 driver. So I'm not worried about this, any breaks will be compile breaks and can be delt with.

What I'd like is to minimize logic changes and maximimize re-use so you don't have to make bad re-implementations. Like pkvm shouldn't be building a weaker tlbi, it shouldn't have different logic for errata and FEAT, it shouldn't be doing STE changes without the hitless logic, etc, etc. All these things are easier to solve with greater direct code re-use..

Thus I feel the trade of off 'use the code with no changed via #define' is better than 'try to carefully cut away and avoid #define'

Jason

#47 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Jason Gunthorpe
Quoted 20 lines · Jason Gunthorpe
Quoted 19 lines · Mostafa Saleh
Quoted 17 lines · Jason Gunthorpe
Quoted 4 lines · Mostafa Saleh

So far this is the list of requirements/changes needed share the stage-2 page table (besides the obvious: same page table format, granularity, endianness...)

Quoted 8 lines

1) HW BBM is not supported in the hypervisor page table, that’s because it can generate TLB conflict aborts, which the hypervisor can not handle because of the limited syndrome information. We can rely on FEAT_BBML3 which was newly introduced to work around that, it’s quite niche and not supported in KVM yet or have an allow list similar to the kernel
(as in cpu_supports_bbml2_noabort()) which also limits the number of CPUs that can run this.

Do you think pkvm will need BBM? Hitless replace of a PTE is already a pretty advanced feature and the SMMU has its own support matrix there too. Is it for shared/private conversion?

Quoted 6 lines
Quoted 2 lines

Yes, we can break block on memory donation which is transfer of ownership to the hypervisor or a guest.

So you need BBM support on the SMMU too? That is probably a big problem because the SMMU is often mismatched to the CPU :\

Yes, that's why it's hard to find systems that can easily share the CPU page table with the SMMU (some might even have mis-match in OAS/PS)

Quoted 2 lines

Also io-pgtable arm cannot trigger BBM behaviors, so how do you implement it?

At the moment, we workaround this by mapping all the memory with PTE level, while MMIO remains at block level as they never change ownership at the moment.

This is one of the missing features I plan to add after this series, if you look in the cover letter, these are listed under “Future work”

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

No.. once you turn on IO like this you don't have page faults anymore. Everything must be permantently mapped into the SMMU view, it can never be made non-present and you must run without page faults. That's what you have in the io-pgtable constructed table, right?

Quoted 7 lines
Quoted 3 lines

Exactly, but the CPU page table doesn’t guarantee that, so we either have to handle page faults in the IOMMU, or completely change how KVM deals with stage-2 if we want to share the page table with the CPU.

So that's the real explanation, KVM cannot manage the S2 in the right way so you can't share it. RMM/etc are managing the S2 without pointless page faults so they can share it.

Well, there is not really a right way, even with a fully populated stage-2 page table, you can’t guarantee not getting TLB conflict aborts without FEAT_BBML3 (which is quite recent), unless you map everything with a leaf level, which then impacts performance.

Thanks,
Mostafa

Quoted 16 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

Alternatively, we can pin the stage-2 pages, that would require some hypercalls, hacks to the driver/IOMMU API and possibly new semantics in the DMA-API for IDENTITY devices as they will still need to pin the pages as they are actually in stage-2 translation and not bypass.

?? Then how does this series work?

This series works fine as it shadows the page table and doesn't share it with the CPU, so it fully populates the address space.

Which is why it is so weird that KVM is using a partially populated S2 when there is, and must, be a fully populated one for the SMMU. But I understand there are reasons fo rthis.

Jason

#48 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 9 lines · Mostafa Saleh
Quoted 2 lines

Also io-pgtable arm cannot trigger BBM behaviors, so how do you implement it?

At the moment, we workaround this by mapping all the memory with PTE level, while MMIO remains at block level as they never change ownership at the moment.

This is one of the missing features I plan to add after this series, if you look in the cover letter, these are listed under “Future work”

Hrm, I would not be happy to see io-pgtable modified to have bbm features :\ They are very complex this is what iommupt is for..

Quoted 8 lines
Quoted 3 lines

So that's the real explanation, KVM cannot manage the S2 in the right way so you can't share it. RMM/etc are managing the S2 without pointless page faults so they can share it.

Well, there is not really a right way, even with a fully populated stage-2 page table, you can’t guarantee not getting TLB conflict aborts without FEAT_BBML3 (which is quite recent), unless you map everything with a leaf level, which then impacts performance.

I mean, there is a right way - you need to rely on the BBM features in all HW and fully populate the S2 for no faults. So if that isn't present then you are doing things that will harm performance to work around the issue. It's fine, but just explain in the commit message.

Jason

#37 lore raw

Re: [PATCH v6 06/25] iommu/io-pgtable-arm: Rework to use the iommu-pages API

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Mostafa Saleh
Quoted 3 lines

IDK, why? virt_to_phys() is part of the iommu-pages API, I'd just leave it.. If you want to narrow it then #define it for pkvm when compiling this file..

It is not going to be part of the iommu-pages API, I meant in io-pgtable-arm, we will use something arm_lpae_virt_to_phys()... which is then implemented differently for pkvm.

Again why? I think the main goal should be to not mess up the normal code.

Quoted 9 lines
  #define virt_to_phys pkvm_virt_to_phys

Does that, we should be leaning into this pattern I think, not adding unnecessary churn...

If anything is needed then it should be an iommu_pages function to wrapper virt_to_phys() for use by iommu_pages uses but I'd rather not..

Sure, no strong opinion, I did post this patch along with cleanups separately in:
https://lore.kernel.org/linux-iommu/[email protected]/

Thanks,
Mostafa

Quoted 1 line

Jason

#2 lore raw

Re: [PATCH v6 01/25] KVM: arm64: Generalize trace clock

In reply to Mostafa Saleh

Hi Mostafa,

Sorry I should have looked at this a while ago!

Quoted 11 lines · Mostafa Saleh

IOMMU drivers need to track time, mainly for timeouts. Generalize the tracing clock functions in the hypervsior, so they can be used from IOMMU drivers.

1) Make the compilation independent from tracing.

2) As drivers might need to use that quite early, provide default values for the clock data based on cntfrq_el0, the driver can keep using these values without calling hyp_clock_update() as they don't need to sync with the host timers.

Quoted 4 lines

This is mainly used for timeouts, so a malicious host can DoS the system or cause premature timeouts which likely end up in hyp panic, that should be acceptable as neither of those would undermine the security guarantees.

This still sounds very risky if the usage shift, and as I explain later I don't believe this is completely OK as regard to mult/shift updates.

Quoted 52 lines
---
 arch/arm64/kvm/hyp/include/nvhe/clock.h | 11 ++-----
 arch/arm64/kvm/hyp/nvhe/Makefile        |  4 +--
 arch/arm64/kvm/hyp/nvhe/clock.c         | 44 ++++++++++++++++++++++---
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++
 arch/arm64/kvm/hyp/nvhe/trace.c         |  4 +--
 5 files changed, 51 insertions(+), 17 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/clock.h +3 -8
index 9f429f5c0664..e6a0e43af88d 100644
@@ -5,12 +5,7 @@
 
 #include <asm/kvm_hyp.h>
 
-#ifdef CONFIG_NVHE_EL2_TRACING
-void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
-u64 trace_clock(void);
-#else
-static inline void
-trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) { }
-static inline u64 trace_clock(void) { return 0; }
-#endif
+void hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
+u64 hyp_clock_ns(void);
+int hyp_clock_init(void);
 #endif
arch/arm64/kvm/hyp/nvhe/Makefile +2 -2
index 62cdfbff7562..89d0533921f9 100644
@@ -26,10 +26,10 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
-	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
+	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
-hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o trace.o events.o
+hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += trace.o events.o
 hyp-obj-y += $(lib-objs)
 
 # Path to simple_ring_buffer.c
arch/arm64/kvm/hyp/nvhe/clock.c +0 -0
index 32fc4313fe43..53d0bd55e866 100644

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 21

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/clock.c +1 -1
@@ -18,7 +18,41 @@ static struct clock_data {
 		u64 cyc_overflow64;
 	} data[2];
 	u64 cur;
-} trace_clock_data;
+} clock_data;

I believe this should be separated. the trace_clock is "unsafe" but synchronised with the host. the clock you need must be "safe" but we don't really need to be synchronised.

So overall, I would keep all the trace_clock_* interface unchanged and introduce in the same file a hyp_clock, with its own hyp_clock_data.

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 23

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -0
+
+#define HYP_CLK_SEC_TO_NS 1000000000UL

Not possible to use NSEC_PER_SEC?

Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/clock.c +20 -0
+
+int hyp_clock_init(void)
+{
+	u32 timer_freq = read_sysreg(cntfrq_el0);
+	u32 shift = 32;
+	u64 mult;
+
+	/*
+	 * KVM will not initialize if FW didn't set cntfrq_el0, that is already
+	 * part of the boot protocol.
+	 */
+	if (!timer_freq)
+		return -ENODEV;
+
+	/* Timer freq can't be larger than 1Ghz by spec. */
+	if (timer_freq > HYP_CLK_SEC_TO_NS)
+		return -EINVAL;
+
+	/* Simplified logic from clocks_calc_mult_shift() */
+	do {

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 53

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/clock.c +10 -0
+		mult = (HYP_CLK_SEC_TO_NS << shift);
+		mult = div_u64(mult, timer_freq);
+		if (mult <= (~0U))
+			break;
+		shift--;
+	} while (shift > 0);
+
+	clock_data.data[0].shift = shift;
+	clock_data.data[0].mult = mult;
+	clock_data.data[0].cyc_overflow64 = ULONG_MAX / mult;

I don't know if we want to update this from time to time, but we can't rely on host provided data. Perhaps the call to that clock could self-update?

Worst case we fallback on 128-bits mult. but perhaps this isn't a problem for a wait loop()?

And as this is wait loop, perhaps we could reduce the resolution quite heavily, so we reduce the risk of overflow?

arch/arm64/kvm/hyp/nvhe/clock.c static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift) · line 66

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -0
+	return 0;
+}
 
 static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift)
 {
@@ -30,9 +64,9 @@ static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift)
 }
 
 /* Does not guarantee no reader on the modified bank. */

^
And here's another reason why you should have another clock. It works because of tricks in the tracing interface.

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -2
-void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
+void hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 {
-	struct clock_data *clock = &trace_clock_data;
+	struct clock_data *clock = &clock_data;
 	u64 bank = clock->cur ^ 1;
 
 	clock->data[bank].mult			= mult;

arch/arm64/kvm/hyp/nvhe/clock.c void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) · line 85

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -2
@@ -45,9 +79,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 }
 
 /* Use untrusted host data */
-u64 trace_clock(void)
+u64 hyp_clock_ns(void)
 {
-	struct clock_data *clock = &trace_clock_data;
+	struct clock_data *clock = &clock_data;
 	u64 bank = smp_load_acquire(&clock->cur);

The whole bank system is not necessary if not updated by the host.

Quoted 50 lines
arch/arm64/kvm/hyp/nvhe/clock.c +0 -0
 	u64 cyc, ns;
 
arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index d8e5b563fd3d..8041f6e80cd1 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/clock.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
@@ -312,6 +313,10 @@ void __noreturn __pkvm_init_finalise(void)
 	};
 	pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
 
+	ret = hyp_clock_init();
+	if (ret)
+		goto out;
+
 	ret = fix_host_ownership();
 	if (ret)
 		goto out;
arch/arm64/kvm/hyp/nvhe/trace.c +2 -2
index a6ca27b18e15..e30de840e6c2 100644
@@ -35,7 +35,7 @@ static bool hyp_trace_buffer_loaded(struct hyp_trace_buffer *trace_buffer)
 void *tracing_reserve_entry(unsigned long length)
 {
 	return simple_ring_buffer_reserve(this_cpu_ptr(trace_buffer.simple_rbs), length,
-					  trace_clock());
+					  hyp_clock_ns());
 }
 
 void tracing_commit_entry(void)
@@ -285,7 +285,7 @@ void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 	}
 
 	/* ...we can now override the old one and swap. */
-	trace_clock_update(mult, shift, epoch_ns, epoch_cyc);
+	hyp_clock_update(mult, shift, epoch_ns, epoch_cyc);
 }
 
 int __tracing_reset(unsigned int cpu)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#3 lore raw

Re: [PATCH v6 01/25] KVM: arm64: Generalize trace clock

[...]

Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/trace.c +1 -1
index a6ca27b18e15..e30de840e6c2 100644
@@ -35,7 +35,7 @@ static bool hyp_trace_buffer_loaded(struct hyp_trace_buffer *trace_buffer)
 void *tracing_reserve_entry(unsigned long length)
 {
 	return simple_ring_buffer_reserve(this_cpu_ptr(trace_buffer.simple_rbs), length,
-					  trace_clock());
+					  hyp_clock_ns());
 }
 
Quoted 7 lines

arch/arm64/kvm/hyp/nvhe/trace.c void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) · line 288

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/trace.c +1 -1
 void tracing_commit_entry(void)
@@ -285,7 +285,7 @@ void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 	}
 
 	/* ...we can now override the old one and swap. */
-	trace_clock_update(mult, shift, epoch_ns, epoch_cyc);
+	hyp_clock_update(mult, shift, epoch_ns, epoch_cyc);

And also in any case we have two behaviours: one when tracing is running (sync'ed hyp_clock) and one when it is not (floating hyp_clock)

So we might well do floating all the time.

Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -0
 }
 
 int __tracing_reset(unsigned int cpu)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#75 lore raw

Re: [PATCH v6 16/25] iommu/arm-smmu-v3-kvm: Add CMDQ functions

In reply to Mostafa Saleh
Quoted 57 lines · Mostafa Saleh

Add functions to access the command queue, there are 2 main usage:
- Hypervisor's own commands, as TLB invalidation, would use functions as smmu_send_cmd(), which creates and sends a command.
- Add host commands to the shadow command queue, after being filtered, these will be added with smmu_add_cmd_raw.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  14 ++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 107 ++++++++++++++++++
 2 files changed, 115 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -6
index f904f4d19609..3fc499608d76 100644
@@ -1156,19 +1156,21 @@ u32 smmu_idr5_to_oas(u32 reg);
 unsigned long smmu_idr5_to_pgsize(u32 reg);
 
 /* Queue functions shared between kernel and hyp. */
-static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
+static inline u32 queue_space(struct arm_smmu_ll_queue *q)
 {
-	u32 space, prod, cons;
+	u32 prod, cons;
 
 	prod = Q_IDX(q, q->prod);
 	cons = Q_IDX(q, q->cons);
 
 	if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons))
-		space = (1 << q->max_n_shift) - (prod - cons);
-	else
-		space = cons - prod;
+		return (1 << q->max_n_shift) - (prod - cons);
+	return cons - prod;
+}
 
-	return space >= n;
+static inline bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
+{
+	return queue_space(q) >= n;
 }
 
 static inline bool queue_full(struct arm_smmu_ll_queue *q)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
index 3b77796dafc7..aac455599728 100644
@@ -6,6 +6,7 @@
  */
 #include <asm/kvm_hyp.h>
 
+#include <nvhe/clock.h>
 #include <nvhe/iommu.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/trap_handler.h>

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 24

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
@@ -22,6 +23,31 @@ struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;
 
 #define cmdq_size(cmdq)	((1 << ((cmdq)->llq.max_n_shift)) * CMDQ_ENT_DWORDS * 8)

I only looked at that smmu_wait() as this is the hyp_clock_ns() caller.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 28

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+/*
+ * Wait until @cond is true.
+ * Return 0 on success, or -ETIMEDOUT

nit: Would be nice to explain use_wfe.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 30

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
+ */
+#define smmu_wait(use_wfe, _cond)					\

nit:, (__use_wfe, __cond), or (use_fw, cond)

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 33

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+({									\
+	int __ret = 0;							\
+	u64 delay = hyp_clock_ns() + ARM_SMMU_POLL_TIMEOUT_US * 1000;	\

Does it help to lower the risk for the 128-bits fallback if we keep the resolution to _us?

Also, ARM_SMMU_POLL_TIMEOUT_US seems to be 1s. Blocking that long at EL2 is really terrible. Any chance to lower that?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 38

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
+									\
+	while (!(_cond)) {						\
+		if (use_wfe) {						\
+			wfe();						\
+			if ((_cond))					\

nit: I believe there's no need for the double () here is there?

Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +12 -0
+				break;					\
+		} else {						\
+			cpu_relax();					\
+		}							\
+		if (hyp_clock_ns() >= delay) {				\
+			__ret = -ETIMEDOUT;				\
+			break;						\
+		}							\
+	}								\
+	__ret;								\
+})
+

[...]

#39 lore raw

Re: [PATCH v6 07/25] KVM: arm64: iommu: Introduce IOMMU driver infrastructure

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh

To establish DMA isolation, KVM needs an IOMMU driver which provides ops implemented at EL2.

Only one driver can be used and is registered with kvm_iommu_register_driver() by passing pointer to the ops.

This must be called before module_init() which is the point KVM initializes.

Everywhere s/KVM/pKVM/ as this can be registered only if pKVM is enabled.

Quoted 21 lines
---
 arch/arm64/include/asm/kvm_host.h       |  5 +++++
 arch/arm64/kvm/Makefile                 |  2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h | 13 +++++++++++++
 arch/arm64/kvm/hyp/nvhe/Makefile        |  3 ++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c   | 20 +++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++++
 arch/arm64/kvm/iommu.c                  | 26 +++++++++++++++++++++++++
 7 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c
 create mode 100644 arch/arm64/kvm/iommu.c
arch/arm64/include/asm/kvm_host.h +0 -0
index 851f6171751c..52898d2a3ec6 100644

arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1736

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1733,4 +1733,9 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg
 
 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
+#ifndef __KVM_NVHE_HYPERVISOR__

I think it's not necessary, a lot of things aren't guarded for __KVM_NVHE_HYPERVISOR__.

arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1737

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1 -0
+struct kvm_iommu_ops;

Perhaps we can keep this private to hyp code by just using (void *) here?

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +3 -0
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops);
+#endif
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Makefile +0 -0
index 59612d2f277c..0ddef54f7434 100644

arch/arm64/kvm/Makefile kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ · line 27

Quoted 5 lines
arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 vgic/vgic-v5.o
+	 vgic/vgic-v5.o iommu.o

alphabetical order.

Quoted 44 lines
arch/arm64/kvm/Makefile +0 -0
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/hyp/include/nvhe/iommu.h new file +13 -0
new file mode 100644
index 000000000000..1ac70cc28a9e
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ARM64_KVM_NVHE_IOMMU_H__
+#define __ARM64_KVM_NVHE_IOMMU_H__
+
+#include <asm/kvm_host.h>
+
+struct kvm_iommu_ops {
+	int (*init)(void);
+};
+
+int kvm_iommu_init(void);
+
+#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index 89d0533921f9..606c0e1b7bd0 100644
@@ -24,7 +24,8 @@ CFLAGS_switch.nvhe.o += -Wno-override-init
 
 hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
-	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
+	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o \
+	 iommu/iommu.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +2 -0
new file mode 100644
index 000000000000..406c8fb9b3b9
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 12

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +10 -0
+ * IOMMU operations for pKVM
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ */
+#include <nvhe/iommu.h>
+
+/* Only one set of ops supported */
+struct kvm_iommu_ops *kvm_iommu_ops;
+
+

Empty line.

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 17

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +5 -0
+int kvm_iommu_init(void)
+{
+	/* Keep DMA isolation optional. */
+	if (!kvm_iommu_ops || !kvm_iommu_ops->init)
+		return 0;

The absence of the SME workaround on affected systems breaks pKVM init. So perhaps the IOMMU should do the same? (see pkvm_check_sme_dvmsync_fw_call())

Quoted 33 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +3 -0
+
+	return kvm_iommu_ops->init();
+}
arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index 8041f6e80cd1..1f6b221db9a0 100644
@@ -14,6 +14,7 @@
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -329,6 +330,10 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
+	ret = kvm_iommu_init();
+	if (ret)
+		goto out;
+
 	ret = hyp_ffa_init(ffa_proxy_pages);
 	if (ret)
 		goto out;
arch/arm64/kvm/iommu.c new file +1 -0
new file mode 100644
index 000000000000..f247384fa193
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-only

arch/arm64/kvm/iommu.c line 11

Quoted 10 lines
arch/arm64/kvm/iommu.c new file +10 -0
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Mostafa Saleh <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+
+extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+
+static DEFINE_MUTEX(kvm_iommu_reg_lock);

could be moved directly into the only user: kvm_iommu_register_driver

arch/arm64/kvm/iommu.c line 13

Quoted 2 lines
arch/arm64/kvm/iommu.c new file +2 -0
+
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)

__init ?

arch/arm64/kvm/iommu.c line 17

Quoted 4 lines
arch/arm64/kvm/iommu.c new file +4 -0
+{
+	guard(mutex)(&kvm_iommu_reg_lock);
+
+	/* Only protected KVM before de-privilege. */

Don't think that comment brings much information, looking at the line below.

arch/arm64/kvm/iommu.c line 19

Quoted 2 lines
arch/arm64/kvm/iommu.c new file +2 -0
+	if (!is_protected_kvm_enabled() || is_kvm_arm_initialised())
+		return -EINVAL;

-EPERM?

Quoted 10 lines
arch/arm64/kvm/iommu.c new file +7 -0
+
+	if (kvm_nvhe_sym(kvm_iommu_ops))
+		return -EBUSY;
+
+	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
+	return 0;
+}
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#62 lore raw

Re: [PATCH v6 09/25] KVM: arm64: iommu: Add memory pool

In reply to Mostafa Saleh
Quoted 58 lines · Mostafa Saleh

IOMMU drivers would require to allocate memory for the shadow page table. Similar to the host stage-2 CPU page table, the IOMMU pool is allocated early from the carveout and it's memory is added in a pool which the IOMMU driver can allocate from and reclaim at run time.

As this is too early for drivers to use init calls, set the number of page allocated from the kernel command line "kvm-arm.hyp_iommu_pages".

Later when the driver registers, it will pass how many pages it needs, and if it was less than what was allocated, it will fail to register.

---
 .../admin-guide/kernel-parameters.txt         |  4 +++
 arch/arm64/include/asm/kvm_host.h             |  3 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |  7 +++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         | 21 +++++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               | 12 ++++++-
 arch/arm64/kvm/iommu.c                        | 33 ++++++++++++++++++-
 arch/arm64/kvm/pkvm.c                         |  1 +
 7 files changed, 76 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -0
index cf3807641d89..5e49946ff7ed 100644
@@ -3283,6 +3283,10 @@ Kernel parameters
 			trap: set WFI instruction trap
 
 			notrap: clear WFI instruction trap
+	kvm-arm.hyp_iommu_pages=
+			[KVM, ARM, EARLY]
+			Number of pages allocated for the IOMMU pool from the
+			KVM carveout when running in protected mode.
 
 	kvm_cma_resv_ratio=n [PPC,EARLY]
 			Reserves given percentage from system memory area for
arch/arm64/include/asm/kvm_host.h +2 -1
index 52898d2a3ec6..17f4cce86ec3 100644
@@ -1735,7 +1735,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
 struct kvm_iommu_ops;
-int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops);
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops, unsigned int pool_pages);
+unsigned int kvm_iommu_pages(void);
 #endif
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/hyp/include/nvhe/iommu.h +0 -0
index 6277d845cdcf..eba94b4f6050 100644

arch/arm64/kvm/hyp/include/nvhe/iommu.h struct kvm_iommu_ops { · line 18

Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/iommu.h +3 -1
@@ -10,8 +10,13 @@ struct kvm_iommu_ops {
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
-int kvm_iommu_init(void);
+int kvm_iommu_init(void *pool_base, unsigned int nr_pages);
 
 int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 				enum kvm_pgtable_prot prot);
+
+/* Returns zeroed memory. */

If we expect drivers to use that perhaps we should document a bit better (same for kvm_iommu_reclaim_pages()

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c

Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/iommu.h +3 -0
+void *kvm_iommu_donate_pages(u8 order);
+void kvm_iommu_reclaim_pages(void *ptr);
+
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +0 -0
index 1db52bd87c38..53cb5e4b0aac 100644

Do we really need that subfolder iommu/ ?

Quoted 51 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +20 -1
@@ -15,6 +15,7 @@ struct kvm_iommu_ops *kvm_iommu_ops;
 
 /* Protected by host_mmu.lock */
 static bool kvm_idmap_initialized;
+static struct hyp_pool iommu_pages_pool;
 
 static inline int pkvm_to_iommu_prot(enum kvm_pgtable_prot prot)
 {
@@ -95,7 +96,7 @@ static int kvm_iommu_snapshot_host_stage2(void)
 	return ret;
 }
 
-int kvm_iommu_init(void)
+int kvm_iommu_init(void *pool_base, unsigned int nr_pages)
 {
 	int ret;
 
@@ -103,6 +104,14 @@ int kvm_iommu_init(void)
 	    !kvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)
+		return -ENOMEM;
+
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = kvm_iommu_ops->init();
 	if (ret)
 		return ret;
@@ -120,3 +129,13 @@ int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 
 	return kvm_iommu_ops->host_stage2_idmap(start, end, pkvm_to_iommu_prot(prot));
 }
+
+void *kvm_iommu_donate_pages(u8 order)
+{
+	return hyp_alloc_pages(&iommu_pages_pool, order);
+}
+
+void kvm_iommu_reclaim_pages(void *ptr)
+{
+	hyp_put_page(&iommu_pages_pool, ptr);
+}
arch/arm64/kvm/hyp/nvhe/setup.c +0 -0
index 1f6b221db9a0..215014e42c27 100644

arch/arm64/kvm/hyp/nvhe/setup.c line 27

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/setup.c +2 -0
@@ -23,6 +23,9 @@
 
 unsigned long hyp_nr_cpus;
 
+/* See kvm_iommu_pages() */
+unsigned int hyp_kvm_iommu_pages;

nit: we tend to use everywhere unsigned long or u64 for nr_pages.

Quoted 39 lines
arch/arm64/kvm/hyp/nvhe/setup.c +9 -1
+
 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
 			 (unsigned long)__per_cpu_start)
 
@@ -34,6 +37,7 @@ static void *selftest_base;
 static void *ffa_proxy_pages;
 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
 static struct hyp_pool hpool;
+static void *iommu_base;
 
 static int divide_memory_pool(void *virt, unsigned long size)
 {
@@ -71,6 +75,12 @@ static int divide_memory_pool(void *virt, unsigned long size)
 	if (!ffa_proxy_pages)
 		return -ENOMEM;
 
+	if (hyp_kvm_iommu_pages) {
+		iommu_base = hyp_early_alloc_contig(hyp_kvm_iommu_pages);
+		if (!iommu_base)
+			return -ENOMEM;
+	}
+
 	return 0;
 }
 
@@ -330,7 +340,7 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
-	ret = kvm_iommu_init();
+	ret = kvm_iommu_init(iommu_base, hyp_kvm_iommu_pages);
 	if (ret)
 		goto out;
 
arch/arm64/kvm/iommu.c +0 -0
index f247384fa193..213429ceb549 100644

arch/arm64/kvm/iommu.c line 14

Quoted 9 lines
arch/arm64/kvm/iommu.c +2 -1
@@ -7,10 +7,11 @@
 #include <linux/kvm_host.h>
 
 extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+extern unsigned int kvm_nvhe_sym(hyp_kvm_iommu_pages);
 
 static DEFINE_MUTEX(kvm_iommu_reg_lock);
 
-int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops, unsigned int pool_pages)

nit: kvm_iommu_init uses nr_pages.

Quoted 20 lines
arch/arm64/kvm/iommu.c +10 -0
 {
 	guard(mutex)(&kvm_iommu_reg_lock);
 
@@ -21,6 +22,36 @@ int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(kvm_iommu_ops))
 		return -EBUSY;
 
+	/* See kvm_iommu_pages() */
+	if (pool_pages > kvm_nvhe_sym(hyp_kvm_iommu_pages)) {
+		kvm_err("Not enough memory for the IOMMU pool, need 0x%x pages, check kvm-arm.hyp_iommu_pages",
+			pool_pages);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
 	return 0;
 }
+
+unsigned int kvm_iommu_pages(void)
+{

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 47

Quoted 10 lines
arch/arm64/kvm/iommu.c +10 -0
+	/*
+	 * This is used very early during setup_arch() before any initcalls
+	 * or any drivers are registered.
+	 * This value is set by a command line option.
+	 * Later, when the driver is registered, it will pass the number
+	 * pages needed for it's page tables, if it was less that what
+	 * the system has already allocated, the registration will fail.
+	 */
+	return kvm_nvhe_sym(hyp_kvm_iommu_pages);
+}

Could we actually estimate the memory needed, based on the allocation for the host stage-2 and an estimation of MMIO coverage? This would allow the whole thing to run without setting this value as a best effort and to make it optional, which is surely more user-friendly?

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 55

Quoted 8 lines
arch/arm64/kvm/iommu.c +8 -0
+
+/* Number of pages to reserve for iommu pool*/
+static int __init early_hyp_iommu_pages(char *arg)
+{
+	if (!arg)
+		return -EINVAL;
+
+	return kstrtouint(arg, 0, &kvm_nvhe_sym(hyp_kvm_iommu_pages));

How about using a memory size here with memparse()? This would be more transparent for the commandline which can work on different page-size systems.

Quoted 17 lines
arch/arm64/kvm/iommu.c +2 -0
+}
+early_param("kvm-arm.hyp_iommu_pages", early_hyp_iommu_pages);
arch/arm64/kvm/pkvm.c +1 -0
index 053e4f733e4b..79dd14db4919 100644
@@ -63,6 +63,7 @@ void __init kvm_hyp_reserve(void)
 	hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
 	hyp_mem_pages += pkvm_selftest_pages();
 	hyp_mem_pages += hyp_ffa_proxy_pages();
+	hyp_mem_pages += kvm_iommu_pages();
 
 	/*
 	 * Try to allocate a PMD-aligned region to reduce TLB pressure once
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#6 lore raw

Re: [PATCH v6 02/25] KVM: arm64: Donate MMIO to the hypervisor

In reply to Mostafa Saleh
Quoted 30 lines · Mostafa Saleh

Add a function to donate MMIO to the hypervisor so IOMMU hypervisor drivers can protect and access the MMIO of IOMMUs.

As donating MMIO is very rare, and we don’t need to encode the full state, it’s reasonable to have a separate function to do this. It will init the host s2 page table with an invalid leaf with the owner ID to prevent the host from mapping the page on faults.

Also, prevent kvm_pgtable_stage2_unmap() from removing owner ID from stage-2 PTEs, as this can be triggered from recycle logic under memory pressure. There is no code relying on this, as all ownership changes is done via kvm_pgtable_stage2_set_owner()

For the error path in IOMMU drivers, add a function to donate MMIO back from hyp to host. However, that leaks the hypervisor virtual address range which should be acceptable as this is quite rare and it matches the behaviour of fix_map/block.

---
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   2 +
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 119 +++++++++++++++++-
 arch/arm64/kvm/hyp/pgtable.c                  |   9 +-
 3 files changed, 121 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0 -0
index 3cbfae0e3dda..ff440204d2c7 100644

arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn); · line 40

Quoted 5 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
@@ -36,6 +36,8 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_host_unshare_hyp(u64 pfn);
 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
+int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size, unsigned long *haddr);
+int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size);

Can we have u64 pfn, u64 nr_pages here to match all the other functions?

Also, as this is used by a "module" perhaps a sort of kerneldoc would be nice, especially to explain this isn't mapped into the hyp idmap range.

Quoted 30 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0 -0
 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
arch/arm64/kvm/hyp/nvhe/mem_protect.c +19 -0
index 28a471d1927c..2fb20a63a417 100644
@@ -353,6 +353,38 @@ int __pkvm_prot_finalize(void)
 	return 0;
 }
 
+/* Unmap MMIO region while skipping donated PTEs. */
+static int host_stage2_unmap_mmio_region(u64 start, u64 size)
+{
+	struct kvm_pgtable *pgt = &host_mmu.pgt;
+	u64 unmap_start = start;
+	u64 addr = start;
+	kvm_pte_t pte;
+	int ret = 0;
+	u8 level;
+
+	while (addr < start + size) {
+		ret = kvm_pgtable_get_leaf(pgt, addr, &pte, &level);
+		if (ret)
+			return ret;
+		if (!kvm_pte_valid(pte) && pte != 0) {
+			if (addr > unmap_start) {
+				ret = kvm_pgtable_stage2_unmap(pgt, unmap_start,
+							       addr - unmap_start);
+				if (ret)

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_prot_finalize(void) · line 384

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+					return ret;
+			}
+			addr += kvm_granule_size(level);
+			unmap_start = addr;
+		} else {
+			addr += kvm_granule_size(level);
+		}
+	}
+	if (addr > unmap_start)
+		ret = kvm_pgtable_stage2_unmap(pgt, unmap_start, addr - unmap_start);

Ah shame, that's a lot of table walking. I guess we could:

* create our own custom walker here but that would duplicate a lot of logic from pgtable.c. So doesn't look great.

* Retry kvm_pgtable_stage2_unmap() as long as it return -EPERM... but then we it needs to return where it has stopped?

* Modify kvm_pgtable_stage2_unmap() to just skip the refcounted entries? There's already a flag something that looks like this: KVM_PGTABLE_WALK_IGNORE_EAGAIN

I am not so convinced by the first two points but the last one is probably worth exploring.

Quoted 26 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +7 -2
+	return ret;
+}
+
 static int host_stage2_unmap_dev_all(void)
 {
 	struct kvm_pgtable *pgt = &host_mmu.pgt;
@@ -363,11 +395,11 @@ static int host_stage2_unmap_dev_all(void)
 	/* Unmap all non-memory regions to recycle the pages */
 	for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
 		reg = &hyp_memory[i];
-		ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
+		ret = host_stage2_unmap_mmio_region(addr, reg->base - addr);
 		if (ret)
 			return ret;
 	}
-	return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
+	return host_stage2_unmap_mmio_region(addr, BIT(pgt->ia_bits) - addr);
 }
 
 /*
@@ -1087,6 +1119,89 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
 	return ret;
 }
 
+int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size, unsigned long *haddr)
+{

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1133

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+	kvm_pte_t pte;
+	u64 offset;
+	int ret;
+
+	/* Only before de-privilege. */
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!PAGE_ALIGNED(addr | size))
+		return -EINVAL;

Would be nice here to have pfn_range_is_valid() even though this is only used by modules? (ditto __pkvm_hyp_donate_host_mmio())

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1137

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -0
+
+	ret = __pkvm_create_private_mapping(addr, size, PAGE_HYP_DEVICE, haddr);
+	if (ret)
+		return ret;

Perhaps better to do it once it's been annotated into the host s2, so that we don't burn the private range in case of failure.

Quoted 46 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +46 -0
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(addr + offset)) {
+			ret = -EINVAL;
+			goto unlock;
+		}
+		ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL);
+		if (ret)
+			goto unlock;
+		if (pte && !kvm_pte_valid(pte)) {
+			ret = -EPERM;
+			goto unlock;
+		}
+	}
+	/*
+	 * We set HYP as the owner of the MMIO pages in the host stage-2, for:
+	 * - host aborts: host_stage2_adjust_range() would fail for invalid non zero PTEs.
+	 * - recycle under memory pressure: host_stage2_unmap_dev_all() would call
+	 *   kvm_pgtable_stage2_unmap() which will not clear non zero invalid ptes (counted).
+	 * - other MMIO donation: Would fail as we check that the PTE is valid or empty.
+	 */
+	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
+			      addr, size, &host_s2_pool,
+			      KVM_HOST_INVALID_PTE_TYPE_DONATION,
+			      FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, PKVM_ID_HYP));
+unlock:
+	host_unlock_component();
+	return ret;
+}
+
+int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size)
+{
+	kvm_pte_t pte;
+	u64 offset;
+	int ret = 0;
+
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!PAGE_ALIGNED(addr | size))
+		return -EINVAL;
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(addr + offset)) {

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1193

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+			ret = -EINVAL;
+			goto unlock;
+		}
+		ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL);
+		if (ret)
+			goto unlock;
+		if (!pte || kvm_pte_valid(pte)) {
+			ret = -EINVAL;
+			goto unlock;
+		}

It should probably verify it is also mapped as we expect into the hyp.

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1199

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+		if (FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) != PKVM_ID_HYP) {
+			ret = -EPERM;
+			goto unlock;
+		}
+	}
+	WARN_ON(host_stage2_idmap_locked(addr, size, PKVM_HOST_MMIO_PROT));

Perhaps we are better of simply zapping the entry and not map it proactively?

And even if we can't "reclaim" the VA-space from the private range, it would be good to unmap it. I have made a function for that as part of the allocator series:

https://lore.kernel.org/all/[email protected]/

Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
+unlock:
+	host_unlock_component();
+	return ret;
+}
+
 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
arch/arm64/kvm/hyp/pgtable.c +2 -7
index 0c1defa5fb0f..b64a50f9bfa8 100644
@@ -1159,13 +1159,8 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
 	kvm_pte_t *childp = NULL;
 	bool need_flush = false;
 
-	if (!kvm_pte_valid(ctx->old)) {
-		if (stage2_pte_is_counted(ctx->old)) {
-			kvm_clear_pte(ctx->ptep);
-			mm_ops->put_page(ctx->ptep);
-		}
-		return 0;
-	}
+	if (!kvm_pte_valid(ctx->old))
+		return stage2_pte_is_counted(ctx->old) ? -EPERM : 0;
 
 	if (kvm_pte_table(ctx->old, ctx->level)) {
 		childp = kvm_pte_follow(ctx->old, mm_ops);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#4 lore raw

Re: [PATCH v6 01/25] KVM: arm64: Generalize trace clock

In reply to Vincent Donnefort

Hi Vincent,

Quoted 16 lines · Vincent Donnefort

Hi Mostafa,

Sorry I should have looked at this a while ago!

Quoted 11 lines · Mostafa Saleh

IOMMU drivers need to track time, mainly for timeouts. Generalize the tracing clock functions in the hypervsior, so they can be used from IOMMU drivers.

1) Make the compilation independent from tracing.

2) As drivers might need to use that quite early, provide default values for the clock data based on cntfrq_el0, the driver can keep using these values without calling hyp_clock_update() as they don't need to sync with the host timers.

Quoted 7 lines
Quoted 4 lines

This is mainly used for timeouts, so a malicious host can DoS the system or cause premature timeouts which likely end up in hyp panic, that should be acceptable as neither of those would undermine the security guarantees.

This still sounds very risky if the usage shift, and as I explain later I don't believe this is completely OK as regard to mult/shift updates.

I see, I still think it is not a security risk though. My main concern is switching banks between polling might cause problems.

Quoted 63 lines
Quoted 53 lines
---
 arch/arm64/kvm/hyp/include/nvhe/clock.h | 11 ++-----
 arch/arm64/kvm/hyp/nvhe/Makefile        |  4 +--
 arch/arm64/kvm/hyp/nvhe/clock.c         | 44 ++++++++++++++++++++++---
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++
 arch/arm64/kvm/hyp/nvhe/trace.c         |  4 +--
 5 files changed, 51 insertions(+), 17 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/clock.h +3 -8
index 9f429f5c0664..e6a0e43af88d 100644
@@ -5,12 +5,7 @@
 
 #include <asm/kvm_hyp.h>
 
-#ifdef CONFIG_NVHE_EL2_TRACING
-void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
-u64 trace_clock(void);
-#else
-static inline void
-trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) { }
-static inline u64 trace_clock(void) { return 0; }
-#endif
+void hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc);
+u64 hyp_clock_ns(void);
+int hyp_clock_init(void);
 #endif
arch/arm64/kvm/hyp/nvhe/Makefile +2 -2
index 62cdfbff7562..89d0533921f9 100644
@@ -26,10 +26,10 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
-	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
+	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
-hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o trace.o events.o
+hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += trace.o events.o
 hyp-obj-y += $(lib-objs)
 
 # Path to simple_ring_buffer.c
arch/arm64/kvm/hyp/nvhe/clock.c +0 -0
index 32fc4313fe43..53d0bd55e866 100644

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 21

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/clock.c +1 -1
@@ -18,7 +18,41 @@ static struct clock_data {
 		u64 cyc_overflow64;
 	} data[2];
 	u64 cur;
-} trace_clock_data;
+} clock_data;

I believe this should be separated. the trace_clock is "unsafe" but synchronised with the host. the clock you need must be "safe" but we don't really need to be synchronised.

Quoted 2 lines

So overall, I would keep all the trace_clock_* interface unchanged and introduce in the same file a hyp_clock, with its own hyp_clock_data.

Makes sense, I will do that, I will make the file compile outside of tracing but I will keep the tracing code as is. I will introduce 2 functions to read the system timer and to init the hypervisor which was similar to the earlier versions.

Quoted 6 lines

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 23

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -0
+
+#define HYP_CLK_SEC_TO_NS 1000000000UL

Not possible to use NSEC_PER_SEC?

It should, we have to include include/vdso/time64.h for that.

Quoted 34 lines
Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/clock.c +20 -0
+
+int hyp_clock_init(void)
+{
+	u32 timer_freq = read_sysreg(cntfrq_el0);
+	u32 shift = 32;
+	u64 mult;
+
+	/*
+	 * KVM will not initialize if FW didn't set cntfrq_el0, that is already
+	 * part of the boot protocol.
+	 */
+	if (!timer_freq)
+		return -ENODEV;
+
+	/* Timer freq can't be larger than 1Ghz by spec. */
+	if (timer_freq > HYP_CLK_SEC_TO_NS)
+		return -EINVAL;
+
+	/* Simplified logic from clocks_calc_mult_shift() */
+	do {

arch/arm64/kvm/hyp/nvhe/clock.c static struct clock_data { · line 53

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/clock.c +10 -0
+		mult = (HYP_CLK_SEC_TO_NS << shift);
+		mult = div_u64(mult, timer_freq);
+		if (mult <= (~0U))
+			break;
+		shift--;
+	} while (shift > 0);
+
+	clock_data.data[0].shift = shift;
+	clock_data.data[0].mult = mult;
+	clock_data.data[0].cyc_overflow64 = ULONG_MAX / mult;

I don't know if we want to update this from time to time, but we can't rely on host provided data. Perhaps the call to that clock could self-update?

Quoted 5 lines

Worst case we fallback on 128-bits mult. but perhaps this isn't a problem for a wait loop()?

And as this is wait loop, perhaps we could reduce the resolution quite heavily, so we reduce the risk of overflow?

As mentioned, I will drop this and rely on a wrapper for the system counter instead, and the caller can handle the overflow as in __delay()

Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -0
+	return 0;
+}
 
 static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift)
 {
@@ -30,9 +64,9 @@ static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift)
 }
 
Quoted 5 lines

arch/arm64/kvm/hyp/nvhe/clock.c static u64 __clock_mult_uint128(u64 cyc, u32 mult, u32 shift) · line 66

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/clock.c +0 -0
 /* Does not guarantee no reader on the modified bank. */

^
And here's another reason why you should have another clock. It works because of tricks in the tracing interface.

Agreed, this was my concern too.

Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -2
-void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
+void hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 {
-	struct clock_data *clock = &trace_clock_data;
+	struct clock_data *clock = &clock_data;
 	u64 bank = clock->cur ^ 1;
 
 	clock->data[bank].mult			= mult;
@@ -45,9 +79,9 @@ void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
 }
 
Quoted 9 lines

arch/arm64/kvm/hyp/nvhe/clock.c void trace_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc) · line 85

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/clock.c +2 -2
 /* Use untrusted host data */
-u64 trace_clock(void)
+u64 hyp_clock_ns(void)
 {
-	struct clock_data *clock = &trace_clock_data;
+	struct clock_data *clock = &clock_data;
 	u64 bank = smp_load_acquire(&clock->cur);

The whole bank system is not necessary if not updated by the host.

But tracing can be enabled along side the SMMUv3.

Thanks,
Mostafa

#7 lore raw

Re: [PATCH v6 02/25] KVM: arm64: Donate MMIO to the hypervisor

In reply to Vincent Donnefort
Quoted 26 lines · Vincent Donnefort
Quoted 25 lines · Mostafa Saleh

Add a function to donate MMIO to the hypervisor so IOMMU hypervisor drivers can protect and access the MMIO of IOMMUs.

As donating MMIO is very rare, and we don’t need to encode the full state, it’s reasonable to have a separate function to do this. It will init the host s2 page table with an invalid leaf with the owner ID to prevent the host from mapping the page on faults.

Also, prevent kvm_pgtable_stage2_unmap() from removing owner ID from stage-2 PTEs, as this can be triggered from recycle logic under memory pressure. There is no code relying on this, as all ownership changes is done via kvm_pgtable_stage2_set_owner()

For the error path in IOMMU drivers, add a function to donate MMIO back from hyp to host. However, that leaks the hypervisor virtual address range which should be acceptable as this is quite rare and it matches the behaviour of fix_map/block.

---
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   2 +
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 119 +++++++++++++++++-
 arch/arm64/kvm/hyp/pgtable.c                  |   9 +-
 3 files changed, 121 insertions(+), 9 deletions(-)
Quoted 13 lines

arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn); · line 40

Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
index 3cbfae0e3dda..ff440204d2c7 100644
@@ -36,6 +36,8 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
 int __pkvm_host_unshare_hyp(u64 pfn);
 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
+int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size, unsigned long *haddr);
+int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size);

Can we have u64 pfn, u64 nr_pages here to match all the other functions?

I see the convention is that phys_addr_t is used for physical addresses as (addr_is_memory(), host_stage2_idmap_locked(()...) while u64 is used for pfn.

Quoted 2 lines

Also, as this is used by a "module" perhaps a sort of kerneldoc would be nice, especially to explain this isn't mapped into the hyp idmap range.

Sure I can add a comment.

Quoted 44 lines
Quoted 31 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0 -0
 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
arch/arm64/kvm/hyp/nvhe/mem_protect.c +19 -0
index 28a471d1927c..2fb20a63a417 100644
@@ -353,6 +353,38 @@ int __pkvm_prot_finalize(void)
 	return 0;
 }
 
+/* Unmap MMIO region while skipping donated PTEs. */
+static int host_stage2_unmap_mmio_region(u64 start, u64 size)
+{
+	struct kvm_pgtable *pgt = &host_mmu.pgt;
+	u64 unmap_start = start;
+	u64 addr = start;
+	kvm_pte_t pte;
+	int ret = 0;
+	u8 level;
+
+	while (addr < start + size) {
+		ret = kvm_pgtable_get_leaf(pgt, addr, &pte, &level);
+		if (ret)
+			return ret;
+		if (!kvm_pte_valid(pte) && pte != 0) {
+			if (addr > unmap_start) {
+				ret = kvm_pgtable_stage2_unmap(pgt, unmap_start,
+							       addr - unmap_start);
+				if (ret)

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_prot_finalize(void) · line 384

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+					return ret;
+			}
+			addr += kvm_granule_size(level);
+			unmap_start = addr;
+		} else {
+			addr += kvm_granule_size(level);
+		}
+	}
+	if (addr > unmap_start)
+		ret = kvm_pgtable_stage2_unmap(pgt, unmap_start, addr - unmap_start);

Ah shame, that's a lot of table walking. I guess we could:

Quoted 2 lines

* create our own custom walker here but that would duplicate a lot of logic from pgtable.c. So doesn't look great.

Yes, that seems too complicated.

Quoted 3 lines

* Retry kvm_pgtable_stage2_unmap() as long as it return -EPERM... but then we it needs to return where it has stopped?

Yes, that is not possible with its current interface.

Quoted 4 lines

* Modify kvm_pgtable_stage2_unmap() to just skip the refcounted entries? There's already a flag something that looks like this: KVM_PGTABLE_WALK_IGNORE_EAGAIN

I thought about this, my main concern is that now any unmap will fail silently if it hits a refcounted page. Propably not too bad as that only is used in recycling.

Quoted 27 lines

I am not so convinced by the first two points but the last one is probably worth exploring.

Quoted 24 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -2
+	return ret;
+}
+
 static int host_stage2_unmap_dev_all(void)
 {
 	struct kvm_pgtable *pgt = &host_mmu.pgt;
@@ -363,11 +395,11 @@ static int host_stage2_unmap_dev_all(void)
 	/* Unmap all non-memory regions to recycle the pages */
 	for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
 		reg = &hyp_memory[i];
-		ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
+		ret = host_stage2_unmap_mmio_region(addr, reg->base - addr);
 		if (ret)
 			return ret;
 	}
-	return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
+	return host_stage2_unmap_mmio_region(addr, BIT(pgt->ia_bits) - addr);
 }
 
 /*
@@ -1087,6 +1119,89 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
 	return ret;
 }
 
Quoted 15 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1133

Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +12 -0
+int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size, unsigned long *haddr)
+{
+	kvm_pte_t pte;
+	u64 offset;
+	int ret;
+
+	/* Only before de-privilege. */
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!PAGE_ALIGNED(addr | size))
+		return -EINVAL;

Would be nice here to have pfn_range_is_valid() even though this is only used by modules? (ditto __pkvm_hyp_donate_host_mmio())

Will do.

Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1137

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -0
+
+	ret = __pkvm_create_private_mapping(addr, size, PAGE_HYP_DEVICE, haddr);
+	if (ret)
+		return ret;

Perhaps better to do it once it's been annotated into the host s2, so that we don't burn the private range in case of failure.

This really should not fail, or the system is already have bigger problems, and that also does not allocate much space (128K), I see that is easier to do this way than having to fix the host stage-2 if the allocation fails.

Quoted 51 lines
Quoted 51 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +50 -0
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(addr + offset)) {
+			ret = -EINVAL;
+			goto unlock;
+		}
+		ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL);
+		if (ret)
+			goto unlock;
+		if (pte && !kvm_pte_valid(pte)) {
+			ret = -EPERM;
+			goto unlock;
+		}
+	}
+	/*
+	 * We set HYP as the owner of the MMIO pages in the host stage-2, for:
+	 * - host aborts: host_stage2_adjust_range() would fail for invalid non zero PTEs.
+	 * - recycle under memory pressure: host_stage2_unmap_dev_all() would call
+	 *   kvm_pgtable_stage2_unmap() which will not clear non zero invalid ptes (counted).
+	 * - other MMIO donation: Would fail as we check that the PTE is valid or empty.
+	 */
+	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
+			      addr, size, &host_s2_pool,
+			      KVM_HOST_INVALID_PTE_TYPE_DONATION,
+			      FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, PKVM_ID_HYP));
+unlock:
+	host_unlock_component();
+	return ret;
+}
+
+int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size)
+{
+	kvm_pte_t pte;
+	u64 offset;
+	int ret = 0;
+
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!PAGE_ALIGNED(addr | size))
+		return -EINVAL;
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(addr + offset)) {
+			ret = -EINVAL;
+			goto unlock;
+		}
+		ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL);
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1193

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+		if (ret)
+			goto unlock;
+		if (!pte || kvm_pte_valid(pte)) {
+			ret = -EINVAL;
+			goto unlock;
+		}

It should probably verify it is also mapped as we expect into the hyp.

I thought about that, but the caller is be trusted and I was aiming for simplicity, so the check for PKVM_ID_HYP seemed enough, but no strong opinion.

Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 1199

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+		if (FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) != PKVM_ID_HYP) {
+			ret = -EPERM;
+			goto unlock;
+		}
+	}
+	WARN_ON(host_stage2_idmap_locked(addr, size, PKVM_HOST_MMIO_PROT));

Perhaps we are better of simply zapping the entry and not map it proactively?

I do not think it matters that much, this is a failure path, and the space is too small anyway. But no strong opinion also.

Quoted 7 lines

And even if we can't "reclaim" the VA-space from the private range, it would be good to unmap it. I have made a function for that as part of the allocator series:

https://lore.kernel.org/all/[email protected]/

I do not think that is a big problem at the moment all the other users of the VA space leak it on failure, I can re-use this logic when the allocator seires lands, otherwise I'd avoid adding more code.

Thanks,
Mostafa

#40 lore raw

Re: [PATCH v6 07/25] KVM: arm64: iommu: Introduce IOMMU driver infrastructure

In reply to Vincent Donnefort
Quoted 11 lines · Vincent Donnefort
Quoted 8 lines · Mostafa Saleh

To establish DMA isolation, KVM needs an IOMMU driver which provides ops implemented at EL2.

Only one driver can be used and is registered with kvm_iommu_register_driver() by passing pointer to the ops.

This must be called before module_init() which is the point KVM initializes.

Everywhere s/KVM/pKVM/ as this can be registered only if pKVM is enabled.

Will do.

Quoted 23 lines
Quoted 23 lines
---
 arch/arm64/include/asm/kvm_host.h       |  5 +++++
 arch/arm64/kvm/Makefile                 |  2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h | 13 +++++++++++++
 arch/arm64/kvm/hyp/nvhe/Makefile        |  3 ++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c   | 20 +++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c         |  5 +++++
 arch/arm64/kvm/iommu.c                  | 26 +++++++++++++++++++++++++
 7 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c
 create mode 100644 arch/arm64/kvm/iommu.c
arch/arm64/include/asm/kvm_host.h +0 -0
index 851f6171751c..52898d2a3ec6 100644
@@ -1733,4 +1733,9 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg
 
Quoted 6 lines

arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1736

Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +1 -0
 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
+#ifndef __KVM_NVHE_HYPERVISOR__

I think it's not necessary, a lot of things aren't guarded for __KVM_NVHE_HYPERVISOR__.

That makes it clear that it is hypervisor code, but no strong opinion.

Quoted 4 lines

arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1737

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+struct kvm_iommu_ops;

Perhaps we can keep this private to hyp code by just using (void *) here?

That does not leak the struct, it is just a forward declaration, I beleive it is more clear and readable this way.

Quoted 17 lines
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +3 -0
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops);
+#endif
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Makefile +0 -0
index 59612d2f277c..0ddef54f7434 100644

arch/arm64/kvm/Makefile kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ · line 27

Quoted 5 lines
arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \
 	 vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
-	 vgic/vgic-v5.o
+	 vgic/vgic-v5.o iommu.o

alphabetical order.

That does not seem to be already sorted.

Quoted 49 lines
Quoted 49 lines
arch/arm64/kvm/Makefile +0 -0
 kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
arch/arm64/kvm/hyp/include/nvhe/iommu.h new file +13 -0
new file mode 100644
index 000000000000..1ac70cc28a9e
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ARM64_KVM_NVHE_IOMMU_H__
+#define __ARM64_KVM_NVHE_IOMMU_H__
+
+#include <asm/kvm_host.h>
+
+struct kvm_iommu_ops {
+	int (*init)(void);
+};
+
+int kvm_iommu_init(void);
+
+#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index 89d0533921f9..606c0e1b7bd0 100644
@@ -24,7 +24,8 @@ CFLAGS_switch.nvhe.o += -Wno-override-init
 
 hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
 	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
-	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
+	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o \
+	 iommu/iommu.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o clock.o
 hyp-obj-y += ../../../kernel/smccc-call.o
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +6 -0
new file mode 100644
index 000000000000..406c8fb9b3b9
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * IOMMU operations for pKVM
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ */
Quoted 9 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 12

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +6 -0
+#include <nvhe/iommu.h>
+
+/* Only one set of ops supported */
+struct kvm_iommu_ops *kvm_iommu_ops;
+
+

Empty line.

Will remove it.

Quoted 9 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 17

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +5 -0
+int kvm_iommu_init(void)
+{
+	/* Keep DMA isolation optional. */
+	if (!kvm_iommu_ops || !kvm_iommu_ops->init)
+		return 0;

The absence of the SME workaround on affected systems breaks pKVM init. So perhaps the IOMMU should do the same? (see pkvm_check_sme_dvmsync_fw_call())

I initially done something similar, but I was not sure if that is too limiting as now pKVM will not run on many hardware, I think the best option is to make this cmdline configurable wether DMA isolation is required or not.
But for this series we can keep it optional.

Quoted 37 lines
Quoted 37 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c new file +3 -0
+
+	return kvm_iommu_ops->init();
+}
arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index 8041f6e80cd1..1f6b221db9a0 100644
@@ -14,6 +14,7 @@
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -329,6 +330,10 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
+	ret = kvm_iommu_init();
+	if (ret)
+		goto out;
+
 	ret = hyp_ffa_init(ffa_proxy_pages);
 	if (ret)
 		goto out;
arch/arm64/kvm/iommu.c new file +5 -0
new file mode 100644
index 000000000000..f247384fa193
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Mostafa Saleh <[email protected]>
+ */
Quoted 8 lines

arch/arm64/kvm/iommu.c line 11

Quoted 6 lines
arch/arm64/kvm/iommu.c new file +6 -0
+
+#include <linux/kvm_host.h>
+
+extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+
+static DEFINE_MUTEX(kvm_iommu_reg_lock);

could be moved directly into the only user: kvm_iommu_register_driver

No strong opinion, it seemed it can be easily re-used this way.

Quoted 5 lines

arch/arm64/kvm/iommu.c line 13

Quoted 3 lines
arch/arm64/kvm/iommu.c new file +2 -0
+
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)

__init ?

Will do.

Quoted 7 lines

arch/arm64/kvm/iommu.c line 17

Quoted 5 lines
arch/arm64/kvm/iommu.c new file +4 -0
+{
+	guard(mutex)(&kvm_iommu_reg_lock);
+
+	/* Only protected KVM before de-privilege. */

Don't think that comment brings much information, looking at the line below.

I will remove it.

Quoted 5 lines

arch/arm64/kvm/iommu.c line 19

Quoted 3 lines
arch/arm64/kvm/iommu.c new file +2 -0
+	if (!is_protected_kvm_enabled() || is_kvm_arm_initialised())
+		return -EINVAL;

-EPERM?

I can see it both ways, my thought that it is invalid to register a driver after a certain point, but -EPERM makes sense also.

Thanks,
Mostafa

#63 lore raw

Re: [PATCH v6 09/25] KVM: arm64: iommu: Add memory pool

In reply to Vincent Donnefort
Quoted 25 lines · Vincent Donnefort
Quoted 24 lines · Mostafa Saleh

IOMMU drivers would require to allocate memory for the shadow page table. Similar to the host stage-2 CPU page table, the IOMMU pool is allocated early from the carveout and it's memory is added in a pool which the IOMMU driver can allocate from and reclaim at run time.

As this is too early for drivers to use init calls, set the number of page allocated from the kernel command line "kvm-arm.hyp_iommu_pages".

Later when the driver registers, it will pass how many pages it needs, and if it was less than what was allocated, it will fail to register.

---
 .../admin-guide/kernel-parameters.txt         |  4 +++
 arch/arm64/include/asm/kvm_host.h             |  3 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |  7 +++-
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         | 21 +++++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               | 12 ++++++-
 arch/arm64/kvm/iommu.c                        | 33 ++++++++++++++++++-
 arch/arm64/kvm/pkvm.c                         |  1 +
 7 files changed, 76 insertions(+), 5 deletions(-)

[...]

Quoted 2 lines

If we expect drivers to use that perhaps we should document a bit better (same for kvm_iommu_reclaim_pages()

Will do.

Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c

Quoted 9 lines
arch/arm64/kvm/hyp/include/nvhe/iommu.h +3 -0
+void *kvm_iommu_donate_pages(u8 order);
+void kvm_iommu_reclaim_pages(void *ptr);
+
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +0 -0
index 1db52bd87c38..53cb5e4b0aac 100644

Do we really need that subfolder iommu/ ?

Propably not, I will remove it.

Quoted 53 lines
Quoted 53 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +20 -1
@@ -15,6 +15,7 @@ struct kvm_iommu_ops *kvm_iommu_ops;
 
 /* Protected by host_mmu.lock */
 static bool kvm_idmap_initialized;
+static struct hyp_pool iommu_pages_pool;
 
 static inline int pkvm_to_iommu_prot(enum kvm_pgtable_prot prot)
 {
@@ -95,7 +96,7 @@ static int kvm_iommu_snapshot_host_stage2(void)
 	return ret;
 }
 
-int kvm_iommu_init(void)
+int kvm_iommu_init(void *pool_base, unsigned int nr_pages)
 {
 	int ret;
 
@@ -103,6 +104,14 @@ int kvm_iommu_init(void)
 	    !kvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)
+		return -ENOMEM;
+
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = kvm_iommu_ops->init();
 	if (ret)
 		return ret;
@@ -120,3 +129,13 @@ int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 
 	return kvm_iommu_ops->host_stage2_idmap(start, end, pkvm_to_iommu_prot(prot));
 }
+
+void *kvm_iommu_donate_pages(u8 order)
+{
+	return hyp_alloc_pages(&iommu_pages_pool, order);
+}
+
+void kvm_iommu_reclaim_pages(void *ptr)
+{
+	hyp_put_page(&iommu_pages_pool, ptr);
+}
arch/arm64/kvm/hyp/nvhe/setup.c +0 -0
index 1f6b221db9a0..215014e42c27 100644
@@ -23,6 +23,9 @@
 
Quoted 6 lines

arch/arm64/kvm/hyp/nvhe/setup.c line 27

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/setup.c +2 -0
 unsigned long hyp_nr_cpus;
 
+/* See kvm_iommu_pages() */
+unsigned int hyp_kvm_iommu_pages;

nit: we tend to use everywhere unsigned long or u64 for nr_pages.

Initially it was size_t. However, the hyp allocator uses unsigned int so I was matching its type, I was considering to send a patch upstream for this but that is not really a problem at the moment as it only overflows if the system has ~2TB of memory.

Quoted 79 lines
Quoted 40 lines
arch/arm64/kvm/hyp/nvhe/setup.c +9 -1
+
 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
 			 (unsigned long)__per_cpu_start)
 
@@ -34,6 +37,7 @@ static void *selftest_base;
 static void *ffa_proxy_pages;
 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
 static struct hyp_pool hpool;
+static void *iommu_base;
 
 static int divide_memory_pool(void *virt, unsigned long size)
 {
@@ -71,6 +75,12 @@ static int divide_memory_pool(void *virt, unsigned long size)
 	if (!ffa_proxy_pages)
 		return -ENOMEM;
 
+	if (hyp_kvm_iommu_pages) {
+		iommu_base = hyp_early_alloc_contig(hyp_kvm_iommu_pages);
+		if (!iommu_base)
+			return -ENOMEM;
+	}
+
 	return 0;
 }
 
@@ -330,7 +340,7 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
-	ret = kvm_iommu_init();
+	ret = kvm_iommu_init(iommu_base, hyp_kvm_iommu_pages);
 	if (ret)
 		goto out;
 
arch/arm64/kvm/iommu.c +0 -0
index f247384fa193..213429ceb549 100644

arch/arm64/kvm/iommu.c line 14

Quoted 9 lines
arch/arm64/kvm/iommu.c +2 -1
@@ -7,10 +7,11 @@
 #include <linux/kvm_host.h>
 
 extern struct kvm_iommu_ops *kvm_nvhe_sym(kvm_iommu_ops);
+extern unsigned int kvm_nvhe_sym(hyp_kvm_iommu_pages);
 
 static DEFINE_MUTEX(kvm_iommu_reg_lock);
 
-int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
+int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops, unsigned int pool_pages)

nit: kvm_iommu_init uses nr_pages.

Quoted 27 lines
arch/arm64/kvm/iommu.c +17 -0
 {
 	guard(mutex)(&kvm_iommu_reg_lock);
 
@@ -21,6 +22,36 @@ int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(kvm_iommu_ops))
 		return -EBUSY;
 
+	/* See kvm_iommu_pages() */
+	if (pool_pages > kvm_nvhe_sym(hyp_kvm_iommu_pages)) {
+		kvm_err("Not enough memory for the IOMMU pool, need 0x%x pages, check kvm-arm.hyp_iommu_pages",
+			pool_pages);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
 	return 0;
 }
+
+unsigned int kvm_iommu_pages(void)
+{
+	/*
+	 * This is used very early during setup_arch() before any initcalls
+	 * or any drivers are registered.
+	 * This value is set by a command line option.
+	 * Later, when the driver is registered, it will pass the number
+	 * pages needed for it's page tables, if it was less that what
+	 * the system has already allocated, the registration will fail.
Quoted 8 lines

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 47

Quoted 3 lines
arch/arm64/kvm/iommu.c +3 -0
+	 */
+	return kvm_nvhe_sym(hyp_kvm_iommu_pages);
+}

Could we actually estimate the memory needed, based on the allocation for the host stage-2 and an estimation of MMIO coverage? This would allow the whole thing to run without setting this value as a best effort and to make it optional, which is surely more user-friendly?

Not really, this abstracts the IOMMU, so it does not have to match the host stage-2, in addition that the SMMUv3 driver requires extra pages for other in-memory data-structure.

At the moment, this is set from the command line which is not really best effort as the driver later confirms how much memory it needs.

Quoted 12 lines

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 55

Quoted 9 lines
arch/arm64/kvm/iommu.c +8 -0
+
+/* Number of pages to reserve for iommu pool*/
+static int __init early_hyp_iommu_pages(char *arg)
+{
+	if (!arg)
+		return -EINVAL;
+
+	return kstrtouint(arg, 0, &kvm_nvhe_sym(hyp_kvm_iommu_pages));

How about using a memory size here with memparse()? This would be more transparent for the commandline which can work on different page-size systems.

Makes sense, will do.

Thanks,
Mostafa

#76 lore raw

Re: [PATCH v6 16/25] iommu/arm-smmu-v3-kvm: Add CMDQ functions

In reply to Vincent Donnefort
Quoted 13 lines · Vincent Donnefort
Quoted 12 lines · Mostafa Saleh

Add functions to access the command queue, there are 2 main usage:
- Hypervisor's own commands, as TLB invalidation, would use functions as smmu_send_cmd(), which creates and sends a command.
- Add host commands to the shadow command queue, after being filtered, these will be added with smmu_add_cmd_raw.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  14 ++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 107 ++++++++++++++++++
 2 files changed, 115 insertions(+), 6 deletions(-)

[...]

Quoted 9 lines

I only looked at that smmu_wait() as this is the hyp_clock_ns() caller.

Quoted 4 lines

+/*
+ * Wait until @cond is true.
+ * Return 0 on success, or -ETIMEDOUT

nit: Would be nice to explain use_wfe.

I will add it.

Quoted 5 lines
Quoted 3 lines

+ */
+#define smmu_wait(use_wfe, _cond) \

nit:, (__use_wfe, __cond), or (use_fw, cond)

Will do.

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 33

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+({									\
+	int __ret = 0;							\
+	u64 delay = hyp_clock_ns() + ARM_SMMU_POLL_TIMEOUT_US * 1000;	\

Does it help to lower the risk for the 128-bits fallback if we keep the resolution to _us?

I will remove the tracing clock support as mentioned and I will handle the overflow in smmu_wait()

Quoted 4 lines

Also, ARM_SMMU_POLL_TIMEOUT_US seems to be 1s. Blocking that long at EL2 is really terrible. Any chance to lower that?

That should never really happen, I am re-using the kernel max, but I can change this for the hypervisor.

Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus; · line 38

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
+									\
+	while (!(_cond)) {						\
+		if (use_wfe) {						\
+			wfe();						\
+			if ((_cond))					\

nit: I believe there's no need for the double () here is there?

Will do.

Thanks,
Mostafa

#49 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 128 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

There are 3 different ways to add the callback:
1) In the high level memory transitions: (__pkvm_host_donate_hyp(), __pkvm_host_donate_guest()...

2) In Lower level functions covering all transitions
- host_stage2_set_owner_metadata_locked() which covers:
- __pkvm_host_donate_hyp()
- __pkvm_host_donate_guest()
- __pkvm_host_donate_hyp()
- __pkvm_guest_unshare_host()
- host_stage2_set_owner_locked() only for ID_HOST which covers:
- __pkvm_hyp_donate_host()
- __pkvm_host_force_reclaim_page_guest()
- __pkvm_host_reclaim_page_guest()
- __pkvm_guest_share_host()

3) In the lowest level function __host_update_page_state(), which requires only one callback. However, in that case the page state is not enough as we might need to know the old state also.

Option #3 was implemented here.

For some cases, an SMMUv3 may be able to share the same page-table used with the host CPU stage-2 directly.

However, this is too strict and requires changes to the core hypervisor page-table code, plus it would require the hypervisor to handle IOMMU page-faults. This can be added later as an optimization for SMMUV3.

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |   4 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   2 +
 arch/arm64/kvm/hyp/nvhe/iommu/iommu.c         | 108 +++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  35 ++++++
 4 files changed, 146 insertions(+), 3 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +4 -0
index 1ac70cc28a9e..6277d845cdcf 100644
@@ -3,11 +3,15 @@
 #define __ARM64_KVM_NVHE_IOMMU_H__
 
 #include <asm/kvm_host.h>
+#include <asm/kvm_pgtable.h>
 
 struct kvm_iommu_ops {
 	int (*init)(void);
+	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
 int kvm_iommu_init(void);
 
+int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
+				enum kvm_pgtable_prot prot);
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -0
index ff440204d2c7..f7faecc3b70a 100644
@@ -54,6 +54,8 @@ int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct
 int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu);
 
 bool addr_is_memory(phys_addr_t phys);
+u64 find_mem_range_from(u64 start, bool *is_memory);
+
 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id);
 int kvm_host_prepare_stage2(void *pgt_pool_base);
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +38 -0
index 406c8fb9b3b9..1db52bd87c38 100644
@@ -4,17 +4,119 @@
  *
  * Copyright (C) 2022 Linaro Ltd.
  */
+#include <linux/iommu.h>
+
 #include <nvhe/iommu.h>
+#include <nvhe/mem_protect.h>
+#include <nvhe/spinlock.h>
 
 /* Only one set of ops supported */
 struct kvm_iommu_ops *kvm_iommu_ops;
 
+/* Protected by host_mmu.lock */
+static bool kvm_idmap_initialized;
+
+static inline int pkvm_to_iommu_prot(enum kvm_pgtable_prot prot)
+{
+	int iommu_prot = 0;
+
+	if (prot & KVM_PGTABLE_PROT_R)
+		iommu_prot |= IOMMU_READ;
+	if (prot & KVM_PGTABLE_PROT_W)
+		iommu_prot |= IOMMU_WRITE;
+
+	/* We don't understand that, might be dangerous. */
+	WARN_ON(prot & ~PKVM_HOST_MEM_PROT);
+	return iommu_prot;
+}
+
+static int __snapshot_host_stage2(const struct kvm_pgtable_visit_ctx *ctx,
+				  enum kvm_pgtable_walk_flags visit)
+{
+	u64 start = ctx->addr;
+	u64 end = start + kvm_granule_size(ctx->level);
+	kvm_pte_t pte = *ctx->ptep;
+	bool is_memory;
+	u64 region_end;
+	int prot;
+	int ret;
+
+	/*
+	 * Keep annotated PTEs unmapped, and map everything else even lazily
+	 * mapped MMIO with pte == 0, as the IOMMU can't handle page faults.
+	 * That maps the whole address space which can be large, but that doesn't
+	 * use a lot of memory as it will be mostly large block (1 GB with 4kb pages)
+	 */

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +10 -0
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

Quoted 94 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +57 -3
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */
+	do {
+		prot = IOMMU_READ | IOMMU_WRITE;
+		region_end = find_mem_range_from(start, &is_memory);
+		region_end = min(end, region_end);
+		if (!is_memory)
+			prot |= IOMMU_MMIO;
+
+		ret = kvm_iommu_ops->host_stage2_idmap(start, region_end, prot);
+		if (ret)
+			return ret;
+
+		start = region_end;
+	} while (start < end);
+
+	return 0;
+}
+
+static int kvm_iommu_snapshot_host_stage2(void)
+{
+	int ret;
+	struct kvm_pgtable_walker walker = {
+		.cb	= __snapshot_host_stage2,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+	struct kvm_pgtable *pgt = &host_mmu.pgt;
+
+	hyp_spin_lock(&host_mmu.lock);
+	ret = kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker);
+	/* Start receiving calls to host_stage2_idmap. */
+	kvm_idmap_initialized = !ret;
+	hyp_spin_unlock(&host_mmu.lock);
+
+	return ret;
+}
 
 int kvm_iommu_init(void)
 {
-	/* Keep DMA isolation optional. */
-	if (!kvm_iommu_ops || !kvm_iommu_ops->init)
+	int ret;
+
+	if (!kvm_iommu_ops || !kvm_iommu_ops->init ||
+	    !kvm_iommu_ops->host_stage2_idmap)
+		return 0;
+
+	ret = kvm_iommu_ops->init();
+	if (ret)
+		return ret;
+
+	return kvm_iommu_snapshot_host_stage2();
+}
+
+int kvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
+				enum kvm_pgtable_prot prot)
+{
+	hyp_assert_lock_held(&host_mmu.lock);
+
+	if (!kvm_idmap_initialized)
 		return 0;
 
-	return kvm_iommu_ops->init();
+	return kvm_iommu_ops->host_stage2_idmap(start, end, pkvm_to_iommu_prot(prot));
 }
arch/arm64/kvm/hyp/nvhe/mem_protect.c +9 -0
index 2fb20a63a417..b54cb72ed88c 100644
@@ -15,6 +15,7 @@
 #include <hyp/fault.h>
 
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 587

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)

I would really split this. I know that this is convinient, but as the function says, it only update the page state so it shouldn't hide an update to the IOMMU.

Beside, we have examples already in Android where we want to update the page-state but not the IOMMU, so it doesn't feel future-proof...

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8 -0
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 608

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;

... and that would avoid that sort of things here. The caller decides if the IOMMU is updated or not.

And as the patch says, we "shadow" the host stage2. So probably modifying host_stage2_idmap and host_stage2_set_owner_metdata() sounds really a better approach.

Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8 -0
+
+	/*
+	 * Only update the IOMMU from here, as MMIO can't transition after
+	 * de-privilege, that will need to change when device assignment
+	 * is supported.
+	 * And WARN on failure as we can't unroll at this point.
+	 */
+	WARN_ON(kvm_iommu_host_stage2_idmap(addr, addr + size, prot));
 
 }
 

#define KVM_HOST_DONATION_PTE_OWNER_MASK GENMASK(3, 1)

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#50 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +3 -0
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */

[...]

Quoted 18 lines
Quoted 18 lines

#include <nvhe/mm.h>

arch/arm64/kvm/hyp/nvhe/mem_protect.c +8 -0
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 587

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)

I would really split this. I know that this is convinient, but as the function says, it only update the page state so it shouldn't hide an update to the IOMMU.

I mention a couple of alternatives in the commit message, I tried to implement it differently which was harder to reason about as the calls was scattered everywhere and any small refactor will possibly break it.

Did you have a split in my mind? I open to rework it.

Quoted 20 lines

Beside, we have examples already in Android where we want to update the page-state but not the IOMMU, so it doesn't feel future-proof...

Quoted 16 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +13 -0
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 608

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;

... and that would avoid that sort of things here. The caller decides if the IOMMU is updated or not.

Typically, the IOMMU is updated if the CPU is.

Quoted 4 lines

And as the patch says, we "shadow" the host stage2. So probably modifying host_stage2_idmap and host_stage2_set_owner_metdata() sounds really a better approach.

Initially, before the pKVM merge upstream I was doing something similar as that only required one hook [1]. However, after rebasing I found that would be too complicated and I have to add many more (as mentioned in the commit message). But I can re-visit this approach in v7.

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
Mostafa

#51 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 37 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

Quoted 4 lines

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

Quoted 30 lines
Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +3 -0
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */

[...]

Quoted 18 lines
Quoted 18 lines

#include <nvhe/mm.h>

arch/arm64/kvm/hyp/nvhe/mem_protect.c +8 -0
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 587

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)

I would really split this. I know that this is convinient, but as the function says, it only update the page state so it shouldn't hide an update to the IOMMU.

Quoted 5 lines

I mention a couple of alternatives in the commit message, I tried to implement it differently which was harder to reason about as the calls was scattered everywhere and any small refactor will possibly break it.

Did you have a split in my mind? I open to rework it.

Yes, I meant the option #2.

Quoted 33 lines
Quoted 21 lines

Beside, we have examples already in Android where we want to update the page-state but not the IOMMU, so it doesn't feel future-proof...

Quoted 16 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +13 -0
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 608

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;

... and that would avoid that sort of things here. The caller decides if the IOMMU is updated or not.

Typically, the IOMMU is updated if the CPU is.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And as the patch says, we "shadow" the host stage2. So probably modifying host_stage2_idmap and host_stage2_set_owner_metdata() sounds really a better approach.

Initially, before the pKVM merge upstream I was doing something similar as that only required one hook [1]. However, after rebasing I found that would be too complicated and I have to add many more (as mentioned in the commit message). But I can re-visit this approach in v7.

What bit would be more complicated? As you've wrote in the commit message, in option#2 you only need two calls really:

* host_stage2_set_owner_locked()
* host_stage2_set_owner_metadata_locked().

This fits better the narative for the "shadow" page-table: when we map the host stage-2, we map the iommu and when we unmap the host stage-2, we unmap the iommu.

Quoted 6 lines

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
Mostafa

#52 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 43 lines · Vincent Donnefort
Quoted 37 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

Quoted 4 lines

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

What I have in mind is the completely untested snippet:

index 75b00c323310..64cd175ad6c0 100644

arch/arm64/kvm/hyp/nvhe/setup.c +7 -1
@@ -226,10 +226,11 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
        case PKVM_PAGE_SHARED_OWNED:
                set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
                set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
+               kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
                break;
        case PKVM_PAGE_SHARED_BORROWED:
                set_hyp_state(page, PKVM_PAGE_SHARED_BORROWED);
-               set_host_state(page, PKVM_PAGE_SHARED_OWNED);
+               kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
                break;
        default:
                return -EINVAL;
@@ -258,15 +259,20 @@ static int fix_host_ownership(void)
                .cb     = fix_host_ownership_walker,
                .flags  = KVM_PGTABLE_WALK_LEAF,
        };
+       u64 mmio = 0;
        int i, ret;
 
        for (i = 0; i < hyp_memblock_nr; i++) {
                struct memblock_region *reg = &hyp_memory[i];
                u64 start = (u64)hyp_phys_to_virt(reg->base);
 
+               kvm_iommu_host_stage2_idmap(mmio, start, IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO);
+
                ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
                if (ret)
                        return ret;
+
+               mmio = start + reg->size;
        }
 

I believe this does the same as kvm_iommu_snapshot_host_stage2(). WDYS?

Quoted 97 lines
Quoted 31 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +3 -0
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */

[...]

Quoted 18 lines
Quoted 18 lines

#include <nvhe/mm.h>

(quoted diff) +8 -0
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
Quoted 4 lines

Review int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 587

Quoted 1 line
(quoted diff) +0 -0
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)

I would really split this. I know that this is convinient, but as the function says, it only update the page state so it shouldn't hide an update to the IOMMU.

Quoted 5 lines

I mention a couple of alternatives in the commit message, I tried to implement it differently which was harder to reason about as the calls was scattered everywhere and any small refactor will possibly break it.

Did you have a split in my mind? I open to rework it.

Yes, I meant the option #2.

Quoted 33 lines
Quoted 21 lines

Beside, we have examples already in Android where we want to update the page-state but not the IOMMU, so it doesn't feel future-proof...

Quoted 16 lines
(quoted diff) +13 -0
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
Quoted 8 lines

Review int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 608

Quoted 5 lines
(quoted diff) +5 -0
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;

... and that would avoid that sort of things here. The caller decides if the IOMMU is updated or not.

Typically, the IOMMU is updated if the CPU is.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And as the patch says, we "shadow" the host stage2. So probably modifying host_stage2_idmap and host_stage2_set_owner_metdata() sounds really a better approach.

Initially, before the pKVM merge upstream I was doing something similar as that only required one hook [1]. However, after rebasing I found that would be too complicated and I have to add many more (as mentioned in the commit message). But I can re-visit this approach in v7.

What bit would be more complicated? As you've wrote in the commit message, in option#2 you only need two calls really:

* host_stage2_set_owner_locked()
* host_stage2_set_owner_metadata_locked().

This fits better the narative for the "shadow" page-table: when we map the host stage-2, we map the iommu and when we unmap the host stage-2, we unmap the iommu.

Quoted 6 lines

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
Mostafa

#54 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Vincent Donnefort
Quoted 43 lines · Vincent Donnefort
Quoted 37 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

Quoted 4 lines

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

fix_host_ownership() only walks the hypervisor page table, and fixes the hypervisor pages in the host. That means that the IOMMU page table has to be fully populated first, then we unmap the donated pages. That seems more complicated and it feels that decoupling the IOMMU logic outside of this would be better.

Also, I rely on the IOMMU init to be done at the end of setup, so we do not have to clean the IOMMU init if any other part of KVM fails.

Quoted 84 lines
Quoted 31 lines
Quoted 5 lines
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +3 -0
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */

[...]

Quoted 18 lines
Quoted 18 lines

#include <nvhe/mm.h>

arch/arm64/kvm/hyp/nvhe/mem_protect.c +8 -0
@@ -481,6 +482,14 @@ static int check_range_allowed_memory(u64 start, u64 end)
 	return 0;
 }
 
+u64 find_mem_range_from(u64 start, bool *is_memory)
+{
+	struct kvm_mem_range r;
+
+	*is_memory = !!find_mem_range(start, &r);
+	return r.end;
+}
+
 static bool range_is_memory(u64 start, u64 end)
 {
 	struct kvm_mem_range r;
@@ -577,8 +586,34 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
 
Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 587

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state)

I would really split this. I know that this is convinient, but as the function says, it only update the page state so it shouldn't hide an update to the IOMMU.

Quoted 5 lines

I mention a couple of alternatives in the commit message, I tried to implement it differently which was harder to reason about as the calls was scattered everywhere and any small refactor will possibly break it.

Did you have a split in my mind? I open to rework it.

Yes, I meant the option #2.

Quoted 33 lines
Quoted 21 lines

Beside, we have examples already in Android where we want to update the page-state but not the IOMMU, so it doesn't feel future-proof...

Quoted 16 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +13 -0
 {
+	enum pkvm_page_state old = get_host_state(hyp_phys_to_page(addr));
+	enum kvm_pgtable_prot prot = 0;
+
 	for_each_hyp_page(page, addr, size)
 		set_host_state(page, state);
+
+	/*
+	 * Any transition to PKVM_NOPAGE, unmaps the page from the host
+	 * Any transition to PKVM_PAGE_SHARED_BORROWED, maps the page in the host
+	 * Any transition to PKVM_PAGE_SHARED_OWNED is ignored as page is already mapped.
+	 * Transitions to PKVM_PAGE_OWNED from anything but PKVM_NOPAGE are ignored.
+	 * Transitions to PKVM_PAGE_OWNED from PKVM_NOPAGE will map the page.
+	 */
+	if ((state == PKVM_PAGE_SHARED_OWNED) ||
+		((state == PKVM_PAGE_OWNED) && (old != PKVM_NOPAGE)))
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 608

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
+		return;
+
+	if ((state == PKVM_PAGE_SHARED_BORROWED) ||
+		(state == PKVM_PAGE_OWNED))
+		prot = PKVM_HOST_MEM_PROT;

... and that would avoid that sort of things here. The caller decides if the IOMMU is updated or not.

Typically, the IOMMU is updated if the CPU is.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And as the patch says, we "shadow" the host stage2. So probably modifying host_stage2_idmap and host_stage2_set_owner_metdata() sounds really a better approach.

Initially, before the pKVM merge upstream I was doing something similar as that only required one hook [1]. However, after rebasing I found that would be too complicated and I have to add many more (as mentioned in the commit message). But I can re-visit this approach in v7.

What bit would be more complicated? As you've wrote in the commit message, in option#2 you only need two calls really:

Quoted 6 lines

* host_stage2_set_owner_locked()
* host_stage2_set_owner_metadata_locked().

This fits better the narative for the "shadow" page-table: when we map the host stage-2, we map the iommu and when we unmap the host stage-2, we unmap the iommu.

I see, looking again it does not seem as complicated as I was imagining, I will try that for v7.

Thanks,
Mostafa

Quoted 7 lines
Quoted 7 lines

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
Mostafa

#53 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Vincent Donnefort
Quoted 88 lines · Vincent Donnefort
Quoted 43 lines · Vincent Donnefort
Quoted 37 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

Quoted 4 lines

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

What I have in mind is the completely untested snippet:

index 75b00c323310..64cd175ad6c0 100644

arch/arm64/kvm/hyp/nvhe/setup.c +7 -1
@@ -226,10 +226,11 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
        case PKVM_PAGE_SHARED_OWNED:
                set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
                set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
+               kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
                break;
        case PKVM_PAGE_SHARED_BORROWED:
                set_hyp_state(page, PKVM_PAGE_SHARED_BORROWED);
-               set_host_state(page, PKVM_PAGE_SHARED_OWNED);
+               kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
                break;
        default:
                return -EINVAL;
@@ -258,15 +259,20 @@ static int fix_host_ownership(void)
                .cb     = fix_host_ownership_walker,
                .flags  = KVM_PGTABLE_WALK_LEAF,
        };
+       u64 mmio = 0;
        int i, ret;
 
        for (i = 0; i < hyp_memblock_nr; i++) {
                struct memblock_region *reg = &hyp_memory[i];
                u64 start = (u64)hyp_phys_to_virt(reg->base);
 
+               kvm_iommu_host_stage2_idmap(mmio, start, IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO);
+
                ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
                if (ret)
                        return ret;
+
+               mmio = start + reg->size;
        }
 
Quoted 2 lines

I believe this does the same as kvm_iommu_snapshot_host_stage2(). WDYS?

I see. However, that does not init the memory owned by the host, neither unmaps hypervisor owned MMIO.

Besides, as mentioned in the reply, that mean that the IOMMU has to be init earlier which has it's own problems. So, I do not think that it is worth changing the current logic.

Thanks,
Mostafa

#55 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 44 lines · Mostafa Saleh
Quoted 43 lines · Vincent Donnefort
Quoted 41 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 8 lines
Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

fix_host_ownership() only walks the hypervisor page table, and fixes the hypervisor pages in the host.

Yeah, Looking closer I don't think my proposal simplify things so much in the end.

Quoted 4 lines

That means that the IOMMU page table has
to be fully populated first, then we unmap the donated pages. That seems more complicated and it feels that decoupling the IOMMU logic outside of this would be better.

Why more complicated? That's actually another solution I was thinking about, to just map everything as soon as possible and let host_stage2_idmap_locked() and host_stage2_set_owner_locked() unmap what is needed here. (and also in __pkvm_host_donate_hyp_mmio())

We wouldn't need any specific setup walker for the iommu at all.

Quoted 3 lines

Also, I rely on the IOMMU init to be done at the end of setup, so we do not have to clean the IOMMU init if any other part of KVM fails.

We could just leak those pages if it fails?

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +3 -0
+	}
+
+	/* In case of invalid PTE, we need to figure out which part of it is MMIO */

[...]

[...]

#56 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 62 lines · Vincent Donnefort
Quoted 44 lines · Mostafa Saleh
Quoted 43 lines · Vincent Donnefort
Quoted 41 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

arch/arm64/kvm/hyp/nvhe/iommu/iommu.c line 59

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 8 lines
Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

fix_host_ownership() only walks the hypervisor page table, and fixes the hypervisor pages in the host.

Yeah, Looking closer I don't think my proposal simplify things so much in the end.

Quoted 4 lines

That means that the IOMMU page table has
to be fully populated first, then we unmap the donated pages. That seems more complicated and it feels that decoupling the IOMMU logic outside of this would be better.

Quoted 6 lines

Why more complicated? That's actually another solution I was thinking about, to just map everything as soon as possible and let host_stage2_idmap_locked() and host_stage2_set_owner_locked() unmap what is needed here. (and also in __pkvm_host_donate_hyp_mmio())

We wouldn't need any specific setup walker for the iommu at all.

That would walk the table twice, first time with a massive map, then to unmap the pages that was just mapped, issuing TLB invalidations for all of those, also as the IOMMU page table code never frees tables that means we would immediately exahust the IOMMU pool.

Also, I believe that it is better to isolate the IOMMU page table shadowing logic from the core hypervisor page table setup, specially AFAICT, that was not what fix_host_ownership() was designed for, and seems the IOMMU will just piggyback on a existing walker.

Quoted 6 lines
Quoted 4 lines

Also, I rely on the IOMMU init to be done at the end of setup, so we do not have to clean the IOMMU init if any other part of KVM fails.

We could just leak those pages if it fails?

It is not just about pages, the hypervisor will also configure the SMMU registers. Ideally, we would need a remove() driver ops for such cases but I added the IOMMU init call at the end to avoid making this series any bigger.

Thanks,
Mostafa

#57 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 68 lines · Mostafa Saleh
Quoted 67 lines · Vincent Donnefort
Quoted 44 lines · Mostafa Saleh
Quoted 43 lines · Vincent Donnefort
Quoted 41 lines · Mostafa Saleh
Quoted 8 lines · Vincent Donnefort
Quoted 7 lines · Mostafa Saleh

Create a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation.

An initial snapshot is created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

[...]

Quoted 18 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+	 */
+	if (pte && !kvm_pte_valid(pte))
+		return 0;
+
+	if (kvm_pte_valid(pte)) {
+		prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
+		/* If the range is mapped in a single PTE, it must be the same type.*/
+		if (!addr_is_memory(start))
+			prot |= IOMMU_MMIO;
+
+		return kvm_iommu_ops->host_stage2_idmap(start, end, prot);

Do we really need to do that when is_memory()?

fix_host_ownership_walker() by calling host_stage2_idmap_locked() and host_stage2_set_owner_locked() should already handle the memory region. That would also get rid of kvm_idmap_initialized.

Quoted 6 lines

So this one here could only take care of the MMIO?

Overall we would have a common point of synchro which is fix_host_ownership_walker() after which the host ownership is ready for both CPU stage-2 and the IOMMU?

I am not sure I understand, this is another empty page table, so we have to walk all of the host CPU stage-2 page table to shadow it in the IOMMU. if you are refering to the case where it handle zero ptes for memory, I can drop that but it will not change much in this logic.

Quoted 8 lines
Quoted 5 lines

In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be map/unmapped from the host stage-2. Can't we rely on that for the IOMMU page-table as well?

As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?

fix_host_ownership() only walks the hypervisor page table, and fixes the hypervisor pages in the host.

Yeah, Looking closer I don't think my proposal simplify things so much in the end.

Quoted 4 lines

That means that the IOMMU page table has
to be fully populated first, then we unmap the donated pages. That seems more complicated and it feels that decoupling the IOMMU logic outside of this would be better.

Why more complicated? That's actually another solution I was thinking about, to just map everything as soon as possible and let host_stage2_idmap_locked() and host_stage2_set_owner_locked() unmap what is needed here. (and also in __pkvm_host_donate_hyp_mmio())

Quoted 6 lines
Quoted 1 line

We wouldn't need any specific setup walker for the iommu at all.

That would walk the table twice, first time with a massive map, then to unmap the pages that was just mapped, issuing TLB invalidations for all of those, also as the IOMMU page table code never frees tables that means we would immediately exahust the IOMMU pool.

Well the massive map is pretty quick we would mostly just allocate the PGD? And then it would be just for the subsequent unmaps to split the blocks. So nothing to be freed really and no risk to exhaust the pool. But if you put the calls to the host_stage2_* functions that's pretty transparent.

Just to make sure I am clear. Here's what it could look like:

in iommu.c:

int kvm_iommu_stage2_idmap_init(struct kvm_s2_mmu *host_mmu)

{
	u64 addr = 0;

	for (i = 0; i < hyp_memblock_nr; i++) { 
		kvm_iommu_stage2_idmap(addr, reg->start, IOMMU_MMIO);

		addr = reg->start + reg->size;
		kvm_iommu_stage2_idmap(reg->start, addr, IOMMU_READ | IOMMU_WRITE);
	}

	kvm_iommu_stage2_idmap(addr, kvm_phys_size(host_mmu), IOMMU_MMIO);
}

in mem_protect.c:

int kvm_host_prepare_stage2(void *pgt_pool_base)

{
	...

	return kvm_iommu_stage2_idmap_init(mmu);

	/* 
	 * From here, all the changes to the host stage-2 will be reported to
	 * the iommu driver
	 */
}

On the pro side: no custom walker and we really couple the iommu idmap to the host stage-2 map.

On the con side: As you've pointed out, we'd need indeed the iommu to be ready early enough.

If you want to stick to the snapshot function, could we have the walker to first walk the hyp_memblock? the .arg could just pass if we are walking a MMIO region? That would avoid that find_mem_range_from() and I believe would massively simplify the snapshot walker.

Quoted 21 lines

Also, I believe that it is better to isolate the IOMMU page table shadowing logic from the core hypervisor page table setup, specially AFAICT, that was not what fix_host_ownership() was designed for, and seems the IOMMU will just piggyback on a existing walker.

Quoted 6 lines
Quoted 4 lines

Also, I rely on the IOMMU init to be done at the end of setup, so we do not have to clean the IOMMU init if any other part of KVM fails.

We could just leak those pages if it fails?

It is not just about pages, the hypervisor will also configure the SMMU registers. Ideally, we would need a remove() driver ops for such cases but I added the IOMMU init call at the end to avoid making this series any bigger.

Thanks,
Mostafa

#58 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 11 lines · Vincent Donnefort
Quoted 5 lines · Mostafa Saleh

That would walk the table twice, first time with a massive map, then to unmap the pages that was just mapped, issuing TLB invalidations for all of those, also as the IOMMU page table code never frees tables that means we would immediately exahust the IOMMU pool.

Well the massive map is pretty quick we would mostly just allocate the PGD? And then it would be just for the subsequent unmaps to split the blocks. So nothing to be freed really and no risk to exhaust the pool. But if you put the calls to the host_stage2_* functions that's pretty transparent.

Not for the IOMMU, everything is mapped with a leaf mapping, because the pgtable code does not support splitting blocks, this is something I plan to add support for in a follow up series after this one is merged along side other optimizations.

Quoted 36 lines

Just to make sure I am clear. Here's what it could look like:

in iommu.c:

int kvm_iommu_stage2_idmap_init(struct kvm_s2_mmu *host_mmu)

{
	u64 addr = 0;

	for (i = 0; i < hyp_memblock_nr; i++) { 
		kvm_iommu_stage2_idmap(addr, reg->start, IOMMU_MMIO);

		addr = reg->start + reg->size;
		kvm_iommu_stage2_idmap(reg->start, addr, IOMMU_READ | IOMMU_WRITE);
	}

	kvm_iommu_stage2_idmap(addr, kvm_phys_size(host_mmu), IOMMU_MMIO);
}

in mem_protect.c:

int kvm_host_prepare_stage2(void *pgt_pool_base)

{
	...

	return kvm_iommu_stage2_idmap_init(mmu);

	/* 
	 * From here, all the changes to the host stage-2 will be reported to
	 * the iommu driver
	 */
}

On the pro side: no custom walker and we really couple the iommu idmap to the host stage-2 map.

Quoted 8 lines

On the con side: As you've pointed out, we'd need indeed the iommu to be ready early enough.

If you want to stick to the snapshot function, could we have the walker to first walk the hyp_memblock? the .arg could just pass if we are walking a MMIO region? That would avoid that find_mem_range_from() and I believe would massively simplify the snapshot walker.

I see, let me try that.

Unrelated to this, but while looking into option#2 I see that __pkvm_guest_share_host() does not call either of the host_stage2_set_owner* so that requires another explicit pkvm_iommu_host_stage2_idmap() call.

I still plan to use option#2 in v7.

Thanks,
Mostafa

#64 lore raw

Re: [PATCH v6 09/25] KVM: arm64: iommu: Add memory pool

In reply to Mostafa Saleh

[...]

Quoted 38 lines
Quoted 29 lines

nit: kvm_iommu_init uses nr_pages.

Quoted 27 lines

{

arch/arm64/kvm/iommu.c +17 -0
 	guard(mutex)(&kvm_iommu_reg_lock);
@@ -21,6 +22,36 @@ int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(kvm_iommu_ops))
 		return -EBUSY;
 
+	/* See kvm_iommu_pages() */
+	if (pool_pages > kvm_nvhe_sym(hyp_kvm_iommu_pages)) {
+		kvm_err("Not enough memory for the IOMMU pool, need 0x%x pages, check kvm-arm.hyp_iommu_pages",
+			pool_pages);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
 	return 0;
 }
+
+unsigned int kvm_iommu_pages(void)
+{
+	/*
+	 * This is used very early during setup_arch() before any initcalls
+	 * or any drivers are registered.
+	 * This value is set by a command line option.
+	 * Later, when the driver is registered, it will pass the number
+	 * pages needed for it's page tables, if it was less that what
+	 * the system has already allocated, the registration will fail.
Quoted 8 lines

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 47

Quoted 3 lines
arch/arm64/kvm/iommu.c +3 -0
+	 */
+	return kvm_nvhe_sym(hyp_kvm_iommu_pages);
+}

Could we actually estimate the memory needed, based on the allocation for the host stage-2 and an estimation of MMIO coverage? This would allow the whole thing to run without setting this value as a best effort and to make it optional, which is surely more user-friendly?

Quoted 3 lines

Not really, this abstracts the IOMMU, so it does not have to match the host stage-2, in addition that the SMMUv3 driver requires extra pages for other in-memory data-structure.

What I thought would be to take __hyp_pgtable_total_pages() and then add on top of that memory to cover the MMIO region. As of "here's memory we think your driver will need" and of course that can't take into account any "driver"-specific struct.

To be fair, that is a bit weird to have memory reservation so early for a "driver" but perhaps that is okay because the pKVM protection is only complete if we have that "driver"?

Quoted 21 lines

At the moment, this is set from the command line which is not really best effort as the driver later confirms how much memory it needs.

Quoted 12 lines

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 55

Quoted 9 lines
arch/arm64/kvm/iommu.c +8 -0
+
+/* Number of pages to reserve for iommu pool*/
+static int __init early_hyp_iommu_pages(char *arg)
+{
+	if (!arg)
+		return -EINVAL;
+
+	return kstrtouint(arg, 0, &kvm_nvhe_sym(hyp_kvm_iommu_pages));

How about using a memory size here with memparse()? This would be more transparent for the commandline which can work on different page-size systems.

Makes sense, will do.

Thanks,
Mostafa

#59 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh
Quoted 7 lines · Vincent Donnefort
Quoted 5 lines · Mostafa Saleh

That would walk the table twice, first time with a massive map, then to unmap the pages that was just mapped, issuing TLB invalidations for all of those, also as the IOMMU page table code never frees tables that means we would immediately exahust the IOMMU pool.

Quoted 6 lines
Quoted 4 lines

Well the massive map is pretty quick we would mostly just allocate the PGD? And then it would be just for the subsequent unmaps to split the blocks. So nothing to be freed really and no risk to exhaust the pool. But if you put the calls to the host_stage2_* functions that's pretty transparent.

Not for the IOMMU, everything is mapped with a leaf mapping, because

Ha Ok my bad, I didn't get that!

Quoted 49 lines

the pgtable code does not support splitting blocks, this is something I plan to add support for in a follow up series after this one is merged along side other optimizations.

Quoted 36 lines

Just to make sure I am clear. Here's what it could look like:

in iommu.c:

int kvm_iommu_stage2_idmap_init(struct kvm_s2_mmu *host_mmu)

{
	u64 addr = 0;

	for (i = 0; i < hyp_memblock_nr; i++) { 
		kvm_iommu_stage2_idmap(addr, reg->start, IOMMU_MMIO);

		addr = reg->start + reg->size;
		kvm_iommu_stage2_idmap(reg->start, addr, IOMMU_READ | IOMMU_WRITE);
	}

	kvm_iommu_stage2_idmap(addr, kvm_phys_size(host_mmu), IOMMU_MMIO);
}

in mem_protect.c:

int kvm_host_prepare_stage2(void *pgt_pool_base)

{
	...

	return kvm_iommu_stage2_idmap_init(mmu);

	/* 
	 * From here, all the changes to the host stage-2 will be reported to
	 * the iommu driver
	 */
}

On the pro side: no custom walker and we really couple the iommu idmap to the host stage-2 map.

Quoted 8 lines

On the con side: As you've pointed out, we'd need indeed the iommu to be ready early enough.

If you want to stick to the snapshot function, could we have the walker to first walk the hyp_memblock? the .arg could just pass if we are walking a MMIO region? That would avoid that find_mem_range_from() and I believe would massively simplify the snapshot walker.

Quoted 7 lines

I see, let me try that.

Unrelated to this, but while looking into option#2 I see that __pkvm_guest_share_host() does not call either of the host_stage2_set_owner* so that requires another explicit pkvm_iommu_host_stage2_idmap() call.

It does! it calls __host_set_page_state_range() which calls host_stage2_idmap_locked().

FTR, I don't really like __host_set_page_state_range(). And we removed it recently from our Android tree. Because there's just a special case in __host_set_page_state_range() to cover guest_share_host. So we can easily replace all the callers with __host_update_page_state() and and call the idmap explicitely in __pkvm_guest_share_host().

Quoted 6 lines

I still plan to use option#2 in v7.

Thanks,
Mostafa

#60 lore raw

Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table

Quoted 70 lines · Vincent Donnefort
Quoted 8 lines · Mostafa Saleh
Quoted 7 lines · Vincent Donnefort
Quoted 5 lines · Mostafa Saleh

That would walk the table twice, first time with a massive map, then to unmap the pages that was just mapped, issuing TLB invalidations for all of those, also as the IOMMU page table code never frees tables that means we would immediately exahust the IOMMU pool.

Quoted 6 lines
Quoted 4 lines

Well the massive map is pretty quick we would mostly just allocate the PGD? And then it would be just for the subsequent unmaps to split the blocks. So nothing to be freed really and no risk to exhaust the pool. But if you put the calls to the host_stage2_* functions that's pretty transparent.

Not for the IOMMU, everything is mapped with a leaf mapping, because

Ha Ok my bad, I didn't get that!

Quoted 52 lines

the pgtable code does not support splitting blocks, this is something I plan to add support for in a follow up series after this one is merged along side other optimizations.

Quoted 36 lines

Just to make sure I am clear. Here's what it could look like:

in iommu.c:

int kvm_iommu_stage2_idmap_init(struct kvm_s2_mmu *host_mmu)

{
	u64 addr = 0;

	for (i = 0; i < hyp_memblock_nr; i++) { 
		kvm_iommu_stage2_idmap(addr, reg->start, IOMMU_MMIO);

		addr = reg->start + reg->size;
		kvm_iommu_stage2_idmap(reg->start, addr, IOMMU_READ | IOMMU_WRITE);
	}

	kvm_iommu_stage2_idmap(addr, kvm_phys_size(host_mmu), IOMMU_MMIO);
}

in mem_protect.c:

int kvm_host_prepare_stage2(void *pgt_pool_base)

{
	...

	return kvm_iommu_stage2_idmap_init(mmu);

	/* 
	 * From here, all the changes to the host stage-2 will be reported to
	 * the iommu driver
	 */
}

On the pro side: no custom walker and we really couple the iommu idmap to the host stage-2 map.

Quoted 8 lines

On the con side: As you've pointed out, we'd need indeed the iommu to be ready early enough.

If you want to stick to the snapshot function, could we have the walker to first walk the hyp_memblock? the .arg could just pass if we are walking a MMIO region? That would avoid that find_mem_range_from() and I believe would massively simplify the snapshot walker.

I see, let me try that.

Quoted 7 lines
Quoted 4 lines

Unrelated to this, but while looking into option#2 I see that __pkvm_guest_share_host() does not call either of the host_stage2_set_owner* so that requires another explicit pkvm_iommu_host_stage2_idmap() call.

It does! it calls __host_set_page_state_range() which calls host_stage2_idmap_locked().

But host_stage2_idmap_locked() does not call any set_owner variant, so __pkvm_guest_share_host() would require an explicit pkvm_iommu_host_stage2_idmap() call.

Thanks,
Mostafa

Quoted 13 lines

FTR, I don't really like __host_set_page_state_range(). And we removed it recently from our Android tree. Because there's just a special case in __host_set_page_state_range() to cover guest_share_host. So we can easily replace all the callers with __host_update_page_state() and and call the idmap explicitely in __pkvm_guest_share_host().

Quoted 6 lines

I still plan to use option#2 in v7.

Thanks,
Mostafa

#65 lore raw

Re: [PATCH v6 09/25] KVM: arm64: iommu: Add memory pool

In reply to Vincent Donnefort
Quoted 40 lines · Vincent Donnefort

[...]

Quoted 38 lines
Quoted 37 lines

nit: kvm_iommu_init uses nr_pages.

Quoted 20 lines

{

arch/arm64/kvm/iommu.c +10 -0
 	guard(mutex)(&kvm_iommu_reg_lock);
@@ -21,6 +22,36 @@ int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(kvm_iommu_ops))
 		return -EBUSY;
 
+	/* See kvm_iommu_pages() */
+	if (pool_pages > kvm_nvhe_sym(hyp_kvm_iommu_pages)) {
+		kvm_err("Not enough memory for the IOMMU pool, need 0x%x pages, check kvm-arm.hyp_iommu_pages",
+			pool_pages);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(kvm_iommu_ops) = hyp_ops;
 	return 0;
 }
+
+unsigned int kvm_iommu_pages(void)
+{

arch/arm64/kvm/iommu.c int kvm_iommu_register_driver(struct kvm_iommu_ops *hyp_ops) · line 47

Quoted 10 lines
arch/arm64/kvm/iommu.c +10 -0
+	/*
+	 * This is used very early during setup_arch() before any initcalls
+	 * or any drivers are registered.
+	 * This value is set by a command line option.
+	 * Later, when the driver is registered, it will pass the number
+	 * pages needed for it's page tables, if it was less that what
+	 * the system has already allocated, the registration will fail.
+	 */
+	return kvm_nvhe_sym(hyp_kvm_iommu_pages);
+}

Could we actually estimate the memory needed, based on the allocation for the host stage-2 and an estimation of MMIO coverage? This would allow the whole thing to run without setting this value as a best effort and to make it optional, which is surely more user-friendly?

Quoted 8 lines
Quoted 3 lines

Not really, this abstracts the IOMMU, so it does not have to match the host stage-2, in addition that the SMMUv3 driver requires extra pages for other in-memory data-structure.

What I thought would be to take __hyp_pgtable_total_pages() and then add on top of that memory to cover the MMIO region. As of "here's memory we think your driver will need" and of course that can't take into account any "driver"-specific struct.

I wanted to isolate the concept of IOMMU and SMMUv3 in the core code, so we should not assume how large the page table would be (it does not have to match the CPU topology at all)

Quoted 4 lines

To be fair, that is a bit weird to have memory reservation so early for a "driver" but perhaps that is okay because the pKVM protection is only complete if we have that "driver"?

Yes, there has been multiple attempts in this series, we used to allocate this pool later which was worse in my opinion as it was not part of the pKVM carveout and still needed some hacks to make it work.

Thanks,
Mostafa