Skip to contentSkip to conversation

smp: Evaluate local cond_func() before IPI side-effects

RFC PATCH Applied Mathieu Desnoyers 3 participants 3 messages Dec 3, 2024 – Dec 5, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 smp: Evaluate local cond_func() before IPI side-effects

In smp_call_function_many_cond(), the local cond_func() is evaluated after triggering the remote CPU IPIs.

If cond_func() depends on loading shared state updated by other CPU's IPI handlers func(), then triggering execution of remote CPUs IPI before evaluating cond_func() may have unexpected consequences.

One example scenario is evaluating a jiffies delay in cond_func(), which is updated by func() in the IPI handlers. This situation can prevent execution of periodic cleanup code on the local CPU.

---
 kernel/smp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/smp.c +3 -2
index 27dc31a146a3..f104c8e83fc4 100644
@@ -815,7 +815,8 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 	WARN_ON_ONCE(!in_task());
 
 	/* Check if we need local execution. */
-	if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
+	if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask) &&
+	    (!cond_func || cond_func(this_cpu, info)))
 		run_local = true;
 
 	/* Check if we need remote execution, i.e., any CPU excluding this one. */
@@ -868,7 +869,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 			send_call_function_ipi_mask(cfd->cpumask_ipi);
 	}
 
-	if (run_local && (!cond_func || cond_func(this_cpu, info))) {
+	if (run_local) {
 		unsigned long flags;
 
 		local_irq_save(flags);
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Mathieu Desnoyers

In smp_call_function_many_cond(), the local cond_func() is evaluated after triggering the remote CPU IPIs.

If cond_func() depends on loading shared state updated by other CPU's IPI handlers func(), then triggering execution of remote CPUs IPI before
evaluating cond_func() may have unexpected consequences.

Quoted 8 lines

One example scenario is evaluating a jiffies delay in cond_func(), which
is updated by func() in the IPI handlers. This situation can prevent execution of periodic cleanup code on the local CPU.

Signature · 1 line
All Rights Reversed.
#2 lore raw

[tip: locking/core] smp/scf: Evaluate local cond_func() before IPI side-effects

In reply to Mathieu Desnoyers
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     63a48181fbcddefe5fb4c6618938bb64c543945b
Gitweb:        https://git.kernel.org/tip/63a48181fbcddefe5fb4c6618938bb64c543945b
Author:        Mathieu Desnoyers <[email protected]>
AuthorDate:    Tue, 03 Dec 2024 11:35:58 -05:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 05 Dec 2024 14:25:28 +01:00

smp/scf: Evaluate local cond_func() before IPI side-effects

In smp_call_function_many_cond(), the local cond_func() is evaluated after triggering the remote CPU IPIs.

If cond_func() depends on loading shared state updated by other CPU's IPI handlers func(), then triggering execution of remote CPUs IPI before evaluating cond_func() may have unexpected consequences.

One example scenario is evaluating a jiffies delay in cond_func(), which is updated by func() in the IPI handlers. This situation can prevent execution of periodic cleanup code on the local CPU.

---
 kernel/smp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/smp.c +3 -2
index 27dc31a..f104c8e 100644
@@ -815,7 +815,8 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 	WARN_ON_ONCE(!in_task());
 
 	/* Check if we need local execution. */
-	if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
+	if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask) &&
+	    (!cond_func || cond_func(this_cpu, info)))
 		run_local = true;
 
 	/* Check if we need remote execution, i.e., any CPU excluding this one. */
@@ -868,7 +869,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 			send_call_function_ipi_mask(cfd->cpumask_ipi);
 	}
 
-	if (run_local && (!cond_func || cond_func(this_cpu, info))) {
+	if (run_local) {
 		unsigned long flags;
 
 		local_irq_save(flags);