2021-12-13 12:13:53

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 0/4] coresight: etm: Correct PID tracing for non-root namespace

If a profiling program runs in a non-root PID namespace, if CoreSight
driver enables PID tracing (with contextID), it can lead to mismatching
issue between the context ID traced in hardware (from the root
namespace) and the PIDs gathered by profiling tool (e.g. perf) in its
non-root namespace.

CoreSight driver has tried to address this issue for the contextID
related interfaces under sysfs, but it misses to prevent user to set
VMID (virtual contextID) for kernel runs in EL2 with VHE; furthermore,
it misses to handle the case when the profiling tool runs in the
non-root PID namespace.

For this reason, this patch series is to correct contextID tracing for
non-root namespace. After applied this patchset, patch 02 doesn't
permit users to access virtual contextID via sysfs nodes in the non-root
PID namespace, patch 03 and 04 stop to trace PID packet for non-root PID
namespace.

This patch is dependent on the patchset "pid: Introduce helper
task_is_in_root_ns()" [1].

[1] https://lore.kernel.org/lkml/[email protected]/


Leo Yan (4):
coresight: etm4x: Add lock for reading virtual context ID comparator
coresight: etm4x: Don't use virtual contextID for non-root PID
namespace
coresight: etm4x: Don't trace PID for non-root PID namespace
coresight: etm3x: Don't trace PID for non-root PID namespace

.../coresight/coresight-etm3x-core.c | 4 +++
.../coresight/coresight-etm4x-core.c | 10 +++++--
.../coresight/coresight-etm4x-sysfs.c | 30 +++++++++++++++++++
3 files changed, 42 insertions(+), 2 deletions(-)

--
2.25.1



2021-12-13 12:13:55

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 1/4] coresight: etm4x: Add lock for reading virtual context ID comparator

Updates to the values and the index are protected via the spinlock.
Ensure we use the same lock to read the value safely.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Suzuki K Poulose <[email protected]>
---
drivers/hwtracing/coresight/coresight-etm4x-sysfs.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c b/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
index 10ef2a29006e..2f3b4eef8261 100644
--- a/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
+++ b/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
@@ -2111,7 +2111,9 @@ static ssize_t vmid_val_show(struct device *dev,
struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
struct etmv4_config *config = &drvdata->config;

+ spin_lock(&drvdata->spinlock);
val = (unsigned long)config->vmid_val[config->vmid_idx];
+ spin_unlock(&drvdata->spinlock);
return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
}

--
2.25.1


2021-12-13 12:13:58

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 2/4] coresight: etm4x: Don't use virtual contextID for non-root PID namespace

As commented in the function ctxid_pid_store(), it can cause the PID
values mismatching between context ID tracing and PID allocated in a
non-root namespace.

For this reason, when a process runs in non-root PID namespace, the
driver doesn't allow PID tracing and returns failure when access
contextID related sysfs nodes.

VMID works for virtual contextID when the kernel runs in EL2 mode with
VHE; on the other hand, the driver doesn't prevent users from accessing
it when programs run in the non-root namespace. Thus this can lead
to same issues with contextID described above.

This patch imposes the checking on VMID related sysfs knobs and returns
failure if current process runs in non-root PID namespace.

Signed-off-by: Leo Yan <[email protected]>
---
.../coresight/coresight-etm4x-sysfs.c | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)

diff --git a/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c b/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
index 2f3b4eef8261..a00c0d1bbd85 100644
--- a/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
+++ b/drivers/hwtracing/coresight/coresight-etm4x-sysfs.c
@@ -2111,6 +2111,13 @@ static ssize_t vmid_val_show(struct device *dev,
struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
struct etmv4_config *config = &drvdata->config;

+ /*
+ * Don't use virtual contextID tracing if coming from a PID namespace.
+ * See comment in ctxid_pid_store().
+ */
+ if (!task_is_in_init_pid_ns(current))
+ return -EINVAL;
+
spin_lock(&drvdata->spinlock);
val = (unsigned long)config->vmid_val[config->vmid_idx];
spin_unlock(&drvdata->spinlock);
@@ -2125,6 +2132,13 @@ static ssize_t vmid_val_store(struct device *dev,
struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
struct etmv4_config *config = &drvdata->config;

+ /*
+ * Don't use virtual contextID tracing if coming from a PID namespace.
+ * See comment in ctxid_pid_store().
+ */
+ if (!task_is_in_init_pid_ns(current))
+ return -EINVAL;
+
/*
* only implemented when vmid tracing is enabled, i.e. at least one
* vmid comparator is implemented and at least 8 bit vmid size
@@ -2148,6 +2162,13 @@ static ssize_t vmid_masks_show(struct device *dev,
struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
struct etmv4_config *config = &drvdata->config;

+ /*
+ * Don't use virtual contextID tracing if coming from a PID namespace.
+ * See comment in ctxid_pid_store().
+ */
+ if (!task_is_in_init_pid_ns(current))
+ return -EINVAL;
+
spin_lock(&drvdata->spinlock);
val1 = config->vmid_mask0;
val2 = config->vmid_mask1;
@@ -2165,6 +2186,13 @@ static ssize_t vmid_masks_store(struct device *dev,
struct etmv4_config *config = &drvdata->config;
int nr_inputs;

+ /*
+ * Don't use virtual contextID tracing if coming from a PID namespace.
+ * See comment in ctxid_pid_store().
+ */
+ if (!task_is_in_init_pid_ns(current))
+ return -EINVAL;
+
/*
* only implemented when vmid tracing is enabled, i.e. at least one
* vmid comparator is implemented and at least 8 bit vmid size
--
2.25.1


2021-12-13 12:14:02

by Leo Yan

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

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 <[email protected]>
---
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 86a313857b58..f3eda536267c 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


2021-12-13 12:14:05

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 4/4] coresight: etm3x: Don't trace PID for non-root PID namespace

ETMv3 driver enables PID tracing by directly using perf config from
userspace, this means the tracer will capture PID packets from root
namespace but the profiling session runs in non-root PID namespace.
Finally, the recorded packets can mislead perf reporting with the
mismatched PID values.

This patch changes to only enable PID tracing for root PID namespace.
Note, the hardware supports VMID tracing from ETMv3.5, but the driver
never enables VMID trace, this patch doesn't handle VMID trace (bit 30
in ETMCR register) particularly.

Signed-off-by: Leo Yan <[email protected]>
---
drivers/hwtracing/coresight/coresight-etm3x-core.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/hwtracing/coresight/coresight-etm3x-core.c b/drivers/hwtracing/coresight/coresight-etm3x-core.c
index cf64ce73a741..7d413ba8b823 100644
--- a/drivers/hwtracing/coresight/coresight-etm3x-core.c
+++ b/drivers/hwtracing/coresight/coresight-etm3x-core.c
@@ -340,6 +340,10 @@ static int etm_parse_event_config(struct etm_drvdata *drvdata,

config->ctrl = attr->config;

+ /* Don't trace contextID when runs in non-root PID namespace */
+ if (!task_is_in_init_pid_ns(current))
+ config->ctrl &= ~ETMCR_CTXID_SIZE;
+
/*
* Possible to have cores with PTM (supports ret stack) and ETM
* (never has ret stack) on the same SoC. So if we have a request
--
2.25.1


2021-12-13 19:17:00

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] coresight: etm3x: Don't trace PID for non-root PID namespace

Hi Leo,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.16-rc5]
[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]

url: https://github.com/0day-ci/linux/commits/Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2585cf9dfaaddf00b069673f27bb3f8530e2039c
config: arm-buildonly-randconfig-r003-20211213 (https://download.01.org/0day-ci/archive/20211214/[email protected]/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project b6a2ddb6c8ac29412b1361810972e15221fa021c)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/81d5f47788c40d34c8159d64d4505eb485254e8f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
git checkout 81d5f47788c40d34c8159d64d4505eb485254e8f
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/hwtracing/coresight/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> drivers/hwtracing/coresight/coresight-etm3x-core.c:344:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
if (!task_is_in_init_pid_ns(current))
^
1 error generated.


vim +/task_is_in_init_pid_ns +344 drivers/hwtracing/coresight/coresight-etm3x-core.c

301
302 #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | \
303 ETMCR_TIMESTAMP_EN | \
304 ETMCR_RETURN_STACK)
305
306 static int etm_parse_event_config(struct etm_drvdata *drvdata,
307 struct perf_event *event)
308 {
309 struct etm_config *config = &drvdata->config;
310 struct perf_event_attr *attr = &event->attr;
311
312 if (!attr)
313 return -EINVAL;
314
315 /* Clear configuration from previous run */
316 memset(config, 0, sizeof(struct etm_config));
317
318 if (attr->exclude_kernel)
319 config->mode = ETM_MODE_EXCL_KERN;
320
321 if (attr->exclude_user)
322 config->mode = ETM_MODE_EXCL_USER;
323
324 /* Always start from the default config */
325 etm_set_default(config);
326
327 /*
328 * By default the tracers are configured to trace the whole address
329 * range. Narrow the field only if requested by user space.
330 */
331 if (config->mode)
332 etm_config_trace_mode(config);
333
334 /*
335 * At this time only cycle accurate, return stack and timestamp
336 * options are available.
337 */
338 if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
339 return -EINVAL;
340
341 config->ctrl = attr->config;
342
343 /* Don't trace contextID when runs in non-root PID namespace */
> 344 if (!task_is_in_init_pid_ns(current))
345 config->ctrl &= ~ETMCR_CTXID_SIZE;
346
347 /*
348 * Possible to have cores with PTM (supports ret stack) and ETM
349 * (never has ret stack) on the same SoC. So if we have a request
350 * for return stack that can't be honoured on this core then
351 * clear the bit - trace will still continue normally
352 */
353 if ((config->ctrl & ETMCR_RETURN_STACK) &&
354 !(drvdata->etmccer & ETMCCER_RETSTACK))
355 config->ctrl &= ~ETMCR_RETURN_STACK;
356
357 return 0;
358 }
359

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

2021-12-13 19:48:05

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 2/4] coresight: etm4x: Don't use virtual contextID for non-root PID namespace

Hi Leo,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.16-rc5]
[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]

url: https://github.com/0day-ci/linux/commits/Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2585cf9dfaaddf00b069673f27bb3f8530e2039c
config: arm64-randconfig-r034-20211213 (https://download.01.org/0day-ci/archive/20211214/[email protected]/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project b6a2ddb6c8ac29412b1361810972e15221fa021c)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/3b7eceb25155c98432dea4821c4fc571b44d72e3
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
git checkout 3b7eceb25155c98432dea4821c4fc571b44d72e3
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/hwtracing/coresight/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> drivers/hwtracing/coresight/coresight-etm4x-sysfs.c:2118:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
if (!task_is_in_init_pid_ns(current))
^
drivers/hwtracing/coresight/coresight-etm4x-sysfs.c:2139:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
if (!task_is_in_init_pid_ns(current))
^
drivers/hwtracing/coresight/coresight-etm4x-sysfs.c:2169:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
if (!task_is_in_init_pid_ns(current))
^
drivers/hwtracing/coresight/coresight-etm4x-sysfs.c:2193:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
if (!task_is_in_init_pid_ns(current))
^
4 errors generated.


vim +/task_is_in_init_pid_ns +2118 drivers/hwtracing/coresight/coresight-etm4x-sysfs.c

2105
2106 static ssize_t vmid_val_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2109 {
2110 unsigned long val;
2111 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
2112 struct etmv4_config *config = &drvdata->config;
2113
2114 /*
2115 * Don't use virtual contextID tracing if coming from a PID namespace.
2116 * See comment in ctxid_pid_store().
2117 */
> 2118 if (!task_is_in_init_pid_ns(current))
2119 return -EINVAL;
2120
2121 spin_lock(&drvdata->spinlock);
2122 val = (unsigned long)config->vmid_val[config->vmid_idx];
2123 spin_unlock(&drvdata->spinlock);
2124 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
2125 }
2126

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

2021-12-13 21:46:04

by kernel test robot

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

Hi Leo,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.16-rc5]
[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]

url: https://github.com/0day-ci/linux/commits/Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2585cf9dfaaddf00b069673f27bb3f8530e2039c
config: arm64-randconfig-r034-20211213 (https://download.01.org/0day-ci/archive/20211214/[email protected]/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project b6a2ddb6c8ac29412b1361810972e15221fa021c)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/dd716cd12b2f0e47fcc2b0e3e9172e4e70ad4877
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
git checkout dd716cd12b2f0e47fcc2b0e3e9172e4e70ad4877
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/hwtracing/coresight/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> drivers/hwtracing/coresight/coresight-etm4x-core.c:661:6: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
task_is_in_init_pid_ns(current))
^
1 error generated.


vim +/task_is_in_init_pid_ns +661 drivers/hwtracing/coresight/coresight-etm4x-core.c

606
607 static int etm4_parse_event_config(struct coresight_device *csdev,
608 struct perf_event *event)
609 {
610 int ret = 0;
611 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
612 struct etmv4_config *config = &drvdata->config;
613 struct perf_event_attr *attr = &event->attr;
614 unsigned long cfg_hash;
615 int preset;
616
617 /* Clear configuration from previous run */
618 memset(config, 0, sizeof(struct etmv4_config));
619
620 if (attr->exclude_kernel)
621 config->mode = ETM_MODE_EXCL_KERN;
622
623 if (attr->exclude_user)
624 config->mode = ETM_MODE_EXCL_USER;
625
626 /* Always start from the default config */
627 etm4_set_default_config(config);
628
629 /* Configure filters specified on the perf cmd line, if any. */
630 ret = etm4_set_event_filters(drvdata, event);
631 if (ret)
632 goto out;
633
634 /* Go from generic option to ETMv4 specifics */
635 if (attr->config & BIT(ETM_OPT_CYCACC)) {
636 config->cfg |= BIT(4);
637 /* TRM: Must program this for cycacc to work */
638 config->ccctlr = ETM_CYC_THRESHOLD_DEFAULT;
639 }
640 if (attr->config & BIT(ETM_OPT_TS)) {
641 /*
642 * Configure timestamps to be emitted at regular intervals in
643 * order to correlate instructions executed on different CPUs
644 * (CPU-wide trace scenarios).
645 */
646 ret = etm4_config_timestamp_event(drvdata);
647
648 /*
649 * No need to go further if timestamp intervals can't
650 * be configured.
651 */
652 if (ret)
653 goto out;
654
655 /* bit[11], Global timestamp tracing bit */
656 config->cfg |= BIT(11);
657 }
658
659 /* Only trace contextID when runs in root PID namespace */
660 if ((attr->config & BIT(ETM_OPT_CTXTID)) &&
> 661 task_is_in_init_pid_ns(current))
662 /* bit[6], Context ID tracing bit */
663 config->cfg |= BIT(ETM4_CFG_BIT_CTXTID);
664
665 /*
666 * If set bit ETM_OPT_CTXTID2 in perf config, this asks to trace VMID
667 * for recording CONTEXTIDR_EL2. Do not enable VMID tracing if the
668 * kernel is not running in EL2.
669 */
670 if (attr->config & BIT(ETM_OPT_CTXTID2)) {
671 if (!is_kernel_in_hyp_mode()) {
672 ret = -EINVAL;
673 goto out;
674 }
675
676 /* Only trace virtual contextID when runs in root PID namespace */
677 if (task_is_in_init_pid_ns(current))
678 config->cfg |= BIT(ETM4_CFG_BIT_VMID) |
679 BIT(ETM4_CFG_BIT_VMID_OPT);
680 }
681
682 /* return stack - enable if selected and supported */
683 if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
684 /* bit[12], Return stack enable bit */
685 config->cfg |= BIT(12);
686
687 /*
688 * Set any selected configuration and preset.
689 *
690 * This extracts the values of PMU_FORMAT_ATTR(configid) and PMU_FORMAT_ATTR(preset)
691 * in the perf attributes defined in coresight-etm-perf.c.
692 * configid uses bits 63:32 of attr->config2, preset uses bits 3:0 of attr->config.
693 * A zero configid means no configuration active, preset = 0 means no preset selected.
694 */
695 if (attr->config2 & GENMASK_ULL(63, 32)) {
696 cfg_hash = (u32)(attr->config2 >> 32);
697 preset = attr->config & 0xF;
698 ret = cscfg_csdev_enable_active_config(csdev, cfg_hash, preset);
699 }
700
701 out:
702 return ret;
703 }
704

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

2021-12-14 04:46:40

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] coresight: etm3x: Don't trace PID for non-root PID namespace

Hi,

On Tue, Dec 14, 2021 at 03:16:42AM +0800, kernel test robot wrote:
> Hi Leo,
>
> I love your patch! Yet something to improve:
>
> [auto build test ERROR on linus/master]
> [also build test ERROR on v5.16-rc5]
> [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]
>
> url: https://github.com/0day-ci/linux/commits/Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
> base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2585cf9dfaaddf00b069673f27bb3f8530e2039c
> config: arm-buildonly-randconfig-r003-20211213 (https://download.01.org/0day-ci/archive/20211214/[email protected]/config)
> compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project b6a2ddb6c8ac29412b1361810972e15221fa021c)
> reproduce (this is a W=1 build):
> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> chmod +x ~/bin/make.cross
> # install arm cross compiling tool for clang build
> # apt-get install binutils-arm-linux-gnueabi
> # https://github.com/0day-ci/linux/commit/81d5f47788c40d34c8159d64d4505eb485254e8f
> git remote add linux-review https://github.com/0day-ci/linux
> git fetch --no-tags linux-review Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
> git checkout 81d5f47788c40d34c8159d64d4505eb485254e8f
> # save the config file to linux build tree
> mkdir build_dir
> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/hwtracing/coresight/
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <[email protected]>
>
> All errors (new ones prefixed by >>):
>
> >> drivers/hwtracing/coresight/coresight-etm3x-core.c:344:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
> if (!task_is_in_init_pid_ns(current))
> ^
> 1 error generated.
>
>
> vim +/task_is_in_init_pid_ns +344 drivers/hwtracing/coresight/coresight-etm3x-core.c
>
> 301
> 302 #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | \
> 303 ETMCR_TIMESTAMP_EN | \
> 304 ETMCR_RETURN_STACK)
> 305
> 306 static int etm_parse_event_config(struct etm_drvdata *drvdata,
> 307 struct perf_event *event)
> 308 {
> 309 struct etm_config *config = &drvdata->config;
> 310 struct perf_event_attr *attr = &event->attr;
> 311
> 312 if (!attr)
> 313 return -EINVAL;
> 314
> 315 /* Clear configuration from previous run */
> 316 memset(config, 0, sizeof(struct etm_config));
> 317
> 318 if (attr->exclude_kernel)
> 319 config->mode = ETM_MODE_EXCL_KERN;
> 320
> 321 if (attr->exclude_user)
> 322 config->mode = ETM_MODE_EXCL_USER;
> 323
> 324 /* Always start from the default config */
> 325 etm_set_default(config);
> 326
> 327 /*
> 328 * By default the tracers are configured to trace the whole address
> 329 * range. Narrow the field only if requested by user space.
> 330 */
> 331 if (config->mode)
> 332 etm_config_trace_mode(config);
> 333
> 334 /*
> 335 * At this time only cycle accurate, return stack and timestamp
> 336 * options are available.
> 337 */
> 338 if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
> 339 return -EINVAL;
> 340
> 341 config->ctrl = attr->config;
> 342
> 343 /* Don't trace contextID when runs in non-root PID namespace */
> > 344 if (!task_is_in_init_pid_ns(current))

This patchset is based on another patchset [1], as described on the
cover letter patch. This is why here reports for building failure.

To avoid the false positive reporting, if any better practice I can
follow up to resolve the dependency between two patchsets, please let
me know and I will do in next time.

Thanks,
Leo

[1] https://lore.kernel.org/lkml/[email protected]/

> 345 config->ctrl &= ~ETMCR_CTXID_SIZE;
> 346
> 347 /*
> 348 * Possible to have cores with PTM (supports ret stack) and ETM
> 349 * (never has ret stack) on the same SoC. So if we have a request
> 350 * for return stack that can't be honoured on this core then
> 351 * clear the bit - trace will still continue normally
> 352 */
> 353 if ((config->ctrl & ETMCR_RETURN_STACK) &&
> 354 !(drvdata->etmccer & ETMCCER_RETSTACK))
> 355 config->ctrl &= ~ETMCR_RETURN_STACK;
> 356
> 357 return 0;
> 358 }
> 359
>
> ---
> 0-DAY CI Kernel Test Service, Intel Corporation
> https://lists.01.org/hyperkitty/list/[email protected]

2021-12-14 11:00:03

by Suzuki K Poulose

[permalink] [raw]
Subject: Re: [PATCH v2 0/4] coresight: etm: Correct PID tracing for non-root namespace

On 13/12/2021 12:13, Leo Yan wrote:
> If a profiling program runs in a non-root PID namespace, if CoreSight
> driver enables PID tracing (with contextID), it can lead to mismatching
> issue between the context ID traced in hardware (from the root
> namespace) and the PIDs gathered by profiling tool (e.g. perf) in its
> non-root namespace.
>
> CoreSight driver has tried to address this issue for the contextID
> related interfaces under sysfs, but it misses to prevent user to set
> VMID (virtual contextID) for kernel runs in EL2 with VHE; furthermore,
> it misses to handle the case when the profiling tool runs in the
> non-root PID namespace.
>
> For this reason, this patch series is to correct contextID tracing for
> non-root namespace. After applied this patchset, patch 02 doesn't
> permit users to access virtual contextID via sysfs nodes in the non-root
> PID namespace, patch 03 and 04 stop to trace PID packet for non-root PID
> namespace.
>
> This patch is dependent on the patchset "pid: Introduce helper
> task_is_in_root_ns()" [1].
>
> [1] https://lore.kernel.org/lkml/[email protected]/
>
>
> Leo Yan (4):
> coresight: etm4x: Add lock for reading virtual context ID comparator
> coresight: etm4x: Don't use virtual contextID for non-root PID
> namespace
> coresight: etm4x: Don't trace PID for non-root PID namespace
> coresight: etm3x: Don't trace PID for non-root PID namespace
>

For the series,

Reviewed-by: Suzuki K Poulose <[email protected]>

2021-12-16 08:55:06

by Chen, Rong A

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] coresight: etm3x: Don't trace PID for non-root PID namespace



On 12/14/21 12:46, Leo Yan wrote:
> Hi,
>
> On Tue, Dec 14, 2021 at 03:16:42AM +0800, kernel test robot wrote:
>> Hi Leo,
>>
>> I love your patch! Yet something to improve:
>>
>> [auto build test ERROR on linus/master]
>> [also build test ERROR on v5.16-rc5]
>> [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]
>>
>> url: https://github.com/0day-ci/linux/commits/Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
>> base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2585cf9dfaaddf00b069673f27bb3f8530e2039c
>> config: arm-buildonly-randconfig-r003-20211213 (https://download.01.org/0day-ci/archive/20211214/[email protected]/config)
>> compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project b6a2ddb6c8ac29412b1361810972e15221fa021c)
>> reproduce (this is a W=1 build):
>> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
>> chmod +x ~/bin/make.cross
>> # install arm cross compiling tool for clang build
>> # apt-get install binutils-arm-linux-gnueabi
>> # https://github.com/0day-ci/linux/commit/81d5f47788c40d34c8159d64d4505eb485254e8f
>> git remote add linux-review https://github.com/0day-ci/linux
>> git fetch --no-tags linux-review Leo-Yan/coresight-etm-Correct-PID-tracing-for-non-root-namespace/20211213-201632
>> git checkout 81d5f47788c40d34c8159d64d4505eb485254e8f
>> # save the config file to linux build tree
>> mkdir build_dir
>> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/hwtracing/coresight/
>>
>> If you fix the issue, kindly add following tag as appropriate
>> Reported-by: kernel test robot <[email protected]>
>>
>> All errors (new ones prefixed by >>):
>>
>>>> drivers/hwtracing/coresight/coresight-etm3x-core.c:344:7: error: implicit declaration of function 'task_is_in_init_pid_ns' [-Werror,-Wimplicit-function-declaration]
>> if (!task_is_in_init_pid_ns(current))
>> ^
>> 1 error generated.
>>
>>
>> vim +/task_is_in_init_pid_ns +344 drivers/hwtracing/coresight/coresight-etm3x-core.c
>>
>> 301
>> 302 #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | \
>> 303 ETMCR_TIMESTAMP_EN | \
>> 304 ETMCR_RETURN_STACK)
>> 305
>> 306 static int etm_parse_event_config(struct etm_drvdata *drvdata,
>> 307 struct perf_event *event)
>> 308 {
>> 309 struct etm_config *config = &drvdata->config;
>> 310 struct perf_event_attr *attr = &event->attr;
>> 311
>> 312 if (!attr)
>> 313 return -EINVAL;
>> 314
>> 315 /* Clear configuration from previous run */
>> 316 memset(config, 0, sizeof(struct etm_config));
>> 317
>> 318 if (attr->exclude_kernel)
>> 319 config->mode = ETM_MODE_EXCL_KERN;
>> 320
>> 321 if (attr->exclude_user)
>> 322 config->mode = ETM_MODE_EXCL_USER;
>> 323
>> 324 /* Always start from the default config */
>> 325 etm_set_default(config);
>> 326
>> 327 /*
>> 328 * By default the tracers are configured to trace the whole address
>> 329 * range. Narrow the field only if requested by user space.
>> 330 */
>> 331 if (config->mode)
>> 332 etm_config_trace_mode(config);
>> 333
>> 334 /*
>> 335 * At this time only cycle accurate, return stack and timestamp
>> 336 * options are available.
>> 337 */
>> 338 if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
>> 339 return -EINVAL;
>> 340
>> 341 config->ctrl = attr->config;
>> 342
>> 343 /* Don't trace contextID when runs in non-root PID namespace */
>> > 344 if (!task_is_in_init_pid_ns(current))
> This patchset is based on another patchset [1], as described on the
> cover letter patch. This is why here reports for building failure.
>
> To avoid the false positive reporting, if any better practice I can
> follow up to resolve the dependency between two patchsets, please let
> me know and I will do in next time.

Hi Leo,

Sorry for the inconvenience, the bot doesn't support to parse the link
in cover letter yet,
we suggest to use '--base' as documented
inhttps://git-scm.com/docs/git-format-patch

Best Regards,
Rong CHen

>
> Thanks,
> Leo
>
> [1] https://lore.kernel.org/lkml/[email protected]/
>
>> 345 config->ctrl &= ~ETMCR_CTXID_SIZE;
>> 346
>> 347 /*
>> 348 * Possible to have cores with PTM (supports ret stack) and ETM
>> 349 * (never has ret stack) on the same SoC. So if we have a request
>> 350 * for return stack that can't be honoured on this core then
>> 351 * clear the bit - trace will still continue normally
>> 352 */
>> 353 if ((config->ctrl & ETMCR_RETURN_STACK) &&
>> 354 !(drvdata->etmccer & ETMCCER_RETSTACK))
>> 355 config->ctrl &= ~ETMCR_RETURN_STACK;
>> 356
>> 357 return 0;
>> 358 }
>> 359
>>
>> ---
>> 0-DAY CI Kernel Test Service, Intel Corporation
>> https://lists.01.org/hyperkitty/list/[email protected]