1. 15 Sep, 2020 5 commits
  2. 11 Sep, 2020 14 commits
  3. 10 Sep, 2020 8 commits
  4. 09 Sep, 2020 4 commits
    • Andrii Nakryiko's avatar
      perf: Stop using deprecated bpf_program__title() · 8081ede1
      Andrii Nakryiko authored
      Switch from deprecated bpf_program__title() API to
      bpf_program__section_name(). Also drop unnecessary error checks because
      neither bpf_program__title() nor bpf_program__section_name() can fail or
      return NULL.
      
      Fixes: 52109584 ("libbpf: Deprecate notion of BPF program "title" in favor of "section name"")
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Reviewed-by: default avatarTobias Klauser <tklauser@distanz.ch>
      Acked-by: default avatarJiri Olsa <jolsa@redhat.com>
      Link: https://lore.kernel.org/bpf/20200908180127.1249-1-andriin@fb.com
      8081ede1
    • Yonghong Song's avatar
      selftests/bpf: Fix test_sysctl_loop{1, 2} failure due to clang change · 7fb5eefd
      Yonghong Song authored
      Andrii reported that with latest clang, when building selftests, we have
      error likes:
        error: progs/test_sysctl_loop1.c:23:16: in function sysctl_tcp_mem i32 (%struct.bpf_sysctl*):
        Looks like the BPF stack limit of 512 bytes is exceeded.
        Please move large on stack variables into BPF per-cpu array map.
      
      The error is triggered by the following LLVM patch:
        https://reviews.llvm.org/D87134
      
      For example, the following code is from test_sysctl_loop1.c:
        static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx)
        {
          volatile char tcp_mem_name[] = "net/ipv4/tcp_mem/very_very_very_very_long_pointless_string";
          ...
        }
      Without the above LLVM patch, the compiler did optimization to load the string
      (59 bytes long) with 7 64bit loads, 1 8bit load and 1 16bit load,
      occupying 64 byte stack size.
      
      With the above LLVM patch, the compiler only uses 8bit loads, but subregister is 32bit.
      So stack requirements become 4 * 59 = 236 bytes. Together with other stuff on
      the stack, total stack size exceeds 512 bytes, hence compiler complains and quits.
      
      To fix the issue, removing "volatile" key word or changing "volatile" to
      "const"/"static const" does not work, the string is put in .rodata.str1.1 section,
      which libbpf did not process it and errors out with
        libbpf: elf: skipping unrecognized data section(6) .rodata.str1.1
        libbpf: prog 'sysctl_tcp_mem': bad map relo against '.L__const.is_tcp_mem.tcp_mem_name'
                in section '.rodata.str1.1'
      
      Defining the string const as global variable can fix the issue as it puts the string constant
      in '.rodata' section which is recognized by libbpf. In the future, when libbpf can process
      '.rodata.str*.*' properly, the global definition can be changed back to local definition.
      
      Defining tcp_mem_name as a global, however, triggered a verifier failure.
         ./test_progs -n 7/21
        libbpf: load bpf program failed: Permission denied
        libbpf: -- BEGIN DUMP LOG ---
        libbpf:
        invalid stack off=0 size=1
        verification time 6975 usec
        stack depth 160+64
        processed 889 insns (limit 1000000) max_states_per_insn 4 total_states
        14 peak_states 14 mark_read 10
      
        libbpf: -- END LOG --
        libbpf: failed to load program 'sysctl_tcp_mem'
        libbpf: failed to load object 'test_sysctl_loop2.o'
        test_bpf_verif_scale:FAIL:114
        #7/21 test_sysctl_loop2.o:FAIL
      This actually exposed a bpf program bug. In test_sysctl_loop{1,2}, we have code
      like
        const char tcp_mem_name[] = "<...long string...>";
        ...
        char name[64];
        ...
        for (i = 0; i < sizeof(tcp_mem_name); ++i)
            if (name[i] != tcp_mem_name[i])
                return 0;
      In the above code, if sizeof(tcp_mem_name) > 64, name[i] access may be
      out of bound. The sizeof(tcp_mem_name) is 59 for test_sysctl_loop1.c and
      79 for test_sysctl_loop2.c.
      
      Without promotion-to-global change, old compiler generates code where
      the overflowed stack access is actually filled with valid value, so hiding
      the bpf program bug. With promotion-to-global change, the code is different,
      more specifically, the previous loading constants to stack is gone, and
      "name" occupies stack[-64:0] and overflow access triggers a verifier error.
      To fix the issue, adjust "name" buffer size properly.
      Reported-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/20200909171542.3673449-1-yhs@fb.com
      7fb5eefd
    • Yonghong Song's avatar
      selftests/bpf: Add test for map_ptr arithmetic · e6054fc1
      Yonghong Song authored
      Change selftest map_ptr_kern.c with disabling inlining for
      one of subtests, which will fail the test without previous
      verifier change. Also added to verifier test for both
      "map_ptr += scalar" and "scalar += map_ptr" arithmetic.
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/20200908175703.2463721-1-yhs@fb.com
      e6054fc1
    • Yonghong Song's avatar
      bpf: Permit map_ptr arithmetic with opcode add and offset 0 · 7c696732
      Yonghong Song authored
      Commit 41c48f3a ("bpf: Support access
      to bpf map fields") added support to access map fields
      with CORE support. For example,
      
                  struct bpf_map {
                          __u32 max_entries;
                  } __attribute__((preserve_access_index));
      
                  struct bpf_array {
                          struct bpf_map map;
                          __u32 elem_size;
                  } __attribute__((preserve_access_index));
      
                  struct {
                          __uint(type, BPF_MAP_TYPE_ARRAY);
                          __uint(max_entries, 4);
                          __type(key, __u32);
                          __type(value, __u32);
                  } m_array SEC(".maps");
      
                  SEC("cgroup_skb/egress")
                  int cg_skb(void *ctx)
                  {
                          struct bpf_array *array = (struct bpf_array *)&m_array;
      
                          /* .. array->map.max_entries .. */
                  }
      
      In kernel, bpf_htab has similar structure,
      
      	    struct bpf_htab {
      		    struct bpf_map map;
                          ...
                  }
      
      In the above cg_skb(), to access array->map.max_entries, with CORE, the clang will
      generate two builtin's.
                  base = &m_array;
                  /* access array.map */
                  map_addr = __builtin_preserve_struct_access_info(base, 0, 0);
                  /* access array.map.max_entries */
                  max_entries_addr = __builtin_preserve_struct_access_info(map_addr, 0, 0);
      	    max_entries = *max_entries_addr;
      
      In the current llvm, if two builtin's are in the same function or
      in the same function after inlining, the compiler is smart enough to chain
      them together and generates like below:
                  base = &m_array;
                  max_entries = *(base + reloc_offset); /* reloc_offset = 0 in this case */
      and we are fine.
      
      But if we force no inlining for one of functions in test_map_ptr() selftest, e.g.,
      check_default(), the above two __builtin_preserve_* will be in two different
      functions. In this case, we will have code like:
         func check_hash():
                  reloc_offset_map = 0;
                  base = &m_array;
                  map_base = base + reloc_offset_map;
                  check_default(map_base, ...)
         func check_default(map_base, ...):
                  max_entries = *(map_base + reloc_offset_max_entries);
      
      In kernel, map_ptr (CONST_PTR_TO_MAP) does not allow any arithmetic.
      The above "map_base = base + reloc_offset_map" will trigger a verifier failure.
        ; VERIFY(check_default(&hash->map, map));
        0: (18) r7 = 0xffffb4fe8018a004
        2: (b4) w1 = 110
        3: (63) *(u32 *)(r7 +0) = r1
         R1_w=invP110 R7_w=map_value(id=0,off=4,ks=4,vs=8,imm=0) R10=fp0
        ; VERIFY_TYPE(BPF_MAP_TYPE_HASH, check_hash);
        4: (18) r1 = 0xffffb4fe8018a000
        6: (b4) w2 = 1
        7: (63) *(u32 *)(r1 +0) = r2
         R1_w=map_value(id=0,off=0,ks=4,vs=8,imm=0) R2_w=invP1 R7_w=map_value(id=0,off=4,ks=4,vs=8,imm=0) R10=fp0
        8: (b7) r2 = 0
        9: (18) r8 = 0xffff90bcb500c000
        11: (18) r1 = 0xffff90bcb500c000
        13: (0f) r1 += r2
        R1 pointer arithmetic on map_ptr prohibited
      
      To fix the issue, let us permit map_ptr + 0 arithmetic which will
      result in exactly the same map_ptr.
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/20200908175702.2463625-1-yhs@fb.com
      7c696732
  5. 07 Sep, 2020 3 commits
  6. 04 Sep, 2020 6 commits
    • Daniel T. Lee's avatar
      samples, bpf: Add xsk_fwd test file to .gitignore · f9bec5d7
      Daniel T. Lee authored
      This commit adds xsk_fwd test file to .gitignore which is newly added
      to samples/bpf.
      Signed-off-by: default avatarDaniel T. Lee <danieltimlee@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20200904063434.24963-2-danieltimlee@gmail.com
      f9bec5d7
    • Daniel T. Lee's avatar
      samples, bpf: Replace bpf_program__title() with bpf_program__section_name() · 698584df
      Daniel T. Lee authored
      From commit 52109584 ("libbpf: Deprecate notion of BPF program
      "title" in favor of "section name""), the term title has been replaced
      with section name in libbpf.
      
      Since the bpf_program__title() has been deprecated, this commit
      switches this function to bpf_program__section_name(). Due to
      this commit, the compilation warning issue has also been resolved.
      
      Fixes: 52109584 ("libbpf: Deprecate notion of BPF program "title" in favor of "section name"")
      Signed-off-by: default avatarDaniel T. Lee <danieltimlee@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20200904063434.24963-1-danieltimlee@gmail.com
      698584df
    • Andrii Nakryiko's avatar
      libbpf: Fix potential multiplication overflow · 8eb62958
      Andrii Nakryiko authored
      Detected by LGTM static analyze in Github repo, fix potential multiplication
      overflow before result is casted to size_t.
      
      Fixes: 8505e870 ("libbpf: Implement generalized .BTF.ext func/line info adjustment")
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20200904041611.1695163-2-andriin@fb.com
      8eb62958
    • Andrii Nakryiko's avatar
      libbpf: Fix another __u64 cast in printf · 17e54b09
      Andrii Nakryiko authored
      Another issue of __u64 needing either %lu or %llu, depending on the
      architecture. Fix with cast to `unsigned long long`.
      
      Fixes: 7e06aad5 ("libbpf: Add multi-prog section support for struct_ops")
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20200904041611.1695163-1-andriin@fb.com
      17e54b09
    • Hao Luo's avatar
      selftests/bpf: Fix check in global_data_init. · 95cec14b
      Hao Luo authored
      The returned value of bpf_object__open_file() should be checked with
      libbpf_get_error() rather than NULL. This fix prevents test_progs from
      crash when test_global_data.o is not present.
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/20200903200528.747884-1-haoluo@google.com
      95cec14b
    • Alexei Starovoitov's avatar
      Merge branch 'libbpf-support-bpf-to-bpf-calls' · b4ff7ad7
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      Currently, libbpf supports a limited form of BPF-to-BPF subprogram calls. The
      restriction is that entry-point BPF program should use *all* of defined
      sub-programs in BPF .o file. If any of the subprograms is not used, such
      entry-point BPF program will be rejected by verifier as containing unreachable
      dead code. This is not a big limitation for cases with single entry-point BPF
      programs, but is quite a heavy restriction for multi-programs that use only
      partially overlapping set of subprograms.
      
      This patch set removes all such restrictions and adds complete support for
      using BPF sub-program calls on BPF side. This is achieved through libbpf
      tracking subprograms individually and detecting which subprograms are used by
      any given entry-point BPF program, and subsequently only appending and
      relocating code for just those used subprograms.
      
      In addition, libbpf now also supports multiple entry-point BPF programs within
      the same ELF section. This allows to structure code so that there are few
      variants of BPF programs of the same type and attaching to the same target
      (e.g., for tracepoints and kprobes) without the need to worry about ELF
      section name clashes.
      
      This patch set opens way for more wider adoption of BPF subprogram calls,
      especially for real-world production use-cases with complicated net of
      subprograms. This will allow to further scale BPF verification process through
      good use of global functions, which can be verified independently. This is
      also important prerequisite for static linking which allows static BPF
      libraries to not worry about naming clashes for section names, as well as use
      static non-inlined functions (subprograms) without worries of verifier
      rejecting program due to dead code.
      
      Patch set is structured as follows:
      - patched 1-6 contain all the libbpf changes necessary to support multi-prog
        sections and bpf2bpf subcalls;
      - patch 7 adds dedicated selftests validating all combinations of possible
        sub-calls (within and across sections, static vs global functions);
      - patch 8 deprecated bpf_program__title() in favor of
        bpf_program__section_name(). The intent was to also deprecate
        bpf_object__find_program_by_title() as it's now non-sensical with multiple
        programs per section. But there were too many selftests uses of this and
        I didn't want to delay this patches further and make it even bigger, so left
        it for a follow up cleanup;
      - patches 9-10 remove uses for title-related APIs from bpftool and
        bpf_program__title() use from selftests;
      - patch 11 is converting fexit_bpf2bpf to have explicit subtest (it does
        contain 4 subtests, which are not handled as sub-tests);
      - patches 12-14 convert few complicated BPF selftests to use __noinline
        functions to further validate correctness of libbpf's bpf2bpf processing
        logic.
      
      v2->v3:
        - explained subprog relocation algorithm in more details (Alexei);
        - pyperf, strobelight and cls_redirect got new subprog variants, leaving
          other modes intact (Alexei);
      v1->v2:
        - rename DEPRECATED to LIBBPF_DEPRECATED to avoid name clashes;
        - fix test_subprogs build;
        - convert a bunch of complicated selftests to __noinline (Alexei).
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      b4ff7ad7