[PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event

Levi Yun posted 1 patch 2 months, 2 weeks ago
There is a newer version of this series
kernel/trace/trace_event_perf.c | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event
Posted by Levi Yun 2 months, 2 weeks ago
When a tracepoint event is created with attr.freq = 1,
'hwc->period_left' is not initialized correctly. As a result,
in the perf_swevent_overflow() function, when the first time the event occurs,
it calculates the event overflow and the perf_swevent_set_period() returns 3,
this leads to the event are recorded for three duplicate times.

Step to reproduce:
    1. Enable the tracepoint event & starting tracing
         $ echo 1 > /sys/kernel/tracing/events/module/module_free
         $ echo 1 > /sys/kernel/tracing/tracing_on

    2. Record with perf
         $ perf record -a --strict-freq -F 1 -e "module:module_free"

    3. Trigger module_free event.
         $ modprobe -i sunrpc
         $ modprobe -r sunrpc

Result:
     - Trace pipe result:
         $ cat trace_pipe
         modprobe-174509  [003] .....  6504.868896: module_free: sunrpc

     - perf sample:
         modprobe  174509 [003]  6504.868980: module:module_free: sunrpc
         modprobe  174509 [003]  6504.868980: module:module_free: sunrpc
         modprobe  174509 [003]  6504.868980: module:module_free: sunrpc

By setting period_left via perf_swevent_set_period() as other sw_event did,
This problem could be solved.

After patch:
     - Trace pipe result:
         $ cat trace_pipe
         modprobe 1153096 [068] 613468.867774: module:module_free: xfs

     - perf sample
         modprobe 1153096 [068] 613468.867794: module:module_free: xfs

Fixes: bd2b5b12849a ("perf_counter: More aggressive frequency adjustment")
Signed-off-by: Levi Yun <yeoreum.yun@arm.com>
---
 kernel/trace/trace_event_perf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
index 05e791241812..9050c631fe06 100644
--- a/kernel/trace/trace_event_perf.c
+++ b/kernel/trace/trace_event_perf.c
@@ -352,10 +352,16 @@ void perf_uprobe_destroy(struct perf_event *p_event)
 int perf_trace_add(struct perf_event *p_event, int flags)
 {
 	struct trace_event_call *tp_event = p_event->tp_event;
+	struct hw_perf_event *hwc = &p_event->hw;

 	if (!(flags & PERF_EF_START))
 		p_event->hw.state = PERF_HES_STOPPED;

+	if (is_sampling_event(event)) {
+		hwc->last_period = hwc->sample_period;
+		perf_swevent_set_period(p_event);
+	}
+
 	/*
 	 * If TRACE_REG_PERF_ADD returns false; no custom action was performed
 	 * and we need to take the default action of enqueueing our event on
--
LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}
Re: [PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event
Posted by kernel test robot 2 months, 2 weeks ago
Hi Levi,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.11-rc7 next-20240912]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Levi-Yun/trace-trace_event_perf-remove-duplicate-samples-on-the-first-tracepoint-event/20240911-202917
base:   linus/master
patch link:    https://lore.kernel.org/r/20240911122747.4168556-1-yeoreum.yun%40arm.com
patch subject: [PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event
config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20240913/202409130445.H7LIIGpd-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240913/202409130445.H7LIIGpd-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409130445.H7LIIGpd-lkp@intel.com/

All errors (new ones prefixed by >>):

>> kernel/trace/trace_event_perf.c:360:24: error: use of undeclared identifier 'event'
     360 |         if (is_sampling_event(event)) {
         |                               ^
   1 error generated.


vim +/event +360 kernel/trace/trace_event_perf.c

   351	
   352	int perf_trace_add(struct perf_event *p_event, int flags)
   353	{
   354		struct trace_event_call *tp_event = p_event->tp_event;
   355		struct hw_perf_event *hwc = &p_event->hw;
   356	
   357		if (!(flags & PERF_EF_START))
   358			p_event->hw.state = PERF_HES_STOPPED;
   359	
 > 360		if (is_sampling_event(event)) {
   361			hwc->last_period = hwc->sample_period;
   362			perf_swevent_set_period(p_event);
   363		}
   364	
   365		/*
   366		 * If TRACE_REG_PERF_ADD returns false; no custom action was performed
   367		 * and we need to take the default action of enqueueing our event on
   368		 * the right per-cpu hlist.
   369		 */
   370		if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event)) {
   371			struct hlist_head __percpu *pcpu_list;
   372			struct hlist_head *list;
   373	
   374			pcpu_list = tp_event->perf_events;
   375			if (WARN_ON_ONCE(!pcpu_list))
   376				return -EINVAL;
   377	
   378			list = this_cpu_ptr(pcpu_list);
   379			hlist_add_head_rcu(&p_event->hlist_entry, list);
   380		}
   381	
   382		return 0;
   383	}
   384	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event
Posted by kernel test robot 2 months, 2 weeks ago
Hi Levi,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.11-rc7 next-20240912]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Levi-Yun/trace-trace_event_perf-remove-duplicate-samples-on-the-first-tracepoint-event/20240911-202917
base:   linus/master
patch link:    https://lore.kernel.org/r/20240911122747.4168556-1-yeoreum.yun%40arm.com
patch subject: [PATCH] trace/trace_event_perf: remove duplicate samples on the first tracepoint event
config: sparc64-defconfig (https://download.01.org/0day-ci/archive/20240912/202409122013.3PMFMv8D-lkp@intel.com/config)
compiler: sparc64-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240912/202409122013.3PMFMv8D-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409122013.3PMFMv8D-lkp@intel.com/

All errors (new ones prefixed by >>):

   kernel/trace/trace_event_perf.c: In function 'perf_trace_add':
>> kernel/trace/trace_event_perf.c:360:31: error: 'event' undeclared (first use in this function); did you mean 'p_event'?
     360 |         if (is_sampling_event(event)) {
         |                               ^~~~~
         |                               p_event
   kernel/trace/trace_event_perf.c:360:31: note: each undeclared identifier is reported only once for each function it appears in


vim +360 kernel/trace/trace_event_perf.c

   351	
   352	int perf_trace_add(struct perf_event *p_event, int flags)
   353	{
   354		struct trace_event_call *tp_event = p_event->tp_event;
   355		struct hw_perf_event *hwc = &p_event->hw;
   356	
   357		if (!(flags & PERF_EF_START))
   358			p_event->hw.state = PERF_HES_STOPPED;
   359	
 > 360		if (is_sampling_event(event)) {
   361			hwc->last_period = hwc->sample_period;
   362			perf_swevent_set_period(p_event);
   363		}
   364	
   365		/*
   366		 * If TRACE_REG_PERF_ADD returns false; no custom action was performed
   367		 * and we need to take the default action of enqueueing our event on
   368		 * the right per-cpu hlist.
   369		 */
   370		if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event)) {
   371			struct hlist_head __percpu *pcpu_list;
   372			struct hlist_head *list;
   373	
   374			pcpu_list = tp_event->perf_events;
   375			if (WARN_ON_ONCE(!pcpu_list))
   376				return -EINVAL;
   377	
   378			list = this_cpu_ptr(pcpu_list);
   379			hlist_add_head_rcu(&p_event->hlist_entry, list);
   380		}
   381	
   382		return 0;
   383	}
   384	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki