Skip to content

Commit da9d35e

Browse files
Björn TöpelAlexei Starovoitov
authored andcommitted
samples/bpf: Add BPF_ATOMIC_OP macro for BPF samples
Brendan Jackman added extend atomic operations to the BPF instruction set in commit 7064a73 ("Merge branch 'Atomics for eBPF'"), which introduces the BPF_ATOMIC_OP macro. However, that macro was missing for the BPF samples. Fix that by adding it into bpf_insn.h. Fixes: 91c960b ("bpf: Rename BPF_XADD and prepare to encode other atomics in .imm") Signed-off-by: Björn Töpel <bjorn.topel@intel.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Brendan Jackman <jackmanb@google.com> Link: https://lore.kernel.org/bpf/20210118091753.107572-1-bjorn.topel@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 89f479f commit da9d35e

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

‎samples/bpf/bpf_insn.h‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,31 @@ struct bpf_insn;
134134
.off = OFF, \
135135
.imm = 0 })
136136

137-
/* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
138-
139-
#define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
137+
/*
138+
* Atomic operations:
139+
*
140+
* BPF_ADD *(uint *) (dst_reg + off16) += src_reg
141+
* BPF_AND *(uint *) (dst_reg + off16) &= src_reg
142+
* BPF_OR *(uint *) (dst_reg + off16) |= src_reg
143+
* BPF_XOR *(uint *) (dst_reg + off16) ^= src_reg
144+
* BPF_ADD | BPF_FETCH src_reg = atomic_fetch_add(dst_reg + off16, src_reg);
145+
* BPF_AND | BPF_FETCH src_reg = atomic_fetch_and(dst_reg + off16, src_reg);
146+
* BPF_OR | BPF_FETCH src_reg = atomic_fetch_or(dst_reg + off16, src_reg);
147+
* BPF_XOR | BPF_FETCH src_reg = atomic_fetch_xor(dst_reg + off16, src_reg);
148+
* BPF_XCHG src_reg = atomic_xchg(dst_reg + off16, src_reg)
149+
* BPF_CMPXCHG r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg)
150+
*/
151+
152+
#define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF) \
140153
((struct bpf_insn) { \
141154
.code = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC, \
142155
.dst_reg = DST, \
143156
.src_reg = SRC, \
144157
.off = OFF, \
145-
.imm = BPF_ADD })
158+
.imm = OP })
159+
160+
/* Legacy alias */
161+
#define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF)
146162

147163
/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
148164

0 commit comments

Comments
 (0)