[PATCH v8 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver
Mostafa Saleh
smostafa at google.com
Wed Sep 23 04:52:36 PDT 2026
On Tue, Sep 22, 2026 at 05:22:29PM -0700, Nicolin Chen wrote:
> On Tue, Sep 22, 2026 at 01:12:44PM +0000, Mostafa Saleh wrote:
> > 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".
Will do.
>
> > +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-kvm.c
> > @@ -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.
>
> 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)
> > + *
> > + * 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"
> > +
> > +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.
> > +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.
>
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.
> > + */
> > + if (np) {
> > + of_node_put(np);
> > + return host_s2_pgtable_pages() + 500;
> > + }
> > +
> > + return 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?
Yes, Sashiko replied to this mail with it and I replied:
https://lore.kernel.org/all/20260922132814.D398D1F000FF@smtp.kernel.org/
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.
>
> > + 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()
>
> > + 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.
> > +static int __init kvm_arm_smmu_v3_register(void)
> > +{
> [...]
> > +out_err:
> > + kvm_arm_smmu_count = 0;
> > + kvm_arm_smmu_array = NULL;
> > + return ret;
> > +};
>
> No ';' after '}'.
Oops, will fix it.
>
> > +static int kvm_arm_smmu_v3_post_init(void)
>
> __init?
>
That should be possible, I will add it.
> > +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> > @@ -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
> > +/*
> > + * 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
>
> ?
I think "is bound", I am not sure if bounds/unbounds is correct.
Thanks,
Mostafa
>
> Nicolin
More information about the linux-arm-kernel
mailing list