Skip to contentSkip to conversation

Perf kvm commands bug fix

PATCH Superseded Dapeng Mi 3 participants 17 messages Aug 5, 2025 – Aug 11, 2025 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dapeng Mi

author
#0 lore raw

cover Perf kvm commands bug fix

This patch-set fixes perf kvm commands issues, like missed memory allocation check/free, out of range memory access and especially the issue that fails to sample guest with "perf kvm record/top" commands on Intel platforms.

The commit 634d36f82517 ("perf record: Just use "cycles:P" as the
default event") changes to use PEBS event to do sampling by default
including guest sampling. This breaks host to sample guest with commands "perf kvm record/top" on Intel platforms.

Since the change "KVM: x86/pmu: Add basic support to enable guest PEBS
via DS"[1] starts, host loses the capability to sample guest with PEBS
since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This leads to PEBS events can't be used to sample guest by host, otherwise no guest PEBS records can be really sampled. The patches 4-5/5 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Tests:
* Run command "perf kvm record -a && perf kvm report" and "perf kvm top" on Intel Sapphire Rapids platform, guest records can be captured successfully.
* Since no powerpc platforms on hand, doesn't check the patches on powerpc. Any test on powerpc is appreciated.

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

Dapeng Mi (5):
perf tools kvm: Add missed memory allocation check and free perf tools kvm: Fix the potential out of range memory access issue perf tools: Add helper x86__is_intel_cpu()
perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel perf tools kvm: Use "cycles" to sample guest for "kvm top" on Intel

 tools/perf/arch/x86/util/kvm-stat.c | 46 ++++++++++++++++++
 tools/perf/builtin-kvm.c            | 73 ++++++++++++++++++++++++-----
 tools/perf/util/env.c               | 22 +++++++++
 tools/perf/util/env.h               |  2 +
 4 files changed, 131 insertions(+), 12 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/5 perf tools kvm: Add missed memory allocation check and free

Current code allocates rec_argv[] array, but doesn't check if the allocation is successful and explicitly free the rec_argv[] array.

Add them back.

---
 tools/perf/builtin-kvm.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
tools/perf/builtin-kvm.c +23 -6
index 7b15b4a705e4..f78a67a199ff 100644
@@ -1719,7 +1719,9 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
 	set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
 
 	record_usage = kvm_stat_record_usage;
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int
@@ -2006,6 +2008,9 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("record");
 	rec_argv[i++] = strdup("-o");
 	rec_argv[i++] = strdup(file_name);
@@ -2014,16 +2019,21 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int __cmd_report(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("report");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2032,17 +2042,22 @@ static int __cmd_report(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_report(i, rec_argv);
+	ret = cmd_report(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int
 __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("buildid-list");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2051,7 +2066,9 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_buildid_list(i, rec_argv);
+	ret = cmd_buildid_list(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 int cmd_kvm(int argc, const char **argv)
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 1/5] perf tools kvm: Add missed memory allocation check and free

Quoted 15 lines · Dapeng Mi

Current code allocates rec_argv[] array, but doesn't check if the allocation is successful and explicitly free the rec_argv[] array.

Add them back.

---
 tools/perf/builtin-kvm.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
tools/perf/builtin-kvm.c +0 -0
index 7b15b4a705e4..f78a67a199ff 100644

tools/perf/builtin-kvm.c kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv) · line 1723

Quoted 6 lines
tools/perf/builtin-kvm.c +2 -1
@@ -1719,7 +1719,9 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
 	set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
 
 	record_usage = kvm_stat_record_usage;
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);

Well.. it's not enough just to free rec_argv. You also need to free all items in the rec_argv[]. Probably you want to add more STRDUP_FAIL_EXIT when copying the original argv (here and other places).

Thanks,
Namhyung

Quoted 77 lines
tools/perf/builtin-kvm.c +21 -5
+	return ret;
 }
 
 static int
@@ -2006,6 +2008,9 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("record");
 	rec_argv[i++] = strdup("-o");
 	rec_argv[i++] = strdup(file_name);
@@ -2014,16 +2019,21 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int __cmd_report(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("report");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2032,17 +2042,22 @@ static int __cmd_report(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_report(i, rec_argv);
+	ret = cmd_report(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int
 __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("buildid-list");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2051,7 +2066,9 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_buildid_list(i, rec_argv);
+	ret = cmd_buildid_list(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 int cmd_kvm(int argc, const char **argv)
Signature · 1 line
2.34.1
author
#3 lore raw

Re: [PATCH 1/5] perf tools kvm: Add missed memory allocation check and free

Quoted 18 lines · Namhyung Kim
Quoted 17 lines · Dapeng Mi

Current code allocates rec_argv[] array, but doesn't check if the allocation is successful and explicitly free the rec_argv[] array.

Add them back.

---
 tools/perf/builtin-kvm.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
tools/perf/builtin-kvm.c +0 -0
index 7b15b4a705e4..f78a67a199ff 100644
@@ -1719,7 +1719,9 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
 	set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
 
Quoted 7 lines

tools/perf/builtin-kvm.c kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv) · line 1723

Quoted 4 lines
tools/perf/builtin-kvm.c +2 -1
 	record_usage = kvm_stat_record_usage;
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);

Well.. it's not enough just to free rec_argv. You also need to free all items in the rec_argv[]. Probably you want to add more STRDUP_FAIL_EXIT when copying the original argv (here and other places).

Oh, yes. Would do. Thanks.

Quoted 82 lines

Thanks,
Namhyung

Quoted 77 lines
tools/perf/builtin-kvm.c +21 -5
+	return ret;
 }
 
 static int
@@ -2006,6 +2008,9 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("record");
 	rec_argv[i++] = strdup("-o");
 	rec_argv[i++] = strdup(file_name);
@@ -2014,16 +2019,21 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int __cmd_report(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("report");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2032,17 +2042,22 @@ static int __cmd_report(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_report(i, rec_argv);
+	ret = cmd_report(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 static int
 __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 {
-	int rec_argc, i = 0, j;
+	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
 	rec_argc = argc + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
 	rec_argv[i++] = strdup("buildid-list");
 	rec_argv[i++] = strdup("-i");
 	rec_argv[i++] = strdup(file_name);
@@ -2051,7 +2066,9 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_buildid_list(i, rec_argv);
+	ret = cmd_buildid_list(i, rec_argv);
+	free(rec_argv);
+	return ret;
 }
 
 int cmd_kvm(int argc, const char **argv)
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 2/5 perf tools kvm: Fix the potential out of range memory access issue

In reply to Dapeng Mi

kvm_add_default_arch_event() helper may add 2 extra options but it directly modifies the original argv[] array. This may cause out of range memory access. Fix this issue.

---
 tools/perf/builtin-kvm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/builtin-kvm.c +9 -5
index f78a67a199ff..7e48d2437710 100644
@@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
-	ret = kvm_add_default_arch_event(&argc, argv);
-	if (ret)
-		return -EINVAL;
-
 	rec_argc = argc + 2;
-	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
 	if (!rec_argv)
 		return -ENOMEM;
 
@@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
 	ret = cmd_record(i, rec_argv);
 	free(rec_argv);
 	return ret;
Signature · 1 line
2.34.1
maintainer
#5 lore raw

Re: [PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue

Quoted 14 lines · Dapeng Mi

kvm_add_default_arch_event() helper may add 2 extra options but it directly modifies the original argv[] array. This may cause out of range memory access. Fix this issue.

---
 tools/perf/builtin-kvm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/builtin-kvm.c +0 -0
index f78a67a199ff..7e48d2437710 100644

tools/perf/builtin-kvm.c static int __cmd_record(const char *file_name, int argc, const char **argv) · line 2005

Quoted 8 lines
tools/perf/builtin-kvm.c +0 -4
@@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
-	ret = kvm_add_default_arch_event(&argc, argv);
-	if (ret)
-		return -EINVAL;
-
 	rec_argc = argc + 2;

While at it, can we add a comment for this too? I believe it's for "-o" and filename.

Thanks,
Namhyung

Quoted 23 lines
tools/perf/builtin-kvm.c +9 -1
-	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
 	if (!rec_argv)
 		return -ENOMEM;
 
@@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
 	ret = cmd_record(i, rec_argv);
 	free(rec_argv);
 	return ret;
Signature · 1 line
2.34.1
author
#6 lore raw

Re: [PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue

Quoted 18 lines · Namhyung Kim
Quoted 17 lines · Dapeng Mi

kvm_add_default_arch_event() helper may add 2 extra options but it directly modifies the original argv[] array. This may cause out of range memory access. Fix this issue.

---
 tools/perf/builtin-kvm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/builtin-kvm.c +0 -0
index f78a67a199ff..7e48d2437710 100644
@@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
Quoted 7 lines

tools/perf/builtin-kvm.c static int __cmd_record(const char *file_name, int argc, const char **argv) · line 2005

Quoted 5 lines
tools/perf/builtin-kvm.c +0 -4
-	ret = kvm_add_default_arch_event(&argc, argv);
-	if (ret)
-		return -EINVAL;
-
 	rec_argc = argc + 2;

While at it, can we add a comment for this too? I believe it's for "-o" and filename.

Sure.

Quoted 28 lines

Thanks,
Namhyung

Quoted 23 lines
tools/perf/builtin-kvm.c +9 -1
-	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
 	if (!rec_argv)
 		return -ENOMEM;
 
@@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
 	ret = cmd_record(i, rec_argv);
 	free(rec_argv);
 	return ret;
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 3/5 perf tools: Add helper x86__is_intel_cpu()

In reply to Dapeng Mi

Add helper x86__is_intel_cpu() to indicate if it's a x86 intel platform.

---
 tools/perf/util/env.c | 22 ++++++++++++++++++++++
 tools/perf/util/env.h |  2 ++
 2 files changed, 24 insertions(+)
tools/perf/util/env.c +22 -0
index c8c248754621..f1626d2032cd 100644
@@ -802,3 +802,25 @@ bool x86__is_amd_cpu(void)
 
 	return is_amd;
 }
+
+bool perf_env__is_x86_intel_cpu(struct perf_env *env)
+{
+	static int is_intel; /* 0: Uninitialized, 1: Yes, -1: No */
+
+	if (is_intel == 0)
+		is_intel = env->cpuid && strstarts(env->cpuid, "GenuineIntel") ? 1 : -1;
+
+	return is_intel >= 1 ? true : false;
+}
+
+bool x86__is_intel_cpu(void)
+{
+	struct perf_env env = { .total_mem = 0, };
+	bool is_intel;
+
+	perf_env__cpuid(&env);
+	is_intel = perf_env__is_x86_intel_cpu(&env);
+	perf_env__exit(&env);
+
+	return is_intel;
+}
tools/perf/util/env.h +2 -0
index e00179787a34..9977b85523a8 100644
@@ -201,5 +201,7 @@ void perf_env__find_br_cntr_info(struct perf_env *env,
 
 bool x86__is_amd_cpu(void);
 bool perf_env__is_x86_amd_cpu(struct perf_env *env);
+bool x86__is_intel_cpu(void);
+bool perf_env__is_x86_intel_cpu(struct perf_env *env);
 
 #endif /* __PERF_ENV_H */
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 4/5 perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel

In reply to Dapeng Mi

After KVM supports PEBS for guest on Intel platforms
(https://lore.kernel.org/all/[email protected]/),
host loses the capability to sample guest with PEBS since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This would lead to "perf kvm record" fails to sample guest on Intel platforms since "cycles:P" event is used to sample guest by default as below case shows.

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.787 MB perf.data.guest ]

So to ensure guest record can be sampled successfully, use "cycles" instead of "cycles:P" to sample guest record by default on Intel platforms. With this patch, the guest record can be sampled successfully.

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.783 MB perf.data.guest (23 samples) ]

Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
 tools/perf/arch/x86/util/kvm-stat.c | 46 +++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
tools/perf/arch/x86/util/kvm-stat.c +46 -0
index 424716518b75..cdb5f3e1b5be 100644
@@ -3,9 +3,11 @@
 #include <string.h>
 #include "../../../util/kvm-stat.h"
 #include "../../../util/evsel.h"
+#include "../../../util/env.h"
 #include <asm/svm.h>
 #include <asm/vmx.h>
 #include <asm/kvm.h>
+#include <subcmd/parse-options.h>
 
 define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
 define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
@@ -211,3 +213,47 @@ int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
 
 	return 0;
 }
+
+/*
+ * After KVM supports PEBS for guest on Intel platforms
+ * (https://lore.kernel.org/all/[email protected]/),
+ * host loses the capability to sample guest with PEBS since all PEBS related
+ * MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is
+ * switched to guest GVA at vm-entry. This would lead to "perf kvm record"
+ * fails to sample guest on Intel platforms since "cycles:P" event is used to
+ * sample guest by default.
+ *
+ * So, to avoid this issue explicitly use "cycles" instead of "cycles:P" event
+ * by default to sample guest on Intel platforms.
+ */
+int kvm_add_default_arch_event(int *argc, const char **argv)
+{
+	const char **tmp;
+	bool event = false;
+	int i, j = *argc;
+
+	const struct option event_options[] = {
+		OPT_BOOLEAN('e', "event", &event, NULL),
+		OPT_END()
+	};
+
+	if (!x86__is_intel_cpu())
+		return 0;
+
+	tmp = calloc(j + 1, sizeof(char *));
+	if (!tmp)
+		return -EINVAL;
+
+	for (i = 0; i < j; i++)
+		tmp[i] = argv[i];
+
+	parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
+	if (!event) {
+		argv[j++] = strdup("-e");
+		argv[j++] = strdup("cycles");
+		*argc += 2;
+	}
+
+	free(tmp);
+	return 0;
+}
Signature · 1 line
2.34.1
maintainer
#9 lore raw

Re: [PATCH 4/5] perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel

Quoted 7 lines · Dapeng Mi

After KVM supports PEBS for guest on Intel platforms
(https://lore.kernel.org/all/[email protected]/),
host loses the capability to sample guest with PEBS since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This would lead to "perf kvm record" fails to sample guest on Intel platforms since "cycles:P" event is used to sample guest by default as below case shows.

Do you mean we cannot use "cycles:PG" for perf kvm record?

Quoted 5 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.787 MB perf.data.guest ]

Quoted 8 lines

So to ensure guest record can be sampled successfully, use "cycles" instead of "cycles:P" to sample guest record by default on Intel platforms. With this patch, the guest record can be sampled successfully.

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.783 MB perf.data.guest (23 samples) ]

What if user already gave some events in the command line? I think you need to check if "-e" or "--event" (and "--pfm-events" too) is in the argv[] before adding these.

Thanks,
Namhyung

Quoted 75 lines
Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
 tools/perf/arch/x86/util/kvm-stat.c | 46 +++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
tools/perf/arch/x86/util/kvm-stat.c +46 -0
index 424716518b75..cdb5f3e1b5be 100644
@@ -3,9 +3,11 @@
 #include <string.h>
 #include "../../../util/kvm-stat.h"
 #include "../../../util/evsel.h"
+#include "../../../util/env.h"
 #include <asm/svm.h>
 #include <asm/vmx.h>
 #include <asm/kvm.h>
+#include <subcmd/parse-options.h>
 
 define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
 define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
@@ -211,3 +213,47 @@ int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
 
 	return 0;
 }
+
+/*
+ * After KVM supports PEBS for guest on Intel platforms
+ * (https://lore.kernel.org/all/[email protected]/),
+ * host loses the capability to sample guest with PEBS since all PEBS related
+ * MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is
+ * switched to guest GVA at vm-entry. This would lead to "perf kvm record"
+ * fails to sample guest on Intel platforms since "cycles:P" event is used to
+ * sample guest by default.
+ *
+ * So, to avoid this issue explicitly use "cycles" instead of "cycles:P" event
+ * by default to sample guest on Intel platforms.
+ */
+int kvm_add_default_arch_event(int *argc, const char **argv)
+{
+	const char **tmp;
+	bool event = false;
+	int i, j = *argc;
+
+	const struct option event_options[] = {
+		OPT_BOOLEAN('e', "event", &event, NULL),
+		OPT_END()
+	};
+
+	if (!x86__is_intel_cpu())
+		return 0;
+
+	tmp = calloc(j + 1, sizeof(char *));
+	if (!tmp)
+		return -EINVAL;
+
+	for (i = 0; i < j; i++)
+		tmp[i] = argv[i];
+
+	parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
+	if (!event) {
+		argv[j++] = strdup("-e");
+		argv[j++] = strdup("cycles");
+		*argc += 2;
+	}
+
+	free(tmp);
+	return 0;
+}
Signature · 1 line
2.34.1
author
#10 lore raw

Re: [PATCH 4/5] perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel

Quoted 9 lines · Namhyung Kim
Quoted 7 lines · Dapeng Mi

After KVM supports PEBS for guest on Intel platforms
(https://lore.kernel.org/all/[email protected]/),
host loses the capability to sample guest with PEBS since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This would lead to "perf kvm record" fails to sample guest on Intel platforms since "cycles:P" event is used to sample guest by default as below case shows.

Do you mean we cannot use "cycles:PG" for perf kvm record?

Yes. Here is the output on Intel Sapphire rapids.

sudo ./perf record -e cycles:PG -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.801 MB perf.data ]

No guest records are captured with PEBS, and guest PEBS records can be sampled only without PEBS.

sudo ./perf record -e cycles:G -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.798 MB perf.data (60 samples) ]

Quoted 10 lines
Quoted 10 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.787 MB perf.data.guest ]

So to ensure guest record can be sampled successfully, use "cycles" instead of "cycles:P" to sample guest record by default on Intel platforms. With this patch, the guest record can be sampled successfully.

Quoted 6 lines
Quoted 3 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.783 MB perf.data.guest (23 samples) ]

What if user already gave some events in the command line? I think you need to check if "-e" or "--event" (and "--pfm-events" too) is in the argv[] before adding these.

kvm_add_default_arch_event() would detect if user already sets events explicitly. If so, it won't add "cycles" event any more. Thanks.

Quoted 78 lines

Thanks,
Namhyung

Quoted 74 lines
Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
 tools/perf/arch/x86/util/kvm-stat.c | 46 +++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
tools/perf/arch/x86/util/kvm-stat.c +46 -0
index 424716518b75..cdb5f3e1b5be 100644
@@ -3,9 +3,11 @@
 #include <string.h>
 #include "../../../util/kvm-stat.h"
 #include "../../../util/evsel.h"
+#include "../../../util/env.h"
 #include <asm/svm.h>
 #include <asm/vmx.h>
 #include <asm/kvm.h>
+#include <subcmd/parse-options.h>
 
 define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
 define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
@@ -211,3 +213,47 @@ int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
 
 	return 0;
 }
+
+/*
+ * After KVM supports PEBS for guest on Intel platforms
+ * (https://lore.kernel.org/all/[email protected]/),
+ * host loses the capability to sample guest with PEBS since all PEBS related
+ * MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is
+ * switched to guest GVA at vm-entry. This would lead to "perf kvm record"
+ * fails to sample guest on Intel platforms since "cycles:P" event is used to
+ * sample guest by default.
+ *
+ * So, to avoid this issue explicitly use "cycles" instead of "cycles:P" event
+ * by default to sample guest on Intel platforms.
+ */
+int kvm_add_default_arch_event(int *argc, const char **argv)
+{
+	const char **tmp;
+	bool event = false;
+	int i, j = *argc;
+
+	const struct option event_options[] = {
+		OPT_BOOLEAN('e', "event", &event, NULL),
+		OPT_END()
+	};
+
+	if (!x86__is_intel_cpu())
+		return 0;
+
+	tmp = calloc(j + 1, sizeof(char *));
+	if (!tmp)
+		return -EINVAL;
+
+	for (i = 0; i < j; i++)
+		tmp[i] = argv[i];
+
+	parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
+	if (!event) {
+		argv[j++] = strdup("-e");
+		argv[j++] = strdup("cycles");
+		*argc += 2;
+	}
+
+	free(tmp);
+	return 0;
+}
Signature · 1 line
2.34.1
maintainer
#11 lore raw

Re: [PATCH 4/5] perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel

Quoted 36 lines · Dapeng
Quoted 9 lines · Namhyung Kim
Quoted 7 lines · Dapeng Mi

After KVM supports PEBS for guest on Intel platforms
(https://lore.kernel.org/all/[email protected]/),
host loses the capability to sample guest with PEBS since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This would lead to "perf kvm record" fails to sample guest on Intel platforms since "cycles:P" event is used to sample guest by default as below case shows.

Do you mean we cannot use "cycles:PG" for perf kvm record?

Yes. Here is the output on Intel Sapphire rapids.

sudo ./perf record -e cycles:PG -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.801 MB perf.data ]

No guest records are captured with PEBS, and guest PEBS records can be sampled only without PEBS.

sudo ./perf record -e cycles:G -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.798 MB perf.data (60 samples) ]

Quoted 10 lines
Quoted 10 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.787 MB perf.data.guest ]

So to ensure guest record can be sampled successfully, use "cycles" instead of "cycles:P" to sample guest record by default on Intel platforms. With this patch, the guest record can be sampled successfully.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.783 MB perf.data.guest (23 samples) ]

What if user already gave some events in the command line? I think you need to check if "-e" or "--event" (and "--pfm-events" too) is in the argv[] before adding these.

kvm_add_default_arch_event() would detect if user already sets events explicitly. If so, it won't add "cycles" event any more. Thanks.

Oh, ok. I can see you called parse_options to check the option. You'd better to check "--pfm-events" as well.

Thanks,
Namhyung

author
#12 lore raw

Re: [PATCH 4/5] perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel

Quoted 37 lines · Namhyung Kim
Quoted 36 lines · Dapeng
Quoted 9 lines · Namhyung Kim
Quoted 7 lines · Dapeng Mi

After KVM supports PEBS for guest on Intel platforms
(https://lore.kernel.org/all/[email protected]/),
host loses the capability to sample guest with PEBS since all PEBS related MSRs are switched to guest value after vm-entry, like IA32_DS_AREA MSR is switched to guest GVA at vm-entry. This would lead to "perf kvm record" fails to sample guest on Intel platforms since "cycles:P" event is used to sample guest by default as below case shows.

Do you mean we cannot use "cycles:PG" for perf kvm record?

Yes. Here is the output on Intel Sapphire rapids.

sudo ./perf record -e cycles:PG -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.801 MB perf.data ]

No guest records are captured with PEBS, and guest PEBS records can be sampled only without PEBS.

sudo ./perf record -e cycles:G -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.798 MB perf.data (60 samples) ]

Quoted 12 lines
Quoted 12 lines

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.787 MB perf.data.guest ]

So to ensure guest record can be sampled successfully, use "cycles" instead of "cycles:P" to sample guest record by default on Intel platforms. With this patch, the guest record can be sampled successfully.

sudo perf kvm record -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.783 MB perf.data.guest (23 samples) ]

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

What if user already gave some events in the command line? I think you need to check if "-e" or "--event" (and "--pfm-events" too) is in the argv[] before adding these.

kvm_add_default_arch_event() would detect if user already sets events explicitly. If so, it won't add "cycles" event any more. Thanks.

Oh, ok. I can see you called parse_options to check the option. You'd better to check "--pfm-events" as well.

Sure. Thanks.

Quoted 5 lines

Thanks,
Namhyung

author
#13 lore raw

PATCH 5/5 perf tools kvm: Use "cycles" to sample guest for "kvm top" on Intel

In reply to Dapeng Mi

As same reason with previous patch, use "cyles" instead of "cycles:P" event by default to sample guest for "perf kvm top" command on Intel platforms.

Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
 tools/perf/builtin-kvm.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
tools/perf/builtin-kvm.c +29 -1
index 7e48d2437710..d72b40f3df12 100644
@@ -2075,6 +2075,34 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 	return ret;
 }
 
+static int __cmd_top(int argc, const char **argv)
+{
+	int i = 0, ret;
+	const char **rec_argv;
+
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(argc + 2 + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
+	for (i = 0; i < argc; i++)
+		rec_argv[i] = argv[i];
+
+	BUG_ON(i != argc);
+
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
+	ret = cmd_top(i, rec_argv);
+	free(rec_argv);
+
+	return ret;
+}
+
 int cmd_kvm(int argc, const char **argv)
 {
 	const char *file_name = NULL;
@@ -2135,7 +2163,7 @@ int cmd_kvm(int argc, const char **argv)
 	else if (strlen(argv[0]) > 2 && strstarts("diff", argv[0]))
 		return cmd_diff(argc, argv);
 	else if (!strcmp(argv[0], "top"))
-		return cmd_top(argc, argv);
+		return __cmd_top(argc, argv);
 	else if (strlen(argv[0]) > 2 && strstarts("buildid-list", argv[0]))
 		return __cmd_buildid_list(file_name, argc, argv);
 #if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
Signature · 1 line
2.34.1
author
#14 lore raw

Re: [PATCH 5/5] perf tools kvm: Use "cycles" to sample guest for "kvm top" on Intel

Quoted 51 lines · Dapeng Mi

As same reason with previous patch, use "cyles" instead of "cycles:P" event by default to sample guest for "perf kvm top" command on Intel platforms.

Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
 tools/perf/builtin-kvm.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
tools/perf/builtin-kvm.c +28 -0
index 7e48d2437710..d72b40f3df12 100644
@@ -2075,6 +2075,34 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 	return ret;
 }
 
+static int __cmd_top(int argc, const char **argv)
+{
+	int i = 0, ret;
+	const char **rec_argv;
+
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(argc + 2 + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
+	for (i = 0; i < argc; i++)
+		rec_argv[i] = argv[i];
+
+	BUG_ON(i != argc);
+
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
+	ret = cmd_top(i, rec_argv);
+	free(rec_argv);
+
+	return ret;
+}
+
 int cmd_kvm(int argc, const char **argv)
 {
 	const char *file_name = NULL;

tools/perf/builtin-kvm.c int cmd_kvm(int argc, const char **argv) · line 2169

Quoted 8 lines
tools/perf/builtin-kvm.c +1 -1
@@ -2135,7 +2163,7 @@ int cmd_kvm(int argc, const char **argv)
 	else if (strlen(argv[0]) > 2 && strstarts("diff", argv[0]))
 		return cmd_diff(argc, argv);
 	else if (!strcmp(argv[0], "top"))
-		return cmd_top(argc, argv);
+		return __cmd_top(argc, argv);
 	else if (strlen(argv[0]) > 2 && strstarts("buildid-list", argv[0]))
 		return __cmd_buildid_list(file_name, argc, argv);
 #if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)

This patch would impact powerpc platform as well. Base on the comments before kvm_add_default_arch_event() in tools/perf/arch/powerpc/util/kvm-stat.c, I suppose powerpc also needs this change, otherwise "perf kvm top" command can't sample guest records. But I have no any powerpc on my hand, so it's not tested on powerpc platform. Any test on powerpc is appreciated. Thanks.

#15 lore raw

Re: [PATCH 5/5] perf tools kvm: Use "cycles" to sample guest for "kvm top" on Intel

Quoted 58 lines · Dapeng
Quoted 57 lines · Dapeng Mi

As same reason with previous patch, use "cyles" instead of "cycles:P" event by default to sample guest for "perf kvm top" command on Intel platforms.

Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
  tools/perf/builtin-kvm.c | 30 +++++++++++++++++++++++++++++-
  1 file changed, 29 insertions(+), 1 deletion(-)
tools/perf/builtin-kvm.c +29 -1
index 7e48d2437710..d72b40f3df12 100644
@@ -2075,6 +2075,34 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  	return ret;
  }
 
+static int __cmd_top(int argc, const char **argv)
+{
+	int i = 0, ret;
+	const char **rec_argv;
+
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(argc + 2 + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
+	for (i = 0; i < argc; i++)
+		rec_argv[i] = argv[i];
+
+	BUG_ON(i != argc);
+
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
+	ret = cmd_top(i, rec_argv);
+	free(rec_argv);
+
+	return ret;
+}
+
  int cmd_kvm(int argc, const char **argv)
  {
  	const char *file_name = NULL;
@@ -2135,7 +2163,7 @@ int cmd_kvm(int argc, const char **argv)
  	else if (strlen(argv[0]) > 2 && strstarts("diff", argv[0]))
  		return cmd_diff(argc, argv);
  	else if (!strcmp(argv[0], "top"))
-		return cmd_top(argc, argv);
+		return __cmd_top(argc, argv);
  	else if (strlen(argv[0]) > 2 && strstarts("buildid-list", argv[0]))
Quoted 8 lines

tools/perf/builtin-kvm.c int cmd_kvm(int argc, const char **argv) · line 2169

Quoted 2 lines
tools/perf/builtin-kvm.c +0 -0
  		return __cmd_buildid_list(file_name, argc, argv);
  #if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)

This patch would impact powerpc platform as well. Base on the comments before kvm_add_default_arch_event() in tools/perf/arch/powerpc/util/kvm-stat.c, I suppose powerpc also needs this change, otherwise "perf kvm top" command can't sample guest records. But I have no any powerpc on my hand, so it's not tested on powerpc platform. Any test on powerpc is appreciated. Thanks.

I have powerpc systems . I will test can share the results .

Quoted 2 lines
author
#16 lore raw

Re: [PATCH 5/5] perf tools kvm: Use "cycles" to sample guest for "kvm top" on Intel

Quoted 67 lines · Aditya Bodkhe
Quoted 58 lines · Dapeng
Quoted 57 lines · Dapeng Mi

As same reason with previous patch, use "cyles" instead of "cycles:P" event by default to sample guest for "perf kvm top" command on Intel platforms.

Fixes 634d36f82517 ("perf record: Just use "cycles:P" as the default event")
---
  tools/perf/builtin-kvm.c | 30 +++++++++++++++++++++++++++++-
  1 file changed, 29 insertions(+), 1 deletion(-)
tools/perf/builtin-kvm.c +29 -1
index 7e48d2437710..d72b40f3df12 100644
@@ -2075,6 +2075,34 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  	return ret;
  }
 
+static int __cmd_top(int argc, const char **argv)
+{
+	int i = 0, ret;
+	const char **rec_argv;
+
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(argc + 2 + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
+	for (i = 0; i < argc; i++)
+		rec_argv[i] = argv[i];
+
+	BUG_ON(i != argc);
+
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
+	ret = cmd_top(i, rec_argv);
+	free(rec_argv);
+
+	return ret;
+}
+
  int cmd_kvm(int argc, const char **argv)
  {
  	const char *file_name = NULL;
@@ -2135,7 +2163,7 @@ int cmd_kvm(int argc, const char **argv)
  	else if (strlen(argv[0]) > 2 && strstarts("diff", argv[0]))
  		return cmd_diff(argc, argv);
  	else if (!strcmp(argv[0], "top"))
-		return cmd_top(argc, argv);
+		return __cmd_top(argc, argv);
  	else if (strlen(argv[0]) > 2 && strstarts("buildid-list", argv[0]))
Quoted 8 lines

tools/perf/builtin-kvm.c int cmd_kvm(int argc, const char **argv) · line 2169

Quoted 2 lines
tools/perf/builtin-kvm.c +0 -0
  		return __cmd_buildid_list(file_name, argc, argv);
  #if defined(HAVE_KVM_STAT_SUPPORT) && defined(HAVE_LIBTRACEEVENT)

This patch would impact powerpc platform as well. Base on the comments before kvm_add_default_arch_event() in tools/perf/arch/powerpc/util/kvm-stat.c, I suppose powerpc also needs this change, otherwise "perf kvm top" command can't sample guest records. But I have no any powerpc on my hand, so it's not tested on powerpc platform. Any test on powerpc is appreciated. Thanks.

Quoted 1 line

I have powerpc systems . I will test can share the results .

Thanks.

Quoted 1 line
Quoted 1 line