public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] x86/pkeys: Remove unnecessary XGETBV1 dependency in signal frame exposition
@ 2025-02-14  1:06 Chang S. Bae
  2025-02-14  1:06 ` [PATCH 1/2] x86/fpu: Refactor xfeature bitmask update code for sigframe XSAVE Chang S. Bae
  2025-02-14  1:06 ` [PATCH 2/2] x86/pkeys: Simplify PKRU update in signal frame Chang S. Bae
  0 siblings, 2 replies; 3+ messages in thread
From: Chang S. Bae @ 2025-02-14  1:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, aruna.ramakrishna,
	TonyWWang-oc, chang.seok.bae

Hi all,

This series removes an unnecessary dependency on XGETBV1 which was
brought by commit

    ae6012d72fa6 ("x86/pkeys: Ensure updated PKRU value is XRSTOR'd").

Currently, the PKRU exposure logic overwrites xregs_state->header
->xfeatures in the signal frame, seemingly to emulate XSAVE behavior:
combining the requested feature bitmap with a processor-tracked status
bitmap derived from XGETBV(1).

A previous posting [1] attempted to handle the case where XGETBV(1) is
disabled or unexposed. However, that approach assumed this dependency was
necessary and further modified the logic to skip PKRU writes in such
cases, which appears problematic.

Instead, the XSAVE-written value can be read directly from userspace
memory, as is already done for legacy states [2]. Thus, tying PKRU
exposure in the ABI to XGETBV1 is excessive. This series refactors
reusable code into a helper and applies it to PKRU handling, fully
eliminating the XGETBV1 dependency.

Thanks,
Chang

[1]: https://lore.kernel.org/lkml/20250102075419.2559-1-TonyWWang-oc@zhaoxin.com/
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/signal.c#n139

Chang S. Bae (2):
  x86/fpu: Refactor xfeature bitmask update code for sigframe XSAVE
  x86/pkeys: Simplify PKRU update in signal frame

 arch/x86/kernel/fpu/signal.c | 11 +----------
 arch/x86/kernel/fpu/xstate.h | 21 +++++++++++++++------
 2 files changed, 16 insertions(+), 16 deletions(-)


base-commit: b36de8b904b8ff2095ece7af6b3cfff8c73c2fb1
-- 
2.45.2


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

* [PATCH 1/2] x86/fpu: Refactor xfeature bitmask update code for sigframe XSAVE
  2025-02-14  1:06 [PATCH 0/2] x86/pkeys: Remove unnecessary XGETBV1 dependency in signal frame exposition Chang S. Bae
@ 2025-02-14  1:06 ` Chang S. Bae
  2025-02-14  1:06 ` [PATCH 2/2] x86/pkeys: Simplify PKRU update in signal frame Chang S. Bae
  1 sibling, 0 replies; 3+ messages in thread
From: Chang S. Bae @ 2025-02-14  1:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, aruna.ramakrishna,
	TonyWWang-oc, chang.seok.bae

Currently, saving register states in the signal frame, the legacy feature
bits are always set in xregs_state->header->xfeatures. This code sequence
can be generalized for reuse in similar cases.

Refactor the logic to ensure a consistent approach across similar usages.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
---
 arch/x86/kernel/fpu/signal.c | 11 +----------
 arch/x86/kernel/fpu/xstate.h | 12 ++++++++++++
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index 8f62e0666dea..8abe60919e2f 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -119,7 +119,6 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame,
 {
 	struct xregs_state __user *x = buf;
 	struct _fpx_sw_bytes sw_bytes = {};
-	u32 xfeatures;
 	int err;
 
 	/* Setup the bytes not touched by the [f]xsave and reserved for SW. */
@@ -132,12 +131,6 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame,
 	err |= __put_user(FP_XSTATE_MAGIC2,
 			  (__u32 __user *)(buf + fpstate->user_size));
 
-	/*
-	 * Read the xfeatures which we copied (directly from the cpu or
-	 * from the state in task struct) to the user buffers.
-	 */
-	err |= __get_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
-
 	/*
 	 * For legacy compatible, we always set FP/SSE bits in the bit
 	 * vector while saving the state to the user context. This will
@@ -149,9 +142,7 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame,
 	 * header as well as change any contents in the memory layout.
 	 * xrestore as part of sigreturn will capture all the changes.
 	 */
-	xfeatures |= XFEATURE_MASK_FPSSE;
-
-	err |= __put_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
+	err |= set_xfeature_in_sigframe(x, XFEATURE_MASK_FPSSE);
 
 	return !err;
 }
diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h
index aa16f1a1bbcf..27e20236bbbb 100644
--- a/arch/x86/kernel/fpu/xstate.h
+++ b/arch/x86/kernel/fpu/xstate.h
@@ -69,6 +69,18 @@ static inline u64 xfeatures_mask_independent(void)
 	return fpu_kernel_cfg.independent_features;
 }
 
+static inline int set_xfeature_in_sigframe(struct xregs_state __user *xbuf, u64 mask)
+{
+	u64 xfeatures;
+	int err;
+
+	/* Read the xfeatures value already saved in the user buffer */
+	err  = __get_user(xfeatures, &xbuf->header.xfeatures);
+	xfeatures |= mask;
+	err |= __put_user(xfeatures, &xbuf->header.xfeatures);
+	return err;
+}
+
 /*
  * Update the value of PKRU register that was already pushed onto the signal frame.
  */
-- 
2.45.2


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

* [PATCH 2/2] x86/pkeys: Simplify PKRU update in signal frame
  2025-02-14  1:06 [PATCH 0/2] x86/pkeys: Remove unnecessary XGETBV1 dependency in signal frame exposition Chang S. Bae
  2025-02-14  1:06 ` [PATCH 1/2] x86/fpu: Refactor xfeature bitmask update code for sigframe XSAVE Chang S. Bae
@ 2025-02-14  1:06 ` Chang S. Bae
  1 sibling, 0 replies; 3+ messages in thread
From: Chang S. Bae @ 2025-02-14  1:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, aruna.ramakrishna,
	TonyWWang-oc, chang.seok.bae

Commit ae6012d72fa6 ("x86/pkeys: Ensure updated PKRU value is XRSTOR'd")
modified the signal delivery logic to always set the PKRU bit in
xregs_state->header->xfeatures.

The change currently derives the bitmask value using XGETBV(1), rather
than reading the already written value directly from the buffer. Thus,
this approach induces an unnecessary dependency on XGETBV1 for the PKRU
feature.

Eliminate the dependency by using the established helper function.
Subsequently, remove the unused mask argument from
update_pkru_in_sigframe().

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
Cc: Tony W Wang-oc <TonyWWang-oc@zhaoxin.com>
---
 arch/x86/kernel/fpu/xstate.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h
index 27e20236bbbb..f3fc3369a135 100644
--- a/arch/x86/kernel/fpu/xstate.h
+++ b/arch/x86/kernel/fpu/xstate.h
@@ -84,18 +84,15 @@ static inline int set_xfeature_in_sigframe(struct xregs_state __user *xbuf, u64
 /*
  * Update the value of PKRU register that was already pushed onto the signal frame.
  */
-static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u64 mask, u32 pkru)
+static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u32 pkru)
 {
-	u64 xstate_bv;
 	int err;
 
 	if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE)))
 		return 0;
 
 	/* Mark PKRU as in-use so that it is restored correctly. */
-	xstate_bv = (mask & xfeatures_in_use()) | XFEATURE_MASK_PKRU;
-
-	err =  __put_user(xstate_bv, &buf->header.xfeatures);
+	err = set_xfeature_in_sigframe(buf, XFEATURE_MASK_PKRU);
 	if (err)
 		return err;
 
@@ -316,7 +313,7 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf, u32 pkr
 	clac();
 
 	if (!err)
-		err = update_pkru_in_sigframe(buf, mask, pkru);
+		err = update_pkru_in_sigframe(buf, pkru);
 
 	return err;
 }
-- 
2.45.2


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

end of thread, other threads:[~2025-02-14  1:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-14  1:06 [PATCH 0/2] x86/pkeys: Remove unnecessary XGETBV1 dependency in signal frame exposition Chang S. Bae
2025-02-14  1:06 ` [PATCH 1/2] x86/fpu: Refactor xfeature bitmask update code for sigframe XSAVE Chang S. Bae
2025-02-14  1:06 ` [PATCH 2/2] x86/pkeys: Simplify PKRU update in signal frame Chang S. Bae

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox