1. 20 4月, 2022 2 次提交
    • P
      libbpf: Fix usdt_cookie being cast to 32 bits · 5af25a41
      Pu Lehui 提交于
      The usdt_cookie is defined as __u64, which should not be
      used as a long type because it will be cast to 32 bits
      in 32-bit platforms.
      Signed-off-by: NPu Lehui <pulehui@huawei.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419145238.482134-2-pulehui@huawei.com
      5af25a41
    • A
      libbpf: Support opting out from autoloading BPF programs declaratively · a3820c48
      Andrii Nakryiko 提交于
      Establish SEC("?abc") naming convention (i.e., adding question mark in
      front of otherwise normal section name) that allows to set corresponding
      program's autoload property to false. This is effectively just
      a declarative way to do bpf_program__set_autoload(prog, false).
      
      Having a way to do this declaratively in BPF code itself is useful and
      convenient for various scenarios. E.g., for testing, when BPF object
      consists of multiple independent BPF programs that each needs to be
      tested separately. Opting out all of them by default and then setting
      autoload to true for just one of them at a time simplifies testing code
      (see next patch for few conversions in BPF selftests taking advantage of
      this new feature).
      
      Another real-world use case is in libbpf-tools for cases when different
      BPF programs have to be picked depending on particulars of the host
      kernel due to various incompatible changes (like kernel function renames
      or signature change, or to pick kprobe vs fentry depending on
      corresponding kernel support for the latter). Marking all the different
      BPF program candidates as non-autoloaded declaratively makes this more
      obvious in BPF source code and allows simpler code in user-space code.
      
      When BPF program marked as SEC("?abc") it is otherwise treated just like
      SEC("abc") and bpf_program__section_name() reported will be "abc".
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220419002452.632125-1-andrii@kernel.org
      a3820c48
  2. 11 4月, 2022 1 次提交
    • R
      libbpf: Fix a bug with checking bpf_probe_read_kernel() support in old kernels · d252a4a4
      Runqing Yang 提交于
      Background:
      Libbpf automatically replaces calls to BPF bpf_probe_read_{kernel,user}
      [_str]() helpers with bpf_probe_read[_str](), if libbpf detects that
      kernel doesn't support new APIs. Specifically, libbpf invokes the
      probe_kern_probe_read_kernel function to load a small eBPF program into
      the kernel in which bpf_probe_read_kernel API is invoked and lets the
      kernel checks whether the new API is valid. If the loading fails, libbpf
      considers the new API invalid and replaces it with the old API.
      
      static int probe_kern_probe_read_kernel(void)
      {
      	struct bpf_insn insns[] = {
      		BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),	/* r1 = r10 (fp) */
      		BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),	/* r1 += -8 */
      		BPF_MOV64_IMM(BPF_REG_2, 8),		/* r2 = 8 */
      		BPF_MOV64_IMM(BPF_REG_3, 0),		/* r3 = 0 */
      		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
      		BPF_EXIT_INSN(),
      	};
      	int fd, insn_cnt = ARRAY_SIZE(insns);
      
      	fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL,
                                 "GPL", insns, insn_cnt, NULL);
      	return probe_fd(fd);
      }
      
      Bug:
      On older kernel versions [0], the kernel checks whether the version
      number provided in the bpf syscall, matches the LINUX_VERSION_CODE.
      If not matched, the bpf syscall fails. eBPF However, the
      probe_kern_probe_read_kernel code does not set the kernel version
      number provided to the bpf syscall, which causes the loading process
      alwasys fails for old versions. It means that libbpf will replace the
      new API with the old one even the kernel supports the new one.
      
      Solution:
      After a discussion in [1], the solution is using BPF_PROG_TYPE_TRACEPOINT
      program type instead of BPF_PROG_TYPE_KPROBE because kernel does not
      enfoce version check for tracepoint programs. I test the patch in old
      kernels (4.18 and 4.19) and it works well.
      
        [0] https://elixir.bootlin.com/linux/v4.19/source/kernel/bpf/syscall.c#L1360
        [1] Closes: https://github.com/libbpf/libbpf/issues/473Signed-off-by: NRunqing Yang <rainkin1993@gmail.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220409144928.27499-1-rainkin1993@gmail.com
      d252a4a4
  3. 09 4月, 2022 2 次提交
  4. 08 4月, 2022 2 次提交
  5. 06 4月, 2022 1 次提交
    • A
      libbpf: Wire up USDT API and bpf_link integration · 2e4913e0
      Andrii Nakryiko 提交于
      Wire up libbpf USDT support APIs without yet implementing all the
      nitty-gritty details of USDT discovery, spec parsing, and BPF map
      initialization.
      
      User-visible user-space API is simple and is conceptually very similar
      to uprobe API.
      
      bpf_program__attach_usdt() API allows to programmatically attach given
      BPF program to a USDT, specified through binary path (executable or
      shared lib), USDT provider and name. Also, just like in uprobe case, PID
      filter is specified (0 - self, -1 - any process, or specific PID).
      Optionally, USDT cookie value can be specified. Such single API
      invocation will try to discover given USDT in specified binary and will
      use (potentially many) BPF uprobes to attach this program in correct
      locations.
      
      Just like any bpf_program__attach_xxx() APIs, bpf_link is returned that
      represents this attachment. It is a virtual BPF link that doesn't have
      direct kernel object, as it can consist of multiple underlying BPF
      uprobe links. As such, attachment is not atomic operation and there can
      be brief moment when some USDT call sites are attached while others are
      still in the process of attaching. This should be taken into
      consideration by user. But bpf_program__attach_usdt() guarantees that
      in the case of success all USDT call sites are successfully attached, or
      all the successfuly attachments will be detached as soon as some USDT
      call sites failed to be attached. So, in theory, there could be cases of
      failed bpf_program__attach_usdt() call which did trigger few USDT
      program invocations. This is unavoidable due to multi-uprobe nature of
      USDT and has to be handled by user, if it's important to create an
      illusion of atomicity.
      
      USDT BPF programs themselves are marked in BPF source code as either
      SEC("usdt"), in which case they won't be auto-attached through
      skeleton's <skel>__attach() method, or it can have a full definition,
      which follows the spirit of fully-specified uprobes:
      SEC("usdt/<path>:<provider>:<name>"). In the latter case skeleton's
      attach method will attempt auto-attachment. Similarly, generic
      bpf_program__attach() will have enought information to go off of for
      parameterless attachment.
      
      USDT BPF programs are actually uprobes, and as such for kernel they are
      marked as BPF_PROG_TYPE_KPROBE.
      
      Another part of this patch is USDT-related feature probing:
        - BPF cookie support detection from user-space;
        - detection of kernel support for auto-refcounting of USDT semaphore.
      
      The latter is optional. If kernel doesn't support such feature and USDT
      doesn't rely on USDT semaphores, no error is returned. But if libbpf
      detects that USDT requires setting semaphores and kernel doesn't support
      this, libbpf errors out with explicit pr_warn() message. Libbpf doesn't
      support poking process's memory directly to increment semaphore value,
      like BCC does on legacy kernels, due to inherent raciness and danger of
      such process memory manipulation. Libbpf let's kernel take care of this
      properly or gives up.
      
      Logistically, all the extra USDT-related infrastructure of libbpf is put
      into a separate usdt.c file and abstracted behind struct usdt_manager.
      Each bpf_object has lazily-initialized usdt_manager pointer, which is
      only instantiated if USDT programs are attempted to be attached. Closing
      BPF object frees up usdt_manager resources. usdt_manager keeps track of
      USDT spec ID assignment and few other small things.
      
      Subsequent patches will fill out remaining missing pieces of USDT
      initialization and setup logic.
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Reviewed-by: NAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220404234202.331384-3-andrii@kernel.org
      2e4913e0
  6. 05 4月, 2022 1 次提交
  7. 04 4月, 2022 3 次提交
  8. 21 3月, 2022 2 次提交
  9. 18 3月, 2022 5 次提交
  10. 06 3月, 2022 2 次提交
    • A
      libbpf: Support custom SEC() handlers · 697f104d
      Andrii Nakryiko 提交于
      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: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Tested-by: NAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: NAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-3-andrii@kernel.org
      697f104d
    • A
      libbpf: Allow BPF program auto-attach handlers to bail out · 4fa5bcfe
      Andrii Nakryiko 提交于
      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: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Tested-by: NAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: NAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-2-andrii@kernel.org
      4fa5bcfe
  11. 03 3月, 2022 1 次提交
  12. 01 3月, 2022 1 次提交
  13. 24 2月, 2022 1 次提交
  14. 23 2月, 2022 1 次提交
  15. 17 2月, 2022 2 次提交
  16. 08 2月, 2022 1 次提交
  17. 04 2月, 2022 1 次提交
  18. 03 2月, 2022 1 次提交
  19. 02 2月, 2022 1 次提交
  20. 26 1月, 2022 1 次提交
  21. 25 1月, 2022 2 次提交
  22. 22 1月, 2022 1 次提交
  23. 21 1月, 2022 1 次提交
    • A
      libbpf: deprecate legacy BPF map definitions · 93b8952d
      Andrii Nakryiko 提交于
      Enact deprecation of legacy BPF map definition in SEC("maps") ([0]). For
      the definitions themselves introduce LIBBPF_STRICT_MAP_DEFINITIONS flag
      for libbpf strict mode. If it is set, error out on any struct
      bpf_map_def-based map definition. If not set, libbpf will print out
      a warning for each legacy BPF map to raise awareness that it goes away.
      
      For any use of BPF_ANNOTATE_KV_PAIR() macro providing a legacy way to
      associate BTF key/value type information with legacy BPF map definition,
      warn through libbpf's pr_warn() error message (but don't fail BPF object
      open).
      
      BPF-side struct bpf_map_def is marked as deprecated. User-space struct
      bpf_map_def has to be used internally in libbpf, so it is left
      untouched. It should be enough for bpf_map__def() to be marked
      deprecated to raise awareness that it goes away.
      
      bpftool is an interesting case that utilizes libbpf to open BPF ELF
      object to generate skeleton. As such, even though bpftool itself uses
      full on strict libbpf mode (LIBBPF_STRICT_ALL), it has to relax it a bit
      for BPF map definition handling to minimize unnecessary disruptions. So
      opt-out of LIBBPF_STRICT_MAP_DEFINITIONS for bpftool. User's code that
      will later use generated skeleton will make its own decision whether to
      enforce LIBBPF_STRICT_MAP_DEFINITIONS or not.
      
      There are few tests in selftests/bpf that are consciously using legacy
      BPF map definitions to test libbpf functionality. For those, temporary
      opt out of LIBBPF_STRICT_MAP_DEFINITIONS mode for the duration of those
      tests.
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/272Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20220120060529.1890907-4-andrii@kernel.orgSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      93b8952d
  24. 13 1月, 2022 1 次提交
  25. 06 1月, 2022 3 次提交