linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] perf/x86/lbr: Enable the branch type for the Arch LBR by default
@ 2022-08-16 12:56 kan.liang
  2022-08-16 12:56 ` [PATCH 2/2] perf docs: Update the documentation for the save_type filter kan.liang
  2022-08-22  9:00 ` [tip: perf/urgent] perf/x86/lbr: Enable the branch type for the Arch LBR by default tip-bot2 for Kan Liang
  0 siblings, 2 replies; 4+ messages in thread
From: kan.liang @ 2022-08-16 12:56 UTC (permalink / raw)
  To: peterz, acme, linux-kernel
  Cc: eranian, ak, namhyung, irogers, Kan Liang, stable

From: Kan Liang <kan.liang@linux.intel.com>

On the platform with Arch LBR, the HW raw branch type encoding may leak
to the perf tool when the SAVE_TYPE option is not set.

In the intel_pmu_store_lbr(), the HW raw branch type is stored in
lbr_entries[].type. If the SAVE_TYPE option is set, the
lbr_entries[].type will be converted into the generic PERF_BR_* type
in the intel_pmu_lbr_filter() and exposed to the user tools.
But if the SAVE_TYPE option is NOT set by the user, the current perf
kernel doesn't clear the field. The HW raw branch type leaks.

There are two solutions to fix the issue for the Arch LBR.
One is to clear the field if the SAVE_TYPE option is NOT set.
The other solution is to unconditionally convert the branch type and
expose the generic type to the user tools.

The latter is implemented here, because
- The branch type is valuable information. I don't see a case where
  you would not benefit from the branch type. (Stephane Eranian)
- Not having the branch type DOES NOT save any space in the
  branch record (Stephane Eranian)
- The Arch LBR HW can retrieve the common branch types from the
  LBR_INFO. It doesn't require the high overhead SW disassemble.

Fixes: 47125db27e47 ("perf/x86/intel/lbr: Support Architectural LBR")
Reported-by: Stephane Eranian <eranian@google.com>
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Cc: stable@vger.kernel.org
---
 arch/x86/events/intel/lbr.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index a4a36d482b21..247d36825474 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -1149,6 +1149,14 @@ static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
 
 	if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
 		reg->config = mask;
+
+		/*
+		 * The Arch LBR HW can retrieve the common branch types
+		 * from the LBR_INFO. It doesn't require the high overhead
+		 * SW disassemble.
+		 * Enable the branch type by default for the Arch LBR.
+		 */
+		reg->reg |= X86_BR_TYPE_SAVE;
 		return 0;
 	}
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] perf docs: Update the documentation for the save_type filter
  2022-08-16 12:56 [PATCH 1/2] perf/x86/lbr: Enable the branch type for the Arch LBR by default kan.liang
@ 2022-08-16 12:56 ` kan.liang
  2022-08-25 13:23   ` Liang, Kan
  2022-08-22  9:00 ` [tip: perf/urgent] perf/x86/lbr: Enable the branch type for the Arch LBR by default tip-bot2 for Kan Liang
  1 sibling, 1 reply; 4+ messages in thread
From: kan.liang @ 2022-08-16 12:56 UTC (permalink / raw)
  To: peterz, acme, linux-kernel; +Cc: eranian, ak, namhyung, irogers, Kan Liang

From: Kan Liang <kan.liang@linux.intel.com>

Update the documentation to reflect the kernel changes.

Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
---
 tools/perf/Documentation/perf-record.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 099817ef5150..d5c57ac1ee0a 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -397,6 +397,9 @@ following filters are defined:
 	- abort_tx: only when the target is a hardware transaction abort
 	- cond: conditional branches
 	- save_type: save branch type during sampling in case binary is not available later
+		     For the platforms with Intel Arch LBR support (12th-Gen+ client or
+		     4th-Gen Xeon+ server), the save branch type is unconditionally enabled
+		     when the taken branch stack sampling is enabled.
 
 +
 The option requires at least one branch type among any, any_call, any_ret, ind_call, cond.
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [tip: perf/urgent] perf/x86/lbr: Enable the branch type for the Arch LBR by default
  2022-08-16 12:56 [PATCH 1/2] perf/x86/lbr: Enable the branch type for the Arch LBR by default kan.liang
  2022-08-16 12:56 ` [PATCH 2/2] perf docs: Update the documentation for the save_type filter kan.liang
@ 2022-08-22  9:00 ` tip-bot2 for Kan Liang
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot2 for Kan Liang @ 2022-08-22  9:00 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Stephane Eranian, Kan Liang, Peter Zijlstra (Intel),
	stable, x86, linux-kernel

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     32ba156df1b1c8804a4e5be5339616945eafea22
Gitweb:        https://git.kernel.org/tip/32ba156df1b1c8804a4e5be5339616945eafea22
Author:        Kan Liang <kan.liang@linux.intel.com>
AuthorDate:    Tue, 16 Aug 2022 05:56:11 -07:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 19 Aug 2022 19:47:30 +02:00

perf/x86/lbr: Enable the branch type for the Arch LBR by default

On the platform with Arch LBR, the HW raw branch type encoding may leak
to the perf tool when the SAVE_TYPE option is not set.

In the intel_pmu_store_lbr(), the HW raw branch type is stored in
lbr_entries[].type. If the SAVE_TYPE option is set, the
lbr_entries[].type will be converted into the generic PERF_BR_* type
in the intel_pmu_lbr_filter() and exposed to the user tools.
But if the SAVE_TYPE option is NOT set by the user, the current perf
kernel doesn't clear the field. The HW raw branch type leaks.

There are two solutions to fix the issue for the Arch LBR.
One is to clear the field if the SAVE_TYPE option is NOT set.
The other solution is to unconditionally convert the branch type and
expose the generic type to the user tools.

The latter is implemented here, because
- The branch type is valuable information. I don't see a case where
  you would not benefit from the branch type. (Stephane Eranian)
- Not having the branch type DOES NOT save any space in the
  branch record (Stephane Eranian)
- The Arch LBR HW can retrieve the common branch types from the
  LBR_INFO. It doesn't require the high overhead SW disassemble.

Fixes: 47125db27e47 ("perf/x86/intel/lbr: Support Architectural LBR")
Reported-by: Stephane Eranian <eranian@google.com>
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/20220816125612.2042397-1-kan.liang@linux.intel.com
---
 arch/x86/events/intel/lbr.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index 4f70fb6..47fca6a 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -1097,6 +1097,14 @@ static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
 
 	if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
 		reg->config = mask;
+
+		/*
+		 * The Arch LBR HW can retrieve the common branch types
+		 * from the LBR_INFO. It doesn't require the high overhead
+		 * SW disassemble.
+		 * Enable the branch type by default for the Arch LBR.
+		 */
+		reg->reg |= X86_BR_TYPE_SAVE;
 		return 0;
 	}
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] perf docs: Update the documentation for the save_type filter
  2022-08-16 12:56 ` [PATCH 2/2] perf docs: Update the documentation for the save_type filter kan.liang
@ 2022-08-25 13:23   ` Liang, Kan
  0 siblings, 0 replies; 4+ messages in thread
From: Liang, Kan @ 2022-08-25 13:23 UTC (permalink / raw)
  To: acme, linux-kernel; +Cc: eranian, ak, namhyung, irogers, Peter Zijlstra

Hi Arnaldo,

The kernel change has been merged into the tip perf/urgent branch.

Do you have any comments for the patch?
If it's OK for you, could you please pull this patch?

Thanks,
Kan

On 2022-08-16 8:56 a.m., kan.liang@linux.intel.com wrote:
> From: Kan Liang <kan.liang@linux.intel.com>
> 
> Update the documentation to reflect the kernel changes.
> 
> Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
> ---
>  tools/perf/Documentation/perf-record.txt | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> index 099817ef5150..d5c57ac1ee0a 100644
> --- a/tools/perf/Documentation/perf-record.txt
> +++ b/tools/perf/Documentation/perf-record.txt
> @@ -397,6 +397,9 @@ following filters are defined:
>  	- abort_tx: only when the target is a hardware transaction abort
>  	- cond: conditional branches
>  	- save_type: save branch type during sampling in case binary is not available later
> +		     For the platforms with Intel Arch LBR support (12th-Gen+ client or
> +		     4th-Gen Xeon+ server), the save branch type is unconditionally enabled
> +		     when the taken branch stack sampling is enabled.
>  
>  +
>  The option requires at least one branch type among any, any_call, any_ret, ind_call, cond.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-25 13:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 12:56 [PATCH 1/2] perf/x86/lbr: Enable the branch type for the Arch LBR by default kan.liang
2022-08-16 12:56 ` [PATCH 2/2] perf docs: Update the documentation for the save_type filter kan.liang
2022-08-25 13:23   ` Liang, Kan
2022-08-22  9:00 ` [tip: perf/urgent] perf/x86/lbr: Enable the branch type for the Arch LBR by default tip-bot2 for Kan Liang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).