Skip to content

Commit 547305a

Browse files
rostedtPeter Zijlstra
authored andcommitted
tracepoint: Fix out of sync data passing by static caller
Naresh reported a bug that appears to be a side effect of the static calls. It happens when going from more than one tracepoint callback to a single one, and removing the first callback on the list. The list of tracepoint callbacks holds data and a function to call with the parameters of that tracepoint and a handler to the associated data. old_list: 0: func = foo; data = NULL; 1: func = bar; data = &bar_struct; new_list: 0: func = bar; data = &bar_struct; CPU 0 CPU 1 ----- ----- tp_funcs = old_list; tp_static_caller = tp_interator __DO_TRACE() data = tp_funcs[0].data = NULL; tp_funcs = new_list; tracepoint_update_call() tp_static_caller = tp_funcs[0] = bar; tp_static_caller(data) bar(data) x = data->item = NULL->item BOOM! To solve this, add a tracepoint_synchronize_unregister() between changing tp_funcs and updating the static tracepoint, that does both a synchronize_rcu() and synchronize_srcu(). This will ensure that when the static call is updated to the single callback that it will be receiving the data that it registered with. Fixes: d25e37d ("tracepoint: Optimize using static_call()") Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/linux-next/CA+G9fYvPXVRO0NV7yL=FxCmFEMYkCwdz7R=9W+_votpT824YJA@mail.gmail.com
1 parent de394e7 commit 547305a

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

kernel/tracepoint.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,25 @@ static void *func_remove(struct tracepoint_func **funcs,
221221
return old;
222222
}
223223

224-
static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs)
224+
static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs, bool sync)
225225
{
226226
void *func = tp->iterator;
227227

228228
/* Synthetic events do not have static call sites */
229229
if (!tp->static_call_key)
230230
return;
231231

232-
if (!tp_funcs[1].func)
232+
if (!tp_funcs[1].func) {
233233
func = tp_funcs[0].func;
234+
/*
235+
* If going from the iterator back to a single caller,
236+
* we need to synchronize with __DO_TRACE to make sure
237+
* that the data passed to the callback is the one that
238+
* belongs to that callback.
239+
*/
240+
if (sync)
241+
tracepoint_synchronize_unregister();
242+
}
234243

235244
__static_call_update(tp->static_call_key, tp->static_call_tramp, func);
236245
}
@@ -265,7 +274,7 @@ static int tracepoint_add_func(struct tracepoint *tp,
265274
* include/linux/tracepoint.h using rcu_dereference_sched().
266275
*/
267276
rcu_assign_pointer(tp->funcs, tp_funcs);
268-
tracepoint_update_call(tp, tp_funcs);
277+
tracepoint_update_call(tp, tp_funcs, false);
269278
static_key_enable(&tp->key);
270279

271280
release_probes(old);
@@ -297,11 +306,12 @@ static int tracepoint_remove_func(struct tracepoint *tp,
297306
tp->unregfunc();
298307

299308
static_key_disable(&tp->key);
309+
rcu_assign_pointer(tp->funcs, tp_funcs);
300310
} else {
301-
tracepoint_update_call(tp, tp_funcs);
311+
rcu_assign_pointer(tp->funcs, tp_funcs);
312+
tracepoint_update_call(tp, tp_funcs,
313+
tp_funcs[0].func != old[0].func);
302314
}
303-
304-
rcu_assign_pointer(tp->funcs, tp_funcs);
305315
release_probes(old);
306316
return 0;
307317
}

0 commit comments

Comments
 (0)