1. 17 Dec, 2021 1 commit
    • Christy Lee's avatar
      bpf: Only print scratched registers and stack slots to verifier logs. · 0f55f9ed
      Christy Lee authored
      When printing verifier state for any log level, print full verifier
      state only on function calls or on errors. Otherwise, only print the
      registers and stack slots that were accessed.
      
      Log size differences:
      
      verif_scale_loop6 before: 234566564
      verif_scale_loop6 after: 72143943
      69% size reduction
      
      kfree_skb before: 166406
      kfree_skb after: 55386
      69% size reduction
      
      Before:
      
      156: (61) r0 = *(u32 *)(r1 +0)
      157: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff)) R1=ctx(id=0,off=0,imm=0) R2_w=invP0 R10=fp0 fp-8_w=00000000 fp-16_w=00\
      000000 fp-24_w=00000000 fp-32_w=00000000 fp-40_w=00000000 fp-48_w=00000000 fp-56_w=00000000 fp-64_w=00000000 fp-72_w=00000000 fp-80_w=00000\
      000 fp-88_w=00000000 fp-96_w=00000000 fp-104_w=00000000 fp-112_w=00000000 fp-120_w=00000000 fp-128_w=00000000 fp-136_w=00000000 fp-144_w=00\
      000000 fp-152_w=00000000 fp-160_w=00000000 fp-168_w=00000000 fp-176_w=00000000 fp-184_w=00000000 fp-192_w=00000000 fp-200_w=00000000 fp-208\
      _w=00000000 fp-216_w=00000000 fp-224_w=00000000 fp-232_w=00000000 fp-240_w=00000000 fp-248_w=00000000 fp-256_w=00000000 fp-264_w=00000000 f\
      p-272_w=00000000 fp-280_w=00000000 fp-288_w=00000000 fp-296_w=00000000 fp-304_w=00000000 fp-312_w=00000000 fp-320_w=00000000 fp-328_w=00000\
      000 fp-336_w=00000000 fp-344_w=00000000 fp-352_w=00000000 fp-360_w=00000000 fp-368_w=00000000 fp-376_w=00000000 fp-384_w=00000000 fp-392_w=\
      00000000 fp-400_w=00000000 fp-408_w=00000000 fp-416_w=00000000 fp-424_w=00000000 fp-432_w=00000000 fp-440_w=00000000 fp-448_w=00000000
      ; return skb->len;
      157: (95) exit
      Func#4 is safe for any args that match its prototype
      Validating get_constant() func#5...
      158: R1=invP(id=0) R10=fp0
      ; int get_constant(long val)
      158: (bf) r0 = r1
      159: R0_w=invP(id=1) R1=invP(id=1) R10=fp0
      ; return val - 122;
      159: (04) w0 += -122
      160: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff)) R1=invP(id=1) R10=fp0
      ; return val - 122;
      160: (95) exit
      Func#5 is safe for any args that match its prototype
      Validating get_skb_ifindex() func#6...
      161: R1=invP(id=0) R2=ctx(id=0,off=0,imm=0) R3=invP(id=0) R10=fp0
      ; int get_skb_ifindex(int val, struct __sk_buff *skb, int var)
      161: (bc) w0 = w3
      162: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff)) R1=invP(id=0) R2=ctx(id=0,off=0,imm=0) R3=invP(id=0) R10=fp0
      
      After:
      
      156: (61) r0 = *(u32 *)(r1 +0)
      157: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff)) R1=ctx(id=0,off=0,imm=0)
      ; return skb->len;
      157: (95) exit
      Func#4 is safe for any args that match its prototype
      Validating get_constant() func#5...
      158: R1=invP(id=0) R10=fp0
      ; int get_constant(long val)
      158: (bf) r0 = r1
      159: R0_w=invP(id=1) R1=invP(id=1)
      ; return val - 122;
      159: (04) w0 += -122
      160: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff))
      ; return val - 122;
      160: (95) exit
      Func#5 is safe for any args that match its prototype
      Validating get_skb_ifindex() func#6...
      161: R1=invP(id=0) R2=ctx(id=0,off=0,imm=0) R3=invP(id=0) R10=fp0
      ; int get_skb_ifindex(int val, struct __sk_buff *skb, int var)
      161: (bc) w0 = w3
      162: R0_w=invP(id=0,umax_value=4294967295,var_off=(0x0; 0xffffffff)) R3=invP(id=0)
      Signed-off-by: default avatarChristy Lee <christylee@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211216213358.3374427-2-christylee@fb.com
      0f55f9ed
  2. 16 Dec, 2021 11 commits
  3. 15 Dec, 2021 1 commit
  4. 14 Dec, 2021 9 commits
  5. 13 Dec, 2021 13 commits
  6. 12 Dec, 2021 5 commits
    • Alexei Starovoitov's avatar
      bpf: Silence coverity false positive warning. · f18a4997
      Alexei Starovoitov authored
      Coverity issued the following warning:
      6685            cands = bpf_core_add_cands(cands, main_btf, 1);
      6686            if (IS_ERR(cands))
      >>>     CID 1510300:    (RETURN_LOCAL)
      >>>     Returning pointer "cands" which points to local variable "local_cand".
      6687                    return cands;
      
      It's a false positive.
      Add ERR_CAST() to silence it.
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f18a4997
    • Jiapeng Chong's avatar
      bpf: Use kmemdup() to replace kmalloc + memcpy · 4674f210
      Jiapeng Chong authored
      Eliminate the follow coccicheck warning:
      
      ./kernel/bpf/btf.c:6537:13-20: WARNING opportunity for kmemdup.
      Reported-by: default avatarAbaci Robot <abaci@linux.alibaba.com>
      Signed-off-by: default avatarJiapeng Chong <jiapeng.chong@linux.alibaba.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/1639030882-92383-1-git-send-email-jiapeng.chong@linux.alibaba.com
      4674f210
    • Alexei Starovoitov's avatar
      Merge branch 'introduce bpf_strncmp() helper' · 84ef3f0b
      Alexei Starovoitov authored
      Hou Tao says:
      
      ====================
      
      Hi,
      
      The motivation for introducing bpf_strncmp() helper comes from
      two aspects:
      
      (1) clang doesn't always replace strncmp() automatically
      In tracing program, sometimes we need to using a home-made
      strncmp() to check whether or not the file name is expected.
      
      (2) the performance of home-made strncmp is not so good
      As shown in the benchmark in patch #4, the performance of
      bpf_strncmp() helper is 18% or 33% better than home-made strncmp()
      under x86-64 or arm64 when the compared string length is 64. When
      the string length grows to 4095, the performance win will be
      179% or 600% under x86-64 or arm64.
      
      Any comments are welcome.
      Regards,
      Tao
      
      Change Log:
      v2:
       * rebased on bpf-next
       * drop patch "selftests/bpf: factor out common helpers for benchmarks"
         (suggested by Andrii)
       * remove unnecessary inline functions and add comments for programs which
         will be rejected by verifier in patch 4 (suggested by Andrii)
       * rename variables used in will-fail programs to clarify the purposes.
      
      v1: https://lore.kernel.org/bpf/20211130142215.1237217-1-houtao1@huawei.com
       * change API to bpf_strncmp(const char *s1, u32 s1_sz, const char *s2)
       * add benchmark refactor and benchmark between bpf_strncmp() and strncmp()
      
      RFC: https://lore.kernel.org/bpf/20211106132822.1396621-1-houtao1@huawei.com/
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      84ef3f0b
    • Hou Tao's avatar
      selftests/bpf: Add test cases for bpf_strncmp() · bdbee82b
      Hou Tao authored
      Four test cases are added:
      (1) ensure the return value is expected
      (2) ensure no const string size is rejected
      (3) ensure writable target is rejected
      (4) ensure no null-terminated target is rejected
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211210141652.877186-5-houtao1@huawei.com
      bdbee82b
    • Hou Tao's avatar
      selftests/bpf: Add benchmark for bpf_strncmp() helper · 9c42652f
      Hou Tao authored
      Add benchmark to compare the performance between home-made strncmp()
      in bpf program and bpf_strncmp() helper. In summary, the performance
      win of bpf_strncmp() under x86-64 is greater than 18% when the compared
      string length is greater than 64, and is 179% when the length is 4095.
      Under arm64 the performance win is even bigger: 33% when the length
      is greater than 64 and 600% when the length is 4095.
      
      The following is the details:
      
      no-helper-X: use home-made strncmp() to compare X-sized string
      helper-Y: use bpf_strncmp() to compare Y-sized string
      
      Under x86-64:
      
      no-helper-1          3.504 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-1             3.347 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-8          3.357 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      helper-8             3.307 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-32         3.064 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-32            3.253 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-64         2.563 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      helper-64            3.040 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-128        1.975 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-128           2.641 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-512        0.759 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-512           1.574 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-2048       0.329 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-2048          0.602 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-4095       0.117 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-4095          0.327 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      
      Under arm64:
      
      no-helper-1          2.806 ± 0.004M/s (drops 0.000 ± 0.000M/s)
      helper-1             2.819 ± 0.002M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-8          2.797 ± 0.109M/s (drops 0.000 ± 0.000M/s)
      helper-8             2.786 ± 0.025M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-32         2.399 ± 0.011M/s (drops 0.000 ± 0.000M/s)
      helper-32            2.703 ± 0.002M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-64         2.020 ± 0.015M/s (drops 0.000 ± 0.000M/s)
      helper-64            2.702 ± 0.073M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-128        1.604 ± 0.001M/s (drops 0.000 ± 0.000M/s)
      helper-128           2.516 ± 0.002M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-512        0.699 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-512           2.106 ± 0.003M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-2048       0.215 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-2048          1.223 ± 0.003M/s (drops 0.000 ± 0.000M/s)
      
      no-helper-4095       0.112 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      helper-4095          0.796 ± 0.000M/s (drops 0.000 ± 0.000M/s)
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211210141652.877186-4-houtao1@huawei.com
      9c42652f