iommu/amd: Avoid get_irq_table() from atomic context
PATCH 1/2 iommu/amd: Avoid get_irq_table() from atomic context
get_irq_table() acquires amd_iommu_devtable_lock which is not a raw lock, and thus cannot be acquired from atomic context on PREEMPT_RT. Many calls to modify_irte*() come from atomic context due to the IRQ desc->lock, as does amd_iommu_update_ga() due to the preemption disabling in vcpu_load/put().
The only difference between calling get_irq_table() and reading from irq_lookup_table[] directly, other than the lock acquisition and amd_iommu_rlookup_table[] check, is if the table entry is unpopulated, which should never happen when looking up a devid that came from an irq_2_irte struct, as get_irq_table() would have already been called on that devid during irq_remapping_alloc().
The lock acquisition is not needed in these cases because entries in irq_lookup_table[] never change once non-NULL -- nor would the amd_iommu_devtable_lock usage in get_irq_table() provide meaningful protection if they did, since it's released before using the looked up table in the get_irq_table() caller.
The amd_iommu_rlookup_table[] check is not needed because irq_lookup_table[devid] should never be non-NULL if amd_iommu_rlookup_table[devid] is NULL.
--- drivers/iommu/amd_iommu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iommu/amd_iommu.c +4 -4
@@ -3732,7 +3732,7 @@ static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,if (iommu ==NULL )return -EINVAL ; - table =get_irq_table (devid, false); + table = irq_lookup_table[devid];if (!table)return -ENOMEM ;
@@ -3765,7 +3765,7 @@ static int modify_irte(u16 devid, int index, union irte *irte)if (iommu ==NULL )return -EINVAL ; - table =get_irq_table (devid, false); + table = irq_lookup_table[devid];if (!table)return -ENOMEM ;
@@ -3789,7 +3789,7 @@ static void free_irte(u16 devid, int index)if (iommu ==NULL )return ; - table =get_irq_table (devid, false); + table = irq_lookup_table[devid];if (!table)return ;
@@ -4392,7 +4392,7 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *data)if (!iommu)return -ENODEV ; - irt =get_irq_table (devid, false); + irt = irq_lookup_table[devid];if (!irt)return -ENODEV ;
Signature · 1 line
1.8.3.1
A message that is not in the archive (sent off-list or not yet received): 20180121092854.28290-1-swood-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
PATCH 2/2 amd/iommu: Use raw locks on atomic context paths
Several functions in this driver are called from atomic context, and thus raw locks must be used in order to be safe on PREEMPT_RT.
This includes paths that must wait for command completion, which is a potential PREEMPT_RT latency concern but not easily avoidable.
--- drivers/iommu/amd_iommu.c | 30 +++++++++++++++--------------- drivers/iommu/amd_iommu_init.c | 2 +- drivers/iommu/amd_iommu_types.h | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-)
drivers/iommu/amd_iommu.c +15 -15
@@ -1055,9 +1055,9 @@ static int iommu_queue_command_sync(struct amd_iommu *iommu,unsigned long flags;int ret; -spin_lock_irqsave (&iommu->lock, flags); +raw_spin_lock_irqsave (&iommu->lock, flags); ret =__iommu_queue_command_sync (iommu, cmd, sync); -spin_unlock_irqrestore (&iommu->lock, flags); +raw_spin_unlock_irqrestore (&iommu->lock, flags);return ret; }
@@ -1083,7 +1083,7 @@ static int iommu_completion_wait(struct amd_iommu *iommu)build_completion_wait (&cmd, (u64)&iommu->cmd_sem); -spin_lock_irqsave (&iommu->lock, flags); +raw_spin_lock_irqsave (&iommu->lock, flags); iommu->cmd_sem =0 ;
@@ -1094,7 +1094,7 @@ static int iommu_completion_wait(struct amd_iommu *iommu) ret =wait_on_sem (&iommu->cmd_sem); out_unlock: -spin_unlock_irqrestore (&iommu->lock, flags); +raw_spin_unlock_irqrestore (&iommu->lock, flags);return ret; }
@@ -3626,7 +3626,7 @@ static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic) goto out_unlock;/* Initialize table spin-lock */ -spin_lock_init (&table->lock); +raw_spin_lock_init (&table->lock);if (ioapic)/* Keep the first 32 indexes free for IOAPIC interrupts */
@@ -3688,7 +3688,7 @@ static int alloc_irq_index(u16 devid, int count, bool align)if (align) alignment =roundup_pow_of_two (count); -spin_lock_irqsave (&table->lock, flags); +raw_spin_lock_irqsave (&table->lock, flags);/* Scan table for free entries */ for (index = ALIGN(table->min_index, alignment), c = 0;
@@ -3715,7 +3715,7 @@ static int alloc_irq_index(u16 devid, int count, bool align) index = -ENOSPC ; out: -spin_unlock_irqrestore (&table->lock, flags); +raw_spin_unlock_irqrestore (&table->lock, flags);return index; }
@@ -3736,7 +3736,7 @@ static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,if (!table)return -ENOMEM ; -spin_lock_irqsave (&table->lock, flags); +raw_spin_lock_irqsave (&table->lock, flags); entry = (struct irte_ga *)table->table; entry = &entry[index];
@@ -3747,7 +3747,7 @@ static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,if (data) data->ref = entry; -spin_unlock_irqrestore (&table->lock, flags); +raw_spin_unlock_irqrestore (&table->lock, flags);iommu_flush_irt (iommu, devid);iommu_completion_wait (iommu);
@@ -3769,9 +3769,9 @@ static int modify_irte(u16 devid, int index, union irte *irte)if (!table)return -ENOMEM ; -spin_lock_irqsave (&table->lock, flags); +raw_spin_lock_irqsave (&table->lock, flags); table->table[index] = irte->val; -spin_unlock_irqrestore (&table->lock, flags); +raw_spin_unlock_irqrestore (&table->lock, flags);iommu_flush_irt (iommu, devid);iommu_completion_wait (iommu);
@@ -3793,9 +3793,9 @@ static void free_irte(u16 devid, int index)if (!table)return ; -spin_lock_irqsave (&table->lock, flags); +raw_spin_lock_irqsave (&table->lock, flags); iommu->irte_ops->clear_allocated (table, index); -spin_unlock_irqrestore (&table->lock, flags); +raw_spin_unlock_irqrestore (&table->lock, flags);iommu_flush_irt (iommu, devid);iommu_completion_wait (iommu);
@@ -4396,7 +4396,7 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *data)if (!irt)return -ENODEV ; -spin_lock_irqsave (&irt->lock, flags); +raw_spin_lock_irqsave (&irt->lock, flags);if (ref->lo.fields_vapic.guest_mode) {if (cpu >=0 )
@@ -4405,7 +4405,7 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *data)barrier (); } -spin_unlock_irqrestore (&irt->lock, flags); +raw_spin_unlock_irqrestore (&irt->lock, flags);iommu_flush_irt (iommu, devid);iommu_completion_wait (iommu);
drivers/iommu/amd_iommu_init.c +1 -1
@@ -1474,7 +1474,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h) {int ret; -spin_lock_init (&iommu->lock); +raw_spin_lock_init (&iommu->lock);/* Add IOMMU to internal data structures */ list_add_tail (&iommu->list, &amd_iommu_list);
drivers/iommu/amd_iommu_types.h +2 -2
@@ -408,7 +408,7 @@#define IRQ_TABLE_ALIGNMENT 128struct irq_remap_table { -spinlock_t lock; +raw_spinlock_t lock;unsigned min_index;u32 *table; };
@@ -490,7 +490,7 @@ struct amd_iommu {int index;/* locks the accesses to the hardware */ -spinlock_t lock; +raw_spinlock_t lock;/* Pointer to PCI device of this IOMMU */ struct pci_dev *dev;
Signature · 1 line
1.8.3.1
A message that is not in the archive (sent off-list or not yet received): 20180121092854.28290-2-swood-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Joerg Roedel
AppliedRe: [PATCH 2/2] amd/iommu: Use raw locks on atomic context paths
Quoted 6 lines · Scott Wood
Several functions in this driver are called from atomic context, and thus raw locks must be used in order to be safe on
PREEMPT_RT.This includes paths that must wait for command completion, which is a potential
PREEMPT_RTlatency concern but not easily avoidable.
Quoted 6 lines
Signed-off-by Scott Wood <[email protected]>--- drivers/iommu/amd_iommu.c | 30 +++++++++++++++--------------- drivers/iommu/amd_iommu_init.c | 2 +- drivers/iommu/amd_iommu_types.h | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-)
Applied, thanks.
Hi Scott,
Quoted 3 lines · Scott Wood
The amd_iommu_rlookup_table[] check is not needed because irq_lookup_table[devid] should never be non-NULL if amd_iommu_rlookup_table[devid] is NULL.
Your reasoning is correct, but I'd like the patch make the code more robust when accessing the irq_lookup_table directly.
Can you change rename get_irq_table() into something like alloc_irq_table() and build a new get_irq_table() around the irq_lookup_table access that throws a WARN_ON when the pointer is NULL?
Joerg