1. 10 Jul, 2024 5 commits
    • Matt Bobrowski's avatar
      bpf: relax zero fixed offset constraint on KF_TRUSTED_ARGS/KF_RCU · 605c9699
      Matt Bobrowski authored
      Currently, BPF kfuncs which accept trusted pointer arguments
      i.e. those flagged as KF_TRUSTED_ARGS, KF_RCU, or KF_RELEASE, all
      require an original/unmodified trusted pointer argument to be supplied
      to them. By original/unmodified, it means that the backing register
      holding the trusted pointer argument that is to be supplied to the BPF
      kfunc must have its fixed offset set to zero, or else the BPF verifier
      will outright reject the BPF program load. However, this zero fixed
      offset constraint that is currently enforced by the BPF verifier onto
      BPF kfuncs specifically flagged to accept KF_TRUSTED_ARGS or KF_RCU
      trusted pointer arguments is rather unnecessary, and can limit their
      usability in practice. Specifically, it completely eliminates the
      possibility of constructing a derived trusted pointer from an original
      trusted pointer. To put it simply, a derived pointer is a pointer
      which points to one of the nested member fields of the object being
      pointed to by the original trusted pointer.
      
      This patch relaxes the zero fixed offset constraint that is enforced
      upon BPF kfuncs which specifically accept KF_TRUSTED_ARGS, or KF_RCU
      arguments. Although, the zero fixed offset constraint technically also
      applies to BPF kfuncs accepting KF_RELEASE arguments, relaxing this
      constraint for such BPF kfuncs has subtle and unwanted
      side-effects. This was discovered by experimenting a little further
      with an initial version of this patch series [0]. The primary issue
      with relaxing the zero fixed offset constraint on BPF kfuncs accepting
      KF_RELEASE arguments is that it'd would open up the opportunity for
      BPF programs to supply both trusted pointers and derived trusted
      pointers to them. For KF_RELEASE BPF kfuncs specifically, this could
      be problematic as resources associated with the backing pointer could
      be released by the backing BPF kfunc and cause instabilities for the
      rest of the kernel.
      
      With this new fixed offset semantic in-place for BPF kfuncs accepting
      KF_TRUSTED_ARGS and KF_RCU arguments, we now have more flexibility
      when it comes to the BPF kfuncs that we're able to introduce moving
      forward.
      
      Early discussions covering the possibility of relaxing the zero fixed
      offset constraint can be found using the link below. This will provide
      more context on where all this has stemmed from [1].
      
      Notably, pre-existing tests have been updated such that they provide
      coverage for the updated zero fixed offset
      functionality. Specifically, the nested offset test was converted from
      a negative to positive test as it was already designed to assert zero
      fixed offset semantics of a KF_TRUSTED_ARGS BPF kfunc.
      
      [0] https://lore.kernel.org/bpf/ZnA9ndnXKtHOuYMe@google.com/
      [1] https://lore.kernel.org/bpf/ZhkbrM55MKQ0KeIV@google.com/Signed-off-by: default avatarMatt Bobrowski <mattbobrowski@google.com>
      Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Link: https://lore.kernel.org/r/20240709210939.1544011-1-mattbobrowski@google.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      605c9699
    • Alexei Starovoitov's avatar
      Merge branch 'fix-libbpf-bpf-skeleton-forward-backward-compat' · 02779af2
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      Fix libbpf BPF skeleton forward/backward compat
      
      Fix recently identified (but long standing) bug with handling BPF skeleton
      forward and backward compatibility. On libbpf side, even though BPF skeleton
      was always designed to be forward and backwards compatible through recording
      actual size of constrituents of BPF skeleton itself (map/prog/var skeleton
      definitions), libbpf implementation did implicitly hard-code those sizes by
      virtue of using a trivial array access syntax.
      
      This issue will only affect libbpf used as a shared library. Statically
      compiled libbpfs will always be in sync with BPF skeleton, bypassing this
      problem altogether.
      
      This patch set fixes libbpf, but also mitigates the problem for old libbpf
      versions by teaching bpftool to generate more conservative BPF skeleton,
      if possible (i.e., if there are no struct_ops maps defined).
      
      v1->v2:
        - fix SOB, add acks, typo fixes (Quentin, Eduard);
        - improve reporting of skipped map auto-attachment (Alan, Eduard).
      ====================
      
      Link: https://lore.kernel.org/r/20240708204540.4188946-1-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      02779af2
    • Andrii Nakryiko's avatar
      libbpf: improve old BPF skeleton handling for map auto-attach · a459f4bb
      Andrii Nakryiko authored
      Improve how we handle old BPF skeletons when it comes to BPF map
      auto-attachment. Emit one warn-level message per each struct_ops map
      that could have been auto-attached, if user provided recent enough BPF
      skeleton version. Don't spam log if there are no relevant struct_ops
      maps, though.
      
      This should help users realize that they probably need to regenerate BPF
      skeleton header with more recent bpftool/libbpf-cargo (or whatever other
      means of BPF skeleton generation).
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20240708204540.4188946-4-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a459f4bb
    • Andrii Nakryiko's avatar
      libbpf: fix BPF skeleton forward/backward compat handling · 99fb9531
      Andrii Nakryiko authored
      BPF skeleton was designed from day one to be extensible. Generated BPF
      skeleton code specifies actual sizes of map/prog/variable skeletons for
      that reason and libbpf is supposed to work with newer/older versions
      correctly.
      
      Unfortunately, it was missed that we implicitly embed hard-coded most
      up-to-date (according to libbpf's version of libbpf.h header used to
      compile BPF skeleton header) sizes of those structs, which can differ
      from the actual sizes at runtime when libbpf is used as a shared
      library.
      
      We have a few places were we just index array of maps/progs/vars, which
      implicitly uses these potentially invalid sizes of structs.
      
      This patch aims to fix this problem going forward. Once this lands,
      we'll backport these changes in Github repo to create patched releases
      for older libbpfs.
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Fixes: d66562fb ("libbpf: Add BPF object skeleton support")
      Fixes: 430025e5 ("libbpf: Add subskeleton scaffolding")
      Fixes: 08ac454e ("libbpf: Auto-attach struct_ops BPF maps in BPF skeleton")
      Co-developed-by: default avatarMykyta Yatsenko <yatsenko@meta.com>
      Signed-off-by: default avatarMykyta Yatsenko <yatsenko@meta.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20240708204540.4188946-3-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      99fb9531
    • Andrii Nakryiko's avatar
      bpftool: improve skeleton backwards compat with old buggy libbpfs · 06e71ad5
      Andrii Nakryiko authored
      Old versions of libbpf don't handle varying sizes of bpf_map_skeleton
      struct correctly. As such, BPF skeleton generated by newest bpftool
      might not be compatible with older libbpf (though only when libbpf is
      used as a shared library), even though it, by design, should.
      
      Going forward libbpf will be fixed, plus we'll release bug fixed
      versions of relevant old libbpfs, but meanwhile try to mitigate from
      bpftool side by conservatively assuming older and smaller definition of
      bpf_map_skeleton, if possible. Meaning, if there are no struct_ops maps.
      
      If there are struct_ops, then presumably user would like to have
      auto-attaching logic and struct_ops map link placeholders, so use the
      full bpf_map_skeleton definition in that case.
      Acked-by: default avatarQuentin Monnet <qmo@kernel.org>
      Co-developed-by: default avatarMykyta Yatsenko <yatsenko@meta.com>
      Signed-off-by: default avatarMykyta Yatsenko <yatsenko@meta.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20240708204540.4188946-2-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      06e71ad5
  2. 09 Jul, 2024 21 commits
  3. 08 Jul, 2024 13 commits
  4. 06 Jul, 2024 1 commit