Skip to contentSkip to conversation

LoongArch: BPF: Remove fatal signed-extension instructions

PATCH In review Hengqi Chen 4 participants 9 messages Dec 4, 2023 – Dec 6, 2023

Subsystems (3)
lore
Share & tools
lore

Hengqi Chen

author reviewer
#0 lore raw

PATCH 1/1 LoongArch: BPF: Remove fatal signed-extension instructions

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing
    Can't find bpf_testmod.ko kernel module: -2
    WARNING! Selftests relying on bpf_testmod.ko will be skipped.
    [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
    [   47.843498] Oops[#1]:
    [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
    [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
    [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
    [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
    [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
    [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
    [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
    [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
    [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
    [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
    [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
    [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
    [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
    [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
    [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
    [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
    [   47.853706]  BADV: ffffffff9218d000
    [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
    [   47.854733] Modules linked in:
    [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
    [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
    [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
    [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
    [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
    [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
    [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
    [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
    [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
    [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
    [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
    [   47.862077]         ...
    [   47.862311] Call Trace:
    [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
    [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
    [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
    [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
    [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
    [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
    [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
    [   47.866373]
    [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
    [   47.867381]
    [   47.867749] ---[ end trace 0000000000000000 ]---
    [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
    [   47.870138] Kernel relocated by 0x31c0000
    [   47.870868]  .text @ 0x90000000033c0000
    [   47.871361]  .data @ 0x9000000004690000
    [   47.871777]  .bss  @ 0x9000000005a6e000
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
    				   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

[0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c

Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
 arch/loongarch/net/bpf_jit.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +2 -10
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
 	/* function return */
 	case BPF_JMP | BPF_EXIT:
-		emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
 		if (i == ctx->prog->len - 1)
 			break;
 
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 			}
 			break;
 		case BPF_DW:
-			if (is_signed_imm12(off)) {
-				emit_insn(ctx, ldd, dst, src, off);
-			} else if (is_signed_imm14(off)) {
-				emit_insn(ctx, ldptrd, dst, src, off);
-			} else {
-				move_imm(ctx, t1, off, is32);
-				emit_insn(ctx, ldxd, dst, src, t1);
-			}
+			move_imm(ctx, t1, off, is32);
+			emit_insn(ctx, ldxd, dst, src, t1);
 			break;
 		}
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 108 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

     # ./test_progs -t verifier_map_ptr_mixing
     Can't find bpf_testmod.ko kernel module: -2
     WARNING! Selftests relying on bpf_testmod.ko will be skipped.
     [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
     [   47.843498] Oops[#1]:
     [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
     [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
     [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
     [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
     [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
     [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
     [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
     [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
     [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
     [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
     [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
     [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
     [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
     [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
     [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
     [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
     [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
     [   47.853706]  BADV: ffffffff9218d000
     [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
     [   47.854733] Modules linked in:
     [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
     [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
     [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
     [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
     [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
     [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
     [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
     [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
     [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
     [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
     [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
     [   47.862077]         ...
     [   47.862311] Call Trace:
     [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
     [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
     [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
     [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
     [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
     [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
     [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
     [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
     [   47.866373]
     [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
     [   47.867381]
     [   47.867749] ---[ end trace 0000000000000000 ]---
     [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
     [   47.870138] Kernel relocated by 0x31c0000
     [   47.870868]  .text @ 0x90000000033c0000
     [   47.871361]  .data @ 0x9000000004690000
     [   47.871777]  .bss  @ 0x9000000005a6e000
     [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

     cls_redirect()
       -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

     ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
     				   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

   [0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c
Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
  arch/loongarch/net/bpf_jit.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +0 -2
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
  	/* function return */
  	case BPF_JMP | BPF_EXIT:
-		emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
  		if (i == ctx->prog->len - 1)
  			break;
 

arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 994

Quoted 7 lines
arch/loongarch/net/bpf_jit.c +0 -4
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
  			}
  			break;
  		case BPF_DW:
-			if (is_signed_imm12(off)) {
-				emit_insn(ctx, ldd, dst, src, off);
-			} else if (is_signed_imm14(off)) {
-				emit_insn(ctx, ldptrd, dst, src, off);

I suspect there are some bugs with other ldptr.{w,d} and stptr.{w.d}. Because their offsets are 4-bytes aligned 16-bit signed numbers.

[1] https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#_ldptr_wd_stptr_wd

Thanks.

Quoted 9 lines
arch/loongarch/net/bpf_jit.c +2 -4
-			} else {
-				move_imm(ctx, t1, off, is32);
-				emit_insn(ctx, ldxd, dst, src, t1);
-			}
+			move_imm(ctx, t1, off, is32);
+			emit_insn(ctx, ldxd, dst, src, t1);
  			break;
  		}
 
author reviewer
#2 lore raw
Quoted 122 lines · Jinyang He
Quoted 108 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

     # ./test_progs -t verifier_map_ptr_mixing
     Can't find bpf_testmod.ko kernel module: -2
     WARNING! Selftests relying on bpf_testmod.ko will be skipped.
     [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
     [   47.843498] Oops[#1]:
     [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
     [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
     [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
     [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
     [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
     [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
     [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
     [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
     [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
     [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
     [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
     [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
     [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
     [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
     [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
     [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
     [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
     [   47.853706]  BADV: ffffffff9218d000
     [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
     [   47.854733] Modules linked in:
     [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
     [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
     [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
     [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
     [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
     [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
     [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
     [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
     [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
     [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
     [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
     [   47.862077]         ...
     [   47.862311] Call Trace:
     [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
     [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
     [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
     [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
     [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
     [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
     [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
     [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
     [   47.866373]
     [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
     [   47.867381]
     [   47.867749] ---[ end trace 0000000000000000 ]---
     [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
     [   47.870138] Kernel relocated by 0x31c0000
     [   47.870868]  .text @ 0x90000000033c0000
     [   47.871361]  .data @ 0x9000000004690000
     [   47.871777]  .bss  @ 0x9000000005a6e000
     [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

     cls_redirect()
       -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

     ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
                                 BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

   [0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c
Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
  arch/loongarch/net/bpf_jit.c | 12 ++----------
  1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +0 -2
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
      /* function return */
      case BPF_JMP | BPF_EXIT:
-             emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
              if (i == ctx->prog->len - 1)
                      break;
 

arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 994

Quoted 7 lines
arch/loongarch/net/bpf_jit.c +0 -4
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
                      }
                      break;
              case BPF_DW:
-                     if (is_signed_imm12(off)) {
-                             emit_insn(ctx, ldd, dst, src, off);
-                     } else if (is_signed_imm14(off)) {
-                             emit_insn(ctx, ldptrd, dst, src, off);

I suspect there are some bugs with other ldptr.{w,d} and stptr.{w.d}. Because their offsets are 4-bytes aligned 16-bit signed numbers.

Quoted 4 lines

[1]
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#_ldptr_wd_stptr_wd

Probably, but I am inclined to keep it as it is for now since there are no issues exposed.

Quoted 12 lines

Thanks.

Quoted 9 lines
arch/loongarch/net/bpf_jit.c +2 -4
-                     } else {
-                             move_imm(ctx, t1, off, is32);
-                             emit_insn(ctx, ldxd, dst, src, t1);
-                     }
+                     move_imm(ctx, t1, off, is32);
+                     emit_insn(ctx, ldxd, dst, src, t1);
                      break;
              }
 
maintainer
#3 lore raw
In reply to Hengqi Chen

Hi, Hengqi,

Why privileged bpf programs have no problem?

Huacai

Quoted 128 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing
    Can't find bpf_testmod.ko kernel module: -2
    WARNING! Selftests relying on bpf_testmod.ko will be skipped.
    [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
    [   47.843498] Oops[#1]:
    [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
    [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
    [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
    [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
    [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
    [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
    [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
    [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
    [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
    [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
    [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
    [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
    [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
    [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
    [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
    [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
    [   47.853706]  BADV: ffffffff9218d000
    [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
    [   47.854733] Modules linked in:
    [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
    [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
    [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
    [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
    [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
    [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
    [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
    [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
    [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
    [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
    [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
    [   47.862077]         ...
    [   47.862311] Call Trace:
    [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
    [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
    [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
    [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
    [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
    [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
    [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
    [   47.866373]
    [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
    [   47.867381]
    [   47.867749] ---[ end trace 0000000000000000 ]---
    [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
    [   47.870138] Kernel relocated by 0x31c0000
    [   47.870868]  .text @ 0x90000000033c0000
    [   47.871361]  .data @ 0x9000000004690000
    [   47.871777]  .bss  @ 0x9000000005a6e000
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
                                   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

[0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c

Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
 arch/loongarch/net/bpf_jit.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +2 -11
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
        /* function return */
        case BPF_JMP | BPF_EXIT:
-               emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
                if (i == ctx->prog->len - 1)
                        break;
 
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
                        }
                        break;
                case BPF_DW:
-                       if (is_signed_imm12(off)) {
-                               emit_insn(ctx, ldd, dst, src, off);
-                       } else if (is_signed_imm14(off)) {
-                               emit_insn(ctx, ldptrd, dst, src, off);
-                       } else {
-                               move_imm(ctx, t1, off, is32);
-                               emit_insn(ctx, ldxd, dst, src, t1);
-                       }
+                       move_imm(ctx, t1, off, is32);
+                       emit_insn(ctx, ldxd, dst, src, t1);
                        break;
                }
 
--

2.34.1

author reviewer
#4 lore raw
Quoted 5 lines · Huacai Chen

Hi, Hengqi,

Why privileged bpf programs have no problem?

I guess you mean the verifier_movsx test, but this patch does NOT fix it. This patch is intended to fix cls_redirect, sockopt_inherit and verifier_map_ptr_mixing tests.

Quoted 131 lines

Huacai

Quoted 128 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing
    Can't find bpf_testmod.ko kernel module: -2
    WARNING! Selftests relying on bpf_testmod.ko will be skipped.
    [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
    [   47.843498] Oops[#1]:
    [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
    [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
    [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
    [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
    [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
    [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
    [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
    [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
    [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
    [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
    [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
    [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
    [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
    [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
    [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
    [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
    [   47.853706]  BADV: ffffffff9218d000
    [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
    [   47.854733] Modules linked in:
    [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
    [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
    [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
    [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
    [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
    [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
    [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
    [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
    [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
    [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
    [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
    [   47.862077]         ...
    [   47.862311] Call Trace:
    [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
    [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
    [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
    [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
    [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
    [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
    [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
    [   47.866373]
    [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
    [   47.867381]
    [   47.867749] ---[ end trace 0000000000000000 ]---
    [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
    [   47.870138] Kernel relocated by 0x31c0000
    [   47.870868]  .text @ 0x90000000033c0000
    [   47.871361]  .data @ 0x9000000004690000
    [   47.871777]  .bss  @ 0x9000000005a6e000
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
                                   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

[0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c

Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
 arch/loongarch/net/bpf_jit.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +2 -11
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
        /* function return */
        case BPF_JMP | BPF_EXIT:
-               emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
                if (i == ctx->prog->len - 1)
                        break;
 
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
                        }
                        break;
                case BPF_DW:
-                       if (is_signed_imm12(off)) {
-                               emit_insn(ctx, ldd, dst, src, off);
-                       } else if (is_signed_imm14(off)) {
-                               emit_insn(ctx, ldptrd, dst, src, off);
-                       } else {
-                               move_imm(ctx, t1, off, is32);
-                               emit_insn(ctx, ldxd, dst, src, t1);
-                       }
+                       move_imm(ctx, t1, off, is32);
+                       emit_insn(ctx, ldxd, dst, src, t1);
                        break;
                }
 
--

2.34.1

maintainer
#5 lore raw
Quoted 11 lines · Hengqi Chen
Quoted 5 lines · Huacai Chen

Hi, Hengqi,

Why privileged bpf programs have no problem?

I guess you mean the verifier_movsx test, but this patch does NOT fix it. This patch is intended to fix cls_redirect, sockopt_inherit and verifier_map_ptr_mixing tests.

Yes, when you replied to me in another email, I thought you got the root cause of verifier_movsx. So that problem still has no fix by now?

Huacai

Quoted 133 lines
Quoted 132 lines

Huacai

Quoted 128 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing
    Can't find bpf_testmod.ko kernel module: -2
    WARNING! Selftests relying on bpf_testmod.ko will be skipped.
    [   47.842279] CPU 0 Unable to handle kernel paging request at virtual address ffffffff9218d000, era == 9000000003573ff8, ra == ffff8000020dbc70
    [   47.843498] Oops[#1]:
    [   47.843870] CPU: 0 PID: 242 Comm: test_progs Not tainted 6.7.0-rc2+ #27
    [   47.844308] Hardware name: QEMU QEMU Virtual Machine, BIOS unknown 2/2/2022
    [   47.844850] pc 9000000003573ff8 ra ffff8000020dbc70 tp 9000000097618000 sp 900000009761bad0
    [   47.845484] a0 ffffffff9218d000 a1 900000009761bad8 a2 0000000000000001 a3 900000009761bc20
    [   47.846097] a4 900000009761bc24 a5 ffffffff9218d000 a6 0000000000000021 a7 0000000000000118
    [   47.846643] t0 ffff8000020dbbd8 t1 9000000003573ff8 t2 900000009230dd00 t3 000c65d400000000
    [   47.847220] t4 ffffffffffffffff t5 000000012ea823f8 t6 0000000000000000 t7 0000000000000000
    [   47.847753] t8 000000000005b900 u0 900000009761ba88 s9 900000009761bb20 s0 ffff80001b278000
    [   47.848259] s1 0000000000000001 s2 900000009230dd00 s3 900000009761bc20 s4 900000009761bae0
    [   47.848796] s5 0000000000000000 s6 900000000438be20 s7 900000009761bb54 s8 9000000005b967c0
    [   47.849400]    ra: ffff8000020dbc70 bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.850543]   ERA: 9000000003573ff8 bpf_map_lookup_elem+0x0/0xc
    [   47.851180]  CRMD: 000000b0 (PLV0 -IE -DA +PG DACF=CC DACM=CC -WE)
    [   47.852203]  PRMD: 00000004 (PPLV0 +PIE -PWE)
    [   47.852592]  EUEN: 00000003 (+FPE +SXE -ASXE -BTE)
    [   47.852964]  ECFG: 00071c1c (LIE=2-4,10-12 VS=7)
    [   47.853351] ESTAT: 00010000 [PIL] (IS= ECode=1 EsubCode=0)
    [   47.853706]  BADV: ffffffff9218d000
    [   47.854113]  PRID: 0014c010 (Loongson-64bit, Loongson-3A5000)
    [   47.854733] Modules linked in:
    [   47.855261] Process test_progs (pid: 242, threadinfo=0000000071b66eac, task=00000000aa177492)
    [   47.856344] Stack : 900000009761bb58 0000000000000000 0000000000000000 900000009761bc24
    [   47.857347]         900000009761bc20 900000009230dd00 0000000000000001 ffff80001b278000
    [   47.858064]         900000009761bb80 900000000438becc 0000000000000000 900000009761bb68
    [   47.858691]         ffff80001b278048 9000000005254000 90000000042ec12c 0000000000000000
    [   47.859332]         0000000000000000 900000009761bb80 0000000000000000 0000000000000001
    [   47.859832]         0000000b230f0ab8 0000000000000000 ffff80001b278000 0000000000000000
    [   47.860268]         0000000000000000 8dfb8454e0eb5b83 ffff80001b278000 9000000005b967c0
    [   47.860701]         9000000093c80000 ffff80001b278000 0000000000000040 900000009230dd00
    [   47.861190]         900000009393f400 9000000097863400 900000009761bd88 0000000000000000
    [   47.861650]         9000000005254000 900000000438d100 0000000000000001 0000000000000001
    [   47.862077]         ...
    [   47.862311] Call Trace:
    [   47.862568] [<9000000003573ff8>] bpf_map_lookup_elem+0x0/0xc
    [   47.863511] [<ffff8000020dbc70>] bpf_prog_4ecec27f4a4ae8c7_pointers_for_lookup_hash_array+0x98/0xe4
    [   47.864150] [<900000000438becc>] bpf_test_run+0x1e4/0x2f0
    [   47.864544] [<900000000438d100>] bpf_prog_test_run_skb+0x314/0x67c
    [   47.864934] [<900000000354cd18>] __sys_bpf+0x514/0x26d8
    [   47.865261] [<900000000354f4e8>] sys_bpf+0x18/0x28
    [   47.865596] [<90000000045b6c3c>] do_syscall+0x7c/0x94
    [   47.865932] [<90000000033e1b1c>] handle_syscall+0xbc/0x158
    [   47.866373]
    [   47.866549] Code: 00150317  53fe67ff  542c6841 <2600008c> 28c1818c  4c000180  2600008c  28c1a18c  4c000180
    [   47.867381]
    [   47.867749] ---[ end trace 0000000000000000 ]---
    [   47.868514] Kernel panic - not syncing: Fatal exception in interrupt
    [   47.870138] Kernel relocated by 0x31c0000
    [   47.870868]  .text @ 0x90000000033c0000
    [   47.871361]  .data @ 0x9000000004690000
    [   47.871777]  .bss  @ 0x9000000005a6e000
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
                                   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand. Finally, we got the wrong addresses and result in kernel panics.

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

[0]: https://lore.kernel.org/loongarch/CAEyhmHT6UOryPZi7eEfw01Y_CjAqxy8NM6eKkdvdojJqB9P7eQ@mail.gmail.com/T/#m78408fe3db883f384406e30e6b2b1f1e3fd57c1c

Fixes 5dc615520c4d ("LoongArch: Add BPF JIT support")
---
 arch/loongarch/net/bpf_jit.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
arch/loongarch/net/bpf_jit.c +2 -11
index 169ff8b3915e..d76281efc5b8 100644
@@ -911,8 +911,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
        /* function return */
        case BPF_JMP | BPF_EXIT:
-               emit_sext_32(ctx, regmap[BPF_REG_0], true);
-
                if (i == ctx->prog->len - 1)
                        break;
 
@@ -988,14 +986,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
                        }
                        break;
                case BPF_DW:
-                       if (is_signed_imm12(off)) {
-                               emit_insn(ctx, ldd, dst, src, off);
-                       } else if (is_signed_imm14(off)) {
-                               emit_insn(ctx, ldptrd, dst, src, off);
-                       } else {
-                               move_imm(ctx, t1, off, is32);
-                               emit_insn(ctx, ldxd, dst, src, t1);
-                       }
+                       move_imm(ctx, t1, off, is32);
+                       emit_insn(ctx, ldxd, dst, src, t1);
                        break;
                }
 
--

2.34.1

maintainer
#6 lore raw
Quoted 6 lines · Huacai Chen
Quoted 5 lines · Hengqi Chen
Quoted 3 lines · Huacai Chen

Hi, Hengqi,

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why privileged bpf programs have no problem?

I guess you mean the verifier_movsx test, but this patch does NOT fix it. This patch is intended to fix cls_redirect, sockopt_inherit and verifier_map_ptr_mixing tests.

Yes, when you replied to me in another email, I thought you got the root cause of verifier_movsx. So that problem still has no fix by now?

I spent some time to analysis and have fixed the failed test case about verifier_movsx, I will submit a patch after internal review and more test, maybe at the end of this week or at the beginning of next week.

Here are the test results with the unsubmitted local patch:

[root@linux bpf]# echo 1 > /proc/sys/net/core/bpf_jit_enable [root@linux bpf]# echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled [root@linux bpf]# ./test_progs -t verifier_movsx

#361/1   verifier_movsx/MOV32SX, S8:OK
#361/2   verifier_movsx/MOV32SX, S8 @unpriv:OK
#361/3   verifier_movsx/MOV32SX, S16:OK
#361/4   verifier_movsx/MOV32SX, S16 @unpriv:OK
#361/5   verifier_movsx/MOV64SX, S8:OK
#361/6   verifier_movsx/MOV64SX, S8 @unpriv:OK
#361/7   verifier_movsx/MOV64SX, S16:OK
#361/8   verifier_movsx/MOV64SX, S16 @unpriv:OK
#361/9   verifier_movsx/MOV64SX, S32:OK
#361/10  verifier_movsx/MOV64SX, S32 @unpriv:OK
#361/11  verifier_movsx/MOV32SX, S8, range_check:OK
#361/12  verifier_movsx/MOV32SX, S8, range_check @unpriv:OK
#361/13  verifier_movsx/MOV32SX, S16, range_check:OK
#361/14  verifier_movsx/MOV32SX, S16, range_check @unpriv:OK
#361/15  verifier_movsx/MOV32SX, S16, range_check 2:OK
#361/16  verifier_movsx/MOV32SX, S16, range_check 2 @unpriv:OK
#361/17  verifier_movsx/MOV64SX, S8, range_check:OK
#361/18  verifier_movsx/MOV64SX, S8, range_check @unpriv:OK
#361/19  verifier_movsx/MOV64SX, S16, range_check:OK
#361/20  verifier_movsx/MOV64SX, S16, range_check @unpriv:OK
#361/21  verifier_movsx/MOV64SX, S32, range_check:OK
#361/22  verifier_movsx/MOV64SX, S32, range_check @unpriv:OK
#361/23  verifier_movsx/MOV64SX, S16, R10 Sign Extension:OK
#361/24  verifier_movsx/MOV64SX, S16, R10 Sign Extension @unpriv:OK
#361     verifier_movsx:OK

Summary: 1/24 PASSED, 0 SKIPPED, 0 FAILED

[root@linux bpf]# echo 1 > /proc/sys/net/core/bpf_jit_enable [root@linux bpf]# echo 1 > /proc/sys/kernel/unprivileged_bpf_disabled [root@linux bpf]# ./test_progs -t verifier_movsx

#361/1   verifier_movsx/MOV32SX, S8:OK
#361/2   verifier_movsx/MOV32SX, S8 @unpriv:SKIP
#361/3   verifier_movsx/MOV32SX, S16:OK
#361/4   verifier_movsx/MOV32SX, S16 @unpriv:SKIP
#361/5   verifier_movsx/MOV64SX, S8:OK
#361/6   verifier_movsx/MOV64SX, S8 @unpriv:SKIP
#361/7   verifier_movsx/MOV64SX, S16:OK
#361/8   verifier_movsx/MOV64SX, S16 @unpriv:SKIP
#361/9   verifier_movsx/MOV64SX, S32:OK
#361/10  verifier_movsx/MOV64SX, S32 @unpriv:SKIP
#361/11  verifier_movsx/MOV32SX, S8, range_check:OK
#361/12  verifier_movsx/MOV32SX, S8, range_check @unpriv:SKIP
#361/13  verifier_movsx/MOV32SX, S16, range_check:OK
#361/14  verifier_movsx/MOV32SX, S16, range_check @unpriv:SKIP
#361/15  verifier_movsx/MOV32SX, S16, range_check 2:OK
#361/16  verifier_movsx/MOV32SX, S16, range_check 2 @unpriv:SKIP
#361/17  verifier_movsx/MOV64SX, S8, range_check:OK
#361/18  verifier_movsx/MOV64SX, S8, range_check @unpriv:SKIP
#361/19  verifier_movsx/MOV64SX, S16, range_check:OK
#361/20  verifier_movsx/MOV64SX, S16, range_check @unpriv:SKIP
#361/21  verifier_movsx/MOV64SX, S32, range_check:OK
#361/22  verifier_movsx/MOV64SX, S32, range_check @unpriv:SKIP
#361/23  verifier_movsx/MOV64SX, S16, R10 Sign Extension:OK
#361/24  verifier_movsx/MOV64SX, S16, R10 Sign Extension @unpriv:SKIP
#361     verifier_movsx:OK (SKIP: 12/24)

Summary: 1/12 PASSED, 12 SKIPPED, 0 FAILED

Thanks,
Tiezhu

maintainer
#7 lore raw
In reply to Hengqi Chen

Hi Hengqi,

Quoted 4 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing

...

Quoted 1 line
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

According to the commit message, the code itself looks good to me, thanks for your patch.

In my opinion, I think it is better to update the commit message and split the changes into two separate patches.

Quoted 8 lines

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The test case that cause panic is "verifier_map_ptr_mixing", but here the example is another test case "test_cls_redirect", please take "verifier_map_ptr_mixing" as an example to analysis to keep it consistent.

Quoted 11 lines

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
    				   BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand.

The initial aim of ldd and ldptrd is to reduce one instruction, if it has problem, we can use ldxd directly, but ...

Quoted 1 line

Finally, we got the wrong addresses and result in kernel panics.

... if the test case "cgrp_ls_tp_btf" triggers another panic, and the cause is another reason related with BPF_DW case, please submit a separate patch so that we can record the cause and analysis clearly.

Quoted 5 lines

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

Thanks,
Tiezhu

author reviewer
#8 lore raw
Quoted 56 lines · Tiezhu Yang

Hi Hengqi,

Quoted 4 lines · Hengqi Chen

As reported on mailing list ([0]), several BPF testcases could triggered kernel panics like:

    # ./test_progs -t verifier_map_ptr_mixing

...

Quoted 1 line
    [   47.872612] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

According to the commit message, the code itself looks good to me, thanks for your patch.

In my opinion, I think it is better to update the commit message and split the changes into two separate patches.

Quoted 8 lines

Take the cls_redirect case as an example:

    cls_redirect()
      -> get_global_metrics() returns pcpu ptr 0xfffffefffc00b480

The pointer is later signed-extended to 0xfffffffffc00b480 at BPF_JMP | BPF_EXIT. During BPF prog run, this triggers unhandled page fault and a kernel panic.

The test case that cause panic is "verifier_map_ptr_mixing", but here the example is another test case "test_cls_redirect", please take "verifier_map_ptr_mixing" as an example to analysis to keep it consistent.

Quoted 11 lines

The cgrp_ls_tp_btf test reveals another kind of kernel panic when handling memory load:

    ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
                                 BPF_LOCAL_STORAGE_GET_F_CREATE);

The expression task->cgroups->dfl_cgrp involves two memory load. Since the field offset fits in imm12 or imm14, we use ldd or ldptrd to perform memory load. But the ldd and ldptrd instructions both have the side effect that it will signed-extended the imm operand.

The initial aim of ldd and ldptrd is to reduce one instruction, if it has problem, we can use ldxd directly, but ...

Quoted 1 line

Finally, we got the wrong addresses and result in kernel panics.

... if the test case "cgrp_ls_tp_btf" triggers another panic, and the cause is another reason related with BPF_DW case, please submit a separate patch so that we can record the cause and analysis clearly.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Let's get rid of those fatal signed-extension instrutions to fix the issues. For the first case, no arches do a sext on function return, the fix should be safe. For the second case, we fallback to use a generic ldxd instruction.

Thanks,
Tiezhu

OK, will split this up and add a cover letter.
(I was too lazy and hesitant about making it a patch series.)