1. 07 Aug, 2015 12 commits
    • Wang Nan's avatar
      bpf tools: Collect relocation sections from SHT_REL sections · b62f06e8
      Wang Nan authored
      This patch collects relocation sections into 'struct object'.  Such
      sections are used for connecting maps to bpf programs. 'reloc' field in
      'struct bpf_object' is introduced for storing such information.
      
      This patch simply store the data into 'reloc' field. Following patch
      will parse them to know the exact instructions which are needed to be
      relocated.
      
      Note that the collected data will be invalid after ELF object file is
      closed.
      
      This is the second patch related to map relocation. The first one is
      'bpf tools: Collect symbol table from SHT_SYMTAB section'. The
      principle of map relocation is described in its commit message.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-14-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      b62f06e8
    • Wang Nan's avatar
      bpf tools: Collect eBPF programs from their own sections · a5b8bd47
      Wang Nan authored
      This patch collects all programs in an object file into an array of
      'struct bpf_program' for further processing. That structure is for
      representing each eBPF program. 'bpf_prog' should be a better name, but
      it has been used by linux/filter.h. Although it is a kernel space name,
      I still prefer to call it 'bpf_program' to prevent possible confusion.
      
      bpf_object__add_program() creates a new 'struct bpf_program' object.
      It first init a variable in stack using bpf_program__init(), then if
      success, enlarges obj->programs array and copy the new object in.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-13-git-send-email-wangnan0@huawei.com
      [ Made bpf_object__add_program() propagate the error (-EINVAL or -ENOMEM) ]
      Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      a5b8bd47
    • Wang Nan's avatar
      bpf tools: Collect symbol table from SHT_SYMTAB section · bec7d68c
      Wang Nan authored
      This patch collects symbols section. This section is useful when linking
      BPF maps.
      
      What 'bpf_map_xxx()' functions actually require are map's file
      descriptors (and the internal verifier converts fds into pointers to
      'struct bpf_map'), which we don't know when compiling. Therefore, we
      should make compiler generate a 'ldr_64 r1, <imm>' instruction, and
      fill the 'imm' field with the actual file descriptor when loading in
      libbpf.
      
      BPF programs should be written in this way:
      
       struct bpf_map_def SEC("maps") my_map = {
          .type = BPF_MAP_TYPE_HASH,
          .key_size = sizeof(unsigned long),
          .value_size = sizeof(unsigned long),
          .max_entries = 1000000,
       };
      
       SEC("my_func=sys_write")
       int my_func(void *ctx)
       {
           ...
           bpf_map_update_elem(&my_map, &key, &value, BPF_ANY);
           ...
       }
      
      Compiler should convert '&my_map' into a 'ldr_64, r1, <imm>'
      instruction, where imm should be the address of 'my_map'. According to
      the address, libbpf knows which map it actually referenced, and then
      fills the imm field with the 'fd' of that map created by it.
      
      However, since we never really 'link' the object file, the imm field is
      only a record in relocation section. Therefore libbpf should do the
      relocation:
      
       1. In relocation section (type == SHT_REL), positions of each such
          'ldr_64' instruction are recorded with a reference of an entry in
          symbol table (SHT_SYMTAB);
      
       2. From records in symbol table we can find the indics of map
          variables.
      
      Libbpf first record SHT_SYMTAB and positions of each instruction which
      required bu such operation. Then create file descriptor. Finally, after
      map creation complete, replace the imm field.
      
      This is the first patch of BPF map related stuff. It records SHT_SYMTAB
      into object's efile field for further use.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-12-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      bec7d68c
    • Wang Nan's avatar
      bpf tools: Collect map definitions from 'maps' section · 0b3d1efa
      Wang Nan authored
      If maps are used by eBPF programs, corresponding object file(s) should
      contain a section named 'map'. Which contains map definitions. This
      patch copies the data of the whole section. Map data parsing should be
      acted just before map loading.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-11-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      0b3d1efa
    • Wang Nan's avatar
      bpf tools: Collect version and license from ELF sections · cb1e5e96
      Wang Nan authored
      Expand bpf_obj_elf_collect() to collect license and kernel version
      information in eBPF object file. eBPF object file should have a section
      named 'license', which contains a string. It should also have a section
      named 'version', contains a u32 LINUX_VERSION_CODE.
      
      bpf_obj_validate() is introduced to validate object file after loaded.
      Currently it only check existence of 'version' section.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-10-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      cb1e5e96
    • Wang Nan's avatar
      bpf tools: Iterate over ELF sections to collect information · 29603665
      Wang Nan authored
      bpf_obj_elf_collect() is introduced to iterate over each elf sections to
      collection information in eBPF object files. This function will futher
      enhanced to collect license, kernel version, programs, configs and map
      information.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-9-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      29603665
    • Wang Nan's avatar
      bpf tools: Check endianness and make libbpf fail early · cc4228d5
      Wang Nan authored
      Check endianness according to EHDR. Code is taken from
      tools/perf/util/symbol-elf.c.
      
      Libbpf doesn't magically convert missmatched endianness. Even if we swap
      eBPF instructions to correct byte order, we are unable to deal with
      endianness in code logical generated by LLVM.
      
      Therefore, libbpf should simply reject missmatched ELF object, and let
      LLVM to create good code.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-8-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      cc4228d5
    • Wang Nan's avatar
      bpf tools: Read eBPF object from buffer · 6c956392
      Wang Nan authored
      To support dynamic compiling, this patch allows caller to pass a
      in-memory buffer to libbpf by bpf_object__open_buffer(). libbpf calls
      elf_memory() to open it as ELF object file.
      
      Because __bpf_object__open() collects all required data and won't need
      that buffer anymore, libbpf uses that buffer directly instead of clone a
      new buffer. Caller of libbpf can free that buffer or use it do other
      things after bpf_object__open_buffer() return.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-7-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      6c956392
    • Wang Nan's avatar
      bpf tools: Open eBPF object file and do basic validation · 1a5e3fb1
      Wang Nan authored
      This patch defines basic interface of libbpf. 'struct bpf_object' will
      be the handler of each object file. Its internal structure is hide to
      user. eBPF object files are compiled by LLVM as ELF format. In this
      patch, libelf is used to open those files, read EHDR and do basic
      validation according to e_type and e_machine.
      
      All elf related staffs are grouped together and reside in efile field of
      'struct bpf_object'. bpf_object__elf_finish() is introduced to clear it.
      
      After all eBPF programs in an object file are loaded, related ELF
      information is useless. Close the object file and free those memory.
      
      The zfree() and zclose() functions are introduced to ensure setting NULL
      pointers and negative file descriptors after resources are released.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-6-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      1a5e3fb1
    • Wang Nan's avatar
      bpf tools: Allow caller to set printing function · b3f59d66
      Wang Nan authored
      By libbpf_set_print(), users of libbpf are allowed to register he/she
      own debug, info and warning printing functions. Libbpf will use those
      functions to print messages. If not provided, default info and warning
      printing functions are fprintf(stderr, ...); default debug printing
      is NULL.
      
      This API is designed to be used by perf, enables it to register its own
      logging functions to make all logs uniform, instead of separated
      logging level control.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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/1435716878-189507-5-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      b3f59d66
    • Wang Nan's avatar
      bpf tools: Introduce 'bpf' library and add bpf feature check · 1b76c13e
      Wang Nan authored
      This is the first patch of libbpf. The goal of libbpf is to create a
      standard way for accessing eBPF object files. This patch creates
      'Makefile' and 'Build' for it, allows 'make' to build libbpf.a and
      libbpf.so, 'make install' to put them into proper directories.
      Most part of Makefile is borrowed from traceevent.
      
      Before building, it checks the existence of libelf in Makefile, and deny
      to build if not found. Instead of throwing an error if libelf not found,
      the error raises in a phony target "elfdep". This design is to ensure
      'make clean' still workable even if libelf is not found.
      
      Because libbpf requires 'kern_version' field set for 'union bpf_attr'
      (bpfdep" is used for that dependency), Kernel BPF API is also checked
      by intruducing a new feature check 'bpf' into tools/build/feature,
      which checks the existence and version of linux/bpf.h. When building
      libbpf, it searches that file from include/uapi/linux in kernel source
      tree (controlled by FEATURE_CHECK_CFLAGS-bpf). Since it searches kernel
      source tree it reside, installing of newest kernel headers is not
      required, except we are trying to port these files to an old kernel.
      
      To avoid checking that file when perf building, the newly introduced
      'bpf' feature check doesn't added into FEATURE_TESTS and
      FEATURE_DISPLAY by default in tools/build/Makefile.feature, but added
      into libbpf's specific.
      Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
      Acked-by: default avatarAlexei 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>
      Bcc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-4-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      1b76c13e
    • Ingo Molnar's avatar
      Merge tag 'perf-core-for-mingo' of... · 1354ac6a
      Ingo Molnar authored
      Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux into perf/core
      
      Pull perf/core improvements and fixes from Arnaldo Carvalho de Melo:
      
      User visible changes:
      
        - IPC and cycle accounting in 'perf annotate'. (Andi Kleen)
      
        - Display cycles in branch sort mode in 'perf report'. (Andi Kleen)
      
        - Add total time column to 'perf trace' syscall stats summary. (Milian Woff)
      
      Infrastructure changes:
      
        - PMU helpers to use in Intel PT. (Adrian Hunter)
      
        - Fix perf-with-kcore script not to split args with spaces. (Adrian Hunter)
      
        - Add empty Build files for some more architectures. (Ben Hutchings)
      
        - Move 'perf stat' config variables to a struct to allow using some
          of its functions in more places. (Jiri Olsa)
      
        - Add DWARF register names for 'xtensa' arch. (Max Filippov)
      
        - Implement BPF programs attached to uprobes. (Wang Nan)
      Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
      1354ac6a
  2. 06 Aug, 2015 27 commits
  3. 05 Aug, 2015 1 commit