[PATCH 07/10] perf tools: Separate exclude_hv fallback

Namhyung Kim posted 10 patches 4 months, 1 week ago
[PATCH 07/10] perf tools: Separate exclude_hv fallback
Posted by Namhyung Kim 4 months, 1 week ago
The exclude_hv was added in the evsel__fallback() in the commit
4ec8d984895fef43a ("perf record: Fix priv level with branch sampling
for paranoid=2") to address branch stack samples on Intel PMUs.
As some other PMUs might not support that, let's separate the bit from
exclude_kernel to make sure it can add the bit only if required.

Technically it should change the modifier string at the end of the
event name.  ":u" is for exclude_kernel + exclude_hv, so it should be
":uh" if it has exclude_kernel only.  That means the default events for
regular users will looks like "cycles:Puh" (for perf record) or
"instructions:uh" (for perf stat).  But I'm not sure if it's worth the
trouble so I didn't touch the name in this patch.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/evsel.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 1a4f52767942e5ad..c5df45bb74dfc1b5 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3389,10 +3389,20 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
 		free(evsel->name);
 		evsel->name = new_name;
 		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
-			  "to fall back to excluding kernel and hypervisor "
-			  " samples", paranoid);
+			  "to fall back to excluding kernel samples", paranoid);
 		evsel->core.attr.exclude_kernel = 1;
-		evsel->core.attr.exclude_hv     = 1;
+
+		return true;
+	} else if (err == EACCES && !evsel->core.attr.exclude_hv &&
+		   (paranoid = perf_event_paranoid()) > 1) {
+		/* If event has exclude user then don't exclude hv. */
+		if (evsel->core.attr.exclude_user)
+			return false;
+
+		/* Intel branch stack requires exclude_hv */
+		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
+			  "to fall back to excluding hypervisor samples", paranoid);
+		evsel->core.attr.exclude_hv = 1;
 
 		return true;
 	} else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&
-- 
2.46.0.469.g59c65b2a67-goog
Re: [PATCH 07/10] perf tools: Separate exclude_hv fallback
Posted by Liang, Kan 4 months, 1 week ago

On 2024-09-05 4:24 p.m., Namhyung Kim wrote:
> The exclude_hv was added in the evsel__fallback() in the commit
> 4ec8d984895fef43a ("perf record: Fix priv level with branch sampling
> for paranoid=2") to address branch stack samples on Intel PMUs.
> As some other PMUs might not support that, let's separate the bit from
> exclude_kernel to make sure it can add the bit only if required.
> 
> Technically it should change the modifier string at the end of the
> event name.  ":u" is for exclude_kernel + exclude_hv, so it should be
> ":uh" if it has exclude_kernel only.  That means the default events for
> regular users will looks like "cycles:Puh" (for perf record) or
> "instructions:uh" (for perf stat).  But I'm not sure if it's worth the
> trouble so I didn't touch the name in this patch.
> 
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/util/evsel.c | 16 +++++++++++++---
>  1 file changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 1a4f52767942e5ad..c5df45bb74dfc1b5 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -3389,10 +3389,20 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
>  		free(evsel->name);
>  		evsel->name = new_name;
>  		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
> -			  "to fall back to excluding kernel and hypervisor "
> -			  " samples", paranoid);
> +			  "to fall back to excluding kernel samples", paranoid);
>  		evsel->core.attr.exclude_kernel = 1;
> -		evsel->core.attr.exclude_hv     = 1;
> +
> +		return true;
> +	} else if (err == EACCES && !evsel->core.attr.exclude_hv &&
> +		   (paranoid = perf_event_paranoid()) > 1) {
> +		/* If event has exclude user then don't exclude hv. */
> +		if (evsel->core.attr.exclude_user)
> +			return false;
> +
> +		/* Intel branch stack requires exclude_hv */

I don't think it's an requirement for Intel branch stack. The HV is
ignored for all X86.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/events/core.c#n542

I think it's a generic request for branch on all arch.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/events/core.c#n366

Thanks,
Kan

> +		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
> +			  "to fall back to excluding hypervisor samples", paranoid);
> +		evsel->core.attr.exclude_hv = 1;
>  
>  		return true;
>  	} else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&
Re: [PATCH 07/10] perf tools: Separate exclude_hv fallback
Posted by Namhyung Kim 3 months, 2 weeks ago
On Fri, Sep 06, 2024 at 11:21:21AM -0400, Liang, Kan wrote:
> 
> 
> On 2024-09-05 4:24 p.m., Namhyung Kim wrote:
> > The exclude_hv was added in the evsel__fallback() in the commit
> > 4ec8d984895fef43a ("perf record: Fix priv level with branch sampling
> > for paranoid=2") to address branch stack samples on Intel PMUs.
> > As some other PMUs might not support that, let's separate the bit from
> > exclude_kernel to make sure it can add the bit only if required.
> > 
> > Technically it should change the modifier string at the end of the
> > event name.  ":u" is for exclude_kernel + exclude_hv, so it should be
> > ":uh" if it has exclude_kernel only.  That means the default events for
> > regular users will looks like "cycles:Puh" (for perf record) or
> > "instructions:uh" (for perf stat).  But I'm not sure if it's worth the
> > trouble so I didn't touch the name in this patch.
> > 
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> >  tools/perf/util/evsel.c | 16 +++++++++++++---
> >  1 file changed, 13 insertions(+), 3 deletions(-)
> > 
> > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > index 1a4f52767942e5ad..c5df45bb74dfc1b5 100644
> > --- a/tools/perf/util/evsel.c
> > +++ b/tools/perf/util/evsel.c
> > @@ -3389,10 +3389,20 @@ bool evsel__fallback(struct evsel *evsel, struct target *target, int err,
> >  		free(evsel->name);
> >  		evsel->name = new_name;
> >  		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
> > -			  "to fall back to excluding kernel and hypervisor "
> > -			  " samples", paranoid);
> > +			  "to fall back to excluding kernel samples", paranoid);
> >  		evsel->core.attr.exclude_kernel = 1;
> > -		evsel->core.attr.exclude_hv     = 1;
> > +
> > +		return true;
> > +	} else if (err == EACCES && !evsel->core.attr.exclude_hv &&
> > +		   (paranoid = perf_event_paranoid()) > 1) {
> > +		/* If event has exclude user then don't exclude hv. */
> > +		if (evsel->core.attr.exclude_user)
> > +			return false;
> > +
> > +		/* Intel branch stack requires exclude_hv */
> 
> I don't think it's an requirement for Intel branch stack. The HV is
> ignored for all X86.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/events/core.c#n542
> 
> I think it's a generic request for branch on all arch.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/events/core.c#n366

Ok, I'll update the comment.

Thanks,
Namhyung

> 
> > +		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
> > +			  "to fall back to excluding hypervisor samples", paranoid);
> > +		evsel->core.attr.exclude_hv = 1;
> >  
> >  		return true;
> >  	} else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest &&