1. 29 11月, 2016 3 次提交
  2. 25 11月, 2016 1 次提交
    • E
      tools lib bpf: Fix maps resolution · 4708bbda
      Eric Leblond 提交于
      It is not correct to assimilate the elf data of the maps section to an
      array of map definition. In fact the sizes differ. The offset provided
      in the symbol section has to be used instead.
      
      This patch fixes a bug causing a elf with two maps not to load
      correctly.
      
      Wang Nan added:
      
      This patch requires a name for each BPF map, so array of BPF maps is not
      allowed. This restriction is reasonable, because kernel verifier forbid
      indexing BPF map from such array unless the index is a fixed value, but
      if the index is fixed why not merging it into name?
      
      For example:
      
      Program like this:
        ...
        unsigned long cpu = get_smp_processor_id();
        int *pval = map_lookup_elem(&map_array[cpu], &key);
        ...
      
      Generates bytecode like this:
      
      0: (b7) r1 = 0
      1: (63) *(u32 *)(r10 -4) = r1
      2: (b7) r1 = 680997
      3: (63) *(u32 *)(r10 -8) = r1
      4: (85) call 8
      5: (67) r0 <<= 4
      6: (18) r1 = 0x112dd000
      8: (0f) r0 += r1
      9: (bf) r2 = r10
      10: (07) r2 += -4
      11: (bf) r1 = r0
      12: (85) call 1
      
      Where instruction 8 is the computation, 8 and 11 render r1 to an invalid
      value for function map_lookup_elem, causes verifier report error.
      Signed-off-by: NEric Leblond <eric@regit.org>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Wang Nan <wangnan0@huawei.com>
      [ Merge bpf_object__init_maps_name into bpf_object__init_maps.
        Fix segfault for buggy BPF script Validate obj->maps ]
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/20161115040617.69788-5-wangnan0@huawei.comSigned-off-by: NWang Nan <wangnan0@huawei.com>
      Signed-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      4708bbda
  3. 26 7月, 2016 1 次提交
  4. 14 7月, 2016 2 次提交
  5. 13 7月, 2016 1 次提交
  6. 05 7月, 2016 1 次提交
  7. 29 6月, 2016 1 次提交
  8. 07 6月, 2016 7 次提交
  9. 26 1月, 2016 1 次提交
    • W
      perf bpf: Check relocation target section · 666810e8
      Wang Nan 提交于
      Libbpf should check the target section before doing relocation to ensure
      the relocation is correct. If not, a bug in LLVM causes an error. See
      [1].  Also, if an incorrect BPF script uses both global variable and
      map, global variable whould be treated as map and be relocated without
      error.
      
      This patch saves the id of the map section into obj->efile and compare
      target section of a relocation symbol against it during relocation.
      
      Previous patch introduces a test case about this problem.  After this
      patch:
      
        # ~/perf test BPF
        37: Test BPF filter                                          :
        37.1: Test basic BPF filtering                               : Ok
        37.2: Test BPF prologue generation                           : Ok
        37.3: Test BPF relocation checker                            : Ok
      
        # perf test -v BPF
        ...
        37.3: Test BPF relocation checker                            :
        ...
        libbpf: loading object '[bpf_relocation_test]' from buffer
        libbpf: section .strtab, size 126, link 0, flags 0, type=3
        libbpf: section .text, size 0, link 0, flags 6, type=1
        libbpf: section .data, size 0, link 0, flags 3, type=1
        libbpf: section .bss, size 0, link 0, flags 3, type=8
        libbpf: section func=sys_write, size 104, link 0, flags 6, type=1
        libbpf: found program func=sys_write
        libbpf: section .relfunc=sys_write, size 16, link 10, flags 0, type=9
        libbpf: section maps, size 16, link 0, flags 3, type=1
        libbpf: maps in [bpf_relocation_test]: 16 bytes
        libbpf: section license, size 4, link 0, flags 3, type=1
        libbpf: license of [bpf_relocation_test] is GPL
        libbpf: section version, size 4, link 0, flags 3, type=1
        libbpf: kernel version of [bpf_relocation_test] is 40400
        libbpf: section .symtab, size 144, link 1, flags 0, type=2
        libbpf: map 0 is "my_table"
        libbpf: collecting relocating info for: 'func=sys_write'
        libbpf: Program 'func=sys_write' contains non-map related relo data pointing to section 65522
        bpf: failed to load buffer
        Compile BPF program failed.
        test child finished with 0
        ---- end ----
        Test BPF filter subtest 2: Ok
      
      [1] https://llvm.org/bugs/show_bug.cgi?id=26243Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@kernel.org>
      Tested-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Li Zefan <lizefan@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Will Deacon <will.deacon@arm.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1453715801-7732-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      666810e8
  10. 12 1月, 2016 2 次提交
  11. 11 1月, 2016 1 次提交
  12. 07 1月, 2016 1 次提交
  13. 11 12月, 2015 2 次提交
    • W
      tools lib bpf: Fetch map names from correct strtab · 77ba9a5b
      Wang Nan 提交于
      Namhyung Kim pointed out a potential problem in original code that it
      fetches names of maps from section header string table, which is used
      to store section names.
      
      Original code doesn't cause error because of a LLVM behavior that, it
      combines shstrtab into strtab. For example:
      
       $ echo 'int func() {return 0;}' | x86_64-oe-linux-clang -x c -o temp.o -c -
       $ readelf -h ./temp.o
       ELF Header:
         Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00
         ...
         Section header string table index: 1
       $ readelf -S ./temp.o
       There are 10 section headers, starting at offset 0x288:
      
       Section Headers:
         [Nr] Name              Type             Address           Offset
              Size              EntSize          Flags  Link  Info  Align
         [ 0]                   NULL             0000000000000000  00000000
              0000000000000000  0000000000000000           0     0     0
         [ 1] .strtab           STRTAB           0000000000000000  00000230
              0000000000000051  0000000000000000           0     0     1
              ...
       $ readelf -p .strtab ./temp.o
      
       String dump of section '.strtab':
         [     1]  .text
         [     7]  .comment
         [    10]  .bss
         [    15]  .note.GNU-stack
         [    25]  .rela.eh_frame
         [    34]  func
         [    39]  .strtab
         [    41]  .symtab
         [    49]  .data
         [    4f]  -
      
       $ readelf -p .shstrtab ./temp.o
       readelf: Warning: Section '.shstrtab' was not dumped because it does not exist!
      
      Where, 'section header string table index' points to '.strtab', and
      symbol names are also stored there.
      
      However, in case of gcc:
      
       $ echo 'int func() {return 0;}' | gcc -x c -o temp.o -c -
       $ readelf -p .shstrtab ./temp.o
      
       String dump of section '.shstrtab':
         [     1]  .symtab
         [     9]  .strtab
         [    11]  .shstrtab
         [    1b]  .text
         [    21]  .data
         [    27]  .bss
         [    2c]  .comment
         [    35]  .note.GNU-stack
         [    45]  .rela.eh_frame
       $ readelf -p .strtab ./temp.o
      
       String dump of section '.strtab':
         [     1]  func
      
      They are separated sections.
      
      Although original code doesn't cause error, we'd better use canonical
      method for fetching symbol names to avoid potential behavior changing.
      This patch learns from readelf's code, fetches string from sh_link
      of .symbol section.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Reported-and-Acked-by: NNamhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1449541544-67621-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      77ba9a5b
    • W
      tools lib bpf: Check return value of strdup when reading map names · 973170e6
      Wang Nan 提交于
      Commit 561bbcca ("tools lib bpf:
      Extract and collect map names from BPF object file") forgets checking
      return value of strdup(). This patch fixes it. It also checks names
      pointer before strcmp() for safety.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NNamhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Fixes: 561bbcca ("tools lib bpf: Extract and collect map names from BPF object file")
      Link: http://lkml.kernel.org/r/1449541544-67621-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      973170e6
  14. 28 11月, 2015 2 次提交
  15. 27 11月, 2015 1 次提交
  16. 26 11月, 2015 1 次提交
  17. 19 11月, 2015 1 次提交
    • W
      bpf tools: Load a program with different instances using preprocessor · b580563e
      Wang Nan 提交于
      This patch is a preparation for BPF prologue support which allows
      generating a series of BPF bytecode for fetching kernel data before
      calling program code. With the newly introduced multiple instances
      support, perf is able to create different prologues for different kprobe
      points.
      
      Before this patch, a bpf_program can be loaded into kernel only once,
      and get the only resulting fd. What this patch does is to allow creating
      and loading different variants of one bpf_program, then fetching their
      fds.
      
      Here we describe the basic idea in this patch. The detailed description
      of the newly introduced APIs can be found in comments in the patch body.
      
      The key of this patch is the new mechanism in bpf_program__load().
      Instead of loading BPF program into kernel directly, it calls a
      'pre-processor' to generate program instances which would be finally
      loaded into the kernel based on the original code. To enable the
      generation of multiple instances, libbpf passes an index to the
      pre-processor so it know which instance is being loaded.
      
      Pre-processor should be called from libbpf's user (perf) using
      bpf_program__set_prep(). The number of instances and the relationship
      between indices and the target instance should be clear when calling
      bpf_program__set_prep().
      
      To retrieve a fd for a specific instance of a program,
      bpf_program__nth_fd() is introduced. It returns the resulting fd
      according to index.
      Signed-off-by: NHe Kuang <hekuang@huawei.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1447675815-166222-8-git-send-email-wangnan0@huawei.comSigned-off-by: NWang Nan <wangnan0@huawei.com>
      [ Enclosed multi-line if/else blocks with {}, (*func_ptr)() -> func_ptr() ]
      Signed-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      b580563e
  18. 07 11月, 2015 2 次提交
    • W
      bpf tools: Add new API bpf_object__get_kversion() · 45825d8a
      Wang Nan 提交于
      bpf_object__get_kversion() can be used to fetch value of object's
      'version' section. Following patch will use it for error reporting.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1446817783-86722-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      45825d8a
    • W
      bpf tools: Improve libbpf error reporting · 6371ca3b
      Wang Nan 提交于
      In this patch, a series of libbpf specific error numbers and
      libbpf_strerror() are introduced to help reporting errors.
      
      Functions are updated to pass correct the error number through the
      CHECK_ERR() macro.
      
      All users of bpf_object__open{_buffer}() and bpf_program__title() in
      perf are modified accordingly. In addition, due to the error codes
      changing, bpf__strerror_load() is also modified to use them.
      
      bpf__strerror_head() is also changed accordingly so it can parse libbpf
      errors. bpf_loader_strerror() is introduced for that purpose, and will
      be improved by the following patch.
      
      load_program() is improved not to dump log buffer if it is empty. log
      buffer is also used to deduce whether the error was caused by an invalid
      program or other problem.
      
      v1 -> v2:
      
       - Using macro for error code.
      
       - Fetch error message based on array index, eliminate for-loop.
      
       - Use log buffer to detect the reason of failure. 3 new error code
         are introduced to replace LIBBPF_ERRNO__LOAD.
      
      In v1:
      
        # perf record -e ./test_ill_program.o ls
        event syntax error: './test_ill_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        # perf record -e ./test_kversion_nomatch_program.o ls
        event syntax error: './test_kversion_nomatch_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        # perf record -e ./test_big_program.o ls
        event syntax error: './test_big_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        In v2:
      
        # perf record -e ./test_ill_program.o ls
        event syntax error: './test_ill_program.o'
                             \___ Kernel verifier blocks program loading
        SKIP
      
        # perf record -e ./test_kversion_nomatch_program.o
        event syntax error: './test_kversion_nomatch_program.o'
                             \___ Incorrect kernel version
        SKIP
        (Will be further improved by following patches)
      
        # perf record -e ./test_big_program.o
        event syntax error: './test_big_program.o'
                             \___ Program too big
        SKIP
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Tested-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1446817783-86722-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      6371ca3b
  19. 05 11月, 2015 1 次提交
  20. 03 11月, 2015 1 次提交
  21. 29 9月, 2015 2 次提交
  22. 22 9月, 2015 2 次提交
    • A
      tools lib bpf: Use FEATURE_USER to allow building in the same dir as perf · 65f041be
      Arnaldo Carvalho de Melo 提交于
      When building tools/lib/bpf as part of the tools/perf/ build process,
      which will happend when we introduce a patch wiring that up, we end up
      stomping on the feature detection caching mechanism, that uses a file in
      the output directory (O=) that is shared by libbpf and perf to check if
      something changed from one build to another that requires redoing the
      feature detection process.
      
      By using the recently introduced FEATURE_USER tools/build/ knob, we can
      avoid that:
      
      Before, every make invokation would run the feature detection:
      
        $ make O=/tmp/build/perf -C tools/perf
        make: Entering directory '/home/git/linux/tools/perf'
        Auto-detecting system features:
        ...                         dwarf: [ on  ]
        ...                         glibc: [ on  ]
        <SNIP>
        ...                     get_cpuid: [ on  ]
        ...                           bpf: [ on  ]
      
          GEN      perf-archive
          GEN      perf-with-kcore
      
        Auto-detecting system features:
        ...                        libelf: [ on  ]
        ...                           bpf: [ on  ]
        <SNIP>
      
      After:
      
        $ make O=/tmp/build/perf -C tools/perf
        make: Entering directory '/home/git/linux/tools/perf'
          BUILD:   Doing 'make -j4' parallel build
        make: Leaving directory '/home/git/linux/tools/perf'
        $
      
      Because we now have two different feature detection state files:
      
        $ ls -la /tmp/build/perf/FEATURE-DUMP*
        -rw-rw-r--. 1 acme acme 338 Sep 21 17:25 /tmp/build/perf/FEATURE-DUMP
        -rw-rw-r--. 1 acme acme  33 Sep 21 17:25 /tmp/build/perf/FEATURE-DUMP.libbpf
        $
      Acked-by: NJiri Olsa <jolsa@kernel.org>
      Cc: Adrian Hunter <adrian.hunter@intel.com>
      Cc: Alexei Starovoitov <ast@plumgrid.com>
      Cc: Borislav Petkov <bp@suse.de>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: Frederic Weisbecker <fweisbec@gmail.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Stephane Eranian <eranian@google.com>
      Cc: Wang Nan <wangnan0@huawei.com>
      Cc: pi3orama@163.com
      Fixes: 1b76c13e ("bpf tools: Introduce 'bpf' library and add bpf feature check")
      Link: http://lkml.kernel.org/n/tip-s6ev9wfqy7pvvs58emys2g90@git.kernel.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      65f041be
    • A
      tools lib bpf: Fix up FEATURE_{TESTS,DISPLAY} usage · 20517cd9
      Arnaldo Carvalho de Melo 提交于
      When libbpf was introduced it wrongly asked for the "libelf" and "bpf"
      feature tests to be performed (via FEATURE_TESTS), while asking that
      "libbpf", "libelf-mmap", "libelf-getphdrnum" and "bpf" to have the
      result of its respective tests to be displayed (via FEATURE_DISPLAY).
      
      Due to another recently bug fixed in the tools/build/ infrastructure
      ("tools build: Fixup feature detection display function name") the
      results for the entries in the FEATURE_DISPLAY, for this case, were
      appearing as all succeeding, when two of them (the ones only on the
      DISPLAY) were not even being performed.
      
      Before:
      
        $ make -C tools/lib/bpf/
        make: Entering directory '/home/git/linux/tools/lib/bpf'
        Auto-detecting system features:
        ...                        libelf: [ on  ]
        ...             libelf-getphdrnum: [ OFF ]
        ...                   libelf-mmap: [ OFF ]
        ...                           bpf: [ on  ]
        <SNIP>
      
      After, with FEATURE_TESTS == FEATURE_DISPLAY:
      
        Auto-detecting system features:
        ...                        libelf: [ on  ]
        ...             libelf-getphdrnum: [ on  ]
        ...                   libelf-mmap: [ on  ]
        ...                           bpf: [ on  ]
        <SNIP>
      
      I just inverted, so that it tests the four features but displays just
      the libelf and mmap ones, to make it more compact. So it becomes:
      
        $ make -C tools/lib/bpf/
        make: Entering directory '/home/git/linux/tools/lib/bpf'
      
        Auto-detecting system features:
        ...                        libelf: [ on  ]
        ...                           bpf: [ on  ]
      Acked-by: NJiri Olsa <jolsa@kernel.org>
      Cc: Adrian Hunter <adrian.hunter@intel.com>
      Cc: Alexei Starovoitov <ast@plumgrid.com>
      Cc: Borislav Petkov <bp@suse.de>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: Frederic Weisbecker <fweisbec@gmail.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Stephane Eranian <eranian@google.com>
      Cc: Wang Nan <wangnan0@huawei.com>
      Cc: pi3orama@163.com
      Fixes: 1b76c13e ("bpf tools: Introduce 'bpf' library and add bpf feature check")
      Link: http://lkml.kernel.org/n/tip-y4bd59e6j9rzzojiyeqrg2jq@git.kernel.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      20517cd9
  23. 01 9月, 2015 1 次提交
    • W
      bpf tools: New API to get name from a BPF object · acf860ae
      Wang Nan 提交于
      Before this patch there's no way to connect a loaded bpf object
      to its source file. However, during applying perf's '--filter' to BPF
      object, without this connection makes things harder, because perf loads
      all programs together, but '--filter' setting is for each object.
      
      The API of bpf_object__open_buffer() is changed to allow passing a name.
      Fortunately, at this time there's only one user of it (perf test LLVM),
      so we change it together.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Cc: Alexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1440742821-44548-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      acf860ae
  24. 07 8月, 2015 2 次提交