提交 915c4dfc 编写于 作者: R Roman Gushchin 提交者: Zheng Zengkai

bpf: sched: introduce bpf_sched_enable()

maillist inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5F6X6
CVE: NA

Reference: https://lore.kernel.org/all/20210916162451.709260-1-guro@fb.com/

-------------------

Introduce a dedicated static key and the bpf_sched_enabled() wrapper
to guard all invocations of bpf programs in the scheduler code.

It will help to avoid any potential performance regression in a case
when no scheduler bpf programs are attached.
Signed-off-by: NRoman Gushchin <guro@fb.com>
Signed-off-by: NChen Hui <judy.chenhui@huawei.com>
Signed-off-by: NRen Zhijie <renzhijie2@huawei.com>
上级 628738d6
......@@ -6,6 +6,8 @@
#ifdef CONFIG_BPF_SCHED
#include <linux/jump_label.h>
#define BPF_SCHED_HOOK(RET, DEFAULT, NAME, ...) \
RET bpf_sched_##NAME(__VA_ARGS__);
#include <linux/sched_hook_defs.h>
......@@ -14,6 +16,23 @@
int bpf_sched_verify_prog(struct bpf_verifier_log *vlog,
const struct bpf_prog *prog);
DECLARE_STATIC_KEY_FALSE(bpf_sched_enabled_key);
static inline bool bpf_sched_enabled(void)
{
return static_branch_unlikely(&bpf_sched_enabled_key);
}
static inline void bpf_sched_inc(void)
{
static_branch_inc(&bpf_sched_enabled_key);
}
static inline void bpf_sched_dec(void)
{
static_branch_dec(&bpf_sched_enabled_key);
}
#else /* !CONFIG_BPF_SCHED */
static inline int bpf_sched_verify_prog(struct bpf_verifier_log *vlog,
......@@ -22,5 +41,10 @@ static inline int bpf_sched_verify_prog(struct bpf_verifier_log *vlog,
return -EOPNOTSUPP;
}
static inline bool bpf_sched_enabled(void)
{
return false;
}
#endif /* CONFIG_BPF_SCHED */
#endif /* _LINUX_BPF_SCHED_H */
......@@ -31,6 +31,7 @@
#include <linux/poll.h>
#include <linux/bpf-netns.h>
#include <linux/rcupdate_trace.h>
#include <linux/bpf_sched.h>
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
(map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
......@@ -2531,6 +2532,11 @@ static void bpf_tracing_link_release(struct bpf_link *link)
struct bpf_tracing_link *tr_link =
container_of(link, struct bpf_tracing_link, link);
#ifdef CONFIG_BPF_SCHED
if (link->prog->type == BPF_PROG_TYPE_SCHED)
bpf_sched_dec();
#endif
WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
tr_link->trampoline));
......@@ -2718,6 +2724,11 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
goto out_unlock;
}
#ifdef CONFIG_BPF_SCHED
if (prog->type == BPF_PROG_TYPE_SCHED)
bpf_sched_inc();
#endif
link->tgt_prog = tgt_prog;
link->trampoline = tr;
......
......@@ -6,6 +6,8 @@
#include <linux/btf_ids.h>
#include "sched.h"
DEFINE_STATIC_KEY_FALSE(bpf_sched_enabled_key);
/*
* For every hook declare a nop function where a BPF program can be attached.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册