• Hao Luo's avatar
    bpf: Reject programs that try to load __percpu memory. · 5844101a
    Hao Luo authored
    With the introduction of the btf_type_tag "percpu", we can add a
    MEM_PERCPU to identify those pointers that point to percpu memory.
    The ability of differetiating percpu pointers from regular memory
    pointers have two benefits:
    
     1. It forbids unexpected use of percpu pointers, such as direct loads.
        In kernel, there are special functions used for accessing percpu
        memory. Directly loading percpu memory is meaningless. We already
        have BPF helpers like bpf_per_cpu_ptr() and bpf_this_cpu_ptr() that
        wrap the kernel percpu functions. So we can now convert percpu
        pointers into regular pointers in a safe way.
    
     2. Previously, bpf_per_cpu_ptr() and bpf_this_cpu_ptr() only work on
        PTR_TO_PERCPU_BTF_ID, a special reg_type which describes static
        percpu variables in kernel (we rely on pahole to encode them into
        vmlinux BTF). Now, since we can identify __percpu tagged pointers,
        we can also identify dynamically allocated percpu memory as well.
        It means we can use bpf_xxx_cpu_ptr() on dynamic percpu memory.
        This would be very convenient when accessing fields like
        "cgroup->rstat_cpu".
    Signed-off-by: default avatarHao Luo <haoluo@google.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Acked-by: default avatarYonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20220304191657.981240-4-haoluo@google.com
    5844101a
btf.c 188 KB