[PATCH 0/3] coresight: etm-perf: Fix pid tracing with VHE

Suzuki K Poulose
2020-11-10T18:33:55Z

With the Virtualization Host Extensions, the kernel can run at EL2.
In this case the pid is written to CONTEXTIDR_EL2 instead of the
CONTEXTIDR_EL1. Thus the normal coresight tracing will be unable
to detect the PID of the thread generating the trace by looking
at the CONTEXTIDR_EL1. Thus, depending on the kernel EL, we must
switch to tracing the correct CONTEXTIDR register.

With VHE, we must set the TRCCONFIGR.VMID and TRCCONFIGR.VMID_OPT
to include the CONTEXTIDR_EL2 as the VMID in the trace. This
requires the perf tool to detect the changes in the TRCCONFIGR and
use the VMID / CID field for the PID. The challenge here is for
the perf tool to detect the kernel behavior.

Instead of the previously proposed invasive approaches, this set
implements a less intrusive mechanism, by playing with the
perf_event.attribute.config bits.

Some facts:

- The perf tool requests pid tracing and timestamp in some
scenarios. (e.g, system wide, task bound (!per-thread).

- The default pid tracing is via requesting "contextid"
But this only works for EL1 kernel.

- "contextid" tracing is useful for tracing VMs (when
we get to virtualization support). So we don't want
to move this around.

So this patch series introduces two new format bits:

- contextid_in_vmid -> Is only supported when the VMID tracing
and CONTEXTIDR_EL2 both are supported. When requested the perf
etm4x backend sets (TRCCONFIGR.VMID | TRCCONFIGR.VMID_OPT).
As per ETMv4.4 TRM, when the core supports VHE, the CONTEXTIDR_EL2
tracing is mandatory. (See the field TRCID2.VMIDOPT)

- pid -> Is an alias for the correct config to enable PID tracing
on any kernel.
i.e, in EL1 kernel -> pid == contextid
EL2 kernel -> pid == contextid_in_vmid

With this, the perf tool is also updated to request the "pid"
tracing whenever available, falling back to "contextid" if it
is unavailable (to support new tool running on older kernels).

The perf tool will also set the TRCCONFIGR accordingly based
on the config bits, allowing the decoder to output the appropriate
fields.

I have another patch for the perf decoder to set the TID from VMID
when the cid is invalid and and the vmid is valid. But this doesn't
verify if the trcconfigr.vmid_opt was set. I will leave this to
Mike Leach to fix it properly.

Tested on Juno (EL1 kernel) and N1SDP (El2 kernel). Feedback welcome.

A tree with these patches are available here :

git.gitlab.arm.com:linux-arm/linux-skp.git coresight/el2/pid

Suzuki K Poulose (3):
coresight: etm-perf: Add support for PID tracing for kernel at EL2
perf: cs_etm: Use pid tracing explicitly instead of contextid
rfc: perf: cs_etm: Detect pid in VMID for kernel running at EL2

.../hwtracing/coresight/coresight-etm-perf.c | 14 ++++
.../coresight/coresight-etm4x-core.c | 9 +++
include/linux/coresight-pmu.h | 11 ++--
tools/include/linux/coresight-pmu.h | 11 ++--
tools/perf/arch/arm/util/cs-etm.c | 65 ++++++++++++++-----
.../perf/util/cs-etm-decoder/cs-etm-decoder.c | 28 ++++----
6 files changed, 101 insertions(+), 37 deletions(-)

--
2.24.1