1. 24 Nov, 2022 8 commits
  2. 23 Nov, 2022 9 commits
  3. 22 Nov, 2022 7 commits
  4. 21 Nov, 2022 12 commits
  5. 20 Nov, 2022 4 commits
    • Alexei Starovoitov's avatar
      Merge branch 'bpf: Implement two type cast kfuncs' · 99429b22
      Alexei Starovoitov authored
      Yonghong Song says:
      
      ====================
      
      Currenty, a non-tracing bpf program typically has a single 'context' argument
      with predefined uapi struct type. Following these uapi struct, user is able
      to access other fields defined in uapi header. Inside the kernel, the
      user-seen 'context' argument is replaced with 'kernel context' (or 'kctx'
      in short) which can access more information than what uapi header provides.
      To access other info not in uapi header, people typically do two things:
        (1). extend uapi to access more fields rooted from 'context'.
        (2). use bpf_probe_read_kernl() helper to read particular field based on
          kctx.
      Using (1) needs uapi change and using (2) makes code more complex since
      direct memory access is not allowed.
      
      There are already a few instances trying to access more information from
      kctx:
        . trying to access some fields from perf_event kctx ([1]).
        . trying to access some fields from xdp kctx ([2]).
      
      This patch set tried to allow direct memory access for kctx fields
      by introducing bpf_cast_to_kern_ctx() kfunc.
      
      Martin mentioned a use case like type casting below:
        #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
      basically a 'unsigned char *" casted to 'struct skb_shared_info *'. This patch
      set tries to support such a use case as well with bpf_rdonly_cast().
      
      For the patch series, Patch 1 added support for a kfunc available to all
      prog types. Patch 2 added bpf_cast_to_kern_ctx() kfunc. Patch 3 added
      bpf_rdonly_cast() kfunc. Patch 4 added a few positive and negative tests.
      
        [1] https://lore.kernel.org/bpf/ad15b398-9069-4a0e-48cb-4bb651ec3088@meta.com/
        [2] https://lore.kernel.org/bpf/20221109215242.1279993-1-john.fastabend@gmail.com/
      
      Changelog:
        v3 -> v4:
          - remove unnecessary bpf_ctx_convert.t error checking
          - add and use meta.ret_btf_id instead of meta.arg_constant.value for
            bpf_cast_to_kern_ctx().
          - add PTR_TRUSTED to the return PTR_TO_BTF_ID type for bpf_cast_to_kern_ctx().
        v2 -> v3:
          - rebase on top of bpf-next (for merging conflicts)
          - add the selftest to s390x deny list
        rfcv1 -> v2:
          - break original one kfunc into two.
          - add missing error checks and error logs.
          - adapt to the new conventions in
            https://lore.kernel.org/all/20221118015614.2013203-1-memxor@gmail.com/
            for example, with __ign and __k suffix.
          - added support in fixup_kfunc_call() to replace kfunc calls with a single mov.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      99429b22
    • Yonghong Song's avatar
      bpf: Add type cast unit tests · 58d84bee
      Yonghong Song authored
      Three tests are added. One is from John Fastabend ({1]) which tests
      tracing style access for xdp program from the kernel ctx.
      Another is a tc test to test both kernel ctx tracing style access
      and explicit non-ctx type cast. The third one is for negative tests
      including two tests, a tp_bpf test where the bpf_rdonly_cast()
      returns a untrusted ptr which cannot be used as helper argument,
      and a tracepoint test where the kernel ctx is a u64.
      
      Also added the test to DENYLIST.s390x since s390 does not currently
      support calling kernel functions in JIT mode.
      
        [1] https://lore.kernel.org/bpf/20221109215242.1279993-1-john.fastabend@gmail.com/Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221120195442.3114844-1-yhs@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      58d84bee
    • Yonghong Song's avatar
      bpf: Add a kfunc for generic type cast · a35b9af4
      Yonghong Song authored
      Implement bpf_rdonly_cast() which tries to cast the object
      to a specified type. This tries to support use case like below:
        #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
      where skb_end_pointer(SKB) is a 'unsigned char *' and needs to
      be casted to 'struct skb_shared_info *'.
      
      The signature of bpf_rdonly_cast() looks like
         void *bpf_rdonly_cast(void *obj, __u32 btf_id)
      The function returns the same 'obj' but with PTR_TO_BTF_ID with
      btf_id. The verifier will ensure btf_id being a struct type.
      
      Since the supported type cast may not reflect what the 'obj'
      represents, the returned btf_id is marked as PTR_UNTRUSTED, so
      the return value and subsequent pointer chasing cannot be
      used as helper/kfunc arguments.
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221120195437.3114585-1-yhs@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a35b9af4
    • Yonghong Song's avatar
      bpf: Add a kfunc to type cast from bpf uapi ctx to kernel ctx · fd264ca0
      Yonghong Song authored
      Implement bpf_cast_to_kern_ctx() kfunc which does a type cast
      of a uapi ctx object to the corresponding kernel ctx. Previously
      if users want to access some data available in kctx but not
      in uapi ctx, bpf_probe_read_kernel() helper is needed.
      The introduction of bpf_cast_to_kern_ctx() allows direct
      memory access which makes code simpler and easier to understand.
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221120195432.3113982-1-yhs@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      fd264ca0