Skip to content

Commit f3a0592

Browse files
author
Marc Zyngier
committed
irqchip/gic-v4.1: Ensure mutual exclusion between vPE affinity change and RD access
Before GICv4.1, all operations would be serialized with the affinity changes by virtue of using the same ITS command queue. With v4.1, things change, as invalidations (and a number of other operations) are issued using the redistributor MMIO frame. We must thus make sure that these redistributor accesses cannot race against aginst the affinity change, or we may end-up talking to the wrong redistributor. To ensure this, we expand the irq_to_cpuid() helper to take a spinlock when the LPI is mapped to a vLPI (a new per-VPE lock) on each operation that requires mutual exclusion. Signed-off-by: Marc Zyngier <maz@kernel.org> Reviewed-by: Zenghui Yu <yuzenghui@huawei.com> Link: https://lore.kernel.org/r/20200304203330.4967-4-maz@kernel.org
1 parent 28d160d commit f3a0592

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎drivers/irqchip/irq-gic-v3-its.c‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,41 @@ static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
239239
return NULL;
240240
}
241241

242-
static int irq_to_cpuid(struct irq_data *d)
242+
static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
243+
{
244+
raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
245+
return vpe->col_idx;
246+
}
247+
248+
static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
249+
{
250+
raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
251+
}
252+
253+
static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
243254
{
244-
struct its_device *its_dev = irq_data_get_irq_chip_data(d);
245255
struct its_vlpi_map *map = get_vlpi_map(d);
256+
int cpu;
246257

247-
if (map)
248-
return map->vpe->col_idx;
258+
if (map) {
259+
cpu = vpe_to_cpuid_lock(map->vpe, flags);
260+
} else {
261+
/* Physical LPIs are already locked via the irq_desc lock */
262+
struct its_device *its_dev = irq_data_get_irq_chip_data(d);
263+
cpu = its_dev->event_map.col_map[its_get_event_id(d)];
264+
/* Keep GCC quiet... */
265+
*flags = 0;
266+
}
249267

250-
return its_dev->event_map.col_map[its_get_event_id(d)];
268+
return cpu;
269+
}
270+
271+
static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
272+
{
273+
struct its_vlpi_map *map = get_vlpi_map(d);
274+
275+
if (map)
276+
vpe_to_cpuid_unlock(map->vpe, flags);
251277
}
252278

253279
static struct its_collection *valid_col(struct its_collection *col)
@@ -1329,7 +1355,9 @@ static void direct_lpi_inv(struct irq_data *d)
13291355
{
13301356
struct its_vlpi_map *map = get_vlpi_map(d);
13311357
void __iomem *rdbase;
1358+
unsigned long flags;
13321359
u64 val;
1360+
int cpu;
13331361

13341362
if (map) {
13351363
struct its_device *its_dev = irq_data_get_irq_chip_data(d);
@@ -1344,10 +1372,12 @@ static void direct_lpi_inv(struct irq_data *d)
13441372
}
13451373

13461374
/* Target the redistributor this LPI is currently routed to */
1347-
rdbase = per_cpu_ptr(gic_rdists->rdist, irq_to_cpuid(d))->rd_base;
1375+
cpu = irq_to_cpuid_lock(d, &flags);
1376+
rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
13481377
gic_write_lpir(val, rdbase + GICR_INVLPIR);
13491378

13501379
wait_for_syncr(rdbase);
1380+
irq_to_cpuid_unlock(d, flags);
13511381
}
13521382

13531383
static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
@@ -3486,17 +3516,25 @@ static int its_vpe_set_affinity(struct irq_data *d,
34863516
{
34873517
struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
34883518
int from, cpu = cpumask_first(mask_val);
3519+
unsigned long flags;
34893520

34903521
/*
34913522
* Changing affinity is mega expensive, so let's be as lazy as
34923523
* we can and only do it if we really have to. Also, if mapped
34933524
* into the proxy device, we need to move the doorbell
34943525
* interrupt to its new location.
3526+
*
3527+
* Another thing is that changing the affinity of a vPE affects
3528+
* *other interrupts* such as all the vLPIs that are routed to
3529+
* this vPE. This means that the irq_desc lock is not enough to
3530+
* protect us, and that we must ensure nobody samples vpe->col_idx
3531+
* during the update, hence the lock below which must also be
3532+
* taken on any vLPI handling path that evaluates vpe->col_idx.
34953533
*/
3496-
if (vpe->col_idx == cpu)
3534+
from = vpe_to_cpuid_lock(vpe, &flags);
3535+
if (from == cpu)
34973536
goto out;
34983537

3499-
from = vpe->col_idx;
35003538
vpe->col_idx = cpu;
35013539

35023540
/*
@@ -3512,6 +3550,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
35123550

35133551
out:
35143552
irq_data_update_effective_affinity(d, cpumask_of(cpu));
3553+
vpe_to_cpuid_unlock(vpe, flags);
35153554

35163555
return IRQ_SET_MASK_OK_DONE;
35173556
}
@@ -3855,6 +3894,7 @@ static int its_vpe_init(struct its_vpe *vpe)
38553894
return -ENOMEM;
38563895
}
38573896

3897+
raw_spin_lock_init(&vpe->vpe_lock);
38583898
vpe->vpe_id = vpe_id;
38593899
vpe->vpt_page = vpt_page;
38603900
if (gic_rdists->has_rvpeid)

‎include/linux/irqchip/arm-gic-v4.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ struct its_vpe {
5353
};
5454
};
5555

56+
/*
57+
* Ensures mutual exclusion between affinity setting of the
58+
* vPE and vLPI operations using vpe->col_idx.
59+
*/
60+
raw_spinlock_t vpe_lock;
5661
/*
5762
* This collection ID is used to indirect the target
5863
* redistributor for this VPE. The ID itself isn't involved in

0 commit comments

Comments
 (0)