Skip to content

Commit b5f935e

Browse files
mhiramatrostedt
authored andcommitted
tracing/probe: Add trace_event_file access APIs for trace_probe
Add trace_event_file access APIs for trace_probe data structure. This simplifies enabling/disabling operations in uprobe and kprobe events so that those don't touch deep inside the trace_probe. This also removing a redundant synchronization when the kprobe event is used from perf, since the perf itself uses tracepoint_synchronize_unregister() after disabling (ftrace- defined) event, thus we don't have to synchronize in that path. Also we don't need to identify local trace_kprobe too anymore. Link: http://lkml.kernel.org/r/155931584587.28323.372301976283354629.stgit@devnote2 Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 46e5376 commit b5f935e

4 files changed

Lines changed: 109 additions & 101 deletions

File tree

‎kernel/trace/trace_kprobe.c‎

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -290,34 +290,27 @@ static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
290290
static int
291291
enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
292292
{
293-
struct event_file_link *link;
293+
bool enabled = trace_probe_is_enabled(&tk->tp);
294294
int ret = 0;
295295

296296
if (file) {
297-
link = kmalloc(sizeof(*link), GFP_KERNEL);
298-
if (!link) {
299-
ret = -ENOMEM;
300-
goto out;
301-
}
302-
303-
link->file = file;
304-
list_add_tail_rcu(&link->list, &tk->tp.files);
297+
ret = trace_probe_add_file(&tk->tp, file);
298+
if (ret)
299+
return ret;
300+
} else
301+
tk->tp.flags |= TP_FLAG_PROFILE;
305302

306-
tk->tp.flags |= TP_FLAG_TRACE;
307-
ret = __enable_trace_kprobe(tk);
308-
if (ret) {
309-
list_del_rcu(&link->list);
310-
kfree(link);
311-
tk->tp.flags &= ~TP_FLAG_TRACE;
312-
}
303+
if (enabled)
304+
return 0;
313305

314-
} else {
315-
tk->tp.flags |= TP_FLAG_PROFILE;
316-
ret = __enable_trace_kprobe(tk);
317-
if (ret)
306+
ret = __enable_trace_kprobe(tk);
307+
if (ret) {
308+
if (file)
309+
trace_probe_remove_file(&tk->tp, file);
310+
else
318311
tk->tp.flags &= ~TP_FLAG_PROFILE;
319312
}
320-
out:
313+
321314
return ret;
322315
}
323316

@@ -328,54 +321,34 @@ enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
328321
static int
329322
disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
330323
{
331-
struct event_file_link *link = NULL;
332-
int wait = 0;
324+
struct trace_probe *tp = &tk->tp;
333325
int ret = 0;
334326

335327
if (file) {
336-
link = find_event_file_link(&tk->tp, file);
337-
if (!link) {
338-
ret = -EINVAL;
339-
goto out;
340-
}
341-
342-
list_del_rcu(&link->list);
343-
wait = 1;
344-
if (!list_empty(&tk->tp.files))
328+
if (!trace_probe_get_file_link(tp, file))
329+
return -ENOENT;
330+
if (!trace_probe_has_single_file(tp))
345331
goto out;
346-
347-
tk->tp.flags &= ~TP_FLAG_TRACE;
332+
tp->flags &= ~TP_FLAG_TRACE;
348333
} else
349-
tk->tp.flags &= ~TP_FLAG_PROFILE;
334+
tp->flags &= ~TP_FLAG_PROFILE;
350335

351-
if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
336+
if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
352337
if (trace_kprobe_is_return(tk))
353338
disable_kretprobe(&tk->rp);
354339
else
355340
disable_kprobe(&tk->rp.kp);
356-
wait = 1;
357341
}
358342

359-
/*
360-
* if tk is not added to any list, it must be a local trace_kprobe
361-
* created with perf_event_open. We don't need to wait for these
362-
* trace_kprobes
363-
*/
364-
if (list_empty(&tk->devent.list))
365-
wait = 0;
366343
out:
367-
if (wait) {
344+
if (file)
368345
/*
369-
* Synchronize with kprobe_trace_func/kretprobe_trace_func
370-
* to ensure disabled (all running handlers are finished).
371-
* This is not only for kfree(), but also the caller,
372-
* trace_remove_event_call() supposes it for releasing
373-
* event_call related objects, which will be accessed in
374-
* the kprobe_trace_func/kretprobe_trace_func.
346+
* Synchronization is done in below function. For perf event,
347+
* file == NULL and perf_trace_event_unreg() calls
348+
* tracepoint_synchronize_unregister() to ensure synchronize
349+
* event. We don't need to care about it.
375350
*/
376-
synchronize_rcu();
377-
kfree(link); /* Ignored if link == NULL */
378-
}
351+
trace_probe_remove_file(tp, file);
379352

380353
return ret;
381354
}
@@ -1044,7 +1017,7 @@ kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
10441017
{
10451018
struct event_file_link *link;
10461019

1047-
list_for_each_entry_rcu(link, &tk->tp.files, list)
1020+
trace_probe_for_each_link_rcu(link, &tk->tp)
10481021
__kprobe_trace_func(tk, regs, link->file);
10491022
}
10501023
NOKPROBE_SYMBOL(kprobe_trace_func);
@@ -1094,7 +1067,7 @@ kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
10941067
{
10951068
struct event_file_link *link;
10961069

1097-
list_for_each_entry_rcu(link, &tk->tp.files, list)
1070+
trace_probe_for_each_link_rcu(link, &tk->tp)
10981071
__kretprobe_trace_func(tk, ri, regs, link->file);
10991072
}
11001073
NOKPROBE_SYMBOL(kretprobe_trace_func);

‎kernel/trace/trace_probe.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,50 @@ int trace_probe_register_event_call(struct trace_probe *tp)
936936

937937
return ret;
938938
}
939+
940+
int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
941+
{
942+
struct event_file_link *link;
943+
944+
link = kmalloc(sizeof(*link), GFP_KERNEL);
945+
if (!link)
946+
return -ENOMEM;
947+
948+
link->file = file;
949+
INIT_LIST_HEAD(&link->list);
950+
list_add_tail_rcu(&link->list, &tp->files);
951+
tp->flags |= TP_FLAG_TRACE;
952+
return 0;
953+
}
954+
955+
struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
956+
struct trace_event_file *file)
957+
{
958+
struct event_file_link *link;
959+
960+
trace_probe_for_each_link(link, tp) {
961+
if (link->file == file)
962+
return link;
963+
}
964+
965+
return NULL;
966+
}
967+
968+
int trace_probe_remove_file(struct trace_probe *tp,
969+
struct trace_event_file *file)
970+
{
971+
struct event_file_link *link;
972+
973+
link = trace_probe_get_file_link(tp, file);
974+
if (!link)
975+
return -ENOENT;
976+
977+
list_del_rcu(&link->list);
978+
synchronize_rcu();
979+
kfree(link);
980+
981+
if (list_empty(&tp->files))
982+
tp->flags &= ~TP_FLAG_TRACE;
983+
984+
return 0;
985+
}

‎kernel/trace/trace_probe.h‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,32 @@ static inline bool trace_probe_is_registered(struct trace_probe *tp)
248248
return !!(tp->flags & TP_FLAG_REGISTERED);
249249
}
250250

251-
int trace_probe_init(struct trace_probe *tp, const char *event,
252-
const char *group);
253-
void trace_probe_cleanup(struct trace_probe *tp);
254-
int trace_probe_register_event_call(struct trace_probe *tp);
255251
static inline int trace_probe_unregister_event_call(struct trace_probe *tp)
256252
{
257253
/* tp->event is unregistered in trace_remove_event_call() */
258254
return trace_remove_event_call(&tp->call);
259255
}
260256

257+
static inline bool trace_probe_has_single_file(struct trace_probe *tp)
258+
{
259+
return !!list_is_singular(&tp->files);
260+
}
261+
262+
int trace_probe_init(struct trace_probe *tp, const char *event,
263+
const char *group);
264+
void trace_probe_cleanup(struct trace_probe *tp);
265+
int trace_probe_register_event_call(struct trace_probe *tp);
266+
int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file);
267+
int trace_probe_remove_file(struct trace_probe *tp,
268+
struct trace_event_file *file);
269+
struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
270+
struct trace_event_file *file);
271+
272+
#define trace_probe_for_each_link(pos, tp) \
273+
list_for_each_entry(pos, &(tp)->files, list)
274+
#define trace_probe_for_each_link_rcu(pos, tp) \
275+
list_for_each_entry_rcu(pos, &(tp)->files, list)
276+
261277
/* Check the name is good for event/group/fields */
262278
static inline bool is_good_name(const char *name)
263279
{
@@ -270,18 +286,6 @@ static inline bool is_good_name(const char *name)
270286
return true;
271287
}
272288

273-
static inline struct event_file_link *
274-
find_event_file_link(struct trace_probe *tp, struct trace_event_file *file)
275-
{
276-
struct event_file_link *link;
277-
278-
list_for_each_entry(link, &tp->files, list)
279-
if (link->file == file)
280-
return link;
281-
282-
return NULL;
283-
}
284-
285289
#define TPARG_FL_RETURN BIT(0)
286290
#define TPARG_FL_KERNEL BIT(1)
287291
#define TPARG_FL_FENTRY BIT(2)

‎kernel/trace/trace_uprobe.c‎

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
863863
return 0;
864864

865865
rcu_read_lock();
866-
list_for_each_entry_rcu(link, &tu->tp.files, list)
866+
trace_probe_for_each_link_rcu(link, &tu->tp)
867867
__uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
868868
rcu_read_unlock();
869869

@@ -877,7 +877,7 @@ static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
877877
struct event_file_link *link;
878878

879879
rcu_read_lock();
880-
list_for_each_entry_rcu(link, &tu->tp.files, list)
880+
trace_probe_for_each_link_rcu(link, &tu->tp)
881881
__uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
882882
rcu_read_unlock();
883883
}
@@ -924,21 +924,15 @@ probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
924924
filter_func_t filter)
925925
{
926926
bool enabled = trace_probe_is_enabled(&tu->tp);
927-
struct event_file_link *link = NULL;
928927
int ret;
929928

930929
if (file) {
931930
if (tu->tp.flags & TP_FLAG_PROFILE)
932931
return -EINTR;
933932

934-
link = kmalloc(sizeof(*link), GFP_KERNEL);
935-
if (!link)
936-
return -ENOMEM;
937-
938-
link->file = file;
939-
list_add_tail_rcu(&link->list, &tu->tp.files);
940-
941-
tu->tp.flags |= TP_FLAG_TRACE;
933+
ret = trace_probe_add_file(&tu->tp, file);
934+
if (ret < 0)
935+
return ret;
942936
} else {
943937
if (tu->tp.flags & TP_FLAG_TRACE)
944938
return -EINTR;
@@ -973,13 +967,11 @@ probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
973967
uprobe_buffer_disable();
974968

975969
err_flags:
976-
if (file) {
977-
list_del(&link->list);
978-
kfree(link);
979-
tu->tp.flags &= ~TP_FLAG_TRACE;
980-
} else {
970+
if (file)
971+
trace_probe_remove_file(&tu->tp, file);
972+
else
981973
tu->tp.flags &= ~TP_FLAG_PROFILE;
982-
}
974+
983975
return ret;
984976
}
985977

@@ -990,26 +982,18 @@ probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
990982
return;
991983

992984
if (file) {
993-
struct event_file_link *link;
994-
995-
link = find_event_file_link(&tu->tp, file);
996-
if (!link)
985+
if (trace_probe_remove_file(&tu->tp, file) < 0)
997986
return;
998987

999-
list_del_rcu(&link->list);
1000-
/* synchronize with u{,ret}probe_trace_func */
1001-
synchronize_rcu();
1002-
kfree(link);
1003-
1004-
if (!list_empty(&tu->tp.files))
988+
if (trace_probe_is_enabled(&tu->tp))
1005989
return;
1006-
}
990+
} else
991+
tu->tp.flags &= ~TP_FLAG_PROFILE;
1007992

1008993
WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1009994

1010995
uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
1011996
tu->inode = NULL;
1012-
tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
1013997

1014998
uprobe_buffer_disable();
1015999
}

0 commit comments

Comments
 (0)