Commit 82c25c3e authored by Guan Jing's avatar Guan Jing Committed by Lu Jialin
Browse files

sched: basic infrastructure for scheduler bpf

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

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



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

This commit introduces basic definitions and infrastructure for
scheduler bpf programs. It defines the BPF_PROG_TYPE_SCHED program
type and the BPF_SCHED attachment type.

The implementation is inspired by lsm bpf programs and is based on
kretprobes. This will allow to add new hooks with a minimal changes to
the kernel code and without any changes to libbpf/bpftool.
It's very convenient as I anticipate a large number of private patches
being used for a long time before (or if at all) reaching upstream.

Sched programs are expected to return an int, which meaning will be
context defined.

This patch doesn't add any real scheduler hooks (only a stub), it will
be done by following patches in the series.

Scheduler bpf programs as now are very restricted in what they can do:
only the bpf_printk() helper is available. The scheduler context can
impose significant restrictions on what's safe and what's not. So
let's extend their abilities on case by case basis when a need arise.

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 34239429
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_BPF_SCHED_H
#define _LINUX_BPF_SCHED_H

#include <linux/bpf.h>

#ifdef CONFIG_BPF_SCHED

#define BPF_SCHED_HOOK(RET, DEFAULT, NAME, ...) \
	RET bpf_sched_##NAME(__VA_ARGS__);
#include <linux/sched_hook_defs.h>
#undef BPF_SCHED_HOOK

int bpf_sched_verify_prog(struct bpf_verifier_log *vlog,
			  const struct bpf_prog *prog);

#else /* !CONFIG_BPF_SCHED */

static inline int bpf_sched_verify_prog(struct bpf_verifier_log *vlog,
			  const struct bpf_prog *prog)
{
	return -EOPNOTSUPP;
}

#endif /* CONFIG_BPF_SCHED */
#endif /* _LINUX_BPF_SCHED_H */
+4 −0
Original line number Diff line number Diff line
@@ -83,6 +83,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SYSCALL, bpf_syscall,
BPF_PROG_TYPE(BPF_PROG_TYPE_NETFILTER, netfilter,
	      struct bpf_nf_ctx, struct bpf_nf_ctx)
#endif
#ifdef CONFIG_BPF_SCHED
BPF_PROG_TYPE(BPF_PROG_TYPE_SCHED, bpf_sched,
	      void *, void *)
#endif /* CONFIG_BPF_SCHED */

BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
+2 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
BPF_SCHED_HOOK(int, 0, dummy, void)
+2 −0
Original line number Diff line number Diff line
@@ -988,6 +988,7 @@ enum bpf_prog_type {
	BPF_PROG_TYPE_SK_LOOKUP,
	BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
	BPF_PROG_TYPE_NETFILTER,
	BPF_PROG_TYPE_SCHED,
};

enum bpf_attach_type {
@@ -1040,6 +1041,7 @@ enum bpf_attach_type {
	BPF_TCX_INGRESS,
	BPF_TCX_EGRESS,
	BPF_TRACE_UPROBE_MULTI,
	BPF_SCHED,
	__MAX_BPF_ATTACH_TYPE
};

+3 −0
Original line number Diff line number Diff line
@@ -5982,6 +5982,9 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
				return true;
			t = btf_type_by_id(btf, t->type);
			break;
#ifdef CONFIG_BPF_SCHED
		case BPF_SCHED:
#endif
		case BPF_MODIFY_RETURN:
			/* For now the BPF_MODIFY_RETURN can only be attached to
			 * functions that return an int.
Loading