Skip to contentSkip to conversation

irqchip/gic-v4: Fix ordering between vmapp and vpe locks

PATCH Superseded Marc Zyngier 3 participants 6 messages Jul 23, 2024 – Jul 29, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 irqchip/gic-v4: Fix ordering between vmapp and vpe locks

The (recently established) lock ordering mandates that the per-VM vmapp_lock is acquired before we take the per-VPE lock.

As it turns out, its_vpe_set_affinity() takes the VPE lock, and then calls into its_send_vmovp(), which itself takes the vmapp lock. Obviously, this isn't what we want.

As its_send_vmovp() is only called from its_vpe_set_affinity(), hoist the vmapp locking from the former into the latter, restoring the expected order.

Fixes f0eb154c39471 ("irqchip/gic-v4: Substitute vmovp_lock for a per-VM lock")
---
 drivers/irqchip/irq-gic-v3-its.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +11 -7
index 951ec140bcea2..b88c6011c8771 100644
@@ -1328,12 +1328,6 @@ static void its_send_vmovp(struct its_vpe *vpe)
 		return;
 	}
 
-	/*
-	 * Protect against concurrent updates of the mapping state on
-	 * individual VMs.
-	 */
-	guard(raw_spinlock_irqsave)(&vpe->its_vm->vmapp_lock);
-
 	/*
 	 * Yet another marvel of the architecture. If using the
 	 * its_list "feature", we need to make sure that all ITSs
@@ -3808,7 +3802,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	unsigned int from, cpu = nr_cpu_ids;
 	struct cpumask *table_mask;
-	unsigned long flags;
+	unsigned long flags, vmapp_flags;
 
 	/*
 	 * Changing affinity is mega expensive, so let's be as lazy as
@@ -3822,7 +3816,14 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	 * protect us, and that we must ensure nobody samples vpe->col_idx
 	 * during the update, hence the lock below which must also be
 	 * taken on any vLPI handling path that evaluates vpe->col_idx.
+	 *
+	 * Finally, we must protect ourselves against concurrent
+	 * updates of the mapping state on this VM should the ITS list
+	 * be in use.
 	 */
+	if (its_list_map)
+		raw_spin_lock_irqsave(&vpe->its_vm->vmapp_lock, vmapp_flags);
+
 	from = vpe_to_cpuid_lock(vpe, &flags);
 	table_mask = gic_data_rdist_cpu(from)->vpe_table_mask;
 
@@ -3852,6 +3853,9 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
 	vpe_to_cpuid_unlock(vpe, flags);
 
+	if (its_list_map)
+		raw_spin_unlock_irqrestore(&vpe->its_vm->vmapp_lock, vmapp_flags);
+
 	return IRQ_SET_MASK_OK_DONE;
 }
 
Signature · 1 line
2.39.2
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Marc Zyngier

The (recently established) lock ordering mandates that the per-VM vmapp_lock is acquired before we take the per-VPE lock.

As it turns out, its_vpe_set_affinity() takes the VPE lock, and then calls into its_send_vmovp(), which itself takes the vmapp lock. Obviously, this isn't what we want.

Quoted 6 lines

As its_send_vmovp() is only called from its_vpe_set_affinity(), hoist the vmapp locking from the former into the latter, restoring the expected order.

Fixes f0eb154c39471 ("irqchip/gic-v4: Substitute vmovp_lock for a per-VM lock")

Hi Marc,

Thanks,
Zhou

Quoted 56 lines
---
 drivers/irqchip/irq-gic-v3-its.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +11 -7
index 951ec140bcea2..b88c6011c8771 100644
@@ -1328,12 +1328,6 @@ static void its_send_vmovp(struct its_vpe *vpe)
 		return;
 	}
 
-	/*
-	 * Protect against concurrent updates of the mapping state on
-	 * individual VMs.
-	 */
-	guard(raw_spinlock_irqsave)(&vpe->its_vm->vmapp_lock);
-
 	/*
 	 * Yet another marvel of the architecture. If using the
 	 * its_list "feature", we need to make sure that all ITSs
@@ -3808,7 +3802,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	unsigned int from, cpu = nr_cpu_ids;
 	struct cpumask *table_mask;
-	unsigned long flags;
+	unsigned long flags, vmapp_flags;
 
 	/*
 	 * Changing affinity is mega expensive, so let's be as lazy as
@@ -3822,7 +3816,14 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	 * protect us, and that we must ensure nobody samples vpe->col_idx
 	 * during the update, hence the lock below which must also be
 	 * taken on any vLPI handling path that evaluates vpe->col_idx.
+	 *
+	 * Finally, we must protect ourselves against concurrent
+	 * updates of the mapping state on this VM should the ITS list
+	 * be in use.
 	 */
+	if (its_list_map)
+		raw_spin_lock_irqsave(&vpe->its_vm->vmapp_lock, vmapp_flags);
+
 	from = vpe_to_cpuid_lock(vpe, &flags);
 	table_mask = gic_data_rdist_cpu(from)->vpe_table_mask;
 
@@ -3852,6 +3853,9 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
 	vpe_to_cpuid_unlock(vpe, flags);
 
+	if (its_list_map)
+		raw_spin_unlock_irqrestore(&vpe->its_vm->vmapp_lock, vmapp_flags);
+
 	return IRQ_SET_MASK_OK_DONE;
 }
 
#2 lore raw
In reply to Marc Zyngier

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3805

Quoted 6 lines · Marc Zyngier
drivers/irqchip/irq-gic-v3-its.c +1 -1
@@ -3808,7 +3802,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	unsigned int from, cpu = nr_cpu_ids;
 	struct cpumask *table_mask;
-	unsigned long flags;
+	unsigned long flags, vmapp_flags;

What's this flags business for? its_vpe_set_affinity() is called with interrupts disabled, no?

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3825

Quoted 13 lines
drivers/irqchip/irq-gic-v3-its.c +6 -0
 	/*
 	 * Changing affinity is mega expensive, so let's be as lazy as
@@ -3822,7 +3816,14 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	 * protect us, and that we must ensure nobody samples vpe->col_idx
 	 * during the update, hence the lock below which must also be
 	 * taken on any vLPI handling path that evaluates vpe->col_idx.
+	 *
+	 * Finally, we must protect ourselves against concurrent
+	 * updates of the mapping state on this VM should the ITS list
+	 * be in use.
 	 */
+	if (its_list_map)
+		raw_spin_lock_irqsave(&vpe->its_vm->vmapp_lock, vmapp_flags);

Confused. This changes the locking from unconditional to conditional. What's the rationale here?

Thanks,

tglx

author maintainer
#3 lore raw
Quoted 11 lines · Thomas Gleixner

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3805

Quoted 6 lines · Marc Zyngier
drivers/irqchip/irq-gic-v3-its.c +1 -1
@@ -3808,7 +3802,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	unsigned int from, cpu = nr_cpu_ids;
 	struct cpumask *table_mask;
-	unsigned long flags;
+	unsigned long flags, vmapp_flags;

What's this flags business for? its_vpe_set_affinity() is called with interrupts disabled, no?

Duh. Of course. Cargo-culted braindead logic. I'll fix that.

Quoted 17 lines
Quoted 4 lines
drivers/irqchip/irq-gic-v3-its.c +0 -0
 	/*
 	 * Changing affinity is mega expensive, so let's be as lazy as

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3825

Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +6 -0
@@ -3822,7 +3816,14 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	 * protect us, and that we must ensure nobody samples vpe->col_idx
 	 * during the update, hence the lock below which must also be
 	 * taken on any vLPI handling path that evaluates vpe->col_idx.
+	 *
+	 * Finally, we must protect ourselves against concurrent
+	 * updates of the mapping state on this VM should the ITS list
+	 * be in use.
 	 */
+	if (its_list_map)
+		raw_spin_lock_irqsave(&vpe->its_vm->vmapp_lock, vmapp_flags);

Confused. This changes the locking from unconditional to conditional. What's the rationale here?

I think I'm confused too. I've written this as a mix of the VMOVP lock (which must be conditional) and the new VMAPP lock, which must be taken to avoid racing against a new vcpu coming up. And of course, this makes zero sense.

I'll get some sleep first, and then fix this correctly. Thanks for spotting it.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#4 lore raw
In reply to Thomas Gleixner
Quoted 12 lines · Thomas Gleixner

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3805

Quoted 6 lines · Marc Zyngier
drivers/irqchip/irq-gic-v3-its.c +1 -1
@@ -3808,7 +3802,7 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	unsigned int from, cpu = nr_cpu_ids;
 	struct cpumask *table_mask;
-	unsigned long flags;
+	unsigned long flags, vmapp_flags;

What's this flags business for? its_vpe_set_affinity() is called with interrupts disabled, no?

Quoted 16 lines

drivers/irqchip/irq-gic-v3-its.c static int its_vpe_set_affinity(struct irq_data *d, · line 3825

Quoted 13 lines
drivers/irqchip/irq-gic-v3-its.c +6 -0
 	/*
 	 * Changing affinity is mega expensive, so let's be as lazy as
@@ -3822,7 +3816,14 @@ static int its_vpe_set_affinity(struct irq_data *d,
 	 * protect us, and that we must ensure nobody samples vpe->col_idx
 	 * during the update, hence the lock below which must also be
 	 * taken on any vLPI handling path that evaluates vpe->col_idx.
+	 *
+	 * Finally, we must protect ourselves against concurrent
+	 * updates of the mapping state on this VM should the ITS list
+	 * be in use.
 	 */
+	if (its_list_map)
+		raw_spin_lock_irqsave(&vpe->its_vm->vmapp_lock, vmapp_flags);

Confused. This changes the locking from unconditional to conditional. What's the rationale here?

Haven't managed to sleep much, but came to the conclusion that I wasn't that stupid in my initial patch. Let's look at the full picture, starting with its_send_vmovp():

        if (!its_list_map) {
                its = list_first_entry(&its_nodes, struct its_node, entry);
                desc.its_vmovp_cmd.col = &its->collections[col_id];
                its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
                return;
        }

        /*
         * Protect against concurrent updates of the mapping state on
         * individual VMs.
         */
        guard(raw_spinlock_irqsave)(&vpe->its_vm->vmapp_lock);

The vmapp locking *is* conditional. Which makes a lot of sense as the presence of ITS list is the only thing that prevents the VPEs from being mapped eagerly at VM startup time (although this is a performance reason, and not a correctness issue).

So there is no point in taking that lock if there is no ITS list, given that the VPEs are mapped before we can do anything else. This has the massive benefit of allowing concurrent VPE affinity changes on modern HW.

This means that on GICv4.0 without ITSList or GICv4.1, the only lock we need to acquire is the VPE lock itself on VPE affinity change.

I'll respin the patch shortly.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw
Quoted 22 lines · Marc Zyngier
Quoted 2 lines · Thomas Gleixner

Confused. This changes the locking from unconditional to conditional. What's the rationale here?

Haven't managed to sleep much, but came to the conclusion that I wasn't that stupid in my initial patch. Let's look at the full picture, starting with its_send_vmovp():

        if (!its_list_map) {
                its = list_first_entry(&its_nodes, struct its_node, entry);
                desc.its_vmovp_cmd.col = &its->collections[col_id];
                its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
                return;
        }

        /*
         * Protect against concurrent updates of the mapping state on
         * individual VMs.
         */
        guard(raw_spinlock_irqsave)(&vpe->its_vm->vmapp_lock);
Quoted 1 line

The vmapp locking *is* conditional. Which makes a lot of sense as the

Misread the patch ...