iommu/arm-smmu-v3: Support guest-level Realm VSMMU (Part-1)
Subsystems (5)
v4 cover iommu/arm-smmu-v3: Support guest-level Realm VSMMU (Part-1)
This series prepares Realm guest IOMMU and DMA subsystems for devices that are present but not yet inside the Realm trust boundary.
Inside a confidential guest, we expect that all the iommus are going to be T=1 aware; there seems to be no interest in supporting IOMMUs that handle T=0 only. This means they only work on devices that have been put into the run state and issue T=1 transactions.
Today an iommu driver assumes it translates for every device behind it: it probes the device, takes a translating default domain, and puts the DMA API on its page tables. That assumption is wrong for T=0 devices, whose DMA the hypervisor translates instead, so the mappings made by the guest are never consulted and the device silently stops working.
To solve this problem, let the core treat each confidential vIOMMU as being bypassed by every device associated with it. Part 2 will add the per-device T=1 state, along with the transition.
While T=0, the device remains outside the Realm trust boundary. Its DMA is translated by the host or hypervisor:
[device: T=0] ===> [hypervisor] ===> [memory]
|
+-- (guest association) --> [confidential IOMMU: blocking DMA]
Legend: ===> DMA path; --> guest topology and controlAfter reaching T=1, the device is inside the Realm trust boundary. Its DMA may use the guest VSMMU after RMM binds the device to its virtual stream:
[device] ===> [VSMMU: S1] ===> [RMM/PSMMU: S2 + DPT] ===> [memory]
As part 1 of Realm VSMMU support, implement enough core infrastructure to manage a confidential vIOMMU:
- Detect and mark it as cc_private in the core iommu structures
- Support devices in T=0 mode by keeping the DMA API in direct mode. The iommu is up and running, but parks such a device in its blocking domain and translates for nothing. The hypervisor owns the whole T=0 path, ATS included
- Keep it parked: refuse a translating domain by every route in, whether through the default domain, sysfs, group attach or replace, or PASID
Then implement SMMUv3 support:
- Let a CPU-integrated IOMMU declare that its DMA can reach private memory, so coherent allocations and streaming mappings avoid the SWIOTLB shared pool
- Identify a T=1 VSMMU using trusted RSI because firmware does not distinguish it from a normal T=0 SMMU
- Validate the firmware MMIO range against RSI before activating the VSMMU and allocating its queues and tables from private memory
The series is on github:
https://github.com/nicolinc/iommufd/commits/smmuv3_realm_guest_p1-v4
Changelog
v4:
* Add Reviewed-by from Jason and Acked-by from Marek
* Check cc_private directly in iommu_dma_prepare_msi()
* Move the is_realm_world() checks into the probe helpers
* Explain why a realm guest cannot use the Tegra241 CMDQV yet
* Reword the DEV_FLAG_DMA_CC_PRIVATE commit message per Jason
* Rename iommu_using_t0_stream() to iommu_cc_viommu_bypassed()
* Rename "confidential" to "cc_private" and make it a bitfield
* Keep require_direct, and skip its checks for a bypassed vIOMMU instead
* Drop "Introduce TDISP T=0 state", as the hypervisor owns ATS. This also drops the PCI reset hooks and the tdisp_t1 flag
v3:
https://lore.kernel.org/all/[email protected]/
* Add Acked-by from Marek
* Disable and enable ATS for T=0 devices across a PCI reset
* Rename dma_require_decrypted() to dma_require_unencrypted()
* Explain the minimum STU used to enable ATS while TDISP is T=0
v2:
https://lore.kernel.org/all/[email protected]/
* Rebase on v7.4-rc1
* Drop Assisted-by tags
* Drop Aneesh's firmware patch that was merged
* Skip Tegra CMDQV implementation in the realm world
* Use arm_smccc_1_2_smc() to zero inputs as RMM spec demands
* Add missing iommu_using_t0_stream() check in iommu_dma_prepare_msi()
v1:
https://lore.kernel.org/all/[email protected]/
Future work
===========
Part 2 will add the T=1 transition after validation through:
- TSM, whose guest-side interface remains under discussion
- RMM, using the Arm-specific RSI VDEV command
For a multi-device IOMMU group, the first device accepted into T=1 claims the group. Part 2 will reject T=1 for every other member.
The current RSI binding validation returns one VSID, so part 2 must reject a firmware specification containing multiple SIDs upon the T=1 transition.
Nicolin Chen (6):
firmware: arm_rmm: Add VSMMU commands and fields
dma-mapping: Let a device declare that it reaches private memory
dma-mapping: Keep DMA memory private for capable devices
iommu: Let a driver mark an IOMMU as confidential
iommu: Use the blocking domain for a bypassed confidential vIOMMU iommu/arm-smmu-v3: Probe a guest-level Realm VSMMU via RSI commands
drivers/iommu/iommu-priv.h | 11 +++ include/linux/arm-rsi-cmds.h | 29 ++++++++ include/linux/arm-smccc-rsi.h | 20 ++++++ include/linux/device.h | 4 ++ include/linux/dma-direct.h | 7 +- include/linux/dma-mapping.h | 4 ++ include/linux/iommu.h | 10 +++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 78 +++++++++++++++++++- drivers/iommu/iommu.c | 80 +++++++++++++++++++-- kernel/dma/direct.c | 21 +++--- kernel/dma/mapping.c | 23 +++++- kernel/dma/swiotlb.c | 2 +- 12 files changed, 269 insertions(+), 20 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
PATCH v4 2/6 dma-mapping: Let a device declare that it reaches private memory
Inside a confidential VM the HW controls how the DMA translation works. It boils down to a single bit, either the vIOMMU is ignored and only a bit of shared/decrypted address space is reachable or the vIOMMU is used and all address space is reachable. The state of this HW control varies per device and is an input to the DMA API just like all the other DMA parameters, and defines the rules for how the DMA API builds dma_addr_t.
Only one bit is needed, so add a DEV_FLAG_DMA_CC_PRIVATE. The bit only has meaning inside a confidential VM and is to only be consumed by the DMA API.
It reflects the current HW configuration:
- True, if the HW allows the device to DMA to the full address space
- False, to keep the current confidential VM behavior of the DMA API unchanged
Like other DMA API parameters it must not change once the DMA API is used. Basically it should not change while a driver is bound.
However, some SOC embedded devices like the SMMU, are hardwired to be able to access all memory. In this case the driver itself will have some way to confirm if the device has full DMA access and can call dma_set_cc_private() very early, before using the DMA API, to set it up. This flow is similar to dma_set_mask_and_coherent().
A following change teaches the DMA API to use this bit inside confidential VMs. A future series will let TDISP-capable PCIe devices control this bit as well using the TSM subsystem.
--- include/linux/device.h | 4 ++++ include/linux/dma-mapping.h | 4 ++++ kernel/dma/mapping.c | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+)
include/linux/device.h +4 -0
@@ -604,6 +604,8 @@ struct device_physical_location {* ancestor device. * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online. * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline(). +* @DEV_FLAG_DMA_CC_PRIVATE: The device is able to access private (encrypted) +* memory, no shared memory bouncing is required from the DMA API. * @DEV_FLAG_COUNT: Number of defined struct_device_flags. */ enum struct_device_flags {
@@ -617,6 +619,7 @@ enum struct_device_flags {DEV_FLAG_OF_NODE_REUSED =7 ,DEV_FLAG_OFFLINE_DISABLED =8 ,DEV_FLAG_OFFLINE =9 , +DEV_FLAG_DMA_CC_PRIVATE =10 , DEV_FLAG_COUNT };
@@ -834,6 +837,7 @@ __create_dev_flag_accessors(dma_coherent, DEV_FLAG_DMA_COHERENT);__create_dev_flag_accessors (of_node_reused,DEV_FLAG_OF_NODE_REUSED );__create_dev_flag_accessors (offline_disabled,DEV_FLAG_OFFLINE_DISABLED );__create_dev_flag_accessors (offline,DEV_FLAG_OFFLINE ); +__create_dev_flag_accessors (dma_cc_private,DEV_FLAG_DMA_CC_PRIVATE );#undef __create_dev_flag_accessors
include/linux/dma-mapping.h +4 -0
@@ -218,6 +218,7 @@ void *dma_vmap_noncontiguous(struct device *dev, size_t size,void dma_vunmap_noncontiguous (struct device *dev,void *vaddr);int dma_mmap_noncontiguous (struct device *dev,struct vm_area_struct *vma,size_t size,struct sg_table *sgt); +void dma_set_cc_private (struct device *dev,bool private);#else /* CONFIG_HAS_DMA */ static inline dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, size_t size,
@@ -357,6 +358,9 @@ static inline int dma_mmap_noncontiguous(struct device *dev, {return -EINVAL ; } +static inline void dma_set_cc_private (struct device *dev,bool private) +{ +}#endif /* CONFIG_HAS_DMA */ #ifdef CONFIG_IOMMU_DMA
kernel/dma/mapping.c +21 -0
@@ -501,6 +501,27 @@ static void dma_setup_need_sync(struct device *dev)static inline void dma_setup_need_sync (struct device *dev) { }#endif /* !CONFIG_DMA_NEED_SYNC */ +/** +* dma_set_cc_private - Set whether @dev can DMA to private memory +* @dev: device whose confidential-computing DMA access is being updated +* @private: whether the device can DMA to private memory +* +* Called when a device is known to be able to DMA to private memory. +* For architected CPU integrated devices their kernel drivers will +* self-accept using a driver specific validation. Other devices will +* have a userspace managed process. +* +* DMA mapping must not be active when this flag is changed. For CPU +* integrated devices the driver should self accept early during probe +* after validation. Other devices have this flag set automatically +* before probing. +*/ +void dma_set_cc_private (struct device *dev,bool private) +{ +dev_assign_dma_cc_private (dev, private); +} +EXPORT_SYMBOL_GPL (dma_set_cc_private); +/* * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems * that the intention is to allow exporting memory allocated via the
Signature · 1 line
2.43.0
PATCH v4 4/6 iommu: Let a driver mark an IOMMU as confidential
In reply to Nicolin ChenAn IOMMU inside a confidential VM needs private memory for its queues and tables. Later changes also need to identify it when handling TDISP devices.
Add a cc_private marker and a helper that IOMMU drivers use to set it. The helper also marks the IOMMU device as able to reach private memory through DMA. Drivers must call it before iommu_device_register().
Make cc_private a bitfield, so that later flags can be packed next to it. But keep ready as a normal bool. Both READ_ONCE() and WRITE_ONCE() access the ready field, and they need its address. A bitfield has no address.
--- include/linux/iommu.h | 10 ++++++++++ drivers/iommu/iommu.c | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+)
include/linux/iommu.h +10 -0
@@ -824,6 +824,8 @@ struct iommu_domain_ops {* @singleton_group: Used internally for drivers that have only one group * @max_pasids: number of supported PASIDs * @ready: set once iommu_device_register() has completed successfully +* @cc_private: this instance runs inside a confidential VM. Set by the driver +* before any dma allocation. */ struct iommu_device {struct list_head list;
@@ -833,6 +835,7 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + bool cc_private : 1; };/**
@@ -892,6 +895,8 @@ struct dev_iommu {int iommu_device_register (struct iommu_device *iommu,const struct iommu_ops *ops,struct device *hwdev); +void iommu_device_set_cc_private (struct iommu_device *iommu, +struct device *hwdev);void iommu_device_unregister (struct iommu_device *iommu); int iommu_device_sysfs_add(struct iommu_device *iommu, struct device *parent,
@@ -1423,6 +1428,11 @@ static inline int iommu_device_register(struct iommu_device *iommu,return -ENODEV ; } +static inline void iommu_device_set_cc_private (struct iommu_device *iommu, +struct device *hwdev) +{ +} +static inline struct iommu_device *dev_to_iommu_device (struct device *dev) {return NULL ;
drivers/iommu/iommu.c +20 -0
@@ -17,6 +17,7 @@#include <linux/slab.h> #include <linux/errno.h> #include <linux/host1x_context_bus.h> +#include <linux/dma-mapping.h> #include <linux/iommu.h> #include <linux/iommufd.h> #include <linux/idr.h>
@@ -313,6 +314,25 @@ int iommu_device_register(struct iommu_device *iommu, }EXPORT_SYMBOL_GPL (iommu_device_register); +/** +* iommu_device_set_cc_private - Mark an IOMMU instance as confidential +* @iommu: the IOMMU instance +* @hwdev: the struct device of @iommu, whose own DMA reaches private memory +* +* Declare that @iommu runs inside a confidential VM +* +* Call this before @iommu makes any DMA allocation of its own, and not merely +* before iommu_device_register(). Queues and tables allocated any earlier land +* in shared memory that the hypervisor can read. +*/ +void iommu_device_set_cc_private (struct iommu_device *iommu, +struct device *hwdev) +{ + iommu->cc_private = true; +dma_set_cc_private (hwdev, true); +} +EXPORT_SYMBOL_GPL (iommu_device_set_cc_private); +void iommu_device_unregister (struct iommu_device *iommu) {for (int i =0 ; i <ARRAY_SIZE (iommu_buses); i++)
Signature · 1 line
2.43.0
PATCH v4 6/6 iommu/arm-smmu-v3: Probe a guest-level Realm VSMMU via RSI commands
In reply to Nicolin ChenIn CCA, the ACPI/etc doesn't tell the OS whether the SMMU is a confidential T=1 instance or a normal T=0 one. Instead, it has to be learned by issuing a trusted RSI. Before the kernel can operate the trusted T=1 IOMMU, it also has to validate all the information that it gets from ACPI against the true information that it gets from the RSI call, to ensure the ACPI is correct and prevent substitution attacks.
Use RSI_VSMMU_GET_INFO during probe and require the returned register range to match firmware. Then use RSI_ARCH_DEV_ACTIVATE before mapping registers.
Currently the driver doesn't support a T=0 SMMU inside a realm. For example it doesn't make the page table allocations into shared memory. If we are in a realm reject any SMMU that is not T=1.
Once the SMMU is activated, its DMA will follow the DEV_FLAG_DMA_CC_PRIVATE rules. It must set that flag to ensure its coherent allocations for its own structures are allocated from private memory, not the SWIOTLB shared pool.
Also skip the Tegra241 CMDQV in the realm world. The CMDQV hardware can be used in realms. But neither the RMM spec nor the RMM firmware supports the CMDQV yet. From a guest perspective, there is no way to verify the NVDA200C ACPI device with the RMM yet. Thus, its MMIO space cannot be used for now.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 78 ++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +77 -1
@@ -11,9 +11,11 @@#include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/arm-rsi-cmds.h> #include <linux/bitops.h> #include <linux/crash_dump.h> #include <linux/delay.h> +#include <linux/dma-mapping.h> #include <linux/err.h> #include <linux/interrupt.h> #include <linux/io-pgtable.h>
@@ -5545,6 +5547,15 @@ static void tegra_cmdqv_dt_probe(struct device_node *smmu_node,struct platform_device *pdev;struct device_node *np; +/* +* A realm can have a CMDQV. But neither the RMM spec nor the firmware +* supports the CMDQV yet. So, a realm guest has no way to verify it via +* the RMM yet. Thus, its MMIO space cannot be used. Realms only get the +* standard SMMU for now. +*/ +if (is_realm_world ()) +return ; + np =of_parse_phandle (smmu_node,"nvidia,cmdqv" ,0 );if (!np)return ;
@@ -5572,8 +5583,19 @@ static void tegra_cmdqv_dt_probe(struct device_node *smmu_node,static void acpi_smmu_dsdt_probe_tegra241_cmdqv (struct acpi_iort_node *node,struct arm_smmu_device *smmu) { -const char *uid =kasprintf (GFP_KERNEL ,"%u" , node->identifier);struct acpi_device *adev; +const char *uid; + +/* +* A realm can have a CMDQV. But neither the RMM spec nor the firmware +* supports the CMDQV yet. So, a realm guest has no way to verify it via +* the RMM yet. Thus, its MMIO space cannot be used. Realms only get the +* standard SMMU for now. +*/ +if (is_realm_world ()) +return ; + + uid =kasprintf (GFP_KERNEL ,"%u" , node->identifier);/* Look for an NVDA200C node whose _UID matches the SMMU node ID */ adev =acpi_dev_get_first_match_dev ("NVDA200C" , uid,-1 );
@@ -5779,6 +5801,56 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)return ERR_PTR (ret); } +static void arm_smmu_clear_realm_private (void *dev) +{ +dma_set_cc_private (dev, false); +} + +static int arm_smmu_probe_realm_vsmmu (struct arm_smmu_device *smmu, +const struct resource *res) +{ +struct device *dev = smmu->dev; +unsigned long rsi_ret; +phys_addr_t top; + +if (!is_realm_world ()) +return 0 ; + +/* +* Currently the driver does not support a T=0 SMMU inside a realm. For +* instance it does not make the page table allocations into shared +* memory. If we are in a realm reject any SMMU that is not T=1. +*/ + rsi_ret =rsi_vsmmu_get_info (res->start, &top); +if (rsi_ret !=RSI_SUCCESS ) { +dev_err (dev,"RSI_VSMMU_GET_INFO failed for %pr: %lu\n" , res, + rsi_ret); +return -ENODEV ; + } + +if (top != res->end +1 ) { +dev_err (dev,"VSMMU range %pr ends at %pa\n" , res, &top); +return -EINVAL ; + } + + rsi_ret =rsi_arch_dev_activate (res->start,RSI_ARCH_DEV_SMMUV3 ); +if (rsi_ret !=RSI_SUCCESS ) { +dev_err (dev,"RSI_ARCH_DEV_ACTIVATE failed for %pr: %lu\n" , res, + rsi_ret); +return -EIO ; + } + +/* +* Once activated, the SMMU DMA follows DEV_FLAG_DMA_CC_PRIVATE so its +* queues and tables are allocated from private memory, not the SWIOTLB +* shared pool. It only translates for a device we've requested the RMM +* to put into T=1. +*/ +iommu_device_set_cc_private (&smmu->iommu, dev); + +return devm_add_action_or_reset (dev, arm_smmu_clear_realm_private, dev); +} +static int arm_smmu_device_probe (struct platform_device *pdev) {int irq, ret;
@@ -5814,6 +5886,10 @@ static int arm_smmu_device_probe(struct platform_device *pdev) } ioaddr = res->start; + ret =arm_smmu_probe_realm_vsmmu (smmu, res); +if (ret) +return ret; +/* * Don't map the IMPLEMENTATION DEFINED regions, since they may contain * the PMCG registers which are reserved by the PMU driver.
Signature · 1 line
2.43.0
PATCH v4 1/6 firmware: arm_rmm: Add VSMMU commands and fields
In reply to Nicolin ChenDEN0137 2.0-bet3 adds the RSI_VSMMU_GET_INFO command and replaces the old RSI_VSMMU_ACTIVATE command with RSI_ARCH_DEV_ACTIVATE.
Add the command IDs, return codes, SMMUv3 device type, and helpers for both commands.
--- include/linux/arm-rsi-cmds.h | 29 +++++++++++++++++++++++++++++ include/linux/arm-smccc-rsi.h | 20 ++++++++++++++++++++ 2 files changed, 49 insertions(+)
include/linux/arm-rsi-cmds.h +29 -0
@@ -236,4 +236,33 @@ static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule, return res.a0; } +static inline unsigned long rsi_vsmmu_get_info (phys_addr_t addr, +phys_addr_t *top) +{ +struct arm_smccc_1_2_regs regs = { + .a0 =SMC_RSI_VSMMU_GET_INFO , + .a1 = addr, + }; + +arm_smccc_1_2_smc (®s, ®s); +if (regs.a0 ==RSI_SUCCESS && top) + *top = regs.a1; + +return regs.a0; +} + +static inline unsigned long rsi_arch_dev_activate (phys_addr_t base, +unsigned long dev_type) +{ +struct arm_smccc_1_2_regs regs = { + .a0 =SMC_RSI_ARCH_DEV_ACTIVATE , + .a1 = base, + .a2 = dev_type, + }; + +arm_smccc_1_2_smc (®s, ®s); + +return regs.a0; +} +#endif /* __LINUX_ARM_RSI_CMDS_H_ */
include/linux/arm-smccc-rsi.h +20 -0
@@ -37,6 +37,7 @@#define RSI_ERROR_STATE UL(2)#define RSI_INCOMPLETE UL(3)#define RSI_ERROR_UNKNOWN UL(4) +#define RSI_ERROR_DEVICE UL(5)#define SMC_RSI_FID (n) ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ ARM_SMCCC_SMC_64, \
@@ -190,4 +191,23 @@ struct realm_config {*/ #define SMC_RSI_HOST_CALL SMC_RSI_FID(0x199) +/* +* Get information about a VSMMU. +* +* arg1 == Base IPA of the VSMMU +* ret0 == Status / error +* ret1 == Top IPA of the VSMMU +*/ +#define SMC_RSI_VSMMU_GET_INFO SMC_RSI_FID(0x19A) + +#define RSI_ARCH_DEV_SMMUV3 UL(0) + +/* +* Activate an architectural device. +* +* arg1 == Base IPA of the architectural device +* arg2 == Architectural device type +* ret0 == Status / error +*/ +#define SMC_RSI_ARCH_DEV_ACTIVATE SMC_RSI_FID(0x19B)#endif /* __LINUX_ARM_SMCCC_RSI_H_ */
Signature · 1 line
2.43.0
PATCH v4 3/6 dma-mapping: Keep DMA memory private for capable devices
In reply to Nicolin ChenArchitecture code can require devices to use unencrypted memory. This makes the DMA API use shared memory for coherent allocations and bounce streaming mappings through SWIOTLB.
A device marked with DEV_FLAG_DMA_CC_PRIVATE is able to reach the private memory directly. Add dma_require_unencrypted() that combines the new flag with force_dma_unencrypted(), and use it in the generic DMA code. Also use "unencrypted" in the local variables that hold its result, to avoid mixing the "unencrypted" and "decrypted" terms.
--- include/linux/dma-direct.h | 7 ++++++- kernel/dma/direct.c | 21 +++++++++++---------- kernel/dma/mapping.c | 2 +- kernel/dma/swiotlb.c | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-)
include/linux/dma-direct.h +6 -1
@@ -134,6 +134,11 @@ static inline bool force_dma_unencrypted(struct device *dev) }#endif /* CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED */ +static inline bool dma_require_unencrypted (struct device *dev) +{ +return !dev_dma_cc_private (dev) &&force_dma_unencrypted (dev); +} +static inline bool dma_capable (struct device *dev,dma_addr_t addr,size_t size,bool is_ram,unsigned long attrs) {
@@ -146,7 +151,7 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,* requires unencrypted DMA addresses. Treat it as not DMA-capable * so the caller can fall back to a suitable SWIOTLB pool. */ -if (!(attrs &DMA_ATTR_CC_SHARED ) &&force_dma_unencrypted (dev)) +if (!(attrs &DMA_ATTR_CC_SHARED ) &&dma_require_unencrypted (dev))return false; if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
kernel/dma/direct.c +11 -10
@@ -41,9 +41,9 @@ static inline struct page *dma_direct_to_page(struct device *dev,u64 dma_direct_get_required_mask (struct device *dev) { -bool require_decrypted =force_dma_unencrypted (dev); +bool require_unencrypted =dma_require_unencrypted (dev);phys_addr_t phys = ((phys_addr_t )max_pfn <<PAGE_SHIFT ) -1 ; -u64 max_dma =phys_to_dma_direct (dev, phys, require_decrypted); +u64 max_dma =phys_to_dma_direct (dev, phys, require_unencrypted);return (1ULL << (fls64 (max_dma) -1 )) *2 -1 ; }
@@ -72,8 +72,9 @@ static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 *phys_limit)bool dma_coherent_ok (struct device *dev,phys_addr_t phys,size_t size) { -bool require_decrypted =force_dma_unencrypted (dev); -dma_addr_t dma_addr =phys_to_dma_direct (dev, phys, require_decrypted); +bool require_unencrypted =dma_require_unencrypted (dev); +dma_addr_t dma_addr =phys_to_dma_direct (dev, phys, + require_unencrypted);if (dma_addr ==DMA_MAPPING_ERROR )return false;
@@ -226,7 +227,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,struct page *page;void *cpu_addr; -if (force_dma_unencrypted (dev)) +if (dma_require_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) {
@@ -385,7 +386,7 @@ void dma_direct_free(struct device *dev, size_t size,* If the allocation used decrypted/shared backing pages, restore * the encryption state on free. */ -if (force_dma_unencrypted (dev)) +if (dma_require_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
@@ -452,7 +453,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,struct page *page;void *cpu_addr; -if (force_dma_unencrypted (dev)) +if (dma_require_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&dma_direct_use_pool (dev, gfp))
@@ -496,7 +497,7 @@ void dma_direct_free_pages(struct device *dev, size_t size,* if the device had requested for an unencrypted buffer, * convert it to encrypted on free */ -bool mark_mem_encrypted =force_dma_unencrypted (dev); +bool mark_mem_encrypted =dma_require_unencrypted (dev);/* If page is not from an atomic pool, dma_free_from_pool_page() fails */ if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
@@ -665,7 +666,7 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,const pgoff_t pgoff_end =vma_end_pgoff (vma);int ret = -ENXIO ; -if (force_dma_unencrypted (dev)) +if (dma_require_unencrypted (dev)) attrs |=DMA_ATTR_CC_SHARED ; vma->vm_page_prot =dma_pgprot (dev, vma->vm_page_prot, attrs);
@@ -814,7 +815,7 @@ size_t dma_direct_max_mapping_size(struct device *dev)/* If SWIOTLB is active, use its maximum mapping size */ if (is_swiotlb_active (dev) && (dma_addressing_limited (dev) ||is_swiotlb_force_bounce (dev) || -force_dma_unencrypted (dev))) +dma_require_unencrypted (dev)))return swiotlb_max_mapping_size (dev);return SIZE_MAX ;
kernel/dma/mapping.c +1 -1
@@ -673,7 +673,7 @@ void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,return NULL ; } -if (force_dma_unencrypted (dev)) +if (dma_require_unencrypted (dev)) attrs |= __DMA_ATTR_ALLOC_CC_SHARED;if (dma_alloc_from_dev_coherent (dev, size, dma_handle, &cpu_addr)) {
kernel/dma/swiotlb.c +1 -1
@@ -1550,7 +1550,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,if (cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) {/* swiotlb pool is incorrect for this device */ -if (unlikely (mem->cc_shared !=force_dma_unencrypted (dev))) +if (unlikely (mem->cc_shared !=dma_require_unencrypted (dev)))return (phys_addr_t )DMA_MAPPING_ERROR ; }else if (cc_platform_has (CC_ATTR_HOST_MEM_ENCRYPT )) {
Signature · 1 line
2.43.0
PATCH v4 5/6 iommu: Use the blocking domain for a bypassed confidential vIOMMU
In reply to Nicolin ChenInside a confidential VM, a device stays associated with its confidential vIOMMU, even before it is accepted into T=1. Until then, its DMA bypasses the vIOMMU. The hypervisor translates that DMA through the T=0 stream.
Without core support, such a device gets a translating default domain. The DMA API then hands out IOVAs that only the vIOMMU knows about, whereas the DMA of the device goes into the hypervisor. Also, dma-iommu keeps the DMA buffers in private memory, which the device cannot reach.
Add iommu_cc_viommu_bypassed(). For now, every device behind a cc_private IOMMU bypasses it. Use the blocking domain as the default domain for such a device. This keeps the DMA API on dma-direct, which uses shared buffers and matches the DMA path set up by the hypervisor.
Linux's model requires a device to either use T=0 or T=1. Aside from MSI, it cannot use T=0 for normal DMA once moved into T=1 mode, since the device will have IOVA's that are not valid in T=0.
A strict IOMMU_RESV_DIRECT region sets require_direct for the device. One such region is the MSI doorbell RMR that the VMM creates for an accelerated vSMMU. The core then refuses the blocking domain for that device, and the IOMMU fails to register. At release, the core also moves such a device to an identity domain. Skip both for a device behind a bypassed vIOMMU. This vIOMMU is not in the DMA path of the device, so it needs no 1:1 mappings. Keep require_direct as reported by firmware, for a future paging domain.
Reject sysfs domain changes, driver-managed group attachment, and any PASID attachments that would install a translating domain. The state is uniform across each group, since all devices on a confidential IOMMU bypass it now.
The MSI of such a device always goes through T=0 and needs no MSI cookie. Check cc_private directly there, since MSI stays on T=0 even after a device moves into T=1.
--- drivers/iommu/iommu-priv.h | 11 +++++++ drivers/iommu/iommu.c | 60 ++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 6 deletions(-)
drivers/iommu/iommu-priv.h +11 -0
@@ -8,6 +8,17 @@#include <linux/iommu-debug-pagealloc.h> #include <linux/msi.h> +/* +* A bypassed confidential vIOMMU does not translate the DMA of the device. The +* hypervisor sets up identity translation for the device instead. PCIe ATS on +* its own controls the ATS behavior. For now, every device behind a cc_private +* IOMMU bypasses it. +*/ +static inline bool iommu_cc_viommu_bypassed (struct device *dev) +{ +return dev->iommu->iommu_dev->cc_private; +} +static inline const struct iommu_ops *dev_iommu_ops (struct device *dev) {/*
drivers/iommu/iommu.c +54 -6
121 lines not on this page · raw message
Signature · 1 line
2.43.0