Skip to content

Commit de94857

Browse files
bjackmanAlexei Starovoitov
authored andcommitted
bpf: Document new atomic instructions
Document new atomic instructions. Signed-off-by: Brendan Jackman <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 98d666d commit de94857

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Documentation/networking/filter.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,39 @@ encoding.
10531053
.imm = BPF_ADD, .code = BPF_ATOMIC | BPF_W | BPF_STX: lock xadd *(u32 *)(dst_reg + off16) += src_reg
10541054
.imm = BPF_ADD, .code = BPF_ATOMIC | BPF_DW | BPF_STX: lock xadd *(u64 *)(dst_reg + off16) += src_reg
10551055
1056+
The basic atomic operations supported are:
1057+
1058+
BPF_ADD
1059+
BPF_AND
1060+
BPF_OR
1061+
BPF_XOR
1062+
1063+
Each having equivalent semantics with the ``BPF_ADD`` example, that is: the
1064+
memory location addresed by ``dst_reg + off`` is atomically modified, with
1065+
``src_reg`` as the other operand. If the ``BPF_FETCH`` flag is set in the
1066+
immediate, then these operations also overwrite ``src_reg`` with the
1067+
value that was in memory before it was modified.
1068+
1069+
The more special operations are:
1070+
1071+
BPF_XCHG
1072+
1073+
This atomically exchanges ``src_reg`` with the value addressed by ``dst_reg +
1074+
off``.
1075+
1076+
BPF_CMPXCHG
1077+
1078+
This atomically compares the value addressed by ``dst_reg + off`` with
1079+
``R0``. If they match it is replaced with ``src_reg``, The value that was there
1080+
before is loaded back to ``R0``.
1081+
10561082
Note that 1 and 2 byte atomic operations are not supported.
10571083

1084+
Except ``BPF_ADD`` _without_ ``BPF_FETCH`` (for legacy reasons), all 4 byte
1085+
atomic operations require alu32 mode. Clang enables this mode by default in
1086+
architecture v3 (``-mcpu=v3``). For older versions it can be enabled with
1087+
``-Xclang -target-feature -Xclang +alu32``.
1088+
10581089
You may encounter BPF_XADD - this is a legacy name for BPF_ATOMIC, referring to
10591090
the exclusive-add operation encoded when the immediate field is zero.
10601091

0 commit comments

Comments
 (0)