1. Jan 04, 2024
    • Andrii Nakryiko's avatar
      df7c3f7d
    • Alexei Starovoitov's avatar
      Merge branch 'bpf-reduce-memory-usage-for-bpf_global_percpu_ma' · f8506c57
      Alexei Starovoitov authored
      Yonghong Song says:
      
      ====================
      bpf: Reduce memory usage for bpf_global_percpu_ma
      
      Currently when a bpf program intends to allocate memory for percpu kptr,
      the verifier will call bpf_mem_alloc_init() to prefill all supported
      unit sizes and this caused memory consumption very big for large number
      of cpus. For example, for 128-cpu system, the total memory consumption
      with initial prefill is ~175MB. Things will become worse for systems
      with even more cpus.
      
      Patch 1 avoids unnecessary extra percpu memory allocation.
      Patch 2 adds objcg to bpf_mem_alloc at init stage so objcg can be
      associated with root cgroup and objcg can be passed to later
      bpf_mem_alloc_percpu_unit_init().
      Patch 3 addresses memory consumption issue by avoiding to prefill
      with all unit sizes, i.e. only prefilling with user specified size.
      Patch 4 further reduces memory consumption by limiting the
      number of prefill entries for percpu memory allocation.
      Patch 5 has much smaller low/high watermarks for percpu allocation
      to reduce memory consumption.
      Patch 6 rejects percpu memory allocation with bpf_global_percpu_ma
      when allocation size is greater than 512 bytes.
      Patch 7 fixed test_bpf_ma test due to Patch 5.
      Patch 8 added one test to show the verification failure log message.
      
      Changelogs:
        v5 -> v6:
          . Change bpf_mem_alloc_percpu_init() to add objcg as one of parameters.
            For bpf_global_percpu_ma, the objcg is NULL, corresponding root memcg.
        v4 -> v5:
          . Do not do bpf_global_percpu_ma initialization at init stage, instead
            doing initialization when the verifier knows it is going to be used
            by bpf prog.
          . Using much smaller low/high watermarks for percpu allocation.
        v3 -> v4:
          . Add objcg to bpf_mem_alloc during init stage.
          . Initialize objcg at init stage but use it in bpf_mem_alloc_percpu_unit_init().
          . Remove check_obj_size() in bpf_mem_alloc_percpu_unit_init().
        v2 -> v3:
          . Clear the bpf_mem_cache if prefill fails.
          . Change test_bpf_ma percpu allocation tests to use bucket_size
            as allocation size instead of bucket_size - 8.
          . Remove __GFP_ZERO flag from __alloc_percpu_gfp() call.
        v1 -> v2:
          . Avoid unnecessary extra percpu memory allocation.
          . Add a separate function to do bpf_global_percpu_ma initialization
          . promote.
          . Promote function static 'sizes' array to file static.
          . Add comments to explain to refill only one item for percpu alloc.
      ====================
      
      Link: https://lore.kernel.org/r/20231222031729.1287957-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f8506c57
    • Yonghong Song's avatar
      selftests/bpf: Add a selftest with > 512-byte percpu allocation size · adc8c454
      Yonghong Song authored
      
      
      Add a selftest to capture the verification failure when the allocation
      size is greater than 512.
      
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031812.1293190-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      adc8c454
    • Yonghong Song's avatar
      selftests/bpf: Cope with 512 bytes limit with bpf_global_percpu_ma · 21f5a801
      Yonghong Song authored
      
      
      In the previous patch, the maximum data size for bpf_global_percpu_ma
      is 512 bytes. This breaks selftest test_bpf_ma. The test is adjusted
      in two aspects:
        - Since the maximum allowed data size for bpf_global_percpu_ma is
          512, remove all tests beyond that, names sizes 1024, 2048 and 4096.
        - Previously the percpu data size is bucket_size - 8 in order to
          avoid percpu allocation into the next bucket. This patch removed
          such data size adjustment thanks to Patch 1.
      
      Also, a better way to generate BTF type is used than adding
      a member to the value struct.
      
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031807.1292853-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      21f5a801
    • Yonghong Song's avatar
      bpf: Limit up to 512 bytes for bpf_global_percpu_ma allocation · 5c1a3765
      Yonghong Song authored
      
      
      For percpu data structure allocation with bpf_global_percpu_ma,
      the maximum data size is 4K. But for a system with large
      number of cpus, bigger data size (e.g., 2K, 4K) might consume
      a lot of memory. For example, the percpu memory consumption
      with unit size 2K and 1024 cpus will be 2K * 1K * 1k = 2GB
      memory.
      
      We should discourage such usage. Let us limit the maximum data
      size to be 512 for bpf_global_percpu_ma allocation.
      
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031801.1290841-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      5c1a3765
    • Yonghong Song's avatar
      bpf: Use smaller low/high marks for percpu allocation · 0e2ba9f9
      Yonghong Song authored
      
      
      Currently, refill low/high marks are set with the assumption
      of normal non-percpu memory allocation. For example, for
      an allocation size 256, for non-percpu memory allocation,
      low mark is 32 and high mark is 96, resulting in the
      batch allocation of 48 elements and the allocated memory
      will be 48 * 256 = 12KB for this particular cpu.
      Assuming an 128-cpu system, the total memory consumption
      across all cpus will be 12K * 128 = 1.5MB memory.
      
      This might be okay for non-percpu allocation, but may not be
      good for percpu allocation, which will consume 1.5MB * 128 = 192MB
      memory in the worst case if every cpu has a chance of memory
      allocation.
      
      In practice, percpu allocation is very rare compared to
      non-percpu allocation. So let us have smaller low/high marks
      which can avoid unnecessary memory consumption.
      
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Link: https://lore.kernel.org/r/20231222031755.1289671-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0e2ba9f9
    • Yonghong Song's avatar
      bpf: Refill only one percpu element in memalloc · 5b95e638
      Yonghong Song authored
      
      
      Typically for percpu map element or data structure, once allocated,
      most operations are lookup or in-place update. Deletion are really
      rare. Currently, for percpu data strcture, 4 elements will be
      refilled if the size is <= 256. Let us just do with one element
      for percpu data. For example, for size 256 and 128 cpus, the
      potential saving will be 3 * 256 * 128 * 128 = 12MB.
      
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031750.1289290-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      5b95e638
    • Yonghong Song's avatar
      bpf: Allow per unit prefill for non-fix-size percpu memory allocator · c39aa3b2
      Yonghong Song authored
      Commit 41a5db8d ("Add support for non-fix-size percpu mem allocation")
      added support for non-fix-size percpu memory allocation.
      Such allocation will allocate percpu memory for all buckets on all
      cpus and the memory consumption is in the order to quadratic.
      For example, let us say, 4 cpus, unit size 16 bytes, so each
      cpu has 16 * 4 = 64 bytes, with 4 cpus, total will be 64 * 4 = 256 bytes.
      Then let us say, 8 cpus with the same unit size, each cpu
      has 16 * 8 = 128 bytes, with 8 cpus, total will be 128 * 8 = 1024 bytes.
      So if the number of cpus doubles, the number of memory consumption
      will be 4 times. So for a system with large number of cpus, the
      memory consumption goes up quickly with quadratic order.
      For example, for 4KB percpu allocation, 128 cpus. The total memory
      consumption will 4KB * 128 * 128 = 64MB. Things will become
      worse if the number of cpus is bigger (e.g., 512, 1024, etc.)
      
      In Commit 41a5db8d, the non-fix-size percpu memory allocation is
      done in boot time, so for system with large number of cpus, the initial
      percpu memory consumption is very visible. For example, for 128 cpu
      system, the total percpu memory allocation will be at least
      (16 + 32 + 64 + 96 + 128 + 196 + 256 + 512 + 1024 + 2048 + 4096)
        * 128 * 128 = ~138MB.
      which is pretty big. It will be even bigger for larger number of cpus.
      
      Note that the current prefill also allocates 4 entries if the unit size
      is less than 256. So on top of 138MB memory consumption, this will
      add more consumption with
      3 * (16 + 32 + 64 + 96 + 128 + 196 + 256) * 128 * 128 = ~38MB.
      Next patch will try to reduce this memory consumption.
      
      Later on, Commit 1fda5bb6
      
       ("bpf: Do not allocate percpu memory
      at init stage") moved the non-fix-size percpu memory allocation
      to bpf verificaiton stage. Once a particular bpf_percpu_obj_new()
      is called by bpf program, the memory allocator will try to fill in
      the cache with all sizes, causing the same amount of percpu memory
      consumption as in the boot stage.
      
      To reduce the initial percpu memory consumption for non-fix-size
      percpu memory allocation, instead of filling the cache with all
      supported allocation sizes, this patch intends to fill the cache
      only for the requested size. As typically users will not use large
      percpu data structure, this can save memory significantly.
      For example, the allocation size is 64 bytes with 128 cpus.
      Then total percpu memory amount will be 64 * 128 * 128 = 1MB,
      much less than previous 138MB.
      
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Link: https://lore.kernel.org/r/20231222031745.1289082-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c39aa3b2
    • Yonghong Song's avatar
      bpf: Add objcg to bpf_mem_alloc · 9fc8e802
      Yonghong Song authored
      
      
      The objcg is a bpf_mem_alloc level property since all bpf_mem_cache's
      are with the same objcg. This patch made such a property explicit.
      The next patch will use this property to save and restore objcg
      for percpu unit allocator.
      
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031739.1288590-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      9fc8e802
    • Yonghong Song's avatar
      bpf: Avoid unnecessary extra percpu memory allocation · 9beda16c
      Yonghong Song authored
      
      
      Currently, for percpu memory allocation, say if the user
      requests allocation size to be 32 bytes, the actually
      calculated size will be 40 bytes and it further rounds
      to 64 bytes, and eventually 64 bytes are allocated,
      wasting 32-byte memory.
      
      Change bpf_mem_alloc() to calculate the cache index
      based on the user-provided allocation size so unnecessary
      extra memory can be avoided.
      
      Suggested-by: default avatarHou Tao <houtao1@huawei.com>
      Acked-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20231222031734.1288400-1-yonghong.song@linux.dev
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      9beda16c
    • Martin KaFai Lau's avatar
      Merge branch 'fix sockmap + stream af_unix memleak' · 417fa6d1
      Martin KaFai Lau authored
      
      
      John Fastabend says:
      
      ====================
      There was a memleak when streaming af_unix sockets were inserted into
      multiple sockmap slots and/or maps. This is because each insert would
      call a proto update operatino and these must be allowed to be called
      multiple times. The streaming af_unix implementation recently added
      a refcnt to handle a use after free issue, however it introduced a
      memleak when inserted into multiple maps.
      
      This series fixes the memleak, adds a note in the code so we remember
      that proto updates need to support this. And then we add three tests
      for each of the slightly different iterations of adding sockets into
      multiple maps. I kept them as 3 independent test cases here. I have
      some slight preference for this they could however be a single test,
      but then you don't get to run them independently which was sort of
      useful while debugging.
      ====================
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      417fa6d1
    • John Fastabend's avatar
      bpf: sockmap, add tests for proto updates replace socket · bdbca46d
      John Fastabend authored
      
      
      Add test that replaces the same socket with itself. This exercises a
      corner case where old element and new element have the same posck.
      Test protocols: TCP, UDP, stream af_unix and dgram af_unix.
      
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/r/20231221232327.43678-6-john.fastabend@gmail.com
      bdbca46d
    • John Fastabend's avatar
      bpf: sockmap, add tests for proto updates single socket to many map · f1300467
      John Fastabend authored
      
      
      Add test with multiple maps where each socket is inserted in multiple
      maps. Test protocols: TCP, UDP, stream af_unix and dgram af_unix.
      
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/r/20231221232327.43678-5-john.fastabend@gmail.com
      f1300467
    • John Fastabend's avatar
      bpf: sockmap, add tests for proto updates many to single map · 8c1b382a
      John Fastabend authored
      
      
      Add test with a single map where each socket is inserted multiple
      times. Test protocols: TCP, UDP, stream af_unix and dgram af_unix.
      
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/r/20231221232327.43678-4-john.fastabend@gmail.com
      8c1b382a
    • John Fastabend's avatar
      bpf: sockmap, added comments describing update proto rules · 7865dfb1
      John Fastabend authored
      
      
      Add a comment describing that the psock update proto callbback can be
      called multiple times and this must be safe.
      
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/r/20231221232327.43678-3-john.fastabend@gmail.com
      7865dfb1
    • John Fastabend's avatar
      bpf: sockmap, fix proto update hook to avoid dup calls · 16b2f264
      John Fastabend authored
      When sockets are added to a sockmap or sockhash we allocate and init a
      psock. Then update the proto ops with sock_map_init_proto the flow is
      
        sock_hash_update_common
          sock_map_link
            psock = sock_map_psock_get_checked() <-returns existing psock
            sock_map_init_proto(sk, psock)       <- updates sk_proto
      
      If the socket is already in a map this results in the sock_map_init_proto
      being called multiple times on the same socket. We do this because when
      a socket is added to multiple maps this might result in a new set of BPF
      programs being attached to the socket requiring an updated ops struct.
      
      This creates a rule where it must be safe to call psock_update_sk_prot
      multiple times. When we added a fix for UAF through unix sockets in patch
      4dd9a38a753fc we broke this rule by adding a sock_hold in that path
      to ensure the sock is not released. The result is if a af_unix stream sock
      is placed in multiple maps it results in a memory leak because we call
      sock_hold multiple times with only a single sock_put on it.
      
      Fixes: 8866730a
      
       ("bpf, sockmap: af_unix stream sockets need to hold ref for pair sock")
      Reported-by: default avatarXingwei Lee <xrivendell7@gmail.com>
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/r/20231221232327.43678-2-john.fastabend@gmail.com
      16b2f264
    • Andrii Nakryiko's avatar
      Merge branch 'bpf-volatile-compare' · b4560055
      Andrii Nakryiko authored
      Alexei Starovoitov says:
      
      ====================
      bpf: volatile compare
      
      From: Alexei Starovoitov <ast@kernel.org>
      
      v2->v3:
      Debugged profiler.c regression. It was caused by basic block layout.
      Introduce bpf_cmp_likely() and bpf_cmp_unlikely() macros.
      Debugged redundant <<=32, >>=32 with u32 variables. Added cast workaround.
      
      v1->v2:
      Fixed issues pointed out by Daniel, added more tests, attempted to convert profiler.c,
      but barrier_var() wins vs bpf_cmp(). To be investigated.
      Patches 1-4 are good to go, but 5 needs more work.
      ====================
      
      Link: https://lore.kernel.org/r/20231226191148.48536-1-alexei.starovoitov@gmail.com
      
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      b4560055
    • Alexei Starovoitov's avatar
      selftests/bpf: Convert profiler.c to bpf_cmp. · 7e3811cb
      Alexei Starovoitov authored
      
      
      Convert profiler[123].c to "volatile compare" to compare barrier_var() approach vs bpf_cmp_likely() vs bpf_cmp_unlikely().
      
      bpf_cmp_unlikely() produces correct code, but takes much longer to verify:
      
      ./veristat -C -e prog,insns,states before after_with_unlikely
      Program                               Insns (A)  Insns (B)  Insns       (DIFF)  States (A)  States (B)  States     (DIFF)
      ------------------------------------  ---------  ---------  ------------------  ----------  ----------  -----------------
      kprobe__proc_sys_write                     1603      19606  +18003 (+1123.08%)         123        1678  +1555 (+1264.23%)
      kprobe__vfs_link                          11815      70305   +58490 (+495.05%)         971        4967   +3996 (+411.53%)
      kprobe__vfs_symlink                        5464      42896   +37432 (+685.07%)         434        3126   +2692 (+620.28%)
      kprobe_ret__do_filp_open                   5641      44578   +38937 (+690.25%)         446        3162   +2716 (+608.97%)
      raw_tracepoint__sched_process_exec         2770      35962  +33192 (+1198.27%)         226        3121  +2895 (+1280.97%)
      raw_tracepoint__sched_process_exit         1526       2135      +609 (+39.91%)         133         208      +75 (+56.39%)
      raw_tracepoint__sched_process_fork          265        337       +72 (+27.17%)          19          24       +5 (+26.32%)
      tracepoint__syscalls__sys_enter_kill      18782     140407  +121625 (+647.56%)        1286       12176  +10890 (+846.81%)
      
      bpf_cmp_likely() is equivalent to barrier_var():
      
      ./veristat -C -e prog,insns,states before after_with_likely
      Program                               Insns (A)  Insns (B)  Insns   (DIFF)  States (A)  States (B)  States (DIFF)
      ------------------------------------  ---------  ---------  --------------  ----------  ----------  -------------
      kprobe__proc_sys_write                     1603       1663    +60 (+3.74%)         123         127    +4 (+3.25%)
      kprobe__vfs_link                          11815      12090   +275 (+2.33%)         971         971    +0 (+0.00%)
      kprobe__vfs_symlink                        5464       5448    -16 (-0.29%)         434         426    -8 (-1.84%)
      kprobe_ret__do_filp_open                   5641       5739    +98 (+1.74%)         446         446    +0 (+0.00%)
      raw_tracepoint__sched_process_exec         2770       2608   -162 (-5.85%)         226         216   -10 (-4.42%)
      raw_tracepoint__sched_process_exit         1526       1526     +0 (+0.00%)         133         133    +0 (+0.00%)
      raw_tracepoint__sched_process_fork          265        265     +0 (+0.00%)          19          19    +0 (+0.00%)
      tracepoint__syscalls__sys_enter_kill      18782      18970   +188 (+1.00%)        1286        1286    +0 (+0.00%)
      kprobe__proc_sys_write                     2700       2809   +109 (+4.04%)         107         109    +2 (+1.87%)
      kprobe__vfs_link                          12238      12366   +128 (+1.05%)         267         269    +2 (+0.75%)
      kprobe__vfs_symlink                        7139       7365   +226 (+3.17%)         167         175    +8 (+4.79%)
      kprobe_ret__do_filp_open                   7264       7070   -194 (-2.67%)         180         182    +2 (+1.11%)
      raw_tracepoint__sched_process_exec         3768       3453   -315 (-8.36%)         211         199   -12 (-5.69%)
      raw_tracepoint__sched_process_exit         3138       3138     +0 (+0.00%)          83          83    +0 (+0.00%)
      raw_tracepoint__sched_process_fork          265        265     +0 (+0.00%)          19          19    +0 (+0.00%)
      tracepoint__syscalls__sys_enter_kill      26679      24327  -2352 (-8.82%)        1067        1037   -30 (-2.81%)
      kprobe__proc_sys_write                     1833       1833     +0 (+0.00%)         157         157    +0 (+0.00%)
      kprobe__vfs_link                           9995      10127   +132 (+1.32%)         803         803    +0 (+0.00%)
      kprobe__vfs_symlink                        5606       5672    +66 (+1.18%)         451         451    +0 (+0.00%)
      kprobe_ret__do_filp_open                   5716       5782    +66 (+1.15%)         462         462    +0 (+0.00%)
      raw_tracepoint__sched_process_exec         3042       3042     +0 (+0.00%)         278         278    +0 (+0.00%)
      raw_tracepoint__sched_process_exit         1680       1680     +0 (+0.00%)         146         146    +0 (+0.00%)
      raw_tracepoint__sched_process_fork          299        299     +0 (+0.00%)          25          25    +0 (+0.00%)
      tracepoint__syscalls__sys_enter_kill      18372      18372     +0 (+0.00%)        1558        1558    +0 (+0.00%)
      
      default (mcpu=v3), no_alu32, cpuv4 have similar differences.
      
      Note one place where bpf_nop_mov() is used to workaround the verifier lack of link
      between the scalar register and its spill to stack.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-7-alexei.starovoitov@gmail.com
      7e3811cb
    • Alexei Starovoitov's avatar
      bpf: Add bpf_nop_mov() asm macro. · 0bcc62aa
      Alexei Starovoitov authored
      
      
      bpf_nop_mov(var) asm macro emits nop register move: rX = rX.
      If 'var' is a scalar and not a fixed constant the verifier will assign ID to it.
      If it's later spilled the stack slot will carry that ID as well.
      Hence the range refining comparison "if rX < const" will update all copies
      including spilled slot.
      This macro is a temporary workaround until the verifier gets smarter.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-6-alexei.starovoitov@gmail.com
      0bcc62aa
    • Alexei Starovoitov's avatar
      selftests/bpf: Remove bpf_assert_eq-like macros. · 907dbd3e
      Alexei Starovoitov authored
      
      
      Since the last user was converted to bpf_cmp, remove bpf_assert_eq/ne/... macros.
      
      __bpf_assert_op() macro is kept for experiments, since it's slightly more efficient
      than bpf_assert(bpf_cmp_unlikely()) until LLVM is fixed.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-5-alexei.starovoitov@gmail.com
      907dbd3e
    • Alexei Starovoitov's avatar
      selftests/bpf: Convert exceptions_assert.c to bpf_cmp · 624cd2a1
      Alexei Starovoitov authored
      
      
      Convert exceptions_assert.c to bpf_cmp_unlikely() macro.
      
      Since
      
      bpf_assert(bpf_cmp_unlikely(var, ==, 100));
      other code;
      
      will generate assembly code:
      
        if r1 == 100 goto L2;
        r0 = 0
        call bpf_throw
      L1:
        other code;
        ...
      
      L2: goto L1;
      
      LLVM generates redundant basic block with extra goto. LLVM will be fixed eventually.
      Right now it's less efficient than __bpf_assert(var, ==, 100) macro that produces:
        if r1 == 100 goto L1;
        r0 = 0
        call bpf_throw
      L1:
        other code;
      
      But extra goto doesn't hurt the verification process.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-4-alexei.starovoitov@gmail.com
      624cd2a1
    • Alexei Starovoitov's avatar
      bpf: Introduce "volatile compare" macros · a8b242d7
      Alexei Starovoitov authored
      
      
      Compilers optimize conditional operators at will, but often bpf programmers
      want to force compilers to keep the same operator in asm as it's written in C.
      Introduce bpf_cmp_likely/unlikely(var1, conditional_op, var2) macros that can be used as:
      
      -               if (seen >= 1000)
      +               if (bpf_cmp_unlikely(seen, >=, 1000))
      
      The macros take advantage of BPF assembly that is C like.
      
      The macros check the sign of variable 'seen' and emits either
      signed or unsigned compare.
      
      For example:
      int a;
      bpf_cmp_unlikely(a, >, 0) will be translated to 'if rX s> 0 goto' in BPF assembly.
      
      unsigned int a;
      bpf_cmp_unlikely(a, >, 0) will be translated to 'if rX > 0 goto' in BPF assembly.
      
      C type conversions coupled with comparison operator are tricky.
        int i = -1;
        unsigned int j = 1;
        if (i < j) // this is false.
      
        long i = -1;
        unsigned int j = 1;
        if (i < j) // this is true.
      
      Make sure BPF program is compiled with -Wsign-compare then the macros will catch
      the mistake.
      
      The macros check LHS (left hand side) only to figure out the sign of compare.
      
      'if 0 < rX goto' is not allowed in the assembly, so the users
      have to use a variable on LHS anyway.
      
      The patch updates few tests to demonstrate the use of the macros.
      
      The macro allows to use BPF_JSET in C code, since LLVM doesn't generate it at
      present. For example:
      
      if (i & j) compiles into r0 &= r1; if r0 == 0 goto
      
      while
      
      if (bpf_cmp_unlikely(i, &, j)) compiles into if r0 & r1 goto
      
      Note that the macros has to be careful with RHS assembly predicate.
      Since:
      u64 __rhs = 1ull << 42;
      asm goto("if r0 < %[rhs] goto +1" :: [rhs] "ri" (__rhs));
      LLVM will silently truncate 64-bit constant into s32 imm.
      
      Note that [lhs] "r"((short)LHS) the type cast is a workaround for LLVM issue.
      When LHS is exactly 32-bit LLVM emits redundant <<=32, >>=32 to zero upper 32-bits.
      When LHS is 64 or 16 or 8-bit variable there are no shifts.
      When LHS is 32-bit the (u64) cast doesn't help. Hence use (short) cast.
      It does _not_ truncate the variable before it's assigned to a register.
      
      Traditional likely()/unlikely() macros that use __builtin_expect(!!(x), 1 or 0)
      have no effect on these macros, hence macros implement the logic manually.
      bpf_cmp_unlikely() macro preserves compare operator as-is while
      bpf_cmp_likely() macro flips the compare.
      
      Consider two cases:
      A.
        for() {
          if (foo >= 10) {
            bar += foo;
          }
          other code;
        }
      
      B.
        for() {
          if (foo >= 10)
             break;
          other code;
        }
      
      It's ok to use either bpf_cmp_likely or bpf_cmp_unlikely macros in both cases,
      but consider that 'break' is effectively 'goto out_of_the_loop'.
      Hence it's better to use bpf_cmp_unlikely in the B case.
      While 'bar += foo' is better to keep as 'fallthrough' == likely code path in the A case.
      
      When it's written as:
      A.
        for() {
          if (bpf_cmp_likely(foo, >=, 10)) {
            bar += foo;
          }
          other code;
        }
      
      B.
        for() {
          if (bpf_cmp_unlikely(foo, >=, 10))
             break;
          other code;
        }
      
      The assembly will look like:
      A.
        for() {
          if r1 < 10 goto L1;
            bar += foo;
        L1:
          other code;
        }
      
      B.
        for() {
          if r1 >= 10 goto L2;
          other code;
        }
        L2:
      
      The bpf_cmp_likely vs bpf_cmp_unlikely changes basic block layout, hence it will
      greatly influence the verification process. The number of processed instructions
      will be different, since the verifier walks the fallthrough first.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-3-alexei.starovoitov@gmail.com
      a8b242d7
    • Alexei Starovoitov's avatar
      selftests/bpf: Attempt to build BPF programs with -Wsign-compare · 495d2d81
      Alexei Starovoitov authored
      
      
      GCC's -Wall includes -Wsign-compare while clang does not.
      Since BPF programs are built with clang we need to add this flag explicitly
      to catch problematic comparisons like:
      
        int i = -1;
        unsigned int j = 1;
        if (i < j) // this is false.
      
        long i = -1;
        unsigned int j = 1;
        if (i < j) // this is true.
      
      C standard for reference:
      
      - If either operand is unsigned long the other shall be converted to unsigned long.
      
      - Otherwise, if one operand is a long int and the other unsigned int, then if a
      long int can represent all the values of an unsigned int, the unsigned int
      shall be converted to a long int; otherwise both operands shall be converted to
      unsigned long int.
      
      - Otherwise, if either operand is long, the other shall be converted to long.
      
      - Otherwise, if either operand is unsigned, the other shall be converted to unsigned.
      
      Unfortunately clang's -Wsign-compare is very noisy.
      It complains about (s32)a == (u32)b which is safe and doen't have surprising behavior.
      
      This patch fixes some of the issues. It needs a follow up to fix the rest.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/bpf/20231226191148.48536-2-alexei.starovoitov@gmail.com
      495d2d81
    • Andrii Nakryiko's avatar
      Merge branch 'bpf-simplify-checking-size-of-helper-accesses' · a640de4c
      Andrii Nakryiko authored
      Andrei Matei says:
      
      ====================
      bpf: Simplify checking size of helper accesses
      
      v3->v4:
      - kept only the minimal change, undoing debatable changes (Andrii)
      - dropped the second patch from before, with changes to the error
        message (Andrii)
      - extracted the new test into a separate patch (Andrii)
      - added Acked by Andrii
      
      v2->v3:
      - split the error-logging function to a separate patch (Andrii)
      - make the error buffers smaller (Andrii)
      - include size of memory region for PTR_TO_MEM (Andrii)
      - nits from Andrii and Eduard
      
      v1->v2:
      - make the error message include more info about the context of the
        zero-sized access (Andrii)
      ====================
      
      Link: https://lore.kernel.org/r/20231221232225.568730-1-andreimatei1@gmail.com
      
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      a640de4c
    • Andrei Matei's avatar
      bpf: Add a possibly-zero-sized read test · 72187506
      Andrei Matei authored
      
      
      This patch adds a test for the condition that the previous patch mucked
      with - illegal zero-sized helper memory access. As opposed to existing
      tests, this new one uses a size whose lower bound is zero, as opposed to
      a known-zero one.
      
      Signed-off-by: default avatarAndrei Matei <andreimatei1@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20231221232225.568730-3-andreimatei1@gmail.com
      72187506
    • Andrei Matei's avatar
      bpf: Simplify checking size of helper accesses · 8a021e7f
      Andrei Matei authored
      
      
      This patch simplifies the verification of size arguments associated to
      pointer arguments to helpers and kfuncs. Many helpers take a pointer
      argument followed by the size of the memory access performed to be
      performed through that pointer. Before this patch, the handling of the
      size argument in check_mem_size_reg() was confusing and wasteful: if the
      size register's lower bound was 0, then the verification was done twice:
      once considering the size of the access to be the lower-bound of the
      respective argument, and once considering the upper bound (even if the
      two are the same). The upper bound checking is a super-set of the
      lower-bound checking(*), except: the only point of the lower-bound check
      is to handle the case where zero-sized-accesses are explicitly not
      allowed and the lower-bound is zero. This static condition is now
      checked explicitly, replacing a much more complex, expensive and
      confusing verification call to check_helper_mem_access().
      
      Error messages change in this patch. Before, messages about illegal
      zero-size accesses depended on the type of the pointer and on other
      conditions, and sometimes the message was plain wrong: in some tests
      that changed you'll see that the old message was something like "R1 min
      value is outside of the allowed memory range", where R1 is the pointer
      register; the error was wrongly claiming that the pointer was bad
      instead of the size being bad. Other times the information that the size
      came for a register with a possible range of values was wrong, and the
      error presented the size as a fixed zero. Now the errors refer to the
      right register. However, the old error messages did contain useful
      information about the pointer register which is now lost; recovering
      this information was deemed not important enough.
      
      (*) Besides standing to reason that the checks for a bigger size access
      are a super-set of the checks for a smaller size access, I have also
      mechanically verified this by reading the code for all types of
      pointers. I could convince myself that it's true for all but
      PTR_TO_BTF_ID (check_ptr_to_btf_access). There, simply looking
      line-by-line does not immediately prove what we want. If anyone has any
      qualms, let me know.
      
      Signed-off-by: default avatarAndrei Matei <andreimatei1@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20231221232225.568730-2-andreimatei1@gmail.com
      8a021e7f
  2. Jan 03, 2024
  3. Jan 02, 2024