Skip to contentSkip to conversation

Perf kvm commands bug fix

PATCH Applied Dapeng Mi 4 participants 14 messages Aug 11, 2025 – Sep 19, 2025 perf events

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Dapeng Mi

author
#0 lore raw

v2 cover Perf kvm commands bug fix

his 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 5-6/6 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Changes:
v1 -> v2:
* Free memory allocated by strdup().
* Check "--pfm-events" in kvm_add_default_arch_event() as well.
* Opportunistically fix the missed memory allocation and free issue in builtin-kwork.
* Comments refine.

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 (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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 |  51 +++++++++++
 tools/perf/builtin-kvm.c            | 130 ++++++++++++++++++++--------
 tools/perf/builtin-kwork.c          |  27 ++++--
 tools/perf/util/env.c               |  22 +++++
 tools/perf/util/env.h               |   2 +
 tools/perf/util/kvm-stat.h          |  10 +++
 6 files changed, 203 insertions(+), 39 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/6 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 | 81 ++++++++++++++++++++++++++++------------
 1 file changed, 58 insertions(+), 23 deletions(-)
tools/perf/builtin-kvm.c +58 -23
index 7b15b4a705e4..9813f1eb56f9 100644
@@ -1638,9 +1638,11 @@ static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
 
 #define STRDUP_FAIL_EXIT(s)		\
 	({	char *_p;		\
-	_p = strdup(s);		\
-		if (!_p)		\
-			return -ENOMEM;	\
+		_p = strdup(s);		\
+		if (!_p) {		\
+			ret = -ENOMEM;	\
+			goto EXIT;	\
+		}			\
 		_p;			\
 	})
 
@@ -1688,7 +1690,7 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
 		rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
 
 	for (j = 0; j < events_tp_size; j++) {
-		rec_argv[i++] = "-e";
+		rec_argv[i++] = STRDUP_FAIL_EXIT("-e");
 		rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
 	}
 
@@ -1696,7 +1698,7 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
 	rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
-		rec_argv[i] = argv[j];
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[j]);
 
 	set_option_flag(record_options, 'e', "event", PARSE_OPT_HIDDEN);
 	set_option_flag(record_options, 0, "filter", PARSE_OPT_HIDDEN);
@@ -1719,7 +1721,13 @@ 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);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	free(rec_argv);
+	return ret;
 }
 
 static int
@@ -2006,52 +2014,79 @@ 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 *));
-	rec_argv[i++] = strdup("record");
-	rec_argv[i++] = strdup("-o");
-	rec_argv[i++] = strdup(file_name);
+	if (!rec_argv)
+		return -ENOMEM;
+
+	rec_argv[i++] = STRDUP_FAIL_EXIT("record");
+	rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
+	rec_argv[i++] = STRDUP_FAIL_EXIT(file_name);
 	for (j = 1; j < argc; j++, i++)
-		rec_argv[i] = argv[j];
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[j]);
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	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 *));
-	rec_argv[i++] = strdup("report");
-	rec_argv[i++] = strdup("-i");
-	rec_argv[i++] = strdup(file_name);
+	if (!rec_argv)
+		return -ENOMEM;
+
+	rec_argv[i++] = STRDUP_FAIL_EXIT("report");
+	rec_argv[i++] = STRDUP_FAIL_EXIT("-i");
+	rec_argv[i++] = STRDUP_FAIL_EXIT(file_name);
 	for (j = 1; j < argc; j++, i++)
-		rec_argv[i] = argv[j];
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[j]);
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_report(i, rec_argv);
+	ret = cmd_report(i, rec_argv);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	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 *));
-	rec_argv[i++] = strdup("buildid-list");
-	rec_argv[i++] = strdup("-i");
-	rec_argv[i++] = strdup(file_name);
+	if (!rec_argv)
+		return -ENOMEM;
+
+	rec_argv[i++] = STRDUP_FAIL_EXIT("buildid-list");
+	rec_argv[i++] = STRDUP_FAIL_EXIT("-i");
+	rec_argv[i++] = STRDUP_FAIL_EXIT(file_name);
 	for (j = 1; j < argc; j++, i++)
-		rec_argv[i] = argv[j];
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[j]);
 
 	BUG_ON(i != rec_argc);
 
-	return cmd_buildid_list(i, rec_argv);
+	ret = cmd_buildid_list(i, rec_argv);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	free(rec_argv);
+	return ret;
 }
 
 int cmd_kvm(int argc, const char **argv)
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/6 perf tools kwork: Add missed memory allocation check and free

In reply to Dapeng Mi

Same with previous builtin-kvm code, perf_kwork__record() doesn't check the memory allocation and explicitly free the allocated memory. Just fix it.

---
 tools/perf/builtin-kwork.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
tools/perf/builtin-kwork.c +22 -5
index d2e08de5976d..7f3068264568 100644
@@ -2273,12 +2273,23 @@ static void setup_event_list(struct perf_kwork *kwork,
 	pr_debug("\n");
 }
 
+#define STRDUP_FAIL_EXIT(s)		\
+	({	char *_p;		\
+		_p = strdup(s);		\
+		if (!_p) {		\
+			ret = -ENOMEM;	\
+			goto EXIT;	\
+		}			\
+		_p;			\
+	})
+
 static int perf_kwork__record(struct perf_kwork *kwork,
 			      int argc, const char **argv)
 {
 	const char **rec_argv;
 	unsigned int rec_argc, i, j;
 	struct kwork_class *class;
+	int ret;
 
 	const char *const record_args[] = {
 		"record",
@@ -2298,17 +2309,17 @@ static int perf_kwork__record(struct perf_kwork *kwork,
 		return -ENOMEM;
 
 	for (i = 0; i < ARRAY_SIZE(record_args); i++)
-		rec_argv[i] = strdup(record_args[i]);
+		rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
 
 	list_for_each_entry(class, &kwork->class_list, list) {
 		for (j = 0; j < class->nr_tracepoints; j++) {
-			rec_argv[i++] = strdup("-e");
-			rec_argv[i++] = strdup(class->tp_handlers[j].name);
+			rec_argv[i++] = STRDUP_FAIL_EXIT("-e");
+			rec_argv[i++] = STRDUP_FAIL_EXIT(class->tp_handlers[j].name);
 		}
 	}
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
-		rec_argv[i] = argv[j];
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[j]);
 
 	BUG_ON(i != rec_argc);
 
@@ -2317,7 +2328,13 @@ static int perf_kwork__record(struct perf_kwork *kwork,
 		pr_debug("%s ", rec_argv[j]);
 	pr_debug("\n");
 
-	return cmd_record(i, rec_argv);
+	ret = cmd_record(i, rec_argv);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	free(rec_argv);
+	return ret;
 }
 
 int cmd_kwork(int argc, const char **argv)
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/6 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 | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
tools/perf/builtin-kvm.c +10 -5
index 9813f1eb56f9..d297a7b2c088 100644
@@ -2008,11 +2008,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;
+	/*
+	 * Besides the 2 more options "-o" and "filename",
+	 * kvm_add_default_arch_event() may add 2 extra options,
+	 * so allocate 4 more items.
+	 */
+	rec_argc = argc + 2 + 2;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 	if (!rec_argv)
 		return -ENOMEM;
@@ -2025,6 +2026,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)
+		goto EXIT;
+
 	ret = cmd_record(i, rec_argv);
 
 EXIT:
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/6 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
#5 lore raw

PATCH v2 5/6 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 | 51 +++++++++++++++++++++++++++++
 tools/perf/builtin-kvm.c            | 10 ------
 tools/perf/util/kvm-stat.h          | 10 ++++++
 3 files changed, 61 insertions(+), 10 deletions(-)
tools/perf/arch/x86/util/kvm-stat.c +51 -0
index 424716518b75..bff36f9345ea 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,52 @@ 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 ret = 0, i, j = *argc;
+
+	const struct option event_options[] = {
+		OPT_BOOLEAN('e', "event", &event, NULL),
+		OPT_BOOLEAN(0, "pfm-events", &event, NULL),
+		OPT_END()
+	};
+
+	if (!x86__is_intel_cpu())
+		return 0;
+
+	tmp = calloc(j + 1, sizeof(char *));
+	if (!tmp)
+		return -ENOMEM;
+
+	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_FAIL_EXIT("-e");
+		argv[j++] = STRDUP_FAIL_EXIT("cycles");
+		*argc += 2;
+	}
+
+	free(tmp);
+	return 0;
+
+EXIT:
+	free(tmp);
+	return ret;
+}
tools/perf/builtin-kvm.c +0 -10
index d297a7b2c088..c0d62add4996 100644
@@ -1636,16 +1636,6 @@ static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
 	return ret;
 }
 
-#define STRDUP_FAIL_EXIT(s)		\
-	({	char *_p;		\
-		_p = strdup(s);		\
-		if (!_p) {		\
-			ret = -ENOMEM;	\
-			goto EXIT;	\
-		}			\
-		_p;			\
-	})
-
 int __weak setup_kvm_events_tp(struct perf_kvm_stat *kvm __maybe_unused)
 {
 	return 0;
tools/perf/util/kvm-stat.h +10 -0
index 4249542544bb..53db3d56108b 100644
@@ -190,5 +190,15 @@ static inline struct kvm_info *kvm_info__new(void)
 #define kvm_info__zput(ki) do { } while (0)
 #endif /* HAVE_KVM_STAT_SUPPORT */
 
+#define STRDUP_FAIL_EXIT(s)		\
+	({	char *_p;		\
+		_p = strdup(s);		\
+		if (!_p) {		\
+			ret = -ENOMEM;	\
+			goto EXIT;	\
+		}			\
+		_p;			\
+	})
+
 extern int kvm_add_default_arch_event(int *argc, const char **argv);
 #endif /* __PERF_KVM_STAT_H */
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 6/6 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 | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
tools/perf/builtin-kvm.c +33 -1
index c0d62add4996..f0f285763f19 100644
@@ -2084,6 +2084,38 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
 	return ret;
 }
 
+static int __cmd_top(int argc, const char **argv)
+{
+	int rec_argc, 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_argc = argc + 2;
+	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	if (!rec_argv)
+		return -ENOMEM;
+
+	for (i = 0; i < argc; i++)
+		rec_argv[i] = STRDUP_FAIL_EXIT(argv[i]);
+
+	BUG_ON(i != argc);
+
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		goto EXIT;
+
+	ret = cmd_top(i, rec_argv);
+
+EXIT:
+	for (i = 0; i < rec_argc; i++)
+		free((void *)rec_argv[i]);
+	free(rec_argv);
+	return ret;
+}
+
 int cmd_kvm(int argc, const char **argv)
 {
 	const char *file_name = NULL;
@@ -2144,7 +2176,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
maintainer Acked-by on the v2 series
#7 lore raw
In reply to Dapeng Mi
Quoted 39 lines · Dapeng Mi

his 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 5-6/6 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Changes:
v1 -> v2:
* Free memory allocated by strdup().
* Check "--pfm-events" in kvm_add_default_arch_event() as well.
* Opportunistically fix the missed memory allocation and free issue in builtin-kwork.
* Comments refine.

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]/

Quoted 7 lines

Dapeng Mi (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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

Thanks,
Namhyung

Quoted 14 lines
 tools/perf/arch/x86/util/kvm-stat.c |  51 +++++++++++
 tools/perf/builtin-kvm.c            | 130 ++++++++++++++++++++--------
 tools/perf/builtin-kwork.c          |  27 ++++--
 tools/perf/util/env.c               |  22 +++++
 tools/perf/util/env.h               |   2 +
 tools/perf/util/kvm-stat.h          |  10 +++
 6 files changed, 203 insertions(+), 39 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73

Signature · 1 line
2.34.1
author
#8 lore raw
Quoted 40 lines · Namhyung Kim
Quoted 39 lines · Dapeng Mi

his 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 5-6/6 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Changes:
v1 -> v2:
* Free memory allocated by strdup().
* Check "--pfm-events" in kvm_add_default_arch_event() as well.
* Opportunistically fix the missed memory allocation and free issue in builtin-kwork.
* Comments refine.

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]/

Quoted 8 lines
Quoted 7 lines

Dapeng Mi (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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

Namhyung, thanks for your ack. Do you think is it good to be queued or still need other acks?

Quoted 17 lines

Thanks,
Namhyung

Quoted 13 lines
 tools/perf/arch/x86/util/kvm-stat.c |  51 +++++++++++
 tools/perf/builtin-kvm.c            | 130 ++++++++++++++++++++--------
 tools/perf/builtin-kwork.c          |  27 ++++--
 tools/perf/util/env.c               |  22 +++++
 tools/perf/util/env.h               |   2 +
 tools/perf/util/kvm-stat.h          |  10 +++
 6 files changed, 203 insertions(+), 39 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73

Signature · 1 line
2.34.1
maintainer Applied
#9 lore raw
In reply to Namhyung Kim
Quoted 10 lines · Namhyung Kim
Quoted 7 lines · Dapeng Mi

Dapeng Mi (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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

Thanks, applied to perf-tools-next,

- Arnaldo

reviewer
#10 lore raw
In reply to Dapeng Mi
Quoted 10 lines · Dapeng Mi

his 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.

Huh? That change is:

```
$ git show 634d36f82517
commit 634d36f82517eb5c6a9b9ec7fe3ba19dbbcb7809
Author: Namhyung Kim <[email protected]>
Date:   Tue Oct 15 23:23:58 2024 -0700

    perf record: Just use "cycles:P" as the default event

    The fallback logic can add ":u" modifier if needed.
...
-               bool can_profile_kernel = perf_event_paranoid_check(1);
-
-               err = parse_event(rec->evlist, can_profile_kernel ?
"cycles:P" : "cycles:Pu");
+               err = parse_event(rec->evlist, "cycles:P");
...
```

isn't the precision the same before and after? I think you've blamed the wrong patch.

The change to use cycles:P looks to come from commit 7b100989b4f6 ("perf evlist: Remove __evlist__add_default") but the old code was doing things like "evsel->precise_max = true;" so I think I was just carrying forward behavior. The use of precise_max comes from commit 4e8a5c155137 ("perf evsel: Fix max perf_event_attr.precise_ip detection") from over 6 years ago, and the behavior before that also appears to have been to use the maximum supported precision.

Apart from the blame and commit message being off I think the change is okay, delta my usual complaint that weak symbols are the devil's work.

Thanks,
Ian

Quoted 50 lines

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 5-6/6 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Changes:
v1 -> v2:
* Free memory allocated by strdup().
* Check "--pfm-events" in kvm_add_default_arch_event() as well.
* Opportunistically fix the missed memory allocation and free issue in builtin-kwork.
* Comments refine.

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 (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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 |  51 +++++++++++
 tools/perf/builtin-kvm.c            | 130 ++++++++++++++++++++--------
 tools/perf/builtin-kwork.c          |  27 ++++--
 tools/perf/util/env.c               |  22 +++++
 tools/perf/util/env.h               |   2 +
 tools/perf/util/kvm-stat.h          |  10 +++
 6 files changed, 203 insertions(+), 39 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73 --
2.34.1

author
#11 lore raw
Quoted 39 lines · Ian Rogers
Quoted 9 lines · Dapeng Mi

his 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.

Huh? That change is:
```

$ git show 634d36f82517
commit 634d36f82517eb5c6a9b9ec7fe3ba19dbbcb7809
Author: Namhyung Kim <[email protected]>
Date:   Tue Oct 15 23:23:58 2024 -0700

perf record: Just use "cycles:P" as the default event

The fallback logic can add ":u" modifier if needed.
...

(quoted diff) +0 -3
-               bool can_profile_kernel = perf_event_paranoid_check(1);
-
-               err = parse_event(rec->evlist, can_profile_kernel ?

"cycles:P" : "cycles:Pu");
+ err = parse_event(rec->evlist, "cycles:P");
...
```
isn't the precision the same before and after? I think you've blamed the wrong patch.

The change to use cycles:P looks to come from commit 7b100989b4f6 ("perf evlist: Remove __evlist__add_default") but the old code was doing things like "evsel->precise_max = true;" so I think I was just carrying forward behavior. The use of precise_max comes from commit 4e8a5c155137 ("perf evsel: Fix max perf_event_attr.precise_ip detection") from over 6 years ago, and the behavior before that also appears to have been to use the maximum supported precision.

Quoted 3 lines

Apart from the blame and commit message being off I think the change is okay, delta my usual complaint that weak symbols are the devil's work.

Hmm, yeah, you're right. Thanks for correcting this.

Quoted 54 lines

Thanks,
Ian

Quoted 50 lines

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 5-6/6 fix this issue by using "cycles" event instead of PEBS event "cycles:P" to sample guest on Intel platforms.

Changes:
v1 -> v2:
* Free memory allocated by strdup().
* Check "--pfm-events" in kvm_add_default_arch_event() as well.
* Opportunistically fix the missed memory allocation and free issue in builtin-kwork.
* Comments refine.

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 (6):
perf tools kvm: Add missed memory allocation check and free perf tools kwork: 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 |  51 +++++++++++
 tools/perf/builtin-kvm.c            | 130 ++++++++++++++++++++--------
 tools/perf/builtin-kwork.c          |  27 ++++--
 tools/perf/util/env.c               |  22 +++++
 tools/perf/util/env.h               |   2 +
 tools/perf/util/kvm-stat.h          |  10 +++
 6 files changed, 203 insertions(+), 39 deletions(-)

base-commit: 6235ce77749f45cac27f630337e2fdf04e8a6c73 --
2.34.1

#12 lore raw
Quoted 40 lines · Dapeng
Quoted 39 lines · Ian Rogers
Quoted 9 lines · Dapeng Mi

his 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.

Huh? That change is:
```

$ git show 634d36f82517
commit 634d36f82517eb5c6a9b9ec7fe3ba19dbbcb7809
Author: Namhyung Kim <[email protected]>
Date:   Tue Oct 15 23:23:58 2024 -0700

perf record: Just use "cycles:P" as the default event

The fallback logic can add ":u" modifier if needed.
...

(quoted diff) +0 -3
-               bool can_profile_kernel = perf_event_paranoid_check(1);
-
-               err = parse_event(rec->evlist, can_profile_kernel ?

"cycles:P" : "cycles:Pu");
+ err = parse_event(rec->evlist, "cycles:P");
...
```
isn't the precision the same before and after? I think you've blamed the wrong patch.

The change to use cycles:P looks to come from commit 7b100989b4f6 ("perf evlist: Remove __evlist__add_default") but the old code was doing things like "evsel->precise_max = true;" so I think I was just carrying forward behavior. The use of precise_max comes from commit 4e8a5c155137 ("perf evsel: Fix max perf_event_attr.precise_ip detection") from over 6 years ago, and the behavior before that also appears to have been to use the maximum supported precision.

Quoted 5 lines
Quoted 3 lines

Apart from the blame and commit message being off I think the change is okay, delta my usual complaint that weak symbols are the devil's work.

Hmm, yeah, you're right. Thanks for correcting this.

Hi Dapeng,

Can you please fix the patch descriptions and Fixes references
and resubmit?

- Arnaldo

author
#13 lore raw
Quoted 41 lines · Arnaldo Carvalho de Melo
Quoted 40 lines · Dapeng
Quoted 39 lines · Ian Rogers
Quoted 9 lines · Dapeng Mi

his 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.

Huh? That change is:
```

$ git show 634d36f82517
commit 634d36f82517eb5c6a9b9ec7fe3ba19dbbcb7809
Author: Namhyung Kim <[email protected]>
Date:   Tue Oct 15 23:23:58 2024 -0700

perf record: Just use "cycles:P" as the default event

The fallback logic can add ":u" modifier if needed.
...

(quoted diff) +0 -3
-               bool can_profile_kernel = perf_event_paranoid_check(1);
-
-               err = parse_event(rec->evlist, can_profile_kernel ?

"cycles:P" : "cycles:Pu");
+ err = parse_event(rec->evlist, "cycles:P");
...
```
isn't the precision the same before and after? I think you've blamed the wrong patch.

The change to use cycles:P looks to come from commit 7b100989b4f6 ("perf evlist: Remove __evlist__add_default") but the old code was doing things like "evsel->precise_max = true;" so I think I was just carrying forward behavior. The use of precise_max comes from commit 4e8a5c155137 ("perf evsel: Fix max perf_event_attr.precise_ip detection") from over 6 years ago, and the behavior before that also appears to have been to use the maximum supported precision.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

Apart from the blame and commit message being off I think the change is okay, delta my usual complaint that weak symbols are the devil's work.

Hmm, yeah, you're right. Thanks for correcting this.

Hi Dapeng,

Can you please fix the patch descriptions and Fixes references
and resubmit?

Sure. would do. Thanks.

Quoted 2 lines

- Arnaldo