Skip to content
Snippets Groups Projects
  1. Apr 05, 2022
    • Andrii Nakryiko's avatar
      libbpf: Add BPF-side of USDT support · d72e2968
      Andrii Nakryiko authored
      
      Add BPF-side implementation of libbpf-provided USDT support. This
      consists of single header library, usdt.bpf.h, which is meant to be used
      from user's BPF-side source code. This header is added to the list of
      installed libbpf header, along bpf_helpers.h and others.
      
      BPF-side implementation consists of two BPF maps:
        - spec map, which contains "a USDT spec" which encodes information
          necessary to be able to fetch USDT arguments and other information
          (argument count, user-provided cookie value, etc) at runtime;
        - IP-to-spec-ID map, which is only used on kernels that don't support
          BPF cookie feature. It allows to lookup spec ID based on the place
          in user application that triggers USDT program.
      
      These maps have default sizes, 256 and 1024, which are chosen
      conservatively to not waste a lot of space, but handling a lot of common
      cases. But there could be cases when user application needs to either
      trace a lot of different USDTs, or USDTs are heavily inlined and their
      arguments are located in a lot of differing locations. For such cases it
      might be necessary to size those maps up, which libbpf allows to do by
      overriding BPF_USDT_MAX_SPEC_CNT and BPF_USDT_MAX_IP_CNT macros.
      
      It is an important aspect to keep in mind. Single USDT (user-space
      equivalent of kernel tracepoint) can have multiple USDT "call sites".
      That is, single logical USDT is triggered from multiple places in user
      application. This can happen due to function inlining. Each such inlined
      instance of USDT invocation can have its own unique USDT argument
      specification (instructions about the location of the value of each of
      USDT arguments). So while USDT looks very similar to usual uprobe or
      kernel tracepoint, under the hood it's actually a collection of uprobes,
      each potentially needing different spec to know how to fetch arguments.
      
      User-visible API consists of three helper functions:
        - bpf_usdt_arg_cnt(), which returns number of arguments of current USDT;
        - bpf_usdt_arg(), which reads value of specified USDT argument (by
          it's zero-indexed position) and returns it as 64-bit value;
        - bpf_usdt_cookie(), which functions like BPF cookie for USDT
          programs; this is necessary as libbpf doesn't allow specifying actual
          BPF cookie and utilizes it internally for USDT support implementation.
      
      Each bpf_usdt_xxx() APIs expect struct pt_regs * context, passed into
      BPF program. On kernels that don't support BPF cookie it is used to
      fetch absolute IP address of the underlying uprobe.
      
      usdt.bpf.h also provides BPF_USDT() macro, which functions like
      BPF_PROG() and BPF_KPROBE() and allows much more user-friendly way to
      get access to USDT arguments, if USDT definition is static and known to
      the user. It is expected that majority of use cases won't have to use
      bpf_usdt_arg_cnt() and bpf_usdt_arg() directly and BPF_USDT() will cover
      all their needs.
      
      Last, usdt.bpf.h is utilizing BPF CO-RE for one single purpose: to
      detect kernel support for BPF cookie. If BPF CO-RE dependency is
      undesirable, user application can redefine BPF_USDT_HAS_BPF_COOKIE to
      either a boolean constant (or equivalently zero and non-zero), or even
      point it to its own .rodata variable that can be specified from user's
      application user-space code. It is important that
      BPF_USDT_HAS_BPF_COOKIE is known to BPF verifier as static value (thus
      .rodata and not just .data), as otherwise BPF code will still contain
      bpf_get_attach_cookie() BPF helper call and will fail validation at
      runtime, if not dead-code eliminated.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220404234202.331384-2-andrii@kernel.org
      d72e2968
  2. Apr 04, 2022
  3. Mar 26, 2022
  4. Mar 21, 2022
  5. Mar 18, 2022
  6. Mar 09, 2022
  7. Mar 08, 2022
  8. Mar 05, 2022
    • Andrii Nakryiko's avatar
      libbpf: Support custom SEC() handlers · 697f104d
      Andrii Nakryiko authored
      
      Allow registering and unregistering custom handlers for BPF program.
      This allows user applications and libraries to plug into libbpf's
      declarative SEC() definition handling logic. This allows to offload
      complex and intricate custom logic into external libraries, but still
      provide a great user experience.
      
      One such example is USDT handling library, which has a lot of code and
      complexity which doesn't make sense to put into libbpf directly, but it
      would be really great for users to be able to specify BPF programs with
      something like SEC("usdt/<path-to-binary>:<usdt_provider>:<usdt_name>")
      and have correct BPF program type set (BPF_PROGRAM_TYPE_KPROBE, as it is
      uprobe) and even support BPF skeleton's auto-attach logic.
      
      In some cases, it might be even good idea to override libbpf's default
      handling, like for SEC("perf_event") programs. With custom library, it's
      possible to extend logic to support specifying perf event specification
      right there in SEC() definition without burdening libbpf with lots of
      custom logic or extra library dependecies (e.g., libpfm4). With current
      patch it's possible to override libbpf's SEC("perf_event") handling and
      specify a completely custom ones.
      
      Further, it's possible to specify a generic fallback handling for any
      SEC() that doesn't match any other custom or standard libbpf handlers.
      This allows to accommodate whatever legacy use cases there might be, if
      necessary.
      
      See doc comments for libbpf_register_prog_handler() and
      libbpf_unregister_prog_handler() for detailed semantics.
      
      This patch also bumps libbpf development version to v0.8 and adds new
      APIs there.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-3-andrii@kernel.org
      697f104d
    • Andrii Nakryiko's avatar
      libbpf: Allow BPF program auto-attach handlers to bail out · 4fa5bcfe
      Andrii Nakryiko authored
      
      Allow some BPF program types to support auto-attach only in subste of
      cases. Currently, if some BPF program type specifies attach callback, it
      is assumed that during skeleton attach operation all such programs
      either successfully attach or entire skeleton attachment fails. If some
      program doesn't support auto-attachment from skeleton, such BPF program
      types shouldn't have attach callback specified.
      
      This is limiting for cases when, depending on how full the SEC("")
      definition is, there could either be enough details to support
      auto-attach or there might not be and user has to use some specific API
      to provide more details at runtime.
      
      One specific example of such desired behavior might be SEC("uprobe"). If
      it's specified as just uprobe auto-attach isn't possible. But if it's
      SEC("uprobe/<some_binary>:<some_func>") then there are enough details to
      support auto-attach. Note that there is a somewhat subtle difference
      between auto-attach behavior of BPF skeleton and using "generic"
      bpf_program__attach(prog) (which uses the same attach handlers under the
      cover). Skeleton allow some programs within bpf_object to not have
      auto-attach implemented and doesn't treat that as an error. Instead such
      BPF programs are just skipped during skeleton's (optional) attach step.
      bpf_program__attach(), on the other hand, is called when user *expects*
      auto-attach to work, so if specified program doesn't implement or
      doesn't support auto-attach functionality, that will be treated as an
      error.
      
      Another improvement to the way libbpf is handling SEC()s would be to not
      require providing dummy kernel function name for kprobe. Currently,
      SEC("kprobe/whatever") is necessary even if actual kernel function is
      determined by user at runtime and bpf_program__attach_kprobe() is used
      to specify it. With changes in this patch, it's possible to support both
      SEC("kprobe") and SEC("kprobe/<actual_kernel_function"), while only in
      the latter case auto-attach will be performed. In the former one, such
      kprobe will be skipped during skeleton attach operation.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-2-andrii@kernel.org
      4fa5bcfe
  9. Mar 03, 2022
  10. Mar 01, 2022
  11. Feb 28, 2022
  12. Feb 23, 2022
  13. Feb 22, 2022
  14. Feb 20, 2022
  15. Feb 17, 2022
  16. Feb 16, 2022
  17. Feb 12, 2022
  18. Feb 11, 2022
  19. Feb 10, 2022
  20. Feb 09, 2022
Loading