Skip to content

Commit 8819495

Browse files
dthalerborkmann
authored andcommitted
bpf, docs: Shift operations are defined to use a mask
Update the documentation regarding shift operations to explain the use of a mask, since otherwise shifting by a value out of range (like negative) is undefined. Signed-off-by: Dave Thaler <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 2a36c26 commit 8819495

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Documentation/bpf/instruction-set.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ BPF_MUL 0x20 dst \*= src
163163
BPF_DIV 0x30 dst = (src != 0) ? (dst / src) : 0
164164
BPF_OR 0x40 dst \|= src
165165
BPF_AND 0x50 dst &= src
166-
BPF_LSH 0x60 dst <<= src
167-
BPF_RSH 0x70 dst >>= src
166+
BPF_LSH 0x60 dst <<= (src & mask)
167+
BPF_RSH 0x70 dst >>= (src & mask)
168168
BPF_NEG 0x80 dst = ~src
169169
BPF_MOD 0x90 dst = (src != 0) ? (dst % src) : dst
170170
BPF_XOR 0xa0 dst ^= src
171171
BPF_MOV 0xb0 dst = src
172-
BPF_ARSH 0xc0 sign extending shift right
172+
BPF_ARSH 0xc0 sign extending dst >>= (src & mask)
173173
BPF_END 0xd0 byte swap operations (see `Byte swap instructions`_ below)
174174
======== ===== ==========================================================
175175

@@ -204,6 +204,9 @@ for ``BPF_ALU64``, 'imm' is first sign extended to 64 bits and the result
204204
interpreted as an unsigned 64-bit value. There are no instructions for
205205
signed division or modulo.
206206

207+
Shift operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31)
208+
for 32-bit operations.
209+
207210
Byte swap instructions
208211
~~~~~~~~~~~~~~~~~~~~~~
209212

0 commit comments

Comments
 (0)