Skip to contentSkip to conversation

x86/bpf: Avoid emitting LOCK prefix for XCHG atomic ops

PATCH Applied Uros Bizjak 2 participants 2 messages Dec 8, 2025 – Dec 22, 2025

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

PATCH 1/1 x86/bpf: Avoid emitting LOCK prefix for XCHG atomic ops

The x86 XCHG instruction is implicitly locked when one of the operands is a memory location, making an explicit LOCK prefix unnecessary.

Stop emitting the LOCK prefix for BPF_XCHG in the JIT atomic read-modify-write helpers. This avoids redundant instruction prefixes while preserving correct atomic semantics.

No functional change for other atomic operations.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/net/bpf_jit_comp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +5 -2
index b69dc7194e2c..77d724525808 100644
@@ -1305,7 +1305,8 @@ static int emit_atomic_rmw(u8 **pprog, u32 atomic_op,
 {
 	u8 *prog = *pprog;
 
-	EMIT1(0xF0); /* lock prefix */
+	if (atomic_op != BPF_XCHG)
+		EMIT1(0xF0); /* lock prefix */
 
 	maybe_emit_mod(&prog, dst_reg, src_reg, bpf_size == BPF_DW);
 
@@ -1347,7 +1348,9 @@ static int emit_atomic_rmw_index(u8 **pprog, u32 atomic_op, u32 size,
 {
 	u8 *prog = *pprog;
 
-	EMIT1(0xF0); /* lock prefix */
+	if (atomic_op != BPF_XCHG)
+		EMIT1(0xF0); /* lock prefix */
+
 	switch (size) {
 	case BPF_W:
 		EMIT1(add_3mod(0x40, dst_reg, src_reg, index_reg));
Signature · 1 line
2.52.0
#1 lore raw
Quoted 20 lines · Uros Bizjak

The x86 XCHG instruction is implicitly locked when one of the operands is a memory location, making an explicit LOCK prefix unnecessary.

Stop emitting the LOCK prefix for BPF_XCHG in the JIT atomic read-modify-write helpers. This avoids redundant instruction prefixes while preserving correct atomic semantics.

No functional change for other atomic operations.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Quoted 3 lines
---
 arch/x86/net/bpf_jit_comp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Applied to bpf-next.