Skip to contentSkip to conversation

perf/x86/intel/lbr: Zero the xstate buffer on allocation

PATCH Applied Thomas Gleixner 3 participants 3 messages Jun 11, 2021 – Jun 24, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/x86/intel/lbr: Zero the xstate buffer on allocation

XRSTORS requires a valid xstate buffer to work correctly. XSAVES does not guarantee to write a fully valid buffer according to the SDM:

"XSAVES does not write to any parts of the XSAVE header other than the
XSTATE_BV and XCOMP_BV fields."

XRSTORS triggers a #GP:

"If bytes 63:16 of the XSAVE header are not all zero."

It's dubious at best how this can work at all when the buffer is not zeroed before use.

Allocate the buffers with __GFP_ZERO to prevent XRSTORS failure.

Fixes ce711ea3cab9 ("perf/x86/intel/lbr: Support XSAVES/XRSTORS for LBR context switch")
Cc Kan Liang <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/events/intel/lbr.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +2 -1
@@ -731,7 +731,8 @@ void reserve_lbr_buffers(void)
 		if (!kmem_cache || cpuc->lbr_xsave)
 			continue;
 
-		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache, GFP_KERNEL,
+		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
+							GFP_KERNEL | __GFP_ZERO,
 							cpu_to_node(cpu));
 	}
 }
#1 lore raw
Quoted 6 lines · Thomas Gleixner

XRSTORS requires a valid xstate buffer to work correctly. XSAVES does not guarantee to write a fully valid buffer according to the SDM:

"XSAVES does not write to any parts of the XSAVE header other than the
XSTATE_BV and XCOMP_BV fields."

Quoted 7 lines

XRSTORS triggers a #GP:

"If bytes 63:16 of the XSAVE header are not all zero."

It's dubious at best how this can work at all when the buffer is not zeroed before use.

I didn't run into any issues when I did the test. I guess the reserved bits in the buffer may always happen to be 0. That's why it didn't set off the alarm to me. Thank you very much for the fix.

Thanks,
Kan

Quoted 24 lines

Allocate the buffers with __GFP_ZERO to prevent XRSTORS failure.

Fixes ce711ea3cab9 ("perf/x86/intel/lbr: Support XSAVES/XRSTORS for LBR context switch")
Cc Kan Liang <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
  arch/x86/events/intel/lbr.c |    3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +2 -1
@@ -731,7 +731,8 @@ void reserve_lbr_buffers(void)
  		if (!kmem_cache || cpuc->lbr_xsave)
  			continue;
 
-		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache, GFP_KERNEL,
+		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
+							GFP_KERNEL | __GFP_ZERO,
  							cpu_to_node(cpu));
  	}
  }
#2 lore raw
In reply to Thomas Gleixner
The following commit has been merged into the perf/urgent branch of tip:

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

Commit-ID:     7f049fbdd57f6ea71dc741d903c19c73b2f70950
Gitweb:        https://git.kernel.org/tip/7f049fbdd57f6ea71dc741d903c19c73b2f70950
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Fri, 11 Jun 2021 15:03:16 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 24 Jun 2021 08:49:03 +02:00

perf/x86/intel/lbr: Zero the xstate buffer on allocation

XRSTORS requires a valid xstate buffer to work correctly. XSAVES does not guarantee to write a fully valid buffer according to the SDM:

"XSAVES does not write to any parts of the XSAVE header other than the
XSTATE_BV and XCOMP_BV fields."

XRSTORS triggers a #GP:

"If bytes 63:16 of the XSAVE header are not all zero."

It's dubious at best how this can work at all when the buffer is not zeroed before use.

Allocate the buffers with __GFP_ZERO to prevent XRSTORS failure.

Fixes ce711ea3cab9 ("perf/x86/intel/lbr: Support XSAVES/XRSTORS for LBR context switch")
---
 arch/x86/events/intel/lbr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +2 -1
index 4409d2c..e8453de 100644
@@ -731,7 +731,8 @@ void reserve_lbr_buffers(void)
 		if (!kmem_cache || cpuc->lbr_xsave)
 			continue;
 
-		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache, GFP_KERNEL,
+		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
+							GFP_KERNEL | __GFP_ZERO,
 							cpu_to_node(cpu));
 	}
 }