BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] docs/bpf: Document some special sdiv/smod operations
@ 2024-09-27  3:39 Yonghong Song
  2024-10-01  1:50 ` Alexei Starovoitov
  0 siblings, 1 reply; 19+ messages in thread
From: Yonghong Song @ 2024-09-27  3:39 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team,
	Martin KaFai Lau

Patch [1] fixed possible kernel crash due to specific sdiv/smod operations
in bpf program. The following are related operations and the expected results
of those operations:
  - LLONG_MIN/-1 = LLONG_MIN
  - INT_MIN/-1 = INT_MIN
  - LLONG_MIN%-1 = 0
  - INT_MIN%-1 = 0

Those operations are replaced with codes which won't cause
kernel crash. This patch documents what operations may cause exception and
what replacement operations are.

  [1] https://lore.kernel.org/all/20240913150326.1187788-1-yonghong.song@linux.dev/

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 .../bpf/standardization/instruction-set.rst   | 25 +++++++++++++++----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
index ab820d565052..d150c1d7ad3b 100644
--- a/Documentation/bpf/standardization/instruction-set.rst
+++ b/Documentation/bpf/standardization/instruction-set.rst
@@ -347,11 +347,26 @@ register.
   =====  =====  =======  ==========================================================
 
 Underflow and overflow are allowed during arithmetic operations, meaning
-the 64-bit or 32-bit value will wrap. If BPF program execution would
-result in division by zero, the destination register is instead set to zero.
-If execution would result in modulo by zero, for ``ALU64`` the value of
-the destination register is unchanged whereas for ``ALU`` the upper
-32 bits of the destination register are zeroed.
+the 64-bit or 32-bit value will wrap. There are also a few arithmetic operations
+which may cause exception for certain architectures. Since crashing the kernel
+is not an option, those operations are replaced with alternative operations.
+
+.. table:: Arithmetic operations with possible exceptions
+
+  =====  ==========  =============================  ==========================
+  name   class       original                       replacement
+  =====  ==========  =============================  ==========================
+  DIV    ALU64/ALU   dst /= 0                       dst = 0
+  SDIV   ALU64/ALU   dst s/= 0                      dst = 0
+  MOD    ALU64       dst %= 0                       dst = dst (no replacement)
+  MOD    ALU         dst %= 0                       dst = (u32)dst
+  SMOD   ALU64       dst s%= 0                      dst = dst (no replacement)
+  SMOD   ALU         dst s%= 0                      dst = (u32)dst
+  SDIV   ALU64       dst s/= -1 (dst = LLONG_MIN)   dst = LLONG_MIN
+  SDIV   ALU         dst s/= -1 (dst = INT_MIN)     dst = (u32)INT_MIN
+  SMOD   ALU64       dst s%= -1 (dst = LLONG_MIN)   dst = 0
+  SMOD   ALU         dst s%= -1 (dst = INT_MIN)     dst = 0
+  =====  ==========  =============================  ===========================
 
 ``{ADD, X, ALU}``, where 'code' = ``ADD``, 'source' = ``X``, and 'class' = ``ALU``, means::
 
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2024-11-08 20:34 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-27  3:39 [PATCH bpf-next] docs/bpf: Document some special sdiv/smod operations Yonghong Song
2024-10-01  1:50 ` Alexei Starovoitov
2024-10-01 15:48   ` Yonghong Song
2024-10-01 19:54     ` Dave Thaler
2024-10-01 19:54       ` [Bpf] " Dave Thaler
2024-10-02 20:13       ` Alexei Starovoitov
2024-10-02 20:13         ` [Bpf] " Alexei Starovoitov
2024-11-08  2:30         ` Dave Thaler
2024-11-08  2:30           ` [Bpf] " Dave Thaler
2024-11-08 18:38           ` Alexei Starovoitov
2024-11-08 18:38             ` [Bpf] " Alexei Starovoitov
2024-11-08 18:53             ` Dave Thaler
2024-11-08 18:53               ` [Bpf] " Dave Thaler
2024-11-08 19:00               ` Yonghong Song
2024-11-08 19:00                 ` [Bpf] " Yonghong Song
2024-11-08 20:34               ` Alexei Starovoitov
2024-11-08 20:34                 ` [Bpf] " Alexei Starovoitov
2024-10-04  5:28       ` Yonghong Song
2024-10-04  5:28         ` [Bpf] " Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox