Enhance dmar to support device hotplug
PATCH v2 1/1 Enhance dmar to support device hotplug
This is the v2 patch, the v1 link: http://marc.info/?l=linux-pci&m=138364004628824&w=2
v1->v2: keep (pci_dev *) pointer array in dmar_drhd_uni, only use pci device id
to update pci_dev * pointer info during device hotplug in intel iommu driver notifier.
Currently, DMAR driver save target pci devices pointers for drhd/rmrr/atsr in (pci_dev *) array, but never update these info after initialization. It's not safe, because pci devices maybe hot added or removed during system running. They will have new pci_dev * pointer. So if there have two IOMMUs or more in system, these devices will find a wrong drhd during DMA mapping. And DMAR faults will occur. This patch save pci device id as well as (pci_dev *) to fix this issue. Pci device id will be used to update pci_dev * poninter during device hotplug in intel iommu driver notifier. Other, here use list to manage target devices for IOMMU,
we can easily use list helper.
Yijing Wang (1):
IOMMU: enhance dmar to support device hotplug
drivers/iommu/dmar.c | 82 +++++++++++----------- drivers/iommu/intel-iommu.c | 161 +++++++++++++++++++++++++++++------------- include/linux/dmar.h | 24 ++++-- 3 files changed, 167 insertions(+), 100 deletions(-)
PATCH v2 1/1 IOMMU: enhance dmar to support device hotplug
Currently, DMAR driver save target pci devices pointers for drhd/rmrr/atsr in (pci_dev *) array, but never update it after initialization. This is not safe, because pci devices maybe hot added or removed during system running. They will have new pci_dev * pointer. So if there have two IOMMUs or more in system, these devices will find a wrong drhd during DMA mapping. And DMAR faults will occur. This patch save pci device id to fix this issue. Pci device id will be used to update pci_dev *pointer info during device hotplug in intel iommu driver notifier, Other we use list to manage target devices for IOMMU, then we can easily use list helper.
Fault log: after remove and rescan a pci device
[ 611.857095] dmar: DRHD: handling fault status reg 2 [ 611.857109] dmar: DMAR:[DMA Read] Request device [86:00.3] fault addr ffff7000 [ 611.857109] DMAR:[fault reason 02] Present bit in context entry is clear [ 611.857524] dmar: DRHD: handling fault status reg 102 [ 611.857534] dmar: DMAR:[DMA Read] Request device [86:00.3] fault addr ffff6000 [ 611.857534] DMAR:[fault reason 02] Present bit in context entry is clear [ 611.857936] dmar: DRHD: handling fault status reg 202 [ 611.857947] dmar: DMAR:[DMA Read] Request device [86:00.3] fault addr ffff5000 [ 611.857947] DMAR:[fault reason 02] Present bit in context entry is clear [ 611.858351] dmar: DRHD: handling fault status reg 302 [ 611.858362] dmar: DMAR:[DMA Read] Request device [86:00.3] fault addr ffff4000 [ 611.858362] DMAR:[fault reason 02] Present bit in context entry is clear [ 611.860819] IPv6: ADDRCONF(NETDEV_UP): eth3: link is not ready [ 611.860983] dmar: DRHD: handling fault status reg 402 [ 611.860995] dmar: INTR-REMAP: Request device [[86:00.3] fault index a4 [ 611.860995] INTR-REMAP:[fault reason 34] Present field in the IRTE entry is clear
--- drivers/iommu/dmar.c | 82 +++++++++++----------- drivers/iommu/intel-iommu.c | 161 +++++++++++++++++++++++++++++------------- include/linux/dmar.h | 24 ++++-- 3 files changed, 167 insertions(+), 100 deletions(-)
drivers/iommu/dmar.c +40 -42
@@ -65,11 +65,12 @@ static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd) }static int __init dmar_parse_one_dev_scope (struct acpi_dmar_device_scope *scope, -struct pci_dev **dev,u16 segment) +struct list_head *head,u16 segment) {struct pci_bus *bus;struct pci_dev *pdev =NULL ;struct acpi_dmar_pci_path *path; +struct dmar_device *dmar_dev;int count; bus =pci_find_bus (segment, scope->bus);
@@ -99,8 +100,7 @@ static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope, }if (!pdev) {pr_warn ("Device scope device [%04x:%02x:%02x.%02x] not found\n" , - segment, scope->bus, path->device, path->function); - *dev =NULL ; + segment, scope->bus, path->dev, path->fn);return 0 ; } if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
@@ -111,50 +111,36 @@ static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope, pci_name(pdev));return -EINVAL ; } - *dev = pdev; + + dmar_dev =kzalloc (sizeof (struct dmar_device ),GFP_KERNEL ); +if (!dmar_dev) { +pci_dev_put (pdev); +return -ENOMEM ; + } + dmar_dev->pdev = pdev; + dmar_dev->bus = pdev->bus->number; + dmar_dev->devfn = pdev->devfn; + dmar_dev->segment = segment; + +list_add_tail (&dmar_dev->list, head); +return 0 ; } -int __init dmar_parse_dev_scope (void *start,void *end,int *cnt, -struct pci_dev ***devices,u16 segment) +int __init dmar_parse_dev_scope (void *start,void *end, +struct list_head *head,u16 segment) {struct acpi_dmar_device_scope *scope; -void * tmp = start; -int index;int ret; - *cnt =0 ; -while (start < end) { - scope = start; -if (scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_ENDPOINT || - scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_BRIDGE ) - (*cnt)++; -else if (scope->entry_type !=ACPI_DMAR_SCOPE_TYPE_IOAPIC && - scope->entry_type !=ACPI_DMAR_SCOPE_TYPE_HPET ) { -pr_warn ("Unsupported device scope\n" ); - } - start += scope->length; - } -if (*cnt ==0 ) -return 0 ; - - *devices =kcalloc (*cnt,sizeof (struct pci_dev *),GFP_KERNEL ); -if (!*devices) -return -ENOMEM ; - - start = tmp; - index =0 ;while (start < end) { scope = start;if (scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_ENDPOINT || scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_BRIDGE ) { - ret =dmar_parse_one_dev_scope (scope, - &(*devices)[index], segment); + ret =dmar_parse_one_dev_scope (scope, head, segment);if (ret) { -kfree (*devices);return ret; } - index ++; } start += scope->length; }
@@ -183,6 +169,7 @@ dmar_parse_one_drhd(struct acpi_dmar_header *header) dmaru->reg_base_addr = drhd->address; dmaru->segment = drhd->segment; dmaru->include_all = drhd->flags &0x1 ;/* BIT0: INCLUDE_ALL */ +INIT_LIST_HEAD (&dmaru->devices); ret =alloc_iommu (dmaru);if (ret) {
@@ -193,6 +180,19 @@ dmar_parse_one_drhd(struct acpi_dmar_header *header)return 0 ; } +static void dmar_free (struct dmar_drhd_unit *dmaru) +{ +struct dmar_device *dmar_dev, *tmp; + +list_for_each_entry_safe (dmar_dev, tmp, + &dmaru->devices, list) { +if (dmar_dev->pdev) +pci_dev_put (dmar_dev->pdev); +list_del (&dmar_dev->list); +kfree (dmar_dev); + } +} +static int __init dmar_parse_dev (struct dmar_drhd_unit *dmaru) {struct acpi_dmar_hardware_unit *drhd;
@@ -205,11 +205,10 @@ static int __init dmar_parse_dev(struct dmar_drhd_unit *dmaru) ret =dmar_parse_dev_scope ((void *)(drhd +1 ), ((void *)drhd) + drhd->header.length, - &dmaru->devices_cnt, &dmaru->devices, - drhd->segment); + &dmaru->devices, drhd->segment);if (ret) {list_del (&dmaru->list); -kfree (dmaru); +dmar_free (dmaru); }return ret; }
@@ -378,14 +377,14 @@ parse_dmar_table(void)return ret; } -static int dmar_pci_device_match (struct pci_dev *devices[],int cnt, +static int dmar_pci_device_match (struct list_head *head,struct pci_dev *dev) { -int index; +struct dmar_device *dmar_dev;while (dev) { -for (index =0 ; index < cnt; index++) -if (dev == devices[index]) +list_for_each_entry (dmar_dev, head, list) +if (dev == dmar_dev->pdev)return 1 ;/* Check our parent */
@@ -412,8 +411,7 @@ dmar_find_matched_drhd_unit(struct pci_dev *dev) drhd->segment == pci_domain_nr(dev->bus))return dmaru; -if (dmar_pci_device_match (dmaru->devices, - dmaru->devices_cnt, dev)) +if (dmar_pci_device_match (&dmaru->devices, dev))return dmaru; }
drivers/iommu/intel-iommu.c +111 -50
291 lines not on this page · raw message
include/linux/dmar.h +16 -8
@@ -32,6 +32,17 @@ struct acpi_dmar_header;#define DMAR_INTR_REMAP 0x1#define DMAR_X2APIC_OPT_OUT 0x2 +/* Save pci device id here to help update +* (pci_dev *) pointer during device hotplug +*/ +struct dmar_device { +struct list_head list; +u8 bus; +u8 devfn; +u16 segment; +struct pci_dev *pdev; +}; +struct intel_iommu ;#ifdef CONFIG_DMAR_TABLE extern struct acpi_table_header *dmar_tbl;
@@ -39,8 +50,7 @@ struct dmar_drhd_unit {struct list_head list;/* list of drhd units */ struct acpi_dmar_header *hdr;/* ACPI header */ u64 reg_base_addr;/* register base address*/ -struct pci_dev **devices;/* target device array */ -int devices_cnt;/* target device count */ +struct list_head devices;/* target device list */ u16 segment;/* PCI domain */ u8 ignored:1; /* ignore drhd */ u8 include_all:1;
@@ -139,8 +149,7 @@ struct dmar_rmrr_unit {struct acpi_dmar_header *hdr;/* ACPI header */ u64 base_address;/* reserved base address*/ u64 end_address;/* reserved end address */ -struct pci_dev **devices;/* target devices */ -int devices_cnt;/* target device count */ +struct list_head devices;/* target device list */ };#define for_each_rmrr_units (rmrr) \
@@ -149,16 +158,15 @@ struct dmar_rmrr_unit {struct dmar_atsr_unit {struct list_head list;/* list of ATSR units */ struct acpi_dmar_header *hdr;/* ACPI header */ -struct pci_dev **devices;/* target devices */ -int devices_cnt;/* target device count */ +struct list_head devices;/* target device list */ u8 include_all:1 ;/* include all ports */ };int dmar_parse_rmrr_atsr_dev (void );extern int dmar_parse_one_rmrr (struct acpi_dmar_header *header);extern int dmar_parse_one_atsr (struct acpi_dmar_header *header); -extern int dmar_parse_dev_scope (void *start,void *end,int *cnt, -struct pci_dev ***devices,u16 segment); +extern int dmar_parse_dev_scope (void *start,void *end, +struct list_head *head,u16 segment);extern int intel_iommu_init (void );#else /* !CONFIG_INTEL_IOMMU: */ static inline int intel_iommu_init (void ) {return -ENODEV ; }
Signature · 1 line
1.7.1
Joerg Roedel
maintainerRe: [PATCH v2] IOMMU: enhance dmar to support device hotplug
Quoted 19 lines · Yijing Wang
drivers/iommu/intel-iommu.c +8
-6@@ -3641,21 +3681,42 @@ static int device_notifier(struct notifier_block *nb,struct device *dev = data;struct pci_dev *pdev =to_pci_dev (dev);struct dmar_domain *domain; +struct dmar_device *dmar_dev; +struct dmar_drhd_unit *drhd; -if (iommu_no_mapping (dev)) -return 0 ; - - domain =find_domain (pdev); -if (!domain) -return 0 ; +switch (action) { +case BUS_NOTIFY_ADD_DEVICE : +for_each_drhd_unit (drhd) +list_for_each_entry (dmar_dev, &drhd->devices, list) +if (dmar_dev->segment ==pci_domain_nr (pdev->bus) + && dmar_dev->bus == pdev->bus->number
drivers/iommu/intel-iommu.c static int device_notifier(struct notifier_block *nb, · line 3702
Quoted 10 lines
drivers/iommu/intel-iommu.c +10
-0+ && dmar_dev->devfn == pdev->devfn) + dmar_dev->pdev =pci_dev_get (pdev); +break ; +case BUS_NOTIFY_DEL_DEVICE : +for_each_drhd_unit (drhd) +list_for_each_entry (dmar_dev, &drhd->devices, list) +if (dmar_dev->pdev == pdev) { +pci_dev_put (pdev); + dmar_dev->pdev =NULL ; + }
How is that synchronized with other users of this dmar_dev structure. Could it happen that you drop the device reference while other parts of the driver still use it?
Joerg
Re: [PATCH v2] IOMMU: enhance dmar to support device hotplug
Quoted 26 lines · Joerg Roedel
Quoted 25 lines · Yijing Wang
drivers/iommu/intel-iommu.c +14
-6@@ -3641,21 +3681,42 @@ static int device_notifier(struct notifier_block *nb,struct device *dev = data;struct pci_dev *pdev =to_pci_dev (dev);struct dmar_domain *domain; +struct dmar_device *dmar_dev; +struct dmar_drhd_unit *drhd; -if (iommu_no_mapping (dev)) -return 0 ; - - domain =find_domain (pdev); -if (!domain) -return 0 ; +switch (action) { +case BUS_NOTIFY_ADD_DEVICE : +for_each_drhd_unit (drhd) +list_for_each_entry (dmar_dev, &drhd->devices, list) +if (dmar_dev->segment ==pci_domain_nr (pdev->bus) + && dmar_dev->bus == pdev->bus->number + && dmar_dev->devfn == pdev->devfn) + dmar_dev->pdev =pci_dev_get (pdev); +break ; +case BUS_NOTIFY_DEL_DEVICE : +for_each_drhd_unit (drhd) +list_for_each_entry (dmar_dev, &drhd->devices, list)
Quoted 8 lines
drivers/iommu/intel-iommu.c static int device_notifier(struct notifier_block *nb, · line 3702
Quoted 4 lines
drivers/iommu/intel-iommu.c +4
-0+if (dmar_dev->pdev == pdev) { +pci_dev_put (pdev); + dmar_dev->pdev =NULL ; + }How is that synchronized with other users of this
dmar_devstructure. Could it happen that you drop the device reference while other parts of the driver still use it?
Hi Joerg,
Thanks for your review and comments!
We use original struct dmar_drhd_unit->devices to attach the pci device to specific DMAR, eg. use dmar_find_matched_drhd_unit() to find the attached DMAR by pci_dev * pointer. So if the related pci_dev was removed, I think we can safely set dmar_dev->pdev = NULL; No pci device will use it again until the new pci device hot add.
One problem in this solution is PCI bus number maybe changed after device hotplug, so use the bus,device,function id to update the dmar device scope maybe unreliable.
Jiang Liu also provide a solution to fix this problem by save device scope pathes, I think that's a good idea.
Thanks!
Yijing.
Quoted 13 lines
Joerg
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/.
Signature · 2 lines
Thanks! Yijing
Quoted 26 lines · Yijing Wang
This is the v2 patch, the v1 link: http://marc.info/?l=linux-pci&m=138364004628824&w=2
v1->v2: keep (pci_dev*) pointer array indmar_drhd_uni, only use pci device id
to updatepci_dev* pointer info during device hotplug in intel iommu driver notifier.Currently, DMAR driver save target pci devices pointers for drhd/rmrr/atsr in (
pci_dev*) array, but never update these info after initialization. It's not safe, because pci devices maybe hot added or removed during system running. They will have newpci_dev* pointer. So if there have two IOMMUs or more in system, these devices will find a wrong drhd during DMA mapping. And DMAR faults will occur. This patch save pci device id as well as (pci_dev*) to fix this issue. Pci device id will be used to updatepci_dev* poninter during device hotplug in intel iommu driver notifier. Other, here use list to manage target devices for IOMMU,
we can easily use list helper.Yijing Wang (1):
IOMMU: enhance dmar to support device hotplugdrivers/iommu/dmar.c | 82 +++++++++++----------- drivers/iommu/intel-iommu.c | 161 +++++++++++++++++++++++++++++------------- include/linux/dmar.h | 24 ++++-- 3 files changed, 167 insertions(+), 100 deletions(-)
Quoted 5 lines
_______________________________________________
iommu mailing list iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Can this bug & fix be demonstrated by configuring & de-configuring VFs on an SRIOV device, since that effectively looks like a hot-add & hot-remove ?
Quoted 27 lines · Don Dutile
Quoted 26 lines · Yijing Wang
This is the v2 patch, the v1 link: http://marc.info/?l=linux-pci&m=138364004628824&w=2
v1->v2: keep (pci_dev*) pointer array indmar_drhd_uni, only use pci device id
to updatepci_dev* pointer info during device hotplug in intel iommu driver notifier.Currently, DMAR driver save target pci devices pointers for drhd/rmrr/atsr in (
pci_dev*) array, but never update these info after initialization. It's not safe, because pci devices maybe hot added or removed during system running. They will have newpci_dev* pointer. So if there have two IOMMUs or more in system, these devices will find a wrong drhd during DMA mapping. And DMAR faults will occur. This patch save pci device id as well as (pci_dev*) to fix this issue. Pci device id will be used to updatepci_dev* poninter during device hotplug in intel iommu driver notifier. Other, here use list to manage target devices for IOMMU,
we can easily use list helper.Yijing Wang (1):
IOMMU: enhance dmar to support device hotplugdrivers/iommu/dmar.c | 82 +++++++++++----------- drivers/iommu/intel-iommu.c | 161 +++++++++++++++++++++++++++++------------- include/linux/dmar.h | 24 ++++-- 3 files changed, 167 insertions(+), 100 deletions(-)
Quoted 8 lines
Quoted 5 lines
_______________________________________________
iommu mailing list iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/iommuCan this bug & fix be demonstrated by configuring & de-configuring VFs on an SRIOV device, since that effectively looks like a hot-add & hot-remove ?
Hi Don,
It seems can not be demonstrated by configuring & de-configuring VFs, because
if we only hotplug VFs, the device scope info saved in drhd->devices array will not become stale. In my platform, the target device scope devices are root port, so only we remove and rescan root port, we can trigger this problem.
Thanks!
Yijing.
Quoted 5 lines
.
Signature · 2 lines
Thanks! Yijing