Skip to contentSkip to conversation

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

PATCH In review Mostafa Saleh 3 participants 78 messages Oct 15 – Nov 19

Subsystems (6)
Documentation KVM arm64 IOMMU +2 more subsystems
lore
Share & tools
lore
#0 lore raw

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

Changes from v8:
v8: https://lore.kernel.org/all/[email protected]/
- Rebase on iommu/next with the new TLB invalidation rework
- Rename files (Nicolin, Jason)
- Move more IDR parsing logic to common code (Nicolin)
- Move Kconfig and depend on OF (Nicolin)
- IDR0.COHACC must agree with firmware (Nicolin)
- Fix waiting with SEV (Sashiko)
- Drop extra heuristics about carveout size (Nicolin)
- Minor reworks and cleanups

===================
I ran Sashiko locally and it was helpful in discovering problems in the series. However, it still shows large number of critical and high severity issues, I went through them and I believe they are false positives, mainly because (in the order of frequently reported):
- It doesn't understand WARNs are fatal in the hypervisor.
- It doesn't understand that a malicious host can DoS the system and pKVM doesn't guarantee availability
- It doesn't understand the SMMUv3 spec and makes stuff up (eg. about CMD_SYC CS field it makes up an non-existent encoding or wrong semantics for the gbpa register)
- It seems to look at one patch at a time and not the whole series, and as the series is written in a way to be bisectable that confuses it.

Design:
=======
Assumptions:
------------
One of the important points, is that this doesn’t emulate the full SMMUv3 architecture, but only the parts used by Linux kernel. That’s why enablement of this (ARM_SMMU_V3_PKVM) depends on (ARM_SMMU_V3=y) so we are sure of the driver behaviour.

Any new change in the driver will likely trigger a WARN_ON ending up in panic, that will require to support also in the hypervisor.

Most notable assumptions:
- Changing of stream table format/size or l2 pointers is not allowed after initialization.
- leaf=0 CFGI is not allowed.
- CFGI_ALL with any value but 31 is not allowed.
- Some commands which are not used are not allowed.
- Values set in ARM_SMMU_CR1 are hardcoded and don't change.

Emulation logic mainly targets:

1) Command Queue
----------------
At boot time, the hypervisor will allocate a shadow command queue (doesn't need to match the host size) which then sets up in HW, then it will trap access to

i) ARM_SMMU_CMDQ_BASE
That can only be written when the cmdq is disabled. Then on enable, the hypervisor will put the host command queue in a shared state to avoid transition into the hypervisor or VMs. It will be unshared with the cmdq is disabled

ii) ARM_SMMU_CMDQ_PROD
Trigger emulation code, where the hypervisor will copy the commands between cons and prod, of the host queue and sanitise them (mostly WARNs if the host is malicious and issuing commands it shouldn't) then eagerly consume them, updating the host cons.

iii) ARM_SMMU_CMDQ_CONS
No much logic, just return the emulated cons + error bits.

2) Stream table
---------------
Similar to the command queue, the first level is allocated at boot with max possible size, then the hypervisor will trap access to:
i) ARM_SMMU_STRTAB_BASE/ARM_SMMU_STRTAB_BASE_CFG: Keep track of the stream table to put it in a shared state.

On CFGI_STE, the hypervisor will read the STE in scope from the host copy, shadow L2 pointers if needed and attach stage-2.

3) GBPA
-------
The hypervisor will set GBPA to abort at boot, then any read from the host will return ABORT and writes are ignored.
If the host tries to clear GBPA, it will look like GBPA is refusing to update and time out.

4) EVTQ and PRIDQ
No shadowing needed for those queues, but the hypervisor needs to keep track of them to put them in a shared state so they can’t be used by the host or the hypervisor.

Bisectibility:
==============
I wrote the patches where most of them are bisectable at run time (so we can run with a prefix of the series till MMIO emulation, cmdq emulation, STE or full nested) that was very helpful in debugging, and I kept it like this to make debugging easier.

Constraints:
============
1) Discovery:
-------------
Only device trees are supported at the moment.

2) Shadow page table
--------------------
Uses page granularity (leaf) for memory, that's because of the lack of split_block_unmap() logic.

Boot and Probe ordering:
=======================
The main SMMUv3 MUST be only bound/probed after KVM fully initialises so it can set up the MMIO emulation.

The KVM SMMUv3 driver is loaded early before KVM init so it can register itself, during that point it will probe all the SMMUs from the platform bus and bind them to the driver.

Then at a later init call it will create an auxiliary device per SMMU, that the main driver will probe. The main driver still relies on this device(parent) for all driver activity.

Future work
===========
1) Sharing page tables will be an interesting optimization, which was discussed at LPC [1]

2) There is currently ongoing work to enable RPM, that will possibly enable/disable the SMMU frequently, we might need some optimizations to avoid re-shadowing the CMDQ/STE unnecessarily.

3) Add support for non-coherent SMMUs.

4) Optimizations (as using block mappings for memory).

Patches overview
=================
The patches are split as follows:

Patches 01: Core hypervisor: Dealing with MMIO and timers. Patches 02-05: Refactoring of io-pgtable-arm and SMMUv3 driver. Patches 06-09: Hypervisor IOMMU core: pagetable management, dabts. Patches 10-25: KVM SMMUv3 code.

Tested on Lenovo IdeaCentre mini X and Qemu.

A development branch can be found at [2]

[1] https://lpc.events/event/20/contributions/2555/
[2] https://android-kvm.googlesource.com/linux/+/refs/heads/kvm-smmu-v9

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

Mostafa Saleh (24):
KVM: arm64: Donate MMIO to the hypervisor iommu/arm-smmu-v3: Move Queue and STE functions to header iommu/arm-smmu-v3: Move IDR parsing to common functions iommu/arm-smmu-v3: Move hitless machinery to common code iommu/arm-smmu-v3: Move RangeInval to common code
KVM: arm64: iommu: Introduce IOMMU driver infrastructure
KVM: arm64: iommu: Shadow host stage-2 page table
KVM: arm64: iommu: Add memory pool
KVM: arm64: iommu: Support DABT for IOMMU iommu/arm-smmu-v3-kvm: Add the kernel driver iommu/arm-smmu-v3-kvm: Probe SMMU HW iommu/arm-smmu-v3-kvm: Add MMIO emulation iommu/arm-smmu-v3-kvm: Shadow the command queue iommu/arm-smmu-v3-kvm: Add CMDQ functions iommu/arm-smmu-v3-kvm: Emulate CMDQ for host iommu/arm-smmu-v3-kvm: Shadow stream table iommu/arm-smmu-v3-kvm: Shadow STEs iommu/arm-smmu-v3-kvm: Share other queues iommu/arm-smmu-v3-kvm: Emulate GBPA iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs iommu/arm-smmu-v3-kvm: Enable nesting
KVM: arm64: Add documentation for pKVM DMA isolation

 .../admin-guide/kernel-parameters.txt         |    7 +
 Documentation/virt/kvm/arm/pkvm.rst           |   23 +-
 arch/arm64/include/asm/kvm_host.h             |    8 +
 arch/arm64/include/asm/kvm_pgtable.h          |    2 +
 arch/arm64/include/asm/kvm_pkvm.h             |   10 +
 arch/arm64/kvm/Makefile                       |    2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |   37 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |    9 +-
 arch/arm64/kvm/hyp/nvhe/iommu.c               |  157 ++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  143 +-
 arch/arm64/kvm/hyp/nvhe/setup.c               |   19 +
 arch/arm64/kvm/hyp/pgtable.c                  |    8 +-
 arch/arm64/kvm/iommu.c                        |   68 +
 arch/arm64/kvm/pkvm.c                         |    1 +
 drivers/iommu/arm/Kconfig                     |   12 +
 drivers/iommu/arm/arm-smmu-v3/Makefile        |    3 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  |  646 ++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |   54 +
 .../arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c   |  227 +++
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c  |    2 +-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  756 +--------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   77 +-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 1425 +++++++++++++++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |   89 +
 .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h |   88 +
 drivers/iommu/io-pgtable-arm.c                |   14 +-
 drivers/iommu/io-pgtable-arm.h                |    9 +
 28 files changed, 3190 insertions(+), 716 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu.c
 create mode 100644 arch/arm64/kvm/iommu.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h

base-commit: c233d59828627bfaab750ec5546e81970475f951

Signature · 1 line
2.56.0.176.g3575e65682-goog
#1 lore raw

PATCH v9 1/25 KVM: arm64: Donate MMIO to the hypervisor

Add __pkvm_host_donate_hyp_mmio() so IOMMU drivers in the hypervisor can take ownership of the IOMMU MMIO. The range is mapped in the hypervisor private range and annotated as owned by the hypervisor in the host stage-2, so host accesses fault and further donations of the same range are refused. MMIO has no struct hyp_page, so the annotation is the only state, and a dedicated function is simpler than teaching the memory donation path about MMIO. Donation is only allowed before the host is de-privileged, which is when the hypervisor IOMMU drivers initialise.

Make kvm_pgtable_stage2_unmap() leave invalid counted PTEs (annotations) in place. host_stage2_unmap_dev_all() unmaps all non-memory ranges to recycle page-table pages under memory pressure, and would otherwise drop the annotation and let the host fault the MMIO back in. No caller depends on the old behaviour: host_stage2_unmap_dev_all() only covers non-memory ranges, where no annotation existed until now, and the guest unmap paths check that the PTE is valid first. Returning an error for counted PTEs instead is not an option, as a non-zero visitor return stops the walk, which would abort recycling at the first hypervisor-owned MMIO page and inject an abort into the host for an unrelated address.

Add __pkvm_hyp_donate_host_mmio() for the IOMMU driver error path. It unmaps the range from the hypervisor and maps it back in the host stage-2. The private VA range cannot be reclaimed and is leaked, as with every other failed private mapping.

---
 arch/arm64/include/asm/kvm_pgtable.h          |  2 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 10 ++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 99 ++++++++++++++++++-
 arch/arm64/kvm/hyp/pgtable.c                  |  8 +-
 4 files changed, 112 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +2 -0
index 41a8687938eb..20b1adb8a058 100644
@@ -713,6 +713,8 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
  * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
  * FWB is not supported by the CPU.
  *
+ * Invalid counted PTEs (annotations) are left in place.
+ *
  * Return: 0 on success, negative error code on failure.
  */
 int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +10 -0
index ec85a9547120..c9c3e2dbbe58 100644
@@ -36,6 +36,16 @@ 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);
+/*
+ * Donate MMIO range to the hypervisor, it will be mapped in the hypervisor's
+ * private range and unmapped from the host stage-2.
+ */
+int __pkvm_host_donate_hyp_mmio(u64 pfn, u64 nr_pages, unsigned long *haddr);
+/*
+ * Undo __pkvm_host_donate_hyp_mmio(), typically on an error path. @haddr is
+ * the address returned by the donation.
+ */
+int __pkvm_hyp_donate_host_mmio(u64 pfn, u64 nr_pages, unsigned long haddr);
 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 +98 -1

111 lines not on this page · raw message

arch/arm64/kvm/hyp/pgtable.c +2 -6
index b74dd5ce1efd..58b74450c824 100644
@@ -1161,13 +1161,9 @@ 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);
-		}
+	/* Invalid PTEs are left in place, including annotations. */
+	if (!kvm_pte_valid(ctx->old))
 		return 0;
-	}
 
 	if (kvm_pte_table(ctx->old, ctx->level)) {
 		childp = kvm_pte_follow(ctx->old, mm_ops);
Signature · 1 line
2.56.0.176.g3575e65682-goog
#2 lore raw

PATCH v9 2/25 iommu/arm-smmu-v3: Move Queue and STE functions to header

In reply to Mostafa Saleh

The KVM SMMUv3 driver would re-use some of the CMDQ and STE code inside the hypervisor. Move those functions to the header file that would be shared between the host kernel and the hypervisor.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 62 --------------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 63 +++++++++++++++++++++
 2 files changed, 63 insertions(+), 62 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -62
index 34e916ea339f..101ece0e38d3 100644
@@ -140,33 +140,6 @@ static void parse_driver_options(struct arm_smmu_device *smmu)
 }
 
 /* Low-level queue manipulation functions */
-static bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
-{
-	u32 space, 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 space >= n;
-}
-
-static bool queue_full(struct arm_smmu_ll_queue *q)
-{
-	return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
-	       Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
-}
-
-static bool queue_empty(struct arm_smmu_ll_queue *q)
-{
-	return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
-	       Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
-}
-
 static bool queue_consumed(struct arm_smmu_ll_queue *q, u32 prod)
 {
 	return ((Q_WRP(q, q->cons) == Q_WRP(q, prod)) &&
@@ -185,12 +158,6 @@ static void queue_sync_cons_out(struct arm_smmu_queue *q)
 	writel_relaxed(q->llq.cons, q->cons_reg);
 }
 
-static void queue_inc_cons(struct arm_smmu_ll_queue *q)
-{
-	u32 cons = Q_POS(q, q->cons) + 1;
-	q->cons = Q_OVF(q->cons) | Q_POS(q, cons);
-}
-
 static void queue_sync_cons_ovf(struct arm_smmu_queue *q)
 {
 	struct arm_smmu_ll_queue *llq = &q->llq;
@@ -221,12 +188,6 @@ static int queue_sync_prod_in(struct arm_smmu_queue *q)
 	return ret;
 }
 
-static u32 queue_inc_prod_n(struct arm_smmu_ll_queue *q, int n)
-{
-	u32 prod = Q_POS(q, q->prod) + n;
-	return Q_OVF(q->prod) | Q_POS(q, prod);
-}
-
 static void queue_poll_init(struct arm_smmu_device *smmu,
 			    struct arm_smmu_queue_poll *qp)
 {
@@ -254,14 +215,6 @@ static int queue_poll(struct arm_smmu_queue_poll *qp)
 	return 0;
 }
 
-static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
-{
-	int i;
-
-	for (i = 0; i < n_dwords; ++i)
-		*dst++ = cpu_to_le64(*src++);
-}
-
 static void queue_read(u64 *dst, __le64 *src, size_t n_dwords)
 {
 	int i;
@@ -1817,21 +1770,6 @@ static void arm_smmu_free_cd_tables(struct arm_smmu_master *master)
 }
 
 /* Stream table manipulation functions */
-static void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst,
-					  dma_addr_t l2ptr_dma)
-{
-	u64 val = 0;
-
-	val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, STRTAB_SPLIT + 1);
-	val |= l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK;
-
-	/* Ensure the new L2 table is fully visible. */
-	dma_wmb();
-
-	/* The HW has 64 bit atomicity with stores to the L2 STE table */
-	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
-}
-
 struct arm_smmu_ste_writer {
 	struct arm_smmu_entry_writer writer;
 	u32 sid;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +63 -0

69 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
#3 lore raw

PATCH v9 3/25 iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh

The hypervisor SMMUv3 driver needs the same IDR0, IDR3, IDR5 and IIDR parsing as the kernel driver, plus the IDR1 checks it depends on (embedded implementation, ATTR_TYPES_OVR, StreamID size). Move them to arm-smmu-v3-common-lib.c, which will be built for both, and the IIDR implementer and product IDs to arm-smmu-v3.h.

The two drivers use different device structs, so the shared code is written against ARM_SMMU_OBJ, which each compilation unit defines. The kernel maps it to struct arm_smmu_device. Static asserts check that the fields the shared code uses exist with the expected types.

The validity checks stay with the parsing. dev_err() and dev_info() become arm_smmu_err() and arm_smmu_info(), defined per world next to ARM_SMMU_OBJ, as the hypervisor cannot print to the console. The disable_msipolling module parameter is passed as an argument so the hypervisor can opt out, and cpus_have_cap() becomes cpus_have_final_cap(), which the hypervisor can use.

No functional change intended.

---
 drivers/iommu/arm/arm-smmu-v3/Makefile        |   2 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 240 ++++++++++++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |  39 +++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 220 +---------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   6 +
 5 files changed, 297 insertions(+), 210 deletions(-)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h
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 +240 -0

240 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +39 -0
new file mode 100644
index 000000000000..926a84b11a82
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ARM_SMMU_V3_COMMON_LIB_H
+#define __ARM_SMMU_V3_COMMON_LIB_H
+
+#include <linux/build_bug.h>
+#include <linux/compiler_types.h>
+#include <linux/kernel.h>
+
+/*
+ * The IDR probe functions are shared by the kernel and the hypervisor drivers,
+ * which define ARM_SMMU_OBJ, arm_smmu_err() and arm_smmu_info() differently.
+ * Ensure the fields used by them are defined and have the correct types.
+ */
+#ifndef __KVM_NVHE_HYPERVISOR__
+#include <linux/device.h>
+
+#include "arm-smmu-v3.h"
+
+typedef struct arm_smmu_device ARM_SMMU_OBJ;
+
+#define arm_smmu_err(smmu, fmt, ...)	dev_err((smmu)->dev, fmt, ##__VA_ARGS__)
+#define arm_smmu_info(smmu, fmt, ...)	dev_info((smmu)->dev, fmt, ##__VA_ARGS__)
+#endif
+
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, features), u32));
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, options), u32));
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, oas), unsigned long));
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, pgsize_bitmap), unsigned long));
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, base), void __iomem *));
+static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, sid_bits), unsigned int));
+
+void arm_smmu_device_iidr_probe(ARM_SMMU_OBJ *smmu);
+int arm_smmu_device_idr0_probe(ARM_SMMU_OBJ *smmu, bool disable_msipolling,
+			       u32 *idr0);
+int arm_smmu_device_idr1_probe(ARM_SMMU_OBJ *smmu, u32 *idr1);
+void arm_smmu_device_idr3_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_device_idr5_probe(ARM_SMMU_OBJ *smmu);
+
+#endif /* __ARM_SMMU_V3_COMMON_LIB_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11 -209

272 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
index 8c8957831286..bc4c25421835 100644
@@ -87,6 +87,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
Signature · 1 line
2.56.0.176.g3575e65682-goog
#5 lore raw

PATCH v9 4/25 iommu/arm-smmu-v3: Move hitless machinery to common code

In reply to Mostafa Saleh

Move the hitless STE functions to the common file so they can be reused by the hypervisor.

No functional change.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 246 +++++++++++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |   6 +
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c  |   2 +-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 249 ------------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   6 +-
 5 files changed, 254 insertions(+), 255 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +246 -0
index 22467aaca191..cf9287cdbd19 100644
@@ -5,6 +5,7 @@
  */
 
 #include <asm/cpufeature.h>
+#include <kunit/visibility.h>
 
 #include "arm-smmu-v3.h"
 #include "arm-smmu-v3-common-lib.h"
@@ -238,3 +239,248 @@ u32 arm_smmu_device_idr5_probe(ARM_SMMU_OBJ *smmu)
 
 	return reg;
 }
+
+/*
+ * Based on the value of ent report which bits of the STE the HW will access. It
+ * would be nice if this was complete according to the spec, but minimally it
+ * has to capture the bits this driver uses.
+ */
+void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits)
+{
+	unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0]));
+
+	used_bits[0] = cpu_to_le64(STRTAB_STE_0_V);
+	if (!(ent[0] & cpu_to_le64(STRTAB_STE_0_V)))
+		return;
+
+	used_bits[0] |= cpu_to_le64(STRTAB_STE_0_CFG);
+
+	/* S1 translates */
+	if (cfg & BIT(0)) {
+		used_bits[0] |= cpu_to_le64(STRTAB_STE_0_S1FMT |
+					    STRTAB_STE_0_S1CTXPTR_MASK |
+					    STRTAB_STE_0_S1CDMAX);
+		used_bits[1] |=
+			cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR |
+				    STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH |
+				    STRTAB_STE_1_S1STALLD | STRTAB_STE_1_STRW |
+				    STRTAB_STE_1_EATS | STRTAB_STE_1_MEV |
+				    STRTAB_STE_1_INSTCFG);
+		used_bits[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID);
+
+		/*
+		 * See 13.5 Summary of attribute/permission configuration fields
+		 * for the SHCFG behavior.
+		 */
+		if (FIELD_GET(STRTAB_STE_1_S1DSS, le64_to_cpu(ent[1])) ==
+		    STRTAB_STE_1_S1DSS_BYPASS)
+			used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG);
+	}
+
+	/* S2 translates */
+	if (cfg & BIT(1)) {
+		used_bits[1] |=
+			cpu_to_le64(STRTAB_STE_1_S2FWB | STRTAB_STE_1_EATS |
+				    STRTAB_STE_1_SHCFG | STRTAB_STE_1_MEV |
+				    STRTAB_STE_1_INSTCFG);
+		used_bits[2] |=
+			cpu_to_le64(STRTAB_STE_2_S2VMID | STRTAB_STE_2_VTCR |
+				    STRTAB_STE_2_S2AA64 | STRTAB_STE_2_S2ENDI |
+				    STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2S |
+				    STRTAB_STE_2_S2R);
+		used_bits[3] |= cpu_to_le64(STRTAB_STE_3_S2TTB_MASK);
+	}
+
+	if (cfg == STRTAB_STE_0_CFG_BYPASS)
+		used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG);
+}
+EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_ste_used);
+
+void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
+				  __le64 *safe_bits)
+{
+	const u64 eats_s1chk =
+		FIELD_PREP(STRTAB_STE_1_EATS, STRTAB_STE_1_EATS_S1CHK);
+	const u64 eats_trans =
+		FIELD_PREP(STRTAB_STE_1_EATS, STRTAB_STE_1_EATS_TRANS);
+
+	/*
+	 * When an STE changes EATS_TRANS, the sequencing code in the attach
+	 * logic already will have the PCI cap for ATS disabled. Thus at this
+	 * moment we can expect that the device will not generate ATS queries
+	 * and so we don't care about the sequencing of EATS. The purpose of
+	 * EATS_TRANS is to protect the system from hostile untrusted devices
+	 * that issue ATS when the PCI config space is disabled. However, if
+	 * EATS_TRANS is being changed, then we must have already trusted the
+	 * device as the EATS_TRANS security block is being disabled.
+	 *
+	 *  Note: now the EATS_TRANS update is moved to the first entry_set().
+	 *  Changing S2S and EATS might transiently result in S2S=1 and EATS=1
+	 *  which is a bad STE (see "5.2 Stream Table Entry"). In such a case,
+	 *  we can't do a hitless update. Also, it should not be added to the
+	 *  safe bits with STRTAB_STE_1_EATS_S1CHK, because EATS=0b11 would be
+	 *  effectively an errant 0b00 configuration.
+	 */
+	if (!((cur[1] | target[1]) & cpu_to_le64(eats_s1chk)) &&
+	    !((cur[2] | target[2]) & cpu_to_le64(STRTAB_STE_2_S2S)))
+		safe_bits[1] |= cpu_to_le64(eats_trans);
+
+	/*
+	 * MEV does not meaningfully impact the operation of the HW, it only
+	 * changes how many fault events are generated, thus we can relax it
+	 * when computing the ordering. The spec notes the device can act like
+	 * MEV=1 anyhow:
+	 *
+	 *  Note: Software must expect, and be able to deal with, coalesced
+	 *  fault records even when MEV == 0.
+	 */
+	safe_bits[1] |= cpu_to_le64(STRTAB_STE_1_MEV);
+}
+EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_ste_update_safe);
+
+/*
+ * Figure out if we can do a hitless update of entry to become target. Returns a
+ * bit mask where 1 indicates that qword needs to be set disruptively.
+ * unused_update is an intermediate value of entry that has unused bits set to
+ * their new values.
+ */
+static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer,
+				    const __le64 *entry, const __le64 *target,
+				    __le64 *unused_update)
+{
+	__le64 target_used[NUM_ENTRY_QWORDS] = {};
+	__le64 cur_used[NUM_ENTRY_QWORDS] = {};
+	__le64 safe[NUM_ENTRY_QWORDS] = {};
+	u8 used_qword_diff = 0;
+	unsigned int i;
+
+	writer->ops->get_used(entry, cur_used);
+	writer->ops->get_used(target, target_used);
+	if (writer->ops->get_update_safe)
+		writer->ops->get_update_safe(entry, target, safe);
+
+	for (i = 0; i != NUM_ENTRY_QWORDS; i++) {
+		/*
+		 * Safe is only used for bits that are used by both entries,
+		 * otherwise it is sequenced according to the unused entry.
+		 */
+		safe[i] &= target_used[i] & cur_used[i];
+
+		/*
+		 * Check that masks are up to date, the make functions are not
+		 * allowed to set a bit to 1 if the used function doesn't say it
+		 * is used.
+		 */
+		WARN_ON_ONCE(target[i] & ~target_used[i]);
+
+		/* Bits can change because they are not currently being used */
+		cur_used[i] &= ~safe[i];
+		unused_update[i] = (entry[i] & cur_used[i]) |
+				   (target[i] & ~cur_used[i]);
+		/*
+		 * Each bit indicates that a used bit in a qword needs to be
+		 * changed after unused_update is applied.
+		 */
+		if ((unused_update[i] & target_used[i]) != target[i])
+			used_qword_diff |= 1 << i;
+	}
+	return used_qword_diff;
+}
+
+static void entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry,
+		      const __le64 *target, unsigned int start,
+		      unsigned int len)
+{
+	bool changed = false;
+	unsigned int i;
+
+	for (i = start; len != 0; len--, i++) {
+		if (entry[i] != target[i]) {
+			WRITE_ONCE(entry[i], target[i]);
+			changed = true;
+		}
+	}
+
+	if (changed)
+		writer->ops->sync(writer);
+}
+
+/*
+ * Update the STE/CD to the target configuration. The transition from the
+ * current entry to the target entry takes place over multiple steps that
+ * attempts to make the transition hitless if possible. This function takes care
+ * not to create a situation where the HW can perceive a corrupted entry. HW is
+ * only required to have a 64 bit atomicity with stores from the CPU, while
+ * entries are many 64 bit values big.
+ *
+ * The difference between the current value and the target value is analyzed to
+ * determine which of three updates are required - disruptive, hitless or no
+ * change.
+ *
+ * In the most general disruptive case we can make any update in three steps:
+ *  - Disrupting the entry (V=0)
+ *  - Fill now unused qwords, execpt qword 0 which contains V
+ *  - Make qword 0 have the final value and valid (V=1) with a single 64
+ *    bit store
+ *
+ * However this disrupts the HW while it is happening. There are several
+ * interesting cases where a STE/CD can be updated without disturbing the HW
+ * because only a small number of bits are changing (S1DSS, CONFIG, etc) or
+ * because the used bits don't intersect. We can detect this by calculating how
+ * many 64 bit values need update after adjusting the unused bits and skip the
+ * V=0 process. This relies on the IGNORED behavior described in the
+ * specification.
+ */
+void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry,
+			  const __le64 *target)
+{
+	__le64 unused_update[NUM_ENTRY_QWORDS];
+	u8 used_qword_diff;
+
+	/*
+	 * Many of the entry structures have pointers to other structures that
+	 * need to have their updates be visible before any writes of the entry
+	 * happen.
+	 */
+	dma_wmb();
+
+	used_qword_diff =
+		arm_smmu_entry_qword_diff(writer, entry, target, unused_update);
+	if (hweight8(used_qword_diff) == 1) {
+		/*
+		 * Only one qword needs its used bits to be changed. This is a
+		 * hitless update, update all bits the current STE/CD is
+		 * ignoring to their new values, then update a single "critical
+		 * qword" to change the STE/CD and finally 0 out any bits that
+		 * are now unused in the target configuration.
+		 */
+		unsigned int critical_qword_index = ffs(used_qword_diff) - 1;
+
+		/*
+		 * Skip writing unused bits in the critical qword since we'll be
+		 * writing it in the next step anyways. This can save a sync
+		 * when the only change is in that qword.
+		 */
+		unused_update[critical_qword_index] =
+			entry[critical_qword_index];
+		entry_set(writer, entry, unused_update, 0, NUM_ENTRY_QWORDS);
+		entry_set(writer, entry, target, critical_qword_index, 1);
+		entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS);
+	} else if (used_qword_diff) {
+		/*
+		 * At least two qwords need their inuse bits to be changed. This
+		 * requires a breaking update, zero the V bit, write all qwords
+		 * but 0, then set qword 0
+		 */
+		unused_update[0] = 0;
+		entry_set(writer, entry, unused_update, 0, 1);
+		entry_set(writer, entry, target, 1, NUM_ENTRY_QWORDS - 1);
+		entry_set(writer, entry, target, 0, 1);
+	} else {
+		/*
+		 * No inuse bit changed, though safe bits may have changed.
+		 */
+		entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS);
+	}
+}
+EXPORT_SYMBOL_IF_KUNIT(arm_smmu_write_entry);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h +6 -0
index 926a84b11a82..55ffa880b093 100644
@@ -36,4 +36,10 @@ int arm_smmu_device_idr1_probe(ARM_SMMU_OBJ *smmu, u32 *idr1);
 void arm_smmu_device_idr3_probe(ARM_SMMU_OBJ *smmu);
 u32 arm_smmu_device_idr5_probe(ARM_SMMU_OBJ *smmu);
 
+void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits);
+void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
+				  __le64 *safe_bits);
+void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
+			  const __le64 *target);
+
 #endif /* __ARM_SMMU_V3_COMMON_LIB_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1 -1
index 5a4e45e15125..d9df9adc14ea 100644
@@ -6,6 +6,7 @@
 #include <linux/io-pgtable.h>
 
 #include "arm-smmu-v3.h"
+#include "arm-smmu-v3-common-lib.h"
 
 struct arm_smmu_test_writer {
 	struct arm_smmu_entry_writer writer;
@@ -18,7 +19,6 @@ struct arm_smmu_test_writer {
 	unsigned int num_syncs;
 };
 
-#define NUM_ENTRY_QWORDS 8
 #define NUM_EXPECTED_SYNCS(x) x
 
 static struct arm_smmu_ste bypass_ste;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -249

261 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -5
index bc4c25421835..d07fca3d1cf5 100644
@@ -226,6 +226,7 @@ struct arm_vsmmu;
 #define STRTAB_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 6)
 
 #define STRTAB_STE_DWORDS		8
+#define NUM_ENTRY_QWORDS		8
 
 struct arm_smmu_ste {
 	__le64 data[STRTAB_STE_DWORDS];
@@ -1140,11 +1141,6 @@ void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
 				 bool ats_enabled);
 
 #if IS_ENABLED(CONFIG_KUNIT)
-void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits);
-void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
-				  __le64 *safe_bits);
-void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
-			  const __le64 *target);
 void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits);
 void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
 			      struct arm_smmu_ste *target);
Signature · 1 line
2.56.0.176.g3575e65682-goog
#6 lore raw

PATCH v9 5/25 iommu/arm-smmu-v3: Move RangeInval to common code

In reply to Mostafa Saleh

Move arm_smmu_tlbi_calc_range() to the common code shared with the hypervisor as it will use it later to populate TLBIs for the SMMUv3 stage-2 page table.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 160 ++++++++++++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |   2 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 160 ------------------
 3 files changed, 162 insertions(+), 160 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +160 -0
index cf9287cdbd19..8c15ed04ccdb 100644
@@ -484,3 +484,163 @@ void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry,
 	}
 }
 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_write_entry);
+
+/*
+ * Check address alignment for TTL hint per SMMUv3 H.a Section 4.4.1. Address
+ * bits below the alignment must be zero, otherwise UNPREDICTABLE.
+ */
+static bool arm_smmu_ttl_addr_aligned(u64 address, unsigned int tg,
+				      unsigned int ttl)
+{
+	unsigned int pgsz_lg2 = arm_smmu_pt_level_to_lg2sz(tg, 3 - ttl);
+
+	return !(address & GENMASK_U64(pgsz_lg2 - 1, 0));
+}
+
+struct arm_smmu_range_inv {
+	u64 start_tg;
+	/* Normal integer, not encoded. 0 means 0.*/
+	u64 num;
+	unsigned int scale;
+};
+
+static unsigned int arm_smmu_range_inv_calc_scale(u64 num_tg)
+{
+	return fls64((num_tg - 1) / (CMDQ_TLBI_RANGE_NUM_MAX + 1));
+}
+
+static u64 arm_smmu_range_inv_calc_num(u64 num_tg, unsigned int scale)
+{
+	return DIV_ROUND_UP_ULL(num_tg, 1ULL << scale);
+}
+
+/*
+ * Initialize the smallest range invalidation covering num_tg and ending at
+ * last_tg.
+ */
+static struct arm_smmu_range_inv arm_smmu_range_inv_init_end(u64 last_tg,
+							     u64 num_tg)
+{
+	struct arm_smmu_range_inv range_inv = {};
+
+	if (!num_tg)
+		return range_inv;
+
+	range_inv.scale = arm_smmu_range_inv_calc_scale(num_tg);
+	range_inv.num = arm_smmu_range_inv_calc_num(num_tg, range_inv.scale);
+	range_inv.start_tg = last_tg - ((range_inv.num << range_inv.scale) - 1);
+	return range_inv;
+}
+
+static void
+arm_smmu_tlbi_add_range_cmd(struct arm_smmu_tlbi *tlbi,
+			    const struct arm_smmu_range_inv *range_inv, u8 ttl,
+			    u8 tg_enc)
+{
+	struct arm_smmu_cmd *cmd = &tlbi->range.cmds[tlbi->range.num_cmds++];
+	unsigned int tgsz_lg2 = tg_enc * 2 + 10;
+	u64 iova = range_inv->start_tg << tgsz_lg2;
+	unsigned int num = range_inv->num - 1;
+
+	/* 16K granule TTL=1 is reserved (Section 4.4.1) */
+	if (WARN_ON(tgsz_lg2 == 14 && ttl == 1))
+		ttl = 0;
+
+	/* Verify address alignment for the TTL hint */
+	if (ttl && !arm_smmu_ttl_addr_aligned(iova, tgsz_lg2, ttl))
+		ttl = 0;
+
+	/*
+	 * SMMUv3 H.a Section 4.4.1: TG!=0, NUM==0, SCALE==0, TTL==0 is Reserved
+	 * and causes CERROR_ILL. Single tg uses NUM=0, SCALE=0 with a TTL hint
+	 * to target only the exact leaf entry.
+	 *
+	 * For a single tg invalidation a 0 TTL can come from places like the
+	 * SVA path that don't have enough information to get a TTL, or as a
+	 * side of effect of the splitting.
+	 *
+	 * A single-TG invalidation cannot reach this point if it is part of a
+	 * CONT group, so it is safe to transform it into a single invalidation.
+	 * The ARM_SMMU_OPT_FULL_CONT_RANGE_INV errata does not apply.
+	 */
+	if (!num && !range_inv->scale && !ttl)
+		tg_enc = 0;
+
+	cmd->data[0] = FIELD_PREP(CMDQ_TLBI_0_NUM, num) |
+		       FIELD_PREP(CMDQ_TLBI_0_SCALE, range_inv->scale);
+	cmd->data[1] = FIELD_PREP(CMDQ_TLBI_1_LEAF, tlbi->leaf_only) |
+		       FIELD_PREP(CMDQ_TLBI_1_TTL, ttl) |
+		       FIELD_PREP(CMDQ_TLBI_1_TG, tg_enc) | iova;
+}
+
+/*
+ * Generate up to two range TLBI command payloads covering [iova, iova+size).
+ * Sets use_full_inv if the range is too large to represent.
+ *
+ * Normally the first range invalidation is the largest representable span which
+ * does not exceed the requested range. If necessary, the second range
+ * invalidation is the smallest representable range covering the remainder and
+ * is anchored at the end. Any excess coverage from the second range
+ * invalidation overlaps the first instead of exceeding the requested range.
+ *
+ * For SVA on an invs containing an SMMU with ARM_SMMU_OPT_FULL_CONT_RANGE_INV,
+ * produce only a single range invalidation and overinvalidate so any potential CONT is
+ * covered by one command.
+ */
+void arm_smmu_tlbi_calc_range(struct arm_smmu_tlbi *tlbi,
+			      bool single_range_inv)
+{
+	u8 tgsz_lg2 = tlbi->tgsz_lg2;
+	struct arm_smmu_range_inv first = { .start_tg = tlbi->iova >>
+							tgsz_lg2 };
+	u64 last_tg = (tlbi->iova + tlbi->size - 1) >> tgsz_lg2;
+	u64 num_tg = last_tg - first.start_tg + 1;
+	u8 tg_enc = (tgsz_lg2 - 10) / 2;
+	struct arm_smmu_range_inv trail;
+	u8 ttl = 0;
+
+	/*
+	 * 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 leave TTL=0.
+	 */
+	if (tlbi->leaf_only)
+		ttl = 4 - ((ilog2(tlbi->iopte_size) - 3) / (tgsz_lg2 - 3));
+
+	/*
+	 * The spec defines the invalidated range as:
+	 *   Range = ((NUM+1) * 2^SCALE) * Translation_Granule_Size
+	 * NUM is 5 bits, so (NUM+1) covers 1..32 granules. Find the smallest
+	 * SCALE at which a single command could cover num_tg.
+	 *
+	 * Unlike other IOMMUs the spec has no alignment requirement on the
+	 * address beyond alignment to tg (so long as TTL=0).
+	 */
+	first.scale = arm_smmu_range_inv_calc_scale(num_tg);
+	if (first.scale > 31) {
+		/* Range too large for a single command do full invalidation */
+		tlbi->range.use_full_inv = true;
+		return;
+	}
+
+	if (single_range_inv) {
+		/*
+		 * Produce a single invalidation by rounding up and disabling
+		 * the trailer.
+		 */
+		first.num = arm_smmu_range_inv_calc_num(num_tg, first.scale);
+		trail.num = 0;
+	} else {
+		/*
+		 * Produce two invalidations by rounding down and adding a
+		 * second trailing range invalidation anchored at the end.
+		 */
+		first.num = num_tg >> first.scale;
+		trail = arm_smmu_range_inv_init_end(
+			last_tg, num_tg - ((u64)first.num << first.scale));
+	}
+	arm_smmu_tlbi_add_range_cmd(tlbi, &first, ttl, tg_enc);
+
+	if (trail.num)
+		arm_smmu_tlbi_add_range_cmd(tlbi, &trail, ttl, tg_enc);
+}
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h +2 -0
index 55ffa880b093..9f6949a60fe8 100644
@@ -42,4 +42,6 @@ void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
 void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
 			  const __le64 *target);
 
+void arm_smmu_tlbi_calc_range(struct arm_smmu_tlbi *tlbi, bool single_range_inv);
+
 #endif /* __ARM_SMMU_V3_COMMON_LIB_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -160

166 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
#8 lore raw

PATCH v9 6/25 KVM: arm64: iommu: Introduce IOMMU driver infrastructure

In reply to Mostafa Saleh

To establish DMA isolation, pKVM needs an IOMMU driver that provides ops implemented at EL2.

Only one driver can be used. It is registered before KVM initialises with pkvm_iommu_register_driver().
Registration must come from an initcall between core_initcall() and fs_initcall(): earlier and the CPU capabilities are not finalised, later and kvm_arm_init() at device_initcall level has already consumed the ops.

The hypervisor calls the driver's init last in __pkvm_init_finalise(), so that no step that can fail runs after it, as drivers have no teardown. DMA isolation stays optional: without a registered driver pKVM initialises as before.

---
 arch/arm64/include/asm/kvm_host.h       |  7 +++++
 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.c         | 19 ++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c         | 10 +++++++
 arch/arm64/kvm/iommu.c                  | 35 +++++++++++++++++++++++++
 7 files changed, 87 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/iommu.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/iommu.c
 create mode 100644 arch/arm64/kvm/iommu.c
arch/arm64/include/asm/kvm_host.h +7 -0
index cd9b9d2462f9..0c7fb6fa24f5 100644
@@ -1717,4 +1717,11 @@ 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);
 
+struct pkvm_iommu_ops;
+extern struct pkvm_iommu_ops *kvm_nvhe_sym(pkvm_iommu_ops);
+
+#ifndef __KVM_NVHE_HYPERVISOR__
+int __init pkvm_iommu_register_driver(struct pkvm_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..df3d0cc5d4db
@@ -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 pkvm_iommu_ops {
+	int (*init)(void);
+};
+
+int pkvm_iommu_init(void);
+
+#endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index f57450ebcb49..7cb79db9bc47 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.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
 hyp-obj-y += ../../../kernel/smccc-call.o
arch/arm64/kvm/hyp/nvhe/iommu.c new file +19 -0
new file mode 100644
index 000000000000..f4572b2dba76
@@ -0,0 +1,19 @@
+// 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 pkvm_iommu_ops *pkvm_iommu_ops __ro_after_init;
+
+int pkvm_iommu_init(void)
+{
+	/* Keep DMA isolation optional. */
+	if (!pkvm_iommu_ops || !pkvm_iommu_ops->init)
+		return 0;
+
+	return pkvm_iommu_ops->init();
+}
arch/arm64/kvm/hyp/nvhe/setup.c +10 -0
index 45ac5f2ba4f7..620b05c2799a 100644
@@ -13,6 +13,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>
@@ -342,6 +343,15 @@ void __noreturn __pkvm_init_finalise(void)
 	if (ret)
 		goto out;
 
+	/*
+	 * IOMMU init is done at the end, as it sets up and initializes many
+	 * resources that would be leaked if the hypervisor fails after as there
+	 * is no remove_iommu_driver() at the moment.
+	 */
+	ret = pkvm_iommu_init();
+	if (ret)
+		goto out;
+
 	pkvm_hyp_vm_table_init(vm_table_base);
 
 	pkvm_ownership_selftest(selftest_base);
arch/arm64/kvm/iommu.c new file +35 -0
new file mode 100644
index 000000000000..ab3f37d61579
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Mostafa Saleh <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+
+#include <asm/kvm_mmu.h>
+
+static DEFINE_MUTEX(pkvm_iommu_reg_lock);
+
+/**
+ * pkvm_iommu_register_driver() - Register the hypervisor IOMMU driver
+ * @hyp_ops: Kernel address of the ops, which live in the hypervisor image,
+ *           i.e. &kvm_nvhe_sym(ops).
+ *
+ * Only one driver can be registered. Call from an initcall between
+ * core_initcall() and fs_initcall().
+ *
+ * Return: 0 on success.
+ */
+int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops)
+{
+	guard(mutex)(&pkvm_iommu_reg_lock);
+
+	if (!is_protected_kvm_enabled() || is_kvm_arm_initialised())
+		return -EPERM;
+
+	if (kvm_nvhe_sym(pkvm_iommu_ops))
+		return -EBUSY;
+
+	kvm_nvhe_sym(pkvm_iommu_ops) = kern_hyp_va(kvm_ksym_ref(hyp_ops));
+	return 0;
+}
Signature · 1 line
2.56.0.176.g3575e65682-goog
#9 lore raw

PATCH v9 7/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 taken after the driver init, then on every ownership change a callback is called for the IOMMU driver to update its page table. The callback runs under host_mmu.lock, prot 0 unmaps, and a range may already be mapped with the same prot.

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() and
     __pkvm_guest_unshare_host().
   - host_stage2_set_owner_locked() for PKVM_ID_HOST, which covers
     __pkvm_hyp_donate_host(), __pkvm_host_force_reclaim_page_guest()
     and __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, the page state is not enough there as the old state is needed as well.

Option 2 is implemented here.

IOMMUs can't handle faults, so the snapshot maps everything the host owns, including what it has not faulted in yet. Failures to update the shadow are fatal, as WARN_ON() at EL2 panics, so a driver must guarantee the update cannot fail. The SMMUv3 driver does so by mapping memory at page granularity, so ownership changes never split a block, and by sizing its page table pool accordingly.

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h |   9 ++
 arch/arm64/kvm/hyp/nvhe/iommu.c         | 123 +++++++++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c   |  27 +++---
 3 files changed, 143 insertions(+), 16 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +9 -0
index df3d0cc5d4db..6cbcc9d65b4b 100644
@@ -3,11 +3,20 @@
 #define __ARM64_KVM_NVHE_IOMMU_H__
 
 #include <asm/kvm_host.h>
+#include <asm/kvm_pgtable.h>
 
 struct pkvm_iommu_ops {
 	int (*init)(void);
+	/*
+	 * Mirror [start, end) of the host stage-2 with IOMMU_* @prot, where 0
+	 * unmaps. Called with host_mmu.lock held. A range may already be mapped
+	 * with the same @prot, which must succeed.
+	 */
+	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
 int pkvm_iommu_init(void);
 
+int pkvm_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/nvhe/iommu.c +119 -4

135 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -12
index 75d250ca809c..c14af6a4877c 100644
@@ -17,6 +17,7 @@
 
 #include <nvhe/arm-smccc.h>
 #include <nvhe/gfp.h>
+#include <nvhe/iommu.h>
 #include <nvhe/memory.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -611,16 +612,15 @@ static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size,
 	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
 			      addr, size, &host_s2_pool,
 			      KVM_HOST_INVALID_PTE_TYPE_DONATION, annotation);
-	if (!ret) {
-		/*
-		 * After stage2 maintenance has happened, but before the page
-		 * owner has changed.
-		 */
-		pkvm_sme_dvmsync_fw_call();
-		__host_update_page_state(addr, size, PKVM_NOPAGE);
-	}
-
-	return ret;
+	if (ret)
+		return ret;
+	/*
+	 * After stage2 maintenance has happened, but before the page
+	 * owner has changed.
+	 */
+	pkvm_sme_dvmsync_fw_call();
+	__host_update_page_state(addr, size, PKVM_NOPAGE);
+	return WARN_ON(pkvm_iommu_host_stage2_idmap(addr, addr + size, 0));
 }
 
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
@@ -633,8 +633,10 @@ int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
 			return -EPERM;
 
 		ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT);
-		if (!ret)
-			__host_update_page_state(addr, size, PKVM_PAGE_OWNED);
+		if (ret)
+			break;
+		__host_update_page_state(addr, size, PKVM_PAGE_OWNED);
+		ret = WARN_ON(pkvm_iommu_host_stage2_idmap(addr, addr + size, PKVM_HOST_MEM_PROT));
 		break;
 	case PKVM_ID_HYP:
 		ret = host_stage2_set_owner_metadata_locked(addr, size,
@@ -1049,6 +1051,7 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
 				       pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_SHARED_OWNED),
 				       &vcpu->vcpu.arch.pkvm_memcache, 0));
 	WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
+	WARN_ON(pkvm_iommu_host_stage2_idmap(phys, phys + PAGE_SIZE, PKVM_HOST_MEM_PROT));
 unlock:
 	guest_unlock_component(vm);
 	host_unlock_component();
Signature · 1 line
2.56.0.176.g3575e65682-goog
#10 lore raw

PATCH v9 8/25 KVM: arm64: iommu: Add memory pool

In reply to Mostafa Saleh

IOMMU drivers need memory for the shadow page table. As with the host stage-2 CPU page table, reserve it early from the pKVM carveout and add it to a hyp_pool that the IOMMU driver allocates from and frees to at run time. The pool is fixed for the lifetime of the hypervisor.

The carveout is reserved before any driver registers, so the pool size comes from the kernel command line, "kvm-arm.iommu_pgt_mem". When the driver registers it passes the number of pages it needs: if that is more than what was reserved, registration fails and pKVM runs without DMA isolation. Otherwise the driver gets the whole reserved pool, not only what it asked for.

---
 .../admin-guide/kernel-parameters.txt         |  7 ++++
 arch/arm64/include/asm/kvm_host.h             |  3 +-
 arch/arm64/include/asm/kvm_pkvm.h             | 10 ++++++
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |  8 ++++-
 arch/arm64/kvm/hyp/nvhe/iommu.c               | 19 +++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               | 11 +++++-
 arch/arm64/kvm/iommu.c                        | 35 ++++++++++++++++++-
 arch/arm64/kvm/pkvm.c                         |  1 +
 8 files changed, 89 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -0
index e75344f4e0cd..96b4151f31c1 100644
@@ -3243,6 +3243,13 @@ Kernel parameters
 			max_snp_asid == min_sev_asid-1, will effectively make
 			SEV-ES unusable.
 
+	kvm-arm.iommu_pgt_mem=nn[KMG]
+			[KVM,ARM,EARLY]
+			Memory allocated for the IOMMU pool from the KVM carveout
+			when running in protected mode (See kvm-arm.mode=). If
+			unset or smaller than the IOMMU driver requires, the driver
+			fails to register and pKVM runs without DMA isolation.
+
 	kvm-arm.mode=
 			[KVM,ARM,EARLY] Select one of KVM/arm64's modes of
 			operation.
arch/arm64/include/asm/kvm_host.h +2 -1
index 0c7fb6fa24f5..253b4bd1f421 100644
@@ -1721,7 +1721,8 @@ struct pkvm_iommu_ops;
 extern struct pkvm_iommu_ops *kvm_nvhe_sym(pkvm_iommu_ops);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
-int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops);
+int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops,
+				      unsigned int nr_pages);
 #endif
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/include/asm/kvm_pkvm.h +10 -0
index cad60569f061..6027c7280e07 100644
@@ -73,6 +73,7 @@ static inline bool kvm_pkvm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
 
 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
+extern unsigned int kvm_nvhe_sym(hyp_iommu_pages);
 
 static inline unsigned long
 hyp_vmemmap_memblock_size(struct memblock_region *reg, size_t vmemmap_entry_size)
@@ -185,6 +186,15 @@ static inline unsigned long hyp_ffa_proxy_pages(void)
 	return (2 * KVM_FFA_MBOX_NR_PAGES) + DIV_ROUND_UP(desc_max, PAGE_SIZE);
 }
 
+/*
+ * Set from the kernel command line, as the carveout is reserved before the
+ * IOMMU driver registers. Registration fails if the driver needs more.
+ */
+static inline unsigned int pkvm_iommu_pages(void)
+{
+	return kvm_nvhe_sym(hyp_iommu_pages);
+}
+
 static inline size_t pkvm_host_sve_state_size(void)
 {
 	if (!system_supports_sve())
arch/arm64/kvm/hyp/include/nvhe/iommu.h +7 -1
index 6cbcc9d65b4b..acd5058ef3ed 100644
@@ -15,8 +15,14 @@ struct pkvm_iommu_ops {
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
 };
 
-int pkvm_iommu_init(void);
+int pkvm_iommu_init(void *pool_base, unsigned int nr_pages);
 
 int pkvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 				 enum kvm_pgtable_prot prot);
+
+/* Allocate pages from the IOMMU carveout, returns zeroed memory. */
+void *pkvm_iommu_alloc_pages(u8 order);
+/* Free pages from pkvm_iommu_alloc_pages(). */
+void pkvm_iommu_free_pages(void *ptr);
+
 #endif /* __ARM64_KVM_NVHE_IOMMU_H__ */
arch/arm64/kvm/hyp/nvhe/iommu.c +18 -1
index 662b1ed2330f..36432bd46c78 100644
@@ -14,6 +14,8 @@
 /* Only one set of ops supported */
 struct pkvm_iommu_ops *pkvm_iommu_ops __ro_after_init;
 
+static struct hyp_pool iommu_pages_pool;
+
 /* Protected by host_mmu.lock */
 static bool pkvm_idmap_initialized;
 
@@ -106,7 +108,7 @@ static int pkvm_iommu_snapshot_host_stage2(void)
 	return ret;
 }
 
-int pkvm_iommu_init(void)
+int pkvm_iommu_init(void *pool_base, unsigned int nr_pages)
 {
 	int ret;
 
@@ -115,6 +117,11 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = pkvm_iommu_ops->init();
 	if (ret)
 		return ret;
@@ -132,3 +139,13 @@ int pkvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 
 	return pkvm_iommu_ops->host_stage2_idmap(start, end, pkvm_to_iommu_prot(prot));
 }
+
+void *pkvm_iommu_alloc_pages(u8 order)
+{
+	return hyp_alloc_pages(&iommu_pages_pool, order);
+}
+
+void pkvm_iommu_free_pages(void *ptr)
+{
+	hyp_put_page(&iommu_pages_pool, ptr);
+}
arch/arm64/kvm/hyp/nvhe/setup.c +10 -1
index 620b05c2799a..8259837fb6de 100644
@@ -22,6 +22,8 @@
 
 unsigned long hyp_nr_cpus;
 
+unsigned int hyp_iommu_pages __ro_after_init;
+
 #define hyp_percpu_size ((unsigned long)__per_cpu_end - \
 			 (unsigned long)__per_cpu_start)
 
@@ -33,6 +35,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)
 {
@@ -70,6 +73,12 @@ static int divide_memory_pool(void *virt, unsigned long size)
 	if (!ffa_proxy_pages)
 		return -ENOMEM;
 
+	if (hyp_iommu_pages) {
+		iommu_base = hyp_early_alloc_contig(hyp_iommu_pages);
+		if (!iommu_base)
+			return -ENOMEM;
+	}
+
 	return 0;
 }
 
@@ -348,7 +357,7 @@ void __noreturn __pkvm_init_finalise(void)
 	 * resources that would be leaked if the hypervisor fails after as there
 	 * is no remove_iommu_driver() at the moment.
 	 */
-	ret = pkvm_iommu_init();
+	ret = pkvm_iommu_init(iommu_base, hyp_iommu_pages);
 	if (ret)
 		goto out;
 
arch/arm64/kvm/iommu.c +34 -1
index ab3f37d61579..4e81878192a0 100644
@@ -7,6 +7,7 @@
 #include <linux/kvm_host.h>
 
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_pkvm.h>
 
 static DEFINE_MUTEX(pkvm_iommu_reg_lock);
 
@@ -14,13 +15,16 @@ static DEFINE_MUTEX(pkvm_iommu_reg_lock);
  * pkvm_iommu_register_driver() - Register the hypervisor IOMMU driver
  * @hyp_ops: Kernel address of the ops, which live in the hypervisor image,
  *           i.e. &kvm_nvhe_sym(ops).
+ * @nr_pages: Number of pages the driver needs from the IOMMU pool, see
+ *            kvm-arm.iommu_pgt_mem.
  *
  * Only one driver can be registered. Call from an initcall between
  * core_initcall() and fs_initcall().
  *
  * Return: 0 on success.
  */
-int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops)
+int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops,
+				      unsigned int nr_pages)
 {
 	guard(mutex)(&pkvm_iommu_reg_lock);
 
@@ -30,6 +34,35 @@ int __init pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops)
 	if (kvm_nvhe_sym(pkvm_iommu_ops))
 		return -EBUSY;
 
+	if (!nr_pages)
+		return -EINVAL;
+
+	if (nr_pages > pkvm_iommu_pages()) {
+		kvm_err("IOMMU pool needs kvm-arm.iommu_pgt_mem=%luK or more\n",
+			nr_pages * PAGE_SIZE / SZ_1K);
+		return -ENOMEM;
+	}
+
 	kvm_nvhe_sym(pkvm_iommu_ops) = kern_hyp_va(kvm_ksym_ref(hyp_ops));
 	return 0;
 }
+
+static int __init early_iommu_pgt_mem(char *arg)
+{
+	unsigned long long requested_size;
+
+	if (!arg)
+		return -EINVAL;
+
+	requested_size = memparse(arg, NULL);
+
+	/* hyp_pool_init() computes nr_pages << PAGE_SHIFT in an unsigned int. */
+	if (requested_size > (UINT_MAX & PAGE_MASK)) {
+		kvm_err("kvm-arm.iommu_pgt_mem is too large\n");
+		return -EINVAL;
+	}
+
+	kvm_nvhe_sym(hyp_iommu_pages) = DIV_ROUND_UP(requested_size, PAGE_SIZE);
+	return 0;
+}
+early_param("kvm-arm.iommu_pgt_mem", early_iommu_pgt_mem);
arch/arm64/kvm/pkvm.c +1 -0
index 8e4c6e4bec12..b6cf01e00f6b 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 += pkvm_iommu_pages();
 
 	/*
 	 * Try to allocate a PMD-aligned region to reduce TLB pressure once
Signature · 1 line
2.56.0.176.g3575e65682-goog
#13 lore raw

PATCH v9 9/25 KVM: arm64: iommu: Support DABT for IOMMU

In reply to Mostafa Saleh

The pKVM SMMUv3 driver emulates the SMMU registers for the host: the MMIO is donated to the hypervisor so that host accesses trap, and the hypervisor performs or rejects each access on the host's behalf.

Add a dabt_handler op and call it from the host data abort path for aborts with a valid instruction syndrome on non-memory addresses. If the driver emulates the access, skip the faulting instruction, otherwise take the normal path, where hyp-owned MMIO is denied.

Only aborts with a valid instruction syndrome can be emulated, so the host must access emulated registers with single-register loads and stores (readl/writel and friends).

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h |  9 +++++++++
 arch/arm64/kvm/hyp/nvhe/iommu.c         |  6 ++++++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c   | 17 +++++++++++++++++
 3 files changed, 32 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +9 -0
index acd5058ef3ed..bd6604f194b5 100644
@@ -13,6 +13,13 @@ struct pkvm_iommu_ops {
 	 * with the same @prot, which must succeed.
 	 */
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
+	/*
+	 * Emulate a host data abort on MMIO @addr
+	 * @esr has a valid instruction syndrome and @regs holds the host x0-x30,
+	 * SRT 31 is the zero register. Return true if the access was emulated,
+	 * which would skip the instruction.
+	 */
+	bool (*dabt_handler)(struct user_pt_regs *regs, u64 esr, u64 addr);
 };
 
 int pkvm_iommu_init(void *pool_base, unsigned int nr_pages);
@@ -25,4 +32,6 @@ void *pkvm_iommu_alloc_pages(u8 order);
 /* Free pages from pkvm_iommu_alloc_pages(). */
 void pkvm_iommu_free_pages(void *ptr);
 
+bool pkvm_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.c +6 -0
index 36432bd46c78..cdf6e87bb68e 100644
@@ -149,3 +149,9 @@ void pkvm_iommu_free_pages(void *ptr)
 {
 	hyp_put_page(&iommu_pages_pool, ptr);
 }
+
+bool pkvm_iommu_host_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)
+{
+	return pkvm_iommu_ops && pkvm_iommu_ops->dabt_handler &&
+	       pkvm_iommu_ops->dabt_handler(regs, esr, addr);
+}
arch/arm64/kvm/hyp/nvhe/mem_protect.c +17 -0
index c14af6a4877c..523bb6bd793b 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_pkvm.h>
 #include <asm/stage2_pgtable.h>
 
+#include <hyp/adjust_pc.h>
 #include <hyp/fault.h>
 
 #include <nvhe/arm-smccc.h>
@@ -778,6 +779,12 @@ static void host_inject_mem_abort(struct kvm_cpu_context *host_ctxt)
 	inject_host_exception(esr);
 }
 
+static bool is_dabt_with_isv(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;
@@ -800,6 +807,16 @@ 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;
 
+	/*
+	 * Let the IOMMU driver emulate accesses to its MMIO.
+	 */
+	if (is_dabt_with_isv(esr) && !addr_is_memory(addr) &&
+	    pkvm_iommu_host_dabt_handler(&host_ctxt->regs, esr,
+					 addr | FAR_TO_FIPA_OFFSET(fault.far_el2))) {
+		kvm_skip_host_instr();
+		return;
+	}
+
 	switch (host_stage2_idmap(addr)) {
 	case -EPERM:
 		host_inject_mem_abort(host_ctxt);
Signature · 1 line
2.56.0.176.g3575e65682-goog
#14 lore raw

PATCH v9 10/25 iommu/arm-smmu-v3-kvm: Add SMMUv3 driver

In reply to Mostafa Saleh

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

Add the skeleton of the Arm SMMUv3 driver at EL2, and build the shared IDR/STE code and lib/hweight.o into the hypervisor.

The kernel driver, added next, fills an array describing the SMMUv3s in the system and hands it to the hypervisor. At init the hypervisor takes ownership of the array and of the SMMU MMIO regions, so the host cannot change either after de-privilege.

---
 arch/arm64/kvm/hyp/nvhe/Makefile              |  5 +
 drivers/iommu/arm/Kconfig                     | 12 +++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |  7 ++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 96 +++++++++++++++++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    | 41 ++++++++
 5 files changed, 161 insertions(+)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
arch/arm64/kvm/hyp/nvhe/Makefile +5 -0
index 7cb79db9bc47..ee86e4b3436f 100644
@@ -33,6 +33,11 @@ hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o 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-hyp.o \
+	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o ../../../../../lib/hweight.o
+
 # Path to simple_ring_buffer.c
 CFLAGS_trace.nvhe.o += -I$(srctree)/kernel/trace/
 
drivers/iommu/arm/Kconfig +12 -0
index 5fac08b89dee..82fbc729563e 100644
@@ -128,6 +128,18 @@ config TEGRA241_CMDQV
 
 	  Say Y here if your system is NVIDIA Tegra241 (Grace) or it has the same
 	  CMDQ-V extension.
+
+config ARM_SMMU_V3_PKVM
+	bool "ARM SMMUv3 support for protected Virtual Machines"
+	depends on KVM && ARM_SMMU_V3=y && OF
+	help
+	  Enable SMMUv3 driver in the KVM hypervisor, to protect VMs against
+	  memory accesses from devices owned by the host.
+	  This works alongside ARM_SMMU_V3 where the kernel driver manages the
+	  SMMUv3 stage-1, and the hypervisor driver manages the stage-2
+	  transparently through trap and emulate.
+
+	  Say Y here if you intend to enable KVM in protected mode.
 endif
 
 config QCOM_IOMMU
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h +7 -0
index 9f6949a60fe8..74b6c05b8f8f 100644
@@ -20,6 +20,13 @@ typedef struct arm_smmu_device ARM_SMMU_OBJ;
 
 #define arm_smmu_err(smmu, fmt, ...)	dev_err((smmu)->dev, fmt, ##__VA_ARGS__)
 #define arm_smmu_info(smmu, fmt, ...)	dev_info((smmu)->dev, fmt, ##__VA_ARGS__)
+#else
+#include "pkvm/arm-smmu-v3-hyp.h"
+
+typedef struct hyp_arm_smmu_v3_device ARM_SMMU_OBJ;
+
+#define arm_smmu_err(smmu, fmt, ...)
+#define arm_smmu_info(smmu, fmt, ...)
 #endif
 
 static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, features), u32));
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c new file +96 -0

96 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
#16 lore raw

PATCH v9 11/25 iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh

When KVM runs in protected mode with CONFIG_ARM_SMMU_V3_PKVM, the hypervisor owns the SMMUv3 hardware and presents emulated SMMUs to the host through trap and emulate.

Add the host side of the driver. At subsys_initcall, before the hypervisor is initialised, it claims the "arm,smmu-v3" platform devices, records their MMIO regions in an array handed to the hypervisor, and registers the hypervisor ops with pKVM, requesting a page table pool the size of the host stage-2 one. After KVM initialises, it creates an auxiliary device for each SMMU. The SMMUv3 driver probes those through a small auxiliary driver that keeps using the platform device for the DMA API, IRQs and devres. The emulation complies with the architecture, so the rest of the driver is unchanged.

---
 drivers/iommu/arm/arm-smmu-v3/Makefile        |   1 +
 .../arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c   | 183 ++++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  65 +++++++
 3 files changed, 249 insertions(+)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c
drivers/iommu/arm/arm-smmu-v3/Makefile +1 -0
index c9ce392e6d31..b782d1fda5fd 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-pkvm-init.o
 
 obj-$(CONFIG_ARM_SMMU_V3_KUNIT_TEST) += arm-smmu-v3-test.o
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +183 -0

183 lines not on this page · raw message

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

77 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
#28 lore raw

PATCH v9 12/25 iommu/arm-smmu-v3-kvm: Probe SMMU HW

In reply to Mostafa Saleh

Probe the SMMU features from the IDR registers with the functions shared with the kernel driver. SubstreamID bits and queue sizes are not needed
here: CDs are not shadowed and the command queue is set up later.

The hypervisor requires two things the kernel driver does not:
- Coherent table walks, reported by both firmware and IDR0.COHACC, as it does no cache maintenance on its tables.
- Nested translation, as the host keeps stage-1 and the hypervisor adds stage-2.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 46 ++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +45 -1
index ba209951039f..2b4a0f961a66 100644
@@ -31,6 +31,47 @@ static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu)
 	smmu->base = NULL;
 }
 
+/*
+ * Mini-probe and validation for the hypervisor.
+ */
+static int smmu_probe(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 reg;
+	int ret;
+
+	/* Not supported yet. */
+	if (!(smmu->features & ARM_SMMU_FEAT_COHERENCY))
+		return -ENXIO;
+
+	ret = arm_smmu_device_idr0_probe(smmu, true, &reg);
+	if (ret)
+		return ret;
+
+	/*
+	 * Core code clears FEAT_ATS with !CONFIG_PCI_ATS, but the hypervisor
+	 * must know if it is actually supported for CR0 emulation.
+	 */
+	if (reg & IDR0_ATS)
+		smmu->features |= ARM_SMMU_FEAT_ATS;
+
+	/* Firmware says coherent, the hardware must agree. */
+	if (!(reg & IDR0_COHACC))
+		return -ENXIO;
+
+	ret = arm_smmu_device_idr1_probe(smmu, &reg);
+	if (ret)
+		return ret;
+
+	arm_smmu_device_idr3_probe(smmu);
+	arm_smmu_device_idr5_probe(smmu);
+	arm_smmu_device_iidr_probe(smmu);
+
+	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
+		return -ENXIO;
+
+	return 0;
+}
+
 static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 {
 	unsigned long haddr;
@@ -45,8 +86,11 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 		return ret;
 
 	smmu->base = (void __iomem *)haddr;
+	ret = smmu_probe(smmu);
+	if (ret)
+		smmu_deinit_device(smmu);
 
-	return 0;
+	return ret;
 }
 
 /* Called while the host is still trusted. */
Signature · 1 line
2.56.0.176.g3575e65682-goog
#30 lore raw

PATCH v9 13/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.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 169 ++++++++++++++++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  18 ++
 2 files changed, 187 insertions(+)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +169 -0
index 2b4a0f961a66..d95b459a796a 100644
@@ -9,6 +9,7 @@
 
 #include <nvhe/iommu.h>
 #include <nvhe/mem_protect.h>
+#include <nvhe/trap_handler.h>
 
 #include "arm-smmu-v3-hyp.h"
 #include "../arm-smmu-v3.h"
@@ -80,6 +81,9 @@ 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);
+	hyp_spin_lock_init(&smmu->hw_lock);
+
 	ret = __pkvm_host_donate_hyp_mmio(hyp_phys_to_pfn(smmu->mmio_addr),
 					  smmu->mmio_size >> PAGE_SHIFT, &haddr);
 	if (ret)
@@ -128,6 +132,170 @@ 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,
+		 * and hide ATS as it is not emulated or handled in TLB invalidation.
+		 */
+		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP | IDR0_ATS);
+		break;
+	/* Passthrough the register access for bisectability, 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;
+
+		if (is_write) {
+			/* Enforce ATS checks. */
+			if (smmu->features & ARM_SMMU_FEAT_ATS)
+				val |= CR0_ATSCHK;
+			mask = read_write;
+		} else {
+			mask = read_write & ~CR0_ATSCHK;
+		}
+		break;
+	case ARM_SMMU_CR0ACK:
+		if (len != sizeof(u32))
+			break;
+		mask = read_only & ~CR0_ATSCHK;
+		break;
+	case ARM_SMMU_CR1:
+		if (len != sizeof(u32))
+			break;
+		/* Don't mess with shareability/cacheability. */
+		if (is_write &&
+		    (val != (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))))
+			break;
+
+		mask = read_write;
+		break;
+	/* Allowed 32 bit registers. */
+	case ARM_SMMU_EVTQ_IRQ_CFG1:
+	case ARM_SMMU_EVTQ_IRQ_CFG2:
+	case ARM_SMMU_GERROR_IRQ_CFG1:
+	case ARM_SMMU_GERROR_IRQ_CFG2:
+	case ARM_SMMU_PRIQ_IRQ_CFG1:
+	case ARM_SMMU_PRIQ_IRQ_CFG2:
+		/* These are RES0 as MSI support is hidden. */
+		val = 0;
+		if (!is_write)
+			goto out_update_regs;
+		fallthrough;
+	case ARM_SMMU_EVTQ_PROD + SZ_64K:
+	case ARM_SMMU_EVTQ_CONS + SZ_64K:
+	case ARM_SMMU_PRIQ_PROD + SZ_64K:
+	case ARM_SMMU_PRIQ_CONS + SZ_64K:
+	case ARM_SMMU_GERRORN:
+	case ARM_SMMU_IRQ_CTRL:
+	case ARM_SMMU_CR2:
+		if (len != sizeof(u32))
+			break;
+		mask = read_write;
+		break;
+	/* Allowed 64 bit registers. */
+	case ARM_SMMU_EVTQ_IRQ_CFG0:
+	case ARM_SMMU_PRIQ_IRQ_CFG0:
+	case ARM_SMMU_GERROR_IRQ_CFG0:
+		/* These are RES0 as MSI support is hidden. */
+		val = 0;
+		if (!is_write)
+			goto out_update_regs;
+		fallthrough;
+	case ARM_SMMU_EVTQ_BASE:
+	case ARM_SMMU_PRIQ_BASE:
+		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_IRQ_CTRLACK:
+	case ARM_SMMU_GERROR:
+		if (len != sizeof(u32))
+			break;
+		mask = read_only;
+		break;
+	}
+
+	if (WARN_ON(!mask))
+		goto out_ret;
+
+	hyp_spin_lock(&smmu->hw_lock);
+	if (is_write) {
+		if (len == sizeof(u64))
+			writeq_relaxed(val & mask, smmu->base + off);
+		else
+			writel_relaxed(val & mask, smmu->base + off);
+
+		hyp_spin_unlock(&smmu->hw_lock);
+		return true;
+	}
+
+	if (len == sizeof(u64))
+		val = readq_relaxed(smmu->base + off) & mask;
+	else
+		val = readl_relaxed(smmu->base + off) & mask;
+	hyp_spin_unlock(&smmu->hw_lock);
+out_update_regs:
+	/*
+	 * Device might be read sensitive, 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;
@@ -137,4 +305,5 @@ static int smmu_host_stage2_idmap(phys_addr_t start, phys_addr_t end, int prot)
 struct pkvm_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-hyp.h +18 -0
index c2630397f57a..afc3ca791583 100644
@@ -4,6 +4,10 @@
 
 #include <asm/kvm_asm.h>
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#include <nvhe/spinlock.h>
+#endif
+
 /**
  * struct hyp_arm_smmu_v3_device - SMMUv3 instance owned by the hypervisor
  * @mmio_addr:		Physical address of the SMMU registers
@@ -14,6 +18,9 @@
  * @oas:		Output address size
  * @pgsize_bitmap:	Supported page sizes
  * @sid_bits:		Number of StreamID bits
+ * @lock:		Lock to protect SMMU emulation
+ * @hw_lock:		Lock to protect SMMU HW (e.g. CMDQ)
+ *			Order smmu.lock => host_mmu.lock => smmu.hw_lock
  *
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
@@ -27,8 +34,19 @@ 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;
+	hyp_spinlock_t		hw_lock;
+#else
+	u32			lock;
+	u32			hw_lock;
+#endif
 };
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+static_assert(sizeof(hyp_spinlock_t) == sizeof(u32));
+#endif
+
 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)
 
Signature · 1 line
2.56.0.176.g3575e65682-goog
#37 lore raw

PATCH v9 14/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.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c   |  25 ++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 127 +++++++++++++++++-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  11 +-
 3 files changed, 161 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c +25 -0
index 5c5f0ff455e3..d7f2e73add7f 100644
@@ -16,6 +16,8 @@
 #include "arm-smmu-v3.h"
 #include "pkvm/arm-smmu-v3-hyp.h"
 
+#define SMMU_KVM_CMDQ_ORDER		get_order(MAX(SZ_64K, PAGE_SIZE))
+
 static size_t kvm_arm_smmu_count;
 static struct hyp_arm_smmu_v3_device *kvm_arm_smmu_array;
 static size_t kvm_arm_smmu_cur;
@@ -23,6 +25,15 @@ static size_t kvm_arm_smmu_cur;
 static void __init 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);
@@ -67,6 +78,7 @@ static int __init smmuv3_nesting_probe(struct platform_device *pdev)
 	struct hyp_arm_smmu_v3_device *smmu;
 	struct device *dev = &pdev->dev;
 	struct resource *res;
+	void *cmdq_base;
 
 	/* Only device tree, ACPI not supported. */
 	if (!dev->of_node)
@@ -94,6 +106,19 @@ static int __init smmuv3_nesting_probe(struct platform_device *pdev)
 	smmu->mmio_addr = res->start;
 	smmu->mmio_size = resource_size(res);
 
+	/*
+	 * 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;
+
 	if (of_dma_is_coherent(dev->of_node))
 		smmu->features |= ARM_SMMU_FEAT_COHERENCY;
 
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +126 -1

179 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +10 -1
index afc3ca791583..3903ae358555 100644
@@ -8,6 +8,8 @@
 #include <nvhe/spinlock.h>
 #endif
 
+#include "../arm-smmu-v3.h"
+
 /**
  * struct hyp_arm_smmu_v3_device - SMMUv3 instance owned by the hypervisor
  * @mmio_addr:		Physical address of the SMMU registers
@@ -21,7 +23,10 @@
  * @lock:		Lock to protect SMMU emulation
  * @hw_lock:		Lock to protect SMMU HW (e.g. CMDQ)
  *			Order smmu.lock => host_mmu.lock => smmu.hw_lock
- *
+ * @cmdq:		CMDQ as observed by HW
+ * @cmdq_host:		Host view of the CMDQ, only q_base, base_dma and llq used
+ * @cmdq_max_shift:	Max shift for the CMDQ probed from HW
+ * @cr0:		Last value of CR0
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
  */
@@ -41,6 +46,10 @@ struct hyp_arm_smmu_v3_device {
 	u32			lock;
 	u32			hw_lock;
 #endif
+	struct arm_smmu_queue	cmdq;
+	struct arm_smmu_queue	cmdq_host;
+	u32			cmdq_max_shift;
+	u32			cr0;
 };
 
 #ifdef __KVM_NVHE_HYPERVISOR__
Signature · 1 line
2.56.0.176.g3575e65682-goog
#44 lore raw

PATCH v9 15/25 iommu/arm-smmu-v3-kvm: Add CMDQ functions

In reply to Mostafa Saleh

Add the command queue accessors. The hypervisor's own commands, such as TLB invalidations, go through smmu_send_cmd(), which queues the command followed by a CMD_SYNC and waits for completion. Filtered host commands are copied into the shadow queue with smmu_add_cmd_raw().

Polling is bounded by a 1 ms timeout measured with the architected counter, and sleeps in WFE when the SMMU supports SEV.

Split queue_space() out of queue_has_space(), as the emulation will check the space in the host's queue.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  14 ++-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 119 ++++++++++++++++++
 2 files changed, 127 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -6
index d07fca3d1cf5..bc2e7e10200a 100644
@@ -1287,19 +1287,21 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
 bool arm_smmu_erratum_repeat_tlbi_cfgi(void);
 
 /* Queue and STE 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-hyp.c +119 -0

137 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
#48 lore raw

PATCH v9 16/25 iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to Mostafa Saleh

Take the command queue away from the host:
- ARM_SMMU_CMDQ_BASE: writes are only accepted while the command queue is disabled and record the host queue base. Reads return the saved value.
- ARM_SMMU_CMDQ_PROD: writes run the emulation, which copies, checks and filters every pending command into the shadow queue, ends the batch with a CMD_SYNC and waits for the hardware to drain it, so the host's view of CONS never runs ahead of completion. Reads return the host copy.
- ARM_SMMU_CMDQ_CONS: writes update the software copy and are only accepted while the queue is disabled, so the host cannot skip submitted commands. Reads return the emulated value plus the error bits of the hardware CONS.

Commands the filter rejects are fatal, as no well-behaved host issues them.

Also emulate IDR1.CMDQS to advertise the shadow queue size, which can be smaller than the hardware's.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 150 +++++++++++++++++-
 1 file changed, 145 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +145 -5
index c9fc21e405e7..dc9b1e659a42 100644
@@ -113,7 +113,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;
@@ -344,6 +343,104 @@ static int smmu_init(void)
 	return ret;
 }
 
+static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *command)
+{
+	u64 type = FIELD_GET(CMDQ_0_OP, command[0]);
+
+	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, command[1]) != 31))
+			return true;
+		break;
+	}
+	case CMDQ_OP_TLBI_NH_ASID:
+	case CMDQ_OP_TLBI_NH_VA:
+	case CMDQ_OP_TLBI_NH_ALL:
+	case 0x13: /* CMD_TLBI_NH_VAA: Not used by Linux */
+	{
+		/* Only allow VMID = 0 */
+		if (FIELD_GET(CMDQ_TLBI_0_VMID, command[0]) != 0)
+			return true;
+		break;
+	}
+	case CMDQ_OP_PREFETCH_CFG:
+	case CMDQ_OP_CFGI_CD:
+	case CMDQ_OP_CFGI_CD_ALL:
+	case CMDQ_OP_TLBI_NSNH_ALL:
+	case CMDQ_OP_PRI_RESP:
+	case CMDQ_OP_RESUME:
+		break;
+	case CMDQ_OP_CMD_SYNC:
+		if (FIELD_GET(CMDQ_SYNC_0_CS, command[0]) == CMDQ_SYNC_0_CS_IRQ) {
+			/* Do not allow MSI */
+			command[0] &= ~CMDQ_SYNC_0_CS;
+			command[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
+			command[1] &= ~CMDQ_SYNC_1_MSIADDR_MASK;
+		}
+		break;
+	default:
+		/* Deny unknown commands */
+		return true;
+	}
+
+	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.base_dma);
+	u64 cmd[CMDQ_ENT_DWORDS];
+	int idx, ret;
+	u32 pending;
+	bool skip;
+
+	if (!is_cmdq_enabled(smmu))
+		return 0;
+
+	pending = (1 << (smmu->cmdq_host.llq.max_n_shift)) - queue_space(&smmu->cmdq_host.llq);
+
+	hyp_spin_lock(&smmu->hw_lock);
+	/* Wait for the command queue to have some space. */
+	ret = smmu_wait(false, smmu_cmdq_has_space(&smmu->cmdq, pending));
+	if (ret) {
+		hyp_spin_unlock(&smmu->hw_lock);
+		return ret;
+	}
+
+	while (pending--) {
+		int i;
+
+		idx = Q_IDX(&smmu->cmdq_host.llq, smmu->cmdq_host.llq.cons);
+		queue_inc_cons(&smmu->cmdq_host.llq);
+
+		/* Copy the command to local buffer avoiding TOCTOU */
+		for (i = 0; i < CMDQ_ENT_DWORDS; ++i)
+			cmd[i] = le64_to_cpu(READ_ONCE(host_cmdq[idx * CMDQ_ENT_DWORDS + i]));
+
+		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);
+
+	ret = smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));
+	hyp_spin_unlock(&smmu->hw_lock);
+	return ret;
+}
+
 static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
 {
 	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
@@ -385,18 +482,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		 */
 		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP | IDR0_ATS);
 		break;
-	/* Passthrough the register access for bisectability, 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 (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 bisectability, handled later */
 	case ARM_SMMU_STRTAB_BASE:
 	case ARM_SMMU_STRTAB_BASE_CFG:
 	case ARM_SMMU_GBPA:
@@ -481,10 +611,20 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		mask = read_write;
 		break;
 	/* Allowed RO 32 bit registers. */
+	case ARM_SMMU_IDR1:
+		if (len != sizeof(u32))
+			break;
+		/* Cap CMDQS to the shadow queue size. */
+		if (!is_write) {
+			val = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
+			val &= ~IDR1_CMDQS;
+			val |= FIELD_PREP(IDR1_CMDQS, smmu->cmdq.llq.max_n_shift);
+			goto out_update_regs;
+		}
+	fallthrough;
 	case ARM_SMMU_IIDR:
 	case ARM_SMMU_IDR5:
 	case ARM_SMMU_IDR3:
-	case ARM_SMMU_IDR1:
 	case ARM_SMMU_IRQ_CTRLACK:
 	case ARM_SMMU_GERROR:
 		if (len != sizeof(u32))
Signature · 1 line
2.56.0.176.g3575e65682-goog
#53 lore raw

PATCH v9 17/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 passthrough for bisectability, that is changed next where CFGI commands will be trapped and used to update the shadow copy that will be used by the 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.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c   |  21 ++-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 122 ++++++++++++++++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  11 ++
 3 files changed, 153 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c +20 -1
index d7f2e73add7f..09078a6c39b0 100644
@@ -17,6 +17,13 @@
 #include "pkvm/arm-smmu-v3-hyp.h"
 
 #define SMMU_KVM_CMDQ_ORDER		get_order(MAX(SZ_64K, PAGE_SIZE))
+/*
+ * 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)))
 
 static size_t kvm_arm_smmu_count;
 static struct hyp_arm_smmu_v3_device *kvm_arm_smmu_array;
@@ -33,6 +40,9 @@ static void __init 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));
@@ -77,8 +87,8 @@ static int __init smmuv3_nesting_probe(struct platform_device *pdev)
 {
 	struct hyp_arm_smmu_v3_device *smmu;
 	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)
@@ -119,6 +129,15 @@ static int __init 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;
+
 	if (of_dma_is_coherent(dev->of_node))
 		smmu->features |= ARM_SMMU_FEAT_COHERENCY;
 
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +122 -0

177 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +11 -0
index 3903ae358555..31ab7a0335a8 100644
@@ -27,6 +27,12 @@
  * @cmdq_host:		Host view of the CMDQ, only q_base, base_dma and llq used
  * @cmdq_max_shift:	Max shift for the CMDQ probed from HW
  * @cr0:		Last value of CR0
+ * @host_ste_cfg:	Host stream table config
+ * @host_ste_base:	Host stream table base
+ * @strtab_cfg:		Stream table config as seen by HW
+ * @strtab_dma:		Physical address of the shadow stream table
+ * @strtab_size:	Size of the shadow stream table
+ *
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
  */
@@ -50,6 +56,11 @@ struct hyp_arm_smmu_v3_device {
 	struct arm_smmu_queue	cmdq_host;
 	u32			cmdq_max_shift;
 	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;
 };
 
 #ifdef __KVM_NVHE_HYPERVISOR__
Signature · 1 line
2.56.0.176.g3575e65682-goog
#58 lore raw

PATCH v9 18/25 iommu/arm-smmu-v3-kvm: Shadow STEs

In reply to Mostafa Saleh

Shadow the STE when the host sends CFGI_STE: copy the host entry as is into the hypervisor-owned stream table, which is the one the hardware uses. For 2-level tables the host L2 table is shared and pinned while the entry is read, and the hypervisor L2 table is allocated from the IOMMU pool the first time its L1 slot is used.

STRTAB_BASE and STRTAB_BASE_CFG are no longer passed through: writes are recorded while the SMMU is disabled and reads return the recorded values.

The next patch attaches the stage-2 page table to the shadow entry.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 124 +++++++++++++++++-
 1 file changed, 118 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +118 -6
index 0fa98915e00d..132f3a9ba308 100644
@@ -25,6 +25,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; \
@@ -290,6 +293,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, i;
+
+	host_l1_desc.l2ptr = READ_ONCE(host_ste_base[arm_smmu_strtab_l1_idx(sid)]);
+	if (!(le64_to_cpu(host_l1_desc.l2ptr) & STRTAB_L1_DESC_SPAN))
+		return -EINVAL;
+
+	host_l2_tab = le64_to_cpu(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);
+
+	for (i = 0; i < STRTAB_STE_DWORDS; ++i)
+		host_ste_out->data[i] =
+			READ_ONCE(l2ptr->stes[arm_smmu_strtab_l2_idx(sid)].data[i]);
+
+	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, i;
+
+	/* Leaf = 0 is not supported, as the kernel driver. */
+	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;
+
+		/* No hypervisor entry, first time the L2 is populated. */
+		if (!l1tab[l1_idx].l2ptr) {
+			struct arm_smmu_strtab_l2 *l2table;
+
+			l2table = pkvm_iommu_alloc_pages(get_order(sizeof(*l2table)));
+			if (!l2table)
+				return -ENOMEM;
+			/* Ensure L2 is observed before writing the ptr. */
+			dma_wmb();
+			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)];
+	}
+
+	for (i = 0; i < STRTAB_STE_DWORDS; ++i)
+		WRITE_ONCE(hyp_ste_ptr->data[i], host_ste_ptr->data[i]);
+	return 0;
+}
+
 static int smmu_init_strtab(struct hyp_arm_smmu_v3_device *smmu)
 {
 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
@@ -410,8 +498,24 @@ 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 leaf = FIELD_GET(CMDQ_CFGI_1_LEAF, command[1]);
+		u32 sid = FIELD_GET(CMDQ_CFGI_0_SID, command[0]);
+		int ret;
+
+		/*
+		 * If STE update is required, flush the CMDQ and drop the lock as that
+		 * might require to update the host page table and acquire its lock.
+		 */
+		writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+		hyp_spin_unlock(&smmu->hw_lock);
+		ret = smmu_reshadow_ste(smmu, sid, leaf);
+		hyp_spin_lock(&smmu->hw_lock);
+		if (ret)
+			return true;
+
 		break;
+	}
 	case CMDQ_OP_CFGI_ALL:
 	{
 		/*
@@ -627,25 +731,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 bisectability, 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 bisectability, handled later */
 	case ARM_SMMU_GBPA:
 		mask = read_write;
 		break;
Signature · 1 line
2.56.0.176.g3575e65682-goog
#61 lore raw

PATCH v9 19/25 iommu/arm-smmu-v3-kvm: Share other queues

In reply to Mostafa Saleh

The event and PRI queues are written by the SMMU, not read by the hypervisor, so they are not shadowed. They still must not be pointed at protected memory: when the host enables a queue, share and pin its memory, which fails unless the host owns it and keeps the host from donating it afterwards.

The queues stay shared once enabled, as the SMMU may keep writing until CR0ACK reflects the disable, and the base may not change once shared. The kernel driver programs each base once and never changes it.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 81 +++++++++++++++++--
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  8 ++
 2 files changed, 84 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +76 -5
index 132f3a9ba308..1972df1d2a8d 100644
@@ -82,6 +82,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);
+}
+
 /*
  * Host structures the hypervisor reads (CMDQ, STE) are shared and pinned:
  * - Prevent the host from donating this memory.
@@ -663,6 +673,20 @@ static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
 				   cmdq_size(&smmu->cmdq_host)));
 }
 
+static void smmu_share_queue(struct hyp_arm_smmu_v3_device *smmu,
+			     unsigned long q_base, size_t ent_size_shift)
+{
+	/* Q_BASE_ADDR_MASK is not enough as the SMMU also ignores bits > OAS */
+	phys_addr_t base = q_base & Q_BASE_ADDR_MASK & ((1ULL << smmu->oas) - 1);
+	size_t size = 1UL << (FIELD_GET(Q_BASE_LOG2SIZE, q_base) + ent_size_shift);
+
+	/* The host must program the base first and it must be aligned to size. */
+	if (WARN_ON(!q_base || (base & (size - 1))))
+		return;
+
+	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)
@@ -768,6 +792,8 @@ 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);
 
 			/* Enforce ATS checks. */
 			if (smmu->features & ARM_SMMU_FEAT_ATS)
@@ -777,6 +803,26 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *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, do not unshare the queues at disable
+			 * as that is more complicated, unsharing from here can lead to
+			 * use-after-unshare issues, and requires ordering with cr0ack.
+			 * The host can disable those queues during shutdown, but it never
+			 * changes the base address (even with RPM), so leave the queue
+			 * shared and assert that multiple host writes do not change it.
+			 */
+			if (!last_evtq_en && is_evtq_enabled(smmu) && !smmu->evtq_shared) {
+				smmu_share_queue(smmu, smmu->evtq_base, EVTQ_ENT_SZ_SHIFT);
+				smmu->evtq_shared = true;
+			}
+
+			if (!last_priq_en && is_priq_enabled(smmu) && !smmu->priq_shared) {
+				smmu_share_queue(smmu, smmu->priq_base, PRIQ_ENT_SZ_SHIFT);
+				smmu->priq_shared = true;
+			}
+
 			if (!last_smmu_en && is_smmu_enabled(smmu))
 				smmu_emulate_enable(smmu);
 			else if (last_smmu_en && !is_smmu_enabled(smmu))
@@ -806,6 +852,34 @@ 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) {
+			/* See ARM_SMMU_CR0 */
+			if (is_evtq_enabled(smmu) ||
+			    (smmu->evtq_shared && smmu->evtq_base != val))
+				break;
+			smmu->evtq_base = val;
+		}
+		mask = read_write;
+		break;
+
+	case ARM_SMMU_PRIQ_BASE:
+		if (len != sizeof(u64))
+			break;
+
+		if (is_write) {
+			/* See ARM_SMMU_CR0 */
+			if (is_priq_enabled(smmu) ||
+			    (smmu->priq_shared && smmu->priq_base != val))
+				break;
+			smmu->priq_base = val;
+		}
+		mask = read_write;
+		break;
+
 	/* Allowed 32 bit registers. */
 	case ARM_SMMU_EVTQ_IRQ_CFG1:
 	case ARM_SMMU_EVTQ_IRQ_CFG2:
@@ -833,15 +907,12 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 	case ARM_SMMU_EVTQ_IRQ_CFG0:
 	case ARM_SMMU_PRIQ_IRQ_CFG0:
 	case ARM_SMMU_GERROR_IRQ_CFG0:
+		if (len != sizeof(u64))
+			break;
 		/* These are RES0 as MSI support is hidden. */
 		val = 0;
 		if (!is_write)
 			goto out_update_regs;
-		fallthrough;
-	case ARM_SMMU_EVTQ_BASE:
-	case ARM_SMMU_PRIQ_BASE:
-		if (len != sizeof(u64))
-			break;
 		mask = read_write;
 		break;
 	/* Allowed RO 32 bit registers. */
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +8 -0
index 31ab7a0335a8..2fb478e9233a 100644
@@ -32,6 +32,10 @@
  * @strtab_cfg:		Stream table config as seen by HW
  * @strtab_dma:		Physical address of the shadow stream table
  * @strtab_size:	Size of the shadow stream table
+ * @evtq_base:		Host EVTQ_BASE value
+ * @priq_base:		Host PRIQ_BASE value
+ * @evtq_shared:	Whether the EVTQ was shared
+ * @priq_shared:	Whether the PRIQ was shared
  *
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
@@ -61,6 +65,10 @@ struct hyp_arm_smmu_v3_device {
 	u64			host_ste_cfg;
 	u64			host_ste_base;
 	struct arm_smmu_strtab_cfg strtab_cfg;
+	u64			evtq_base;
+	u64			priq_base;
+	bool			evtq_shared;
+	bool			priq_shared;
 };
 
 #ifdef __KVM_NVHE_HYPERVISOR__
Signature · 1 line
2.56.0.176.g3575e65682-goog
#64 lore raw

PATCH v9 20/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.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 32 +++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +29 -3
index 1972df1d2a8d..943ffef0bb4a 100644
@@ -217,6 +217,22 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 	return smmu_sync_cmd(smmu);
 }
 
+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);
+}
+
 /* 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)
 {
@@ -456,6 +472,10 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 	if (ret)
 		goto out_deinit;
 
+	ret = smmu_abort_gbpa(smmu);
+	if (ret)
+		goto out_deinit;
+
 	return 0;
 
 out_deinit:
@@ -781,10 +801,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 bisectability, 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.56.0.176.g3575e65682-goog
#65 lore raw

PATCH v9 21/25 iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

In reply to Mostafa Saleh

To be able to populate the shadow stage-2 in the hypervisor, compile io-pgtable-arm object for the hypervisor.

That requires the hypervisor to provide the iommu-pages allocator, redefine the address conversion helpers, and stub out the DMA API.

---
 arch/arm64/kvm/hyp/nvhe/Makefile              |  3 +-
 .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h | 88 +++++++++++++++++++
 drivers/iommu/io-pgtable-arm.c                | 14 +--
 drivers/iommu/io-pgtable-arm.h                |  9 ++
 4 files changed, 106 insertions(+), 8 deletions(-)
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h
arch/arm64/kvm/hyp/nvhe/Makefile +2 -1
index ee86e4b3436f..08da05ddda5d 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-hyp.o \
-	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o ../../../../../lib/hweight.o
+	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o ../../../../../lib/hweight.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/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h new file +88 -0

88 lines not on this page · raw message

drivers/iommu/io-pgtable-arm.c +7 -7
index 476c0e25631a..6b2668c2ef44 100644
@@ -20,7 +20,6 @@
 #include <asm/barrier.h>
 
 #include "io-pgtable-arm.h"
-#include "iommu-pages.h"
 
 #define ARM_LPAE_MAX_ADDR_BITS		52
 #define ARM_LPAE_S2_MAX_CONCAT_PAGES	16
@@ -301,6 +300,7 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 {
 	struct device *dev = cfg->iommu_dev;
 	void *pages;
+	int nid = dev ? dev_to_node(dev) : NUMA_NO_NODE;
 
 	/*
 	 * For very small starting-level translation tables the HW requires a
@@ -311,7 +311,7 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 	if (cfg->alloc)
 		return __arm_lpae_cfg_alloc(size, gfp, cfg, cookie);
 
-	pages = iommu_alloc_pages_node_sz(dev_to_node(dev), gfp, size);
+	pages = iommu_alloc_pages_node_sz(nid, gfp, size);
 	if (!pages)
 		return NULL;
 
@@ -650,7 +650,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);
+	arm_lpae_free_data(data);
 }
 
 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
@@ -964,7 +964,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 = arm_lpae_alloc_data(*data);
 	if (!data)
 		return NULL;
 
@@ -1091,7 +1091,7 @@ arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	arm_lpae_free_data(data);
 	return NULL;
 }
 
@@ -1187,7 +1187,7 @@ arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	arm_lpae_free_data(data);
 	return NULL;
 }
 
@@ -1266,7 +1266,7 @@ arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
 	return &data->iop;
 
 out_free_data:
-	kfree(data);
+	arm_lpae_free_data(data);
 	return NULL;
 }
 
drivers/iommu/io-pgtable-arm.h +9 -0
index ba7cfdf7afa0..4278da41e406 100644
@@ -27,4 +27,13 @@
 #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
 #define ARM_LPAE_TCR_PS_52_BIT		0x6ULL
 
+#ifndef __KVM_NVHE_HYPERVISOR__
+#include "iommu-pages.h"
+#define arm_lpae_alloc_data(obj)	kmalloc_obj(obj)
+#define arm_lpae_free_data(p)		kfree(p)
+
+#else
+#include "arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h"
+#endif
+
 #endif /* IO_PGTABLE_ARM_H_ */
Signature · 1 line
2.56.0.176.g3575e65682-goog
#68 lore raw

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

In reply to Mostafa Saleh

The hypervisor calls back into the driver on every change to the host stage-2 page table.
Mirror those changes into an identity mapped stage-2 for the SMMUv3, That would be attached to all active SIDs.

Difference between memory and MMIO handling:

- Memory is always mapped with PAGE_SIZE. io-pgtable-arm no longer supports split_blk_unmap, so a block cannot be broken into a table once it is mapped, and pages are donated back and forth at PAGE_SIZE granularity. The page table pool is sized to cover all of memory at that granularity.

- MMIO is mapped with the largest block that fits, as it is assumed to cover the whole IAS that is not memory while pKVM only reserves 1G for the page table. MMIO is never donated at runtime, so it is never unmapped and never needs a block to be split.

The TLB maintenance ops are left as stubs, they are implemented in the next patch.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 153 +++++++++++++++++-
 1 file changed, 152 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +152 -1
index 943ffef0bb4a..75b2b1477a4f 100644
@@ -15,6 +15,9 @@
 #include "../arm-smmu-v3.h"
 #include "../arm-smmu-v3-common-lib.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;
 
@@ -72,6 +75,9 @@ static inline u64 hyp_clock_ns(void)
 	__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);
@@ -217,6 +223,24 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 	return smmu_sync_cmd(smmu);
 }
 
+static void smmu_tlb_flush_walk(unsigned long iova, size_t size,
+				size_t granule, void *cookie)
+{
+	/* TBD: Invalidate the range in all the SMMUs. */
+}
+
+static void smmu_tlb_add_page(struct iommu_iotlb_gather *gather,
+			      unsigned long iova, size_t granule,
+			      void *cookie)
+{
+	/* TBD: Invalidate the granule in all the SMMUs. */
+}
+
+static const struct iommu_flush_ops smmu_tlb_ops = {
+	.tlb_flush_walk = smmu_tlb_flush_walk,
+	.tlb_add_page	= smmu_tlb_add_page,
+};
+
 static int smmu_abort_gbpa(struct hyp_arm_smmu_v3_device *smmu)
 {
 	int ret;
@@ -483,6 +507,38 @@ 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 = ~0UL,
+		.ias = 48,
+		.oas = 48,
+		.coherent_walk = true,
+		.quirks = IO_PGTABLE_QUIRK_NO_WARN,
+	};
+	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 = pkvm_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 the host is still trusted. */
 static int smmu_init(void)
 {
@@ -513,7 +569,10 @@ static int smmu_init(void)
 			goto out_reclaim_smmu;
 	}
 
-	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)
@@ -1011,8 +1070,100 @@ 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 allocates 1G 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 triggering
+			 *   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;
+
+			if (ret == -EEXIST) {
+				/*
+				 * It is possible to get EEXIST when a VM dies with pages
+				 * in a shared state.
+				 */
+				ret = 0;
+				size -= pgsize;
+				start += pgsize;
+				continue;
+			}
+			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.56.0.176.g3575e65682-goog
#71 lore raw

PATCH v9 23/25 iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs

In reply to Mostafa Saleh

Implement the io-pgtable tlb ops, which invalidate the shadow stage-2 in every SMMU.

The range invalidation commands come from arm_smmu_tlbi_calc_range() in the common lib, SMMUs without range invalidation get one command per entry.
The hypervisor also needs to invaldiate all of S1 after S2 invalidations by IPA to invalidate combined entries.

Invalidation needs the command queue, so track whether it is enabled in cmdq_active. It is updated with hw_lock held once the CR0 write has been acknowledged, so it cannot change under a concurrent invalidation. For the same reason the host is not allowed to disable the command queue while the SMMU is enabled, as the hypervisor would no longer be able to invalidate. As invalidation is skipped while the command queue is off, flush the TLBs every time the SMMU is enabled.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 156 +++++++++++++++++-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |   2 +
 2 files changed, 155 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +153 -3
index 75b2b1477a4f..01f23fce6be5 100644
@@ -209,7 +209,6 @@ static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu)
 	return smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));
 }
 
-__maybe_unused
 static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 			 struct arm_smmu_cmd *cmd)
 {
@@ -223,17 +222,118 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
 	return smmu_sync_cmd(smmu);
 }
 
+static int smmu_tlb_inv_vmid(struct hyp_arm_smmu_v3_device *smmu)
+{
+	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_S12_VMALL);
+
+	return smmu_send_cmd(smmu, &cmd);
+}
+
+static int __smmu_tlb_inv_range(struct hyp_arm_smmu_v3_device *smmu,
+				struct arm_smmu_cmd *cmd,
+				unsigned long iova, size_t size, size_t granule,
+				bool leaf)
+{
+	struct arm_smmu_tlbi tlbi = {
+		.iova = iova,
+		.size = size,
+		.iopte_size = granule,
+		.tgsz_lg2 = __ffs(idmap_pgtable->cfg.pgsize_bitmap),
+		.leaf_only = leaf,
+	};
+	unsigned int i;
+	int ret;
+
+	arm_smmu_tlbi_calc_range(&tlbi, false);
+	if (tlbi.range.use_full_inv)
+		return smmu_tlb_inv_vmid(smmu);
+
+	for (i = 0; i < tlbi.range.num_cmds; i++) {
+		struct arm_smmu_cmd range_cmd = tlbi.range.cmds[i];
+
+		range_cmd.data[0] |= cmd->data[0];
+		ret = smmu_add_cmd(smmu, &range_cmd);
+		if (ret)
+			return ret;
+	}
+
+	return smmu_sync_cmd(smmu);
+}
+
+static int __smmu_tlb_inv_range_pages(struct hyp_arm_smmu_v3_device *smmu,
+				      struct arm_smmu_cmd *cmd,
+				      unsigned long iova, size_t size, size_t granule,
+				      bool leaf)
+{
+	unsigned long end = iova + size;
+	int ret;
+
+	/* Same threshold as arm_smmu_tlbi_calc_single(). */
+	if (size / granule > 512)
+		return smmu_tlb_inv_vmid(smmu);
+
+	for (; iova < end; iova += granule) {
+		cmd->data[1] = FIELD_PREP(CMDQ_TLBI_1_LEAF, leaf) |
+			       (iova & ~GENMASK_U64(11, 0));
+		ret = smmu_add_cmd(smmu, cmd);
+		if (ret)
+			return ret;
+	}
+
+	return smmu_sync_cmd(smmu);
+}
+
+static int smmu_tlb_inv_range_smmu(struct hyp_arm_smmu_v3_device *smmu,
+				   unsigned long iova, size_t size, size_t granule,
+				   bool leaf)
+{
+	struct arm_smmu_cmd cmd_s1 = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_NH_ALL);
+	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_S2_IPA);
+	int ret;
+
+	if (smmu->features & ARM_SMMU_FEAT_RANGE_INV)
+		ret = __smmu_tlb_inv_range(smmu, &cmd, iova, size, granule, leaf);
+	else
+		ret = __smmu_tlb_inv_range_pages(smmu, &cmd, iova, size, granule, leaf);
+	if (ret)
+		return ret;
+
+	return smmu_send_cmd(smmu, &cmd_s1);
+}
+
+static void smmu_tlb_inv_range(unsigned long iova, size_t size, size_t granule,
+			       bool leaf)
+{
+	struct hyp_arm_smmu_v3_device *smmu;
+
+	for_each_smmu(smmu) {
+		hyp_spin_lock(&smmu->hw_lock);
+		/*
+		 * Don't bother if CMDQ 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.
+		 * When the SMMU is re-enabled the hypervisor cleans the TLBs.
+		 */
+		if (smmu->cmdq_active)
+			WARN_ON(smmu_tlb_inv_range_smmu(smmu, iova, size,
+							granule, leaf));
+		hyp_spin_unlock(&smmu->hw_lock);
+	}
+}
+
 static void smmu_tlb_flush_walk(unsigned long iova, size_t size,
 				size_t granule, void *cookie)
 {
-	/* TBD: Invalidate the range in all the SMMUs. */
+	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)
 {
-	/* TBD: Invalidate the granule in all the SMMUs. */
+	smmu_tlb_inv_range(iova, granule, granule, true);
 }
 
 static const struct iommu_flush_ops smmu_tlb_ops = {
@@ -721,6 +821,33 @@ static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enab
 	return smmu_unshare_pages(strtab_host_base(smmu), size);
 }
 
+static int smmu_flush_all_tlb(struct hyp_arm_smmu_v3_device *smmu)
+{
+	int ret;
+	u32 cr0;
+	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_NSNH_ALL);
+
+	hyp_spin_lock(&smmu->hw_lock);
+	/*
+	 * This must be called when the SMMU is getting enabled.
+	 * First enable the cmdq and then invalidate the TLB.
+	 */
+	cr0 = readl_relaxed(smmu->base + ARM_SMMU_CR0);
+	if (!(cr0 & CR0_CMDQEN)) {
+		cr0 |= CR0_CMDQEN;
+		writel_relaxed(cr0, smmu->base + ARM_SMMU_CR0);
+		ret = smmu_wait(false, readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0);
+		if (ret) {
+			hyp_spin_unlock(&smmu->hw_lock);
+			return ret;
+		}
+	}
+
+	ret = smmu_send_cmd(smmu, &cmd);
+	hyp_spin_unlock(&smmu->hw_lock);
+	return ret;
+}
+
 static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
 {
 	/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
@@ -728,6 +855,8 @@ static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
 		return;
 
 	WARN_ON(smmu_update_ste_shadow(smmu, true));
+	/* Clean the TLBs each time the SMMU is enabled. */
+	WARN_ON(smmu_flush_all_tlb(smmu));
 }
 
 static void smmu_emulate_disable(struct hyp_arm_smmu_v3_device *smmu)
@@ -748,6 +877,13 @@ static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
 
 static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
 {
+	/*
+	 * We can not enable the SMMU if the CMDQ is disabled and similarly
+	 * we can not disable the CMDQ if the SMMU is enabled, as that can
+	 * lead to stale TLBs.
+	 */
+	WARN_ON(is_smmu_enabled(smmu));
+
 	WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
 				   cmdq_size(&smmu->cmdq_host)));
 }
@@ -1033,6 +1169,20 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		else
 			writel_relaxed(val & mask, smmu->base + off);
 
+		/*
+		 * Make sure writes to CR0 are immediately observed, that is important
+		 * when synchronizing with TLB invalidation as reading CR0 is not enough
+		 * to deduce the SMMU state, and we have to enforce the ack with the
+		 * hw_lock acquired.
+		 */
+		if (off == ARM_SMMU_CR0) {
+			u32 cr0 = val;
+
+			WARN_ON(smmu_wait(false,
+					  readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0));
+			smmu->cmdq_active = !!(cr0 & CR0_CMDQEN);
+		}
+
 		hyp_spin_unlock(&smmu->hw_lock);
 		return true;
 	}
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +2 -0
index 2fb478e9233a..9ccee7fb710f 100644
@@ -27,6 +27,7 @@
  * @cmdq_host:		Host view of the CMDQ, only q_base, base_dma and llq used
  * @cmdq_max_shift:	Max shift for the CMDQ probed from HW
  * @cr0:		Last value of CR0
+ * @cmdq_active:	CMDQEN as acknowledged by the hardware, under hw_lock
  * @host_ste_cfg:	Host stream table config
  * @host_ste_base:	Host stream table base
  * @strtab_cfg:		Stream table config as seen by HW
@@ -60,6 +61,7 @@ struct hyp_arm_smmu_v3_device {
 	struct arm_smmu_queue	cmdq_host;
 	u32			cmdq_max_shift;
 	u32			cr0;
+	bool			cmdq_active;
 	dma_addr_t		strtab_dma;
 	size_t			strtab_size;
 	u64			host_ste_cfg;
Signature · 1 line
2.56.0.176.g3575e65682-goog
#74 lore raw

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

In reply to Mostafa Saleh

As now the hypervisor controls the command queue, stream table, and shadows the stage-2 page table, we enable stage-2 translation in case the host puts an STE in bypass or stage-1.

The hypervisor will control the S2 part of the STE and sanitize it.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c    | 112 +++++++++++++++++-
 1 file changed, 106 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +106 -6
index 01f23fce6be5..3562bb222f53 100644
@@ -443,6 +443,60 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_attach_stage_2(struct hyp_arm_smmu_v3_device *smmu,
+			       struct arm_smmu_ste *ste)
+{
+	unsigned long vttbr, cfg;
+	unsigned long ts, sl, ic, oc, sh, tg, ps;
+	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)) {
+		return 0;
+	}
+	/* S2 is not advertised, that should never be attempted. */
+	if ((cfg == STRTAB_STE_0_CFG_S2_TRANS) ||
+	    (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;
+
+	if (smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR) {
+		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));
+	}
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_EATS | STRTAB_STE_1_S2FWB | STRTAB_STE_1_STRW);
+
+	/* 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)
 {
@@ -472,12 +526,40 @@ static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid,
 	return 0;
 }
 
+struct smmu_ste_writer {
+	struct arm_smmu_entry_writer writer;
+	struct hyp_arm_smmu_v3_device *smmu;
+	u32 sid;
+};
+
+static void smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer)
+{
+	struct smmu_ste_writer *ste_writer =
+		container_of(writer, struct smmu_ste_writer, writer);
+	struct arm_smmu_cmd cfgi_cmd = arm_smmu_make_cmd_cfgi_ste(ste_writer->sid, true);
+
+	WARN_ON(smmu_send_cmd(ste_writer->smmu, &cfgi_cmd));
+}
+
+static const struct arm_smmu_entry_writer_ops smmu_ste_writer_ops = {
+	.get_used = arm_smmu_get_ste_used,
+	.get_update_safe = arm_smmu_get_ste_update_safe,
+	.sync = smmu_ste_writer_sync_entry,
+};
+
 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);
+	__le64 used[STRTAB_STE_DWORDS] = {};
+	struct arm_smmu_ste target = {};
 	int ret, i;
+	struct smmu_ste_writer ste_writer = {
+		.writer.ops = &smmu_ste_writer_ops,
+		.smmu = smmu,
+		.sid = sid,
+	};
 
 	/* Leaf = 0 is not supported, as the kernel driver. */
 	if (!leaf || !is_smmu_enabled(smmu) ||
@@ -493,7 +575,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);
@@ -502,8 +584,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;
 
@@ -523,8 +604,27 @@ 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)];
 	}
 
-	for (i = 0; i < STRTAB_STE_DWORDS; ++i)
-		WRITE_ONCE(hyp_ste_ptr->data[i], host_ste_ptr->data[i]);
+	/*
+	 * Summary of each host emulated state vs real HW.
+	 * |	Host	|	HW	|
+	 * ==============================
+	 * |	V=0	|	V=0	|
+	 * |	Abort	|	Abort	|
+	 * |	Bypass	|	S2	|
+	 * |	S1	|	S1+S2	|
+	 */
+	ret = smmu_attach_stage_2(smmu, &target);
+	if (ret)
+		return ret;
+
+	/* The HW ignores unused fields, don't let the host set them. */
+	arm_smmu_get_ste_used(target.data, used);
+	for (i = 0; i < STRTAB_STE_DWORDS; i++)
+		target.data[i] &= used[i];
+
+	hyp_spin_lock(&smmu->hw_lock);
+	arm_smmu_write_entry(&ste_writer.writer, hyp_ste_ptr->data, target.data);
+	hyp_spin_unlock(&smmu->hw_lock);
 	return 0;
 }
 
Signature · 1 line
2.56.0.176.g3575e65682-goog
#77 lore raw

PATCH v9 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 | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
Documentation/virt/kvm/arm/pkvm.rst +22 -1
index 514992a79a83..ce67e2ae1f9a 100644
@@ -77,7 +77,28 @@ 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.
+
+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 stages 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.
+
+The page tables for the stage-2 SMMUv3 will be allocated from a pool created
+at boot with size configured from the command line "kvm-arm.iommu_pgt_mem"
+The size must cover the worst case scenario for leaf mappings of system memory.
+
+This implementation trusts the system 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's
+responsibility to save/restore the SMMUv3 state.
 
 Proxying of Trustzone services
 ------------------------------
Signature · 1 line
2.56.0.176.g3575e65682-goog
#11 lore raw

Re: [PATCH v9 08/25] KVM: arm64: iommu: Add memory pool

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: fatal EL2 panic on IOMMU pool exhaustion --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: fatal EL2 panic on IOMMU pool exhaustion
--

commit 4301acf34cc06e2a4b1fab6b934f06ba07d48a9d
Author: Mostafa Saleh <[email protected]>
Subject: KVM: arm64: iommu: Add memory pool

This patch reserves a memory pool for the IOMMU driver's shadow page tables early during boot based on the kvm-arm.iommu_pgt_mem command line parameter. The driver later allocates from and frees to this pool at runtime in protected KVM mode.

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0
@@ -115,6 +117,11 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)

arch/arm64/kvm/hyp/nvhe/iommu.c int pkvm_iommu_init(void) · line 127

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +5 -0
 		return 0;
 
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = pkvm_iommu_ops->init();
 	if (ret)
 		return ret;

[Severity: High]
Will this trigger a fatal hypervisor panic if the host specifies an undersized pool?

The size of the newly introduced iommu_pages_pool is directly controlled by the host via the kvm-arm.iommu_pgt_mem command line parameter. If the host-provided pool size is too small, pkvm_iommu_snapshot_host_stage2() called immediately after this will exhaust the allocator and return -ENOMEM.

Because pkvm_iommu_init() wraps that call in a WARN_ON():

	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

and WARN_ON() at EL2 in nVHE/pKVM expands to a fatal BRK instruction, this exhaustion will crash the entire machine.

Could this be updated to propagate the error back to the host, instead of treating hyp_alloc_pages() failure as a fatal invariant violation?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#66 lore raw

Re: [PATCH v9 21/25] iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] arch: layering violation by including SMMUv3 header in generic io-pgtable-arm.h --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] arch: layering violation by including SMMUv3 header in generic io-pgtable-arm.h --

commit 251567dd4618edfaa4984ef7282b71a0da331297
Author: Mostafa Saleh <[email protected]>

iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

To be able to populate the shadow stage-2 in the hypervisor, compile io-pgtable-arm object for the hypervisor. This requires the hypervisor to provide the iommu-pages allocator, redefine the address conversion helpers, and stub out the DMA API.

[ ... ]

Quoted 5 lines
drivers/iommu/io-pgtable-arm.h +0 -0
index ba7cfdf7afa03..4278da41e406d 100644

drivers/iommu/io-pgtable-arm.h line 36

Quoted 10 lines
drivers/iommu/io-pgtable-arm.h +7 -0
@@ -27,4 +27,13 @@
 #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
 #define ARM_LPAE_TCR_PS_52_BIT		0x6ULL
 
+#ifndef __KVM_NVHE_HYPERVISOR__
+#include "iommu-pages.h"
+#define arm_lpae_alloc_data(obj)	kmalloc_obj(obj)
+#define arm_lpae_free_data(p)		kfree(p)
+
+#else
+#include "arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h"

[Severity: Low]
Is this an intended architecture layering violation? The generic ARM LPAE page table header drivers/iommu/io-pgtable-arm.h explicitly includes the driver-specific header arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h when compiled for the hypervisor. This tightly couples generic IOMMU framework code directly to the SMMUv3 driver path.

Should the hypervisor stubs be placed in a generic NVHE include directory (e.g., arch/arm64/kvm/hyp/include/nvhe/) to maintain subsystem independence, even if SMMUv3 is currently the only user?

Quoted 3 lines
drivers/iommu/io-pgtable-arm.h +2 -0
+#endif
+
 #endif /* IO_PGTABLE_ARM_H_ */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
#38 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] iommu/arm-smmu-v3-kvm: wrong WARN_ON() causes hyp panic on invalid host MMIO - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] iommu/arm-smmu-v3-kvm: wrong WARN_ON() causes hyp panic on invalid host MMIO
- [Medium] iommu/arm-smmu-v3-kvm: WARN_ON used to validate host input in SMMU emulation --

commit bf6dcb76ab39be1d82600f47f81d772f4c08b9a6
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow the command queue

This patch allocates a shadow command queue per SMMU to be used by the hypervisor at boot. The host command queue is pinned in a shared state when enabled to avoid donation issues and prevent untrusted host access from reaching the hypervisor state.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 236

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
@@ -132,6 +225,23 @@ static int smmu_init(void)
 	return ret;
 }
 
+static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
+
+	smmu->cmdq_host.llq.max_n_shift = min(shift, smmu->cmdq_max_shift);
+	smmu->cmdq_host.base_dma = smmu->cmdq_host.q_base & Q_BASE_ADDR_MASK;
+	smmu->cmdq_host.base_dma &= ~(cmdq_size(&smmu->cmdq_host) - 1);
+	WARN_ON(smmu_share_pages(smmu->cmdq_host.base_dma,
+				 cmdq_size(&smmu->cmdq_host)));

[Severity: Medium]
Will this cause a fatal hypervisor panic if the host provides an invalid physical address or an unshareable memory range?

At EL2, the WARN_ON() macro behaves as a fatal exception. If the host configures the emulated SMMU with an invalid address, smmu_share_pages() can fail and return an error. Wrapping this fallible, host-influenced operation in WARN_ON() will crash the hypervisor. Could this path safely ignore the invalid configuration or return an error instead?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 242

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+}
+
+static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
+				   cmdq_size(&smmu->cmdq_host)));

[Severity: Medium]
Does this also have the same issue with WARN_ON() panicking the hypervisor if the host-provided memory range fails to unshare?

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+}
+

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 274

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -158,6 +268,14 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		break;
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_CMDQ_BASE:
+		if (is_write) {
+			/* Not allowed by the architecture */
+			if (is_cmdq_enabled(smmu))
+				break;

[Severity: Medium]
If the host kernel makes an invalid MMIO write to ARM_SMMU_CMDQ_BASE while the CMDQ is enabled, the switch statement breaks and leaves the mask set to 0.

Later in smmu_dabt_device(), there is a pre-existing check:

	if (WARN_ON(!mask))
		goto out_ret;

Since WARN_ON() is fatal at EL2, will this turn an invalid host MMIO access into a hypervisor panic? It seems like the access should be gracefully ignored or rejected without triggering a fatal assertion.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+			smmu->cmdq_host.q_base = val;
+		}
+		mask = read_write;
+		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#72 lore raw

Re: [PATCH v9 23/25] iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] arm-smmu-v3: incorrect WARN_ON usage at EL2 for hardware timeouts and host inputs - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] arm-smmu-v3: incorrect WARN_ON usage at EL2 for hardware timeouts and host inputs
- [Medium] arm-smmu-v3: guaranteed timeout and hyp panic if host writes reserved bits to CR0 --

commit a876edb8ca1cb558cf1336f7a093e9a259c78f18
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs

This commit implements the io-pgtable TLB operations to invalidate the shadow stage-2 entries across all SMMUs in pKVM. It manages invalidation commands and ensures the command queue is active before flushing TLBs.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_tlb_inv_range(unsigned long iova, size_t size, size_t granule,
+			       bool leaf)
+{
+	struct hyp_arm_smmu_v3_device *smmu;
+
+	for_each_smmu(smmu) {
+		hyp_spin_lock(&smmu->hw_lock);
+		/*

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu, · line 321

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+		 * Don't bother if CMDQ 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.
+		 * When the SMMU is re-enabled the hypervisor cleans the TLBs.
+		 */
+		if (smmu->cmdq_active)
+			WARN_ON(smmu_tlb_inv_range_smmu(smmu, iova, size,
+							granule, leaf));

[Severity: High]
Under pKVM at EL2, WARN_ON() expands to a fatal BUG() instruction which crashes the hypervisor.

Could this WARN_ON() trigger a panic if the hardware SMMU is slow to drain its command queue? The call chain goes through smmu_tlb_inv_range_smmu(), smmu_send_cmd(), smmu_sync_cmd(), and ultimately smmu_wait(), which returns -ETIMEDOUT if the hardware polls for too long. Should this gracefully handle the error or ignore it instead of asserting?

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+		hyp_spin_unlock(&smmu->hw_lock);
+	}
+}

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enab · line 859

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
@@ -721,6 +821,33 @@ static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enab
 static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
 {
 	/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
 	if (WARN_ON(!is_cmdq_enabled(smmu)))
 		return;
 
 	WARN_ON(smmu_update_ste_shadow(smmu, true));
+	/* Clean the TLBs each time the SMMU is enabled. */
+	WARN_ON(smmu_flush_all_tlb(smmu));

[Severity: High]
Does this code panic the hypervisor if the SMMU hardware times out during the TLB flush on enablement? Similar to the above, smmu_flush_all_tlb() can return -ETIMEDOUT from smmu_wait() if the hardware is unresponsive.

Quoted 1 line

}

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu) · line 884

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
@@ -748,6 +877,13 @@ static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
 static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
 {
+	/*
+	 * We can not enable the SMMU if the CMDQ is disabled and similarly
+	 * we can not disable the CMDQ if the SMMU is enabled, as that can
+	 * lead to stale TLBs.
+	 */
+	WARN_ON(is_smmu_enabled(smmu));

[Severity: High]
Is it safe to panic the hypervisor here based on host-supplied configuration? If the host kernel supplies a CR0 value that disables the CMDQ while the SMMU is still enabled, this will trigger the EL2 WARN_ON() and crash the entire host.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
+
 	WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
 				   cmdq_size(&smmu->cmdq_host)));
 }

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -1033,6 +1169,20 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		else
 			writel_relaxed(val & mask, smmu->base + off);
 
+		/*

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 1182

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+		 * Make sure writes to CR0 are immediately observed, that is important
+		 * when synchronizing with TLB invalidation as reading CR0 is not enough
+		 * to deduce the SMMU state, and we have to enforce the ack with the
+		 * hw_lock acquired.
+		 */
+		if (off == ARM_SMMU_CR0) {
+			u32 cr0 = val;
+
+			WARN_ON(smmu_wait(false,
+					  readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0));

[Severity: Medium]
Can this sequence cause a guaranteed timeout and hypervisor panic if the host writes reserved bits to CR0?

The variable cr0 holds the raw, unmasked host input val. When the hypervisor writes to the hardware CR0 register, the hardware will update CR0ACK to reflect the accepted state, ignoring any reserved bits.

If the host writes a value with any bit set that the hardware ignores, readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0 will never evaluate to true. This would cause smmu_wait() to poll until it hits its timeout and return -ETIMEDOUT, triggering the WARN_ON() panic.

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+			smmu->cmdq_active = !!(cr0 & CR0_CMDQEN);
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#62 lore raw

Re: [PATCH v9 19/25] iommu/arm-smmu-v3-kvm: Share other queues

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/arm-smmu-v3-kvm: EL2 panic on kexec due to locked EVTQ/PRIQ base addresses --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3-kvm: EL2 panic on kexec due to locked EVTQ/PRIQ base addresses --

commit bfd6e271a581b5d4148ccc397cc5a82480d46470
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Share other queues

This commit introduces pinning and sharing of the event and PRI queues in the hypervisor to prevent the host from writing to protected memory. It enforces that the host cannot change the base addresses of these queues once they are shared.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 132f3a9ba3084..1972df1d2a8dc 100644

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 863

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
@@ -806,6 +852,34 @@ 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) {
+			/* See ARM_SMMU_CR0 */
+			if (is_evtq_enabled(smmu) ||
+			    (smmu->evtq_shared && smmu->evtq_base != val))
+				break;

[Severity: High]
Does this rigid check prevent the host from performing a successful kexec?

When the host executes a kexec, the new kernel boots and initializes its own SMMUv3 driver, allocating new memory for the EVTQ and writing the new base address here. Since EL2 state persists across kexec and the hypervisor never clears smmu->evtq_shared upon disable, this condition evaluates to true.

The break leaves mask = 0, which then triggers the WARN_ON(!mask) at the end of the handler, resulting in a fatal hypervisor panic.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+			smmu->evtq_base = val;
+		}
+		mask = read_write;
+		break;

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 877

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+
+	case ARM_SMMU_PRIQ_BASE:
+		if (len != sizeof(u64))
+			break;
+
+		if (is_write) {
+			/* See ARM_SMMU_CR0 */
+			if (is_priq_enabled(smmu) ||
+			    (smmu->priq_shared && smmu->priq_base != val))
+				break;

[Severity: High]
Similarly, does this check for the PRIQ base address cause the hypervisor to panic during kexec?

Just like EVTQ, the new kernel initialized during kexec will write a new PRIQ base address, triggering this break and hitting the same fatal WARN_ON(!mask) further down.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+			smmu->priq_base = val;
+		}
+		mask = read_write;
+		break;
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#45 lore raw

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

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/arm-smmu-v3-kvm: swapped WFE logic causes queue polling delays --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3-kvm: swapped WFE logic causes queue polling delays --

commit 533e5a914cd22f171b26c2c5c0e2acfb11756ea5
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add CMDQ functions

This commit adds command queue accessors for the ARM SMMUv3 in the hypervisor. It includes command queuing and synchronization functions, with a 1 ms polling timeout that sleeps in WFE when SEV is supported.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 02e7c61758329..c9fc21e405e79 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -77,6 +113,85 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_unshare_pages(phys_addr_t addr, size_t size) · line 158

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static int smmu_add_cmd(struct hyp_arm_smmu_v3_device *smmu,
+			struct arm_smmu_cmd *cmd)
+{
+	int ret;
+
+	hyp_assert_lock_held(&smmu->hw_lock);
+	ret = smmu_wait(smmu->features & ARM_SMMU_FEAT_SEV,
+			!smmu_cmdq_full(&smmu->cmdq));

[Severity: High]
Does this cause the CPU to stall until a timer event?

When the hypervisor inserts a batch of commands that exceeds the command queue size, smmu_add_cmd() waits for queue space and passes smmu->features & ARM_SMMU_FEAT_SEV as the __use_wfe argument to smmu_wait(), executing wfe().

However, the SMMU only generates an SEV on a CMD_SYNC with CS_SEV. Since hw_lock prevents concurrent insertions and the hypervisor only inserts CMD_SYNC at the end of the batch, no SEVs are generated while the queue drains. The CPU sleeps in wfe() and relies solely on the timer event stream to wake up, introducing massive artificial delays. Stalling EL2 with interrupts masked for milliseconds degrades system stability and risks triggering timeouts.

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	if (ret)
+		return ret;
+
+	smmu_add_cmd_raw(smmu, cmd->data);
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+	return 0;
+}
+
+static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu)
+{

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_unshare_pages(phys_addr_t addr, size_t size) · line 178

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	int ret;
+	struct arm_smmu_cmd cmd;
+
+	hyp_assert_lock_held(&smmu->hw_lock);
+	cmd = arm_smmu_make_cmd_sync(CMDQ_SYNC_0_CS_SEV, 0);
+	ret = smmu_add_cmd(smmu, &cmd);
+	if (ret)
+		return ret;
+
+	return smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));

[Severity: High]
Is the WFE logic swapped between smmu_add_cmd() and smmu_sync_cmd()?

Here, smmu_sync_cmd() inserts a CMD_SYNC with CS_SEV, which will explicitly generate an SEV upon completion. Yet it passes false for __use_wfe to smmu_wait(), forcing a busy-wait (cpu_relax()) and ignoring the precise hardware SEV wakeup it requested.

Quoted 1 line

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#59 lore raw

Re: [PATCH v9 18/25] iommu/arm-smmu-v3-kvm: Shadow STEs

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] hyp-arm64: fatal WARN_ON(skip) on normal pkvm_iommu_alloc_pages() -ENOMEM failure - [High]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hyp-arm64: fatal WARN_ON(skip) on normal pkvm_iommu_alloc_pages() -ENOMEM failure
- [High] iommu/arm-smmu-v3: torn STE write sets Valid bit before configuration --

commit bc7189831be67d30110e4ed04545c980d138dc99
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow STEs

Shadow the STE when the host sends CFGI_STE: copy the host entry as is into the hypervisor-owned stream table, which is the one the hardware uses. For 2-level tables the host L2 table is shared and pinned while the entry is read, and the hypervisor L2 table is allocated from the IOMMU pool the first time its L1 slot is used.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 0fa98915e00d4..132f3a9ba3084 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -290,6 +293,91 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)

[ ... ]

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool leaf)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 366

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +7 -0
+		/* No hypervisor entry, first time the L2 is populated. */
+		if (!l1tab[l1_idx].l2ptr) {
+			struct arm_smmu_strtab_l2 *l2table;
+
+			l2table = pkvm_iommu_alloc_pages(get_order(sizeof(*l2table)));
+			if (!l2table)
+				return -ENOMEM;

[Severity: High]
If this allocation fails, smmu_reshadow_ste() returns -ENOMEM, which causes smmu_filter_command() to return true. In smmu_emulate_cmdq_insert(), this triggers a preexisting WARN_ON(skip):

static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)

{
        ...
        skip = smmu_filter_command(smmu, cmd);
        if (WARN_ON(skip))
                continue;
        ...
}

Since WARN_ON() expands to a fatal BRK at EL2, does this allow a normal out-of-memory condition to panic the hypervisor?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 377

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +11 -0
+			/* Ensure L2 is observed before writing the ptr. */
+			dma_wmb();
+			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)];
+	}
+
+	for (i = 0; i < STRTAB_STE_DWORDS; ++i)
+		WRITE_ONCE(hyp_ste_ptr->data[i], host_ste_ptr->data[i]);

[Severity: High]
Does this copy sequence write the Valid bit (bit 0 of DWord 0) before the rest of the stream table entry is fully populated?

Because the loop starts at i = 0, hyp_ste_ptr->data[0] is written first. If the hardware concurrently fetches this entry, could it read a Valid STE while DWords 1 through 7 still contain uninitialized data?

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
#49 lore raw

Re: [PATCH v9 16/25] iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] iommu/arm-smmu-v3-kvm: user-triggerable hyp panic via unhandled SMMU commands (e.g.…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3-kvm: user-triggerable hyp panic via unhandled SMMU commands (e.g., CMDQ_OP_ATC_INV)
- [Critical] iommu/arm-smmu-v3-kvm: CMD_SYNC filter allows arbitrary memory writes via SIG32
- [High] iommu: EL2 panic on SMMU hardware timeout or CERROR due to WARN_ON
- [Medium] arm-smmu-v3-kvm: Dead-branch traps due to WARN_ON() panicking at EL2
--

commit 44f2b5e9d53208cadd7d659b59bf05189d20f72d
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

This commit introduces command queue emulation for the host in the ARM SMMUv3 pKVM driver. It filters command queue entries, shadows the queue, and prevents the host from directly manipulating the hardware command queue to ensure isolation.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index c9fc21e405e79..dc9b1e659a422 100644

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *command)
+{
+	u64 type = FIELD_GET(CMDQ_0_OP, command[0]);
+
+	switch (type) {

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 374

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+	case CMDQ_OP_TLBI_NH_ASID:
+	case CMDQ_OP_TLBI_NH_VA:
+	case CMDQ_OP_TLBI_NH_ALL:
+	case 0x13: /* CMD_TLBI_NH_VAA: Not used by Linux */
+	{
+		/* Only allow VMID = 0 */
+		if (FIELD_GET(CMDQ_TLBI_0_VMID, command[0]) != 0)
+			return true;

[Severity: Critical]
Could this reject valid nested-translation TLBI commands? A host userspace process with IOMMUFD nested translation might legitimately trigger invalidation commands (like CMDQ_OP_TLBI_NH_VA) where the host injects a non-zero VMID.

Because this returns true, the command gets rejected, which later leads to a hypervisor panic (see below) triggered directly from userspace.

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+		break;
+	}
+	case CMDQ_OP_PREFETCH_CFG:
+	case CMDQ_OP_CFGI_CD:
+	case CMDQ_OP_CFGI_CD_ALL:
+	case CMDQ_OP_TLBI_NSNH_ALL:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 390

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	case CMDQ_OP_PRI_RESP:
+	case CMDQ_OP_RESUME:
+		break;
+	case CMDQ_OP_CMD_SYNC:
+		if (FIELD_GET(CMDQ_SYNC_0_CS, command[0]) == CMDQ_SYNC_0_CS_IRQ) {
+			/* Do not allow MSI */
+			command[0] &= ~CMDQ_SYNC_0_CS;
+			command[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
+			command[1] &= ~CMDQ_SYNC_1_MSIADDR_MASK;
+		}

[Severity: Critical]
Does this allow the host to inject an SMMU hardware instruction that executes an arbitrary physical memory write? The filter specifically neutralizes CS=1 (IRQ), but it appears to ignore CS=3 (SIG32). If a malicious host issues a CMD_SYNC with CS=3, the hardware might perform a 32-bit physical memory write of the host-provided MSIData to the host-provided MSIAddress, compromising the hypervisor or guest memory.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 394

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+		break;
+	default:
+		/* Deny unknown commands */
+		return true;

[Severity: Critical]
Could valid user-triggerable commands hit this default case? For example, CMDQ_OP_ATC_INV appears to be missing from the allowlist. Since it returns true, this will also result in a user-triggerable hypervisor panic when the caller evaluates the rejection.

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +7 -0
+	}
+
+	return false;
+}
+
+static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 433

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+		skip = smmu_filter_command(smmu, cmd);
+		if (WARN_ON(skip))
+			continue;

[Severity: Medium]
Will this execute the continue statement? At EL2 in nVHE/pKVM, WARN_ON() expands to a BRK instruction and triggers a fatal hypervisor panic. Because the macro doesn't return, the error-handling path is dead code, and rejecting a command results in a hard panic.

This also elevates the severity of the filter rejections mentioned above. If userspace can trigger a rejected command (like CMDQ_OP_ATC_INV or a nested TLBI with VMID != 0), it becomes a Denial of Service attack against the host system.

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
+		smmu_add_cmd_raw(smmu, cmd);
+	}
+
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+
+	ret = smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));
+	hyp_spin_unlock(&smmu->hw_lock);
+	return ret;
+}

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
+			     struct user_pt_regs *regs,
+			     u64 esr, u32 off)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 507

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+	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));

[Severity: High]
Can a hardware timeout or CERROR cause a hypervisor panic here? smmu_emulate_cmdq_insert() relies on smmu_wait() to check if the hardware command queue is empty. If the SMMU hardware halts due to an error or takes too long under heavy load, smmu_wait() returns -ETIMEDOUT.

Because the return value is wrapped in WARN_ON(), this timeout will be converted into a fatal EL2 panic instead of propagating the error.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 518

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +11 -0
+			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;

[Severity: Medium]
Similar to the dead-branch trap above, the break statement here appears to be unreachable code. Because WARN_ON() expands to a fatal BUG() at EL2, writing to the CMDQ_CONS register when the queue is enabled will panic the hypervisor instead of just ignoring the write.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+
+			smmu->cmdq_host.llq.cons = val;
+			goto out_ret;
+		} else {

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#31 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu: mmio emulation bitwise logic corrupted on big-endian hosts --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu: mmio emulation bitwise logic corrupted on big-endian hosts --

commit 5a437530a55c608782e029a478141ad9b342c024
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add MMIO emulation

This commit adds a data abort handler for the SMMUs to handle MMIO emulation. Currently, most registers act as a passthrough, and upcoming patches will add CMDQ/STE emulation with logic for specific register access.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -128,6 +132,170 @@ static int smmu_init(void)
+static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
+			     struct user_pt_regs *regs,
+			     u64 esr, u32 off)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 147

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+	bool is_xzr = (rd == 31);
+	u64 val = is_xzr ? 0 : regs->regs[rd];

[Severity: High]
Does extracting the register value directly from the CPU context here bypass endianness conversion on big-endian hosts?

When the host (EL1) uses writel_relaxed() on a big-endian machine, it byte-swaps the native value to little-endian representation before issuing the store. The MMIO trap then retrieves this raw, byte-swapped value directly into val.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+
+	switch (off) {
+	case ARM_SMMU_IDR0:

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 175

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+	case ARM_SMMU_CR0:
+		if (len != sizeof(u32))
+			break;
+
+		if (is_write) {
+			/* Enforce ATS checks. */
+			if (smmu->features & ARM_SMMU_FEAT_ATS)
+				val |= CR0_ATSCHK;

[Severity: High]
Can this bitwise operation corrupt the configuration on big-endian hosts?

Because val contains a byte-swapped value, applying a native bitmask like CR0_ATSCHK modifies bits at the wrong offset.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+			mask = read_write;
+		} else {
+			mask = read_write & ~CR0_ATSCHK;
+		}
+		break;

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 260

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+	if (is_write) {
+		if (len == sizeof(u64))
+			writeq_relaxed(val & mask, smmu->base + off);
+		else
+			writel_relaxed(val & mask, smmu->base + off);

[Severity: High]
Will this double-swap the corrupted value?

Since val was already byte-swapped by the host's write, writing it back using EL2's writel_relaxed() applies a second byte-swap, propagating the incorrect state to the SMMU hardware.

Read paths face a similar issue where the readl_relaxed() output is passed directly to the guest without being swapped back into the format expected by the guest's own le32_to_cpu() calls.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+
+		hyp_spin_unlock(&smmu->hw_lock);
+		return true;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#54 lore raw

Re: [PATCH v9 17/25] iommu/arm-smmu-v3-kvm: Shadow stream table

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] iommu/arm-smmu: missing length check for STRTAB_BASE corrupts hyp state - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] iommu/arm-smmu: missing length check for STRTAB_BASE corrupts hyp state
- [Low] iommu/arm-smmu-v3-kvm: Incorrect WARN_ON usage panics hypervisor on invalid host config --

commit 9c237aa634fe8fc969bb3e8a3f07ec53bd303b77
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow stream table

This commit allocates the shadow stream table per SMMU, sized up to 1MB for the 2-level host table case. Host writes remain passthrough for now, and the stream table is shared/unshared on SMMU enable/disable.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -441,6 +502,46 @@ static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu) · line 538

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
+	if (WARN_ON(!is_cmdq_enabled(smmu)))
+		return;
+
+	WARN_ON(smmu_update_ste_shadow(smmu, true));
+}

[Severity: Low]
Does this panic the hypervisor on invalid host configuration? These assertions evaluate host-supplied states and configurations, which causes a fatal EL2 BUG() when the host makes an invalid transition.

According to the pKVM Threat Model, host availability against itself is not guaranteed, making this a hardening improvement rather than a security bug. However, per the pKVM WARN_ON semantics guide, WARN_ON must only be used for EL2 internal invariant violations. Validating host-supplied inputs with WARN_ON is a correctness violation, as the hypervisor should gracefully handle or ignore invalid state transitions rather than panicking.

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 639

Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
@@ -528,7 +629,23 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		}
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_STRTAB_BASE:
+		if (is_write) {
+			/* Must only be written when SMMU_CR0.SMMUEN == 0. */
+			if (is_smmu_enabled(smmu))
+				break;
+			smmu->host_ste_base = val;
+		}
+		mask = read_write;
+		break;

[Severity: High]
Is a length check missing here? The missing len == sizeof(u64) check allows a 32-bit access to capture the full 64-bit CPU register, corrupting host_ste_base.

A 32-bit write to STRTAB_BASE will assign the entire 64-bit regs->regs[rd] (including uninitialized upper 32 bits) to smmu->host_ste_base. Unlike the ARM_SMMU_CMDQ_BASE trap handler which explicitly ensures the access is the correct width (if (len != sizeof(u64)) break;), this handler omits the validation.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 648

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
 	case ARM_SMMU_STRTAB_BASE_CFG:
+		if (is_write) {
+			/* Must only be written when SMMU_CR0.SMMUEN == 0. */
+			if (is_smmu_enabled(smmu))
+				break;
+			smmu->host_ste_cfg = val;
+		}
+		mask = read_write;
+		break;

[Severity: High]
Similarly, is a length check missing for this 32-bit register? The missing len == sizeof(u32) check allows 64-bit writes to pass through to a 32-bit register offset.

A 64-bit write to STRTAB_BASE_CFG will invoke writeq_relaxed() on a 32-bit register offset (0x88), causing an unaligned or oversized write to Device memory which triggers a fatal EL2 alignment fault.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#69 lore raw

Re: [PATCH v9 22/25] iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/arm-smmu-v3: incorrect recovery from -EEXIST in smmu_host_stage2_idmap leaves holes in…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3: incorrect recovery from -EEXIST in smmu_host_stage2_idmap leaves holes in memory --

commit d265b84d43b0a3e480c297258952f561f0f39998
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

This commit mirrors changes to the host stage-2 page table into an identity mapped stage-2 for the SMMUv3. It handles memory mapped with PAGE_SIZE and MMIO mapped with the largest block that fits.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 943ffef0bb4aa..75b2b1477a4fa 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -1011,8 +1070,100 @@ static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)

[ ... ]

Quoted 19 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +19 -0
+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;

[ ... ]

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
+			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;
+

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr) · line 1141

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+			if (ret == -EEXIST) {
+				/*
+				 * It is possible to get EEXIST when a VM dies with pages
+				 * in a shared state.
+				 */
+				ret = 0;
+				size -= pgsize;
+				start += pgsize;
+				continue;
+			}

[Severity: High]
Does this error handling skip valid pages when a collision occurs later in the batch?

The io-pgtable-arm backend implementation of map_pages validates all entries in the batch before writing any PTEs:

drivers/iommu/io-pgtable-arm.c:arm_lpae_init_pte() {
	for (i = 0; i < num_entries; i++)
		if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
			/* We require an unmap first */
			WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
			return -EEXIST;
		}
}

If a collision is found at any index (for example, the 5th page of the batch), map_pages returns -EEXIST immediately without mapping anything, leaving mapped at 0.

Because mapped is 0, this code assumes the very first page of the batch caused the collision, skips it by doing start += pgsize, and retries the loop. This process repeatedly skips earlier non-colliding pages without mapping them, leaving holes in the SMMU stage 2 identity map and causing translation faults during host DMA.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+			if (!mapped || ret)
+				break;
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#75 lore raw

Re: [PATCH v9 24/25] iommu/arm-smmu-v3-kvm: Enable nesting

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] iommu/arm-smmu-v3: fatal EL2 panic on host-supplied invalid STE or ENOMEM - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3: fatal EL2 panic on host-supplied invalid STE or ENOMEM
- [Medium] arm-smmu-v3-kvm: Incorrect EL2 WARN_ON usage for hardware timeout --

commit 5625f7b65b03ea88554c9ce7668b1dabbbb068b1
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Enable nesting

This commit enables stage-2 translation for the ARM SMMUv3 under pKVM by allowing the hypervisor to sanitize and control the stage-2 portion of the Stream Table Entry (STE).

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 01f23fce6be50..3562bb222f53e 100644

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
@@ -443,6 +443,60 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
+static int smmu_attach_stage_2(struct hyp_arm_smmu_v3_device *smmu,
+			       struct arm_smmu_ste *ste)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 461

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+	/* S2 is not advertised, that should never be attempted. */
+	if ((cfg == STRTAB_STE_0_CFG_S2_TRANS) ||
+	    (cfg == STRTAB_STE_0_CFG_NESTED))
+		return -EINVAL;

[Severity: Critical]
Can this error return lead to a hypervisor panic?

When smmu_attach_stage_2() returns -EINVAL, the error propagates to smmu_filter_command(), which evaluates it and returns true to indicate the command should be skipped.

The preexisting caller smmu_emulate_cmdq_insert() then processes this skip condition using WARN_ON:

smmu_emulate_cmdq_insert() {
    ...
    skip = smmu_filter_command(smmu, cmd);
    if (WARN_ON(skip))
        continue;
}

At EL2 under pKVM, WARN_ON expands to a fatal breakpoint. Since the host can supply an invalid STE configuration that triggers this -EINVAL, could this allow the host to maliciously crash the hypervisor?

Similarly, allocating the L2 table in smmu_reshadow_ste() can fail and return -ENOMEM, which also propagates into this same fatal WARN_ON path during an out-of-memory event.

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, · line 542

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer)
+{
+	struct smmu_ste_writer *ste_writer =
+		container_of(writer, struct smmu_ste_writer, writer);
+	struct arm_smmu_cmd cfgi_cmd = arm_smmu_make_cmd_cfgi_ste(ste_writer->sid, true);
+
+	WARN_ON(smmu_send_cmd(ste_writer->smmu, &cfgi_cmd));
+}

[Severity: Medium]
Is it safe to use WARN_ON for hardware interactions that might time out?

If the SMMU command queue is full and smmu_send_cmd() times out waiting for it to drain, it will return -ETIMEDOUT. Because WARN_ON is fatal at EL2 under pKVM, this transforms a transient hardware latency or a heavy command load into a hypervisor panic.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#17 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] iommu/arm-smmu-v3: native driver fallback bypasses pKVM DMA isolation - [High] mm: UAF of smmu…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3: native driver fallback bypasses pKVM DMA isolation
- [High] mm: UAF of smmu due to devres LIFO inversion on deferred probe
- [Medium] iommu/arm-smmu-v3: aux device creation failure aborts initialization for remaining SMMUs --

commit 989c8869198ba4ebe30e4fc7554bb66910985d0b
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add the kernel driver

When KVM runs in protected mode with CONFIG_ARM_SMMU_V3_PKVM, the hypervisor owns the SMMUv3 hardware and presents emulated SMMUs to the host through trap and emulate. This patch adds the host side of the driver, using an auxiliary device approach for the emulated SMMUs.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +0 -0
new file mode 100644
index 0000000000000..5c5f0ff455e3b

[ ... ]

Quoted 21 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +21 -0
+static int __init kvm_arm_smmu_v3_register(void)
+{
+	size_t nr_pages = smmu_hyp_pgt_pages();
+	int ret;
+
+	if (!is_protected_kvm_enabled() || !nr_pages)
+		return 0;
+
+	ret = kvm_arm_smmu_array_alloc();
+	if (ret)
+		goto out_err;
+
+	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);
+	if (ret)
+		goto out_free;
+
+	ret = pkvm_iommu_register_driver(&kvm_nvhe_sym(smmu_ops), nr_pages);
+	if (ret)
+		goto out_unregister;
+
+	/*

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 134

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +10 -0
+	 * These variables are stored in the nVHE image, and won't be accessible
+	 * after KVM initialization. Ownership of kvm_arm_smmu_array will be
+	 * transferred to the hypervisor as well.
+	 */
+	kvm_hyp_arm_smmu_v3_smmus = kvm_arm_smmu_array;
+	kvm_hyp_arm_smmu_v3_count = kvm_arm_smmu_cur;
+	return ret;
+
+out_unregister:
+	platform_driver_unregister(&smmuv3_nesting_driver);

[Severity: Critical]
Does this fail-open behavior bypass pKVM DMA isolation?

If an SMMU is rejected by smmuv3_nesting_probe() (e.g. due to Cavium errata or unaligned MMIO), or if pkvm_iommu_register_driver() fails, the affected SMMU devices are left unbound or the nesting driver is unregistered.

Later, the native arm_smmu_driver is registered unconditionally in arm-smmu-v3.c and will successfully bind to these unbound SMMU devices. Since the hypervisor wasn't given the MMIO regions for the skipped SMMUs, it does not enforce stage-2 trapping on them. The host EL1 could then program these native SMMUs to perform unrestricted DMA into pKVM-protected guest or hypervisor memory, compromising protected VMs.

Quoted 21 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +21 -0
+out_free:
+	kvm_arm_smmu_array_free();
+out_err:
+	kvm_arm_smmu_count = 0;
+	kvm_arm_smmu_array = NULL;
+	return ret;
+}
+
+static int __init smmu_create_aux_device(struct device *dev, void *data)
+{
+	static int dev_id;
+	struct auxiliary_device *auxdev;
+
+	auxdev = __devm_auxiliary_device_create(dev, "protected_kvm",
+						"smmu_v3_emu", NULL, dev_id++);
+	if (!auxdev)
+		return -ENODEV;
+	return 0;
+}
+
+static int __init kvm_arm_smmu_v3_post_init(void)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 165

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +10 -0
+{
+	if (!kvm_arm_smmu_count)
+		return 0;
+
+	/*
+	 * If KVM failed to initialise, the host stage-2 is never enabled and
+	 * the SMMUs are driven natively through the same path.
+	 */
+	WARN_ON(driver_for_each_device(&smmuv3_nesting_driver.driver, NULL,
+				       NULL, smmu_create_aux_device));

[Severity: Medium]
Does this halt initialization for all remaining SMMUs if one fails?

In kvm_arm_smmu_v3_post_init(), driver_for_each_device() iterates over all bound SMMUs. If smmu_create_aux_device() fails (e.g. due to an ID collision or memory allocation failure in __devm_auxiliary_device_create()), it returns -ENODEV.

The core driver_for_each_device() function halts iteration and returns immediately upon receiving a non-zero value. Consequently, if a single SMMU fails aux device creation, all subsequent SMMUs are silently skipped. These skipped SMMUs won't be probed by the host's arm_smmu_driver_emu, breaking DMA translation for any peripherals behind them.

Should the callback log the error and return 0 to allow the remaining devices to initialize?

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index c2ec33c763a0f..66f38c54742b8 100644

[ ... ]

Quoted 14 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +14 -0
+static int arm_smmu_device_probe_emu(struct auxiliary_device *auxdev,
+				     const struct auxiliary_device_id *id)
+{
+	struct device *parent = auxdev->dev.parent;
+	void *group;
+	int ret;
+
+	dev_info(&auxdev->dev, "Probing from %s\n", dev_name(parent));
+	group = devres_open_group(parent, NULL, GFP_KERNEL);
+	if (!group)
+		return -ENOMEM;
+
+	ret = arm_smmu_device_probe(to_platform_device(parent));
+	if (ret)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5305

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
+		devres_release_group(parent, group);
+	else
+		devres_close_group(parent, group);
+	return ret;
+}
+
+static void arm_smmu_device_remove_emu(struct auxiliary_device *auxdev)
+{
+	arm_smmu_device_remove(to_platform_device(auxdev->dev.parent));
+}

[Severity: High]
Can this lead to a use-after-free of the smmu struct due to devres LIFO inversion during a deferred probe retry?

If arm_smmu_device_probe() returns -EPROBE_DEFER (e.g. from iommu_device_register() -> bus_iommu_probe() -> probe_iommu_group()), arm_smmu_device_probe_emu() correctly cleans up and returns the error, causing the auxiliary bus to defer the probe.

However, __devm_auxiliary_device_create() has already proceeded to register the auxiliary_device_destroy action on the parent's devres list. Later, when the deferred probe retries and succeeds, arm_smmu_device_probe() allocates the smmu struct via devm_kzalloc() and attaches it to the parent's devres list *after* the destroy action.

On parent platform device removal (e.g. via device tree overlay removal or system teardown), devres_release_all(parent) destroys devres actions in LIFO order. It first frees the smmu struct, and subsequently executes auxiliary_device_destroy. This unregisters the aux device and triggers arm_smmu_device_remove_emu(), which then dereferences the freed smmu pointer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#12 lore raw

Re: [PATCH v9 08/25] KVM: arm64: iommu: Add memory pool

In reply to [email protected]
Quoted 40 lines · On Thu, Oct 15, 2026 at 08:18:17PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: fatal EL2 panic on IOMMU pool exhaustion
--

commit 4301acf34cc06e2a4b1fab6b934f06ba07d48a9d
Author: Mostafa Saleh <[email protected]>
Subject: KVM: arm64: iommu: Add memory pool

This patch reserves a memory pool for the IOMMU driver's shadow page tables early during boot based on the kvm-arm.iommu_pgt_mem command line parameter. The driver later allocates from and frees to this pool at runtime in protected KVM mode.

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0
@@ -115,6 +117,11 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)

arch/arm64/kvm/hyp/nvhe/iommu.c int pkvm_iommu_init(void) · line 127

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +5 -0
 		return 0;
 
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = pkvm_iommu_ops->init();
 	if (ret)
 		return ret;

[Severity: High]
Will this trigger a fatal hypervisor panic if the host specifies an undersized pool?

The size of the newly introduced iommu_pages_pool is directly controlled by the host via the kvm-arm.iommu_pgt_mem command line parameter. If the host-provided pool size is too small, pkvm_iommu_snapshot_host_stage2() called immediately after this will exhaust the allocator and return -ENOMEM.

Because pkvm_iommu_init() wraps that call in a WARN_ON():

Quoted 8 lines
	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

and WARN_ON() at EL2 in nVHE/pKVM expands to a fatal BRK instruction, this exhaustion will crash the entire machine.

Could this be updated to propagate the error back to the host, instead of treating hyp_alloc_pages() failure as a fatal invariant violation?

That is by design as explaind the same reply in v8:
https://lore.kernel.org/all/[email protected]/

Thanks,
Mostafa

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#18 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to [email protected]
Quoted 63 lines · On Thu, Oct 15, 2026 at 08:35:03PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3: native driver fallback bypasses pKVM DMA isolation
- [High] mm: UAF of smmu due to devres LIFO inversion on deferred probe
- [Medium] iommu/arm-smmu-v3: aux device creation failure aborts initialization for remaining SMMUs --

commit 989c8869198ba4ebe30e4fc7554bb66910985d0b
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add the kernel driver

When KVM runs in protected mode with CONFIG_ARM_SMMU_V3_PKVM, the hypervisor owns the SMMUv3 hardware and presents emulated SMMUs to the host through trap and emulate. This patch adds the host side of the driver, using an auxiliary device approach for the emulated SMMUs.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +0 -0
new file mode 100644
index 0000000000000..5c5f0ff455e3b

[ ... ]

Quoted 21 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +21 -0
+static int __init kvm_arm_smmu_v3_register(void)
+{
+	size_t nr_pages = smmu_hyp_pgt_pages();
+	int ret;
+
+	if (!is_protected_kvm_enabled() || !nr_pages)
+		return 0;
+
+	ret = kvm_arm_smmu_array_alloc();
+	if (ret)
+		goto out_err;
+
+	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);
+	if (ret)
+		goto out_free;
+
+	ret = pkvm_iommu_register_driver(&kvm_nvhe_sym(smmu_ops), nr_pages);
+	if (ret)
+		goto out_unregister;
+
+	/*

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 134

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +10 -0
+	 * These variables are stored in the nVHE image, and won't be accessible
+	 * after KVM initialization. Ownership of kvm_arm_smmu_array will be
+	 * transferred to the hypervisor as well.
+	 */
+	kvm_hyp_arm_smmu_v3_smmus = kvm_arm_smmu_array;
+	kvm_hyp_arm_smmu_v3_count = kvm_arm_smmu_cur;
+	return ret;
+
+out_unregister:
+	platform_driver_unregister(&smmuv3_nesting_driver);

[Severity: Critical]
Does this fail-open behavior bypass pKVM DMA isolation?

If an SMMU is rejected by smmuv3_nesting_probe() (e.g. due to Cavium errata or unaligned MMIO), or if pkvm_iommu_register_driver() fails, the affected SMMU devices are left unbound or the nesting driver is unregistered.

Quoted 7 lines

Later, the native arm_smmu_driver is registered unconditionally in arm-smmu-v3.c and will successfully bind to these unbound SMMU devices. Since the hypervisor wasn't given the MMIO regions for the skipped SMMUs, it does not enforce stage-2 trapping on them. The host EL1 could then program these native SMMUs to perform unrestricted DMA into pKVM-protected guest or hypervisor memory, compromising protected VMs.

Also by design as replied in v8:
https://lore.kernel.org/all/[email protected]/

Quoted 40 lines
Quoted 21 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +21 -0
+out_free:
+	kvm_arm_smmu_array_free();
+out_err:
+	kvm_arm_smmu_count = 0;
+	kvm_arm_smmu_array = NULL;
+	return ret;
+}
+
+static int __init smmu_create_aux_device(struct device *dev, void *data)
+{
+	static int dev_id;
+	struct auxiliary_device *auxdev;
+
+	auxdev = __devm_auxiliary_device_create(dev, "protected_kvm",
+						"smmu_v3_emu", NULL, dev_id++);
+	if (!auxdev)
+		return -ENODEV;
+	return 0;
+}
+
+static int __init kvm_arm_smmu_v3_post_init(void)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 165

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +10 -0
+{
+	if (!kvm_arm_smmu_count)
+		return 0;
+
+	/*
+	 * If KVM failed to initialise, the host stage-2 is never enabled and
+	 * the SMMUs are driven natively through the same path.
+	 */
+	WARN_ON(driver_for_each_device(&smmuv3_nesting_driver.driver, NULL,
+				       NULL, smmu_create_aux_device));

[Severity: Medium]
Does this halt initialization for all remaining SMMUs if one fails?

In kvm_arm_smmu_v3_post_init(), driver_for_each_device() iterates over all bound SMMUs. If smmu_create_aux_device() fails (e.g. due to an ID collision or memory allocation failure in __devm_auxiliary_device_create()), it returns -ENODEV.

Quoted 9 lines

The core driver_for_each_device() function halts iteration and returns immediately upon receiving a non-zero value. Consequently, if a single SMMU fails aux device creation, all subsequent SMMUs are silently skipped. These skipped SMMUs won't be probed by the host's arm_smmu_driver_emu, breaking DMA translation for any peripherals behind them.

Should the callback log the error and return 0 to allow the remaining devices to initialize?

If anyone fails the system is not trusted so there is no point of creating the others.

Quoted 47 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index c2ec33c763a0f..66f38c54742b8 100644

[ ... ]

Quoted 14 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +14 -0
+static int arm_smmu_device_probe_emu(struct auxiliary_device *auxdev,
+				     const struct auxiliary_device_id *id)
+{
+	struct device *parent = auxdev->dev.parent;
+	void *group;
+	int ret;
+
+	dev_info(&auxdev->dev, "Probing from %s\n", dev_name(parent));
+	group = devres_open_group(parent, NULL, GFP_KERNEL);
+	if (!group)
+		return -ENOMEM;
+
+	ret = arm_smmu_device_probe(to_platform_device(parent));
+	if (ret)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5305

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
+		devres_release_group(parent, group);
+	else
+		devres_close_group(parent, group);
+	return ret;
+}
+
+static void arm_smmu_device_remove_emu(struct auxiliary_device *auxdev)
+{
+	arm_smmu_device_remove(to_platform_device(auxdev->dev.parent));
+}

[Severity: High]
Can this lead to a use-after-free of the smmu struct due to devres LIFO inversion during a deferred probe retry?

If arm_smmu_device_probe() returns -EPROBE_DEFER (e.g. from iommu_device_register() -> bus_iommu_probe() -> probe_iommu_group()), arm_smmu_device_probe_emu() correctly cleans up and returns the error, causing the auxiliary bus to defer the probe.

However, __devm_auxiliary_device_create() has already proceeded to register the auxiliary_device_destroy action on the parent's devres list. Later, when the deferred probe retries and succeeds, arm_smmu_device_probe() allocates the smmu struct via devm_kzalloc() and attaches it to the parent's devres list *after* the destroy action.

Quoted 5 lines

On parent platform device removal (e.g. via device tree overlay removal or system teardown), devres_release_all(parent) destroys devres actions in LIFO order. It first frees the smmu struct, and subsequently executes auxiliary_device_destroy. This unregisters the aux device and triggers arm_smmu_device_remove_emu(), which then dereferences the freed smmu pointer.

My assumption is that the platform device can't be removed as the comment above, the overlay support has no in-kernel user nor a uAPI.

Not sure what does "system teardown" mean, but shutdown won't call devres_release_all(). So I believe there is not path to remove the platform device in that case.

However, as hardening, I might just call auxiliary_device_create() instead of __devm_auxiliary_device_create().

Thanks,
Mostafa

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#32 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to [email protected]
Quoted 23 lines · On Thu, Oct 15, 2026 at 08:30:38PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu: mmio emulation bitwise logic corrupted on big-endian hosts --

commit 5a437530a55c608782e029a478141ad9b342c024
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add MMIO emulation

This commit adds a data abort handler for the SMMUs to handle MMIO emulation. Currently, most registers act as a passthrough, and upcoming patches will add CMDQ/STE emulation with logic for specific register access.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -128,6 +132,170 @@ static int smmu_init(void)
+static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
+			     struct user_pt_regs *regs,
+			     u64 esr, u32 off)
+{

[ ... ]

Quoted 6 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 147

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+	bool is_xzr = (rd == 31);
+	u64 val = is_xzr ? 0 : regs->regs[rd];

[Severity: High]
Does extracting the register value directly from the CPU context here bypass endianness conversion on big-endian hosts?

mmm, I haven't considered that before.
However, BE support was removed on arm64 [1]
https://lore.kernel.org/all/[email protected]/

So, I'd say keep it as is.

Quoted 22 lines

When the host (EL1) uses writel_relaxed() on a big-endian machine, it byte-swaps the native value to little-endian representation before issuing the store. The MMIO trap then retrieves this raw, byte-swapped value directly into val.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+
+	switch (off) {
+	case ARM_SMMU_IDR0:

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 175

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+	case ARM_SMMU_CR0:
+		if (len != sizeof(u32))
+			break;
+
+		if (is_write) {
+			/* Enforce ATS checks. */
+			if (smmu->features & ARM_SMMU_FEAT_ATS)
+				val |= CR0_ATSCHK;

[Severity: High]
Can this bitwise operation corrupt the configuration on big-endian hosts?

Quoted 3 lines

Because val contains a byte-swapped value, applying a native bitmask like CR0_ATSCHK modifies bits at the wrong offset.

Same as above.

Quoted 15 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+			mask = read_write;
+		} else {
+			mask = read_write & ~CR0_ATSCHK;
+		}
+		break;

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 260

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+	if (is_write) {
+		if (len == sizeof(u64))
+			writeq_relaxed(val & mask, smmu->base + off);
+		else
+			writel_relaxed(val & mask, smmu->base + off);

[Severity: High]
Will this double-swap the corrupted value?

Quoted 8 lines

Since val was already byte-swapped by the host's write, writing it back using EL2's writel_relaxed() applies a second byte-swap, propagating the incorrect state to the SMMU hardware.

Read paths face a similar issue where the readl_relaxed() output is passed directly to the guest without being swapped back into the format expected by the guest's own le32_to_cpu() calls.

Same as above.

Thanks,
Mostafa

Quoted 7 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+
+		hyp_spin_unlock(&smmu->hw_lock);
+		return true;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#39 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to [email protected]
Quoted 39 lines · On Thu, Oct 15, 2026 at 08:28:01PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] iommu/arm-smmu-v3-kvm: wrong WARN_ON() causes hyp panic on invalid host MMIO
- [Medium] iommu/arm-smmu-v3-kvm: WARN_ON used to validate host input in SMMU emulation --

commit bf6dcb76ab39be1d82600f47f81d772f4c08b9a6
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow the command queue

This patch allocates a shadow command queue per SMMU to be used by the hypervisor at boot. The host command queue is pinned in a shared state when enabled to avoid donation issues and prevent untrusted host access from reaching the hypervisor state.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 236

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
@@ -132,6 +225,23 @@ static int smmu_init(void)
 	return ret;
 }
 
+static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
+
+	smmu->cmdq_host.llq.max_n_shift = min(shift, smmu->cmdq_max_shift);
+	smmu->cmdq_host.base_dma = smmu->cmdq_host.q_base & Q_BASE_ADDR_MASK;
+	smmu->cmdq_host.base_dma &= ~(cmdq_size(&smmu->cmdq_host) - 1);
+	WARN_ON(smmu_share_pages(smmu->cmdq_host.base_dma,
+				 cmdq_size(&smmu->cmdq_host)));

[Severity: Medium]
Will this cause a fatal hypervisor panic if the host provides an invalid physical address or an unshareable memory range?

Quoted 6 lines

At EL2, the WARN_ON() macro behaves as a fatal exception. If the host configures the emulated SMMU with an invalid address, smmu_share_pages() can fail and return an error. Wrapping this fallible, host-influenced operation in WARN_ON() will crash the hypervisor. Could this path safely ignore the invalid configuration or return an error instead?

By design as replied in v8:
https://lore.kernel.org/all/[email protected]/

Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 242

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+}
+
+static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
+				   cmdq_size(&smmu->cmdq_host)));

[Severity: Medium]
Does this also have the same issue with WARN_ON() panicking the hypervisor if the host-provided memory range fails to unshare?

By design as above.

Quoted 20 lines
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+}
+

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 274

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -158,6 +268,14 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		break;
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_CMDQ_BASE:
+		if (is_write) {
+			/* Not allowed by the architecture */
+			if (is_cmdq_enabled(smmu))
+				break;

[Severity: Medium]
If the host kernel makes an invalid MMIO write to ARM_SMMU_CMDQ_BASE while the CMDQ is enabled, the switch statement breaks and leaves the mask set to 0.

Quoted 8 lines

Later in smmu_dabt_device(), there is a pre-existing check:

	if (WARN_ON(!mask))
		goto out_ret;

Since WARN_ON() is fatal at EL2, will this turn an invalid host MMIO access into a hypervisor panic? It seems like the access should be gracefully ignored or rejected without triggering a fatal assertion.

Yes, by design, if the host makes violates the architecture we just panic.

Thanks,
Mostafa

Quoted 8 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+			smmu->cmdq_host.q_base = val;
+		}
+		mask = read_write;
+		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#46 lore raw

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

In reply to [email protected]
Quoted 37 lines · On Thu, Oct 15, 2026 at 08:30:05PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3-kvm: swapped WFE logic causes queue polling delays --

commit 533e5a914cd22f171b26c2c5c0e2acfb11756ea5
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Add CMDQ functions

This commit adds command queue accessors for the ARM SMMUv3 in the hypervisor. It includes command queuing and synchronization functions, with a 1 ms polling timeout that sleeps in WFE when SEV is supported.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 02e7c61758329..c9fc21e405e79 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -77,6 +113,85 @@ static int smmu_unshare_pages(phys_addr_t addr, size_t size)

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_unshare_pages(phys_addr_t addr, size_t size) · line 158

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static int smmu_add_cmd(struct hyp_arm_smmu_v3_device *smmu,
+			struct arm_smmu_cmd *cmd)
+{
+	int ret;
+
+	hyp_assert_lock_held(&smmu->hw_lock);
+	ret = smmu_wait(smmu->features & ARM_SMMU_FEAT_SEV,
+			!smmu_cmdq_full(&smmu->cmdq));

[Severity: High]
Does this cause the CPU to stall until a timer event?

When the hypervisor inserts a batch of commands that exceeds the command queue size, smmu_add_cmd() waits for queue space and passes smmu->features & ARM_SMMU_FEAT_SEV as the __use_wfe argument to smmu_wait(), executing wfe().

Quoted 8 lines

However, the SMMU only generates an SEV on a CMD_SYNC with CS_SEV. Since hw_lock prevents concurrent insertions and the hypervisor only inserts CMD_SYNC at the end of the batch, no SEVs are generated while the queue drains. The CPU sleeps in wfe() and relies solely on the timer event stream to wake up, introducing massive artificial delays. Stalling EL2 with interrupts masked for milliseconds degrades system stability and risks triggering timeouts.

Not true, the SMMU generates an event when queue become non-full check:
arm_smmu_cmdq_poll_until_not_full()

Also in the spec: 4.1.2
When SMMU_IDR0.SEV == 1, the SMMU triggers a WFE wake-up event when a Command queue becomes non-full and an agent external to the SMMU could have observed that the queue was previously full.

Quoted 24 lines
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	if (ret)
+		return ret;
+
+	smmu_add_cmd_raw(smmu, cmd->data);
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+	return 0;
+}
+
+static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu)
+{

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_unshare_pages(phys_addr_t addr, size_t size) · line 178

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	int ret;
+	struct arm_smmu_cmd cmd;
+
+	hyp_assert_lock_held(&smmu->hw_lock);
+	cmd = arm_smmu_make_cmd_sync(CMDQ_SYNC_0_CS_SEV, 0);
+	ret = smmu_add_cmd(smmu, &cmd);
+	if (ret)
+		return ret;
+
+	return smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));

[Severity: High]
Is the WFE logic swapped between smmu_add_cmd() and smmu_sync_cmd()?

Quoted 4 lines

Here, smmu_sync_cmd() inserts a CMD_SYNC with CS_SEV, which will explicitly generate an SEV upon completion. Yet it passes false for __use_wfe to smmu_wait(), forcing a busy-wait (cpu_relax()) and ignoring the precise hardware SEV wakeup it requested.

I guess the sync command can leverage the WFE also, as I was focused on the empty condition when I wrote it, but that is just an optimization.

Thanks,
Mostafa

Quoted 5 lines
Quoted 2 lines

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#50 lore raw

Re: [PATCH v9 16/25] iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to [email protected]
Quoted 41 lines · On Thu, Oct 15, 2026 at 08:30:32PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3-kvm: user-triggerable hyp panic via unhandled SMMU commands (e.g., CMDQ_OP_ATC_INV)
- [Critical] iommu/arm-smmu-v3-kvm: CMD_SYNC filter allows arbitrary memory writes via SIG32
- [High] iommu: EL2 panic on SMMU hardware timeout or CERROR due to WARN_ON
- [Medium] arm-smmu-v3-kvm: Dead-branch traps due to WARN_ON() panicking at EL2
--

commit 44f2b5e9d53208cadd7d659b59bf05189d20f72d
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

This commit introduces command queue emulation for the host in the ARM SMMUv3 pKVM driver. It filters command queue entries, shadows the queue, and prevents the host from directly manipulating the hardware command queue to ensure isolation.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index c9fc21e405e79..dc9b1e659a422 100644

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+static bool smmu_filter_command(struct hyp_arm_smmu_v3_device *smmu, u64 *command)
+{
+	u64 type = FIELD_GET(CMDQ_0_OP, command[0]);
+
+	switch (type) {

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 374

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+	case CMDQ_OP_TLBI_NH_ASID:
+	case CMDQ_OP_TLBI_NH_VA:
+	case CMDQ_OP_TLBI_NH_ALL:
+	case 0x13: /* CMD_TLBI_NH_VAA: Not used by Linux */
+	{
+		/* Only allow VMID = 0 */
+		if (FIELD_GET(CMDQ_TLBI_0_VMID, command[0]) != 0)
+			return true;

[Severity: Critical]
Could this reject valid nested-translation TLBI commands? A host userspace process with IOMMUFD nested translation might legitimately trigger invalidation commands (like CMDQ_OP_TLBI_NH_VA) where the host injects a non-zero VMID.

Quoted 3 lines

Because this returns true, the command gets rejected, which later leads to a hypervisor panic (see below) triggered directly from userspace.

Wrong, the host kernel only sees a single stage SMMUv3, so it can't issue non zero VMID or IPA invalidations.

Quoted 17 lines
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+		break;
+	}
+	case CMDQ_OP_PREFETCH_CFG:
+	case CMDQ_OP_CFGI_CD:
+	case CMDQ_OP_CFGI_CD_ALL:
+	case CMDQ_OP_TLBI_NSNH_ALL:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 390

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	case CMDQ_OP_PRI_RESP:
+	case CMDQ_OP_RESUME:
+		break;
+	case CMDQ_OP_CMD_SYNC:
+		if (FIELD_GET(CMDQ_SYNC_0_CS, command[0]) == CMDQ_SYNC_0_CS_IRQ) {
+			/* Do not allow MSI */
+			command[0] &= ~CMDQ_SYNC_0_CS;
+			command[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
+			command[1] &= ~CMDQ_SYNC_1_MSIADDR_MASK;
+		}
Quoted 7 lines

[Severity: Critical]
Does this allow the host to inject an SMMU hardware instruction that executes an arbitrary physical memory write? The filter specifically neutralizes CS=1 (IRQ), but it appears to ignore CS=3 (SIG32). If a malicious host issues a CMD_SYNC with CS=3, the hardware might perform a 32-bit physical memory write of the host-provided MSIData to the host-provided MSIAddress, compromising the hypervisor or guest memory.

As mentioned in v8:
https://lore.kernel.org/all/[email protected]/

This is not true, there is no such encoding in the spec.

Quoted 12 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 394

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+		break;
+	default:
+		/* Deny unknown commands */
+		return true;

[Severity: Critical]
Could valid user-triggerable commands hit this default case? For example, CMDQ_OP_ATC_INV appears to be missing from the allowlist. Since it returns true, this will also result in a user-triggerable hypervisor panic when the caller evaluates the rejection.

ATS is hidden from IDR0, the kernel shouldn't attempt to use it.

Quoted 18 lines
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +7 -0
+	}
+
+	return false;
+}
+
+static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 433

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+		skip = smmu_filter_command(smmu, cmd);
+		if (WARN_ON(skip))
+			continue;

[Severity: Medium]
Will this execute the continue statement? At EL2 in nVHE/pKVM, WARN_ON() expands to a BRK instruction and triggers a fatal hypervisor panic. Because the macro doesn't return, the error-handling path is dead code, and rejecting a command results in a hard panic.

Quoted 5 lines

This also elevates the severity of the filter rejections mentioned above. If userspace can trigger a rejected command (like CMDQ_OP_ATC_INV or a nested TLBI with VMID != 0), it becomes a Denial of Service attack against the host system.

Again, by design, any unsupported command will panic, that can be relaxed, but that only happens if the kernel is malicous or buggy so I'd prefer to catch the place caused the issue to make that easier to debug.

Quoted 28 lines
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
+		smmu_add_cmd_raw(smmu, cmd);
+	}
+
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+
+	ret = smmu_wait(false, smmu_cmdq_empty(&smmu->cmdq));
+	hyp_spin_unlock(&smmu->hw_lock);
+	return ret;
+}

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
+			     struct user_pt_regs *regs,
+			     u64 esr, u32 off)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 507

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
+	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));

[Severity: High]
Can a hardware timeout or CERROR cause a hypervisor panic here? smmu_emulate_cmdq_insert() relies on smmu_wait() to check if the hardware command queue is empty. If the SMMU hardware halts due to an error or takes too long under heavy load, smmu_wait() returns -ETIMEDOUT.

Quoted 3 lines

Because the return value is wrapped in WARN_ON(), this timeout will be converted into a fatal EL2 panic instead of propagating the error.

Yes, if the HW hangs, we can't return as the TLB state might leak donated memory, so in that case we have to panic.

Quoted 9 lines
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
+			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) {
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 518

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+			if (WARN_ON(is_cmdq_enabled(smmu)))
+				break;

[Severity: Medium]
Similar to the dead-branch trap above, the break statement here appears to be unreachable code. Because WARN_ON() expands to a fatal BUG() at EL2, writing to the CMDQ_CONS register when the queue is enabled will panic the hypervisor instead of just ignoring the write.

Yes, that just looks cleaner, similar patterns used in mem_protect.c, compilers are smart now and will remove it.

Thanks,
Mostafa

Quoted 9 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+
+			smmu->cmdq_host.llq.cons = val;
+			goto out_ret;
+		} else {

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#55 lore raw

Re: [PATCH v9 17/25] iommu/arm-smmu-v3-kvm: Shadow stream table

In reply to [email protected]
Quoted 34 lines · On Thu, Oct 15, 2026 at 08:30:54PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] iommu/arm-smmu: missing length check for STRTAB_BASE corrupts hyp state
- [Low] iommu/arm-smmu-v3-kvm: Incorrect WARN_ON usage panics hypervisor on invalid host config --

commit 9c237aa634fe8fc969bb3e8a3f07ec53bd303b77
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow stream table

This commit allocates the shadow stream table per SMMU, sized up to 1MB for the 2-level host table case. Host writes remain passthrough for now, and the stream table is shared/unshared on SMMU enable/disable.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -441,6 +502,46 @@ static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu) · line 538

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
+	if (WARN_ON(!is_cmdq_enabled(smmu)))
+		return;
+
+	WARN_ON(smmu_update_ste_shadow(smmu, true));
+}

[Severity: Low]
Does this panic the hypervisor on invalid host configuration? These assertions evaluate host-supplied states and configurations, which causes a fatal EL2 BUG() when the host makes an invalid transition.

Quoted 6 lines

According to the pKVM Threat Model, host availability against itself is not guaranteed, making this a hardening improvement rather than a security bug. However, per the pKVM WARN_ON semantics guide, WARN_ON must only be used for EL2 internal invariant violations. Validating host-supplied inputs with WARN_ON is a correctness violation, as the hypervisor should gracefully handle or ignore invalid state transitions rather than panicking.

Same as replied in v8:
https://lore.kernel.org/all/[email protected]/

Quoted 20 lines

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 639

Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
@@ -528,7 +629,23 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		}
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_STRTAB_BASE:
+		if (is_write) {
+			/* Must only be written when SMMU_CR0.SMMUEN == 0. */
+			if (is_smmu_enabled(smmu))
+				break;
+			smmu->host_ste_base = val;
+		}
+		mask = read_write;
+		break;

[Severity: High]
Is a length check missing here? The missing len == sizeof(u64) check allows a 32-bit access to capture the full 64-bit CPU register, corrupting host_ste_base.

Quoted 6 lines

A 32-bit write to STRTAB_BASE will assign the entire 64-bit regs->regs[rd] (including uninitialized upper 32 bits) to smmu->host_ste_base. Unlike the ARM_SMMU_CMDQ_BASE trap handler which explicitly ensures the access is the correct width (if (len != sizeof(u64)) break;), this handler omits the validation.

Yes, for bisectability, that is fixed next.

Quoted 15 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 648

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
 	case ARM_SMMU_STRTAB_BASE_CFG:
+		if (is_write) {
+			/* Must only be written when SMMU_CR0.SMMUEN == 0. */
+			if (is_smmu_enabled(smmu))
+				break;
+			smmu->host_ste_cfg = val;
+		}
+		mask = read_write;
+		break;

[Severity: High]
Similarly, is a length check missing for this 32-bit register? The missing len == sizeof(u32) check allows 64-bit writes to pass through to a 32-bit register offset.

Quoted 3 lines

A 64-bit write to STRTAB_BASE_CFG will invoke writeq_relaxed() on a 32-bit register offset (0x88), causing an unaligned or oversized write to Device memory which triggers a fatal EL2 alignment fault.

Same as above.

Thanks,
Mostafa

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#60 lore raw

Re: [PATCH v9 18/25] iommu/arm-smmu-v3-kvm: Shadow STEs

In reply to [email protected]
Quoted 48 lines · On Thu, Oct 15, 2026 at 08:30:26PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hyp-arm64: fatal WARN_ON(skip) on normal pkvm_iommu_alloc_pages() -ENOMEM failure
- [High] iommu/arm-smmu-v3: torn STE write sets Valid bit before configuration --

commit bc7189831be67d30110e4ed04545c980d138dc99
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow STEs

Shadow the STE when the host sends CFGI_STE: copy the host entry as is into the hypervisor-owned stream table, which is the one the hardware uses. For 2-level tables the host L2 table is shared and pinned while the entry is read, and the hypervisor L2 table is allocated from the IOMMU pool the first time its L1 slot is used.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 0fa98915e00d4..132f3a9ba3084 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -290,6 +293,91 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)

[ ... ]

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+static int smmu_reshadow_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, bool leaf)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 366

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +7 -0
+		/* No hypervisor entry, first time the L2 is populated. */
+		if (!l1tab[l1_idx].l2ptr) {
+			struct arm_smmu_strtab_l2 *l2table;
+
+			l2table = pkvm_iommu_alloc_pages(get_order(sizeof(*l2table)));
+			if (!l2table)
+				return -ENOMEM;

[Severity: High]
If this allocation fails, smmu_reshadow_ste() returns -ENOMEM, which causes smmu_filter_command() to return true. In smmu_emulate_cmdq_insert(), this triggers a preexisting WARN_ON(skip):

static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)

{
        ...
        skip = smmu_filter_command(smmu, cmd);
        if (WARN_ON(skip))
                continue;
        ...
}
Quoted 3 lines

Since WARN_ON() expands to a fatal BRK at EL2, does this allow a normal out-of-memory condition to panic the hypervisor?

Allocation shouldn't fail as the carveout should cover the worst case.

Quoted 16 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 377

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +11 -0
+			/* Ensure L2 is observed before writing the ptr. */
+			dma_wmb();
+			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)];
+	}
+
+	for (i = 0; i < STRTAB_STE_DWORDS; ++i)
+		WRITE_ONCE(hyp_ste_ptr->data[i], host_ste_ptr->data[i]);

[Severity: High]
Does this copy sequence write the Valid bit (bit 0 of DWord 0) before the rest of the stream table entry is fully populated?

Quoted 4 lines

Because the loop starts at i = 0, hyp_ste_ptr->data[0] is written first. If the hardware concurrently fetches this entry, could it read a Valid STE while DWords 1 through 7 still contain uninitialized data?

This only copies the host STE (which follows the spec) for bisectability that is fixed next by using the ste writer.

Thanks,
Mostafa

Quoted 5 lines
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
#63 lore raw

Re: [PATCH v9 19/25] iommu/arm-smmu-v3-kvm: Share other queues

In reply to [email protected]
Quoted 36 lines · On Thu, Oct 15, 2026 at 08:29:08PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3-kvm: EL2 panic on kexec due to locked EVTQ/PRIQ base addresses --

commit bfd6e271a581b5d4148ccc397cc5a82480d46470
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Share other queues

This commit introduces pinning and sharing of the event and PRI queues in the hypervisor to prevent the host from writing to protected memory. It enforces that the host cannot change the base addresses of these queues once they are shared.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 132f3a9ba3084..1972df1d2a8dc 100644

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 863

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
@@ -806,6 +852,34 @@ 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) {
+			/* See ARM_SMMU_CR0 */
+			if (is_evtq_enabled(smmu) ||
+			    (smmu->evtq_shared && smmu->evtq_base != val))
+				break;

[Severity: High]
Does this rigid check prevent the host from performing a successful kexec?

Quoted 8 lines

When the host executes a kexec, the new kernel boots and initializes its own SMMUv3 driver, allocating new memory for the EVTQ and writing the new base address here. Since EL2 state persists across kexec and the hypervisor never clears smmu->evtq_shared upon disable, this condition evaluates to true.

The break leaves mask = 0, which then triggers the WARN_ON(!mask) at the end of the handler, resulting in a fatal hypervisor panic.

kexec is not supported in pKVM.

Quoted 19 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+			smmu->evtq_base = val;
+		}
+		mask = read_write;
+		break;

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 877

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+
+	case ARM_SMMU_PRIQ_BASE:
+		if (len != sizeof(u64))
+			break;
+
+		if (is_write) {
+			/* See ARM_SMMU_CR0 */
+			if (is_priq_enabled(smmu) ||
+			    (smmu->priq_shared && smmu->priq_base != val))
+				break;

[Severity: High]
Similarly, does this check for the PRIQ base address cause the hypervisor to panic during kexec?

Quoted 4 lines

Just like EVTQ, the new kernel initialized during kexec will write a new PRIQ base address, triggering this break and hitting the same fatal WARN_ON(!mask) further down.

Same as above.

Thanks,
Mostafa

Quoted 8 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+			smmu->priq_base = val;
+		}
+		mask = read_write;
+		break;
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#67 lore raw

Re: [PATCH v9 21/25] iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

In reply to [email protected]
Quoted 39 lines · On Thu, Oct 15, 2026 at 08:26:45PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] arch: layering violation by including SMMUv3 header in generic io-pgtable-arm.h --

commit 251567dd4618edfaa4984ef7282b71a0da331297
Author: Mostafa Saleh <[email protected]>

iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor

To be able to populate the shadow stage-2 in the hypervisor, compile io-pgtable-arm object for the hypervisor. This requires the hypervisor to provide the iommu-pages allocator, redefine the address conversion helpers, and stub out the DMA API.

[ ... ]

Quoted 5 lines
drivers/iommu/io-pgtable-arm.h +0 -0
index ba7cfdf7afa03..4278da41e406d 100644

drivers/iommu/io-pgtable-arm.h line 36

Quoted 10 lines
drivers/iommu/io-pgtable-arm.h +7 -0
@@ -27,4 +27,13 @@
 #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
 #define ARM_LPAE_TCR_PS_52_BIT		0x6ULL
 
+#ifndef __KVM_NVHE_HYPERVISOR__
+#include "iommu-pages.h"
+#define arm_lpae_alloc_data(obj)	kmalloc_obj(obj)
+#define arm_lpae_free_data(p)		kfree(p)
+
+#else
+#include "arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h"

[Severity: Low]
Is this an intended architecture layering violation? The generic ARM LPAE page table header drivers/iommu/io-pgtable-arm.h explicitly includes the driver-specific header arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h when compiled for the hypervisor. This tightly couples generic IOMMU framework code directly to the SMMUv3 driver path.

Quoted 4 lines

Should the hypervisor stubs be placed in a generic NVHE include directory (e.g., arch/arm64/kvm/hyp/include/nvhe/) to maintain subsystem independence, even if SMMUv3 is currently the only user?

No strong opinion, I think keeping it anywhere under drivers/iommu is fine.

Thanks,
Mostafa

Quoted 6 lines
Quoted 3 lines
drivers/iommu/io-pgtable-arm.h +2 -0
+#endif
+
 #endif /* IO_PGTABLE_ARM_H_ */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
#70 lore raw

Re: [PATCH v9 22/25] iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

In reply to [email protected]
Quoted 80 lines · On Thu, Oct 15, 2026 at 08:32:05PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3: incorrect recovery from -EEXIST in smmu_host_stage2_idmap leaves holes in memory --

commit d265b84d43b0a3e480c297258952f561f0f39998
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

This commit mirrors changes to the host stage-2 page table into an identity mapped stage-2 for the SMMUv3. It handles memory mapped with PAGE_SIZE and MMIO mapped with the largest block that fits.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 943ffef0bb4aa..75b2b1477a4fa 100644

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -1011,8 +1070,100 @@ static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr)

[ ... ]

Quoted 19 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +19 -0
+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;

[ ... ]

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +9 -0
+			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;
+

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_handler(struct user_pt_regs *regs, u64 esr, u64 addr) · line 1141

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+			if (ret == -EEXIST) {
+				/*
+				 * It is possible to get EEXIST when a VM dies with pages
+				 * in a shared state.
+				 */
+				ret = 0;
+				size -= pgsize;
+				start += pgsize;
+				continue;
+			}

[Severity: High]
Does this error handling skip valid pages when a collision occurs later in the batch?

The io-pgtable-arm backend implementation of map_pages validates all entries in the batch before writing any PTEs:

drivers/iommu/io-pgtable-arm.c:arm_lpae_init_pte() {
	for (i = 0; i < num_entries; i++)
		if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
			/* We require an unmap first */
			WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
			return -EEXIST;
		}
}

If a collision is found at any index (for example, the 5th page of the batch), map_pages returns -EEXIST immediately without mapping anything, leaving mapped at 0.

Quoted 6 lines

Because mapped is 0, this code assumes the very first page of the batch caused the collision, skips it by doing start += pgsize, and retries the loop. This process repeatedly skips earlier non-colliding pages without mapping them, leaving holes in the SMMU stage 2 identity map and causing translation faults during host DMA.

mmm, yes, this only works now because idmap is called with a single leaf. That logic needs some cleanup just in case that changes.

Thanks,
Mostafa

Quoted 6 lines
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+			if (!mapped || ret)
+				break;
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#73 lore raw

Re: [PATCH v9 23/25] iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs

In reply to [email protected]
Quoted 62 lines · On Thu, Oct 15, 2026 at 08:28:22PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] arm-smmu-v3: incorrect WARN_ON usage at EL2 for hardware timeouts and host inputs
- [Medium] arm-smmu-v3: guaranteed timeout and hyp panic if host writes reserved bits to CR0 --

commit a876edb8ca1cb558cf1336f7a093e9a259c78f18
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs

This commit implements the io-pgtable TLB operations to invalidate the shadow stage-2 entries across all SMMUs in pKVM. It manages invalidation commands and ensures the command queue is active before flushing TLBs.

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0

[ ... ]

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_tlb_inv_range(unsigned long iova, size_t size, size_t granule,
+			       bool leaf)
+{
+	struct hyp_arm_smmu_v3_device *smmu;
+
+	for_each_smmu(smmu) {
+		hyp_spin_lock(&smmu->hw_lock);
+		/*

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu, · line 321

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+		 * Don't bother if CMDQ 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.
+		 * When the SMMU is re-enabled the hypervisor cleans the TLBs.
+		 */
+		if (smmu->cmdq_active)
+			WARN_ON(smmu_tlb_inv_range_smmu(smmu, iova, size,
+							granule, leaf));

[Severity: High]
Under pKVM at EL2, WARN_ON() expands to a fatal BUG() instruction which crashes the hypervisor.

Could this WARN_ON() trigger a panic if the hardware SMMU is slow to drain its command queue? The call chain goes through smmu_tlb_inv_range_smmu(), smmu_send_cmd(), smmu_sync_cmd(), and ultimately smmu_wait(), which returns -ETIMEDOUT if the hardware polls for too long. Should this gracefully handle the error or ignore it instead of asserting?

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
+		hyp_spin_unlock(&smmu->hw_lock);
+	}
+}

[ ... ]

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
@@ -721,6 +821,33 @@ static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enab
 static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
 {
 	/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
 	if (WARN_ON(!is_cmdq_enabled(smmu)))
 		return;
 
Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu) · line 859

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
 	WARN_ON(smmu_update_ste_shadow(smmu, true));
+	/* Clean the TLBs each time the SMMU is enabled. */
+	WARN_ON(smmu_flush_all_tlb(smmu));

[Severity: High]
Does this code panic the hypervisor if the SMMU hardware times out during the TLB flush on enablement? Similar to the above, smmu_flush_all_tlb() can return -ETIMEDOUT from smmu_wait() if the hardware is unresponsive.

No way to recover from -ETIMEDOUT as discussed before, at this HW is propably in a really bad state.

Quoted 4 lines
Quoted 1 line

}

[ ... ]

Quoted 16 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu) · line 884

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
@@ -748,6 +877,13 @@ static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
 static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
 {
+	/*
+	 * We can not enable the SMMU if the CMDQ is disabled and similarly
+	 * we can not disable the CMDQ if the SMMU is enabled, as that can
+	 * lead to stale TLBs.
+	 */
+	WARN_ON(is_smmu_enabled(smmu));

[Severity: High]
Is it safe to panic the hypervisor here based on host-supplied configuration? If the host kernel supplies a CR0 value that disables the CMDQ while the SMMU is still enabled, this will trigger the EL2 WARN_ON() and crash the entire host.

The kernel can panic itself in many other ways.

Quoted 27 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
+
 	WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
 				   cmdq_size(&smmu->cmdq_host)));
 }

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -1033,6 +1169,20 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		else
 			writel_relaxed(val & mask, smmu->base + off);
 
+		/*

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 1182

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+		 * Make sure writes to CR0 are immediately observed, that is important
+		 * when synchronizing with TLB invalidation as reading CR0 is not enough
+		 * to deduce the SMMU state, and we have to enforce the ack with the
+		 * hw_lock acquired.
+		 */
+		if (off == ARM_SMMU_CR0) {
+			u32 cr0 = val;
+
+			WARN_ON(smmu_wait(false,
+					  readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0));

[Severity: Medium]
Can this sequence cause a guaranteed timeout and hypervisor panic if the host writes reserved bits to CR0?

Quoted 9 lines

The variable cr0 holds the raw, unmasked host input val. When the hypervisor writes to the hardware CR0 register, the hardware will update CR0ACK to reflect the accepted state, ignoring any reserved bits.

If the host writes a value with any bit set that the hardware ignores, readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0 will never evaluate to true. This would cause smmu_wait() to poll until it hits its timeout and return -ETIMEDOUT, triggering the WARN_ON() panic.

Same as above.

Thanks,
Mostafa

Quoted 5 lines
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+			smmu->cmdq_active = !!(cr0 & CR0_CMDQEN);
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#76 lore raw

Re: [PATCH v9 24/25] iommu/arm-smmu-v3-kvm: Enable nesting

In reply to [email protected]
Quoted 46 lines · On Thu, Oct 15, 2026 at 08:34:42PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] iommu/arm-smmu-v3: fatal EL2 panic on host-supplied invalid STE or ENOMEM
- [Medium] arm-smmu-v3-kvm: Incorrect EL2 WARN_ON usage for hardware timeout --

commit 5625f7b65b03ea88554c9ce7668b1dabbbb068b1
Author: Mostafa Saleh <[email protected]>

iommu/arm-smmu-v3-kvm: Enable nesting

This commit enables stage-2 translation for the ARM SMMUv3 under pKVM by allowing the hypervisor to sanitize and control the stage-2 portion of the Stream Table Entry (STE).

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -0
index 01f23fce6be50..3562bb222f53e 100644

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +3 -0
@@ -443,6 +443,60 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
+static int smmu_attach_stage_2(struct hyp_arm_smmu_v3_device *smmu,
+			       struct arm_smmu_ste *ste)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu) · line 461

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
+	/* S2 is not advertised, that should never be attempted. */
+	if ((cfg == STRTAB_STE_0_CFG_S2_TRANS) ||
+	    (cfg == STRTAB_STE_0_CFG_NESTED))
+		return -EINVAL;

[Severity: Critical]
Can this error return lead to a hypervisor panic?

When smmu_attach_stage_2() returns -EINVAL, the error propagates to smmu_filter_command(), which evaluates it and returns true to indicate the command should be skipped.

The preexisting caller smmu_emulate_cmdq_insert() then processes this skip condition using WARN_ON:

smmu_emulate_cmdq_insert() {
    ...
    skip = smmu_filter_command(smmu, cmd);
    if (WARN_ON(skip))
        continue;
}
Quoted 7 lines

At EL2 under pKVM, WARN_ON expands to a fatal breakpoint. Since the host can supply an invalid STE configuration that triggers this -EINVAL, could this allow the host to maliciously crash the hypervisor?

Similarly, allocating the L2 table in smmu_reshadow_ste() can fail and return -ENOMEM, which also propagates into this same fatal WARN_ON path during an out-of-memory event.

Yes, that is by design as replied previously.

Quoted 14 lines

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_get_host_l2_ste(struct hyp_arm_smmu_v3_device *smmu, u32 sid, · line 542

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+static void smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer)
+{
+	struct smmu_ste_writer *ste_writer =
+		container_of(writer, struct smmu_ste_writer, writer);
+	struct arm_smmu_cmd cfgi_cmd = arm_smmu_make_cmd_cfgi_ste(ste_writer->sid, true);
+
+	WARN_ON(smmu_send_cmd(ste_writer->smmu, &cfgi_cmd));
+}

[Severity: Medium]
Is it safe to use WARN_ON for hardware interactions that might time out?

Quoted 4 lines

If the SMMU command queue is full and smmu_send_cmd() times out waiting for it to drain, it will return -ETIMEDOUT. Because WARN_ON is fatal at EL2 under pKVM, this transforms a transient hardware latency or a heavy command load into a hypervisor panic.

As discussed in the last patch.

Thanks,
Mostafa

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
Reviewed-by on v9 3/25
#4 lore raw

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

In reply to Mostafa Saleh
Quoted 18 lines · Mostafa Saleh

The hypervisor SMMUv3 driver needs the same IDR0, IDR3, IDR5 and IIDR parsing as the kernel driver, plus the IDR1 checks it depends on (embedded implementation, ATTR_TYPES_OVR, StreamID size). Move them to arm-smmu-v3-common-lib.c, which will be built for both, and the IIDR implementer and product IDs to arm-smmu-v3.h.

The two drivers use different device structs, so the shared code is written against ARM_SMMU_OBJ, which each compilation unit defines. The kernel maps it to struct arm_smmu_device. Static asserts check that the fields the shared code uses exist with the expected types.

The validity checks stay with the parsing. dev_err() and dev_info() become arm_smmu_err() and arm_smmu_info(), defined per world next to ARM_SMMU_OBJ, as the hypervisor cannot print to the console. The disable_msipolling module parameter is passed as an argument so the hypervisor can opt out, and cpus_have_cap() becomes cpus_have_final_cap(), which the hypervisor can use.

Quoted 4 lines

No functional change intended.

Reviewed-by on v9 10/25
#15 lore raw

Re: [PATCH v9 10/25] iommu/arm-smmu-v3-kvm: Add SMMUv3 driver

In reply to Mostafa Saleh
Quoted 5 lines · Mostafa Saleh

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

Add the skeleton of the Arm SMMUv3 driver at EL2, and build the shared IDR/STE code and lib/hweight.o into the hypervisor.

Quoted 7 lines

The kernel driver, added next, fills an array describing the SMMUv3s in the system and hands it to the hypervisor. At init the hypervisor takes ownership of the array and of the SMMU MMIO regions, so the host cannot change either after de-privilege.

With some nits:

Quoted 10 lines

+/**
+ * struct hyp_arm_smmu_v3_device - SMMUv3 instance owned by the hypervisor
+ * @mmio_addr: Physical address of the SMMU registers
+ * @mmio_size: Size of the register region
+ * @base: Hypervisor virtual address of the registers
+ * @features: ARM_SMMU_FEAT_* bits
+ * @options: ARM_SMMU_OPT_* bits
+ * @oas: Output address size
+ * @pgsize_bitmap: Supported page sizes
+ * @sid_bits: Number of StreamID bits

Better drop those tabs, following

Documentation/doc-guide/kernel-doc.rst

Structure, union, and enumeration documentation:

/**
* struct struct_name - Brief description.
* @member1: Description of member1.
* @member2: Description of member2.
* One can provide multiple line descriptions
* for members.
*
* Description of the structure.
*/

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h line 29

Quoted 13 lines

+ *
+ * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
+ * before de-privilege, the rest by the hypervisor driver.
+ */

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h new file +9 -0
+struct hyp_arm_smmu_v3_device {
+	phys_addr_t		mmio_addr;
+	size_t			mmio_size;
+	void __iomem		*base;
+	u32			features;
+	u32			options;
+	unsigned long		oas;
+	unsigned long		pgsize_bitmap;
+	unsigned int		sid_bits;

I'd drop the tabs too.

Nicolin

#19 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 65

Quoted 2 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +2 -0
+static struct platform_driver smmuv3_nesting_driver;
+static int __init smmuv3_nesting_probe(struct platform_device *pdev)

Could it use a different naming than "nesting"?

"Nesting" is widely used in iommufd path for nested translation.

Here, it could be smmuv3_pkvm_driver?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5330

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13 -0
+static struct auxiliary_driver arm_smmu_driver_emu = {
+	.driver = {
+		.suppress_bind_attrs = true,
+	},
+	.name = "arm-smmu-v3-emu",
+	.id_table = arm_smmu_aux_table,
+	.probe = arm_smmu_device_probe_emu,
+	.remove = arm_smmu_device_remove_emu,
+	.shutdown = arm_smmu_device_shutdown_emu,
+};
+
+module_auxiliary_driver(arm_smmu_driver_emu);
+#endif

Nit:
#endif /* CONFIG_ARM_SMMU_V3_PKVM */

Nicolin

Reviewed-by on v9 12/25
#29 lore raw

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

In reply to Mostafa Saleh
Quoted 11 lines · Mostafa Saleh

Probe the SMMU features from the IDR registers with the functions shared with the kernel driver. SubstreamID bits and queue sizes are not needed
here: CDs are not shadowed and the command queue is set up later.

The hypervisor requires two things the kernel driver does not:
- Coherent table walks, reported by both firmware and IDR0.COHACC, as it does no cache maintenance on its tables.
- Nested translation, as the host keeps stage-1 and the hypervisor adds stage-2.

#33 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Mostafa Saleh
Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+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;

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 152

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	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;

Maybe add a small helper to list all 64-bit registers:

static unsigned int smmu_reg_width(u32 off)

{
        switch (off) {
        case ARM_SMMU_CMDQ_BASE:
        case ARM_SMMU_STRTAB_BASE:
        case ARM_SMMU_EVTQ_BASE:
        case ARM_SMMU_PRIQ_BASE:
        case ARM_SMMU_EVTQ_IRQ_CFG0:
        case ARM_SMMU_PRIQ_IRQ_CFG0:
        case ARM_SMMU_GERROR_IRQ_CFG0:
                return sizeof(u64);
        default:
                return sizeof(u32);
        }
}

Then it'd be tighter by testing the width once, prior to switch (off)?

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+	/* Allowed RO 32 bit registers. */
+	case ARM_SMMU_IIDR:
+	case ARM_SMMU_IDR5:
+	case ARM_SMMU_IDR3:
+	case ARM_SMMU_IDR1:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 253

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	case ARM_SMMU_IRQ_CTRLACK:
+	case ARM_SMMU_GERROR:
+		if (len != sizeof(u32))
+			break;
+		mask = read_only;
+		break;
+	}
+
+	if (WARN_ON(!mask))
+		goto out_ret;

Your cover letter mentions "WARNs are fatal in the hypervisor".

Is this WARN_ON fatal?

Nicolin

Reviewed-by on v9 14/25
#40 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Mostafa Saleh
Quoted 7 lines · 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.

Quoted 8 lines

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.

With some nits:

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c static size_t kvm_arm_smmu_cur; · line 30

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c +3 -0
@@ -23,6 +25,15 @@ static size_t kvm_arm_smmu_cur;
 static void __init kvm_arm_smmu_array_free(void)
 {
 	int order;
+	int i;
+
+	for (i = 0 ; i < kvm_arm_smmu_cur ; ++i) {

s/ ;/;/g

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 274

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -158,6 +268,14 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		break;
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_CMDQ_BASE:
+		if (is_write) {
+			/* Not allowed by the architecture */
+			if (is_cmdq_enabled(smmu))
+				break;

Sashiko reported the same WARN_ON issue. You might want to confirm.

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 291

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -170,9 +288,16 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 			break;
 
 		if (is_write) {
+			bool last_cmdq_en = is_cmdq_enabled(smmu);

Maybe cmdq_was_enabled (local) and cmdq_is_enabled (helper)

Nicolin

Reviewed-by on v9 15/25
#47 lore raw

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

In reply to Mostafa Saleh
Quoted 5 lines · Mostafa Saleh

Add the command queue accessors. The hypervisor's own commands, such as TLB invalidations, go through smmu_send_cmd(), which queues the command followed by a CMD_SYNC and waits for completion. Filtered host commands are copied into the shadow queue with smmu_add_cmd_raw().

Quoted 7 lines

Polling is bounded by a 1 ms timeout measured with the architected counter, and sleeps in WFE when the SMMU supports SEV.

Split queue_space() out of queue_has_space(), as the emulation will check the space in the host's queue.

#42 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Mostafa Saleh

One more thing:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_probe(struct hyp_arm_smmu_v3_device *smmu) · line 143

Quoted 10 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+/*
+ * The kernel part of the driver allocates and zeroes the shadow cmdq. Take
+ * ownership of it and program it into the hardware.
+ */
+static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
+{
+	size_t cmdq_nr_pages;
+	int ret;
+
+	smmu->cmdq.llq.max_n_shift = min(smmu->cmdq.llq.max_n_shift, smmu->cmdq_max_shift);

Given this is the cmdq used to run commands eventually..

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 232

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
+
+	smmu->cmdq_host.llq.max_n_shift = min(shift, smmu->cmdq_max_shift);

.. should this be min(shift, smmu->cmdq.llq.max_n_shift)?

Otherwise, a buggy guest SW might setup a larger queue and number of commands?

Nicolin

#51 lore raw

Re: [PATCH v9 16/25] iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to Mostafa Saleh
Quoted 5 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +0 -1
@@ -385,18 +482,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		 */
 		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP | IDR0_ATS);
 		break;
-	/* Passthrough the register access for bisectability, handled later */

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 494

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +6 -0
 	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 (is_cmdq_enabled(smmu))

The spec says the register is RO when CMDQEN=0. So, maybe it should be allowed by just ignoring the write?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 518

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
 	case ARM_SMMU_CMDQ_CONS:
+		if (len != sizeof(u32))
+			break;
+		if (is_write) {
+			if (WARN_ON(is_cmdq_enabled(smmu)))
+				break;

Ditto

Nicolin

#56 lore raw

Re: [PATCH v9 17/25] iommu/arm-smmu-v3-kvm: Shadow stream table

In reply to Mostafa Saleh

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 658

Quoted 5 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -538,6 +655,7 @@ 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);

It'd read better with smmu_was_enabled and smmu_is_enabled.

Quoted 18 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +6 -0
index 3903ae358555..31ab7a0335a8 100644
@@ -27,6 +27,12 @@
  * @cmdq_host:		Host view of the CMDQ, only q_base, base_dma and llq used
  * @cmdq_max_shift:	Max shift for the CMDQ probed from HW
  * @cr0:		Last value of CR0
+ * @host_ste_cfg:	Host stream table config
+ * @host_ste_base:	Host stream table base
+ * @strtab_cfg:		Stream table config as seen by HW
+ * @strtab_dma:		Physical address of the shadow stream table
+ * @strtab_size:	Size of the shadow stream table
+ *
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
  */

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h struct hyp_arm_smmu_v3_device { · line 63

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +5 -0
@@ -50,6 +56,11 @@ struct hyp_arm_smmu_v3_device {
 	struct arm_smmu_queue	cmdq_host;
 	u32			cmdq_max_shift;
 	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;

The order is different here than the kdocs.

Also, it might be cleaner to group host/shadow/hw stuff separately:

struct hyp_arm_smmu_v3_device {
	...
	struct hyp_arm_smmu_v3_device_host host;
	struct hyp_arm_smmu_v3_device_shadow shadow;
	struct hyp_arm_smmu_v3_device_hw hw;
	// Or
	struct {
		...
	} shadow;
	struct {
		...
	} host;
	struct {
		...
	} hw;
	...
};

Nicolin

#35 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Mostafa Saleh

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 292

Quoted 10 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+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);

Going through the followup patches, I found this smmu_dabt_device gets really too big. Perhaps it can be bisect to read and write at least? Something like:

static bool smmu_mmio_handler(smmu, regs, esr, off)

{
        /* decode ESR: len, rd, xzr; width check */
        if (is_write)
                return smmu_mmio_write_handler(smmu, off, len, val);
        val = smmu_mmio_read_handler(smmu, off, len);
        if (!is_xzr)
                regs->regs[rd] = val;
        return true;
}

Nicolin

#20 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Nicolin Chen
Quoted 9 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c line 65

Quoted 2 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c new file +2 -0
+static struct platform_driver smmuv3_nesting_driver;
+static int __init smmuv3_nesting_probe(struct platform_device *pdev)

Could it use a different naming than "nesting"?

"Nesting" is widely used in iommufd path for nested translation.

Here, it could be smmuv3_pkvm_driver?

Sure, pkvm/hyp works.

Quoted 13 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

Quoted 3 lines

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

I can add some ASCII diagram.
If pKVM driver fails, no aux devices are created and the main driver probes the platform SMMUv3 devices as usual.

Quoted 8 lines
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +8 -0
+static struct auxiliary_driver arm_smmu_driver_emu = {
+	.driver = {
+		.suppress_bind_attrs = true,
+	},
+	.name = "arm-smmu-v3-emu",
+	.id_table = arm_smmu_aux_table,
+	.probe = arm_smmu_device_probe_emu,
+	.remove = arm_smmu_device_remove_emu,
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5330

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5 -0
+	.shutdown = arm_smmu_device_shutdown_emu,
+};
+
+module_auxiliary_driver(arm_smmu_driver_emu);
+#endif

Nit:
#endif /* CONFIG_ARM_SMMU_V3_PKVM */

Will fix it.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#34 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Nicolin Chen
Quoted 38 lines · Nicolin Chen
Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +8 -0
+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;

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 152

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	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;

Maybe add a small helper to list all 64-bit registers:

static unsigned int smmu_reg_width(u32 off)

{
        switch (off) {
        case ARM_SMMU_CMDQ_BASE:
        case ARM_SMMU_STRTAB_BASE:
        case ARM_SMMU_EVTQ_BASE:
        case ARM_SMMU_PRIQ_BASE:
        case ARM_SMMU_EVTQ_IRQ_CFG0:
        case ARM_SMMU_PRIQ_IRQ_CFG0:
        case ARM_SMMU_GERROR_IRQ_CFG0:
                return sizeof(u64);
        default:
                return sizeof(u32);
        }
}
Quoted 1 line

Then it'd be tighter by testing the width once, prior to switch (off)?

Makes sense, I will try that.

Quoted 19 lines
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+	/* Allowed RO 32 bit registers. */
+	case ARM_SMMU_IIDR:
+	case ARM_SMMU_IDR5:
+	case ARM_SMMU_IDR3:
+	case ARM_SMMU_IDR1:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 253

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+	case ARM_SMMU_IRQ_CTRLACK:
+	case ARM_SMMU_GERROR:
+		if (len != sizeof(u32))
+			break;
+		mask = read_only;
+		break;
+	}
+
+	if (WARN_ON(!mask))
+		goto out_ret;

Your cover letter mentions "WARNs are fatal in the hypervisor".

Quoted 1 line

Is this WARN_ON fatal?

Yes. This is reached if the host does something not emulated:
1) A bug (writing to a RO register, writing STRTAB if SMMU is enabled...)
2) New arch feature not supported.

In that case, I'd rather panic here and fix the driver instead of ignoring it which will manifest in a harder to debug symptoms later.

Also, this driver only handles the Linux host kernel (which must be built-in in this config) so we can assume how the emulated driver works and what features it use.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#36 lore raw

Re: [PATCH v9 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation

In reply to Nicolin Chen
Quoted 17 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 292

Quoted 10 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+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);

Going through the followup patches, I found this smmu_dabt_device gets really too big. Perhaps it can be bisect to read and write at least? Something like:

static bool smmu_mmio_handler(smmu, regs, esr, off)

Quoted 9 lines
{
        /* decode ESR: len, rd, xzr; width check */
        if (is_write)
                return smmu_mmio_write_handler(smmu, off, len, val);
        val = smmu_mmio_read_handler(smmu, off, len);
        if (!is_xzr)
                regs->regs[rd] = val;
        return true;
}

Yes, I was wondering about the size of the dabt handler also. I will try this, but that will propably won't cut the size in half.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#41 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Nicolin Chen
Quoted 21 lines · Nicolin Chen
Quoted 7 lines · 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.

Quoted 8 lines

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.

With some nits:

Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c static size_t kvm_arm_smmu_cur; · line 30

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-pkvm-init.c +3 -0
@@ -23,6 +25,15 @@ static size_t kvm_arm_smmu_cur;
 static void __init kvm_arm_smmu_array_free(void)
 {
 	int order;
+	int i;
+
+	for (i = 0 ; i < kvm_arm_smmu_cur ; ++i) {

s/ ;/;/g

Will do.

Quoted 12 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 274

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -0
@@ -158,6 +268,14 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		break;
 	/* Passthrough the register access for bisectability, handled later */
 	case ARM_SMMU_CMDQ_BASE:
+		if (is_write) {
+			/* Not allowed by the architecture */
+			if (is_cmdq_enabled(smmu))
+				break;

Sashiko reported the same WARN_ON issue. You might want to confirm.

I replied to sashiko about that, this is by design as I also mentioned in patch #13.

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 291

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -170,9 +288,16 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 			break;
 
 		if (is_write) {
+			bool last_cmdq_en = is_cmdq_enabled(smmu);

Maybe cmdq_was_enabled (local) and cmdq_is_enabled (helper)

Sure.

Thanks,
Mostafa

Quoted 2 lines
Quoted 1 line

Nicolin

#43 lore raw

Re: [PATCH v9 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue

In reply to Nicolin Chen
Quoted 24 lines · Nicolin Chen

One more thing:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_probe(struct hyp_arm_smmu_v3_device *smmu) · line 143

Quoted 10 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +10 -0
+/*
+ * The kernel part of the driver allocates and zeroes the shadow cmdq. Take
+ * ownership of it and program it into the hardware.
+ */
+static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
+{
+	size_t cmdq_nr_pages;
+	int ret;
+
+	smmu->cmdq.llq.max_n_shift = min(smmu->cmdq.llq.max_n_shift, smmu->cmdq_max_shift);

Given this is the cmdq used to run commands eventually..

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static int smmu_init(void) · line 232

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
+static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 shift = smmu->cmdq_host.q_base & Q_BASE_LOG2SIZE;
+
+	smmu->cmdq_host.llq.max_n_shift = min(shift, smmu->cmdq_max_shift);

.. should this be min(shift, smmu->cmdq.llq.max_n_shift)?

Quoted 2 lines

Otherwise, a buggy guest SW might setup a larger queue and number of commands?

Makes sense, as in patch#16, it caps the host view in the emulated IDR1:

			val = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
			val &= ~IDR1_CMDQS;
			val |= FIELD_PREP(IDR1_CMDQS, smmu->cmdq.llq.max_n_shift);

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#52 lore raw

Re: [PATCH v9 16/25] iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

In reply to Nicolin Chen
Quoted 11 lines · Nicolin Chen
Quoted 10 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +4 -1
@@ -385,18 +482,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
 		 */
 		mask = read_only & ~(IDR0_S2P | IDR0_VMID16 | IDR0_MSI | IDR0_HYP | IDR0_ATS);
 		break;
-	/* Passthrough the register access for bisectability, 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.
+		 */
Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 494

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +2 -0
+		if (len != sizeof(u64))
+			break;
 		if (is_write) {
 			/* Not allowed by the architecture */
 			if (is_cmdq_enabled(smmu))

The spec says the register is RO when CMDQEN=0. So, maybe it should be allowed by just ignoring the write?

Yes, that's possible, I am slightly inclined to be more strict here, as this should never happen and if the kernel is buggy or malicous it is better to catch the point it goes wrong rather than trying to debug the side effects of ignoring the write as most likely it will go wrong shortly after (timeouts or commands are ignored causing TLB corruption...).

An alternative would be to ignore it and log something, at the moment tracing is supported in EL2, and IIRC, logging to dmesg is also planned.

Thanks,
Mostafa

Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 518

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +5 -0
 	case ARM_SMMU_CMDQ_CONS:
+		if (len != sizeof(u32))
+			break;
+		if (is_write) {
+			if (WARN_ON(is_cmdq_enabled(smmu)))
+				break;

Ditto

Nicolin

#57 lore raw

Re: [PATCH v9 17/25] iommu/arm-smmu-v3-kvm: Shadow stream table

In reply to Nicolin Chen
Quoted 9 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 658

Quoted 5 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.c +1 -0
@@ -538,6 +655,7 @@ 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);

It'd read better with smmu_was_enabled and smmu_is_enabled.

Sure, will update them.

Quoted 40 lines
Quoted 18 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +6 -0
index 3903ae358555..31ab7a0335a8 100644
@@ -27,6 +27,12 @@
  * @cmdq_host:		Host view of the CMDQ, only q_base, base_dma and llq used
  * @cmdq_max_shift:	Max shift for the CMDQ probed from HW
  * @cr0:		Last value of CR0
+ * @host_ste_cfg:	Host stream table config
+ * @host_ste_base:	Host stream table base
+ * @strtab_cfg:		Stream table config as seen by HW
+ * @strtab_dma:		Physical address of the shadow stream table
+ * @strtab_size:	Size of the shadow stream table
+ *
  * @mmio_addr, @mmio_size and ARM_SMMU_FEAT_COHERENCY are filled by the host
  * before de-privilege, the rest by the hypervisor driver.
  */

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h struct hyp_arm_smmu_v3_device { · line 63

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +5 -0
@@ -50,6 +56,11 @@ struct hyp_arm_smmu_v3_device {
 	struct arm_smmu_queue	cmdq_host;
 	u32			cmdq_max_shift;
 	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;

The order is different here than the kdocs.

Also, it might be cleaner to group host/shadow/hw stuff separately:

struct hyp_arm_smmu_v3_device {
	...
	struct hyp_arm_smmu_v3_device_host host;
	struct hyp_arm_smmu_v3_device_shadow shadow;
	struct hyp_arm_smmu_v3_device_hw hw;
	// Or
	struct {
		...
	} shadow;
Quoted 8 lines
	struct {
		...
	} host;
	struct {
		...
	} hw;
	...
};

That looks better, thanks! I will try it in the next version.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#21 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh
Quoted 14 lines · Mostafa Saleh
Quoted 13 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

Quoted 7 lines
Quoted 3 lines

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

I can add some ASCII diagram.
If pKVM driver fails, no aux devices are created and the main driver probes the platform SMMUv3 devices as usual.

I didn't check closely, but would it be safe to do so?

Is pkvm aware of the missing SMMU device and handling it properly?

When pkvm is enabled and the SMMU device is supposed to be a pkvm device, should it not fall back to the main driver?

Nicolin

#22 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

Quoted 23 lines · Nicolin Chen
Quoted 14 lines · Mostafa Saleh
Quoted 13 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

Quoted 7 lines
Quoted 3 lines

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

I can add some ASCII diagram.
If pKVM driver fails, no aux devices are created and the main driver probes the platform SMMUv3 devices as usual.

Quoted 6 lines

I didn't check closely, but would it be safe to do so?

Is pkvm aware of the missing SMMU device and handling it properly?

When pkvm is enabled and the SMMU device is supposed to be a pkvm device, should it not fall back to the main driver?

If pKVM fails to init for any reason (unsupported layout or non-coherent HW) the SMMU will be bound to the main driver so the system remains functional.

However, pKVM will not be safe in that case regarding DMA isolation as the status quo.
In Android, in that case, pKVM clears the FW (called pvmfw) that loads the protected VM and authenticate it so any protected VM knows the system is not trusted. This part is not upstream yet. [1]

However, there is not much that pKVM can do and I think it is better to keep the system functional in that case than causing a panic or failing KVM init for example.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/virt/kvm/arm/pkvm.rst#n106
Thanks,
Mostafa

Quoted 2 lines

Nicolin

#23 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

Quoted 35 lines · Mostafa Saleh
Quoted 23 lines · Nicolin Chen
Quoted 14 lines · Mostafa Saleh
Quoted 13 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

Quoted 7 lines
Quoted 3 lines

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

I can add some ASCII diagram.
If pKVM driver fails, no aux devices are created and the main driver probes the platform SMMUv3 devices as usual.

Quoted 6 lines

I didn't check closely, but would it be safe to do so?

Is pkvm aware of the missing SMMU device and handling it properly?

When pkvm is enabled and the SMMU device is supposed to be a pkvm device, should it not fall back to the main driver?

If pKVM fails to init for any reason (unsupported layout or non-coherent HW) the SMMU will be bound to the main driver so the system remains functional.

Quoted 5 lines

However, pKVM will not be safe in that case regarding DMA isolation as the status quo.
In Android, in that case, pKVM clears the FW (called pvmfw) that loads the protected VM and authenticate it so any protected VM knows the system is not trusted. This part is not upstream yet. [1]

You might want to double check a dual-SMMU case, where one gets accepted by pkvm driver and the other fails and so bound to the main driver.

It seems the pkvm driver would still pkvm_iommu_register_driver and carry on with an untrusted SMMU. Would it also clear the FW and mark the system as not trusted?

Nicolin

#24 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

Quoted 42 lines · Nicolin Chen
Quoted 35 lines · Mostafa Saleh
Quoted 23 lines · Nicolin Chen
Quoted 14 lines · Mostafa Saleh
Quoted 13 lines · Nicolin Chen

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static struct platform_driver arm_smmu_driver = { · line 5274

Quoted 8 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*
+ * Two devices are involved: the aux device bound to this driver, and the
+ * physical platform device bound to the pKVM init driver, which only uses it
+ * for discovery. This driver keeps using the platform device, as it carries
+ * the fwnode the IOMMU core and consumers link to, the DMA configuration and
+ * the IRQs.

It would be nicer to have a diagram or so to show the co-relationship among different devices and drivers.

Quoted 7 lines
Quoted 3 lines

I assume the regular arm-smmu-v3 driver is a part of the picture too, when the pkvm driver binding fails?

I can add some ASCII diagram.
If pKVM driver fails, no aux devices are created and the main driver probes the platform SMMUv3 devices as usual.

Quoted 6 lines

I didn't check closely, but would it be safe to do so?

Is pkvm aware of the missing SMMU device and handling it properly?

When pkvm is enabled and the SMMU device is supposed to be a pkvm device, should it not fall back to the main driver?

If pKVM fails to init for any reason (unsupported layout or non-coherent HW) the SMMU will be bound to the main driver so the system remains functional.

Quoted 5 lines

However, pKVM will not be safe in that case regarding DMA isolation as the status quo.
In Android, in that case, pKVM clears the FW (called pvmfw) that loads the protected VM and authenticate it so any protected VM knows the system is not trusted. This part is not upstream yet. [1]

Quoted 8 lines

You might want to double check a dual-SMMU case, where one gets accepted by pkvm driver and the other fails and so bound to the main driver.

It seems the pkvm driver would still pkvm_iommu_register_driver and carry on with an untrusted SMMU. Would it also clear the FW and mark the system as not trusted?

That shouldn't be possible, if one SMMU fail the whole thing will and the driver should clean up after as the probe is done through:

	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);

In that case, that system will be considered untrusted.

Thanks,
Mostafa

Quoted 1 line

Nicolin

#25 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

Quoted 5 lines · Mostafa Saleh
Quoted 4 lines · Nicolin Chen

You might want to double check a dual-SMMU case, where one gets accepted by pkvm driver and the other fails and so bound to the main driver.

Quoted 8 lines
Quoted 4 lines

It seems the pkvm driver would still pkvm_iommu_register_driver and carry on with an untrusted SMMU. Would it also clear the FW and mark the system as not trusted?

That shouldn't be possible, if one SMMU fail the whole thing will and the driver should clean up after as the probe is done through:

	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);

If I read __platform_driver_probe correctly:
* __driver_attach() doesn't propagate ret from driver_probe_device() via bus_for_each_dev to the caller
* bus_for_each_dev(..., is_bound_to_driver) at the end also returns true, as one SMMU is bound to the driver while the other failed.
?

Nicolin

#26 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

In reply to Mostafa Saleh
Quoted 10 lines · Mostafa Saleh
Quoted 9 lines · Nicolin Chen
Quoted 3 lines · Mostafa Saleh

In Android, in that case, pKVM clears the FW (called pvmfw) that loads the protected VM and authenticate it so any protected VM knows the system is not trusted. This part is not upstream yet. [1]

You might want to double check a dual-SMMU case, where one gets accepted by pkvm driver and the other fails and so bound to the main driver.

Quoted 8 lines
Quoted 4 lines

It seems the pkvm driver would still pkvm_iommu_register_driver and carry on with an untrusted SMMU. Would it also clear the FW and mark the system as not trusted?

That shouldn't be possible, if one SMMU fail the whole thing will and the driver should clean up after as the probe is done through:

	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);

Hmm, if I read __platform_driver_probe correctly:
* __driver_attach() doesn't propagate ret from driver_probe_device() via bus_for_each_dev to the caller
* bus_for_each_dev(..., is_bound_to_driver) at the end also returns true, as one SMMU is bound to the driver while the other failed
?

Nicolin

#27 lore raw

Re: [PATCH v9 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver

Quoted 20 lines · Nicolin Chen
Quoted 10 lines · Mostafa Saleh
Quoted 9 lines · Nicolin Chen
Quoted 3 lines · Mostafa Saleh

In Android, in that case, pKVM clears the FW (called pvmfw) that loads the protected VM and authenticate it so any protected VM knows the system is not trusted. This part is not upstream yet. [1]

You might want to double check a dual-SMMU case, where one gets accepted by pkvm driver and the other fails and so bound to the main driver.

Quoted 8 lines
Quoted 4 lines

It seems the pkvm driver would still pkvm_iommu_register_driver and carry on with an untrusted SMMU. Would it also clear the FW and mark the system as not trusted?

That shouldn't be possible, if one SMMU fail the whole thing will and the driver should clean up after as the probe is done through:

	ret = platform_driver_probe(&smmuv3_nesting_driver, smmuv3_nesting_probe);
Quoted 6 lines

Hmm, if I read __platform_driver_probe correctly:
* __driver_attach() doesn't propagate ret from driver_probe_device() via bus_for_each_dev to the caller
* bus_for_each_dev(..., is_bound_to_driver) at the end also returns true, as one SMMU is bound to the driver while the other failed
?

I double checked, you are right. In android we have the following:

	ret = platform_driver_probe(&kvm_arm_smmu_driver, kvm_arm_smmu_probe);
	if (ret)
		goto err_free;
	if (kvm_arm_smmu_cur != kvm_arm_smmu_count) {
		/* A device exists but failed to probe */
		ret = -EUNATCH;
		goto err_free;
	}

We need that also here, I will add it in the next version.

Thanks,
Mostafa

Quoted 2 lines

Nicolin