Skip to contentSkip to conversation

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

PATCH Superseded Mostafa Saleh 5 participants 72 messages Sep 22 – Sep 23

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

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

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

Changes from v7:
v7: https://lore.kernel.org/all/[email protected]/
- Use pfn, nr_pages instead of addr, size in MMIO functions [Vincent]
- Inline system timer handling in the driver [Vincent]
- Update comments [Fuad]
- Remove TLB invalidation re-use and introduce helpers instead [Jason]
- Implement a different TLB algorithm for the hypervisor based on 2 overlapping commands as currently on the list.
- Rely on macro tricks to reuse more functions between drivers [Jason]
- Use the hitless machinery for the hypervisor STE updates [Jason]
- Fixed an issue with small command queue sizes.
- Update and rename patches message to be more clear.

Notes about Sashiko
===================
I ran Sashiko locally and it was helpful in discovering problems in the series. However, it still shows large number of critical and high severity issues, I went through them and I believe they are false 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.
I don’t usually use ACPI, but I can look into adding that later. (not make this series bigger)

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. (Check comment in patch 14.

Future work
===========
1) Sharing page tables will be an interesting optimization, but has many challenges, I have a prototype for that I plan to send it will be group related series, starting with BBML3 which is already posted [1]. I also have a talk in the KVM MC in LPC 2026 about this.

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://lore.kernel.org/all/[email protected]/
[2] https://android-kvm.googlesource.com/linux/+/refs/heads/kvm-smmu-v8

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: Introduce RangeInval encoding helpers iommu/arm-smmu-v3: Move IDR parsing to common functions iommu/arm-smmu-v3: Move hitless machinery 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         |    5 +
 Documentation/virt/kvm/arm/pkvm.rst           |   23 +-
 arch/arm64/include/asm/kvm_host.h             |    6 +
 arch/arm64/include/asm/kvm_pgtable.h          |    1 +
 arch/arm64/kvm/Makefile                       |    2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |   24 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |    7 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |    9 +-
 arch/arm64/kvm/hyp/nvhe/iommu.c               |  174 ++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  135 +-
 arch/arm64/kvm/hyp/nvhe/setup.c               |   19 +
 arch/arm64/kvm/hyp/pgtable.c                  |   11 +-
 arch/arm64/kvm/iommu.c                        |   64 +
 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  |  416 +++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |   41 +
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  231 +++
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c  |    1 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  561 +------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  107 +-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |   79 +
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 1430 +++++++++++++++++
 .../arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h |   86 +
 drivers/iommu/io-pgtable-arm.c                |   14 +-
 drivers/iommu/io-pgtable-arm.h                |    9 +
 27 files changed, 2960 insertions(+), 511 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-kvm.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/arm-smmu-v3.c
 create mode 100644 drivers/iommu/arm/arm-smmu-v3/pkvm/io-pgtable-arm-hyp.h

base-commit: f0100363d8c374bd8e9ea7c9ba02744f0b802ca4

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#1 lore raw

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

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

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

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

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

---
 arch/arm64/include/asm/kvm_pgtable.h          |  1 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |  7 ++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 93 ++++++++++++++++++-
 arch/arm64/kvm/hyp/pgtable.c                  | 11 +--
 4 files changed, 105 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +1 -0
index 41a8687938eb..223072b3e2cd 100644
@@ -712,6 +712,7 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
  * containing the cleared entry is decremented, with unreferenced pages being
  * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
  * FWB is not supported by the CPU.
+ * This function can't be used to clear invalid counted PTEs (annotations).
  *
  * Return: 0 on success, negative error code on failure.
  */
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +7 -0
index 29935c7da1de..072f4b0e72c2 100644
@@ -36,6 +36,13 @@ 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);
+/* Remaps MMIO range in the host, typically used in error path. */
+int __pkvm_hyp_donate_host_mmio(u64 pfn, u64 nr_pages);
 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 +92 -1

105 lines not on this page · raw message

arch/arm64/kvm/hyp/pgtable.c +5 -6
index b74dd5ce1efd..25af105855e5 100644
@@ -1161,13 +1161,12 @@ 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);
-		}
+	/*
+	 * This check ignores stage2_pte_is_counted() instead of clearing
+	 * the PTE as it might be MMIO owned by the hypervisor.
+	 */
+	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.55.0.1082.g2b9226bbc0-goog
#2 lore raw

Re: [PATCH v8 01/25] KVM: arm64: Donate MMIO to the hypervisor

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: arm64: resource leak and memory attribute mismatch on MMIO donation error path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: arm64: resource leak and memory attribute mismatch on MMIO donation error path --

commit 96c49002ec46fcffdec0cb75c633309cd148cad6
Author: Mostafa Saleh <[email protected]>

KVM: arm64: Donate MMIO to the hypervisor

This commit introduces functions to donate MMIO memory between the host and hypervisor. It enables IOMMU hypervisor drivers to protect and access the MMIO of IOMMUs by mapping the MMIO range in the hypervisor's private range and unmapping it from the host stage-2.

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0

[ ... ]

Quoted 23 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +23 -0
+int __pkvm_host_donate_hyp_mmio(u64 pfn, u64 nr_pages, unsigned long *haddr)
+{
+	u64 phys = hyp_pfn_to_phys(pfn);
+	u64 size = PAGE_SIZE * nr_pages;
+	kvm_pte_t pte;
+	u64 offset;
+	int ret;
+
+	/* Only before de-privilege. */
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!pfn_range_is_valid(pfn, nr_pages))
+		return -EINVAL;
+
+	ret = __pkvm_create_private_mapping(phys, size, PAGE_HYP_DEVICE, haddr);
+	if (ret)
+		return ret;
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(phys + offset)) {
+			ret = -EINVAL;

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

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

[Severity: Medium]
Does this leak the hypervisor private mapping and leave a mismatched memory attribute if validation fails?

In __pkvm_host_donate_hyp_mmio(), the PAGE_HYP_DEVICE mapping is created before the physical address is validated against addr_is_memory() or the page tables. If the validation fails, the function exits via the unlock label and returns an error without rolling back the private mapping.

If the host firmware provides an invalid physical address (like a RAM address) for MMIO donation, this leaves the virtual address space leaked in EL2. Additionally, keeping RAM mapped as PAGE_HYP_DEVICE creates a mismatched memory attribute (Device vs Normal) for RAM in the hypervisor translation regime, which can cause unpredictable behavior.

Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -0
+	/*
+	 * We set HYP as the owner of the MMIO pages in the host stage-2, for:
+	 * - host aborts: host_stage2_adjust_range() would fail for invalid non zero PTEs.
+	 * - recycle under memory pressure: host_stage2_unmap_dev_all() would call
+	 *   kvm_pgtable_stage2_unmap() which will not clear non zero invalid ptes (counted).
+	 * - other MMIO donation: Would fail as we check that the PTE is valid or empty.
+	 */
+	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
+			      phys, size, &host_s2_pool,
+			      KVM_HOST_INVALID_PTE_TYPE_DONATION,
+			      FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, PKVM_ID_HYP));
+unlock:
+	host_unlock_component();
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v8 01/25] KVM: arm64: Donate MMIO to the hypervisor

Quoted 58 lines · On Tue, Sep 22, 2026 at 01:29:15PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: arm64: resource leak and memory attribute mismatch on MMIO donation error path --

commit 96c49002ec46fcffdec0cb75c633309cd148cad6
Author: Mostafa Saleh <[email protected]>

KVM: arm64: Donate MMIO to the hypervisor

This commit introduces functions to donate MMIO memory between the host and hypervisor. It enables IOMMU hypervisor drivers to protect and access the MMIO of IOMMUs by mapping the MMIO range in the hypervisor's private range and unmapping it from the host stage-2.

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0

[ ... ]

Quoted 23 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +23 -0
+int __pkvm_host_donate_hyp_mmio(u64 pfn, u64 nr_pages, unsigned long *haddr)
+{
+	u64 phys = hyp_pfn_to_phys(pfn);
+	u64 size = PAGE_SIZE * nr_pages;
+	kvm_pte_t pte;
+	u64 offset;
+	int ret;
+
+	/* Only before de-privilege. */
+	if (static_branch_unlikely(&kvm_protected_mode_initialized))
+		return -EPERM;
+
+	if (!pfn_range_is_valid(pfn, nr_pages))
+		return -EINVAL;
+
+	ret = __pkvm_create_private_mapping(phys, size, PAGE_HYP_DEVICE, haddr);
+	if (ret)
+		return ret;
+
+	host_lock_component();
+	for (offset = 0; offset < size; offset += PAGE_SIZE) {
+		if (addr_is_memory(phys + offset)) {
+			ret = -EINVAL;

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

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

[Severity: Medium]
Does this leak the hypervisor private mapping and leave a mismatched memory attribute if validation fails?

Quoted 4 lines

In __pkvm_host_donate_hyp_mmio(), the PAGE_HYP_DEVICE mapping is created before the physical address is validated against addr_is_memory() or the page tables. If the validation fails, the function exits via the unlock label and returns an error without rolling back the private mapping.

Yes, in that case the private mapping is leaked, as there is no function to remove private mappings. This a pattern in the hypervisor in similar error cases during init.

Quoted 7 lines

If the host firmware provides an invalid physical address (like a RAM address) for MMIO donation, this leaves the virtual address space leaked in EL2. Additionally, keeping RAM mapped as PAGE_HYP_DEVICE creates a mismatched memory attribute (Device vs Normal) for RAM in the hypervisor translation regime, which can cause unpredictable behavior.

pKVM has to trust the firmware, otherwise much bigger problems can happen. So this check is mainly added for very unlikely conditions (firmware is really busted or during development)

Thanks,
Mostafa

Quoted 18 lines
Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -0
+	/*
+	 * We set HYP as the owner of the MMIO pages in the host stage-2, for:
+	 * - host aborts: host_stage2_adjust_range() would fail for invalid non zero PTEs.
+	 * - recycle under memory pressure: host_stage2_unmap_dev_all() would call
+	 *   kvm_pgtable_stage2_unmap() which will not clear non zero invalid ptes (counted).
+	 * - other MMIO donation: Would fail as we check that the PTE is valid or empty.
+	 */
+	ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
+			      phys, size, &host_s2_pool,
+			      KVM_HOST_INVALID_PTE_TYPE_DONATION,
+			      FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, PKVM_ID_HYP));
+unlock:
+	host_unlock_component();
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

PATCH v8 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 | 59 --------------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 62 +++++++++++++++++++++
 2 files changed, 62 insertions(+), 59 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -59
index 5732f3ba0122..df1924969212 100644
@@ -134,33 +134,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)) &&
@@ -179,12 +152,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_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
-	q->cons = Q_OVF(q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
-}
-
 static void queue_sync_cons_ovf(struct arm_smmu_queue *q)
 {
 	struct arm_smmu_ll_queue *llq = &q->llq;
@@ -216,12 +183,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_WRP(q, q->prod) | Q_IDX(q, q->prod)) + n;
-	return Q_OVF(q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
-}
-
 static void queue_poll_init(struct arm_smmu_device *smmu,
 			    struct arm_smmu_queue_poll *qp)
 {
@@ -249,14 +210,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;
@@ -1796,18 +1749,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;
-
-	/* 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 +62 -0

68 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v8 2/25
#5 lore raw

Re: [PATCH v8 02/25] iommu/arm-smmu-v3: Move Queue and STE functions to header

Quoted 5 lines · 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.

#6 lore raw

PATCH v8 3/25 iommu/arm-smmu-v3: Introduce RangeInval encoding helpers

In reply to Mostafa Saleh

Move some of the RIL encodings into helpers so the hypervisor can reuse them.
The hypervisor is going to have its own algorithm to determine the sequence of RIL commands needed.

---
I removed the refactoring from this patch as this code would be deleted anyway.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++-------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 30 +++++++++++++++++++++
 2 files changed, 34 insertions(+), 10 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4 -10
index df1924969212..2e54d1ba2566 100644
@@ -2418,8 +2418,7 @@ static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu,
 	if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) {
 		num_pages = size >> tg;
 
-		/* Convert page size of 12,14,16 (log2) to 1,2,3 */
-		tg_enc = (tg - 10) / 2;
+		tg_enc = arm_smmu_tlb_inv_tg_enc(tg);
 
 		/*
 		 * Determine what level the granule is at. For non-leaf, both
@@ -2430,7 +2429,7 @@ static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu,
 		 * where both scale and num could be 0 as well.
 		 */
 		if (leaf)
-			ttl = 4 - ((ilog2(granule) - 3) / (tg - 3));
+			ttl = arm_smmu_tlb_inv_ttl(granule, tg);
 		else if ((num_pages & CMDQ_TLBI_RANGE_NUM_MAX) == 1)
 			num_pages++;
 	}
@@ -2452,10 +2451,8 @@ static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu,
 			/* Determine how many chunks of 2^scale size we have */
 			num = (num_pages >> scale) & CMDQ_TLBI_RANGE_NUM_MAX;
 
-			/* Keep the pre-DS 5-bit truncation when scale > 31 */
 			cmd->data[0] = orig_data0 |
-				FIELD_PREP(CMDQ_TLBI_0_NUM, num - 1) |
-				FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);
+				arm_smmu_tlb_inv_range_enc(num, scale);
 
 			/* range is num * 2^scale * pgsize */
 			inv_range = num << (scale + tg);
@@ -2469,10 +2466,7 @@ static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu,
 		 * command and something would be very broken if iova had them
 		 * set.
 		 */
-		cmd->data[1] = FIELD_PREP(CMDQ_TLBI_1_LEAF, leaf) |
-			       FIELD_PREP(CMDQ_TLBI_1_TTL, ttl) |
-			       FIELD_PREP(CMDQ_TLBI_1_TG, tg_enc) |
-			       (iova & ~GENMASK_U64(11, 0));
+		cmd->data[1] = arm_smmu_tlb_inv_addr(iova, leaf, ttl, tg_enc);
 
 		arm_smmu_cmdq_batch_add_cmd_p(smmu, cmds, cmd);
 		iova += inv_range;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +30 -0
index 5b23f6ee78d4..f3e80d103ba1 100644
@@ -1285,6 +1285,36 @@ static inline void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst,
 	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
 }
 
+/* Convert page size of 12,14,16 (log2) to 1,2,3 */
+static inline u8 arm_smmu_tlb_inv_tg_enc(size_t tg)
+{
+	return (tg - 10) / 2;
+}
+
+/* Only valid for leaf invalidations, non-leaf must use TTL=0 */
+static inline u8 arm_smmu_tlb_inv_ttl(size_t granule, size_t tg)
+{
+	return 4 - ((ilog2(granule) - 3) / (tg - 3));
+}
+
+static inline u64 arm_smmu_tlb_inv_addr(unsigned long iova, bool leaf, u8 ttl, u8 tg_enc)
+{
+	return FIELD_PREP(CMDQ_TLBI_1_LEAF, leaf) |
+		FIELD_PREP(CMDQ_TLBI_1_TTL, ttl) |
+		FIELD_PREP(CMDQ_TLBI_1_TG, tg_enc) |
+		(iova & ~GENMASK_U64(11, 0));
+}
+
+/*
+ * @num: Number of 2^scale granules, must be >= 1.
+ */
+static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale)
+{
+	/* Keep the pre-DS 5-bit truncation when scale > 31 */
+	return FIELD_PREP(CMDQ_TLBI_0_NUM, num - 1) |
+		FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);
+}
+
 #ifdef CONFIG_ARM_SMMU_V3_SVA
 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
 void arm_smmu_sva_notifier_synchronize(void);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#7 lore raw

Re: [PATCH v8 03/25] iommu/arm-smmu-v3: Introduce RangeInval encoding helpers

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst, · line 1289

Quoted 2 lines · Mostafa Saleh
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +2 -0
+/* Convert page size of 12,14,16 (log2) to 1,2,3 */
+static inline u8 arm_smmu_tlb_inv_tg_enc(size_t tg)

The prefix "arm_smmu_tlb_" is used by the iommu_flush_ops.

How about arm_smmu_cmd_tlbi_tg_enc?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst, · line 1306

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +7 -0
+static inline u64 arm_smmu_tlb_inv_addr(unsigned long iova, bool leaf, u8 ttl, u8 tg_enc)
+{
+	return FIELD_PREP(CMDQ_TLBI_1_LEAF, leaf) |
+		FIELD_PREP(CMDQ_TLBI_1_TTL, ttl) |
+		FIELD_PREP(CMDQ_TLBI_1_TG, tg_enc) |
+		(iova & ~GENMASK_U64(11, 0));
+}

This actually covers entire data1 than just "addr", so maybe arm_smmu_cmd_tlbi_data1_enc?

Quoted 3 lines

+/*
+ * @num: Number of 2^scale granules, must be >= 1.
+ */

Nit: single-line style

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst, · line 1315

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5 -0
+static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale)
+{
+	/* Keep the pre-DS 5-bit truncation when scale > 31 */
+	return FIELD_PREP(CMDQ_TLBI_0_NUM, num - 1) |
+		FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);

And this could be arm_smmu_cmd_tlbi_data0_enc that takes orig too, matching with data1_enc().

Nicolin

#8 lore raw

PATCH v8 4/25 iommu/arm-smmu-v3: Move IDR parsing to common functions

In reply to Mostafa Saleh

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

As the new functions operate on structs from both the hypervisor and the kernel which would be different, we rely on the compilation unit to having ARM_SMMU_OBJ point to the correct struct; some best-effort static asserts were added .

---
 drivers/iommu/arm/arm-smmu-v3/Makefile        |   2 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 176 +++++++++++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |  31 +++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 179 ++----------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   6 +
 5 files changed, 228 insertions(+), 166 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 +176 -0

176 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +31 -0
new file mode 100644
index 000000000000..e736b6a8c78c
@@ -0,0 +1,31 @@
+/* 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 used by the kernel and the
+ * hypervisor drivers where ARM_SMMU_OBJ might be defined
+ * differently.
+ * Ensure fields used by them are defined and has the correct
+ * types.
+ */
+#ifndef __KVM_NVHE_HYPERVISOR__
+typedef struct arm_smmu_device ARM_SMMU_OBJ;
+#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 *));
+
+void arm_smmu_device_iidr_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr0_probe(ARM_SMMU_OBJ *smmu);
+void arm_smmu_idr3_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr5_probe(ARM_SMMU_OBJ *smmu);
+
+#endif /* __ARM_SMMU_V3_COMMON_LIB_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +14 -165

226 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
index f3e80d103ba1..d5963a01452f 100644
@@ -86,6 +86,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.55.0.1082.g2b9226bbc0-goog
#9 lore raw

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

Quoted 6 lines · Mostafa Saleh

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

As the new functions operate on structs from both the hypervisor and the kernel which would be different, we rely on the compilation unit to having ARM_SMMU_OBJ point to the correct struct; some

s/to having/to have

Quoted 1 line

best-effort static asserts were added .

s/added \./added\.

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 14

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +13 -0
+#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 used by the kernel and the
+ * hypervisor drivers where ARM_SMMU_OBJ might be defined
+ * differently.
+ * Ensure fields used by them are defined and has the correct
+ * types.

s/has/have

We have 80 cols per line to write comments :)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 18

Quoted 4 lines

+ */

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +3 -0
+#ifndef __KVM_NVHE_HYPERVISOR__
+typedef struct arm_smmu_device ARM_SMMU_OBJ;
+#endif

It's probably safer to include arm-smmu-v3.h so everything would be self-defined.

Also, Jason's suggestion in v7 was hyp_arm_smmu_v3_device, which looks nicer than ARM_SMMU_OBJ...

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 29

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +10 -0
+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 *));
+
+void arm_smmu_device_iidr_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr0_probe(ARM_SMMU_OBJ *smmu);
+void arm_smmu_idr3_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr5_probe(ARM_SMMU_OBJ *smmu);

Can we use "arm_smmu_device_xyz_probe" matching with the existing arm_smmu_device_iidr_probe?

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
-	if (!(reg & (IDR0_S1P | IDR0_S2P))) {
+	if (!(smmu->features & (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
 		dev_err(smmu->dev, "no translation support!\n");
 		return -ENXIO;

This change seems unnecessary. The code above and below this line still uses "reg" returned by idr0_probe(). So, the original code should have read well:

        if (!!(reg & IDR0_COHACC) != coherent)
                dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
                         str_true_false(coherent));

	if (!(reg & (IDR0_S1P | IDR0_S2P))) {
                dev_err(smmu->dev, "no translation support!\n");
                return -ENXIO;
        }

        /* We only support the AArch64 table format at present */
        if (!(FIELD_GET(IDR0_TTF, reg) & IDR0_TTF_AARCH64)) {
                dev_err(smmu->dev, "AArch64 table format not supported!\n");
                return -ENXIO;
        }

Nicolin

#10 lore raw

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

Quoted 2 lines · Nicolin Chen

Also, Jason's suggestion in v7 was hyp_arm_smmu_v3_device, which looks nicer than ARM_SMMU_OBJ...

I guess the point is to emphasis the substition technique, I don't mind.

Quoted 5 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

Not sure I understood the approach in this patch, why was anything left behind? I imagined everything would be moved?

Maybe a few more remarks in the commit message?

Quoted 5 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

+1

And the TT_LE/BE and S1/S2 checks, and ...

If kvm doesn't want to dev_err log that's fine, we should able to #define that away too shouldn't we? And maybe it does want to log?

This seems pretty close otherwise

Jason

#11 lore raw

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

Quoted 14 lines · Jason Gunthorpe
Quoted 2 lines · Nicolin Chen

Also, Jason's suggestion in v7 was hyp_arm_smmu_v3_device, which looks nicer than ARM_SMMU_OBJ...

I guess the point is to emphasis the substition technique, I don't mind.

Quoted 5 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

Quoted 4 lines

Not sure I understood the approach in this patch, why was anything left behind? I imagined everything would be moved?

Maybe a few more remarks in the commit message?

As replied to Nicolin, because it is a module param, I am ok with adding this in the commit message, or passing disable_msipolling as a function arg and moving the logic.

Quoted 7 lines
Quoted 6 lines

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

Quoted 6 lines

+1

And the TT_LE/BE and S1/S2 checks, and ...

If kvm doesn't want to dev_err log that's fine, we should able to #define that away too shouldn't we? And maybe it does want to log?

The hypervisor can not print to the kernel console, so dev_err() will not compile, I'd rather keep that outside and let the caller decide what is a probe failure and print it's own message that keeping the code and stubbing dev_err().

Thanks,
Mostafa

Quoted 4 lines

This seems pretty close otherwise

Jason

#12 lore raw

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

Quoted 7 lines · Mostafa Saleh
Quoted 2 lines

If kvm doesn't want to dev_err log that's fine, we should able to #define that away too shouldn't we? And maybe it does want to log?

The hypervisor can not print to the kernel console, so dev_err() will not compile, I'd rather keep that outside and let the caller decide what is a probe failure and print it's own message that keeping the code and stubbing dev_err().

I'd rather stub the dev_err than split it like this. Every driver should still do these checks, it doesn't entirely make sense to omit them from the pkvm flow.. No print, fine (and really, pkvm has no tracing, printing or anything?)

As a stube I'd do something like arm_smmu_err(smmu, "blah"); pkvm can do whatever with that.

Jason

#13 lore raw

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

Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Mostafa Saleh
Quoted 2 lines

If kvm doesn't want to dev_err log that's fine, we should able to #define that away too shouldn't we? And maybe it does want to log?

The hypervisor can not print to the kernel console, so dev_err() will not compile, I'd rather keep that outside and let the caller decide what is a probe failure and print it's own message that keeping the code and stubbing dev_err().

Quoted 4 lines

I'd rather stub the dev_err than split it like this. Every driver should still do these checks, it doesn't entirely make sense to omit them from the pkvm flow.. No print, fine (and really, pkvm has no tracing, printing or anything?)

Tracing is supported, trace_printk() might be coming soon to upstream, but console printing is not supported at the moment.

Quoted 3 lines

As a stube I'd do something like arm_smmu_err(smmu, "blah"); pkvm can do whatever with that.

That's ok by me, we can define those next to the struct typedefs for each one.

Thanks,
Mostafa

Quoted 2 lines

Jason

#14 lore raw

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

In reply to Nicolin Chen
Quoted 9 lines · Nicolin Chen
Quoted 6 lines · Mostafa Saleh

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

As the new functions operate on structs from both the hypervisor and the kernel which would be different, we rely on the compilation unit to having ARM_SMMU_OBJ point to the correct struct; some

s/to having/to have

Will do.

Quoted 4 lines
Quoted 2 lines

best-effort static asserts were added .

s/added \./added\.

Will do.

Quoted 17 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +3 -0
+#ifndef __ARM_SMMU_V3_COMMON_LIB_H
+#define __ARM_SMMU_V3_COMMON_LIB_H
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 14

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +10 -0
+#include <linux/build_bug.h>
+#include <linux/compiler_types.h>
+#include <linux/kernel.h>
+
+/*
+ * The IDR probe functions are used by the kernel and the
+ * hypervisor drivers where ARM_SMMU_OBJ might be defined
+ * differently.
+ * Ensure fields used by them are defined and has the correct
+ * types.

s/has/have

Quoted 1 line

We have 80 cols per line to write comments :)

Will do.

Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 18

Quoted 5 lines

+ */

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +3 -0
+#ifndef __KVM_NVHE_HYPERVISOR__
+typedef struct arm_smmu_device ARM_SMMU_OBJ;
+#endif

It's probably safer to include arm-smmu-v3.h so everything would be self-defined.

Yes, I was not sure about that, I was thinking of making this file included strictly after the struct is defined first but I didn't find a suitable place for that.
So, I can just add the include here before the typedef.

Quoted 3 lines

Also, Jason's suggestion in v7 was hyp_arm_smmu_v3_device, which looks nicer than ARM_SMMU_OBJ...

I think having another name makes the code more readable (not sure if some tools can be confused also) than renaming the hypervisor struct to the kernel one. That makes it clear what is the intent of this.

Quoted 5 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +5 -0
+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 *));
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h line 29

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h new file +5 -0
+
+void arm_smmu_device_iidr_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr0_probe(ARM_SMMU_OBJ *smmu);
+void arm_smmu_idr3_probe(ARM_SMMU_OBJ *smmu);
+u32 arm_smmu_idr5_probe(ARM_SMMU_OBJ *smmu);

Can we use "arm_smmu_device_xyz_probe" matching with the existing arm_smmu_device_iidr_probe?

Sure.

Quoted 6 lines

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

No, this version does not support MSI and hides it.
And this check can not be moved because disable_msipolling is a module_param.
Although it might be possible to pass it as an argument to the function and assume (smmu->features & ARM_SMMU_FEAT_COHERENCY) is set based on FW before the IDR probe similarly, no strong opinion, so this part can all be moved as is.

Quoted 7 lines

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

cpus_have_cap() can not be used in the hypervisor. Also, ARM_SMMU_FEAT_E2H is not exactly FEAT_HYP. As it defines the world the translation lives in based on the kernel EL.

With pKVM at EL2 ARM64_HAS_VIRT_HOST_EXTN is always true anyway. And the hypervisor never owns a page table itself, so it never checks this feature.
Otherwise, I think we can move this check and use cpus_have_final_cap() instead as it can be used in the hypervisor.

Quoted 8 lines

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
-	if (!(reg & (IDR0_S1P | IDR0_S2P))) {
+	if (!(smmu->features & (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
 		dev_err(smmu->dev, "no translation support!\n");
 		return -ENXIO;

This change seems unnecessary. The code above and below this line still uses "reg" returned by idr0_probe(). So, the original code should have read well:

True, I will change it back.

Thanks,
Mostafa

Quoted 17 lines
        if (!!(reg & IDR0_COHACC) != coherent)
                dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
                         str_true_false(coherent));

	if (!(reg & (IDR0_S1P | IDR0_S2P))) {
                dev_err(smmu->dev, "no translation support!\n");
                return -ENXIO;
        }

        /* We only support the AArch64 table format at present */
        if (!(FIELD_GET(IDR0_TTF, reg) & IDR0_TTF_AARCH64)) {
                dev_err(smmu->dev, "AArch64 table format not supported!\n");
                return -ENXIO;
        }

Nicolin

#15 lore raw

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

Quoted 9 lines · Mostafa Saleh
Quoted 5 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

No, this version does not support MSI and hides it. And this check can not be moved because disable_msipolling is a module_param.

Could you move the above if into the shared area and have the callers mask off options if they don't want it?

Quoted 10 lines
Quoted 6 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

cpus_have_cap() can not be used in the hypervisor. Also, ARM_SMMU_FEAT_E2H is not exactly FEAT_HYP. As it defines the world the translation lives in based on the kernel EL.

But don't you need this FEAT to issue invalidations correctly? pkvm never issues a S1 invalidation?

Quoted 4 lines

With pKVM at EL2 ARM64_HAS_VIRT_HOST_EXTN is always true anyway.

And the hypervisor never owns a page table itself, so it never checks this feature.

? I thought the whole point of this was for pkvm to own a S2?

Maybe same as I suggested for MSIPOLL, set the FEAT and mask it later if it can't be used. If KVM implicitly knows it has ARM64_HAS_VIRT_HOST_EXTN then it should operate with ARM_SMMU_FEAT_E2H set.

Quoted 2 lines

Otherwise, I think we can move this check and use cpus_have_final_cap() instead as it can be used in the hypervisor.

Or that, I don't know the difference

Jason

#16 lore raw

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

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Mostafa Saleh
Quoted 5 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (coherent && !disable_msipolling &&
+	    smmu->features & ARM_SMMU_FEAT_MSI)
+		smmu->options |= ARM_SMMU_OPT_MSIPOLL;

Will pKVM ever use MSIPOLL?

Quoted 6 lines
Quoted 3 lines

No, this version does not support MSI and hides it. And this check can not be moved because disable_msipolling is a module_param.

Could you move the above if into the shared area and have the callers mask off options if they don't want it?

Sure, I can move it and pass disable_polling as an arg which the hypervisor will set to true.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (smmu->features & ARM_SMMU_FEAT_HYP &&
+	    cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
+		smmu->features |= ARM_SMMU_FEAT_E2H;

Why is ARM64_HAS_VIRT_HOST_EXTN left behind?

Quoted 6 lines
Quoted 3 lines

cpus_have_cap() can not be used in the hypervisor. Also, ARM_SMMU_FEAT_E2H is not exactly FEAT_HYP. As it defines the world the translation lives in based on the kernel EL.

But don't you need this FEAT to issue invalidations correctly? pkvm never issues a S1 invalidation?

No, STRW is enforced to zero (NS-EL1) with pKVM as the kernel must be running in EL1.

pKVM will issue S1 CMDQ_OP_TLBI_NH_ALL to invalidate all of S1 after S2 changes and invalidations.

Quoted 7 lines
Quoted 5 lines

With pKVM at EL2 ARM64_HAS_VIRT_HOST_EXTN is always true anyway.

And the hypervisor never owns a page table itself, so it never checks this feature.

? I thought the whole point of this was for pkvm to own a S2?

By own I mean "it's own translation regime" as if the hypervisor wants to do DMA itself (synonym to EL2-S1 translation) in that case the hypervisor will have a different STRW and use the E2H stuff. But that never happens, the hypervsior populates S2 to enforce isolation of the host but it never uses it itself.

Quoted 6 lines

Maybe same as I suggested for MSIPOLL, set the FEAT and mask it later if it can't be used. If KVM implicitly knows it has ARM64_HAS_VIRT_HOST_EXTN then it should operate with ARM_SMMU_FEAT_E2H set.

As I replied to Nicolin, the problem here is not the FEAT being used or not, it is that cpus_have_cap() can not be used in the hypervisor.

I think this can be replaced with cpus_have_final_cap() which should be the same and then move this code also.

Quoted 4 lines
Quoted 2 lines

Otherwise, I think we can move this check and use cpus_have_final_cap() instead as it can be used in the hypervisor.

Or that, I don't know the difference

I will try that for the next version.

Thanks,
Mostafa

Quoted 2 lines

Jason

#17 lore raw

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

Quoted 5 lines · Mostafa Saleh
Quoted 2 lines

But don't you need this FEAT to issue invalidations correctly? pkvm never issues a S1 invalidation?

No, STRW is enforced to zero (NS-EL1) with pKVM as the kernel must be running in EL1.

Hmmm. Ok, that is what the iommufd flow ends up doing too, but still, I would expect the hypervisor to have ARM_SMMU_FEAT_E2H, though it would never read it.

Jason

#18 lore raw

PATCH v8 5/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 it can be reused by the hypervisor.

No functional change.

---
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.c  | 240 +++++++++++++++++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |   6 +
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c  |   1 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 242 ------------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |   7 +-
 5 files changed, 249 insertions(+), 247 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.c +240 -0
index a341974e7aaf..39b3cde9f5d2 100644
@@ -4,6 +4,8 @@
  * Arm SMMUv3 driver functions shared with hypervisor.
  */
 
+#include <kunit/visibility.h>
+
 #include "arm-smmu-v3.h"
 #include "arm-smmu-v3-common-lib.h"
 
@@ -174,3 +176,241 @@ u32 arm_smmu_idr5_probe(ARM_SMMU_OBJ *smmu)
 
 	return reg;
 }
+
+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);
+		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);
+		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 e736b6a8c78c..9a7064c8e879 100644
@@ -28,4 +28,10 @@ u32 arm_smmu_idr0_probe(ARM_SMMU_OBJ *smmu);
 void arm_smmu_idr3_probe(ARM_SMMU_OBJ *smmu);
 u32 arm_smmu_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 -0
index add671363c82..ea9c85d2c7f0 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;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -242

256 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +2 -5
index d5963a01452f..28efa733e796 100644
@@ -1088,11 +1088,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);
@@ -1321,6 +1316,8 @@ static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale)
 		FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);
 }
 
+#define NUM_ENTRY_QWORDS 8
+
 #ifdef CONFIG_ARM_SMMU_V3_SVA
 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
 void arm_smmu_sva_notifier_synchronize(void);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v8 5/25
#19 lore raw

Re: [PATCH v8 05/25] iommu/arm-smmu-v3: Move hitless machinery to common code

Quoted 2 lines · Mostafa Saleh

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

s/it/they

Quoted 3 lines

No functional change.

With some nits:

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c enum arm_smmu_msi_index { · line 64

Quoted 10 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -2
@@ -59,10 +59,8 @@ enum arm_smmu_msi_index {
 	ARM_SMMU_MAX_MSIS,
 };
 
-#define NUM_ENTRY_QWORDS 8
 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64));
 static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64));
-
 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {

This line removal seems unnecessary.

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale) · line 1320

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -1321,6 +1316,8 @@ static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale)
 		FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);
 }
 
+#define NUM_ENTRY_QWORDS 8
+

Maybe we should drop the duplicated define in arm-smmu-v3-test.c?

Nicolin

#20 lore raw

Re: [PATCH v8 05/25] iommu/arm-smmu-v3: Move hitless machinery to common code

Quoted 5 lines · Nicolin Chen
Quoted 2 lines · Mostafa Saleh

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

s/it/they

Will do.

Quoted 14 lines
Quoted 4 lines

No functional change.

With some nits:

Quoted 5 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
@@ -59,10 +59,8 @@ enum arm_smmu_msi_index {
 	ARM_SMMU_MAX_MSIS,
 };
 
Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c enum arm_smmu_msi_index { · line 64

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -2
-#define NUM_ENTRY_QWORDS 8
 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64));
 static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64));
-
 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {

This line removal seems unnecessary.

Yes, I will fix it.

Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale) · line 1320

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -1321,6 +1316,8 @@ static inline u64 arm_smmu_tlb_inv_range_enc(u8 num, u8 scale)
 		FIELD_PREP(CMDQ_TLBI_0_SCALE, scale & 0x1f);
 }
 
+#define NUM_ENTRY_QWORDS 8
+

Maybe we should drop the duplicated define in arm-smmu-v3-test.c?

Makes sense, will do.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#21 lore raw

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

In reply to Mostafa Saleh

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

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

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

---
 arch/arm64/include/asm/kvm_host.h       |  5 +++++
 arch/arm64/kvm/Makefile                 |  2 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h | 11 +++++++++++
 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                  | 25 +++++++++++++++++++++++++
 7 files changed, 73 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 +5 -0
index 27fe0cd5b2d7..7ba7d384889e 100644
@@ -1717,4 +1717,9 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg
 
 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
+#ifndef __KVM_NVHE_HYPERVISOR__
+struct pkvm_iommu_ops;
+int 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 +11 -0
new file mode 100644
index 000000000000..83abe1a443c6
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ARM64_KVM_NVHE_IOMMU_H__
+#define __ARM64_KVM_NVHE_IOMMU_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..ef456eff42d2
@@ -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;
+
+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 75b00c323310..9607d1b18a88 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>
@@ -328,6 +329,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 +25 -0
new file mode 100644
index 000000000000..30a3862e93d7
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Mostafa Saleh <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+
+extern struct pkvm_iommu_ops *kvm_nvhe_sym(pkvm_iommu_ops);
+
+static DEFINE_MUTEX(pkvm_iommu_reg_lock);
+
+int 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) = hyp_ops;
+	return 0;
+}
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#22 lore raw

PATCH v8 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 created after the driver init, then on every permission change a callback would be called for the IOMMU driver to update the page table.

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

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

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

Option #2 was implemented here.

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

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

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h |   5 +
 arch/arm64/kvm/hyp/nvhe/iommu.c         | 130 +++++++++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c   |  27 ++---
 3 files changed, 146 insertions(+), 16 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +5 -0
index 83abe1a443c6..2e35ec01c75d 100644
@@ -2,10 +2,15 @@
 #ifndef __ARM64_KVM_NVHE_IOMMU_H__
 #define __ARM64_KVM_NVHE_IOMMU_H__
 
+#include <asm/kvm_pgtable.h>
+
 struct pkvm_iommu_ops {
 	int (*init)(void);
+	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 +126 -4

142 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -12
index c1a8fd811c0f..da5014e4e96b 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>
@@ -603,16 +604,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)
@@ -625,8 +625,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,
@@ -1029,6 +1031,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.55.0.1082.g2b9226bbc0-goog
#23 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: arm64: pkvm: guest-triggerable hyp panic due to WARN_ON misuse --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: arm64: pkvm: guest-triggerable hyp panic due to WARN_ON misuse --

commit 68e26689446b14f6e5c91a79e4493c62586f9fb4
Author: Mostafa Saleh <[email protected]>
Subject: KVM: arm64: iommu: Shadow host stage-2 page table

This commit creates a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation. An initial snapshot is created after the driver init, and on permission changes, a callback updates the IOMMU page table.

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0
index ef456eff42d29..3637b0327d9e8 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +2 -1
@@ -4,16 +4,138 @@
 int pkvm_iommu_init(void)
 {
+	int ret;
+
 	/* Keep DMA isolation optional. */
-	if (!pkvm_iommu_ops || !pkvm_iommu_ops->init)

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

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +7 -1
+	if (!pkvm_iommu_ops || !pkvm_iommu_ops->init ||
+	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
-	return pkvm_iommu_ops->init();
+	ret = pkvm_iommu_ops->init();
+	if (ret)
+		return ret;
+
+	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

[Severity: High]
Does this unintentionally cause a hypervisor panic if the snapshot fails?

At EL2 (nVHE/pKVM), WARN_ON expands to a fatal BRK instruction rather than a warn-and-continue. Wrapping a fallible operation like this triggers a panic during initialization instead of returning the error.

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/iommu.c +1 -0
+}

[ ... ]

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index c1a8fd811c0fa..da5014e4e96b6 100644

[ ... ]

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -9
@@ -603,16 +604,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);
-	}
-

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size, · line 615

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +9 -1
-	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));

[Severity: High]
Does this create a dead-branch trap that skips rolling back the page state?

Since WARN_ON acts as a panic at EL2, any failure in the IOMMU mapping call will immediately crash the hypervisor. It also leaves the earlier mutation from __host_update_page_state without a rollback path on failure.

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) · line 631

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -2
 }
 
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
@@ -625,8 +625,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));

[Severity: High]
Is it intentional that a failure here panics instead of returning the error?

Assigning WARN_ON to a variable suggests an expectation that it returns, but at EL2 it triggers a fatal BRK instruction. This also leaves the prior page state update without a rollback.

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 		break;

[ ... ]

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn) · line 1034

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -0
@@ -1029,6 +1031,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));

[Severity: High]
Can an untrusted guest intentionally trigger a host denial of service here?

By triggering memory sharing hypercalls, a guest might exhaust the IOMMU page pool, forcing an allocation failure in pkvm_iommu_host_stage2_idmap. Wrapping this fallible outcome in WARN_ON will trigger a hypervisor panic.

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 unlock:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#24 lore raw

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

Quoted 41 lines · On Tue, Sep 22, 2026 at 01:23:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: arm64: pkvm: guest-triggerable hyp panic due to WARN_ON misuse --

commit 68e26689446b14f6e5c91a79e4493c62586f9fb4
Author: Mostafa Saleh <[email protected]>
Subject: KVM: arm64: iommu: Shadow host stage-2 page table

This commit creates a page-table for the IOMMU that shadows the host CPU stage-2 to establish DMA isolation. An initial snapshot is created after the driver init, and on permission changes, a callback updates the IOMMU page table.

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0
index ef456eff42d29..3637b0327d9e8 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +2 -1
@@ -4,16 +4,138 @@
 int pkvm_iommu_init(void)
 {
+	int ret;
+
 	/* Keep DMA isolation optional. */
-	if (!pkvm_iommu_ops || !pkvm_iommu_ops->init)

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

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +7 -1
+	if (!pkvm_iommu_ops || !pkvm_iommu_ops->init ||
+	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
-	return pkvm_iommu_ops->init();
+	ret = pkvm_iommu_ops->init();
+	if (ret)
+		return ret;
+
+	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

[Severity: High]
Does this unintentionally cause a hypervisor panic if the snapshot fails?

Quoted 3 lines

At EL2 (nVHE/pKVM), WARN_ON expands to a fatal BRK instruction rather than a warn-and-continue. Wrapping a fallible operation like this triggers a panic during initialization instead of returning the error.

That is intentional as there is no roll-back beyond this point, this pattern is commonly used with pKVM.

Quoted 39 lines
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +1 -0
+}

[ ... ]

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index c1a8fd811c0fa..da5014e4e96b6 100644

[ ... ]

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -9
@@ -603,16 +604,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);
-	}
-

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size, · line 615

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +9 -1
-	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));

[Severity: High]
Does this create a dead-branch trap that skips rolling back the page state?

Quoted 3 lines

Since WARN_ON acts as a panic at EL2, any failure in the IOMMU mapping call will immediately crash the hypervisor. It also leaves the earlier mutation from __host_update_page_state without a rollback path on failure.

This is added for readability, not really a problem.

Quoted 18 lines
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 }
 
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) · line 631

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -2
@@ -625,8 +625,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));

[Severity: High]
Is it intentional that a failure here panics instead of returning the error?

Quoted 3 lines

Assigning WARN_ON to a variable suggests an expectation that it returns, but at EL2 it triggers a fatal BRK instruction. This also leaves the prior page state update without a rollback.

No need for rollback as it panics.

Quoted 14 lines
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 		break;

[ ... ]

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn) · line 1034

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -0
@@ -1029,6 +1031,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));

[Severity: High]
Can an untrusted guest intentionally trigger a host denial of service here?

Quoted 4 lines

By triggering memory sharing hypercalls, a guest might exhaust the IOMMU page pool, forcing an allocation failure in pkvm_iommu_host_stage2_idmap. Wrapping this fallible outcome in WARN_ON will trigger a hypervisor panic.

The IOMMU can't be exhasuted by design, the same WARN_ON() exist in the line above for the CPU page table update also.

Thanks,
Mostafa

Quoted 4 lines
Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 unlock:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#25 lore raw

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

In reply to Mostafa Saleh

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

As this is too early for drivers to use initcalls, the number of pages allocated is set from command line "kvm-arm.iommu_pgt_mem".

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

---
 .../admin-guide/kernel-parameters.txt         |  5 +++
 arch/arm64/include/asm/kvm_host.h             |  3 +-
 arch/arm64/kvm/hyp/include/nvhe/iommu.h       |  8 +++-
 arch/arm64/kvm/hyp/nvhe/iommu.c               | 21 +++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               | 11 ++++-
 arch/arm64/kvm/iommu.c                        | 41 ++++++++++++++++++-
 arch/arm64/kvm/pkvm.c                         |  1 +
 7 files changed, 85 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 33cd30996e47..408a1f431782 100644
@@ -3240,6 +3240,11 @@ 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=).
+
 	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 7ba7d384889e..743d812e8ee0 100644
@@ -1719,7 +1719,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext);
 
 #ifndef __KVM_NVHE_HYPERVISOR__
 struct pkvm_iommu_ops;
-int pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops);
+int pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops, unsigned int nr_pages);
+unsigned int pkvm_iommu_pages(void);
 #endif
 
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/hyp/include/nvhe/iommu.h +7 -1
index 2e35ec01c75d..1fa728ab47d4 100644
@@ -9,8 +9,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 +20 -1
index 3637b0327d9e..cacab0dc462a 100644
@@ -16,6 +16,7 @@ struct pkvm_iommu_ops *pkvm_iommu_ops;
 
 /* Protected by host_mmu.lock */
 static bool pkvm_idmap_initialized;
+static struct hyp_pool iommu_pages_pool;
 
 static inline int pkvm_to_iommu_prot(enum kvm_pgtable_prot prot)
 {
@@ -113,7 +114,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;
 
@@ -122,6 +123,14 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)
+		return -ENOMEM;
+
+	ret = hyp_pool_init(&iommu_pages_pool, hyp_virt_to_pfn(pool_base),
+			    nr_pages, 0);
+	if (ret)
+		return ret;
+
 	ret = pkvm_iommu_ops->init();
 	if (ret)
 		return ret;
@@ -139,3 +148,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 9607d1b18a88..7ce1fc2232da 100644
@@ -22,6 +22,8 @@
 
 unsigned long hyp_nr_cpus;
 
+unsigned int hyp_kvm_iommu_pages;
+
 #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_kvm_iommu_pages) {
+		iommu_base = hyp_early_alloc_contig(hyp_kvm_iommu_pages);
+		if (!iommu_base)
+			return -ENOMEM;
+	}
+
 	return 0;
 }
 
@@ -334,7 +343,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_kvm_iommu_pages);
 	if (ret)
 		goto out;
 
arch/arm64/kvm/iommu.c +40 -1

56 lines not on this page · raw message

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.55.0.1082.g2b9226bbc0-goog
#26 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] kvm: incorrect WARN_ON() on expected OOM during IOMMU shadow page table allocation - [Low] kvm…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kvm: incorrect WARN_ON() on expected OOM during IOMMU shadow page table allocation
- [Low] kvm: math error in early_iommu_pgt_mem() bounds check artificially limits IOMMU pool to 4GB --

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

KVM: arm64: iommu: Add memory pool

This commit introduces an IOMMU memory pool for shadow page tables, allocating it early from the KVM carveout based on the kvm-arm.iommu_pgt_mem command line parameter.

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0

[ ... ]

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +1 -0
@@ -122,6 +123,14 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)

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

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +7 -0
+		return -ENOMEM;
+
+	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: Medium]
Does wrapping pkvm_iommu_snapshot_host_stage2() in WARN_ON() safely handle expected out-of-memory conditions?

Looking at the rest of pkvm_iommu_init(), it ends with:

	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

The snapshot function walks the host page tables and allocates memory for IOMMU shadow mappings from the fixed-size pool configured by the host via kvm-arm.iommu_pgt_mem.

If the user-provided pool is too small, the allocation fails and returns -ENOMEM. At EL2, WARN_ON() expands to a fatal BRK instruction, which panics the hypervisor and crashes the host.

Could we return the error directly without panicking, since this turns a recoverable initialization failure into a host-trusted denial of service vector?

Quoted 3 lines
arch/arm64/kvm/iommu.c +0 -0

[ ... ]

arch/arm64/kvm/iommu.c int pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops) · line 56

Quoted 10 lines
arch/arm64/kvm/iommu.c +10 -0
+static int __init early_iommu_pgt_mem(char *arg)
+{
+	unsigned long long requested_size;
+
+	if (!arg)
+		return -EINVAL;
+
+	requested_size = memparse(arg, NULL);
+
+	if (requested_size > UINT_MAX) {

[Severity: Low]
Is this math check bounding the IOMMU pool size to 4GB?

The early_iommu_pgt_mem() function parses the user-requested size into bytes, and bounds requested_size against UINT_MAX.

Since the target variable hyp_kvm_iommu_pages stores a page count as an unsigned int, the byte limit could safely be much larger, up to (u64)UINT_MAX * PAGE_SIZE.

Does this artificially reject valid configurations on systems with massive RAM that might require larger page table pools?

Quoted 8 lines
arch/arm64/kvm/iommu.c +8 -0
+		kvm_err("kvm-arm.iommu_pgt_mem is too large\n");
+		return -EINVAL;
+	}
+
+	kvm_nvhe_sym(hyp_kvm_iommu_pages) = DIV_ROUND_UP(requested_size, PAGE_SIZE);
+	return 0;
+}
+early_param("kvm-arm.iommu_pgt_mem", early_iommu_pgt_mem);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#27 lore raw

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

Quoted 46 lines · On Tue, Sep 22, 2026 at 01:25:30PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kvm: incorrect WARN_ON() on expected OOM during IOMMU shadow page table allocation
- [Low] kvm: math error in early_iommu_pgt_mem() bounds check artificially limits IOMMU pool to 4GB --

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

KVM: arm64: iommu: Add memory pool

This commit introduces an IOMMU memory pool for shadow page tables, allocating it early from the KVM carveout based on the kvm-arm.iommu_pgt_mem command line parameter.

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +0 -0

[ ... ]

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +1 -0
@@ -122,6 +123,14 @@ int pkvm_iommu_init(void)
 	    !pkvm_iommu_ops->host_stage2_idmap)
 		return 0;
 
+	if (!nr_pages)

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

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/iommu.c +7 -0
+		return -ENOMEM;
+
+	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: Medium]
Does wrapping pkvm_iommu_snapshot_host_stage2() in WARN_ON() safely handle expected out-of-memory conditions?

Looking at the rest of pkvm_iommu_init(), it ends with:

	return WARN_ON(pkvm_iommu_snapshot_host_stage2());

The snapshot function walks the host page tables and allocates memory for IOMMU shadow mappings from the fixed-size pool configured by the host via kvm-arm.iommu_pgt_mem.

Quoted 7 lines

If the user-provided pool is too small, the allocation fails and returns -ENOMEM. At EL2, WARN_ON() expands to a fatal BRK instruction, which panics the hypervisor and crashes the host.

Could we return the error directly without panicking, since this turns a recoverable initialization failure into a host-trusted denial of service vector?

Yes, as mentioned in patch-7, this is by design, the IOMMU pool should never get -ENOMEM similar to the CPU pool.

Otherwise, recovery from this would be too complicated as the hypervisor already poked the HW.

Quoted 22 lines
Quoted 4 lines
arch/arm64/kvm/iommu.c +0 -0

[ ... ]

arch/arm64/kvm/iommu.c int pkvm_iommu_register_driver(struct pkvm_iommu_ops *hyp_ops) · line 56

Quoted 10 lines
arch/arm64/kvm/iommu.c +10 -0
+static int __init early_iommu_pgt_mem(char *arg)
+{
+	unsigned long long requested_size;
+
+	if (!arg)
+		return -EINVAL;
+
+	requested_size = memparse(arg, NULL);
+
+	if (requested_size > UINT_MAX) {

[Severity: Low]
Is this math check bounding the IOMMU pool size to 4GB?

The early_iommu_pgt_mem() function parses the user-requested size into bytes, and bounds requested_size against UINT_MAX.

Quoted 6 lines

Since the target variable hyp_kvm_iommu_pages stores a page count as an unsigned int, the byte limit could safely be much larger, up to (u64)UINT_MAX * PAGE_SIZE.

Does this artificially reject valid configurations on systems with massive RAM that might require larger page table pools?

Not artificially, that is added due to the hypervisor buddy allocator limits, I sent a separate patch for that:
https://lore.kernel.org/all/[email protected]/

Quoted 12 lines
Quoted 9 lines
arch/arm64/kvm/iommu.c +8 -0
+		kvm_err("kvm-arm.iommu_pgt_mem is too large\n");
+		return -EINVAL;
+	}
+
+	kvm_nvhe_sym(hyp_kvm_iommu_pages) = DIV_ROUND_UP(requested_size, PAGE_SIZE);
+	return 0;
+}
+early_param("kvm-arm.iommu_pgt_mem", early_iommu_pgt_mem);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#28 lore raw

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

In reply to Mostafa Saleh

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

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

---
 arch/arm64/kvm/hyp/include/nvhe/iommu.h |  2 ++
 arch/arm64/kvm/hyp/nvhe/iommu.c         | 14 ++++++++++++++
 arch/arm64/kvm/hyp/nvhe/mem_protect.c   | 15 +++++++++++++++
 3 files changed, 31 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/iommu.h +2 -0
index 1fa728ab47d4..3f16879b5fa7 100644
@@ -7,6 +7,7 @@
 struct pkvm_iommu_ops {
 	int (*init)(void);
 	int (*host_stage2_idmap)(phys_addr_t start, phys_addr_t end, int prot);
+	bool (*dabt_handler)(struct user_pt_regs *regs, u64 esr, u64 addr);
 };
 
 int pkvm_iommu_init(void *pool_base, unsigned int nr_pages);
@@ -18,5 +19,6 @@ int pkvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
 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 +14 -0
index cacab0dc462a..b7b11f6616cd 100644
@@ -5,8 +5,11 @@
  * Copyright (C) 2022 Linaro Ltd.
  */
 #include <linux/iommu.h>
+#include <asm/kvm_hyp.h>
 #include <asm/kvm_pkvm.h>
 
+#include <hyp/adjust_pc.h>
+
 #include <nvhe/iommu.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/spinlock.h>
@@ -158,3 +161,14 @@ 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)
+{
+	if (pkvm_iommu_ops && pkvm_iommu_ops->dabt_handler &&
+	    pkvm_iommu_ops->dabt_handler(regs, esr, addr)) {
+		/* DABT handled by the driver, skip to next instruction. */
+		kvm_skip_host_instr();
+		return true;
+	}
+	return false;
+}
arch/arm64/kvm/hyp/nvhe/mem_protect.c +15 -0
index da5014e4e96b..863c571e47e4 100644
@@ -758,6 +758,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;
@@ -780,6 +786,15 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
 	BUG_ON(!(fault.hpfar_el2 & HPFAR_EL2_NS));
 	addr = FIELD_GET(HPFAR_EL2_FIPA, fault.hpfar_el2) << 12;
 
+	/*
+	 * Emulate data aborts for IOMMU drivers, other access will be denied
+	 * by host_stage2_adjust_range()
+	 */
+	if (is_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)))
+		return;
+
 	switch (host_stage2_idmap(addr)) {
 	case -EPERM:
 		host_inject_mem_abort(host_ctxt);
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#29 lore raw

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

In reply to Mostafa Saleh

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

Add the skeleton for an Arm SMMUv3 driver at EL2.

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

This array will be populated in the next patch.

---
 arch/arm64/kvm/hyp/nvhe/Makefile              |  5 +
 drivers/iommu/arm/Kconfig                     | 12 +++
 .../arm/arm-smmu-v3/arm-smmu-v3-common-lib.h  |  4 +
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    | 31 +++++++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 91 +++++++++++++++++++
 5 files changed, 143 insertions(+)
 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/arm-smmu-v3.c
arch/arm64/kvm/hyp/nvhe/Makefile +5 -0
index 7cb79db9bc47..aa76c0f3f147 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.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..fdbed6a51224 100644
@@ -141,3 +141,15 @@ config QCOM_IOMMU
 	select ARM_DMA_USE_IOMMU
 	help
 	  Support for IOMMU on certain Qualcomm SoCs.
+
+config ARM_SMMU_V3_PKVM
+	bool "ARM SMMUv3 support for protected Virtual Machines"
+	depends on KVM && ARM_SMMU_V3=y
+	help
+	  Enable a SMMUv3 driver in the KVM hypervisor, to protect VMs against
+	  memory accesses from devices owned by the host.
+	  This works along side ARM_SMMU_V3 where the kernel driver manages the
+	  stage-1 of the SMMUv3, and the hypervisor driver will manage the
+	  stage-2 transparently through trap and emulate.
+
+	  Say Y here if you intend to enable KVM in protected mode.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-common-lib.h +4 -0
index 9a7064c8e879..3a9d72988942 100644
@@ -15,6 +15,10 @@
  */
 #ifndef __KVM_NVHE_HYPERVISOR__
 typedef struct arm_smmu_device ARM_SMMU_OBJ;
+#else
+#include "pkvm/arm-smmu-v3-hyp.h"
+
+typedef struct hyp_arm_smmu_v3_device ARM_SMMU_OBJ;
 #endif
 
 static_assert(__same_type(typeof_member(ARM_SMMU_OBJ, features), u32));
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h new file +31 -0
new file mode 100644
index 000000000000..17b9454dab85
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __KVM_ARM_SMMU_V3_HYP_H
+#define __KVM_ARM_SMMU_V3_HYP_H
+
+#include <asm/kvm_asm.h>
+
+/*
+ * Parameters from the trusted host:
+ * @mmio_addr		base address of the SMMU registers
+ * @mmio_size		size of the registers resource
+ *
+ * Other members are filled and used at runtime by the SMMU driver.
+ * @base		Virtual address of SMMU registers
+ */
+struct hyp_arm_smmu_v3_device {
+	phys_addr_t		mmio_addr;
+	size_t			mmio_size;
+	void __iomem		*base;
+	u32			features;
+	u32			options;
+	unsigned long		oas;
+	unsigned long		pgsize_bitmap;
+};
+
+extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
+#define kvm_hyp_arm_smmu_v3_count kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count)
+
+extern struct hyp_arm_smmu_v3_device *kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus);
+#define kvm_hyp_arm_smmu_v3_smmus kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_smmus)
+
+#endif /* __KVM_ARM_SMMU_V3_HYP_H */
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#30 lore raw

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

Quoted 5 lines · Mostafa Saleh

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

Add the skeleton for an Arm SMMUv3 driver at EL2.

The driver rely on an array of SMMUv3s on the system, where at

s/rely/relies

drivers/iommu/arm/Kconfig config QCOM_IOMMU · line 147

Quoted 9 lines

+++ b/drivers/iommu/arm/Kconfig

drivers/iommu/arm/Kconfig +4 -0
@@ -141,3 +141,15 @@ config QCOM_IOMMU
 	select ARM_DMA_USE_IOMMU
 	help
 	  Support for IOMMU on certain Qualcomm SoCs.
+
+config ARM_SMMU_V3_PKVM
+	bool "ARM SMMUv3 support for protected Virtual Machines"
+	depends on KVM && ARM_SMMU_V3=y

Should it depend on OF?

drivers/iommu/arm/Kconfig config QCOM_IOMMU · line 149

Quoted 2 lines
drivers/iommu/arm/Kconfig +2 -0
+	help
+	  Enable a SMMUv3 driver in the KVM hypervisor, to protect VMs against

s/a SMMUv3/an SMMUv3

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

Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h new file +11 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __KVM_ARM_SMMU_V3_HYP_H
+#define __KVM_ARM_SMMU_V3_HYP_H
+
+#include <asm/kvm_asm.h>
+
+/*
+ * Parameters from the trusted host:
+ * @mmio_addr		base address of the SMMU registers
+ * @mmio_size		size of the registers resource

Are these still in the host's physical address space or guest's?

If it's still "host" (though trusted), what's different from the ioaddr in the main driver?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 17

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +2 -0
+size_t __ro_after_init kvm_hyp_arm_smmu_v3_count;
+struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;

Should kvm_hyp_arm_smmu_v3_smmus be __ro_after_init as well?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 22

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +5 -0
+
+#define for_each_smmu(smmu) \
+	for ((smmu) = kvm_hyp_arm_smmu_v3_smmus; \
+	     (smmu) != &kvm_hyp_arm_smmu_v3_smmus[kvm_hyp_arm_smmu_v3_count]; \
+	     (smmu)++)

"smmu" sounds too generic. Maybe for_each_pkvm_smmu?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 51

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +2 -0
+/* Called while is the host is still trusted. */
+static int smmu_init(void)

s/while is/while

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 90

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +4 -0
+/* Shared with the kernel driver in EL1 */
+struct pkvm_iommu_ops smmu_ops = {
+	.init				= smmu_init,
+	.host_stage2_idmap		= smmu_host_stage2_idmap,

Can we add a "pvkm_arm_smmu_" prefix for the ops and functions here?

Nicolin

#31 lore raw

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

Quoted 8 lines · Nicolin Chen
Quoted 5 lines · Mostafa Saleh

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

Add the skeleton for an Arm SMMUv3 driver at EL2.

The driver rely on an array of SMMUv3s on the system, where at

s/rely/relies

Will do.

Quoted 12 lines

drivers/iommu/arm/Kconfig config QCOM_IOMMU · line 147

Quoted 10 lines

+++ b/drivers/iommu/arm/Kconfig

drivers/iommu/arm/Kconfig +4 -0
@@ -141,3 +141,15 @@ config QCOM_IOMMU
 	select ARM_DMA_USE_IOMMU
 	help
 	  Support for IOMMU on certain Qualcomm SoCs.
+
+config ARM_SMMU_V3_PKVM
+	bool "ARM SMMUv3 support for protected Virtual Machines"
+	depends on KVM && ARM_SMMU_V3=y

Should it depend on OF?

Makes sense, I will add it.

Quoted 5 lines

drivers/iommu/arm/Kconfig config QCOM_IOMMU · line 149

Quoted 3 lines
drivers/iommu/arm/Kconfig +2 -0
+	help
+	  Enable a SMMUv3 driver in the KVM hypervisor, to protect VMs against

s/a SMMUv3/an SMMUv3

Will do.

Quoted 16 lines

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

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h new file +11 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __KVM_ARM_SMMU_V3_HYP_H
+#define __KVM_ARM_SMMU_V3_HYP_H
+
+#include <asm/kvm_asm.h>
+
+/*
+ * Parameters from the trusted host:
+ * @mmio_addr		base address of the SMMU registers
+ * @mmio_size		size of the registers resource

Are these still in the host's physical address space or guest's?

Quoted 2 lines

If it's still "host" (though trusted), what's different from the ioaddr in the main driver?

This is the physical address of the SMMUv3 as read from the device tree.

The "base" pointer is for the hypervisor virtual address which is created in the private mapping range (similar to ioremap())

The hypervisor doesn't keep the host VA anywhere. Also note that there is no guest support at the moment, only the host.

Quoted 5 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 17

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +2 -0
+size_t __ro_after_init kvm_hyp_arm_smmu_v3_count;
+struct hyp_arm_smmu_v3_device *kvm_hyp_arm_smmu_v3_smmus;

Should kvm_hyp_arm_smmu_v3_smmus be __ro_after_init as well?

That won't work at the moment as the hypervisor writes this pointer after __ro_after_init to convert the kernel VA to hypervisor VA unlike the count which is set once at boot.
It might be possible to make the kernel do this conversion early, I will need to double check.

It's worth noting that __ro_after_init is just for the hypervisor hardening and not for protection as those are protected by stage-2 MMU.

Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 22

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +5 -0
+
+#define for_each_smmu(smmu) \
+	for ((smmu) = kvm_hyp_arm_smmu_v3_smmus; \
+	     (smmu) != &kvm_hyp_arm_smmu_v3_smmus[kvm_hyp_arm_smmu_v3_count]; \
+	     (smmu)++)

"smmu" sounds too generic. Maybe for_each_pkvm_smmu?

This macro is private to this driver, so I guess that's enough, also smmu is used everywhere else, but no strong opinion.

Quoted 5 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 51

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +2 -0
+/* Called while is the host is still trusted. */
+static int smmu_init(void)

s/while is/while

Will do.

Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c line 90

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c new file +4 -0
+/* Shared with the kernel driver in EL1 */
+struct pkvm_iommu_ops smmu_ops = {
+	.init				= smmu_init,
+	.host_stage2_idmap		= smmu_host_stage2_idmap,

Can we add a "pvkm_arm_smmu_" prefix for the ops and functions here?

Similar to above, these symbols are private to this file and the hypervisor symbols gets prefixed with "__kvm_nvhe_" anyway, so they never clash with the kernel. But no strong opinon.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#32 lore raw

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

In reply to Mostafa Saleh

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

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

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

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

187 lines not on this page · raw message

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

79 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#33 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] iommu: pKVM DMA isolation bypass via unbound SMMUs falling back to host driver - [High] iommu…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] iommu: pKVM DMA isolation bypass via unbound SMMUs falling back to host driver
- [High] iommu: Use-after-free of smmu during platform_device removal
--

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

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

This commit introduces the pKVM host driver for the Arm SMMUv3, allowing the hypervisor to trap and emulate SMMU management when KVM runs in protected mode. The driver probes these SMMUs and creates auxiliary devices for them on the aux bus.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +0 -0
new file mode 100644
index 0000000000000..9947d3a44304b

[ ... ]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 76

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+static int smmuv3_nesting_probe(struct platform_device *pdev)
+{
+	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+
+	/* Only device tree, ACPI not supported. */
+	if (!dev->of_node)
+		return -EINVAL;

[Severity: Critical]
Can an adversary bypass pKVM stage-2 memory protections here?

If an SMMU fails to bind to the hypervisor due to early returns in this probe function (like missing a device tree node or hitting the cavium erratum below), it is excluded from kvm_arm_smmu_array and the hypervisor does not trap its MMIO.

When the native host arm_smmu_driver registers at device_initcall, it can successfully bind to this unbound SMMU. Does this allow an adversary host kernel at EL1 to natively program this untrapped SMMU to perform arbitrary DMA into hypervisor or guest memory?

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +10 -0
+
+	if (kvm_arm_smmu_cur >= kvm_arm_smmu_count)
+		return -ENOSPC;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -ENODEV;
+
+	if (of_property_read_bool(dev->of_node, "cavium,cn9900-broken-page1-regspace"))
+		return -EINVAL;

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 2043c6dc1bdf8..d6d1e2d30ef63 100644

[ ... ]

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

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

[Severity: High]
Can this devres setup lead to a use-after-free during platform device removal?

If the parent physical platform device is destroyed (for example, via dynamic DT overlay removal), the driver core calls devres_release_all() which processes cleanups in reverse order of addition (LIFO).

Because arm_smmu_device_probe() adds the smmu allocation to the parent's devres list after the auxiliary device was created in smmu_create_aux_device(), the smmu memory gets freed first.

Following that, the auxiliary_device_destroy devres action executes, unregistering the aux device and triggering driver unbind via arm_smmu_device_remove_emu() and arm_smmu_device_remove(). Does this result in dereferencing the already freed smmu struct?

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11 -0
+	if (ret)
+		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));
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#34 lore raw

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

Quoted 39 lines · On Tue, Sep 22, 2026 at 01:28:14PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] iommu: pKVM DMA isolation bypass via unbound SMMUs falling back to host driver
- [High] iommu: Use-after-free of smmu during platform_device removal
--

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

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

This commit introduces the pKVM host driver for the Arm SMMUv3, allowing the hypervisor to trap and emulate SMMU management when KVM runs in protected mode. The driver probes these SMMUs and creates auxiliary devices for them on the aux bus.

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +0 -0
new file mode 100644
index 0000000000000..9947d3a44304b

[ ... ]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 76

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+static int smmuv3_nesting_probe(struct platform_device *pdev)
+{
+	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+
+	/* Only device tree, ACPI not supported. */
+	if (!dev->of_node)
+		return -EINVAL;

[Severity: Critical]
Can an adversary bypass pKVM stage-2 memory protections here?

If an SMMU fails to bind to the hypervisor due to early returns in this probe function (like missing a device tree node or hitting the cavium erratum below), it is excluded from kvm_arm_smmu_array and the hypervisor does not trap its MMIO.

Quoted 4 lines

When the native host arm_smmu_driver registers at device_initcall, it can successfully bind to this unbound SMMU. Does this allow an adversary host kernel at EL1 to natively program this untrapped SMMU to perform arbitrary DMA into hypervisor or guest memory?

Yes, nothing really we can do about this, the system might have:
- Incompatible SMMUv3 instance.
- Device behind another IOMMUs.
- Devices without IOMMUs.

A system shipping this code should ensure that the driver is probed successfully on their HW.

Quoted 35 lines
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +10 -0
+
+	if (kvm_arm_smmu_cur >= kvm_arm_smmu_count)
+		return -ENOSPC;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -ENODEV;
+
+	if (of_property_read_bool(dev->of_node, "cavium,cn9900-broken-page1-regspace"))
+		return -EINVAL;

[ ... ]

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 2043c6dc1bdf8..d6d1e2d30ef63 100644

[ ... ]

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

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

[Severity: High]
Can this devres setup lead to a use-after-free during platform device removal?

Quoted 7 lines

If the parent physical platform device is destroyed (for example, via dynamic DT overlay removal), the driver core calls devres_release_all() which processes cleanups in reverse order of addition (LIFO).

Because arm_smmu_device_probe() adds the smmu allocation to the parent's devres list after the auxiliary device was created in smmu_create_aux_device(), the smmu memory gets freed first.

Two points:
1) At the moment the device can't be released as suppress_bind_attrs is true. There is no kernel user for the dynamic overlay it is added for testing AFAIU.

2) AFAICT, __devm_auxiliary_device_create() calls, devm_add_action_or_reset() for the device destroy, so the aux device will destroyed before the platform device.

Quoted 6 lines

Following that, the auxiliary_device_destroy devres action executes, unregistering the aux device and triggering driver unbind via arm_smmu_device_remove_emu() and arm_smmu_device_remove(). Does this result in dereferencing the already freed smmu struct?

Same as above.

Thanks,
Mostafa

Quoted 14 lines
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11 -0
+	if (ret)
+		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));
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#35 lore raw

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

In reply to Mostafa Saleh
Quoted 9 lines · Mostafa Saleh

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

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

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

Missing space before "so".

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 3

Quoted 5 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +3 -0
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * pKVM host driver for the Arm SMMUv3

I am a bit confused by pkvm/arm-smmu-v3.c and arm-smmu-v3-kvm.c

It would be nicer to have a design doc or diagram here and the other place to show their relationships (with host and KVM too).

Naming-wise, could it be arm-smmu-v3-pkvm? I also wonder if we could move it into the pkvm folder:
pkvm/arm-smmu-v3-hyp.c
pkvm/arm-smmu-v3.c

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ */
+#include <asm/kvm_mmu.h>
+#include <asm/kvm_pkvm.h>
+
+#include <linux/auxiliary_bus.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 22

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +10 -0
+#include <linux/platform_device.h>
+
+#include "arm-smmu-v3.h"
+#include "pkvm/arm-smmu-v3-hyp.h"
+
+extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
+
+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;

That spaces/tabs in those two lines look a bit odd..

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 57

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+static unsigned int smmu_hyp_pgt_pages(void)
+{
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "arm,smmu-v3");
+
+	/*
+	 * SMMUv3 uses the same format as the CPU stage-2 and hence have the same memory
+	 * requirements, we add extra 500 pages for L2 STEs.
+	 * Only one set of memory is allocated as the page table is shared between all
+	 * the SMMUs.

Mind briefly elaborate the 500 pages in this notes? Why pick 500?

Also, there is no hard requirement, but the main driver still wraps inline comments at 80 cols. So, it would be nicer for this driver to follow that.

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+	 */
+	if (np) {
+		of_node_put(np);
+		return host_s2_pgtable_pages() + 500;
+	}
+
+	return 0;
+}
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 76

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +10 -0
+static struct platform_driver smmuv3_nesting_driver;
+static int smmuv3_nesting_probe(struct platform_device *pdev)
+{
+	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+
+	/* Only device tree, ACPI not supported. */
+	if (!dev->of_node)
+		return -EINVAL;

Sashiko reported a critical finding, which sounds plausible to me:
"
Can an adversary bypass pKVM stage-2 memory protections here? If an SMMU fails to bind to the hypervisor due to early returns in this probe function (like missing a device tree node or hitting the cavium erratum below), it is excluded from kvm_arm_smmu_array and the hypervisor does not trap its MMIO.
When the native host arm_smmu_driver registers at device_initcall, it can successfully bind to this unbound SMMU. Does this allow an adversary host kernel at EL1 to natively program this untrapped SMMU to perform arbitrary DMA into hypervisor or guest memory?
"

Would you please justify?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 79

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +2 -0
+	if (kvm_arm_smmu_cur >= kvm_arm_smmu_count)
+		return -ENOSPC;

Both counts and probe() are coming from Device Tree. So, it doesn't seem possible unless a kernel bug. Should it WARN_ON?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 93

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +6 -0
+	smmu->mmio_addr = res->start;
+	smmu->mmio_size = resource_size(res);
+	if (smmu->mmio_size < SZ_128K) {
+		dev_err(dev, "MMIO region too small(%pr)\n", res);
+		return -EINVAL;
+	}

Should it reject !PAGE_ALIGNED(addr|size) like the other driver?

Quoted 2 lines
(quoted diff) +2 -0
+static int __init kvm_arm_smmu_v3_register(void)
+{

[...]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 140

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +5 -0
+out_err:
+	kvm_arm_smmu_count = 0;
+	kvm_arm_smmu_array = NULL;
+	return ret;
+};

No ';' after '}'.

Quoted 1 line

+static int kvm_arm_smmu_v3_post_init(void)

__init?

Quoted 7 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
@@ -5203,6 +5204,72 @@ static struct platform_driver arm_smmu_driver = {
 module_driver(arm_smmu_driver, platform_driver_register,
 	      arm_smmu_driver_unregister);
 
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*

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

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
+ * Now we have 2 devices, the aux device bound to this driver, and pdev
+ * which is the physical platform device bound to the KVM driver but not used.
+ * However, this driver keeps using the platform device for 2 reasons:
+ * 1) Simplicity: Avoiding changing big parts of the code assuming
+ *    the underlying device is a platform device.
+ * 2) Dealing with DMA-API, irqs(MSIs), RPM... requires the physical device.
+ *
+ * That means arm_smmu_device_probe() allocates its devm resources on the
+ * platform device, where they are not freed when the aux device unbinds.
+ * The devres group bounds them to the lifetime of this binding instead.

There is an "unbinds", so I assume it should be:

s/bounds/binds

?

Nicolin

#36 lore raw

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

Quoted 5 lines · Nicolin Chen
Quoted 4 lines · Mostafa Saleh

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

Quoted 7 lines
Quoted 5 lines

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

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

Missing space before "so".

Will do.

Quoted 11 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 3

Quoted 6 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +3 -0
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * pKVM host driver for the Arm SMMUv3

I am a bit confused by pkvm/arm-smmu-v3.c and arm-smmu-v3-kvm.c

It would be nicer to have a design doc or diagram here and the other place to show their relationships (with host and KVM too).

I can add more info in the cover letter about the code split. Mainly:
- arm-smmu-v3-kvm.c: Is the kernel driver for KVM SMMUv3 (this runs in EL1 and can use all the kernel functions).
The main job of this driver is for discovery, it doesn't do anything else in the runtime.

- pkvm/arm-smmu-v3.c: Is the hypervisor driver (this runs in EL2), this is the driver that does the trap and emulate and is considered trusted. It can not access the kernel functions (all the code split is added for this file)

- After arm-smmu-v3-kvm.c creates the AUX devices, the upstream kernel driver (arm-smmu-v3.c) runs normally and the emulation code in pkvm/arm-smmu-v3.c should be transparent.

Quoted 6 lines

Naming-wise, could it be arm-smmu-v3-pkvm? I also wonder if we could move it into the pkvm folder:
pkvm/arm-smmu-v3-hyp.c
pkvm/arm-smmu-v3.c

Anything under pkvm/ runs in EL2, so arm-smmu-v3-kvm.c should stay outside. I am happy renaming both files (arm-smmu-v3-hyp.c, arm-smmu-v3-pkvm works)

Quoted 13 lines
Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +13 -0
+ *
+ * Copyright (C) 2022 Linaro Ltd.
+ */
+#include <asm/kvm_mmu.h>
+#include <asm/kvm_pkvm.h>
+
+#include <linux/auxiliary_bus.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#include "arm-smmu-v3.h"
+#include "pkvm/arm-smmu-v3-hyp.h"
Quoted 9 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 22

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +6 -0
+
+extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
+
+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;

That spaces/tabs in those two lines look a bit odd..

These are tabs to indent the variables, similar cases exist in the SMMUv3 driver, check arm_smmu_cmdq and friends for example.

Quoted 12 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 57

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+static unsigned int smmu_hyp_pgt_pages(void)
+{
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "arm,smmu-v3");
+
+	/*
+	 * SMMUv3 uses the same format as the CPU stage-2 and hence have the same memory
+	 * requirements, we add extra 500 pages for L2 STEs.
+	 * Only one set of memory is allocated as the page table is shared between all
+	 * the SMMUs.

Mind briefly elaborate the 500 pages in this notes? Why pick 500?

Quoted 4 lines

Also, there is no hard requirement, but the main driver still wraps inline comments at 80 cols. So, it would be nicer for this driver to follow that.

TBH, this is a bit of a hack. The hypervisor can not allocate memory at the runtime. All of the hypervisor memory comes from a carveout allocated at boot (see kvm_hyp_reserve())

So we allocate the worst case for memory mapping with leaf granule. But the hypervisor also need to allocate L2 pointers and the SID space can be massive making the upper limit for this too large.

However, smmu_hyp_pgt_pages() defines the minimum pages required, actual allocation comes from the command line, so it is possible to tune the system without re-compiling the kernel.

Thinking about it now, we can just drop the 500 as this is the lower bound, earlier versions of this series would allocate the carveout based on this size, but it is not needed anymore.

Quoted 33 lines
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +9 -0
+	 */
+	if (np) {
+		of_node_put(np);
+		return host_s2_pgtable_pages() + 500;
+	}
+
+	return 0;
+}
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 76

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +10 -0
+static struct platform_driver smmuv3_nesting_driver;
+static int smmuv3_nesting_probe(struct platform_device *pdev)
+{
+	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+
+	/* Only device tree, ACPI not supported. */
+	if (!dev->of_node)
+		return -EINVAL;

Sashiko reported a critical finding, which sounds plausible to me:
"
Can an adversary bypass pKVM stage-2 memory protections here? If an SMMU fails to bind to the hypervisor due to early returns in this probe function (like missing a device tree node or hitting the cavium erratum below), it is excluded from kvm_arm_smmu_array and the hypervisor does not trap its MMIO.
When the native host arm_smmu_driver registers at device_initcall, it can successfully bind to this unbound SMMU. Does this allow an adversary host kernel at EL1 to natively program this untrapped SMMU to perform arbitrary DMA into hypervisor or guest memory?
"

Quoted 1 line

Would you please justify?

Yes, Sashiko replied to this mail with it and I replied:
https://lore.kernel.org/all/[email protected]/

Basically, yes that is possible. Some SMMUs might not be in the device tree or might not be compatiable or the system might have an SMMUv2 or a completely different arch or no IOMMU at all.

pKVM can not defend against bad FW or missing HW, and if something i missing there is nothing to do about (and it won't even know)

There are some extra notes in the last patch that includes the documentation I can add more.

But this is also similar to how pKVM monitors SMC (TZ channels), by inspecting FFA traffic, if some system decided that TZ will accept other side channels for sharing physical addresses, pKVM can not do anything about it and the system is not secure by design.

Quoted 6 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 79

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +2 -0
+	if (kvm_arm_smmu_cur >= kvm_arm_smmu_count)
+		return -ENOSPC;

Both counts and probe() are coming from Device Tree. So, it doesn't seem possible unless a kernel bug. Should it WARN_ON?

I will add a WARN_OON()

Quoted 10 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 93

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +6 -0
+	smmu->mmio_addr = res->start;
+	smmu->mmio_size = resource_size(res);
+	if (smmu->mmio_size < SZ_128K) {
+		dev_err(dev, "MMIO region too small(%pr)\n", res);
+		return -EINVAL;
+	}

Should it reject !PAGE_ALIGNED(addr|size) like the other driver?

That check is in the hypervisor part but can be added here instead to save a trip. Although that should never happen.

Quoted 10 lines
Quoted 2 lines
(quoted diff) +2 -0
+static int __init kvm_arm_smmu_v3_register(void)
+{

[...]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 140

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +5 -0
+out_err:
+	kvm_arm_smmu_count = 0;
+	kvm_arm_smmu_array = NULL;
+	return ret;
+};

No ';' after '}'.

Oops, will fix it.

Quoted 5 lines
Quoted 2 lines

+static int kvm_arm_smmu_v3_post_init(void)

__init?

That should be possible, I will add it.

Quoted 20 lines
Quoted 7 lines

+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
@@ -5203,6 +5204,72 @@ static struct platform_driver arm_smmu_driver = {
 module_driver(arm_smmu_driver, platform_driver_register,
 	      arm_smmu_driver_unregister);
 
+#ifdef CONFIG_ARM_SMMU_V3_PKVM
+/*

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

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
+ * Now we have 2 devices, the aux device bound to this driver, and pdev
+ * which is the physical platform device bound to the KVM driver but not used.
+ * However, this driver keeps using the platform device for 2 reasons:
+ * 1) Simplicity: Avoiding changing big parts of the code assuming
+ *    the underlying device is a platform device.
+ * 2) Dealing with DMA-API, irqs(MSIs), RPM... requires the physical device.
+ *
+ * That means arm_smmu_device_probe() allocates its devm resources on the
+ * platform device, where they are not freed when the aux device unbinds.
+ * The devres group bounds them to the lifetime of this binding instead.

There is an "unbinds", so I assume it should be:

Quoted 3 lines

s/bounds/binds

?

I think "is bound", I am not sure if bounds/unbounds is correct.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#37 lore raw

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

Quoted 5 lines · Mostafa Saleh

I can add more info in the cover letter about the code split. Mainly:
- arm-smmu-v3-kvm.c: Is the kernel driver for KVM SMMUv3 (this runs in EL1 and can use all the kernel functions).
The main job of this driver is for discovery, it doesn't do anything else in the runtime.

Maybe call this el1 stub or something like that?

guest_pkvm_stub.c
pkvm/hyp.c

?

Quoted 11 lines
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 22

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +5 -0
+extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
+
+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;

That spaces/tabs in those two lines look a bit odd..

These are tabs to indent the variables, similar cases exist in the SMMUv3 driver, check arm_smmu_cmdq and friends for example.

Personally I hate this style, it is a huge PITA for maintenance and makes diffs bigger than they need to do. I remove it whenever I have the chance :)

Quoted 8 lines

TBH, this is a bit of a hack. The hypervisor can not allocate memory at the runtime. All of the hypervisor memory comes from a carveout allocated at boot (see kvm_hyp_reserve())

So we allocate the worst case for memory mapping with leaf granule. But the hypervisor also need to allocate L2 pointers and the SID space can be massive making the upper limit for this too large.

Quoted 7 lines

However, smmu_hyp_pgt_pages() defines the minimum pages required, actual allocation comes from the command line, so it is possible to tune the system without re-compiling the kernel.

Thinking about it now, we can just drop the 500 as this is the lower bound, earlier versions of this series would allocate the carveout based on this size, but it is not needed anymore.

I see, and you can't scan the ACPI to figure out all the SIDs used at this point to get an exact estimation?

Not being able to manage memory after boot is rather an extreme limitation.. But a huge amount of RMM's API complexity comes from its granular memory management so I can't really fault that..

Jason

#38 lore raw

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

Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Mostafa Saleh

I can add more info in the cover letter about the code split. Mainly:
- arm-smmu-v3-kvm.c: Is the kernel driver for KVM SMMUv3 (this runs in EL1 and can use all the kernel functions).
The main job of this driver is for discovery, it doesn't do anything else in the runtime.

Quoted 5 lines

Maybe call this el1 stub or something like that?

guest_pkvm_stub.c
pkvm/hyp.c

It is not really a stub and I was following the convention of arm-smmu-v3-(iommufd/test/sva) I think for the pkvm/ one it can be:
- pkvm/arm-smmu-v3-pkvm.c
- pkvm/arm-smmu-v3-nested.c
- pkvm/arm-smmu-v3-el2.c
- pkvm/arm-smmu-v3-hyp.c

All of those are fine and for the other one:
- arm-smmu-v3-{pkvm, nested, el2, hyp}-init.c
- arm-smmu-v3-pkvm-el1.c

Anything along this lines, otherwise it's too vague IMHO.

Quoted 11 lines

?

Quoted 9 lines
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c line 22

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c new file +5 -0
+extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
+
+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;

That spaces/tabs in those two lines look a bit odd..

Quoted 6 lines
Quoted 2 lines

These are tabs to indent the variables, similar cases exist in the SMMUv3 driver, check arm_smmu_cmdq and friends for example.

Personally I hate this style, it is a huge PITA for maintenance and makes diffs bigger than they need to do. I remove it whenever I have the chance :)

No strong opinion I can change it.

Quoted 13 lines
Quoted 13 lines

TBH, this is a bit of a hack. The hypervisor can not allocate memory at the runtime. All of the hypervisor memory comes from a carveout allocated at boot (see kvm_hyp_reserve())

So we allocate the worst case for memory mapping with leaf granule. But the hypervisor also need to allocate L2 pointers and the SID space can be massive making the upper limit for this too large.

However, smmu_hyp_pgt_pages() defines the minimum pages required, actual allocation comes from the command line, so it is possible to tune the system without re-compiling the kernel.

Quoted 6 lines
Quoted 3 lines

Thinking about it now, we can just drop the 500 as this is the lower bound, earlier versions of this series would allocate the carveout based on this size, but it is not needed anymore.

I see, and you can't scan the ACPI to figure out all the SIDs used at this point to get an exact estimation?

I thought about it, but it is complicated to scan the DT/ACPI/platform bus and I don't think a driver should be doing that; going through each device with IOMMUs, duplicating existing logic...

And even with that, things as PCI won't be there as they are dynamic.

I think the best way is to remove the 500 as it is hacky, and rely on the command line option.

Quoted 5 lines

Not being able to manage memory after boot is rather an extreme limitation.. But a huge amount of RMM's API complexity comes from its granular memory management so I can't really fault that..

Yes, that was one of the main sources of complexity with the para-virtual approach also.

Thanks,
Mostafa

Quoted 1 line

Jason

#39 lore raw

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

Quoted 14 lines · Mostafa Saleh
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Mostafa Saleh

I can add more info in the cover letter about the code split. Mainly:
- arm-smmu-v3-kvm.c: Is the kernel driver for KVM SMMUv3 (this runs in EL1 and can use all the kernel functions).
The main job of this driver is for discovery, it doesn't do anything else in the runtime.

Quoted 5 lines

Maybe call this el1 stub or something like that?

guest_pkvm_stub.c
pkvm/hyp.c

Quoted 6 lines

It is not really a stub and I was following the convention of arm-smmu-v3-(iommufd/test/sva) I think for the pkvm/ one it can be:
- pkvm/arm-smmu-v3-pkvm.c
- pkvm/arm-smmu-v3-nested.c
- pkvm/arm-smmu-v3-el2.c
- pkvm/arm-smmu-v3-hyp.c

pkvm/arm-smmu-v3-hyp.c reads well to me

Quoted 5 lines

All of those are fine and for the other one:
- arm-smmu-v3-{pkvm, nested, el2, hyp}-init.c
- arm-smmu-v3-pkvm-el1.c

Anything along this lines, otherwise it's too vague IMHO.

arm-smmu-v3-pkvm-init.c seems clear?

Linus has complained about the stuttering before, so think about it..

Jason

#40 lore raw

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

In reply to Mostafa Saleh

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

The hypervisor enforces 2 extra requirements:
- Cache coherency.
- Nested translation support.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  4 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 47 +++++++++++++++++++
 2 files changed, 51 insertions(+)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +4 -0
index 17b9454dab85..91177f00fa97 100644
@@ -11,6 +11,9 @@
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
+ * @oas			PA size
+ * @pgsize_bitmap	Supported page sizes
+ * @sid_bits		Max number of SID bits supported
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -20,6 +23,7 @@ struct hyp_arm_smmu_v3_device {
 	u32			options;
 	unsigned long		oas;
 	unsigned long		pgsize_bitmap;
+	unsigned int		sid_bits;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +47 -0

61 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#41 lore raw

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

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

Quoted 9 lines · Mostafa Saleh

+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +3 -0
@@ -11,6 +11,9 @@
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
+ * @oas			PA size
+ * @pgsize_bitmap	Supported page sizes
+ * @sid_bits		Max number of SID bits supported
  */

Some of them belongs to one of the prior patches.

And, if we are going to list them all, could we do in kdoc style?

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
@@ -29,6 +29,47 @@ static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu)
 	smmu->base = NULL;
 }

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 40

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9 -0
 
+/*
+ * Mini-probe and validation for the hypervisor.
+ */
+static int smmu_probe(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 reg;
+
+	if (!(smmu->features & ARM_SMMU_FEAT_COHERENCY))
+		return -EINVAL;

Does this need to check IDR0.COHACC? If not, an inline note that explains why?

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +7 -0
+
+	reg = arm_smmu_idr0_probe(smmu);
+
+	if (!(FIELD_GET(IDR0_TTF, reg) & IDR0_TTF_AARCH64))
+		return -ENXIO;
+
+	if (!(smmu->features & (ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE)))

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 57

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10 -0
+		return -ENXIO;
+
+	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
+	if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL))
+		return -EINVAL;
+
+	smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
+	/* Follows the kernel logic */
+	if (smmu->sid_bits <= STRTAB_SPLIT)
+		smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;

The IDR1 reg guard and sid_bits can be shared with the main driver.

Maybe add an arm_smmu_device_idr1_probe()?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 63

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +6 -0
+
+	arm_smmu_idr3_probe(smmu);
+
+	arm_smmu_idr5_probe(smmu);
+	if (!smmu->oas)
+		smmu->oas = 48;

The truncation can be shared with the main driver too?

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu) · line 93

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
@@ -43,8 +84,14 @@ 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)
+		goto out_ret;
 
 	return 0;
+out_ret:
+	smmu_deinit_device(smmu);

Nit: out_deinit

Nicolin

#42 lore raw

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

Quoted 13 lines · Nicolin Chen

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

Quoted 9 lines · Mostafa Saleh

+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +3 -0
@@ -11,6 +11,9 @@
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
+ * @oas			PA size
+ * @pgsize_bitmap	Supported page sizes
+ * @sid_bits		Max number of SID bits supported
  */

Some of them belongs to one of the prior patches.

Quoted 1 line

And, if we are going to list them all, could we do in kdoc style?

I will fix it.

Quoted 6 lines
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
@@ -29,6 +29,47 @@ static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu)
 	smmu->base = NULL;
 }
 
Quoted 13 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 40

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9 -0
+/*
+ * Mini-probe and validation for the hypervisor.
+ */
+static int smmu_probe(struct hyp_arm_smmu_v3_device *smmu)
+{
+	u32 reg;
+
+	if (!(smmu->features & ARM_SMMU_FEAT_COHERENCY))
+		return -EINVAL;

Does this need to check IDR0.COHACC? If not, an inline note that explains why?

This is similar to the kernel driver logic, where FW will override coherency and in case of mismatch the driver will just WARN and trust the FW.
pKVM can't print a warning as WARN is fatal in EL2. So it just picks what the FW says. I am not sure what systems rely on this, but I can add more strict checks and fail probe in case of mismatch with pKVM.

Quoted 20 lines
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +7 -0
+
+	reg = arm_smmu_idr0_probe(smmu);
+
+	if (!(FIELD_GET(IDR0_TTF, reg) & IDR0_TTF_AARCH64))
+		return -ENXIO;
+
+	if (!(smmu->features & (ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE)))

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 57

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10 -0
+		return -ENXIO;
+
+	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
+	if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL))
+		return -EINVAL;
+
+	smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
+	/* Follows the kernel logic */
+	if (smmu->sid_bits <= STRTAB_SPLIT)
+		smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;

The IDR1 reg guard and sid_bits can be shared with the main driver.

Quoted 2 lines

Maybe add an arm_smmu_device_idr1_probe()?

I thought about that, but this will be only used for sid_bits, the hypervisor struct doesn't have ssid_bits as it doesn't use it, it only shadows the STE and not the CD as the CD is translated by stage-2.

Other fields also not used by the hypervisor (PRIQS, EVTQS...)

As sashiko pointed, I will need to add ARM_SMMU_FEAT_ATTR_TYPES_OVR check in the hypervisor, so maybe arm_smmu_device_idr1_probe() can be added to partially probe the register and return the value so it can be probed the main driver.

Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static void smmu_deinit_device(struct hyp_arm_smmu_v3_device *smmu) · line 63

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +6 -0
+
+	arm_smmu_idr3_probe(smmu);
+
+	arm_smmu_idr5_probe(smmu);
+	if (!smmu->oas)
+		smmu->oas = 48;

The truncation can be shared with the main driver too?

The main driver will print an error, if arm_smmu_idr5_probe() fix it silently we will lose that.

Quoted 9 lines
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
@@ -43,8 +84,14 @@ 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)
+		goto out_ret;
 
Quoted 5 lines

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu) · line 93

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
 	return 0;
+out_ret:
+	smmu_deinit_device(smmu);

Nit: out_deinit

Sure.

Thanks,
Mostafa

Quoted 2 lines

Nicolin

#43 lore raw

PATCH v8 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.h    |  14 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 159 ++++++++++++++++++
 2 files changed, 173 insertions(+)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +14 -0
index 91177f00fa97..58fa14c239e3 100644
@@ -4,6 +4,10 @@
 
 #include <asm/kvm_asm.h>
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#include <nvhe/spinlock.h>
+#endif
+
 /*
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
@@ -14,6 +18,9 @@
  * @oas			PA size
  * @pgsize_bitmap	Supported page sizes
  * @sid_bits		Max number of SID bits supported
+ * @lock		Lock to protect SMMU emulation
+ * @hw_lock		Lock to protect SMMU HW (as CMDQ)
+ *			Order smmu.lock => host_mmu.lock => smmu.hw_lock
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -24,6 +31,13 @@ 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
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +159 -0

187 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#44 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] hyp: fatal WARN_ON(!mask) on host SMMU MMIO access --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hyp: fatal WARN_ON(!mask) on host SMMU MMIO access
--

commit 0f61ec17741805782c471d3c6da0d9dad62368e6
Author: Mostafa Saleh <[email protected]>

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

Add data abort handler for the SMMUs, at the moment most registers are just passthrough.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index f8b7c5406017e..7a51cb70205fe 100644

[ ... ]

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

[ ... ]

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

[ ... ]

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
+	}
+
+	if (WARN_ON(!mask))
+		goto out_ret;

[Severity: Medium]
Is it safe to use WARN_ON() here to validate host-controlled input?

If the host writes an unallowed value to ARM_SMMU_CR1 (breaking out of the switch before setting mask), or accesses an unhandled register offset, mask remains 0. The subsequent WARN_ON(!mask) will trigger a fatal BRK instruction at EL2, crashing the hypervisor and the host.

Since WARN_ON() is fatal at EL2 and does not execute the goto out_ret recovery path, should this validation gracefully handle the abort instead of asserting?

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

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

Quoted 46 lines · On Tue, Sep 22, 2026 at 01:22:32PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hyp: fatal WARN_ON(!mask) on host SMMU MMIO access
--

commit 0f61ec17741805782c471d3c6da0d9dad62368e6
Author: Mostafa Saleh <[email protected]>

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

Add data abort handler for the SMMUs, at the moment most registers are just passthrough.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index f8b7c5406017e..7a51cb70205fe 100644

[ ... ]

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

[ ... ]

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

[ ... ]

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

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
+	}
+
+	if (WARN_ON(!mask))
+		goto out_ret;

[Severity: Medium]
Is it safe to use WARN_ON() here to validate host-controlled input?

Quoted 8 lines

If the host writes an unallowed value to ARM_SMMU_CR1 (breaking out of the switch before setting mask), or accesses an unhandled register offset, mask remains 0. The subsequent WARN_ON(!mask) will trigger a fatal BRK instruction at EL2, crashing the hypervisor and the host.

Since WARN_ON() is fatal at EL2 and does not execute the goto out_ret recovery path, should this validation gracefully handle the abort instead of asserting?

Yes, no well behaved host will attempt this, so a WARN_ON() is better than ignoring it and making it really hard to debug.

Thanks,
Mostafa

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

PATCH v8 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.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  25 ++++
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  10 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 124 ++++++++++++++++++
 3 files changed, 159 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +25 -0
index 9947d3a44304..28f8b1fba8f7 100644
@@ -15,6 +15,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))
+
 extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
 
 static size_t				kvm_arm_smmu_count;
@@ -24,6 +26,15 @@ static size_t				kvm_arm_smmu_cur;
 static void kvm_arm_smmu_array_free(void)
 {
 	int order;
+	int i;
+
+	for (i = 0 ; i < kvm_arm_smmu_cur ; ++i) {
+		struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[i];
+
+		if (smmu->cmdq.base_dma)
+			free_pages((unsigned long)phys_to_virt(smmu->cmdq.base_dma),
+				   SMMU_KVM_CMDQ_ORDER);
+	}
 
 	order = get_order(kvm_arm_smmu_count * sizeof(*kvm_arm_smmu_array));
 	free_pages((unsigned long)kvm_arm_smmu_array, order);
@@ -70,6 +81,7 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
 	struct device *dev = &pdev->dev;
 	struct resource *res;
+	void *cmdq_base;
 
 	/* Only device tree, ACPI not supported. */
 	if (!dev->of_node)
@@ -92,6 +104,19 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
+	/*
+	 * 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.h +10 -0
index 58fa14c239e3..39afdeffcd63 100644
@@ -8,6 +8,8 @@
 #include <nvhe/spinlock.h>
 #endif
 
+#include "../arm-smmu-v3.h"
+
 /*
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
@@ -21,6 +23,10 @@
  * @lock		Lock to protect SMMU emulation
  * @hw_lock		Lock to protect SMMU HW (as 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 and llq used.
+ * @cmdq_max_shift	Max shift for the CMDQ probed from HW.
+ * @cr0			Last value of CR0
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -38,6 +44,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;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +124 -0

172 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#47 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] hyp: wrong WARN_ON() in smmu_emulate_cmdq_enable() causes panic - [Medium] hyp: WARN_ON() on…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] hyp: wrong WARN_ON() in smmu_emulate_cmdq_enable() causes panic
- [Medium] hyp: WARN_ON() on host MMIO access creates dead-branch trap
--

commit 6579ca2101bec241418169d448cfd8bd062f327e
Author: Mostafa Saleh <[email protected]>

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

At boot, allocate a command queue per SMMU which is used as a shadow by the hypervisor. The host command queue is pinned in a shared state to prevent it from being donated to VMs.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 7a51cb70205fe..b26c21b595b47 100644

[ ... ]

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

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9 -0
@@ -132,6 +222,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]
If the host provides an invalid or unmapped command queue base address via the ARM_SMMU_CMDQ_BASE register, smmu_share_pages() will return an error. Since WARN_ON() expands to a fatal break instruction at EL2, would this allow the host to intentionally crash the hypervisor by passing bad input?

Could we handle the smmu_share_pages() failure gracefully instead of using WARN_ON() on host-controlled inputs?

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

[ ... ]

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

Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +8 -0
@@ -158,6 +265,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;
+			smmu->cmdq_host.q_base = val;
+		}
+		mask = read_write;
+		break;

[Severity: Medium]
When the host writes to ARM_SMMU_CMDQ_BASE while the command queue is enabled, the switch statement in smmu_dabt_device() breaks with mask remaining zero.

Later in smmu_dabt_device(), there is an existing check:

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

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
 	if (WARN_ON(!mask))
 		goto out_ret;

Because WARN_ON() at EL2 causes a fatal panic, the goto out_ret recovery path is dead code. Would this allow the host to crash the hypervisor by performing an invalid write to ARM_SMMU_CMDQ_BASE?

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

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

Quoted 39 lines · On Tue, Sep 22, 2026 at 01:32:43PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] hyp: wrong WARN_ON() in smmu_emulate_cmdq_enable() causes panic
- [Medium] hyp: WARN_ON() on host MMIO access creates dead-branch trap
--

commit 6579ca2101bec241418169d448cfd8bd062f327e
Author: Mostafa Saleh <[email protected]>

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

At boot, allocate a command queue per SMMU which is used as a shadow by the hypervisor. The host command queue is pinned in a shared state to prevent it from being donated to VMs.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 7a51cb70205fe..b26c21b595b47 100644

[ ... ]

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

Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +9 -0
@@ -132,6 +222,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]
If the host provides an invalid or unmapped command queue base address via the ARM_SMMU_CMDQ_BASE register, smmu_share_pages() will return an error. Since WARN_ON() expands to a fatal break instruction at EL2, would this allow the host to intentionally crash the hypervisor by passing bad input?

Quoted 2 lines

Could we handle the smmu_share_pages() failure gracefully instead of using WARN_ON() on host-controlled inputs?

No it couldn't, this only happen if the host is malicious or gone too wrong.

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

[ ... ]

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
@@ -158,6 +265,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;
+			smmu->cmdq_host.q_base = val;
Quoted 8 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+		}
+		mask = read_write;
+		break;

[Severity: Medium]
When the host writes to ARM_SMMU_CMDQ_BASE while the command queue is enabled, the switch statement in smmu_dabt_device() breaks with mask remaining zero.

Yes, because that is not allowed by the architecture and no well behaved host will attempt that.

Quoted 10 lines

Later in smmu_dabt_device(), there is an existing check:

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

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
 	if (WARN_ON(!mask))
 		goto out_ret;

Because WARN_ON() at EL2 causes a fatal panic, the goto out_ret recovery path is dead code. Would this allow the host to crash the hypervisor by performing an invalid write to ARM_SMMU_CMDQ_BASE?

Yes, the host can crash the hypervisor in many ways.

Thanks,
Mostafa

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

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

In reply to Mostafa Saleh

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

queue_space() is split out of queue_has_space() as it is going to check the host shadow of the queue.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  14 ++-
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 118 ++++++++++++++++++
 2 files changed, 126 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +8 -6
index 28efa733e796..d48bd9f272e1 100644
@@ -1225,19 +1225,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.c +118 -0

136 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#50 lore raw

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

In reply to Mostafa Saleh

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

Also add emulation for IDR1.CMDQS to return the hypervisor command queue size which can be equal to or smaller to the HW size.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 151 +++++++++++++++++-
 1 file changed, 146 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +146 -5
index 38b8ecc5cc10..8c67e348f4a3 100644
@@ -111,7 +111,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;
@@ -340,6 +339,105 @@ 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);
+	bool use_wfe = smmu->features & ARM_SMMU_FEAT_SEV;
+	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(use_wfe, 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(use_wfe, 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;
@@ -381,18 +479,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:
@@ -468,10 +599,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_GERROR:
 		if (len != sizeof(u32))
 			break;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#51 lore raw

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

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] arm-smmu-v3-kvm: Incomplete CMD_SYNC filtering allows arbitrary MSI DMA writes - [Medium]…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] arm-smmu-v3-kvm: Incomplete CMD_SYNC filtering allows arbitrary MSI DMA writes
- [Medium] arm-smmu-v3-kvm: Data races on concurrent MMIO writes corrupt hypervisor state
- [Medium] arm-smmu-v3-kvm: Misuse of WARN_ON() for untrusted host inputs panics hypervisor
- [Low] arm-smmu-v3-kvm: Unnecessary wfe() sleep when waiting for CMDQ emptiness causes CPU stalls --

commit 59e5e956f9f689e4dbd688035f21063722fbd640
Author: Mostafa Saleh <[email protected]>
Subject: iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

This commit adds emulation for the host command queue in the ARM SMMUv3 hypervisor. It prevents direct access to the command queue from the host, intercepting writes to CMDQ_PROD and CMDQ_CONS to sanitize commands and manage the queue lifecycle.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 38b8ecc5cc10c..8c67e348f4a30 100644

[ ... ]

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
@@ -340,6 +339,105 @@ 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) {

[ ... ]

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

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

[Severity: High]
Does this regression leave the reserved CS value unfiltered? In smmu_filter_command(), if the host submits a CMD_SYNC command with the CS field set to 0b11 (Reserved), the filter only sanitizes MSIADDR if CS is exactly 0b01 (IRQ). Thus, a command with CS=0b11 could bypass the filter unmodified. If the hardware implementation aliases 0b11 to 0b01, this could allow the host to perform arbitrary DMA writes to hypervisor memory.

Quoted 33 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +33 -0
+	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);
+	bool use_wfe = smmu->features & ARM_SMMU_FEAT_SEV;
+	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(use_wfe, smmu_cmdq_has_space(&smmu->cmdq, pending));
+	if (ret) {
+		hyp_spin_unlock(&smmu->hw_lock);
+		return ret;
+	}
+
+	while (pending--) {
+		int i;
+

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

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

[Severity: Medium]
Is this regression safe when using WARN_ON() for untrusted host inputs? At EL2, WARN_ON() expands to a fatal BRK instruction, causing a hypervisor panic. If the host writes a malformed command, this could allow a host-triggered denial of service. Should the code error out or ignore the command instead? I also noticed similar WARN_ON() assertions in smmu_dabt_device() when handling an invalid register access size.

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

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

[Severity: Low]
Could this regression cause the CPU to stall? The smmu_wait() loop uses wfe() to wait for the hardware queue to empty, but the hardware only generates an SEV on a CMD_SYNC command. If the host submits commands without a CMD_SYNC, the CPU could stall in wfe() until a random interrupt wakes it up or the 1ms timeout expires, leading to unnecessary latency.

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

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
@@ -381,18 +479,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,

[ ... ]

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

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -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: Medium]
Could this regression allow multiple threads concurrently mutating smmu->cmdq_host.llq.prod outside smmu->hw_lock to corrupt the queue state? Modifying this value without a lock could allow concurrent host writes to decouple the pending calculation from the consumer index.

Similarly, I see in smmu_dabt_device() that ARM_SMMU_CR0 is modified locklessly when calling smmu_emulate_cmdq_enable(). Could concurrent writes from multiple CPUs cause hypervisor panics due to double page sharing/unsharing or queue desynchronization?

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

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +10 -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]
Could this regression allow the host to panic the hypervisor by attempting an invalid state transition (writing to CONS while the queue is enabled)? Like the other WARN_ON() usages for host inputs, this should likely be an error return instead of an assertion.

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

[ ... ]

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

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

Quoted 31 lines · On Tue, Sep 22, 2026 at 01:32:36PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] arm-smmu-v3-kvm: Incomplete CMD_SYNC filtering allows arbitrary MSI DMA writes
- [Medium] arm-smmu-v3-kvm: Data races on concurrent MMIO writes corrupt hypervisor state
- [Medium] arm-smmu-v3-kvm: Misuse of WARN_ON() for untrusted host inputs panics hypervisor
- [Low] arm-smmu-v3-kvm: Unnecessary wfe() sleep when waiting for CMDQ emptiness causes CPU stalls --

commit 59e5e956f9f689e4dbd688035f21063722fbd640
Author: Mostafa Saleh <[email protected]>
Subject: iommu/arm-smmu-v3-kvm: Emulate CMDQ for host

This commit adds emulation for the host command queue in the ARM SMMUv3 hypervisor. It prevents direct access to the command queue from the host, intercepting writes to CMDQ_PROD and CMDQ_CONS to sanitize commands and manage the queue lifecycle.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 38b8ecc5cc10c..8c67e348f4a30 100644

[ ... ]

Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
@@ -340,6 +339,105 @@ 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) {

[ ... ]

Quoted 16 lines

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

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

[Severity: High]
Does this regression leave the reserved CS value unfiltered? In smmu_filter_command(), if the host submits a CMD_SYNC command with the CS field set to 0b11 (Reserved), the filter only sanitizes MSIADDR if CS is exactly 0b01 (IRQ). Thus, a command with CS=0b11 could bypass the filter unmodified. If the hardware implementation aliases 0b11 to 0b01, this could allow the host to perform arbitrary DMA writes to hypervisor memory.

No, according to the spec:
0b11: Reserved: Causes a CERROR_ILL.

Quoted 45 lines
Quoted 34 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +33 -0
+	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);
+	bool use_wfe = smmu->features & ARM_SMMU_FEAT_SEV;
+	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(use_wfe, smmu_cmdq_has_space(&smmu->cmdq, pending));
+	if (ret) {
+		hyp_spin_unlock(&smmu->hw_lock);
+		return ret;
+	}
+
+	while (pending--) {
+		int i;
+

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

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

[Severity: Medium]
Is this regression safe when using WARN_ON() for untrusted host inputs? At EL2, WARN_ON() expands to a fatal BRK instruction, causing a hypervisor panic. If the host writes a malformed command, this could allow a host-triggered denial of service. Should the code error out or ignore the command instead? I also noticed similar WARN_ON() assertions in smmu_dabt_device() when handling an invalid register access size.

Yes, no well behaved host will issue unsupported commands, so WARN_ON should be OK here.

Quoted 14 lines

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

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +6 -0
+		smmu_add_cmd_raw(smmu, cmd);
+	}
+
+	writel(smmu->cmdq.llq.prod, smmu->cmdq.prod_reg);
+
+	ret = smmu_wait(use_wfe, smmu_cmdq_empty(&smmu->cmdq));

[Severity: Low]
Could this regression cause the CPU to stall? The smmu_wait() loop uses wfe() to wait for the hardware queue to empty, but the hardware only generates an SEV on a CMD_SYNC command. If the host submits commands without a CMD_SYNC, the CPU could stall in wfe() until a random interrupt wakes it up or the 1ms timeout expires, leading to unnecessary latency.

True, I think we should pass WFE as an arg to this macro which is used for certain conditions that are known to send an event. I will fix that.

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

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
@@ -381,18 +479,51 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,

[ ... ]

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

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -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: Medium]
Could this regression allow multiple threads concurrently mutating smmu->cmdq_host.llq.prod outside smmu->hw_lock to corrupt the queue state? Modifying this value without a lock could allow concurrent host writes to decouple the pending calculation from the consumer index.

Quoted 5 lines

Similarly, I see in smmu_dabt_device() that ARM_SMMU_CR0 is modified locklessly when calling smmu_emulate_cmdq_enable(). Could concurrent writes from multiple CPUs cause hypervisor panics due to double page sharing/unsharing or queue desynchronization?

Wrong, smmu->cmdq_host.llq.prod is protected by the smmu->lock and the not hw_lock which only protects the HW command queue.
And there is no part of the code where this is read or written without the smmu->lock.

Also wrong for ARM_SMMU_CR0, the caller takes the lock.

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

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

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

[Severity: Medium]
Could this regression allow the host to panic the hypervisor by attempting an invalid state transition (writing to CONS while the queue is enabled)? Like the other WARN_ON() usages for host inputs, this should likely be an error return instead of an assertion.

Yes as mentioned before this is not allowed by the architecture, so no well-behaved host will do that and WARN_ON should be OK.

Thanks,
Mostafa

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

[ ... ]

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

PATCH v8 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.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c   |  21 ++-
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  10 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 122 ++++++++++++++++++
 3 files changed, 152 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c +20 -1
index 28f8b1fba8f7..6b5a93dfbc2b 100644
@@ -16,6 +16,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)))
 
 extern struct pkvm_iommu_ops kvm_nvhe_sym(smmu_ops);
 
@@ -34,6 +41,9 @@ static void kvm_arm_smmu_array_free(void)
 		if (smmu->cmdq.base_dma)
 			free_pages((unsigned long)phys_to_virt(smmu->cmdq.base_dma),
 				   SMMU_KVM_CMDQ_ORDER);
+		if (smmu->strtab_dma)
+			free_pages((unsigned long)phys_to_virt(smmu->strtab_dma),
+				   SMMU_KVM_STRTAB_ORDER);
 	}
 
 	order = get_order(kvm_arm_smmu_count * sizeof(*kvm_arm_smmu_array));
@@ -80,8 +90,8 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 {
 	struct hyp_arm_smmu_v3_device *smmu = &kvm_arm_smmu_array[kvm_arm_smmu_cur];
 	struct device *dev = &pdev->dev;
+	void *cmdq_base, *strtab;
 	struct resource *res;
-	void *cmdq_base;
 
 	/* Only device tree, ACPI not supported. */
 	if (!dev->of_node)
@@ -117,6 +127,15 @@ static int smmuv3_nesting_probe(struct platform_device *pdev)
 	smmu->cmdq.base_dma = virt_to_phys(cmdq_base);
 	smmu->cmdq.llq.max_n_shift = SMMU_KVM_CMDQ_ORDER + PAGE_SHIFT - CMDQ_ENT_SZ_SHIFT;
 
+	strtab = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, SMMU_KVM_STRTAB_ORDER);
+	if (!strtab) {
+		free_pages((unsigned long)cmdq_base, SMMU_KVM_CMDQ_ORDER);
+		return -ENOMEM;
+	}
+
+	smmu->strtab_dma = virt_to_phys(strtab);
+	smmu->strtab_size = PAGE_SIZE << SMMU_KVM_STRTAB_ORDER;
+
 	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.h +10 -0
index 39afdeffcd63..d4700d3a3c10 100644
@@ -14,6 +14,8 @@
  * Parameters from the trusted host:
  * @mmio_addr		base address of the SMMU registers
  * @mmio_size		size of the registers resource
+ * @strtab_dma		Phys address of stream table
+ * @strtab_size		Stream table size
  *
  * Other members are filled and used at runtime by the SMMU driver.
  * @base		Virtual address of SMMU registers
@@ -27,6 +29,9 @@
  * @cmdq_host		Host view of the CMDQ, only q_base 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 as seen by HW
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -48,6 +53,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;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +122 -0

177 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#54 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] arm-smmu-v3-kvm: unhandled break in switch statement causes EL2 panic on STRTAB access…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] arm-smmu-v3-kvm: unhandled break in switch statement causes EL2 panic on STRTAB access
- [Medium] arm-smmu-v3-kvm: improper WARN_ON() usage for host inputs allows trivial EL2 panics --

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

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

This patch allocates the shadow stream table per SMMU with a maximum size of 1MB for 2-level tables. It sets up passthrough for host writes to the stream table base and configuration registers, laying groundwork for future CFGI command trapping.

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

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
@@ -438,6 +499,46 @@ static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)
 	return ret;
 }
 
+static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enabled)

[ ... ]

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

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

[Severity: Medium]
Could this WARN_ON panic EL2 if the host enables the SMMU without enabling the CMDQ? Since WARN_ON at EL2 behaves as a fatal panic, using it for host-controlled inputs converts an invalid host state into a hypervisor DoS. Would returning an error be safer here?

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+
+	WARN_ON(smmu_update_ste_shadow(smmu, true));
+}

[Severity: Medium]
Similarly, does this panic EL2 if the host provides an invalid stream table configuration? The pKVM guidelines suggest that EL2 should not assert on contracted host inputs.

[ ... ]

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

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
@@ -525,7 +626,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;

[Severity: Medium]
Does this break statement erroneously exit the entire switch block for ARM_SMMU_STRTAB_BASE? If the host writes to this register while the SMMU is enabled, the break bypasses the mask assignment, leaving mask = 0. Later in the function, if (WARN_ON(!mask)) evaluates to true, which triggers a fatal hypervisor panic.

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

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

[Severity: Medium]
Could this break statement similarly cause an EL2 panic on STRTAB_BASE_CFG access? Like the case above, this appears to leave mask = 0 and panic the hypervisor.

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

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
+			smmu->host_ste_cfg = val;
+		}
+		mask = read_write;
+		break;
 	case ARM_SMMU_GBPA:

[ ... ]

[Severity: Medium]
Are there other instances where WARN_ON is used to validate host inputs? For example, in smmu_emulate_cmdq_insert():

smmu_emulate_cmdq_insert()

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

And at the end of smmu_dabt_device():

smmu_dabt_device()

    if (WARN_ON(!mask))
        goto out_ret;

Since these are directly reachable by the host kernel, they allow a trivial host self-DoS by inserting an unsupported command or performing an unhandled access length. Could these assertions be replaced with standard error handling or abort injection?

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

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

Quoted 25 lines · On Tue, Sep 22, 2026 at 01:33:09PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] arm-smmu-v3-kvm: unhandled break in switch statement causes EL2 panic on STRTAB access
- [Medium] arm-smmu-v3-kvm: improper WARN_ON() usage for host inputs allows trivial EL2 panics --

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

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

This patch allocates the shadow stream table per SMMU with a maximum size of 1MB for 2-level tables. It sets up passthrough for host writes to the stream table base and configuration registers, laying groundwork for future CFGI command trapping.

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

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
@@ -438,6 +499,46 @@ static int smmu_emulate_cmdq_insert(struct hyp_arm_smmu_v3_device *smmu)
 	return ret;
 }
 
+static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enabled)

[ ... ]

Quoted 12 lines

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

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

[Severity: Medium]
Could this WARN_ON panic EL2 if the host enables the SMMU without enabling the CMDQ? Since WARN_ON at EL2 behaves as a fatal panic, using it for host-controlled inputs converts an invalid host state into a hypervisor DoS. Would returning an error be safer here?

Yes, as I replied before.

Quoted 10 lines

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

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +3 -0
+
+	WARN_ON(smmu_update_ste_shadow(smmu, true));
+}

[Severity: Medium]
Similarly, does this panic EL2 if the host provides an invalid stream table configuration? The pKVM guidelines suggest that EL2 should not assert on contracted host inputs.

[ ... ]

Quoted 15 lines

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

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
@@ -525,7 +626,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;

[Severity: Medium]
Does this break statement erroneously exit the entire switch block for ARM_SMMU_STRTAB_BASE? If the host writes to this register while the SMMU is enabled, the break bypasses the mask assignment, leaving mask = 0. Later in the function, if (WARN_ON(!mask)) evaluates to true, which triggers a fatal hypervisor panic.

Not an error, that is the intent, a panic will happen if the host violate the spec.

Quoted 15 lines

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

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

[Severity: Medium]
Could this break statement similarly cause an EL2 panic on STRTAB_BASE_CFG access? Like the case above, this appears to leave mask = 0 and panic the hypervisor.

Same as above.

Quoted 19 lines

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

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +4 -0
+			smmu->host_ste_cfg = val;
+		}
+		mask = read_write;
+		break;
 	case ARM_SMMU_GBPA:

[ ... ]

[Severity: Medium]
Are there other instances where WARN_ON is used to validate host inputs? For example, in smmu_emulate_cmdq_insert():

smmu_emulate_cmdq_insert()

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

And at the end of smmu_dabt_device():

Quoted 9 lines

smmu_dabt_device()

    if (WARN_ON(!mask))
        goto out_ret;

Since these are directly reachable by the host kernel, they allow a trivial host self-DoS by inserting an unsupported command or performing an unhandled access length. Could these assertions be replaced with standard error handling or abort injection?

Same as above.

Thanks,
Mostafa

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

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

In reply to Mostafa Saleh

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

Copy the STE as is to the shadow owned by the hypervisor, which is the one seen by HW.
In the next patch, stage-2 page table will be attached.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 124 +++++++++++++++++-
 1 file changed, 118 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +118 -6
index 54049b8b1363..cb9aa7e4a394 100644
@@ -24,6 +24,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; \
@@ -287,6 +290,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;
@@ -406,8 +494,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:
 	{
 		/*
@@ -624,25 +728,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.55.0.1082.g2b9226bbc0-goog
#57 lore raw

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

In reply to Mostafa Saleh

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

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |  8 ++
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 81 +++++++++++++++++--
 2 files changed, 84 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +8 -0
index d4700d3a3c10..ffc57182e57e 100644
@@ -32,6 +32,10 @@
  * @host_ste_cfg	Host stream table config
  * @host_ste_base	Host stream table base
  * @strtab_cfg		Stream table as seen by HW
+ * @evtq_base		Host evtq base reg
+ * @priq_base		Host priq base reg
+ * @evtq_shared		Whether the EVTQ was shared
+ * @priq_shared		Whether the PRIQ was shared
  */
 struct hyp_arm_smmu_v3_device {
 	phys_addr_t		mmio_addr;
@@ -58,6 +62,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;
 };
 
 extern size_t kvm_nvhe_sym(kvm_hyp_arm_smmu_v3_count);
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +76 -5

121 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#58 lore raw

PATCH v8 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.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 32 +++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +29 -3
index 00528380774f..6e023e968ed3 100644
@@ -215,6 +215,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)
 {
@@ -453,6 +469,10 @@ static int smmu_init_device(struct hyp_arm_smmu_v3_device *smmu)
 	if (ret)
 		goto out_ret;
 
+	ret = smmu_abort_gbpa(smmu);
+	if (ret)
+		goto out_ret;
+
 	return 0;
 
 out_ret:
@@ -778,10 +798,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.55.0.1082.g2b9226bbc0-goog
#59 lore raw

PATCH v8 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 | 86 +++++++++++++++++++
 drivers/iommu/io-pgtable-arm.c                | 14 +--
 drivers/iommu/io-pgtable-arm.h                |  9 ++
 4 files changed, 104 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 aa76c0f3f147..64bfd9992d9e 100644
@@ -36,7 +36,8 @@ hyp-obj-y += $(lib-objs)
 HYP_SMMU_V3_DRV_PATH = ../../../../../drivers/iommu/arm/arm-smmu-v3
 
 hyp-obj-$(CONFIG_ARM_SMMU_V3_PKVM) += $(HYP_SMMU_V3_DRV_PATH)/pkvm/arm-smmu-v3.o \
-	$(HYP_SMMU_V3_DRV_PATH)/arm-smmu-v3-common-lib.o ../../../../../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 +86 -0

86 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..ca765aea8745 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(type)	kmalloc_obj(type)
+#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.55.0.1082.g2b9226bbc0-goog
#60 lore raw

PATCH v8 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.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 153 +++++++++++++++++-
 1 file changed, 152 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +152 -1
index 6e023e968ed3..6f0ea3a4e48d 100644
@@ -14,6 +14,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;
 
@@ -70,6 +73,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);
@@ -215,6 +221,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;
@@ -480,6 +504,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 = kvm_alloc_io_pgtable_ops(ARM_64_LPAE_S2, &cfg, NULL);
+	if (!ops)
+		return -ENOMEM;
+	idmap_pgtable = io_pgtable_ops_to_pgtable(ops);
+	return 0;
+}
+
 /* Called while is the host is still trusted. */
 static int smmu_init(void)
 {
@@ -509,7 +565,10 @@ static int smmu_init(void)
 
 	BUILD_BUG_ON(sizeof(hyp_spinlock_t) != sizeof(u32));
 
-	return 0;
+	ret = smmu_init_pgt();
+	if (ret)
+		goto out_reclaim_smmu;
+	return ret;
 
 out_reclaim_smmu:
 	while (smmu != kvm_hyp_arm_smmu_v3_smmus)
@@ -998,8 +1057,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.55.0.1082.g2b9226bbc0-goog
#61 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] arm-smmu-v3-kvm: panic on EL2 allocation failure during STE reshadow --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] arm-smmu-v3-kvm: panic on EL2 allocation failure during STE reshadow --

commit 866d2f52e473ce62e2402494a1198d807826d481
Author: Mostafa Saleh <[email protected]>
Subject: iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

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.

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 6e023e968ed3a..6f0ea3a4e48df 100644

[ ... ]

[Severity: High]
Can a routine allocation failure lead to a hypervisor panic here?

Looking at smmu_reshadow_ste() in drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c, the dynamic allocation can legitimately fail with -ENOMEM when the pool is exhausted:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_reshadow_ste() {
	...
			l2table = pkvm_iommu_alloc_pages(get_order(sizeof(*l2table)));
			if (!l2table)
				return -ENOMEM;
	...
}

This -ENOMEM error is mapped to a true return value in smmu_filter_command():

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_filter_command() {
	...
		ret = smmu_reshadow_ste(smmu, sid, leaf);
		hyp_spin_lock(&smmu->hw_lock);
		if (ret)
			return true;
	...
}

Which is then wrapped in a WARN_ON() in smmu_emulate_cmdq_insert():

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_emulate_cmdq_insert() {
	...
		skip = smmu_filter_command(smmu, cmd);
		if (WARN_ON(skip))
			continue;
	...
}

Does this WARN_ON() turn the -ENOMEM condition into a fatal hypervisor panic?

At EL2, WARN_ON() expands to a fatal BRK instruction, crashing the hypervisor. Host userspace processes can trigger this path (e.g., via VFIO device assignment) by configuring multiple devices to exhaust the L2 table pool, leading to a host-userspace-reachable panic.

Could this be updated to handle hyp_alloc_pages() failures without triggering a fatal assertion?

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

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

Quoted 37 lines · On Tue, Sep 22, 2026 at 01:38:15PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] arm-smmu-v3-kvm: panic on EL2 allocation failure during STE reshadow --

commit 866d2f52e473ce62e2402494a1198d807826d481
Author: Mostafa Saleh <[email protected]>
Subject: iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table

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.

[ ... ]

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 6e023e968ed3a..6f0ea3a4e48df 100644

[ ... ]

[Severity: High]
Can a routine allocation failure lead to a hypervisor panic here?

Looking at smmu_reshadow_ste() in drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c, the dynamic allocation can legitimately fail with -ENOMEM when the pool is exhausted:

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_reshadow_ste() {
	...
			l2table = pkvm_iommu_alloc_pages(get_order(sizeof(*l2table)));
			if (!l2table)
				return -ENOMEM;
	...
}
Quoted 2 lines

This -ENOMEM error is mapped to a true return value in smmu_filter_command():

-ENOMEM shouldn't happen here as the pool should cover the worst case and there is no way to recover from it so it panics.

Quoted 19 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_filter_command() {
	...
		ret = smmu_reshadow_ste(smmu, sid, leaf);
		hyp_spin_lock(&smmu->hw_lock);
		if (ret)
			return true;
	...
}

Which is then wrapped in a WARN_ON() in smmu_emulate_cmdq_insert():

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c:smmu_emulate_cmdq_insert() {
	...
		skip = smmu_filter_command(smmu, cmd);
		if (WARN_ON(skip))
			continue;
	...
}
Quoted 7 lines

Does this WARN_ON() turn the -ENOMEM condition into a fatal hypervisor panic?

At EL2, WARN_ON() expands to a fatal BRK instruction, crashing the hypervisor. Host userspace processes can trigger this path (e.g., via VFIO device assignment) by configuring multiple devices to exhaust the L2 table pool, leading to a host-userspace-reachable panic.

User space can not invent new SIDs, the kernel need to allocate for those regardless.

Thanks,
Mostafa

Quoted 6 lines

Could this be updated to handle hyp_alloc_pages() failures without triggering a fatal assertion?

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

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

In reply to Mostafa Saleh

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

Range invalidation algorithm is based on Robin suggestion on the list for the upstream kernel driver, where a 2 overlapping commands can invalidate any range.

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.

---
Jason is currently reworking the TLB invalidation on the mailing list it might be possible to reuse some of the code once landed.

---
 .../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h    |   2 +
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 177 +++++++++++++++++-
 2 files changed, 176 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h +2 -0
index ffc57182e57e..534d329661cf 100644
@@ -29,6 +29,7 @@
  * @cmdq_host		Host view of the CMDQ, only q_base and llq used.
  * @cmdq_max_shift	Max shift for the CMDQ probed from HW.
  * @cr0			Last value of CR0
+ * @cmdq_active		Is SMMU HW cmdq usable, protected by hw_lock
  * @host_ste_cfg	Host stream table config
  * @host_ste_base	Host stream table base
  * @strtab_cfg		Stream table as seen by HW
@@ -57,6 +58,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;
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +174 -3

222 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#64 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] iommu/arm-smmu-v3-kvm: dead-branch traps and incorrect WARN_ON usage at EL2 --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] iommu/arm-smmu-v3-kvm: dead-branch traps and incorrect WARN_ON usage at EL2 --

commit 12e407f77295df6bfe7066853a3a177e9465aaf7
Author: Mostafa Saleh <[email protected]>

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

This commit implements the io-pgtable flush operations to invalidate the shadow stage-2 in every SMMU. It tracks whether the SMMU command queue is enabled and issues invalidation commands to the hardware when active.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 6f0ea3a4e48df..5417a5c2bf58a 100644

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
@@ -221,17 +220,139 @@ 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)

[ ... ]

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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.c static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu, · line 340

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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: Medium]
Does this WARN_ON() panic the hypervisor if the hardware command queue stalls?

At EL2, WARN_ON() expands to a fatal BRK instruction. If the SMMU command queue is full or stalls for >1ms, smmu_wait() returns -ETIMEDOUT, which will bubble up here and cause an unrecoverable hypervisor panic.

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

[ ... ]

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

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
@@ -725,6 +873,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));
 }

[Severity: Medium]
This isn't a bug introduced by this patch, but looking at the existing code just above this in smmu_emulate_enable():

	if (WARN_ON(!is_cmdq_enabled(smmu)))
		return;

Does this create a dead-branch trap? Because WARN_ON() is fatal at EL2, the intended graceful return on an invalid host state is unreachable, as the warning itself panics the hypervisor.

[ ... ]

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

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +6 -0
@@ -745,6 +895,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: Medium]
Does this allow the host to trivially panic the hypervisor by issuing an invalid sequence?

Since WARN_ON() expands to a panic at EL2, asserting directly on host-controlled state (disabling the CMDQ while the SMMU is enabled) turns a host misconfiguration into a hypervisor panic, bringing down the host and all protected guests.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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.c +1 -0
@@ -1020,6 +1177,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.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 1190

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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]
Could this strict equality check predictably time out and panic the hypervisor if the host writes reserved bits?

If the host writes RES0 bits to CR0, the hardware drops them, making the strict readl_relaxed(...) == cr0 check fail. The poll will then time out, and the WARN_ON() will fatally panic the hypervisor.

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

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

Quoted 51 lines · On Tue, Sep 22, 2026 at 01:45:11PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] iommu/arm-smmu-v3-kvm: dead-branch traps and incorrect WARN_ON usage at EL2 --

commit 12e407f77295df6bfe7066853a3a177e9465aaf7
Author: Mostafa Saleh <[email protected]>

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

This commit implements the io-pgtable flush operations to invalidate the shadow stage-2 in every SMMU. It tracks whether the SMMU command queue is enabled and issues invalidation commands to the hardware when active.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -0
index 6f0ea3a4e48df..5417a5c2bf58a 100644

[ ... ]

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -0
@@ -221,17 +220,139 @@ 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)

[ ... ]

Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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.c static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu, · line 340

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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: Medium]
Does this WARN_ON() panic the hypervisor if the hardware command queue stalls?

Quoted 3 lines

At EL2, WARN_ON() expands to a fatal BRK instruction. If the SMMU command queue is full or stalls for >1ms, smmu_wait() returns -ETIMEDOUT, which will bubble up here and cause an unrecoverable hypervisor panic.

There is no way to recover a command queue timeout, ignoring the error might lead to TLB not being invalidated and leaking the memory content so WARN_ON() is suitable for that.

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

[ ... ]

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

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
@@ -725,6 +873,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));
 }

[Severity: Medium]
This isn't a bug introduced by this patch, but looking at the existing code just above this in smmu_emulate_enable():

Quoted 7 lines
	if (WARN_ON(!is_cmdq_enabled(smmu)))
		return;

Does this create a dead-branch trap? Because WARN_ON() is fatal at EL2, the intended graceful return on an invalid host state is unreachable, as the warning itself panics the hypervisor.

Same as discussed before.

Quoted 17 lines

[ ... ]

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

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +6 -0
@@ -745,6 +895,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: Medium]
Does this allow the host to trivially panic the hypervisor by issuing an invalid sequence?

Quoted 4 lines

Since WARN_ON() expands to a panic at EL2, asserting directly on host-controlled state (disabling the CMDQ while the SMMU is enabled) turns a host misconfiguration into a hypervisor panic, bringing down the host and all protected guests.

Yes, the host can panic the system in many ways.

Quoted 28 lines
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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.c +1 -0
@@ -1020,6 +1177,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.c static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu, · line 1190

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.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]
Could this strict equality check predictably time out and panic the hypervisor if the host writes reserved bits?

Quoted 3 lines

If the host writes RES0 bits to CR0, the hardware drops them, making the strict readl_relaxed(...) == cr0 check fail. The poll will then time out, and the WARN_ON() will fatally panic the hypervisor.

See above.

Thanks,
Mostafa

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

PATCH v8 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.

---
 .../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c  | 109 +++++++++++++++++-
 1 file changed, 103 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +103 -6
index 5417a5c2bf58..39c97404e039 100644
@@ -461,6 +461,57 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_attach_stage_2(struct arm_smmu_ste *ste)
+{
+	unsigned long vttbr, 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;
+
+	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)
 {
@@ -490,12 +541,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) ||
@@ -511,7 +590,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);
@@ -520,8 +599,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;
 
@@ -541,8 +619,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(&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.55.0.1082.g2b9226bbc0-goog
#67 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iommu/arm-smmu-v3: missing ATTR_TYPES_OVR check before setting STE.SHCFG --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3: missing ATTR_TYPES_OVR check before setting STE.SHCFG --

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

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

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.

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
index 5417a5c2bf58a..39c97404e0397 100644
@@ -461,6 +461,57 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_attach_stage_2(struct arm_smmu_ste *ste)
+{

[ ... ]

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

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
+	sl = pgt_cfg->arm_lpae_s2_cfg.vtcr.sl;
+	ts = pgt_cfg->arm_lpae_s2_cfg.vtcr.tsz;
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_SHCFG);
+	ste->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING));

[Severity: High]
Does this code write to a RES0 field on hardware lacking the ATTR_TYPES_OVR feature?

On hardware where the ATTR_TYPES_OVR feature is not supported, the STRTAB_STE_1_SHCFG field is RES0. Writing a non-zero value to it triggers a hardware event that aborts the transaction and breaks DMA functionally for the device.

In the host kernel driver, this is guarded by a check for smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR. Is it possible to add a similar feature check here in smmu_attach_stage_2 before setting the STRTAB_STE_1_SHCFG_INCOMING flag?

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_EATS | STRTAB_STE_1_S2FWB | STRTAB_STE_1_STRW);

[ ... ]

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -2
@@ -520,8 +599,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;
 

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -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]
This is a pre-existing issue, but does this out of memory condition trigger a fatal panic at EL2?

When pkvm_iommu_alloc_pages fails here in smmu_reshadow_ste, the ENOMEM error propagates up to smmu_filter_command.

The caller, smmu_emulate_cmdq_insert, will then hit WARN_ON(skip). At EL2, WARN_ON expands to a fatal break instruction, crashing the entire host system.

Since unprivileged host userspace can influence device mappings and exhaust the hypervisor L2 table memory pool, can this error path be handled without causing a hypervisor panic?

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -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));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#68 lore raw

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

Quoted 40 lines · On Tue, Sep 22, 2026 at 01:49:58PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iommu/arm-smmu-v3: missing ATTR_TYPES_OVR check before setting STE.SHCFG --

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

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

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.

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
index 5417a5c2bf58a..39c97404e0397 100644
@@ -461,6 +461,57 @@ static int smmu_init_cmdq(struct hyp_arm_smmu_v3_device *smmu)
 	return 0;
 }
 
+static int smmu_attach_stage_2(struct arm_smmu_ste *ste)
+{

[ ... ]

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

Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +5 -0
+	sl = pgt_cfg->arm_lpae_s2_cfg.vtcr.sl;
+	ts = pgt_cfg->arm_lpae_s2_cfg.vtcr.tsz;
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_SHCFG);
+	ste->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING));

[Severity: High]
Does this code write to a RES0 field on hardware lacking the ATTR_TYPES_OVR feature?

On hardware where the ATTR_TYPES_OVR feature is not supported, the STRTAB_STE_1_SHCFG field is RES0. Writing a non-zero value to it triggers a hardware event that aborts the transaction and breaks DMA functionally for the device.

Quoted 5 lines

In the host kernel driver, this is guarded by a check for smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR. Is it possible to add a similar feature check here in smmu_attach_stage_2 before setting the STRTAB_STE_1_SHCFG_INCOMING flag?

Technically true, although this was unchecked for the kernel for years, so I doubt any HW cares, but I will fix it.

Quoted 28 lines
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +2 -0
+
+	ste->data[1] &= ~cpu_to_le64(STRTAB_STE_1_EATS | STRTAB_STE_1_S2FWB | STRTAB_STE_1_STRW);

[ ... ]

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +1 -2
@@ -520,8 +599,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;
 

drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -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]
This is a pre-existing issue, but does this out of memory condition trigger a fatal panic at EL2?

When pkvm_iommu_alloc_pages fails here in smmu_reshadow_ste, the ENOMEM error propagates up to smmu_filter_command.

Quoted 7 lines

The caller, smmu_emulate_cmdq_insert, will then hit WARN_ON(skip). At EL2, WARN_ON expands to a fatal break instruction, crashing the entire host system.

Since unprivileged host userspace can influence device mappings and exhaust the hypervisor L2 table memory pool, can this error path be handled without causing a hypervisor panic?

Same as discussed before.

Thanks,
Mostafa

Quoted 7 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c +0 -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));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#69 lore raw

PATCH v8 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.55.0.1082.g2b9226bbc0-goog
#70 lore raw
In reply to Mostafa Saleh
Quoted 4 lines · Mostafa Saleh

Notes about Sashiko
===================
I ran Sashiko locally and it was helpful in discovering problems in the series. However, it still shows large number of critical and high

Quoted 8 lines

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)

Just my two cents here:

From my experience, making commit messages or inline notes slightly clearer against the findings that Sashiko reported could simply help it understand things better and mute most of the false positives.

Otherwise, the number of the findings is a bit scaring; reviewers would have to go through the long list of findings to check whether each of them fits into one of the false-positive categories :-/

Quoted 2 lines

- 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.

Yea, that happens too. Usually I would add a line at the end of the commit message "a subsequent change will do X", which could clarify.

Nicolin

#71 lore raw
Quoted 14 lines · Nicolin Chen
Quoted 4 lines · Mostafa Saleh

Notes about Sashiko
===================
I ran Sashiko locally and it was helpful in discovering problems in the series. However, it still shows large number of critical and high

Quoted 8 lines

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)

Quoted 6 lines

Just my two cents here:

From my experience, making commit messages or inline notes slightly clearer against the findings that Sashiko reported could simply help it understand things better and mute most of the false positives.

I try to do that also, it helps to a certain level, then the code becomes too verbose and the comments will be trivial :/

Quoted 4 lines

Otherwise, the number of the findings is a bit scaring; reviewers would have to go through the long list of findings to check whether each of them fits into one of the false-positive categories :-/

My strategy is to reply to Sashiko on the list to save reviewers time. But my main concern was the large number of false positives might discourage reviewers thinking there will be a newer version with fixes.

Quoted 5 lines
Quoted 2 lines

- 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.

Yea, that happens too. Usually I would add a line at the end of the commit message "a subsequent change will do X", which could clarify.

There were a lot of those when I ran locally. However the one on sashiko.dev does better.

Thanks,
Mostafa

Quoted 2 lines

Nicolin