Add bpf_rcu_read_lock() and bpf_rcu_read_unlock() helpers. Both helpers are available to all program types with CAP_BPF capability. Signed-off-by: Yonghong Song <yhs@xxxxxx> --- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 14 ++++++++++++++ kernel/bpf/core.c | 2 ++ kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 14 ++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 8d948bfcb984..a9bda4c91fc7 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2554,6 +2554,8 @@ extern const struct bpf_func_proto bpf_get_retval_proto; extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto; extern const struct bpf_func_proto bpf_cgrp_storage_get_proto; extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto; +extern const struct bpf_func_proto bpf_rcu_read_lock_proto; +extern const struct bpf_func_proto bpf_rcu_read_unlock_proto; const struct bpf_func_proto *tracing_prog_func_proto( enum bpf_func_id func_id, const struct bpf_prog *prog); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 94659f6b3395..e86389cd6133 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5481,6 +5481,18 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * void bpf_rcu_read_lock(void) + * Description + * Call kernel rcu_read_lock(). + * Return + * None. + * + * void bpf_rcu_read_unlock(void) + * Description + * Call kernel rcu_read_unlock(). + * Return + * None. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -5695,6 +5707,8 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(rcu_read_lock, 212, ##ctx) \ + FN(rcu_read_unlock, 213, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 9c16338bcbe8..26858b579dfc 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2627,6 +2627,8 @@ const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak; const struct bpf_func_proto bpf_spin_lock_proto __weak; const struct bpf_func_proto bpf_spin_unlock_proto __weak; const struct bpf_func_proto bpf_jiffies64_proto __weak; +const struct bpf_func_proto bpf_rcu_read_lock_proto __weak; +const struct bpf_func_proto bpf_rcu_read_unlock_proto __weak; const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 124fd199ce5c..f19416dc8ad1 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1273,6 +1273,28 @@ static const struct bpf_func_proto bpf_timer_start_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_0(bpf_rcu_read_lock) { + rcu_read_lock(); + return 0; +} + +const struct bpf_func_proto bpf_rcu_read_lock_proto = { + .func = bpf_rcu_read_lock, + .gpl_only = false, + .ret_type = RET_VOID, +}; + +BPF_CALL_0(bpf_rcu_read_unlock) { + rcu_read_unlock(); + return 0; +} + +const struct bpf_func_proto bpf_rcu_read_unlock_proto = { + .func = bpf_rcu_read_unlock, + .gpl_only = false, + .ret_type = RET_VOID, +}; + static void drop_prog_refcnt(struct bpf_hrtimer *t) { struct bpf_prog *prog = t->prog; @@ -1627,6 +1649,10 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_spin_lock_proto; case BPF_FUNC_spin_unlock: return &bpf_spin_unlock_proto; + case BPF_FUNC_rcu_read_lock: + return &bpf_rcu_read_lock_proto; + case BPF_FUNC_rcu_read_unlock: + return &bpf_rcu_read_unlock_proto; case BPF_FUNC_jiffies64: return &bpf_jiffies64_proto; case BPF_FUNC_per_cpu_ptr: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 94659f6b3395..e86389cd6133 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5481,6 +5481,18 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * void bpf_rcu_read_lock(void) + * Description + * Call kernel rcu_read_lock(). + * Return + * None. + * + * void bpf_rcu_read_unlock(void) + * Description + * Call kernel rcu_read_unlock(). + * Return + * None. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -5695,6 +5707,8 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(rcu_read_lock, 212, ##ctx) \ + FN(rcu_read_unlock, 213, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't -- 2.30.2