Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf: BTF support for ksyms #1

Closed
wants to merge 7 commits into from
Closed

Commits on Sep 4, 2020

  1. adding ci files

    tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    bf70555 View commit details
    Browse the repository at this point in the history
  2. Pseudo_btf_id is a type of ld_imm insn that associates a btf_id to a

    ksym so that further dereferences on the ksym can use the BTF info
    to validate accesses. Internally, when seeing a pseudo_btf_id ld insn,
    the verifier reads the btf_id stored in the insn[0]'s imm field and
    marks the dst_reg as PTR_TO_BTF_ID. The btf_id points to a VAR_KIND,
    which is encoded in btf_vminux by pahole. If the VAR is not of a struct
    type, the dst reg will be marked as PTR_TO_MEM instead of PTR_TO_BTF_ID
    and the mem_size is resolved to the size of the VAR's type.
    
    From the VAR btf_id, the verifier can also read the address of the
    ksym's corresponding kernel var from kallsyms and use that to fill
    dst_reg.
    
    Therefore, the proper functionality of pseudo_btf_id depends on (1)
    kallsyms and (2) the encoding of kernel global VARs in pahole, which
    should be available since pahole v1.18.
    
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     include/linux/bpf_verifier.h   |   4 ++
     include/linux/btf.h            |  15 +++++
     include/uapi/linux/bpf.h       |  38 ++++++++---
     kernel/bpf/btf.c               |  15 -----
     kernel/bpf/verifier.c          | 112 ++++++++++++++++++++++++++++++---
     tools/include/uapi/linux/bpf.h |  38 ++++++++---
     6 files changed, 182 insertions(+), 40 deletions(-)
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    094bdc3 View commit details
    Browse the repository at this point in the history
  3. If a ksym is defined with a type, libbpf will try to find the ksym's btf

    information from kernel btf. If a valid btf entry for the ksym is found,
    libbpf can pass in the found btf id to the verifier, which validates the
    ksym's type and value.
    
    Typeless ksyms (i.e. those defined as 'void') will not have such btf_id,
    but it has the symbol's address (read from kallsyms) and its value is
    treated as a raw pointer.
    
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     tools/lib/bpf/libbpf.c | 116 ++++++++++++++++++++++++++++++++++++-----
     1 file changed, 102 insertions(+), 14 deletions(-)
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    589391c View commit details
    Browse the repository at this point in the history
  4. Selftests for typed ksyms. Tests two types of ksyms: one is a struct,

    the other is a plain int. This tests two paths in the kernel. Struct
    ksyms will be converted into PTR_TO_BTF_ID by the verifier while int
    typed ksyms will be converted into PTR_TO_MEM.
    
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     .../testing/selftests/bpf/prog_tests/ksyms.c  | 31 +++------
     .../selftests/bpf/prog_tests/ksyms_btf.c      | 63 +++++++++++++++++++
     .../selftests/bpf/progs/test_ksyms_btf.c      | 23 +++++++
     tools/testing/selftests/bpf/trace_helpers.c   | 26 ++++++++
     tools/testing/selftests/bpf/trace_helpers.h   |  4 ++
     5 files changed, 123 insertions(+), 24 deletions(-)
     create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
     create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf.c
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    b9c2524 View commit details
    Browse the repository at this point in the history
  5. Add bpf_per_cpu_ptr() to help bpf programs access percpu vars.

    bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the kernel
    except that it may return NULL. This happens when the cpu parameter is
    out of range. So the caller must check the returned value.
    
    Acked-by: Andrii Nakryiko <andriin@fb.com>
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     include/linux/bpf.h            |  3 ++
     include/linux/btf.h            | 11 ++++++
     include/uapi/linux/bpf.h       | 17 +++++++++
     kernel/bpf/btf.c               | 10 ------
     kernel/bpf/verifier.c          | 66 +++++++++++++++++++++++++++++++---
     kernel/trace/bpf_trace.c       | 18 ++++++++++
     tools/include/uapi/linux/bpf.h | 17 +++++++++
     7 files changed, 128 insertions(+), 14 deletions(-)
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    4bf384f View commit details
    Browse the repository at this point in the history
  6. Add bpf_this_cpu_ptr() to help access percpu var on this cpu. This

    helper always returns a valid pointer, therefore no need to check
    returned value for NULL. Also note that all programs run with
    preemption disabled, which means that the returned pointer is stable
    during all the execution of the program.
    
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     include/linux/bpf.h            |  1 +
     include/uapi/linux/bpf.h       | 14 ++++++++++++++
     kernel/bpf/verifier.c          | 10 +++++++---
     kernel/trace/bpf_trace.c       | 14 ++++++++++++++
     tools/include/uapi/linux/bpf.h | 14 ++++++++++++++
     5 files changed, 50 insertions(+), 3 deletions(-)
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    d3da0a2 View commit details
    Browse the repository at this point in the history
  7. Test bpf_per_cpu_ptr() and bpf_this_cpu_ptr(). Test two paths in the

    kernel. If the base pointer points to a struct, the returned reg is
    of type PTR_TO_BTF_ID. Direct pointer dereference can be applied on
    the returned variable. If the base pointer isn't a struct, the
    returned reg is of type PTR_TO_MEM, which also supports direct pointer
    dereference.
    
    Acked-by: Andrii Nakryiko <andriin@fb.com>
    Signed-off-by: Hao Luo <haoluo@google.com>
    ---
     .../selftests/bpf/prog_tests/ksyms_btf.c      | 10 +++++++
     .../selftests/bpf/progs/test_ksyms_btf.c      | 26 +++++++++++++++++++
     2 files changed, 36 insertions(+)
    haoluo1022 authored and tsipa committed Sep 4, 2020
    Configuration menu
    Copy the full SHA
    bb372fb View commit details
    Browse the repository at this point in the history