[Patch v2 0/6] Perf kvm commands bug fix

Dapeng Mi posted 6 patches 4 months ago
There is a newer version of this series
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(-)
[Patch v2 0/6] Perf kvm commands bug fix
Posted by Dapeng Mi 4 months ago
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/20220411101946.20262-1-likexu@tencent.com/


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
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Ian Rogers 2 months, 4 weeks ago
On Sun, Aug 10, 2025 at 10:56 PM Dapeng Mi <dapeng1.mi@linux.intel.com> wrote:
>
> 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 <namhyung@kernel.org>
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

> 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/20220411101946.20262-1-likexu@tencent.com/
>
>
> 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
>
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Mi, Dapeng 2 months, 4 weeks ago
On 9/18/2025 5:12 AM, Ian Rogers wrote:
> On Sun, Aug 10, 2025 at 10:56 PM Dapeng Mi <dapeng1.mi@linux.intel.com> wrote:
>> 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 <namhyung@kernel.org>
> 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.

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


>
> Thanks,
> Ian
>
>> 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/20220411101946.20262-1-likexu@tencent.com/
>>
>>
>> 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
>>
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Arnaldo Carvalho de Melo 2 months, 3 weeks ago
On Thu, Sep 18, 2025 at 07:52:43AM +0800, Mi, Dapeng wrote:
> On 9/18/2025 5:12 AM, Ian Rogers wrote:
> > On Sun, Aug 10, 2025 at 10:56 PM Dapeng Mi <dapeng1.mi@linux.intel.com> wrote:
> >> 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 <namhyung@kernel.org>
> > 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.
> 
> Hmm, yeah, you're right. Thanks for correcting this. 

Hi Dapeng,

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

- Arnaldo
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Mi, Dapeng 2 months, 3 weeks ago
On 9/19/2025 3:59 AM, Arnaldo Carvalho de Melo wrote:
> On Thu, Sep 18, 2025 at 07:52:43AM +0800, Mi, Dapeng wrote:
>> On 9/18/2025 5:12 AM, Ian Rogers wrote:
>>> On Sun, Aug 10, 2025 at 10:56 PM Dapeng Mi <dapeng1.mi@linux.intel.com> wrote:
>>>> 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 <namhyung@kernel.org>
>>> 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.
>> 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.


>
> - Arnaldo
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Namhyung Kim 4 months ago
On Mon, Aug 11, 2025 at 01:55:40PM +0800, Dapeng Mi wrote:
> 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/20220411101946.20262-1-likexu@tencent.com/
> 
> 
> 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

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung

> 
>  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
>
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Arnaldo Carvalho de Melo 2 months, 4 weeks ago
On Fri, Aug 15, 2025 at 01:15:21PM -0700, Namhyung Kim wrote:
> On Mon, Aug 11, 2025 at 01:55:40PM +0800, Dapeng Mi wrote:
> > 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
 
> Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks, applied to perf-tools-next,

- Arnaldo
Re: [Patch v2 0/6] Perf kvm commands bug fix
Posted by Mi, Dapeng 3 months, 1 week ago
On 8/16/2025 4:15 AM, Namhyung Kim wrote:
> On Mon, Aug 11, 2025 at 01:55:40PM +0800, Dapeng Mi wrote:
>> 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/20220411101946.20262-1-likexu@tencent.com/
>>
>>
>> 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
> Acked-by: Namhyung Kim <namhyung@kernel.org>

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


>
> Thanks,
> Namhyung
>
>>  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
>>