Index | Thread | Search

From:
Christian Ludwig <cludwig@genua.de>
Subject:
amd64/fpu: Avoid multiple FPU resets
To:
<tech@openbsd.org>
Date:
Fri, 13 Jun 2025 14:03:47 +0200

Download raw body.

Thread
Hi,

All kernel crypto code follows the scheme:

	for (objects) {
		fpu_kernel_enter();
		...
		fpu_kernel_exit();
	}

In every iteration, fpu_kernel_exit() resets the FPU state and
fpu_kernel_enter() resets it, again. FPU resets are expensive on some
platforms. Doing the operation twice per loop iteration is clearly not
necessary.

The FPU is always in one of two states when we reach fpu_kernel_enter().
It either holds user state when CPUPF_USERXSTATE is set or it is in
reset state already. The context switching code and signal code follow
this assumption, too. So we can simply drop resetting the FPU in
fpu_kernel_enter() when it does not hold user state.

I have tested it with a softraid/crypto disk. Regress passes, too.

More tests are welcome.


 - Christian

---
 sys/arch/amd64/amd64/fpu.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/sys/arch/amd64/amd64/fpu.c b/sys/arch/amd64/amd64/fpu.c
index b1a7192ba59b..32b0c251dc82 100644
--- a/sys/arch/amd64/amd64/fpu.c
+++ b/sys/arch/amd64/amd64/fpu.c
@@ -150,8 +150,6 @@ fpu_kernel_enter(void)
 	if (ci->ci_pflags & CPUPF_USERXSTATE) {
 		ci->ci_pflags &= ~CPUPF_USERXSTATE;
 		fpusavereset(&curproc->p_addr->u_pcb.pcb_savefpu);
-	} else {
-		fpureset();
 	}
 }
 
-- 
2.34.1