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

Mathieu Desnoyers posted 1 patch 1 year ago
kernel/smp.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH] smp: Evaluate local cond_func() before IPI side-effects
Posted by Mathieu Desnoyers 1 year ago
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.

Link: https://lore.kernel.org/lkml/20241202202213.26a79ed6@fangorn/
Reviewed-by: Rik van Riel <riel@surriel.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mel Gorman <mgorman@suse.de>
Cc: x86@kernel.org
---
 kernel/smp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/smp.c b/kernel/smp.c
index 27dc31a146a3..f104c8e83fc4 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -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);
-- 
2.39.5
Re: [PATCH] smp: Evaluate local cond_func() before IPI side-effects
Posted by Dave Hansen 1 year ago
On 12/3/24 10:39, Mathieu Desnoyers wrote:
> 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.

I always thought this was on purpose so cond_func() can be executed in
parallel with the remote work.

Could we double-check that this doesn't meaningfully slow down IPIs that
have longer work to do?
Re: [PATCH] smp: Evaluate local cond_func() before IPI side-effects
Posted by Mathieu Desnoyers 1 year ago
On 2024-12-03 20:38, Dave Hansen wrote:
> On 12/3/24 10:39, Mathieu Desnoyers wrote:
>> 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.
> 
> I always thought this was on purpose so cond_func() can be executed in
> parallel with the remote work.
> 
> Could we double-check that this doesn't meaningfully slow down IPIs that
> have longer work to do?

I notice that this question was not answered. I did do extensive
benchmark of this effect, but I would not expect a significant
impact there, because the cond_func() I've seen (there are very
few users) are all really short, and should be much shorter than
doing the IPI, so I expect a negligible performance overhead.

But we'll see if any bot observe something unexpected.

Caller code:

fs/buffer.c
1530:	on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1);

#define BH_LRU_SIZE     16

bool has_bh_in_lru(int cpu, void *dummy)
{
         struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
         int i;
         
         for (i = 0; i < BH_LRU_SIZE; i++) {
                 if (b->bhs[i])
                         return true;
         }

         return false;
}

arch/x86/mm/tlb.c
932:		on_each_cpu_cond_mask(tlb_is_not_lazy, flush_tlb_func,

^ this is the small function introduced by Rik's patches.

Thanks,

Mathieu


-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com