1. Dec 11, 2022
  2. Dec 10, 2022
  3. Dec 09, 2022
  4. Dec 08, 2022
    • Alexei Starovoitov's avatar
      Merge branch 'Document some recent core kfunc additions' · 2d141236
      Alexei Starovoitov authored
      David Vernet says:
      
      ====================
      
      A series of recent patch sets introduced kfuncs that allowed struct
      task_struct and struct cgroup objects to be used as kptrs. These were
      introduced in [0], [1], and [2].
      
      [0]: https://lore.kernel.org/lkml/20221120051004.3605026-1-void@manifault.com/
      [1]: https://lore.kernel.org/lkml/20221122145300.251210-2-void@manifault.com/T/
      [2]: https://lore.kernel.org/lkml/20221122055458.173143-1-void@manifault.com/
      
      These are "core" kfuncs, in that they may be used by a wide variety of
      possible BPF tracepoint or struct_ops programs, and are defined in
      kernel/bpf/helpers.c. Even though as kfuncs they have no ABI stability
      guarantees, they should still be properly documented. This patch set
      adds that documentation.
      
      Some other kfuncs were added recently as well, such as
      bpf_rcu_read_lock() and bpf_rcu_read_unlock(). Those could and should be
      added to this "Core kfuncs" section as well in subsequent patch sets.
      
      Note that this patch set does not contain documentation for
      bpf_task_acquire_not_zero(), or bpf_task_kptr_get(). As discussed in
      [3], those kfuncs currently always return NULL pending resolution on how
      to properly protect their arguments using RCU.
      
      [3]: https://lore.kernel.org/all/20221206210538.597606-1-void@manifault.com/
      
      
      ---
      Changelog:
      v2 -> v3:
      - Don't document bpf_task_kptr_get(), and instead provide a more
        substantive example for bpf_cgroup_kptr_get().
      - Further clarify expected behavior of bpf_task_from_pid() in comments
        (Alexei)
      
      v1 -> v2:
      - Expand comment to specify that a map holds a reference to a task kptr
        if we don't end up releasing it (Alexei)
      - Just read task->pid instead of using a probed read (Alexei)
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2d141236
    • David Vernet's avatar
      bpf/docs: Document struct cgroup * kfuncs · 36aa10ff
      David Vernet authored
      
      
      bpf_cgroup_acquire(), bpf_cgroup_release(), bpf_cgroup_kptr_get(), and
      bpf_cgroup_ancestor(), are kfuncs that were recently added to
      kernel/bpf/helpers.c. These are "core" kfuncs in that they're available
      for use in any tracepoint or struct_ops BPF program. Though they have no
      ABI stability guarantees, we should still document them. This patch adds
      a struct cgroup * subsection to the Core kfuncs section which describes
      each of these kfuncs.
      
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221207204911.873646-3-void@manifault.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      36aa10ff
    • David Vernet's avatar
      bpf/docs: Document struct task_struct * kfuncs · 25c5e92d
      David Vernet authored
      
      
      bpf_task_acquire(), bpf_task_release(), and bpf_task_from_pid() are
      kfuncs that were recently added to kernel/bpf/helpers.c. These are
      "core" kfuncs in that they're available for use for any tracepoint or
      struct_ops BPF program. Though they have no ABI stability guarantees, we
      should still document them. This patch adds a new Core kfuncs section to
      the BPF kfuncs doc, and adds entries for all of these task kfuncs.
      
      Note that bpf_task_kptr_get() is not documented, as it still returns
      NULL while we're working to resolve how it can use RCU to ensure struct
      task_struct * lifetime.
      
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221207204911.873646-2-void@manifault.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      25c5e92d
    • Andrii Nakryiko's avatar
      selftests/bpf: convert dynptr_fail and map_kptr_fail subtests to generic tester · 26c386ec
      Andrii Nakryiko authored
      
      
      Convert big chunks of dynptr and map_kptr subtests to use generic
      verification_tester. They are switched from using manually maintained
      tables of test cases, specifying program name and expected error
      verifier message, to btf_decl_tag-based annotations directly on
      corresponding BPF programs: __failure to specify that BPF program is
      expected to fail verification, and __msg() to specify expected log
      message.
      
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20221207201648.2990661-2-andrii@kernel.org
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      26c386ec
    • Andrii Nakryiko's avatar
      selftests/bpf: add generic BPF program tester-loader · 537c3f66
      Andrii Nakryiko authored
      
      
      It's become a common pattern to have a collection of small BPF programs
      in one BPF object file, each representing one test case. On user-space
      side of such tests we maintain a table of program names and expected
      failure or success, along with optional expected verifier log message.
      
      This works, but each set of tests reimplement this mundane code over and
      over again, which is a waste of time for anyone trying to add a new set
      of tests. Furthermore, it's quite error prone as it's way too easy to miss
      some entries in these manually maintained test tables (as evidences by
      dynptr_fail tests, in which ringbuf_release_uninit_dynptr subtest was
      accidentally missed; this is fixed in next patch).
      
      So this patch implements generic test_loader, which accepts skeleton
      name and handles the rest of details: opens and loads BPF object file,
      making sure each program is tested in isolation. Optionally each test
      case can specify expected BPF verifier log message. In case of failure,
      tester makes sure to report verifier log, but it also reports verifier
      log in verbose mode unconditionally.
      
      Now, the interesting deviation from existing custom implementations is
      the use of btf_decl_tag attribute to specify expected-to-fail vs
      expected-to-succeed markers and, optionally, expected log message
      directly next to BPF program source code, eliminating the need to
      manually create and update table of tests.
      
      We define few macros wrapping btf_decl_tag with a convention that all
      values of btf_decl_tag start with "comment:" prefix, and then utilizing
      a very simple "just_some_text_tag" or "some_key_name=<value>" pattern to
      define things like expected success/failure, expected verifier message,
      extra verifier log level (if necessary). This approach is demonstrated
      by next patch in which two existing sets of failure tests are converted.
      
      Tester supports both expected-to-fail and expected-to-succeed programs,
      though this patch set didn't convert any existing expected-to-succeed
      programs yet, as existing tests couple BPF program loading with their
      further execution through attach or test_prog_run. One way to allow
      testing scenarios like this would be ability to specify custom callback,
      executed for each successfully loaded BPF program. This is left for
      follow up patches, after some more analysis of existing test cases.
      
      This test_loader is, hopefully, a start of a test_verifier-like runner,
      but integrated into test_progs infrastructure. It will allow much better
      "user experience" of defining low-level verification tests that can take
      advantage of all the libbpf-provided nicety features on BPF side: global
      variables, declarative maps, etc.  All while having a choice of defining
      it in C or as BPF assembly (through __attribute__((naked)) functions and
      using embedded asm), depending on what makes most sense in each
      particular case. This will be explored in follow up patches as well.
      
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20221207201648.2990661-1-andrii@kernel.org
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      537c3f66
    • Andrii Nakryiko's avatar
      bpf: Remove unused insn_cnt argument from visit_[func_call_]insn() · dcb2288b
      Andrii Nakryiko authored
      
      
      Number of total instructions in BPF program (including subprogs) can and
      is accessed from env->prog->len. visit_func_call_insn() doesn't do any
      checks against insn_cnt anymore, relying on push_insn() to do this check
      internally. So remove unnecessary insn_cnt input argument from
      visit_func_call_insn() and visit_insn() functions.
      
      Suggested-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20221207195534.2866030-1-andrii@kernel.org
      dcb2288b
    • Alexei Starovoitov's avatar
      Merge "do not rely on ALLOW_ERROR_INJECTION for fmod_ret" into bpf-next · 0a6ea1ce
      Alexei Starovoitov authored
      Merge commit 5b481aca
      
       ("bpf: do not rely on ALLOW_ERROR_INJECTION for fmod_ret")
      from hid tree into bpf-next.
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0a6ea1ce
  5. Dec 07, 2022
    • Benjamin Tissoires's avatar
      bpf: do not rely on ALLOW_ERROR_INJECTION for fmod_ret · 5b481aca
      Benjamin Tissoires authored
      The current way of expressing that a non-bpf kernel component is willing
      to accept that bpf programs can be attached to it and that they can change
      the return value is to abuse ALLOW_ERROR_INJECTION.
      This is debated in the link below, and the result is that it is not a
      reasonable thing to do.
      
      Reuse the kfunc declaration structure to also tag the kernel functions
      we want to be fmodret. This way we can control from any subsystem which
      functions are being modified by bpf without touching the verifier.
      
      Link: https://lore.kernel.org/all/20221121104403.1545f9b5@gandalf.local.home/
      
      
      Suggested-by: default avatarAlexei Starovoitov <alexei.starovoitov@gmail.com>
      Signed-off-by: default avatarBenjamin Tissoires <benjamin.tissoires@redhat.com>
      Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/r/20221206145936.922196-2-benjamin.tissoires@redhat.com
      5b481aca
    • Christophe JAILLET's avatar
      net: xsk: Don't include <linux/rculist.h> · e9b4aeed
      Christophe JAILLET authored
      
      
      There is no need to include <linux/rculist.h> here.
      
      Prefer the less invasive <linux/types.h> which is needed for 'hlist_head'.
      
      Signed-off-by: default avatarChristophe JAILLET <christophe.jaillet@wanadoo.fr>
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Link: https://lore.kernel.org/r/88d6a1d88764cca328610854f890a9ca1f4b029e.1670086246.git.christophe.jaillet@wanadoo.fr
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      e9b4aeed
    • Alexei Starovoitov's avatar
      Merge branch 'Refactor verifier prune and jump point handling' · f0c5a2d9
      Alexei Starovoitov authored
      
      
      Andrii Nakryiko says:
      
      ====================
      
      Disentangle prune and jump points in BPF verifier code. They are conceptually
      independent but currently coupled together. This small patch set refactors
      related code and make it possible to have some instruction marked as pruning
      or jump point independently.
      
      Besides just conceptual cleanliness, this allows to remove unnecessary jump
      points (saving a tiny bit of performance and memory usage, potentially), and
      even more importantly it allows for clean extension of special pruning points,
      similarly to how it's done for BPF_FUNC_timer_set_callback. This will be used
      by future patches implementing open-coded BPF iterators.
      
      v1->v2:
        - clarified path #3 commit message and a comment in the code (John);
        - added back mark_jmp_point() to right after subprog call to record
          non-linear implicit jump from BPF_EXIT to right after CALL <subprog>.
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f0c5a2d9
    • Andrii Nakryiko's avatar
      bpf: remove unnecessary prune and jump points · 618945fb
      Andrii Nakryiko authored
      
      
      Don't mark some instructions as jump points when there are actually no
      jumps and instructions are just processed sequentially. Such case is
      handled naturally by precision backtracking logic without the need to
      update jump history. See get_prev_insn_idx(). It goes back linearly by
      one instruction, unless current top of jmp_history is pointing to
      current instruction. In such case we use `st->jmp_history[cnt - 1].prev_idx`
      to find instruction from which we jumped to the current instruction
      non-linearly.
      
      Also remove both jump and prune point marking for instruction right
      after unconditional jumps, as program flow can get to the instruction
      right after unconditional jump instruction only if there is a jump to
      that instruction from somewhere else in the program. In such case we'll
      mark such instruction as prune/jump point because it's a destination of
      a jump.
      
      This change has no changes in terms of number of instructions or states
      processes across Cilium and selftests programs.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Link: https://lore.kernel.org/r/20221206233345.438540-4-andrii@kernel.org
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      618945fb
    • Andrii Nakryiko's avatar
      bpf: mostly decouple jump history management from is_state_visited() · a095f421
      Andrii Nakryiko authored
      
      
      Jump history updating and state equivalence checks are conceptually
      independent, so move push_jmp_history() out of is_state_visited(). Also
      make a decision whether to perform state equivalence checks or not one
      layer higher in do_check(), keeping is_state_visited() unconditionally
      performing state checks.
      
      push_jmp_history() should be performed after state checks. There is just
      one small non-uniformity. When is_state_visited() finds already
      validated equivalent state, it propagates precision marks to current
      state's parent chain. For this to work correctly, jump history has to be
      updated, so is_state_visited() is doing that internally.
      
      But if no equivalent verified state is found, jump history has to be
      updated in a newly cloned child state, so is_jmp_point()
      + push_jmp_history() is performed after is_state_visited() exited with
      zero result, which means "proceed with validation".
      
      This change has no functional changes. It's not strictly necessary, but
      feels right to decouple these two processes.
      
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20221206233345.438540-3-andrii@kernel.org
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a095f421
    • Andrii Nakryiko's avatar
      bpf: decouple prune and jump points · bffdeaa8
      Andrii Nakryiko authored
      
      
      BPF verifier marks some instructions as prune points. Currently these
      prune points serve two purposes.
      
      It's a point where verifier tries to find previously verified state and
      check current state's equivalence to short circuit verification for
      current code path.
      
      But also currently it's a point where jump history, used for precision
      backtracking, is updated. This is done so that non-linear flow of
      execution could be properly backtracked.
      
      Such coupling is coincidental and unnecessary. Some prune points are not
      part of some non-linear jump path, so don't need update of jump history.
      On the other hand, not all instructions which have to be recorded in
      jump history necessarily are good prune points.
      
      This patch splits prune and jump points into independent flags.
      Currently all prune points are marked as jump points to minimize amount
      of changes in this patch, but next patch will perform some optimization
      of prune vs jmp point placement.
      
      No functional changes are intended.
      
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20221206233345.438540-2-andrii@kernel.org
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      bffdeaa8
    • Dave Marchevsky's avatar
      bpf: Loosen alloc obj test in verifier's reg_btf_record · d8939cb0
      Dave Marchevsky authored
      
      
      btf->struct_meta_tab is populated by btf_parse_struct_metas in btf.c.
      There, a BTF record is created for any type containing a spin_lock or
      any next-gen datastructure node/head.
      
      Currently, for non-MAP_VALUE types, reg_btf_record will only search for
      a record using struct_meta_tab if the reg->type exactly matches
      (PTR_TO_BTF_ID | MEM_ALLOC). This exact match is too strict: an
      "allocated obj" type - returned from bpf_obj_new - might pick up other
      flags while working its way through the program.
      
      Loosen the check to be exact for base_type and just use MEM_ALLOC mask
      for type_flag.
      
      This patch is marked Fixes as the original intent of reg_btf_record was
      unlikely to have been to fail finding btf_record for valid alloc obj
      types with additional flags, some of which (e.g. PTR_UNTRUSTED)
      are valid register type states for alloc obj independent of this series.
      However, I didn't find a specific broken repro case outside of this
      series' added functionality, so it's possible that nothing was
      triggering this logic error before.
      
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
      Fixes: 4e814da0 ("bpf: Allow locking bpf_spin_lock in allocated objects")
      Link: https://lore.kernel.org/r/20221206231000.3180914-2-davemarchevsky@fb.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      d8939cb0
    • David Vernet's avatar
      bpf: Don't use rcu_users to refcount in task kfuncs · 156ed20d
      David Vernet authored
      A series of prior patches added some kfuncs that allow struct
      task_struct * objects to be used as kptrs. These kfuncs leveraged the
      'refcount_t rcu_users' field of the task for performing refcounting.
      This field was used instead of 'refcount_t usage', as we wanted to
      leverage the safety provided by RCU for ensuring a task's lifetime.
      
      A struct task_struct is refcounted by two different refcount_t fields:
      
      1. p->usage:     The "true" refcount field which task lifetime. The
      		 task is freed as soon as this refcount drops to 0.
      
      2. p->rcu_users: An "RCU users" refcount field which is statically
      		 initialized to 2, and is co-located in a union with
      		 a struct rcu_head field (p->rcu). p->rcu_users
      		 essentially encapsulates a single p->usage
      		 refcount, and when p->rcu_users goes to 0, an RCU
      		 callback is scheduled on the struct rcu_head which
      		 decrements the p->usage refcount.
      
      Our logic was that by using p->rcu_users, we would be able to use RCU to
      safely issue refcount_inc_not_zero() a task's rcu_users field to
      determine if a task could still be acquired, or was exiting.
      Unfortunately, this does not work due to p->rcu_users and p->rcu sharing
      a union. When p->rcu_users goes to 0, an RCU callback is scheduled to
      drop a single p->usage refcount, and because the fields share a union,
      the refcount immediately becomes nonzero again after the callback is
      scheduled.
      
      If we were to split the fields out of the union, this wouldn't be a
      problem. Doing so should also be rather non-controversial, as there are
      a number of places in struct task_struct that have padding which we
      could use to avoid growing the structure by splitting up the fields.
      
      For now, so as to fix the kfuncs to be correct, this patch instead
      updates bpf_task_acquire() and bpf_task_release() to use the p->usage
      field for refcounting via the get_task_struct() and put_task_struct()
      functions. Because we can no longer rely on RCU, the change also guts
      the bpf_task_acquire_not_zero() and bpf_task_kptr_get() functions
      pending a resolution on the above problem.
      
      In addition, the task fixes the kfunc and rcu_read_lock selftests to
      expect this new behavior.
      
      Fixes: 90660309 ("bpf: Add kfuncs for storing struct task_struct * as a kptr")
      Fixes: fca1aa75
      
       ("bpf: Handle MEM_RCU type properly")
      Reported-by: default avatarMatus Jokay <matus.jokay@stuba.sk>
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20221206210538.597606-1-void@manifault.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      156ed20d
    • Andrii Nakryiko's avatar
      Merge branch 'BPF selftests fixes' · 235d2ef2
      Andrii Nakryiko authored
      
      
      Daan De Meyer says:
      
      ====================
      
      This patch series fixes a few issues I've found while integrating the
      bpf selftests into systemd's mkosi development environment.
      ====================
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      235d2ef2
    • Daan De Meyer's avatar
      selftests/bpf: Use CONFIG_TEST_BPF=m instead of CONFIG_TEST_BPF=y · d0c0b48c
      Daan De Meyer authored
      
      
      CONFIG_TEST_BPF can only be a module, so let's indicate it as such in
      the selftests config.
      
      Signed-off-by: default avatarDaan De Meyer <daan.j.demeyer@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20221205131618.1524337-4-daan.j.demeyer@gmail.com
      d0c0b48c