[PATCH v2 RESEND 3/4] coresight: etm4x: Don't trace PID for non-root PID namespace

From: Leo Yan
Date: Fri Feb 04 2022 - 10:24:38 EST


When runs in perf mode, the driver always enables the PID tracing. This
can lead confusion when the profiling session runs in non-root PID
namespace, whereas it records the PIDs from the root PID namespace.

To avoid confusion for PID tracing, when runs in perf mode, this patch
changes to only enable PID tracing for root PID namespace.

As result, after apply this patch, the perf tool reports PID as '-1' for
all samples:

# unshare --fork --pid perf record -e cs_etm// -m 64K,64K -a \
-o perf_test.data -- uname
# perf report -i perf_test.data --itrace=Zi1000i --stdio

# Total Lost Samples: 0
#
# Samples: 94 of event 'instructions'
# Event count (approx.): 94000
#
# Overhead Command Shared Object Symbol
# ........ ....... ................. ..............................
#
68.09% :-1 [kernel.kallsyms] [k] __sched_text_end
3.19% :-1 [kernel.kallsyms] [k] hrtimer_interrupt
2.13% :-1 [kernel.kallsyms] [k] __bitmap_and
2.13% :-1 [kernel.kallsyms] [k] trace_vbprintk
1.06% :-1 [kernel.kallsyms] [k] __fget_files
1.06% :-1 [kernel.kallsyms] [k] __schedule
1.06% :-1 [kernel.kallsyms] [k] __softirqentry_text_start
1.06% :-1 [kernel.kallsyms] [k] __update_load_avg_cfs_rq
1.06% :-1 [kernel.kallsyms] [k] __update_load_avg_se
1.06% :-1 [kernel.kallsyms] [k] arch_counter_get_cntpct
1.06% :-1 [kernel.kallsyms] [k] check_and_switch_context
1.06% :-1 [kernel.kallsyms] [k] format_decode
1.06% :-1 [kernel.kallsyms] [k] handle_percpu_devid_irq
1.06% :-1 [kernel.kallsyms] [k] irq_enter_rcu
1.06% :-1 [kernel.kallsyms] [k] irqtime_account_irq
1.06% :-1 [kernel.kallsyms] [k] ktime_get
1.06% :-1 [kernel.kallsyms] [k] ktime_get_coarse_real_ts64
1.06% :-1 [kernel.kallsyms] [k] memmove
1.06% :-1 [kernel.kallsyms] [k] perf_ioctl
1.06% :-1 [kernel.kallsyms] [k] perf_output_begin
1.06% :-1 [kernel.kallsyms] [k] perf_output_copy
1.06% :-1 [kernel.kallsyms] [k] profile_tick
1.06% :-1 [kernel.kallsyms] [k] sched_clock
1.06% :-1 [kernel.kallsyms] [k] timerqueue_add
1.06% :-1 [kernel.kallsyms] [k] trace_save_cmdline
1.06% :-1 [kernel.kallsyms] [k] update_load_avg
1.06% :-1 [kernel.kallsyms] [k] vbin_printf

Signed-off-by: Leo Yan <leo.yan@xxxxxxxxxx>
---
drivers/hwtracing/coresight/coresight-etm4x-core.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/hwtracing/coresight/coresight-etm4x-core.c
index bf18128cf5de..e7fc83a1e188 100644
--- a/drivers/hwtracing/coresight/coresight-etm4x-core.c
+++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c
@@ -656,7 +656,9 @@ static int etm4_parse_event_config(struct coresight_device *csdev,
config->cfg |= BIT(11);
}

- if (attr->config & BIT(ETM_OPT_CTXTID))
+ /* Only trace contextID when runs in root PID namespace */
+ if ((attr->config & BIT(ETM_OPT_CTXTID)) &&
+ task_is_in_init_pid_ns(current))
/* bit[6], Context ID tracing bit */
config->cfg |= BIT(ETM4_CFG_BIT_CTXTID);

@@ -670,7 +672,11 @@ static int etm4_parse_event_config(struct coresight_device *csdev,
ret = -EINVAL;
goto out;
}
- config->cfg |= BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT);
+
+ /* Only trace virtual contextID when runs in root PID namespace */
+ if (task_is_in_init_pid_ns(current))
+ config->cfg |= BIT(ETM4_CFG_BIT_VMID) |
+ BIT(ETM4_CFG_BIT_VMID_OPT);
}

/* return stack - enable if selected and supported */
--
2.25.1