Skip to content

Commit 88f1186

Browse files
Suzuki K Poulosegregkh
authored andcommitted
coresight: etm-perf: Support PID tracing for kernel at EL2
When the kernel is running at EL2, the PID is stored in CONTEXTIDR_EL2. So, tracing CONTEXTIDR_EL1 doesn't give us the pid of the process. Thus we should trace the VMID with VMIDOPT set to trace CONTEXTIDR_EL2 instead of CONTEXTIDR_EL1. Given that we have an existing config option "contextid" and this will be useful for tracing virtual machines (when we get to support virtualization). So instead, this patch extends option CTXTID with an extra bit ETM_OPT_CTXTID2 (bit 15), thus on an EL2 kernel, we will have another bit available for the perf tool: ETM_OPT_CTXTID is for kernel running in EL1, ETM_OPT_CTXTID2 is used when kernel runs in EL2 with VHE enabled. The tool must be backward compatible for users, i.e, "contextid" today traces PID and that should remain the same; for this purpose, the perf tool is updated to automatically set corresponding bit for the "contextid" config, therefore, the user doesn't have to bother which EL the kernel is running. i.e, perf record -e cs_etm/contextid/u -- will always do the "pid" tracing, independent of the kernel EL. The driver parses the format "contextid", which traces CONTEXTIDR_EL1 for ETM_OPT_CTXTID (on EL1 kernel) and traces CONTEXTIDR_EL2 for ETM_OPT_CTXTID2 (on EL2 kernel). Besides the enhancement for format "contexid", extra two formats are introduced: "contextid1" and "contextid2". This considers to support tracing both CONTEXTIDR_EL1 and CONTEXTIDR_EL2 when the kernel is running at EL2. Finally, the PMU formats are defined as follow: "contextid1": Available on both EL1 kernel and EL2 kernel. When the kernel is running at EL1, "contextid1" enables the PID tracing; when the kernel is running at EL2, this enables tracing the PID of guest applications. "contextid2": Only usable when the kernel is running at EL2. When selected, enables PID tracing on EL2 kernel. "contextid": Will be an alias for the option that enables PID tracing. I.e, contextid == contextid1, on EL1 kernel. contextid == contextid2, on EL2 kernel. Cc: Mathieu Poirier <[email protected]> Cc: Al Grant <[email protected]> Cc: Mike Leach <[email protected]> Cc: Leo Yan <[email protected]> Reviewed-by: Mike Leach <[email protected]> Signed-off-by: Suzuki K Poulose <[email protected]> [ Added two config formats: contextid1, contextid2 ] Signed-off-by: Leo Yan <[email protected]> Signed-off-by: Mathieu Poirier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 53abf3f commit 88f1186

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

drivers/hwtracing/coresight/coresight-etm-perf.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,40 @@ static DEFINE_PER_CPU(struct coresight_device *, csdev_src);
3232
* now take them as general formats and apply on all ETMs.
3333
*/
3434
PMU_FORMAT_ATTR(cycacc, "config:" __stringify(ETM_OPT_CYCACC));
35-
PMU_FORMAT_ATTR(contextid, "config:" __stringify(ETM_OPT_CTXTID));
35+
/* contextid1 enables tracing CONTEXTIDR_EL1 for ETMv4 */
36+
PMU_FORMAT_ATTR(contextid1, "config:" __stringify(ETM_OPT_CTXTID));
37+
/* contextid2 enables tracing CONTEXTIDR_EL2 for ETMv4 */
38+
PMU_FORMAT_ATTR(contextid2, "config:" __stringify(ETM_OPT_CTXTID2));
3639
PMU_FORMAT_ATTR(timestamp, "config:" __stringify(ETM_OPT_TS));
3740
PMU_FORMAT_ATTR(retstack, "config:" __stringify(ETM_OPT_RETSTK));
3841
/* Sink ID - same for all ETMs */
3942
PMU_FORMAT_ATTR(sinkid, "config2:0-31");
4043

44+
/*
45+
* contextid always traces the "PID". The PID is in CONTEXTIDR_EL1
46+
* when the kernel is running at EL1; when the kernel is at EL2,
47+
* the PID is in CONTEXTIDR_EL2.
48+
*/
49+
static ssize_t format_attr_contextid_show(struct device *dev,
50+
struct device_attribute *attr,
51+
char *page)
52+
{
53+
int pid_fmt = ETM_OPT_CTXTID;
54+
55+
#if defined(CONFIG_CORESIGHT_SOURCE_ETM4X)
56+
pid_fmt = is_kernel_in_hyp_mode() ? ETM_OPT_CTXTID2 : ETM_OPT_CTXTID;
57+
#endif
58+
return sprintf(page, "config:%d\n", pid_fmt);
59+
}
60+
61+
struct device_attribute format_attr_contextid =
62+
__ATTR(contextid, 0444, format_attr_contextid_show, NULL);
63+
4164
static struct attribute *etm_config_formats_attr[] = {
4265
&format_attr_cycacc.attr,
4366
&format_attr_contextid.attr,
67+
&format_attr_contextid1.attr,
68+
&format_attr_contextid2.attr,
4469
&format_attr_timestamp.attr,
4570
&format_attr_retstack.attr,
4671
&format_attr_sinkid.attr,

drivers/hwtracing/coresight/coresight-etm4x-core.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,19 @@ static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
550550
/* bit[6], Context ID tracing bit */
551551
config->cfg |= BIT(ETM4_CFG_BIT_CTXTID);
552552

553+
/*
554+
* If set bit ETM_OPT_CTXTID2 in perf config, this asks to trace VMID
555+
* for recording CONTEXTIDR_EL2. Do not enable VMID tracing if the
556+
* kernel is not running in EL2.
557+
*/
558+
if (attr->config & BIT(ETM_OPT_CTXTID2)) {
559+
if (!is_kernel_in_hyp_mode()) {
560+
ret = -EINVAL;
561+
goto out;
562+
}
563+
config->cfg |= BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT);
564+
}
565+
553566
/* return stack - enable if selected and supported */
554567
if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
555568
/* bit[12], Return stack enable bit */

include/linux/coresight-pmu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
*/
2121
#define ETM_OPT_CYCACC 12
2222
#define ETM_OPT_CTXTID 14
23+
#define ETM_OPT_CTXTID2 15
2324
#define ETM_OPT_TS 28
2425
#define ETM_OPT_RETSTK 29
2526

2627
/* ETMv4 CONFIGR programming bits for the ETM OPTs */
2728
#define ETM4_CFG_BIT_CYCACC 4
2829
#define ETM4_CFG_BIT_CTXTID 6
30+
#define ETM4_CFG_BIT_VMID 7
2931
#define ETM4_CFG_BIT_TS 11
3032
#define ETM4_CFG_BIT_RETSTK 12
33+
#define ETM4_CFG_BIT_VMID_OPT 15
3134

3235
static inline int coresight_get_trace_id(int cpu)
3336
{

0 commit comments

Comments
 (0)