Skip to content

Commit a48ce36

Browse files
LuBaolujoergroedel
authored andcommitted
iommu: Prevent RESV_DIRECT devices from blocking domains
The IOMMU_RESV_DIRECT flag indicates that a memory region must be mapped 1:1 at all times. This means that the region must always be accessible to the device, even if the device is attached to a blocking domain. This is equal to saying that IOMMU_RESV_DIRECT flag prevents devices from being attached to blocking domains. This also implies that devices that implement RESV_DIRECT regions will be prevented from being assigned to user space since taking the DMA ownership immediately switches to a blocking domain. The rule of preventing devices with the IOMMU_RESV_DIRECT regions from being assigned to user space has existed in the Intel IOMMU driver for a long time. Now, this rule is being lifted up to a general core rule, as other architectures like AMD and ARM also have RMRR-like reserved regions. This has been discussed in the community mailing list and refer to below link for more details. Other places using unmanaged domains for kernel DMA must follow the iommu_get_resv_regions() and setup IOMMU_RESV_DIRECT - we do not restrict them in the core code. Cc: Robin Murphy <[email protected]> Cc: Alex Williamson <[email protected]> Cc: Kevin Tian <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/linux-iommu/BN9PR11MB5276E84229B5BD952D78E9598C639@BN9PR11MB5276.namprd11.prod.outlook.com Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Acked-by: Joerg Roedel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent f5ccf55 commit a48ce36

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

drivers/iommu/iommu.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -960,28 +960,30 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
960960
unsigned long pg_size;
961961
int ret = 0;
962962

963-
if (!iommu_is_dma_domain(domain))
964-
return 0;
965-
966-
BUG_ON(!domain->pgsize_bitmap);
967-
968-
pg_size = 1UL << __ffs(domain->pgsize_bitmap);
963+
pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
969964
INIT_LIST_HEAD(&mappings);
970965

966+
if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
967+
return -EINVAL;
968+
971969
iommu_get_resv_regions(dev, &mappings);
972970

973971
/* We need to consider overlapping regions for different devices */
974972
list_for_each_entry(entry, &mappings, list) {
975973
dma_addr_t start, end, addr;
976974
size_t map_size = 0;
977975

978-
start = ALIGN(entry->start, pg_size);
979-
end = ALIGN(entry->start + entry->length, pg_size);
976+
if (entry->type == IOMMU_RESV_DIRECT)
977+
dev->iommu->require_direct = 1;
980978

981-
if (entry->type != IOMMU_RESV_DIRECT &&
982-
entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
979+
if ((entry->type != IOMMU_RESV_DIRECT &&
980+
entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
981+
!iommu_is_dma_domain(domain))
983982
continue;
984983

984+
start = ALIGN(entry->start, pg_size);
985+
end = ALIGN(entry->start + entry->length, pg_size);
986+
985987
for (addr = start; addr <= end; addr += pg_size) {
986988
phys_addr_t phys_addr;
987989

@@ -2122,6 +2124,21 @@ static int __iommu_device_set_domain(struct iommu_group *group,
21222124
{
21232125
int ret;
21242126

2127+
/*
2128+
* If the device requires IOMMU_RESV_DIRECT then we cannot allow
2129+
* the blocking domain to be attached as it does not contain the
2130+
* required 1:1 mapping. This test effectively excludes the device
2131+
* being used with iommu_group_claim_dma_owner() which will block
2132+
* vfio and iommufd as well.
2133+
*/
2134+
if (dev->iommu->require_direct &&
2135+
(new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2136+
new_domain == group->blocking_domain)) {
2137+
dev_warn(dev,
2138+
"Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");
2139+
return -EINVAL;
2140+
}
2141+
21252142
if (dev->iommu->attach_deferred) {
21262143
if (new_domain == group->default_domain)
21272144
return 0;

include/linux/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ struct iommu_fault_param {
411411
* @priv: IOMMU Driver private data
412412
* @max_pasids: number of PASIDs this device can consume
413413
* @attach_deferred: the dma domain attachment is deferred
414+
* @require_direct: device requires IOMMU_RESV_DIRECT regions
414415
*
415416
* TODO: migrate other per device data pointers under iommu_dev_data, e.g.
416417
* struct iommu_group *iommu_group;
@@ -424,6 +425,7 @@ struct dev_iommu {
424425
void *priv;
425426
u32 max_pasids;
426427
u32 attach_deferred:1;
428+
u32 require_direct:1;
427429
};
428430

429431
int iommu_device_register(struct iommu_device *iommu,

0 commit comments

Comments
 (0)