When use perf to trace the sched_wakeup and sched_wakeup_new tracepoint,
there is a bug that output the same event repetitiously.
It can be reproduced by :

        perf record -e sched:sched_wakeup_new ./bug_fork

bug_fork is an demo that can generating wakeup_new events :

        the parent process does nothing but
        fork a child process, and then they both quit.

perf script :

        bug_fork  1078 [002]   184.669341: sched:sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000
        bug_fork  1078 [002]   184.670128: sched:sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000
        bug_fork  1078 [002]   184.670128: sched:sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000
        bug_fork  1078 [002]   184.670128: sched:sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000
        bug_fork  1078 [002]   184.670128: sched:sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000

but ftrace only show one event:

        bug_fork-1078  [002] d...   184.889159: sched_wakeup_new:
comm=bug_fork pid=1079 prio=120 target_cpu=000

perf script repeat prints wakeup_new events multiple times.

These events which trigger this issue not only monitor the current task,
but also specify a target task. For example, the sched_wakeup and
sched_wakeup_new tracepoint will be caught when the current task
wakeup the target task which we traced on.

commit e6dab5ffab59 ("perf/trace: Add ability to set a target task
for events") has designed a method to trace these events which
specify a target task. But there have tow issues when monitoring
multithreaded/multiprocess apps.

First, it match an event(such as wakeup/wakeup_new/stat_*) at the begin,
but the function doesn't return, the event will be matched again because
of task != current.

Second, due to these events are registered/mmaped at per-cpu
or per-thread(--per-thread), so perf_swevent_event will match
these events multiple times in the branch(task != current),
the number of repetitions is just the number of CPUs or threads.

perf_tp_event will only match an event event at a time,
so we will return after an event matched.

Signed-off-by: Cheng Jian <cj.chengj...@huawei.com>
---
 kernel/events/core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index baa134c..5682ead 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7988,12 +7988,16 @@ void perf_tp_event(u16 event_type, u64 count, void 
*record, int entry_size,
 
        /* Use the given event instead of the hlist */
        if (event) {
-               if (perf_tp_event_match(event, &data, regs))
+               if (perf_tp_event_match(event, &data, regs)) {
                        perf_swevent_event(event, count, &data, regs);
+                       goto out;
+               }
        } else {
                hlist_for_each_entry_rcu(event, head, hlist_entry) {
-                       if (perf_tp_event_match(event, &data, regs))
+                       if (perf_tp_event_match(event, &data, regs)) {
                                perf_swevent_event(event, count, &data, regs);
+                               goto out;
+                       }
                }
        }
 
@@ -8015,13 +8019,15 @@ void perf_tp_event(u16 event_type, u64 count, void 
*record, int entry_size,
                                continue;
                        if (event->attr.config != entry->type)
                                continue;
-                       if (perf_tp_event_match(event, &data, regs))
+                       if (perf_tp_event_match(event, &data, regs)) {
                                perf_swevent_event(event, count, &data, regs);
+                               break;
+                       }
                }
 unlock:
                rcu_read_unlock();
        }
-
+out:
        perf_swevent_put_recursion_context(rctx);
 }
 EXPORT_SYMBOL_GPL(perf_tp_event);
-- 
1.8.3.1

Reply via email to