Commit 53bdd5e8 authored by Guan Jing's avatar Guan Jing Committed by Lu Jialin
Browse files

sched: introduce bpf_sched_enable()

maillist inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I8OIT1

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: default avatarRoman Gushchin <guro@fb.com>
Signed-off-by: default avatarChen Hui <judy.chenhui@huawei.com>
Signed-off-by: default avatarRen Zhijie <renzhijie2@huawei.com>
Signed-off-by: default avatarHui Tang <tanghui20@huawei.com>
Signed-off-by: default avatarGuan Jing <guanjing6@huawei.com>
parent 82c25c3e
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -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 */
+11 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include <linux/memcontrol.h>
#include <linux/trace_events.h>
#include <net/netfilter/nf_bpf_link.h>
#include <linux/bpf_sched.h>

#include <net/tcx.h>

@@ -3031,6 +3032,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.link);

#ifdef CONFIG_BPF_SCHED
	if (link->prog->type == BPF_PROG_TYPE_SCHED)
		bpf_sched_dec();
#endif

	WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
						tr_link->trampoline));

@@ -3248,6 +3254,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;

+2 −0
Original line number Diff line number Diff line
@@ -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.
 */