Skip to content

Commit 7799ad4

Browse files
compudjgregkh
authored andcommitted
tracepoint: Fix static call function vs data state mismatch
commit 231264d upstream. On a 1->0->1 callbacks transition, there is an issue with the new callback using the old callback's data. Considering __DO_TRACE_CALL: do { \ struct tracepoint_func *it_func_ptr; \ void *__data; \ it_func_ptr = \ rcu_dereference_raw((&__tracepoint_##name)->funcs); \ if (it_func_ptr) { \ __data = (it_func_ptr)->data; \ ----> [ delayed here on one CPU (e.g. vcpu preempted by the host) ] static_call(tp_func_##name)(__data, args); \ } \ } while (0) It has loaded the tp->funcs of the old callback, so it will try to use the old data. This can be fixed by adding a RCU sync anywhere in the 1->0->1 transition chain. On a N->2->1 transition, we need an rcu-sync because you may have a sequence of 3->2->1 (or 1->2->1) where the element 0 data is unchanged between 2->1, but was changed from 3->2 (or from 1->2), which may be observed by the static call. This can be fixed by adding an unconditional RCU sync in transition 2->1. Note, this fixes a correctness issue at the cost of adding a tremendous performance regression to the disabling of tracepoints. Before this commit: # trace-cmd start -e all # time trace-cmd start -p nop real 0m0.778s user 0m0.000s sys 0m0.061s After this commit: # trace-cmd start -e all # time trace-cmd start -p nop real 0m10.593s user 0m0.017s sys 0m0.259s A follow up fix will introduce a more lightweight scheme based on RCU get_state and cond_sync, that will return the performance back to what it was. As both this change and the lightweight versions are complex on their own, for bisecting any issues that this may cause, they are kept as two separate changes. Link: https://lkml.kernel.org/r/20210805132717.23813-3-mathieu.desnoyers@efficios.com Link: https://lore.kernel.org/io-uring/4ebea8f0-58c9-e571-fd30-0ce4f6f09c70@samba.org/ Cc: stable@vger.kernel.org Cc: Ingo Molnar <mingo@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: "Paul E. McKenney" <paulmck@kernel.org> Cc: Stefan Metzmacher <metze@samba.org> Fixes: d25e37d ("tracepoint: Optimize using static_call()") Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 14673e1 commit 7799ad4

1 file changed

Lines changed: 82 additions & 20 deletions

File tree

‎kernel/tracepoint.c‎

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
#include <linux/sched/task.h>
1616
#include <linux/static_key.h>
1717

18+
enum tp_func_state {
19+
TP_FUNC_0,
20+
TP_FUNC_1,
21+
TP_FUNC_2,
22+
TP_FUNC_N,
23+
};
24+
1825
extern tracepoint_ptr_t __start___tracepoints_ptrs[];
1926
extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
2027

@@ -267,26 +274,29 @@ static void *func_remove(struct tracepoint_func **funcs,
267274
return old;
268275
}
269276

270-
static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs, bool sync)
277+
/*
278+
* Count the number of functions (enum tp_func_state) in a tp_funcs array.
279+
*/
280+
static enum tp_func_state nr_func_state(const struct tracepoint_func *tp_funcs)
281+
{
282+
if (!tp_funcs)
283+
return TP_FUNC_0;
284+
if (!tp_funcs[1].func)
285+
return TP_FUNC_1;
286+
if (!tp_funcs[2].func)
287+
return TP_FUNC_2;
288+
return TP_FUNC_N; /* 3 or more */
289+
}
290+
291+
static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs)
271292
{
272293
void *func = tp->iterator;
273294

274295
/* Synthetic events do not have static call sites */
275296
if (!tp->static_call_key)
276297
return;
277-
278-
if (!tp_funcs[1].func) {
298+
if (nr_func_state(tp_funcs) == TP_FUNC_1)
279299
func = tp_funcs[0].func;
280-
/*
281-
* If going from the iterator back to a single caller,
282-
* we need to synchronize with __DO_TRACE to make sure
283-
* that the data passed to the callback is the one that
284-
* belongs to that callback.
285-
*/
286-
if (sync)
287-
tracepoint_synchronize_unregister();
288-
}
289-
290300
__static_call_update(tp->static_call_key, tp->static_call_tramp, func);
291301
}
292302

@@ -320,9 +330,31 @@ static int tracepoint_add_func(struct tracepoint *tp,
320330
* a pointer to it. This array is referenced by __DO_TRACE from
321331
* include/linux/tracepoint.h using rcu_dereference_sched().
322332
*/
323-
tracepoint_update_call(tp, tp_funcs, false);
324-
rcu_assign_pointer(tp->funcs, tp_funcs);
325-
static_key_enable(&tp->key);
333+
switch (nr_func_state(tp_funcs)) {
334+
case TP_FUNC_1: /* 0->1 */
335+
/* Set static call to first function */
336+
tracepoint_update_call(tp, tp_funcs);
337+
/* Both iterator and static call handle NULL tp->funcs */
338+
rcu_assign_pointer(tp->funcs, tp_funcs);
339+
static_key_enable(&tp->key);
340+
break;
341+
case TP_FUNC_2: /* 1->2 */
342+
/* Set iterator static call */
343+
tracepoint_update_call(tp, tp_funcs);
344+
/*
345+
* Iterator callback installed before updating tp->funcs.
346+
* Requires ordering between RCU assign/dereference and
347+
* static call update/call.
348+
*/
349+
rcu_assign_pointer(tp->funcs, tp_funcs);
350+
break;
351+
case TP_FUNC_N: /* N->N+1 (N>1) */
352+
rcu_assign_pointer(tp->funcs, tp_funcs);
353+
break;
354+
default:
355+
WARN_ON_ONCE(1);
356+
break;
357+
}
326358

327359
release_probes(old);
328360
return 0;
@@ -349,17 +381,47 @@ static int tracepoint_remove_func(struct tracepoint *tp,
349381
/* Failed allocating new tp_funcs, replaced func with stub */
350382
return 0;
351383

352-
if (!tp_funcs) {
384+
switch (nr_func_state(tp_funcs)) {
385+
case TP_FUNC_0: /* 1->0 */
353386
/* Removed last function */
354387
if (tp->unregfunc && static_key_enabled(&tp->key))
355388
tp->unregfunc();
356389

357390
static_key_disable(&tp->key);
391+
/* Set iterator static call */
392+
tracepoint_update_call(tp, tp_funcs);
393+
/* Both iterator and static call handle NULL tp->funcs */
394+
rcu_assign_pointer(tp->funcs, NULL);
395+
/*
396+
* Make sure new func never uses old data after a 1->0->1
397+
* transition sequence.
398+
* Considering that transition 0->1 is the common case
399+
* and don't have rcu-sync, issue rcu-sync after
400+
* transition 1->0 to break that sequence by waiting for
401+
* readers to be quiescent.
402+
*/
403+
tracepoint_synchronize_unregister();
404+
break;
405+
case TP_FUNC_1: /* 2->1 */
358406
rcu_assign_pointer(tp->funcs, tp_funcs);
359-
} else {
407+
/*
408+
* On 2->1 transition, RCU sync is needed before setting
409+
* static call to first callback, because the observer
410+
* may have loaded any prior tp->funcs after the last one
411+
* associated with an rcu-sync.
412+
*/
413+
tracepoint_synchronize_unregister();
414+
/* Set static call to first function */
415+
tracepoint_update_call(tp, tp_funcs);
416+
break;
417+
case TP_FUNC_2: /* N->N-1 (N>2) */
418+
fallthrough;
419+
case TP_FUNC_N:
360420
rcu_assign_pointer(tp->funcs, tp_funcs);
361-
tracepoint_update_call(tp, tp_funcs,
362-
tp_funcs[0].data != old[0].data);
421+
break;
422+
default:
423+
WARN_ON_ONCE(1);
424+
break;
363425
}
364426
release_probes(old);
365427
return 0;

0 commit comments

Comments
 (0)