@@ -1053,8 +1053,39 @@ encoding.
1053
1053
.imm = BPF_ADD, .code = BPF_ATOMIC | BPF_W | BPF_STX: lock xadd *(u32 *)(dst_reg + off16) += src_reg
1054
1054
.imm = BPF_ADD, .code = BPF_ATOMIC | BPF_DW | BPF_STX: lock xadd *(u64 *)(dst_reg + off16) += src_reg
1055
1055
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
+
1056
1082
Note that 1 and 2 byte atomic operations are not supported.
1057
1083
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
+
1058
1089
You may encounter BPF_XADD - this is a legacy name for BPF_ATOMIC, referring to
1059
1090
the exclusive-add operation encoded when the immediate field is zero.
1060
1091
0 commit comments