History

bpf: Introduce bpf_per_cpu_ptr()

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.

Hao Luo committed 6 years ago

Commit eaa6bcb1 parent 2c2f6ab8 files · +132 −13Browse files

Showing the changes to tools/include/uapi (1 of 8 files). Show all

Unified · Split

tools/include/uapi/linux/bpf.h+18−0
@@ -3686,6 +3686,23 @@ union bpf_attr { · 3685 unmodified lines
3686 * Return3686 * Return
3687 * The helper returns **TC_ACT_REDIRECT** on success or3687 * The helper returns **TC_ACT_REDIRECT** on success or
3688 * **TC_ACT_SHOT** on error.3688 * **TC_ACT_SHOT** on error.
3689 *
3690 * void *bpf_per_cpu_ptr(const void *percpu_ptr, u32 cpu)
3691 * Description
3692 * Take a pointer to a percpu ksym, *percpu_ptr*, and return a
3693 * pointer to the percpu kernel variable on *cpu*. A ksym is an
3694 * extern variable decorated with '__ksym'. For ksym, there is a
3695 * global var (either static or global) defined of the same name
3696 * in the kernel. The ksym is percpu if the global var is percpu.
3697 * The returned pointer points to the global percpu var on *cpu*.
3698 *
3699 * bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the
3700 * kernel, except that bpf_per_cpu_ptr() may return NULL. This
3701 * happens if *cpu* is larger than nr_cpu_ids. The caller of
3702 * bpf_per_cpu_ptr() must check the returned value.
3703 * Return
3704 * A pointer pointing to the kernel percpu variable on *cpu*, or
3705 * NULL, if *cpu* is invalid.
3689 */3706 */
3690#define __BPF_FUNC_MAPPER(FN) \3707#define __BPF_FUNC_MAPPER(FN) \
3691 FN(unspec), \3708 FN(unspec), \
@@ -3841,6 +3858,7 @@ union bpf_attr { · 149 unmodified lines
3841 FN(seq_printf_btf), \3858 FN(seq_printf_btf), \
3842 FN(skb_cgroup_classid), \3859 FN(skb_cgroup_classid), \
3843 FN(redirect_neigh), \3860 FN(redirect_neigh), \
3861 FN(bpf_per_cpu_ptr), \
3844 /* */3862 /* */
3845 3863
3846/* integer value in 'imm' field of BPF_CALL instruction selects which helper3864/* integer value in 'imm' field of BPF_CALL instruction selects which helper