Skip to contentSkip to conversation

bpf,x64: Remove unnecessary check on existence of SSE2

PATCH Superseded Jie Meng 3 participants 7 messages Oct 3, 2022 – Oct 7, 2022

Subsystems (3)
lore
Share & tools
lore

Jie Meng

author
#0 lore raw

PATCH 1/1 bpf,x64: Remove unnecessary check on existence of SSE2

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU.

---
 arch/x86/net/bpf_jit_comp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +1 -2
index d09c54f3d2e0..b2124521305e 100644
@@ -1289,8 +1289,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
 
 			/* speculation barrier */
 		case BPF_ST | BPF_NOSPEC:
-			if (boot_cpu_has(X86_FEATURE_XMM2))
-				EMIT_LFENCE();
+			EMIT_LFENCE();
 			break;
 
 			/* ST: *(u8*)(dst_reg + off) = imm */
Signature · 1 line
2.30.2
#1 lore raw
Quoted 3 lines · Jie Meng

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU.

Why do you say this?

The Instruction set reference does mention that:

Exceptions:

#UD If CPUID.01H:EDX.SSE2[bit 26] = 0

(undefined instruction when the CPUID.SSE2 bit is unset)

and also that the CPUID feature flag is SSE2

Quoted 23 lines
---
 arch/x86/net/bpf_jit_comp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +1 -3
index d09c54f3d2e0..b2124521305e 100644
@@ -1289,8 +1289,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
 
                        /* speculation barrier */
                case BPF_ST | BPF_NOSPEC:
-                       if (boot_cpu_has(X86_FEATURE_XMM2))
-                               EMIT_LFENCE();
+                       EMIT_LFENCE();
                        break;
 
                        /* ST: *(u8*)(dst_reg + off) = imm */
--

2.30.2

author
#2 lore raw
Quoted 9 lines · KP Singh
Quoted 3 lines · Jie Meng

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU.

Why do you say this?

The Instruction set reference does mention that:

Quoted 7 lines

Exceptions:

#UD If CPUID.01H:EDX.SSE2[bit 26] = 0

(undefined instruction when the CPUID.SSE2 bit is unset)

and also that the CPUID feature flag is SSE2

Many x86 extensions predate x86-64. When they designed x86-64, AMD decided to make some mandatory (and hence architectural) and SSE2 is one of them[1]. CMOV, NOPL, PAE, NX etc. are other examples.

These extensions' CPUID flags are still set. If code is to be shared between x86 and x86-64 one can still check CPUID, but bpf_jit_comp.c is compiled under x86-64 only so the check is redundant.

There's an example Within kernel code too: arch/x86/lib/copy_user_64.S uses SSE (sfence) and SSE2 (movnti) instructions and doesn't check CPUID for their presence.

---
[1] https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels

Quoted 23 lines
Quoted 23 lines
---
 arch/x86/net/bpf_jit_comp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +1 -3
index d09c54f3d2e0..b2124521305e 100644
@@ -1289,8 +1289,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
 
                        /* speculation barrier */
                case BPF_ST | BPF_NOSPEC:
-                       if (boot_cpu_has(X86_FEATURE_XMM2))
-                               EMIT_LFENCE();
+                       EMIT_LFENCE();
                        break;
 
                        /* ST: *(u8*)(dst_reg + off) = imm */
--

2.30.2

#3 lore raw
Quoted 23 lines · Jie Meng
Quoted 9 lines · KP Singh
Quoted 3 lines · Jie Meng

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU.

Why do you say this?

The Instruction set reference does mention that:

Quoted 7 lines

Exceptions:

#UD If CPUID.01H:EDX.SSE2[bit 26] = 0

(undefined instruction when the CPUID.SSE2 bit is unset)

and also that the CPUID feature flag is SSE2

Many x86 extensions predate x86-64. When they designed x86-64, AMD decided to make some mandatory (and hence architectural) and SSE2 is one of them[1]. CMOV, NOPL, PAE, NX etc. are other examples.

Quoted 8 lines

These extensions' CPUID flags are still set. If code is to be shared between x86 and x86-64 one can still check CPUID, but bpf_jit_comp.c is compiled under x86-64 only so the check is redundant.

There's an example Within kernel code too: arch/x86/lib/copy_user_64.S uses SSE (sfence) and SSE2 (movnti) instructions and doesn't check CPUID for their presence.

Thanks, it makes sense.

Can you please add the explanation to the commit description?

Quoted 26 lines

---
[1] https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels

Quoted 23 lines
Quoted 23 lines
---
 arch/x86/net/bpf_jit_comp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +1 -3
index d09c54f3d2e0..b2124521305e 100644
@@ -1289,8 +1289,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
 
                        /* speculation barrier */
                case BPF_ST | BPF_NOSPEC:
-                       if (boot_cpu_has(X86_FEATURE_XMM2))
-                               EMIT_LFENCE();
+                       EMIT_LFENCE();
                        break;
 
                        /* ST: *(u8*)(dst_reg + off) = imm */
--

2.30.2

author
#4 lore raw

PATCH v2 1/1 bpf,x64: Remove unnecessary check on existence of SSE2

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU. SSE2's CPUID flag is still set to maintain backward compatibility with older code or code shared with x86, but bpf_jit_comp.c is compiled under x86-64 exclusively so the check is redundant.

---
 arch/x86/net/bpf_jit_comp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +1 -2
index d09c54f3d2e0..b2124521305e 100644
@@ -1289,8 +1289,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
 
 			/* speculation barrier */
 		case BPF_ST | BPF_NOSPEC:
-			if (boot_cpu_has(X86_FEATURE_XMM2))
-				EMIT_LFENCE();
+			EMIT_LFENCE();
 			break;
 
 			/* ST: *(u8*)(dst_reg + off) = imm */
Signature · 1 line
2.30.2
Acked-by on v2 1/1
#5 lore raw
Quoted 8 lines · Jie Meng

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU. SSE2's CPUID flag is still set to maintain backward compatibility with older code or code shared with x86, but bpf_jit_comp.c is compiled under x86-64 exclusively so the check is redundant.

#6 lore raw
In reply to Jie Meng
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Wed, 5 Oct 2022 10:00:39 -0700 you wrote:

SSE2 and hence lfence are architectural in x86-64 and no need to check whether they're supported in CPU. SSE2's CPUID flag is still set to maintain backward compatibility with older code or code shared with x86, but bpf_jit_comp.c is compiled under x86-64 exclusively so the check is redundant.

[...]

Here is the summary with links:
- [bpf-next,v2] bpf,x64: Remove unnecessary check on existence of SSE2

    https://git.kernel.org/bpf/bpf-next/c/2e30960097f6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html