Skip to content

Commit 68f4ff0

Browse files
valschneiderPeter Zijlstra
authored andcommitted
sched, smp: Trace smp callback causing an IPI
Context ======= The newly-introduced ipi_send_cpumask tracepoint has a "callback" parameter which so far has only been fed with NULL. While CSD_TYPE_SYNC/ASYNC and CSD_TYPE_IRQ_WORK share a similar backing struct layout (meaning their callback func can be accessed without caring about the actual CSD type), CSD_TYPE_TTWU doesn't even have a function attached to its struct. This means we need to check the type of a CSD before eventually dereferencing its associated callback. This isn't as trivial as it sounds: the CSD type is stored in __call_single_node.u_flags, which get cleared right before the callback is executed via csd_unlock(). This implies checking the CSD type before it is enqueued on the call_single_queue, as the target CPU's queue can be flushed before we get to sending an IPI. Furthermore, send_call_function_single_ipi() only has a CPU parameter, and would need to have an additional argument to trickle down the invoked function. This is somewhat silly, as the extra argument will always be pushed down to the function even when nothing is being traced, which is unnecessary overhead. Changes ======= send_call_function_single_ipi() is only used by smp.c, and is defined in sched/core.c as it contains scheduler-specific ops (set_nr_if_polling() of a CPU's idle task). Split it into two parts: the scheduler bits remain in sched/core.c, and the actual IPI emission is moved into smp.c. This lets us define an __always_inline helper function that can take the related callback as parameter without creating useless register pressure in the non-traced path which only gains a (disabled) static branch. Do the same thing for the multi IPI case. Signed-off-by: Valentin Schneider <vschneid@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20230307143558.294354-8-vschneid@redhat.com
1 parent 253a0fb commit 68f4ff0

3 files changed

Lines changed: 53 additions & 16 deletions

File tree

kernel/sched/core.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,16 +3829,20 @@ void sched_ttwu_pending(void *arg)
38293829
rq_unlock_irqrestore(rq, &rf);
38303830
}
38313831

3832-
void send_call_function_single_ipi(int cpu)
3832+
/*
3833+
* Prepare the scene for sending an IPI for a remote smp_call
3834+
*
3835+
* Returns true if the caller can proceed with sending the IPI.
3836+
* Returns false otherwise.
3837+
*/
3838+
bool call_function_single_prep_ipi(int cpu)
38333839
{
3834-
struct rq *rq = cpu_rq(cpu);
3835-
3836-
if (!set_nr_if_polling(rq->idle)) {
3837-
trace_ipi_send_cpumask(cpumask_of(cpu), _RET_IP_, NULL);
3838-
arch_send_call_function_single_ipi(cpu);
3839-
} else {
3840+
if (set_nr_if_polling(cpu_rq(cpu)->idle)) {
38403841
trace_sched_wake_idle_without_ipi(cpu);
3842+
return false;
38413843
}
3844+
3845+
return true;
38423846
}
38433847

38443848
/*

kernel/sched/smp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
extern void sched_ttwu_pending(void *arg);
88

9-
extern void send_call_function_single_ipi(int cpu);
9+
extern bool call_function_single_prep_ipi(int cpu);
1010

1111
#ifdef CONFIG_SMP
1212
extern void flush_smp_call_function_queue(void);

kernel/smp.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,18 @@ void __init call_function_init(void)
104104
}
105105

106106
static __always_inline void
107-
send_call_function_ipi_mask(struct cpumask *mask)
107+
send_call_function_single_ipi(int cpu, smp_call_func_t func)
108108
{
109-
trace_ipi_send_cpumask(mask, _RET_IP_, NULL);
109+
if (call_function_single_prep_ipi(cpu)) {
110+
trace_ipi_send_cpumask(cpumask_of(cpu), _RET_IP_, func);
111+
arch_send_call_function_single_ipi(cpu);
112+
}
113+
}
114+
115+
static __always_inline void
116+
send_call_function_ipi_mask(struct cpumask *mask, smp_call_func_t func)
117+
{
118+
trace_ipi_send_cpumask(mask, _RET_IP_, func);
110119
arch_send_call_function_ipi_mask(mask);
111120
}
112121

@@ -307,9 +316,8 @@ static __always_inline void csd_unlock(struct __call_single_data *csd)
307316
smp_store_release(&csd->node.u_flags, 0);
308317
}
309318

310-
static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
311-
312-
void __smp_call_single_queue(int cpu, struct llist_node *node)
319+
static __always_inline void
320+
raw_smp_call_single_queue(int cpu, struct llist_node *node, smp_call_func_t func)
313321
{
314322
/*
315323
* The list addition should be visible to the target CPU when it pops
@@ -324,7 +332,32 @@ void __smp_call_single_queue(int cpu, struct llist_node *node)
324332
* equipped to do the right thing...
325333
*/
326334
if (llist_add(node, &per_cpu(call_single_queue, cpu)))
327-
send_call_function_single_ipi(cpu);
335+
send_call_function_single_ipi(cpu, func);
336+
}
337+
338+
static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
339+
340+
void __smp_call_single_queue(int cpu, struct llist_node *node)
341+
{
342+
/*
343+
* We have to check the type of the CSD before queueing it, because
344+
* once queued it can have its flags cleared by
345+
* flush_smp_call_function_queue()
346+
* even if we haven't sent the smp_call IPI yet (e.g. the stopper
347+
* executes migration_cpu_stop() on the remote CPU).
348+
*/
349+
if (trace_ipi_send_cpumask_enabled()) {
350+
call_single_data_t *csd;
351+
smp_call_func_t func;
352+
353+
csd = container_of(node, call_single_data_t, node.llist);
354+
func = CSD_TYPE(csd) == CSD_TYPE_TTWU ?
355+
sched_ttwu_pending : csd->func;
356+
357+
raw_smp_call_single_queue(cpu, node, func);
358+
} else {
359+
raw_smp_call_single_queue(cpu, node, NULL);
360+
}
328361
}
329362

330363
/*
@@ -768,9 +801,9 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
768801
* provided mask.
769802
*/
770803
if (nr_cpus == 1)
771-
send_call_function_single_ipi(last_cpu);
804+
send_call_function_single_ipi(last_cpu, func);
772805
else if (likely(nr_cpus > 1))
773-
send_call_function_ipi_mask(cfd->cpumask_ipi);
806+
send_call_function_ipi_mask(cfd->cpumask_ipi, func);
774807
}
775808

776809
if (run_local && (!cond_func || cond_func(this_cpu, info))) {

0 commit comments

Comments
 (0)