1. 08 Oct, 2021 19 commits
    • Hou Tao's avatar
      bpf/selftests: Add test for writable bare tracepoint · fa7f17d0
      Hou Tao authored
      Add a writable bare tracepoint in bpf_testmod module, and
      trigger its calling when reading /sys/kernel/bpf_testmod
      with a specific buffer length. The reading will return
      the value in writable context if the early return flag
      is enabled in writable context.
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211004094857.30868-4-hotforest@gmail.com
      fa7f17d0
    • Hou Tao's avatar
      libbpf: Support detecting and attaching of writable tracepoint program · ccaf12d6
      Hou Tao authored
      Program on writable tracepoint is BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
      but its attachment is the same as BPF_PROG_TYPE_RAW_TRACEPOINT.
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211004094857.30868-3-hotforest@gmail.com
      ccaf12d6
    • Hou Tao's avatar
      bpf: Support writable context for bare tracepoint · 65223741
      Hou Tao authored
      Commit 9df1c28b ("bpf: add writable context for raw tracepoints")
      supports writable context for tracepoint, but it misses the support
      for bare tracepoint which has no associated trace event.
      
      Bare tracepoint is defined by DECLARE_TRACE(), so adding a corresponding
      DECLARE_TRACE_WRITABLE() macro to generate a definition in __bpf_raw_tp_map
      section for bare tracepoint in a similar way to DEFINE_TRACE_WRITABLE().
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211004094857.30868-2-hotforest@gmail.com
      65223741
    • Andrii Nakryiko's avatar
      Merge branch 'install libbpf headers when using the library' · 1c8dab7d
      Andrii Nakryiko authored
      Quentin Monnet says:
      
      ====================
      
      Libbpf is used at several locations in the repository. Most of the time,
      the tools relying on it build the library in its own directory, and include
      the headers from there. This works, but this is not the cleanest approach.
      It generates objects outside of the directory of the tool which is being
      built, and it also increases the risk that developers include a header file
      internal to libbpf, which is not supposed to be exposed to user
      applications.
      
      This set adjusts all involved Makefiles to make sure that libbpf is built
      locally (with respect to the tool's directory or provided build directory),
      and by ensuring that "make install_headers" is run from libbpf's Makefile
      to export user headers properly.
      
      This comes at a cost: given that the libbpf was so far mostly compiled in
      its own directory by the different components using it, compiling it once
      would be enough for all those components. With the new approach, each
      component compiles its own version. To mitigate this cost, efforts were
      made to reuse the compiled library when possible:
      
      - Make the bpftool version in samples/bpf reuse the library previously
        compiled for the selftests.
      - Make the bpftool version in BPF selftests reuse the library previously
        compiled for the selftests.
      - Similarly, make resolve_btfids in BPF selftests reuse the same compiled
        library.
      - Similarly, make runqslower in BPF selftests reuse the same compiled
        library; and make it rely on the bpftool version also compiled from the
        selftests (instead of compiling its own version).
      - runqslower, when compiled independently, needs its own version of
        bpftool: make them share the same compiled libbpf.
      
      As a result:
      
      - Compiling the samples/bpf should compile libbpf just once.
      - Compiling the BPF selftests should compile libbpf just once.
      - Compiling the kernel (with BTF support) should now lead to compiling
        libbpf twice: one for resolve_btfids, one for kernel/bpf/preload.
      - Compiling runqslower individually should compile libbpf just once. Same
        thing for bpftool, resolve_btfids, and kernel/bpf/preload/iterators.
      
      (Not accounting for the boostrap version of libbpf required by bpftool,
      which was already placed under a dedicated .../boostrap/libbpf/ directory,
      and for which the count remains unchanged.)
      
      A few commits in the series also contain drive-by clean-up changes for
      bpftool includes, samples/bpf/.gitignore, or test_bpftool_build.sh. Please
      refer to individual commit logs for details.
      
      v4:
        - Make the "libbpf_hdrs" dependency an order-only dependency in
          kernel/bpf/preload/Makefile, samples/bpf/Makefile, and
          tools/bpf/runqslower/Makefile. This is to avoid to unconditionally
          recompile the targets.
        - samples/bpf/.gitignore: prefix objects with a "/" to mark that we
          ignore them when at the root of the samples/bpf/ directory.
        - libbpf: add a commit to make "install_headers" depend on the header
          files, to avoid exporting again if the sources are older than the
          targets. This ensures that applications relying on those headers are
          not rebuilt unnecessarily.
        - bpftool: uncouple the copy of nlattr.h from libbpf target, to have it
          depend on the source header itself. By avoiding to reinstall this
          header every time, we avoid unnecessary builds of bpftool.
        - samples/bpf: Add a new commit to remove the FORCE dependency for
          libbpf, and replace it with a "$(wildcard ...)" on the .c/.h files in
          libbpf's directory. This is to avoid always recompiling libbpf/bpftool.
        - Adjust prefixes in commit subjects.
      
      v3:
        - Remove order-only dependencies on $(LIBBPF_INCLUDE) (or equivalent)
          directories, given that they are created by libbpf's Makefile.
        - Add libbpf as a dependency for bpftool/resolve_btfids/runqslower when
          they are supposed to reuse a libbpf compiled previously. This is to
          avoid having several libbpf versions being compiled simultaneously in
          the same directory with parallel builds. Even if this didn't show up
          during tests, let's remain on the safe side.
        - kernel/bpf/preload/Makefile: Rename libbpf-hdrs (dash) dependency as
          libbpf_hdrs.
        - samples/bpf/.gitignore: Add bpftool/
        - samples/bpf/Makefile: Change "/bin/rm -rf" to "$(RM) -r".
        - samples/bpf/Makefile: Add missing slashes for $(LIBBPF_OUTPUT) and
          $(LIBBPF_DESTDIR) when buildling bpftool
        - samples/bpf/Makefile: Add a dependency to libbpf's headers for
          $(TRACE_HELPERS).
        - bpftool's Makefile: Use $(LIBBPF) instead of equivalent (but longer)
          $(LIBBPF_OUTPUT)libbpf.a
        - BPF iterators' Makefile: build bpftool in .output/bpftool (instead of
          .output/), add and clean up variables.
        - runqslower's Makefile: Add an explicit dependency on libbpf's headers
          to several objects. The dependency is not required (libbpf should have
          been compiled and so the headers exported through other dependencies
          for those targets), but they better mark the logical dependency and
          should help if exporting the headers changed in the future.
        - New commit to add an "install-bin" target to bpftool, to avoid
          installing bash completion when buildling BPF iterators and selftests.
      
      v2: Declare an additional dependency on libbpf's headers for
          iterators/iterators.o in kernel/preload/Makefile to make sure that
          these headers are exported before we compile the object file (and not
          just before we link it).
      ====================
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      1c8dab7d
    • Quentin Monnet's avatar
      bpftool: Add install-bin target to install binary only · d7db0a4e
      Quentin Monnet authored
      With "make install", bpftool installs its binary and its bash completion
      file. Usually, this is what we want. But a few components in the kernel
      repository (namely, BPF iterators and selftests) also install bpftool
      locally before using it. In such a case, bash completion is not
      necessary and is just a useless build artifact.
      
      Let's add an "install-bin" target to bpftool, to offer a way to install
      the binary only.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-13-quentin@isovalent.com
      d7db0a4e
    • Quentin Monnet's avatar
      selftests/bpf: Better clean up for runqslower in test_bpftool_build.sh · 87ee33bf
      Quentin Monnet authored
      The script test_bpftool_build.sh attempts to build bpftool in the
      various supported ways, to make sure nothing breaks.
      
      One of those ways is to run "make tools/bpf" from the root of the kernel
      repository. This command builds bpftool, along with the other tools
      under tools/bpf, and runqslower in particular. After running the
      command and upon a successful bpftool build, the script attempts to
      cleanup the generated objects. However, after building with this target
      and in the case of runqslower, the files are not cleaned up as expected.
      
      This is because the "tools/bpf" target sets $(OUTPUT) to
      .../tools/bpf/runqslower/ when building the tool, causing the object
      files to be placed directly under the runqslower directory. But when
      running "cd tools/bpf; make clean", the value for $(OUTPUT) is set to
      ".output" (relative to the runqslower directory) by runqslower's
      Makefile, and this is where the Makefile looks for files to clean up.
      
      We cannot easily fix in the root Makefile (where "tools/bpf" is defined)
      or in tools/scripts/Makefile.include (setting $(OUTPUT)), where changing
      the way the output variables are passed would likely have consequences
      elsewhere. We could change runqslower's Makefile to build in the
      repository instead of in a dedicated ".output/", but doing so just to
      accommodate a test script doesn't sound great. Instead, let's just make
      sure that we clean up runqslower properly by adding the correct command
      to the script.
      
      This will attempt to clean runqslower twice: the first try with command
      "cd tools/bpf; make clean" will search for tools/bpf/runqslower/.output
      and fail to clean it (but will still clean the other tools, in
      particular bpftool), the second one (added in this commit) sets the
      $(OUTPUT) variable like for building with the "tool/bpf" target and
      should succeed.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-12-quentin@isovalent.com
      87ee33bf
    • Quentin Monnet's avatar
      samples/bpf: Do not FORCE-recompile libbpf · a60d24e7
      Quentin Monnet authored
      In samples/bpf/Makefile, libbpf has a FORCE dependency that force it to
      be rebuilt. I read this as a way to keep the library up-to-date, given
      that we do not have, in samples/bpf, a list of the source files for
      libbpf itself. However, a better approach would be to use the
      "$(wildcard ...)" function from make, and to have libbpf depend on all
      the .c and .h files in its directory. This is what samples/bpf/Makefile
      does for bpftool, and also what the BPF selftests' Makefile does for
      libbpf.
      
      Let's update the Makefile to avoid rebuilding libbpf all the time (and
      bpftool on top of it).
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-11-quentin@isovalent.com
      a60d24e7
    • Quentin Monnet's avatar
      samples/bpf: Install libbpf headers when building · 3f7a3318
      Quentin Monnet authored
      API headers from libbpf should not be accessed directly from the source
      directory. Instead, they should be exported with "make install_headers".
      Make sure that samples/bpf/Makefile installs the headers properly when
      building.
      
      The object compiled from and exported by libbpf are now placed into a
      subdirectory of sample/bpf/ instead of remaining in tools/lib/bpf/. We
      attempt to remove this directory on "make clean". However, the "clean"
      target re-enters the samples/bpf/ directory from the root of the
      repository ("$(MAKE) -C ../../ M=$(CURDIR) clean"), in such a way that
      $(srctree) and $(src) are not defined, making it impossible to use
      $(LIBBPF_OUTPUT) and $(LIBBPF_DESTDIR) in the recipe. So we only attempt
      to clean $(CURDIR)/libbpf, which is the default value.
      
      Add a dependency on libbpf's headers for the $(TRACE_HELPERS).
      
      We also change the output directory for bpftool, to place the generated
      objects under samples/bpf/bpftool/ instead of building in bpftool's
      directory directly. Doing so, we make sure bpftool reuses the libbpf
      library previously compiled and installed.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-10-quentin@isovalent.com
      3f7a3318
    • Quentin Monnet's avatar
      samples/bpf: Update .gitignore · 62fde1c8
      Quentin Monnet authored
      Update samples/bpf/.gitignore to ignore files generated when building
      the samples. Add:
      
        - vmlinux.h
        - the generated skeleton files (*.skel.h)
        - the samples/bpf/libbpf/ and .../bpftool/ directories, in preparation
          of a future commit which introduces a local output directory for
          building libbpf and bpftool.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-9-quentin@isovalent.com
      62fde1c8
    • Quentin Monnet's avatar
      bpf: iterators: Install libbpf headers when building · 7bf731dc
      Quentin Monnet authored
      API headers from libbpf should not be accessed directly from the
      library's source directory. Instead, they should be exported with "make
      install_headers". Let's make sure that bpf/preload/iterators/Makefile
      installs the headers properly when building.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-8-quentin@isovalent.com
      7bf731dc
    • Quentin Monnet's avatar
      bpf: preload: Install libbpf headers when building · bf607917
      Quentin Monnet authored
      API headers from libbpf should not be accessed directly from the
      library's source directory. Instead, they should be exported with "make
      install_headers". Let's make sure that bpf/preload/Makefile installs the
      headers properly when building.
      
      Note that we declare an additional dependency for iterators/iterators.o:
      having $(LIBBPF_A) as a dependency to "$(obj)/bpf_preload_umd" is not
      sufficient, as it makes it required only at the linking step. But we
      need libbpf to be compiled, and in particular its headers to be
      exported, before we attempt to compile iterators.o. The issue would not
      occur before this commit, because libbpf's headers were not exported and
      were always available under tools/lib/bpf.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-7-quentin@isovalent.com
      bf607917
    • Quentin Monnet's avatar
      tools/runqslower: Install libbpf headers when building · be79505c
      Quentin Monnet authored
      API headers from libbpf should not be accessed directly from the
      library's source directory. Instead, they should be exported with "make
      install_headers". Let's make sure that runqslower installs the
      headers properly when building.
      
      We use a libbpf_hdrs target to mark the logical dependency on libbpf's
      headers export for a number of object files, even though the headers
      should have been exported at this time (since bpftool needs them, and is
      required to generate the skeleton or the vmlinux.h).
      
      When descending from a parent Makefile, the specific output directories
      for building the library and exporting the headers are configurable with
      BPFOBJ_OUTPUT and BPF_DESTDIR, respectively. This is in addition to
      OUTPUT, on top of which those variables are constructed by default.
      
      Also adjust the Makefile for the BPF selftests. We pass a number of
      variables to the "make" invocation, because we want to point runqslower
      to the (target) libbpf shared with other tools, instead of building its
      own version. In addition, runqslower relies on (target) bpftool, and we
      also want to pass the proper variables to its Makefile so that bpftool
      itself reuses the same libbpf.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-6-quentin@isovalent.com
      be79505c
    • Quentin Monnet's avatar
      tools/resolve_btfids: Install libbpf headers when building · 1478994a
      Quentin Monnet authored
      API headers from libbpf should not be accessed directly from the
      library's source directory. Instead, they should be exported with "make
      install_headers". Let's make sure that resolve_btfids installs the
      headers properly when building.
      
      When descending from a parent Makefile, the specific output directories
      for building the library and exporting the headers are configurable with
      LIBBPF_OUT and LIBBPF_DESTDIR, respectively. This is in addition to
      OUTPUT, on top of which those variables are constructed by default.
      
      Also adjust the Makefile for the BPF selftests in order to point to the
      (target) libbpf shared with other tools, instead of building a version
      specific to resolve_btfids. Remove libbpf's order-only dependencies on
      the include directories (they are created by libbpf and don't need to
      exist beforehand).
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-5-quentin@isovalent.com
      1478994a
    • Quentin Monnet's avatar
      bpftool: Install libbpf headers instead of including the dir · f012ade1
      Quentin Monnet authored
      Bpftool relies on libbpf, therefore it relies on a number of headers
      from the library and must be linked against the library. The Makefile
      for bpftool exposes these objects by adding tools/lib as an include
      directory ("-I$(srctree)/tools/lib"). This is a working solution, but
      this is not the cleanest one. The risk is to involuntarily include
      objects that are not intended to be exposed by the libbpf.
      
      The headers needed to compile bpftool should in fact be "installed" from
      libbpf, with its "install_headers" Makefile target. In addition, there
      is one header which is internal to the library and not supposed to be
      used by external applications, but that bpftool uses anyway.
      
      Adjust the Makefile in order to install the header files properly before
      compiling bpftool. Also copy the additional internal header file
      (nlattr.h), but call it out explicitly. Build (and install headers) in a
      subdirectory under bpftool/ instead of tools/lib/bpf/. When descending
      from a parent Makefile, this is configurable by setting the OUTPUT,
      LIBBPF_OUTPUT and LIBBPF_DESTDIR variables.
      
      Also adjust the Makefile for BPF selftests, so as to reuse the (host)
      libbpf compiled earlier and to avoid compiling a separate version of the
      library just for bpftool.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-4-quentin@isovalent.com
      f012ade1
    • Quentin Monnet's avatar
      bpftool: Remove unused includes to <bpf/bpf_gen_internal.h> · c66a248f
      Quentin Monnet authored
      It seems that the header file was never necessary to compile bpftool,
      and it is not part of the headers exported from libbpf. Let's remove the
      includes from prog.c and gen.c.
      
      Fixes: d510296d ("bpftool: Use syscall/loader program in "prog load" and "gen skeleton" command.")
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-3-quentin@isovalent.com
      c66a248f
    • Quentin Monnet's avatar
      libbpf: Skip re-installing headers file if source is older than target · b79c2ce3
      Quentin Monnet authored
      The "install_headers" target in libbpf's Makefile would unconditionally
      export all API headers to the target directory. When those headers are
      installed to compile another application, this means that make always
      finds newer dependencies for the source files relying on those headers,
      and deduces that the targets should be rebuilt.
      
      Avoid that by making "install_headers" depend on the source header
      files, and (re-)install them only when necessary.
      Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007194438.34443-2-quentin@isovalent.com
      b79c2ce3
    • Yucong Sun's avatar
      selftests/bpf: Fix btf_dump test under new clang · 7e3cbd34
      Yucong Sun authored
      New clang version changed ([0]) type name in dwarf from "long int" to "long",
      this is causing btf_dump tests to fail.
      
        [0] https://github.com/llvm/llvm-project/commit/f6a561c4d6754b13165a49990e8365d819f64c86Signed-off-by: default avatarYucong Sun <sunyucong@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211008173139.1457407-1-fallentree@fb.com
      7e3cbd34
    • Dave Marchevsky's avatar
      selftests/bpf: Remove SEC("version") from test progs · dd65acf7
      Dave Marchevsky authored
      Since commit 6c4fc209 ("bpf: remove useless version check for prog
      load") these "version" sections, which result in bpf_attr.kern_version
      being set, have been unnecessary.
      
      Remove them so that it's obvious to folks using selftests as a guide that
      "modern" BPF progs don't need this section.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007231234.2223081-1-davemarchevsky@fb.com
      dd65acf7
    • Song Liu's avatar
      selftests/bpf: Skip the second half of get_branch_snapshot in vm · aa67fdb4
      Song Liu authored
      VMs running on upstream 5.12+ kernel support LBR. However,
      bpf_get_branch_snapshot couldn't stop the LBR before too many entries
      are flushed. Skip the hit/waste test for VMs before we find a proper fix
      for LBR in VM.
      
      Fixes: 025bd7c7 ("selftests/bpf: Add test for bpf_get_branch_snapshot")
      Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211007050231.728496-1-songliubraving@fb.com
      aa67fdb4
  2. 07 Oct, 2021 4 commits
  3. 06 Oct, 2021 17 commits