RMRR related fixes
Subsystems (3)
cover RMRR related fixes
Currently the Intel reserved region is attached to the RMRR unit and when building the list of RMRR seen by a device we link this unique reserved region without taking care of potential multiple usage of this reserved region by several devices.
Also while reading the vtd spec it is unclear to me whether the RMRR device scope referenced by an RMRR ACPI struct could be a PCI-PCI bridge, in which case I think we also need to check the device belongs to the PCI sub-hierarchy of the device referenced in the scope. This would be true for device_has_rmrr() and intel_iommu_get_resv_regions().
Eric Auger (4):
iommu: Pass a GFP flag parameter to iommu_alloc_resv_region() iommu/vt-d: Duplicate iommu_resv_region objects per device list iommu/vt-d: Handle RMRR with PCI bridge device scopes iommu/vt-d: Handle PCI bridge RMRR device scopes in
intel_iommu_get_resv_regions
drivers/acpi/arm64/iort.c | 3 +- drivers/iommu/amd_iommu.c | 7 ++-- drivers/iommu/arm-smmu-v3.c | 2 +- drivers/iommu/arm-smmu.c | 2 +- drivers/iommu/intel-iommu.c | 68 +++++++++++++++++++++++-------------- drivers/iommu/iommu.c | 7 ++-- include/linux/iommu.h | 2 +- 7 files changed, 55 insertions(+), 36 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/4 iommu: Pass a GFP flag parameter to iommu_alloc_resv_region()
We plan to call iommu_alloc_resv_region in a non preemptible section. Pass a GFP flag to the function and update all the call sites.
--- drivers/acpi/arm64/iort.c | 3 ++- drivers/iommu/amd_iommu.c | 7 ++++--- drivers/iommu/arm-smmu-v3.c | 2 +- drivers/iommu/arm-smmu.c | 2 +- drivers/iommu/intel-iommu.c | 4 ++-- drivers/iommu/iommu.c | 7 ++++--- include/linux/iommu.h | 2 +- 7 files changed, 15 insertions(+), 12 deletions(-)
drivers/acpi/arm64/iort.c +2 -1
@@ -868,7 +868,8 @@ int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)struct iommu_resv_region *region; region =iommu_alloc_resv_region (base +SZ_64K ,SZ_64K , - prot,IOMMU_RESV_MSI ); + prot,IOMMU_RESV_MSI , +GFP_KERNEL );if (region) {list_add_tail (®ion->list, head); resv++;
drivers/iommu/amd_iommu.c +4 -3
@@ -3186,7 +3186,8 @@ static void amd_iommu_get_resv_regions(struct device *dev, type =IOMMU_RESV_RESERVED ; region =iommu_alloc_resv_region (entry->address_start, - length, prot, type); + length, prot, type, +GFP_KERNEL );if (!region) {dev_err (dev,"Out of memory allocating dm-regions\n" );return ;
@@ -3196,14 +3197,14 @@ static void amd_iommu_get_resv_regions(struct device *dev, region =iommu_alloc_resv_region (MSI_RANGE_START ,MSI_RANGE_END -MSI_RANGE_START +1 , -0 ,IOMMU_RESV_MSI ); +0 ,IOMMU_RESV_MSI ,GFP_KERNEL );if (!region)return ;list_add_tail (®ion->list, head); region =iommu_alloc_resv_region (HT_RANGE_START ,HT_RANGE_END -HT_RANGE_START +1 , -0 ,IOMMU_RESV_RESERVED ); +0 ,IOMMU_RESV_RESERVED ,GFP_KERNEL );if (!region)return ;list_add_tail (®ion->list, head);
drivers/iommu/arm-smmu-v3.c +1 -1
@@ -2031,7 +2031,7 @@ static void arm_smmu_get_resv_regions(struct device *dev,int prot =IOMMU_WRITE |IOMMU_NOEXEC |IOMMU_MMIO ; region =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); + prot,IOMMU_RESV_SW_MSI ,GFP_KERNEL );if (!region)return ;
drivers/iommu/arm-smmu.c +1 -1
@@ -1667,7 +1667,7 @@ static void arm_smmu_get_resv_regions(struct device *dev,int prot =IOMMU_WRITE |IOMMU_NOEXEC |IOMMU_MMIO ; region =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); + prot,IOMMU_RESV_SW_MSI ,GFP_KERNEL );if (!region)return ;
drivers/iommu/intel-iommu.c +2 -2
@@ -4221,7 +4221,7 @@ int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg) length = rmrr->end_address - rmrr->base_address +1 ; rmrru->resv =iommu_alloc_resv_region (rmrr->base_address, length, prot, -IOMMU_RESV_DIRECT ); +IOMMU_RESV_DIRECT ,GFP_KERNEL );if (!rmrru->resv) goto free_rmrru;
@@ -5290,7 +5290,7 @@ static void intel_iommu_get_resv_regions(struct device *device, reg =iommu_alloc_resv_region (IOAPIC_RANGE_START ,IOAPIC_RANGE_END -IOAPIC_RANGE_START +1 , -0 ,IOMMU_RESV_MSI ); +0 ,IOMMU_RESV_MSI ,GFP_KERNEL );if (!reg)return ;list_add_tail (®->list, head);
drivers/iommu/iommu.c +4 -3
@@ -260,7 +260,7 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new, } insert: region =iommu_alloc_resv_region (new->start, new->length, - new->prot, new->type); + new->prot, new->type,GFP_KERNEL );if (!region)return -ENOMEM ;
@@ -1898,11 +1898,12 @@ void iommu_put_resv_regions(struct device *dev, struct list_head *list)struct iommu_resv_region *iommu_alloc_resv_region (phys_addr_t start,size_t length,int prot, -enum iommu_resv_type type) +enum iommu_resv_type type, +gfp_t flags) {struct iommu_resv_region *region; - region =kzalloc (sizeof (*region),GFP_KERNEL ); + region =kzalloc (sizeof (*region), flags);if (!region)return NULL ;
include/linux/iommu.h +1 -1
@@ -310,7 +310,7 @@ extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);extern int iommu_request_dm_for_dev (struct device *dev);extern struct iommu_resv_region *iommu_alloc_resv_region (phys_addr_t start,size_t length,int prot, -enum iommu_resv_type type); +enum iommu_resv_type type,gfp_t flags);extern int iommu_get_group_resv_regions (struct iommu_group *group,struct list_head *head);
Signature · 6 lines
2.20.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 2/4 iommu/vt-d: Duplicate iommu_resv_region objects per device list
In reply to Eric Augerintel_iommu_get_resv_regions() aims to return the list of reserved regions accessible by a given @device. However several devices can access the same reserved memory region and when building the list it is not safe to use a single iommu_resv_region object, whose container is the RMRR. This iommu_resv_region must be duplicated per device reserved region list.
Let's remove the struct iommu_resv_region from the RMRR unit and allocate the iommu_resv_region directly in intel_iommu_get_resv_regions().
--- drivers/iommu/intel-iommu.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/iommu/intel-iommu.c +16 -15
@@ -322,7 +322,6 @@ struct dmar_rmrr_unit {u64 end_address;/* reserved end address */ struct dmar_dev_scope *devices;/* target devices */ int devices_cnt;/* target device count */ -struct iommu_resv_region *resv;/* reserved region handle */ };struct dmar_atsr_unit {
@@ -4206,7 +4205,6 @@ static inline void init_iommu_pm_ops(void) {}int __init dmar_parse_one_rmrr (struct acpi_dmar_header *header,void *arg) {struct acpi_dmar_reserved_memory *rmrr; -int prot =DMA_PTE_READ |DMA_PTE_WRITE ;struct dmar_rmrr_unit *rmrru;size_t length;
@@ -4220,22 +4218,16 @@ int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg) rmrru->end_address = rmrr->end_address; length = rmrr->end_address - rmrr->base_address +1 ; - rmrru->resv =iommu_alloc_resv_region (rmrr->base_address, length, prot, -IOMMU_RESV_DIRECT ,GFP_KERNEL ); -if (!rmrru->resv) - goto free_rmrru; rmrru->devices =dmar_alloc_dev_scope ((void *)(rmrr +1 ), ((void *)rmrr) + rmrr->header.length, &rmrru->devices_cnt);if (rmrru->devices_cnt && rmrru->devices ==NULL ) - goto free_all; + goto free_rmrru;list_add (&rmrru->list, &dmar_rmrr_units);return 0 ; -free_all: -kfree (rmrru->resv); free_rmrru:kfree (rmrru); out:
@@ -4453,7 +4445,6 @@ static void intel_iommu_free_dmars(void)list_for_each_entry_safe (rmrru, rmrr_n, &dmar_rmrr_units, list) {list_del (&rmrru->list);dmar_free_dev_scope (&rmrru->devices, &rmrru->devices_cnt); -kfree (rmrru->resv);kfree (rmrru); }
@@ -5271,6 +5262,7 @@ static void intel_iommu_remove_device(struct device *dev)static void intel_iommu_get_resv_regions (struct device *device,struct list_head *head) { +int prot =DMA_PTE_READ |DMA_PTE_WRITE ;struct iommu_resv_region *reg;struct dmar_rmrr_unit *rmrr;struct device *i_dev;
@@ -5280,10 +5272,21 @@ static void intel_iommu_get_resv_regions(struct device *device,for_each_rmrr_units (rmrr) {for_each_active_dev_scope (rmrr->devices, rmrr->devices_cnt, i, i_dev) { +struct iommu_resv_region *resv; +size_t length; +if (i_dev != device)continue ; -list_add_tail (&rmrr->resv->list, head); + length = rmrr->end_address - rmrr->base_address +1 ; + resv =iommu_alloc_resv_region (rmrr->base_address, + length, prot, +IOMMU_RESV_DIRECT , +GFP_ATOMIC ); +if (!resv) +break ; + +list_add_tail (&resv->list, head); } }rcu_read_unlock ();
@@ -5301,10 +5304,8 @@ static void intel_iommu_put_resv_regions(struct device *dev, {struct iommu_resv_region *entry, *next; -list_for_each_entry_safe (entry, next, head, list) { -if (entry->type ==IOMMU_RESV_MSI ) -kfree (entry); - } +list_for_each_entry_safe (entry, next, head, list) +kfree (entry); }#ifdef CONFIG_INTEL_IOMMU_SVM
Signature · 1 line
2.20.1
PATCH 3/4 iommu/vt-d: Handle RMRR with PCI bridge device scopes
In reply to Eric AugerWhen reading the vtd specification and especially the Reserved Memory Region Reporting Structure chapter, it is not obvious a device scope element cannot be a PCI-PCI bridge, in which case all downstream ports are likely to access the reserved memory region. Let's handle this case in device_has_rmrr.
--- drivers/iommu/intel-iommu.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/iommu/intel-iommu.c +23 -9
@@ -736,12 +736,31 @@ static int iommu_dummy(struct device *dev)return dev->archdata.iommu ==DUMMY_DEVICE_DOMAIN_INFO ; } +static bool +is_downstream_to_pci_bridge (struct device *deva,struct device *devb) +{ +struct pci_dev *pdeva, *pdevb; + +if (!dev_is_pci (deva) || !dev_is_pci (devb)) +return false; + + pdeva =to_pci_dev (deva); + pdevb =to_pci_dev (devb); + +if (pdevb->subordinate && + pdevb->subordinate->number <= pdeva->bus->number && + pdevb->subordinate->busn_res.end >= pdeva->bus->number) +return true; + +return false; +} +static struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn) {struct dmar_drhd_unit *drhd =NULL ;struct intel_iommu *iommu;struct device *tmp; -struct pci_dev *ptmp, *pdev =NULL ; +struct pci_dev *pdev =NULL ;u16 segment =0 ;int i;
@@ -787,13 +806,7 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf goto out; } -if (!pdev || !dev_is_pci (tmp)) -continue ; - - ptmp =to_pci_dev (tmp); -if (ptmp->subordinate && - ptmp->subordinate->number <= pdev->bus->number && - ptmp->subordinate->busn_res.end >= pdev->bus->number) +if (is_downstream_to_pci_bridge (dev, tmp)) goto got_pdev; }
@@ -2886,7 +2899,8 @@ static bool device_has_rmrr(struct device *dev)*/ for_each_active_dev_scope (rmrr->devices, rmrr->devices_cnt, i, tmp) -if (tmp == dev) { +if (tmp == dev || +is_downstream_to_pci_bridge (dev, tmp)) {rcu_read_unlock ();return true; }
Signature · 1 line
2.20.1
Re: [PATCH 3/4] iommu/vt-d: Handle RMRR with PCI bridge device scopes
Quoted 20 lines · On Mon, 13 May 2019 09:13:01 +0200 Eric Auger <[email protected]> wrote:
When reading the vtd specification and especially the Reserved Memory Region Reporting Structure chapter, it is not obvious a device scope element cannot be a PCI-PCI bridge, in which case all downstream ports are likely to access the reserved memory region. Let's handle this case in
device_has_rmrr.Fixes ea2447f700ca ("intel-iommu: Prevent devices with RMRRs frombeing placed into SI Domain")
Signed-off-by Eric Auger <[email protected]>--- drivers/iommu/intel-iommu.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)drivers/iommu/intel-iommu.c +0
-0
drivers/iommu/intel-iommu.c static int iommu_dummy(struct device *dev) · line 743
Quoted 8 lines
drivers/iommu/intel-iommu.c +5
-0@@ -736,12 +736,31 @@ static int iommu_dummy(struct device *dev)return dev->archdata.iommu ==DUMMY_DEVICE_DOMAIN_INFO ; } +static bool +is_downstream_to_pci_bridge (struct device *deva,struct device *devb) +{ +struct pci_dev *pdeva, *pdevb; +
is there a more illustrative name for these. i guess deva is is the bridge dev?
Quoted 4 lines
drivers/iommu/intel-iommu.c +4
-0+if (!dev_is_pci (deva) || !dev_is_pci (devb)) +return false; + + pdeva =to_pci_dev (deva);
drivers/iommu/intel-iommu.c static int iommu_dummy(struct device *dev) · line 757
Quoted 10 lines
drivers/iommu/intel-iommu.c +10
-0+ pdevb =to_pci_dev (devb); + +if (pdevb->subordinate && + pdevb->subordinate->number <= pdeva->bus->number && + pdevb->subordinate->busn_res.end >= pdeva->bus->number) +return true; + +return false; +} +
this seems to be a separate cleanup patch.
Quoted 28 lines
drivers/iommu/intel-iommu.c +1
-7static struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn) {struct dmar_drhd_unit *drhd =NULL ;struct intel_iommu *iommu;struct device *tmp; -struct pci_dev *ptmp, *pdev =NULL ; +struct pci_dev *pdev =NULL ;u16 segment =0 ;int i;@@ -787,13 +806,7 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf goto out; } - if (!pdev || !dev_is_pci(tmp)) - continue; - - ptmp = to_pci_dev(tmp); -if (ptmp->subordinate && - ptmp->subordinate->number <=
pdev->bus->number&&drivers/iommu/intel-iommu.c +0
-1- ptmp->subordinate->busn_res.end >=
pdev->bus->number)drivers/iommu/intel-iommu.c +1
-0+if (is_downstream_to_pci_bridge (dev, tmp)) goto got_pdev; }
drivers/iommu/intel-iommu.c static bool device_has_rmrr(struct device *dev) · line 2906
Quoted 9 lines
drivers/iommu/intel-iommu.c +2
-1@@ -2886,7 +2899,8 @@ static bool device_has_rmrr(struct device *dev)*/ for_each_active_dev_scope (rmrr->devices, rmrr->devices_cnt, i, tmp) -if (tmp == dev) { +if (tmp == dev || +is_downstream_to_pci_bridge (dev, tmp)) {rcu_read_unlock ();return true; }
[Jacob Pan]
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 3/4] iommu/vt-d: Handle RMRR with PCI bridge device scopes
Hi Jacob,
Quoted 26 lines · Jacob Pan
Quoted 23 lines · On Mon, 13 May 2019 09:13:01 +0200 Eric Auger <[email protected]> wrote:
When reading the vtd specification and especially the Reserved Memory Region Reporting Structure chapter, it is not obvious a device scope element cannot be a PCI-PCI bridge, in which case all downstream ports are likely to access the reserved memory region. Let's handle this case in
device_has_rmrr.Fixes ea2447f700ca ("intel-iommu: Prevent devices with RMRRs frombeing placed into SI Domain")
Signed-off-by Eric Auger <[email protected]>--- drivers/iommu/intel-iommu.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)drivers/iommu/intel-iommu.c +0
-0@@ -736,12 +736,31 @@ static int iommu_dummy(struct device *dev)return dev->archdata.iommu ==DUMMY_DEVICE_DOMAIN_INFO ; }
Quoted 7 lines
drivers/iommu/intel-iommu.c static int iommu_dummy(struct device *dev) · line 743
Quoted 5 lines
drivers/iommu/intel-iommu.c +5
-0+static bool +is_downstream_to_pci_bridge (struct device *deva,struct device *devb) +{ +struct pci_dev *pdeva, *pdevb; +is there a more illustrative name for these. i guess deva is is the bridge dev?
deva is the candidate PCI device likely to belong to the PCI sub-hierarchy of devb (the candidate bridge).
My concern about the naming was that they are not necessarily a pci device or a bridge. But at least I can add a comment or rename according to your suggestion ;-)
Quoted 6 lines
Quoted 6 lines
drivers/iommu/intel-iommu.c +6
-0+if (!dev_is_pci (deva) || !dev_is_pci (devb)) +return false; + + pdeva =to_pci_dev (deva); + pdevb =to_pci_dev (devb); +
Quoted 8 lines
drivers/iommu/intel-iommu.c static int iommu_dummy(struct device *dev)
Quoted 7 lines
drivers/iommu/intel-iommu.c +7
-0+if (pdevb->subordinate && + pdevb->subordinate->number <= pdeva->bus->number && + pdevb->subordinate->busn_res.end >= pdeva->bus->number) +return true; + +return false; +>> +this seems to be a separate cleanup patch.
I can split into 2 patches: introduction of this helper and its usage in device_to_iommu and second patch using it in iommu_has_rmrr.
Quoted 27 lines
Quoted 27 lines
drivers/iommu/intel-iommu.c +1
-7static struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn) {struct dmar_drhd_unit *drhd =NULL ;struct intel_iommu *iommu;struct device *tmp; -struct pci_dev *ptmp, *pdev =NULL ; +struct pci_dev *pdev =NULL ;u16 segment =0 ;int i;@@ -787,13 +806,7 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf goto out; } - if (!pdev || !dev_is_pci(tmp)) - continue; - - ptmp = to_pci_dev(tmp); -if (ptmp->subordinate && - ptmp->subordinate->number <=
pdev->bus->number&&drivers/iommu/intel-iommu.c +0
-1- ptmp->subordinate->busn_res.end >=
pdev->bus->number)drivers/iommu/intel-iommu.c +1
-0+if (is_downstream_to_pci_bridge (dev, tmp)) goto got_pdev; }
Quoted 13 lines
drivers/iommu/intel-iommu.c static bool device_has_rmrr(struct device *dev) · line 2906
Quoted 10 lines
drivers/iommu/intel-iommu.c +2
-1@@ -2886,7 +2899,8 @@ static bool device_has_rmrr(struct device *dev)*/ for_each_active_dev_scope (rmrr->devices, rmrr->devices_cnt, i, tmp) -if (tmp == dev) { +if (tmp == dev || +is_downstream_to_pci_bridge (dev, tmp)) {rcu_read_unlock ();return true; }[Jacob Pan]
Thanks
Eric
PATCH 4/4 iommu/vt-d: Handle PCI bridge RMRR device scopes in intel_iommu_get_resv_regions
In reply to Eric AugerIn the case the RMRR device scope is a PCI-PCI bridge, let's check the device belongs to the PCI sub-hierarchy.
--- drivers/iommu/intel-iommu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +2 -1
@@ -5289,7 +5289,8 @@ static void intel_iommu_get_resv_regions(struct device *device,struct iommu_resv_region *resv;size_t length; -if (i_dev != device) +if (i_dev != device && + !is_downstream_to_pci_bridge (device, i_dev))continue ; length = rmrr->end_address - rmrr->base_address +1 ;
Signature · 1 line
2.20.1