Skip to contentSkip to conversation

Support kernel module function calls from eBPF

RFC PATCH Superseded Kumar Kartikeya Dwivedi 4 participants 20 messages Aug 30, 2021 – Sep 1, 2021

Subsystems (13)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Support kernel module function calls from eBPF

This set enables kernel module function calls, and also modifies verifier logic to permit invalid kernel function calls as long as they are pruned as part of dead code elimination. This is done to provide better runtime portability for BPF objects, which can conditionally disable parts of code that are pruned later by the verifier (e.g. const volatile vars, kconfig options). libbpf modifications are made along with kernel changes to support module function calls.

It also converts TCP congestion control objects to use the module kfunc support instead of relying on IS_BUILTIN ifdef.

Kumar Kartikeya Dwivedi (8):
bpf: Introduce BPF support for kernel module function calls
bpf: Be conservative during verification for invalid kfunc calls
libbpf: Support kernel module function calls
libbpf: Resolve invalid kfunc calls with imm = 0, off = 0
tools: Allow specifying base BTF file in resolve_btfids
bpf: btf: Introduce helpers for dynamic BTF set registration
bpf: enable TCP congestion control kfunc from modules bpf, selftests: Add basic test for module kfunc call

 include/linux/bpf.h                           |   1 +
 include/linux/bpfptr.h                        |   1 +
 include/linux/btf.h                           |  18 +++
 include/linux/filter.h                        |   9 ++
 include/uapi/linux/bpf.h                      |   3 +-
 kernel/bpf/btf.c                              |  37 +++++++
 kernel/bpf/core.c                             |  14 +++
 kernel/bpf/syscall.c                          |  55 +++++++++-
 kernel/bpf/verifier.c                         | 103 ++++++++++++++----
 kernel/trace/bpf_trace.c                      |   1 +
 net/ipv4/bpf_tcp_ca.c                         |  34 +-----
 net/ipv4/tcp_bbr.c                            |  28 ++++-
 net/ipv4/tcp_cubic.c                          |  26 ++++-
 net/ipv4/tcp_dctcp.c                          |  26 ++++-
 scripts/Makefile.modfinal                     |   1 +
 tools/bpf/resolve_btfids/main.c               |  19 +++-
 tools/include/uapi/linux/bpf.h                |   3 +-
 tools/lib/bpf/bpf.c                           |   3 +
 tools/lib/bpf/libbpf.c                        |  91 ++++++++++++++--
 tools/lib/bpf/libbpf_internal.h               |   2 +
 tools/testing/selftests/bpf/Makefile          |   3 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  23 +++-
 .../selftests/bpf/prog_tests/ksyms_module.c   |  10 +-
 .../selftests/bpf/progs/test_ksyms_module.c   |   9 ++
 24 files changed, 446 insertions(+), 74 deletions(-)
Signature · 1 line
2.33.0
author maintainer
#1 lore raw

RFC PATCH 1/8 bpf: Introduce BPF support for kernel module function calls

This change adds support on the kernel side to allow for BPF programs to call kernel module functions. Userspace will prepare an array of module BTF fds that is passed in during BPF_PROG_LOAD. In the kernel, the module BTF array is placed in the auxilliary struct for bpf_prog.

The verifier then uses insn->off to index into this table. insn->off is used by subtracting one from it, as userspace has to set the index of array in insn->off incremented by 1. This lets us denote vmlinux btf by insn->off == 0, and the prog->aux->kfunc_btf_tab[insn->off - 1] for module BTFs.

---
 include/linux/bpf.h            |  1 +
 include/linux/filter.h         |  9 ++++
 include/uapi/linux/bpf.h       |  3 +-
 kernel/bpf/core.c              | 14 ++++++
 kernel/bpf/syscall.c           | 55 +++++++++++++++++++++-
 kernel/bpf/verifier.c          | 85 ++++++++++++++++++++++++++--------
 tools/include/uapi/linux/bpf.h |  3 +-
 7 files changed, 147 insertions(+), 23 deletions(-)
include/linux/bpf.h +1 -0
index f4c16f19f83e..39f59e5f3a26 100644
@@ -874,6 +874,7 @@ struct bpf_prog_aux {
 	void *jit_data; /* JIT specific data. arch dependent */
 	struct bpf_jit_poke_descriptor *poke_tab;
 	struct bpf_kfunc_desc_tab *kfunc_tab;
+	struct bpf_kfunc_btf_tab *kfunc_btf_tab;
 	u32 size_poke_tab;
 	struct bpf_ksym ksym;
 	const struct bpf_prog_ops *ops;
include/linux/filter.h +9 -0
index 7d248941ecea..46451891633d 100644
@@ -592,6 +592,15 @@ struct bpf_prog {
 	struct bpf_insn		insnsi[];
 };
 
+#define MAX_KFUNC_DESCS 256
+/* There can only be at most MAX_KFUNC_DESCS module BTFs for kernel module
+ * function calls.
+ */
+struct bpf_kfunc_btf_tab {
+	u32 nr_btfs;
+	struct btf_mod_pair btfs[];
+};
+
 struct sk_filter {
 	refcount_t	refcnt;
 	struct rcu_head	rcu;
include/uapi/linux/bpf.h +2 -1
index 791f31dd0abe..4cbb2082a553 100644
@@ -1334,8 +1334,9 @@ union bpf_attr {
 			/* or valid module BTF object fd or 0 to attach to vmlinux */
 			__u32		attach_btf_obj_fd;
 		};
-		__u32		:32;		/* pad */
+		__u32		kfunc_btf_fds_cnt; /* reuse hole for count of BTF fds below */
 		__aligned_u64	fd_array;	/* array of FDs */
+		__aligned_u64   kfunc_btf_fds;  /* array of BTF FDs for module kfunc support */
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/core.c +14 -0
index 9f4636d021b1..73ba6d862df3 100644
@@ -2249,12 +2249,26 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
 	kfree(aux->used_btfs);
 }
 
+static void bpf_free_kfunc_btf_tab(struct bpf_prog_aux *aux)
+{
+	struct bpf_kfunc_btf_tab *tab = aux->kfunc_btf_tab;
+
+	if (tab) {
+		while (tab->nr_btfs--) {
+			module_put(tab->btfs[tab->nr_btfs].module);
+			btf_put(tab->btfs[tab->nr_btfs].btf);
+		}
+		kfree(tab);
+	}
+}
+
 static void bpf_prog_free_deferred(struct work_struct *work)
 {
 	struct bpf_prog_aux *aux;
 	int i;
 
 	aux = container_of(work, struct bpf_prog_aux, work);
+	bpf_free_kfunc_btf_tab(aux);
 	bpf_free_used_maps(aux);
 	bpf_free_used_btfs(aux);
 	if (bpf_prog_is_dev_bound(aux))
kernel/bpf/syscall.c +53 -2

81 lines not on this page · raw message

kernel/bpf/verifier.c +66 -19

199 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +2 -1
index 791f31dd0abe..4cbb2082a553 100644
@@ -1334,8 +1334,9 @@ union bpf_attr {
 			/* or valid module BTF object fd or 0 to attach to vmlinux */
 			__u32		attach_btf_obj_fd;
 		};
-		__u32		:32;		/* pad */
+		__u32		kfunc_btf_fds_cnt; /* reuse hole for count of BTF fds below */
 		__aligned_u64	fd_array;	/* array of FDs */
+		__aligned_u64   kfunc_btf_fds;  /* array of BTF FDs for module kfunc support */
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
Signature · 1 line
2.33.0
author maintainer
#5 lore raw

RFC PATCH 2/8 bpf: Be conservative during verification for invalid kfunc calls

In reply to Kumar Kartikeya Dwivedi

This change modifies the BPF verifier to only return error for invalid kfunc calls specially marked by userspace (with insn->imm == 0) after the verifier has eliminated dead instructions. This can be handled in the fixup stage, and skip processing during add and check stages.

If such an invalid call is dropped, the fixup stage will not encounter insn->imm as 0, otherwise it bails out and returns an error.

This can be used by userspace to use branches to call old and new kfunc helpers across kernel versions by setting the rodata map value before loading the BPF program, enhancing runtime portability. The next patch introduces libbpf support for this.

---
 kernel/bpf/verifier.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
kernel/bpf/verifier.c +18 -0
index de0670a8b1df..9904b9a96b04 100644
@@ -1730,6 +1730,15 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
 		prog_aux->kfunc_tab = tab;
 	}
 
+	/* btf_idr allocates IDs from 1, so func_id == 0 is always invalid, but
+	 * instead of returning an error, be conservative and wait until the
+	 * code elimination pass before returning error, so that invalid calls
+	 * that get pruned out can be in BPF programs loaded from userspace.
+	 * It is also required that offset be 0.
+	 */
+	if (!func_id && !offset)
+		return 0;
+
 	desc_btf = find_kfunc_desc_btf(env, func_id, offset);
 	if (IS_ERR(desc_btf)) {
 		verbose(env, "failed to find BTF for kernel function\n");
@@ -6527,6 +6536,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn)
 	struct btf *desc_btf;
 	int err;
 
+	/* skip for now, but return error when we find this in fixup_kfunc_call */
+	if (!insn->imm)
+		return 0;
+
 	desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off);
 	if (IS_ERR(desc_btf))
 		return PTR_ERR(desc_btf);
@@ -12658,6 +12671,11 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env,
 {
 	const struct bpf_kfunc_desc *desc;
 
+	if (!insn->imm) {
+		verbose(env, "invalid kernel function call not eliminated in verifier pass\n");
+		return -EINVAL;
+	}
+
 	/* insn->imm has the btf func_id. Replace it with
 	 * an address (relative to __bpf_base_call).
 	 */
Signature · 1 line
2.33.0
author maintainer
#6 lore raw

RFC PATCH 3/8 libbpf: Support kernel module function calls

In reply to Kumar Kartikeya Dwivedi
---
 tools/lib/bpf/bpf.c             |  3 ++
 tools/lib/bpf/libbpf.c          | 71 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf_internal.h |  2 +
 3 files changed, 73 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +3 -0
index 2401fad090c5..df2d1ceba146 100644
@@ -265,6 +265,9 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr)
 	attr.line_info_cnt = load_attr->line_info_cnt;
 	attr.line_info = ptr_to_u64(load_attr->line_info);
 
+	attr.kfunc_btf_fds = ptr_to_u64(load_attr->kfunc_btf_fds);
+	attr.kfunc_btf_fds_cnt = load_attr->kfunc_btf_fds_cnt;
+
 	if (load_attr->name)
 		memcpy(attr.prog_name, load_attr->name,
 		       min(strlen(load_attr->name), (size_t)BPF_OBJ_NAME_LEN - 1));
tools/lib/bpf/libbpf.c +68 -3

107 lines not on this page · raw message

tools/lib/bpf/libbpf_internal.h +2 -0
index 533b0211f40a..701719d9caaf 100644
@@ -276,6 +276,8 @@ struct bpf_prog_load_params {
 	__u32 log_level;
 	char *log_buf;
 	size_t log_buf_sz;
+	int *kfunc_btf_fds;
+	__u32 kfunc_btf_fds_cnt;
 };
 
 int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr);
Signature · 1 line
2.33.0
author maintainer
#11 lore raw

RFC PATCH 4/8 libbpf: Resolve invalid kfunc calls with imm = 0, off = 0

In reply to Kumar Kartikeya Dwivedi

Preserve these calls as it allows verifier to succeed in loading the program if they are determined to be unreachable after dead code elimination during program load. If not, the verifier will fail at runtime.

---
 tools/lib/bpf/libbpf.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +16 -4
index c4677ef97caa..9df90098f111 100644
@@ -6736,9 +6736,14 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
 	kfunc_id = find_ksym_btf_id(obj, ext->name, BTF_KIND_FUNC,
 				    &kern_btf, &kern_btf_fd);
 	if (kfunc_id < 0) {
-		pr_warn("extern (func ksym) '%s': not found in kernel BTF\n",
+		pr_warn("extern (func ksym) '%s': not found in kernel BTF, encoding btf_id as 0\n",
 			ext->name);
-		return kfunc_id;
+		/* keep invalid kfuncs, so that verifier can load the program if
+		 * they get removed during DCE pass in the verifier.
+		 * The encoding must be insn->imm = 0, insn->off = 0.
+		 */
+		kfunc_id = kern_btf_fd = 0;
+		goto resolve;
 	}
 
 	if (kern_btf != obj->btf_vmlinux) {
@@ -6798,11 +6803,18 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
 		return -EINVAL;
 	}
 
+resolve:
 	ext->is_set = true;
 	ext->ksym.kernel_btf_obj_fd = kern_btf_fd;
 	ext->ksym.kernel_btf_id = kfunc_id;
-	pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
-		 ext->name, kfunc_id);
+	if (kfunc_id) {
+		pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
+			 ext->name, kfunc_id);
+	} else {
+		ext->ksym.offset = 0;
+		pr_debug("extern (func ksym) '%s': added special invalid kfunc with imm = 0\n",
+			 ext->name);
+	}
 
 	return 0;
 }
Signature · 1 line
2.33.0
author maintainer
#13 lore raw

RFC PATCH 5/8 tools: Allow specifying base BTF file in resolve_btfids

In reply to Kumar Kartikeya Dwivedi

This commits allows specifying the base BTF for resolving btf id lists/sets during link time in the resolve_btfids tool. The base BTF is set to NULL if no path is passed. This allows resolving BTF ids for module kernel objects.

---
 tools/bpf/resolve_btfids/main.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
tools/bpf/resolve_btfids/main.c +17 -2
index de6365b53c9c..206e1120082f 100644
@@ -89,6 +89,7 @@ struct btf_id {
 struct object {
 	const char *path;
 	const char *btf;
+	const char *base_btf_path;
 
 	struct {
 		int		 fd;
@@ -477,16 +478,27 @@ static int symbols_resolve(struct object *obj)
 	int nr_structs  = obj->nr_structs;
 	int nr_unions   = obj->nr_unions;
 	int nr_funcs    = obj->nr_funcs;
+	struct btf *base_btf = NULL;
 	int err, type_id;
 	struct btf *btf;
 	__u32 nr_types;
 
-	btf = btf__parse(obj->btf ?: obj->path, NULL);
+	if (obj->base_btf_path) {
+		base_btf = btf__parse(obj->base_btf_path, NULL);
+		err = libbpf_get_error(base_btf);
+		if (err) {
+			pr_err("FAILED: load base BTF from %s: %s\n",
+			       obj->base_btf_path, strerror(-err));
+			return -1;
+		}
+	}
+
+	btf = btf__parse_split(obj->btf ?: obj->path, base_btf);
 	err = libbpf_get_error(btf);
 	if (err) {
 		pr_err("FAILED: load BTF from %s: %s\n",
 			obj->btf ?: obj->path, strerror(-err));
-		return -1;
+		goto out;
 	}
 
 	err = -1;
@@ -545,6 +557,7 @@ static int symbols_resolve(struct object *obj)
 
 	err = 0;
 out:
+	btf__free(base_btf);
 	btf__free(btf);
 	return err;
 }
@@ -697,6 +710,8 @@ int main(int argc, const char **argv)
 			   "BTF data"),
 		OPT_BOOLEAN(0, "no-fail", &no_fail,
 			   "do not fail if " BTF_IDS_SECTION " section is not found"),
+		OPT_STRING('s', "base-btf", &obj.base_btf_path, "file",
+			   "path of file providing base BTF data"),
 		OPT_END()
 	};
 	int err = -1;
Signature · 1 line
2.33.0
author maintainer
#14 lore raw

RFC PATCH 6/8 bpf: btf: Introduce helpers for dynamic BTF set registration

In reply to Kumar Kartikeya Dwivedi

This adds macros that generate BTF set registration APIs and check_kfunc_call callback. These require a type, which namespaces each BTF set. This is in preparation to allow for nf_conntrack registering unstable helpers it wants to expose to XDP and SCHED_CLS programs in subsequent patches.

With in kernel sets, the way this is supposed to work is, in kernel callback looks up within the in-kernel kfunc whitelist, and then defers to the dynamic BTF set lookup if it doesn't find the BTF id. If there is no in-kernel BTF id set, this callback can be directly used.

Also fix includes for btf.h and bpfptr.h so that they can included in isolation. This is in preparation for their usage in tcp_bbr, tcp_cubic and tcp_dctcp modules in the next patch.

---
 include/linux/bpfptr.h |  1 +
 include/linux/btf.h    | 15 +++++++++++++++
 kernel/bpf/btf.c       | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)
include/linux/bpfptr.h +1 -0
index 546e27fc6d46..46e1757d06a3 100644
@@ -3,6 +3,7 @@
 #ifndef _LINUX_BPFPTR_H
 #define _LINUX_BPFPTR_H
 
+#include <linux/mm.h>
 #include <linux/sockptr.h>
 
 typedef sockptr_t bpfptr_t;
include/linux/btf.h +15 -0
index 214fde93214b..d024b0eb43f9 100644
@@ -5,6 +5,7 @@
 #define _LINUX_BTF_H 1
 
 #include <linux/types.h>
+#include <linux/bpfptr.h>
 #include <uapi/linux/btf.h>
 #include <uapi/linux/bpf.h>
 
@@ -238,4 +239,18 @@ static inline const char *btf_name_by_offset(const struct btf *btf,
 }
 #endif
 
+struct kfunc_btf_set {
+	struct list_head list;
+	struct btf_id_set *set;
+};
+
+/* Register set of BTF ids */
+#define DECLARE_KFUNC_BTF_SET_REG(type)                                        \
+	void register_##type##_kfunc_btf_set(struct kfunc_btf_set *s);         \
+	bool __bpf_check_##type##_kfunc_call(u32 kfunc_id);                    \
+	void unregister_##type##_kfunc_btf_set(struct kfunc_btf_set *s)
+
+#define DEFINE_KFUNC_BTF_SET(set, name)                                        \
+	struct kfunc_btf_set name = { LIST_HEAD_INIT(name.list), (set) }
+
 #endif
kernel/bpf/btf.c +34 -0
index dfe61df4f974..35873495761d 100644
@@ -6215,3 +6215,37 @@ const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
 };
 
 BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct)
+
+/* Typesafe helpers to register BTF ID sets for modules */
+#define DEFINE_KFUNC_BTF_SET_REG(type)                                         \
+	static DEFINE_MUTEX(type##_kfunc_btf_set_mutex);                       \
+	static LIST_HEAD(type##_kfunc_btf_set_list);                           \
+	void register_##type##_kfunc_btf_set(struct kfunc_btf_set *s)          \
+	{                                                                      \
+		mutex_lock(&type##_kfunc_btf_set_mutex);                       \
+		list_add(&s->list, &type##_kfunc_btf_set_list);                \
+		mutex_unlock(&type##_kfunc_btf_set_mutex);                     \
+	}                                                                      \
+	EXPORT_SYMBOL_GPL(register_##type##_kfunc_btf_set);                    \
+	bool __bpf_check_##type##_kfunc_call(u32 kfunc_id)                     \
+	{                                                                      \
+		struct kfunc_btf_set *s;                                       \
+		mutex_lock(&type##_kfunc_btf_set_mutex);                       \
+		list_for_each_entry(s, &type##_kfunc_btf_set_list, list) {     \
+			if (btf_id_set_contains(s->set, kfunc_id)) {           \
+				mutex_unlock(&type##_kfunc_btf_set_mutex);     \
+				return true;                                   \
+			}                                                      \
+		}                                                              \
+		mutex_unlock(&type##_kfunc_btf_set_mutex);                     \
+		return false;                                                  \
+	}                                                                      \
+	void unregister_##type##_kfunc_btf_set(struct kfunc_btf_set *s)        \
+	{                                                                      \
+		if (!s)                                                        \
+			return;                                                \
+		mutex_lock(&type##_kfunc_btf_set_mutex);                       \
+		list_del_init(&s->list);                                       \
+		mutex_unlock(&type##_kfunc_btf_set_mutex);                     \
+	}                                                                      \
+	EXPORT_SYMBOL_GPL(unregister_##type##_kfunc_btf_set)
Signature · 1 line
2.33.0
author maintainer
#15 lore raw

RFC PATCH 7/8 bpf: enable TCP congestion control kfunc from modules

In reply to Kumar Kartikeya Dwivedi

This commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the bpf_tcp_ca kfunc_btf_set list, while the ones not dependent on modules are looked up from the wrapper function.

This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify link-vmlinux.sh to resolve_btfids in TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.

See following commits for background on use of:

CONFIG_X86 ifdef:
569c484f9995 (bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)

CONFIG_DYNAMIC_FTRACE ifdef:
7aae231ac93b (bpf: tcp: Limit calling some tcp cc functions to CONFIG_DYNAMIC_FTRACE)

---
 include/linux/btf.h       |  2 ++
 kernel/bpf/btf.c          |  2 ++
 net/ipv4/bpf_tcp_ca.c     | 34 +++-------------------------------
 net/ipv4/tcp_bbr.c        | 28 +++++++++++++++++++++++++++-
 net/ipv4/tcp_cubic.c      | 26 +++++++++++++++++++++++++-
 net/ipv4/tcp_dctcp.c      | 26 +++++++++++++++++++++++++-
 scripts/Makefile.modfinal |  1 +
 7 files changed, 85 insertions(+), 34 deletions(-)
include/linux/btf.h +2 -0
index d024b0eb43f9..8c0f29ed2af9 100644
@@ -253,4 +253,6 @@ struct kfunc_btf_set {
 #define DEFINE_KFUNC_BTF_SET(set, name)                                        \
 	struct kfunc_btf_set name = { LIST_HEAD_INIT(name.list), (set) }
 
+DECLARE_KFUNC_BTF_SET_REG(bpf_tcp_ca);
+
 #endif
kernel/bpf/btf.c +2 -0
index 35873495761d..cc12470a55f9 100644
@@ -6249,3 +6249,5 @@ BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct)
 		mutex_unlock(&type##_kfunc_btf_set_mutex);                     \
 	}                                                                      \
 	EXPORT_SYMBOL_GPL(unregister_##type##_kfunc_btf_set)
+
+DEFINE_KFUNC_BTF_SET_REG(bpf_tcp_ca);
net/ipv4/bpf_tcp_ca.c +3 -31
index 0dcee9df1326..804f2f912fe9 100644
@@ -223,41 +223,13 @@ BTF_ID(func, tcp_reno_cong_avoid)
 BTF_ID(func, tcp_reno_undo_cwnd)
 BTF_ID(func, tcp_slow_start)
 BTF_ID(func, tcp_cong_avoid_ai)
-#ifdef CONFIG_X86
-#ifdef CONFIG_DYNAMIC_FTRACE
-#if IS_BUILTIN(CONFIG_TCP_CONG_CUBIC)
-BTF_ID(func, cubictcp_init)
-BTF_ID(func, cubictcp_recalc_ssthresh)
-BTF_ID(func, cubictcp_cong_avoid)
-BTF_ID(func, cubictcp_state)
-BTF_ID(func, cubictcp_cwnd_event)
-BTF_ID(func, cubictcp_acked)
-#endif
-#if IS_BUILTIN(CONFIG_TCP_CONG_DCTCP)
-BTF_ID(func, dctcp_init)
-BTF_ID(func, dctcp_update_alpha)
-BTF_ID(func, dctcp_cwnd_event)
-BTF_ID(func, dctcp_ssthresh)
-BTF_ID(func, dctcp_cwnd_undo)
-BTF_ID(func, dctcp_state)
-#endif
-#if IS_BUILTIN(CONFIG_TCP_CONG_BBR)
-BTF_ID(func, bbr_init)
-BTF_ID(func, bbr_main)
-BTF_ID(func, bbr_sndbuf_expand)
-BTF_ID(func, bbr_undo_cwnd)
-BTF_ID(func, bbr_cwnd_event)
-BTF_ID(func, bbr_ssthresh)
-BTF_ID(func, bbr_min_tso_segs)
-BTF_ID(func, bbr_set_state)
-#endif
-#endif  /* CONFIG_DYNAMIC_FTRACE */
-#endif	/* CONFIG_X86 */
 BTF_SET_END(bpf_tcp_ca_kfunc_ids)
 
 static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id)
 {
-	return btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id);
+	if (btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id))
+		return true;
+	return __bpf_check_bpf_tcp_ca_kfunc_call(kfunc_btf_id);
 }
 
 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
net/ipv4/tcp_bbr.c +27 -1
index 6274462b86b4..1fea15dd0e05 100644
@@ -56,6 +56,8 @@
  * otherwise TCP stack falls back to an internal pacing using one high
  * resolution timer per TCP socket and may use more resources.
  */
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
 #include <linux/module.h>
 #include <net/tcp.h>
 #include <linux/inet_diag.h>
@@ -1152,14 +1154,38 @@ static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
 	.set_state	= bbr_set_state,
 };
 
+BTF_SET_START(tcp_bbr_kfunc_ids)
+#ifdef CONFIG_X86
+#ifdef CONFIG_DYNAMIC_FTRACE
+BTF_ID(func, bbr_init)
+BTF_ID(func, bbr_main)
+BTF_ID(func, bbr_sndbuf_expand)
+BTF_ID(func, bbr_undo_cwnd)
+BTF_ID(func, bbr_cwnd_event)
+BTF_ID(func, bbr_ssthresh)
+BTF_ID(func, bbr_min_tso_segs)
+BTF_ID(func, bbr_set_state)
+#endif
+#endif
+BTF_SET_END(tcp_bbr_kfunc_ids)
+
+static DEFINE_KFUNC_BTF_SET(&tcp_bbr_kfunc_ids, tcp_bbr_kfunc_btf_set);
+
 static int __init bbr_register(void)
 {
+	int ret;
+
 	BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE);
-	return tcp_register_congestion_control(&tcp_bbr_cong_ops);
+	ret = tcp_register_congestion_control(&tcp_bbr_cong_ops);
+	if (ret)
+		return ret;
+	register_bpf_tcp_ca_kfunc_btf_set(&tcp_bbr_kfunc_btf_set);
+	return 0;
 }
 
 static void __exit bbr_unregister(void)
 {
+	unregister_bpf_tcp_ca_kfunc_btf_set(&tcp_bbr_kfunc_btf_set);
 	tcp_unregister_congestion_control(&tcp_bbr_cong_ops);
 }
 
net/ipv4/tcp_cubic.c +25 -1
index 4a30deaa9a37..5b36b9442797 100644
@@ -25,6 +25,8 @@
  */
 
 #include <linux/mm.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
 #include <linux/module.h>
 #include <linux/math64.h>
 #include <net/tcp.h>
@@ -482,8 +484,25 @@ static struct tcp_congestion_ops cubictcp __read_mostly = {
 	.name		= "cubic",
 };
 
+BTF_SET_START(tcp_cubic_kfunc_ids)
+#ifdef CONFIG_X86
+#ifdef CONFIG_DYNAMIC_FTRACE
+BTF_ID(func, cubictcp_init)
+BTF_ID(func, cubictcp_recalc_ssthresh)
+BTF_ID(func, cubictcp_cong_avoid)
+BTF_ID(func, cubictcp_state)
+BTF_ID(func, cubictcp_cwnd_event)
+BTF_ID(func, cubictcp_acked)
+#endif
+#endif
+BTF_SET_END(tcp_cubic_kfunc_ids)
+
+static DEFINE_KFUNC_BTF_SET(&tcp_cubic_kfunc_ids, tcp_cubic_kfunc_btf_set);
+
 static int __init cubictcp_register(void)
 {
+	int ret;
+
 	BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
 
 	/* Precompute a bunch of the scaling factors that are used per-packet
@@ -514,11 +533,16 @@ static int __init cubictcp_register(void)
 	/* divide by bic_scale and by constant Srtt (100ms) */
 	do_div(cube_factor, bic_scale * 10);
 
-	return tcp_register_congestion_control(&cubictcp);
+	ret = tcp_register_congestion_control(&cubictcp);
+	if (ret)
+		return ret;
+	register_bpf_tcp_ca_kfunc_btf_set(&tcp_cubic_kfunc_btf_set);
+	return 0;
 }
 
 static void __exit cubictcp_unregister(void)
 {
+	unregister_bpf_tcp_ca_kfunc_btf_set(&tcp_cubic_kfunc_btf_set);
 	tcp_unregister_congestion_control(&cubictcp);
 }
 
net/ipv4/tcp_dctcp.c +25 -1
index 79f705450c16..efc47b4c7a11 100644
@@ -36,6 +36,8 @@
  *	Glenn Judd <[email protected]>
  */
 
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <net/tcp.h>
@@ -236,14 +238,36 @@ static struct tcp_congestion_ops dctcp_reno __read_mostly = {
 	.name		= "dctcp-reno",
 };
 
+BTF_SET_START(tcp_dctcp_kfunc_ids)
+#ifdef CONFIG_X86
+#ifdef CONFIG_DYNAMIC_FTRACE
+BTF_ID(func, dctcp_init)
+BTF_ID(func, dctcp_update_alpha)
+BTF_ID(func, dctcp_cwnd_event)
+BTF_ID(func, dctcp_ssthresh)
+BTF_ID(func, dctcp_cwnd_undo)
+BTF_ID(func, dctcp_state)
+#endif
+#endif
+BTF_SET_END(tcp_dctcp_kfunc_ids)
+
+static DEFINE_KFUNC_BTF_SET(&tcp_dctcp_kfunc_ids, tcp_dctcp_kfunc_btf_set);
+
 static int __init dctcp_register(void)
 {
+	int ret;
+
 	BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
-	return tcp_register_congestion_control(&dctcp);
+	ret = tcp_register_congestion_control(&dctcp);
+	if (ret)
+		return ret;
+	register_bpf_tcp_ca_kfunc_btf_set(&tcp_dctcp_kfunc_btf_set);
+	return 0;
 }
 
 static void __exit dctcp_unregister(void)
 {
+	unregister_bpf_tcp_ca_kfunc_btf_set(&tcp_dctcp_kfunc_btf_set);
 	tcp_unregister_congestion_control(&dctcp);
 }
 
scripts/Makefile.modfinal +1 -0
index 5e9b8057fb24..0755d4b8b74a 100644
@@ -58,6 +58,7 @@ quiet_cmd_btf_ko = BTF [M] $@
       cmd_btf_ko = 							\
 	if [ -f vmlinux ]; then						\
 		LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \
+		$(RESOLVE_BTFIDS) --no-fail -s vmlinux $@; 		\
 	else								\
 		printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \
 	fi;
Signature · 1 line
2.33.0
author maintainer
#18 lore raw

RFC PATCH 8/8 bpf, selftests: Add basic test for module kfunc call

In reply to Kumar Kartikeya Dwivedi

This has to drop light skeleton generation and instead use libbpf skeleton support, as loader program does not support kfunc module calls, yet. This also tests support for invalid kfunc calls we added in prior changes, such that verifier handles invalid call as long as it is removed by code elimination pass (before fixup_kfunc_call).

---
 include/linux/btf.h                           |  1 +
 kernel/bpf/btf.c                              |  1 +
 kernel/trace/bpf_trace.c                      |  1 +
 tools/testing/selftests/bpf/Makefile          |  3 ++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 23 ++++++++++++++++++-
 .../selftests/bpf/prog_tests/ksyms_module.c   | 10 ++++----
 .../selftests/bpf/progs/test_ksyms_module.c   |  9 ++++++++
 7 files changed, 40 insertions(+), 8 deletions(-)
include/linux/btf.h +1 -0
index 8c0f29ed2af9..6e704981c475 100644
@@ -254,5 +254,6 @@ struct kfunc_btf_set {
 	struct kfunc_btf_set name = { LIST_HEAD_INIT(name.list), (set) }
 
 DECLARE_KFUNC_BTF_SET_REG(bpf_tcp_ca);
+DECLARE_KFUNC_BTF_SET_REG(raw_tp);
 
 #endif
kernel/bpf/btf.c +1 -0
index cc12470a55f9..85a0c2737ea1 100644
@@ -6251,3 +6251,4 @@ BTF_ID_LIST_GLOBAL_SINGLE(btf_task_struct_ids, struct, task_struct)
 	EXPORT_SYMBOL_GPL(unregister_##type##_kfunc_btf_set)
 
 DEFINE_KFUNC_BTF_SET_REG(bpf_tcp_ca);
+DEFINE_KFUNC_BTF_SET_REG(raw_tp);
kernel/trace/bpf_trace.c +1 -0
index 8e2eb950aa82..02fe14b5d005 100644
@@ -1599,6 +1599,7 @@ int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
 	.get_func_proto  = raw_tp_prog_func_proto,
 	.is_valid_access = raw_tp_prog_is_valid_access,
+	.check_kfunc_call = __bpf_check_raw_tp_kfunc_call,
 };
 
 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
tools/testing/selftests/bpf/Makefile +2 -1
index 866531c08e4f..1a4aa71e88f4 100644
@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes
 	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
 	$(Q)$(MAKE) $(submake_extras) -C bpf_testmod
 	$(Q)cp bpf_testmod/bpf_testmod.ko $@
+	$(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko
 
 $(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
 	$(call msg,CC,,$@)
@@ -315,7 +316,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
 		linked_vars.skel.h linked_maps.skel.h
 
 LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
-	test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c
+	  test_ringbuf.c atomics.c trace_printk.c
 SKEL_BLACKLIST += $$(LSKELS)
 
 test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +22 -1
index 141d8da687d2..8242f2bb50b4 100644
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #include <linux/error-injection.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/percpu-defs.h>
@@ -13,6 +15,12 @@
 
 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
 
+noinline void
+bpf_testmod_test_mod_kfunc(int i)
+{
+	pr_info("mod kfunc i=%d\n", i);
+}
+
 noinline ssize_t
 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
 		      struct bin_attribute *bin_attr,
@@ -55,13 +63,26 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
 	.write = bpf_testmod_test_write,
 };
 
+BTF_SET_START(bpf_testmod_kfunc_ids)
+BTF_ID(func, bpf_testmod_test_mod_kfunc)
+BTF_SET_END(bpf_testmod_kfunc_ids)
+
+static DEFINE_KFUNC_BTF_SET(&bpf_testmod_kfunc_ids, bpf_testmod_kfunc_btf_set);
+
 static int bpf_testmod_init(void)
 {
-	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+	int ret;
+
+	ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+	if (ret)
+		return ret;
+	register_raw_tp_kfunc_btf_set(&bpf_testmod_kfunc_btf_set);
+	return 0;
 }
 
 static void bpf_testmod_exit(void)
 {
+	unregister_raw_tp_kfunc_btf_set(&bpf_testmod_kfunc_btf_set);
 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
 }
 
tools/testing/selftests/bpf/prog_tests/ksyms_module.c +4 -6
index 2cd5cded543f..d3b0adc2a495 100644
@@ -4,21 +4,19 @@
 #include <test_progs.h>
 #include <bpf/libbpf.h>
 #include <bpf/btf.h>
-#include "test_ksyms_module.lskel.h"
-
-static int duration;
+#include "test_ksyms_module.skel.h"
 
 void test_ksyms_module(void)
 {
-	struct test_ksyms_module* skel;
+	struct test_ksyms_module *skel;
 	int err;
 
 	skel = test_ksyms_module__open_and_load();
-	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
+	if (!ASSERT_OK_PTR(skel, "test_ksyms_module__open_and_load"))
 		return;
 
 	err = test_ksyms_module__attach(skel);
-	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
+	if (!ASSERT_OK(err, "test_ksyms_module__attach"))
 		goto cleanup;
 
 	usleep(1);
tools/testing/selftests/bpf/progs/test_ksyms_module.c +9 -0
index d6a0b3086b90..81f8790cb99d 100644
@@ -6,8 +6,11 @@
 #include <bpf/bpf_helpers.h>
 
 extern const int bpf_testmod_ksym_percpu __ksym;
+extern void bpf_testmod_test_mod_kfunc(int i) __ksym;
+extern void bpf_testmod_invalid_mod_kfunc(void) __ksym;
 
 int out_mod_ksym_global = 0;
+const volatile int x = 0;
 bool triggered = false;
 
 SEC("raw_tp/sys_enter")
@@ -16,6 +19,12 @@ int handler(const void *ctx)
 	int *val;
 	__u32 cpu;
 
+	/* This should be preserved by clang, but DCE'd by verifier, and still
+	 * allow loading the raw_tp prog
+	 */
+	if (x)
+		bpf_testmod_invalid_mod_kfunc();
+	bpf_testmod_test_mod_kfunc(42);
 	val = (int *)bpf_this_cpu_ptr(&bpf_testmod_ksym_percpu);
 	out_mod_ksym_global = *val;
 	triggered = true;
Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH bpf-next RFC v1 1/8] bpf: Introduce BPF support for kernel module function calls

In reply to Kumar Kartikeya Dwivedi
Quoted 59 lines · Kumar Kartikeya Dwivedi

This change adds support on the kernel side to allow for BPF programs to call kernel module functions. Userspace will prepare an array of module BTF fds that is passed in during BPF_PROG_LOAD. In the kernel, the module BTF array is placed in the auxilliary struct for bpf_prog.

The verifier then uses insn->off to index into this table. insn->off is used by subtracting one from it, as userspace has to set the index of array in insn->off incremented by 1. This lets us denote vmlinux btf by insn->off == 0, and the prog->aux->kfunc_btf_tab[insn->off - 1] for module BTFs.

---
 include/linux/bpf.h            |  1 +
 include/linux/filter.h         |  9 ++++
 include/uapi/linux/bpf.h       |  3 +-
 kernel/bpf/core.c              | 14 ++++++
 kernel/bpf/syscall.c           | 55 +++++++++++++++++++++-
 kernel/bpf/verifier.c          | 85 ++++++++++++++++++++++++++--------
 tools/include/uapi/linux/bpf.h |  3 +-
 7 files changed, 147 insertions(+), 23 deletions(-)
include/linux/bpf.h +1 -0
index f4c16f19f83e..39f59e5f3a26 100644
@@ -874,6 +874,7 @@ struct bpf_prog_aux {
 	void *jit_data; /* JIT specific data. arch dependent */
 	struct bpf_jit_poke_descriptor *poke_tab;
 	struct bpf_kfunc_desc_tab *kfunc_tab;
+	struct bpf_kfunc_btf_tab *kfunc_btf_tab;
 	u32 size_poke_tab;
 	struct bpf_ksym ksym;
 	const struct bpf_prog_ops *ops;
include/linux/filter.h +9 -0
index 7d248941ecea..46451891633d 100644
@@ -592,6 +592,15 @@ struct bpf_prog {
 	struct bpf_insn		insnsi[];
 };
 
+#define MAX_KFUNC_DESCS 256
+/* There can only be at most MAX_KFUNC_DESCS module BTFs for kernel module
+ * function calls.
+ */
+struct bpf_kfunc_btf_tab {
+	u32 nr_btfs;
+	struct btf_mod_pair btfs[];
+};
+
 struct sk_filter {
 	refcount_t	refcnt;
 	struct rcu_head	rcu;
include/uapi/linux/bpf.h +0 -0
index 791f31dd0abe..4cbb2082a553 100644

include/uapi/linux/bpf.h union bpf_attr { · line 1337

Quoted 5 lines
include/uapi/linux/bpf.h +1 -1
@@ -1334,8 +1334,9 @@ union bpf_attr {
 			/* or valid module BTF object fd or 0 to attach to vmlinux */
 			__u32		attach_btf_obj_fd;
 		};
-		__u32		:32;		/* pad */
+		__u32		kfunc_btf_fds_cnt; /* reuse hole for count of BTF fds below */

No need for size.

include/uapi/linux/bpf.h union bpf_attr { · line 1339

Quoted 2 lines
include/uapi/linux/bpf.h +1 -0
 		__aligned_u64	fd_array;	/* array of FDs */
+		__aligned_u64   kfunc_btf_fds;  /* array of BTF FDs for module kfunc support */

Just reuse fd_array. No need for another array of FDs.

Quoted 5 lines
include/uapi/linux/bpf.h +5 -0
+		tab = prog->aux->kfunc_btf_tab;
+		for (i = 0; i < n; i++) {
+			struct btf_mod_pair *p;
+			struct btf *mod_btf;
+

include/uapi/linux/bpf.h union bpf_attr {

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+			mod_btf = btf_get_by_fd(fds[i]);
+			if (IS_ERR(mod_btf)) {
+				err = PTR_ERR(mod_btf);
+				goto free_prog;
+			}
+			if (!btf_is_module(mod_btf)) {
+				err = -EINVAL;
+				btf_put(mod_btf);
+				goto free_prog;
+			}

just do that dynamically like access to fd_array is handled in other places. no need to preload.

#3 lore raw

Re: [PATCH bpf-next RFC v1 1/8] bpf: Introduce BPF support for kernel module function calls

In reply to Kumar Kartikeya Dwivedi
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Kumar,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: arc-allyesconfig (attached as .config)
compiler: arceb-elf-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/e868250a992dc2f10616aa6e2882072bb42bb1c5
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531 git checkout e868250a992dc2f10616aa6e2882072bb42bb1c5
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=arc

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from include/linux/perf_event.h:25,
from include/linux/trace_events.h:10,
from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from kernel/bpf/syscall.c:9:
arch/arc/include/asm/perf_event.h:126:27: warning: 'arc_pmu_cache_map' defined but not used [-Wunused-const-variable=]

     126 | static const unsigned int arc_pmu_cache_map[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
         |                           ^~~~~~~~~~~~~~~~~
   arch/arc/include/asm/perf_event.h:91:27: warning: 'arc_pmu_ev_hw_map' defined but not used [-Wunused-const-variable=]
      91 | static const char * const arc_pmu_ev_hw_map[] = {
         |                           ^~~~~~~~~~~~~~~~~
   kernel/bpf/syscall.c: In function 'bpf_prog_load':
>> kernel/bpf/syscall.c:2402:1: warning: the frame size of 1164 bytes is larger than 1024 bytes [-Wframe-larger-than=]
    2402 | }
         | ^

vim +2402 kernel/bpf/syscall.c

09756af46893c1 Alexei Starovoitov      2014-09-26  2160  
af2ac3e13e4575 Alexei Starovoitov      2021-05-13  2161  static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
09756af46893c1 Alexei Starovoitov      2014-09-26  2162  {
09756af46893c1 Alexei Starovoitov      2014-09-26  2163  	enum bpf_prog_type type = attr->prog_type;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2164  	struct bpf_prog *prog, *dst_prog = NULL;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2165  	struct btf *attach_btf = NULL;
09756af46893c1 Alexei Starovoitov      2014-09-26  2166  	char license[128];
09756af46893c1 Alexei Starovoitov      2014-09-26  2167  	bool is_gpl;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2168  	int err;
09756af46893c1 Alexei Starovoitov      2014-09-26  2169  
09756af46893c1 Alexei Starovoitov      2014-09-26  2170  	if (CHECK_ATTR(BPF_PROG_LOAD))
09756af46893c1 Alexei Starovoitov      2014-09-26  2171  		return -EINVAL;
09756af46893c1 Alexei Starovoitov      2014-09-26  2172  
c240eff63a1cf1 Jiong Wang              2019-05-24  2173  	if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
c240eff63a1cf1 Jiong Wang              2019-05-24  2174  				 BPF_F_ANY_ALIGNMENT |
10d274e880eb20 Alexei Starovoitov      2019-08-22  2175  				 BPF_F_TEST_STATE_FREQ |
1e6c62a8821557 Alexei Starovoitov      2020-08-27  2176  				 BPF_F_SLEEPABLE |
c240eff63a1cf1 Jiong Wang              2019-05-24  2177  				 BPF_F_TEST_RND_HI32))
e07b98d9bffe41 David S. Miller         2017-05-10  2178  		return -EINVAL;
e07b98d9bffe41 David S. Miller         2017-05-10  2179  
e9ee9efc0d1765 David Miller            2018-11-30  2180  	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
e9ee9efc0d1765 David Miller            2018-11-30  2181  	    (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2182  	    !bpf_capable())
e9ee9efc0d1765 David Miller            2018-11-30  2183  		return -EPERM;
e9ee9efc0d1765 David Miller            2018-11-30  2184  
09756af46893c1 Alexei Starovoitov      2014-09-26  2185  	/* copy eBPF program license from user space */
af2ac3e13e4575 Alexei Starovoitov      2021-05-13  2186  	if (strncpy_from_bpfptr(license,
af2ac3e13e4575 Alexei Starovoitov      2021-05-13  2187  				make_bpfptr(attr->license, uattr.is_kernel),
09756af46893c1 Alexei Starovoitov      2014-09-26  2188  				sizeof(license) - 1) < 0)
09756af46893c1 Alexei Starovoitov      2014-09-26  2189  		return -EFAULT;
09756af46893c1 Alexei Starovoitov      2014-09-26  2190  	license[sizeof(license) - 1] = 0;
09756af46893c1 Alexei Starovoitov      2014-09-26  2191  
09756af46893c1 Alexei Starovoitov      2014-09-26  2192  	/* eBPF programs must be GPL compatible to use GPL-ed functions */
09756af46893c1 Alexei Starovoitov      2014-09-26  2193  	is_gpl = license_is_gpl_compatible(license);
09756af46893c1 Alexei Starovoitov      2014-09-26  2194  
c04c0d2b968ac4 Alexei Starovoitov      2019-04-01  2195  	if (attr->insn_cnt == 0 ||
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2196  	    attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
ef0915cacd04c9 Daniel Borkmann         2016-12-07  2197  		return -E2BIG;
80b7d81912d807 Chenbo Feng             2017-05-31  2198  	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
80b7d81912d807 Chenbo Feng             2017-05-31  2199  	    type != BPF_PROG_TYPE_CGROUP_SKB &&
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2200  	    !bpf_capable())
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2201  		return -EPERM;
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2202  
b338cb921e6739 Maciej Żenczykowski     2020-06-20  2203  	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2204  		return -EPERM;
2c78ee898d8f10 Alexei Starovoitov      2020-05-13  2205  	if (is_perfmon_prog_type(type) && !perfmon_capable())
1be7f75d1668d6 Alexei Starovoitov      2015-10-07  2206  		return -EPERM;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2207  	if (attr->kfunc_btf_fds_cnt > MAX_KFUNC_DESCS)
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2208  		return -E2BIG;
1be7f75d1668d6 Alexei Starovoitov      2015-10-07  2209  
290248a5b7d829 Andrii Nakryiko         2020-12-03  2210  	/* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
290248a5b7d829 Andrii Nakryiko         2020-12-03  2211  	 * or btf, we need to check which one it is
290248a5b7d829 Andrii Nakryiko         2020-12-03  2212  	 */
290248a5b7d829 Andrii Nakryiko         2020-12-03  2213  	if (attr->attach_prog_fd) {
290248a5b7d829 Andrii Nakryiko         2020-12-03  2214  		dst_prog = bpf_prog_get(attr->attach_prog_fd);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2215  		if (IS_ERR(dst_prog)) {
290248a5b7d829 Andrii Nakryiko         2020-12-03  2216  			dst_prog = NULL;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2217  			attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2218  			if (IS_ERR(attach_btf))
290248a5b7d829 Andrii Nakryiko         2020-12-03  2219  				return -EINVAL;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2220  			if (!btf_is_kernel(attach_btf)) {
8bdd8e275ede97 Andrii Nakryiko         2020-12-07  2221  				/* attaching through specifying bpf_prog's BTF
8bdd8e275ede97 Andrii Nakryiko         2020-12-07  2222  				 * objects directly might be supported eventually
8bdd8e275ede97 Andrii Nakryiko         2020-12-07  2223  				 */
290248a5b7d829 Andrii Nakryiko         2020-12-03  2224  				btf_put(attach_btf);
8bdd8e275ede97 Andrii Nakryiko         2020-12-07  2225  				return -ENOTSUPP;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2226  			}
290248a5b7d829 Andrii Nakryiko         2020-12-03  2227  		}
290248a5b7d829 Andrii Nakryiko         2020-12-03  2228  	} else if (attr->attach_btf_id) {
290248a5b7d829 Andrii Nakryiko         2020-12-03  2229  		/* fall back to vmlinux BTF, if BTF type ID is specified */
290248a5b7d829 Andrii Nakryiko         2020-12-03  2230  		attach_btf = bpf_get_btf_vmlinux();
290248a5b7d829 Andrii Nakryiko         2020-12-03  2231  		if (IS_ERR(attach_btf))
290248a5b7d829 Andrii Nakryiko         2020-12-03  2232  			return PTR_ERR(attach_btf);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2233  		if (!attach_btf)
290248a5b7d829 Andrii Nakryiko         2020-12-03  2234  			return -EINVAL;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2235  		btf_get(attach_btf);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2236  	}
290248a5b7d829 Andrii Nakryiko         2020-12-03  2237  
aac3fc320d9404 Andrey Ignatov          2018-03-30  2238  	bpf_prog_load_fixup_attach_type(attr);
ccfe29eb29c2ed Alexei Starovoitov      2019-10-15  2239  	if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
290248a5b7d829 Andrii Nakryiko         2020-12-03  2240  				       attach_btf, attr->attach_btf_id,
290248a5b7d829 Andrii Nakryiko         2020-12-03  2241  				       dst_prog)) {
290248a5b7d829 Andrii Nakryiko         2020-12-03  2242  		if (dst_prog)
290248a5b7d829 Andrii Nakryiko         2020-12-03  2243  			bpf_prog_put(dst_prog);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2244  		if (attach_btf)
290248a5b7d829 Andrii Nakryiko         2020-12-03  2245  			btf_put(attach_btf);
5e43f899b03a34 Andrey Ignatov          2018-03-30  2246  		return -EINVAL;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2247  	}
5e43f899b03a34 Andrey Ignatov          2018-03-30  2248  
09756af46893c1 Alexei Starovoitov      2014-09-26  2249  	/* plain bpf_prog allocation */
09756af46893c1 Alexei Starovoitov      2014-09-26  2250  	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2251  	if (!prog) {
290248a5b7d829 Andrii Nakryiko         2020-12-03  2252  		if (dst_prog)
290248a5b7d829 Andrii Nakryiko         2020-12-03  2253  			bpf_prog_put(dst_prog);
290248a5b7d829 Andrii Nakryiko         2020-12-03  2254  		if (attach_btf)
290248a5b7d829 Andrii Nakryiko         2020-12-03  2255  			btf_put(attach_btf);
09756af46893c1 Alexei Starovoitov      2014-09-26  2256  		return -ENOMEM;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2257  	}
09756af46893c1 Alexei Starovoitov      2014-09-26  2258  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2259  	if (attr->kfunc_btf_fds_cnt) {
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2260  		struct bpf_kfunc_btf_tab *tab;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2261  		int fds[MAX_KFUNC_DESCS], i;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2262  		bpfptr_t kfunc_btf_fds;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2263  		u32 kfunc_btf_size, n;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2264  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2265  		kfunc_btf_size = min_t(u32, MAX_KFUNC_DESCS, attr->kfunc_btf_fds_cnt);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2266  		kfunc_btf_fds = make_bpfptr(attr->kfunc_btf_fds, uattr.is_kernel);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2267  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2268  		err = -EFAULT;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2269  		if (copy_from_bpfptr(fds, kfunc_btf_fds, kfunc_btf_size * sizeof(int)))
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2270  			goto free_prog;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2271  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2272  		err = -ENOMEM;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2273  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2274  		n = kfunc_btf_size;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2275  		kfunc_btf_size *= sizeof(prog->aux->kfunc_btf_tab->btfs[0]);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2276  		kfunc_btf_size += sizeof(*prog->aux->kfunc_btf_tab);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2277  		prog->aux->kfunc_btf_tab = kzalloc(kfunc_btf_size, GFP_KERNEL);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2278  		if (!prog->aux->kfunc_btf_tab)
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2279  			goto free_prog;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2280  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2281  		tab = prog->aux->kfunc_btf_tab;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2282  		for (i = 0; i < n; i++) {
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2283  			struct btf_mod_pair *p;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2284  			struct btf *mod_btf;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2285  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2286  			mod_btf = btf_get_by_fd(fds[i]);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2287  			if (IS_ERR(mod_btf)) {
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2288  				err = PTR_ERR(mod_btf);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2289  				goto free_prog;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2290  			}
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2291  			if (!btf_is_module(mod_btf)) {
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2292  				err = -EINVAL;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2293  				btf_put(mod_btf);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2294  				goto free_prog;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2295  			}
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2296  
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2297  			p = &tab->btfs[tab->nr_btfs];
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2298  			p->module = btf_try_get_module(mod_btf);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2299  			if (!p->module) {
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2300  				btf_put(mod_btf);
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2301  				goto free_prog;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2302  			}
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2303  			p->btf = mod_btf;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2304  			tab->nr_btfs++;
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2305  		}
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2306  	}
e868250a992dc2 Kumar Kartikeya Dwivedi 2021-08-30  2307  
5e43f899b03a34 Andrey Ignatov          2018-03-30  2308  	prog->expected_attach_type = attr->expected_attach_type;
290248a5b7d829 Andrii Nakryiko         2020-12-03  2309  	prog->aux->attach_btf = attach_btf;
ccfe29eb29c2ed Alexei Starovoitov      2019-10-15  2310  	prog->aux->attach_btf_id = attr->attach_btf_id;
3aac1ead5eb6b7 Toke Høiland-Jørgensen  2020-09-29  2311  	prog->aux->dst_prog = dst_prog;
9a18eedb145d08 Jakub Kicinski          2017-12-27  2312  	prog->aux->offload_requested = !!attr->prog_ifindex;
1e6c62a8821557 Alexei Starovoitov      2020-08-27  2313  	prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
9a18eedb145d08 Jakub Kicinski          2017-12-27  2314  
afdb09c720b62b Chenbo Feng             2017-10-18  2315  	err = security_bpf_prog_alloc(prog->aux);
aaac3ba95e4c8b Alexei Starovoitov      2015-10-07  2316  	if (err)
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2317  		goto free_prog;
afdb09c720b62b Chenbo Feng             2017-10-18  2318  
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2319  	prog->aux->user = get_current_user();
09756af46893c1 Alexei Starovoitov      2014-09-26  2320  	prog->len = attr->insn_cnt;
09756af46893c1 Alexei Starovoitov      2014-09-26  2321  
09756af46893c1 Alexei Starovoitov      2014-09-26  2322  	err = -EFAULT;
af2ac3e13e4575 Alexei Starovoitov      2021-05-13  2323  	if (copy_from_bpfptr(prog->insns,
af2ac3e13e4575 Alexei Starovoitov      2021-05-13  2324  			     make_bpfptr(attr->insns, uattr.is_kernel),
aafe6ae9cee32d Daniel Borkmann         2016-12-18  2325  			     bpf_prog_insn_size(prog)) != 0)
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2326  		goto free_prog_sec;
09756af46893c1 Alexei Starovoitov      2014-09-26  2327  
09756af46893c1 Alexei Starovoitov      2014-09-26  2328  	prog->orig_prog = NULL;
a91263d520246b Daniel Borkmann         2015-09-30  2329  	prog->jited = 0;
09756af46893c1 Alexei Starovoitov      2014-09-26  2330  
85192dbf4de087 Andrii Nakryiko         2019-11-17  2331  	atomic64_set(&prog->aux->refcnt, 1);
a91263d520246b Daniel Borkmann         2015-09-30  2332  	prog->gpl_compatible = is_gpl ? 1 : 0;
09756af46893c1 Alexei Starovoitov      2014-09-26  2333  
9a18eedb145d08 Jakub Kicinski          2017-12-27  2334  	if (bpf_prog_is_dev_bound(prog->aux)) {
ab3f0063c48c26 Jakub Kicinski          2017-11-03  2335  		err = bpf_prog_offload_init(prog, attr);
ab3f0063c48c26 Jakub Kicinski          2017-11-03  2336  		if (err)
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2337  			goto free_prog_sec;
ab3f0063c48c26 Jakub Kicinski          2017-11-03  2338  	}
ab3f0063c48c26 Jakub Kicinski          2017-11-03  2339  
09756af46893c1 Alexei Starovoitov      2014-09-26  2340  	/* find program type: socket_filter vs tracing_filter */
09756af46893c1 Alexei Starovoitov      2014-09-26  2341  	err = find_prog_type(type, prog);
09756af46893c1 Alexei Starovoitov      2014-09-26  2342  	if (err < 0)
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2343  		goto free_prog_sec;
09756af46893c1 Alexei Starovoitov      2014-09-26  2344  
9285ec4c8b61d4 Jason A. Donenfeld      2019-06-21  2345  	prog->aux->load_time = ktime_get_boottime_ns();
8e7ae2518f5265 Martin KaFai Lau        2020-03-13  2346  	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
8e7ae2518f5265 Martin KaFai Lau        2020-03-13  2347  			       sizeof(attr->prog_name));
8e7ae2518f5265 Martin KaFai Lau        2020-03-13  2348  	if (err < 0)
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2349  		goto free_prog_sec;
cb4d2b3f03d8ee Martin KaFai Lau        2017-09-27  2350  
09756af46893c1 Alexei Starovoitov      2014-09-26  2351  	/* run eBPF verifier */
838e96904ff3fc Yonghong Song           2018-11-19  2352  	err = bpf_check(&prog, attr, uattr);
09756af46893c1 Alexei Starovoitov      2014-09-26  2353  	if (err < 0)
09756af46893c1 Alexei Starovoitov      2014-09-26  2354  		goto free_used_maps;
09756af46893c1 Alexei Starovoitov      2014-09-26  2355  
d1c55ab5e41fcd Daniel Borkmann         2016-05-13  2356  	prog = bpf_prog_select_runtime(prog, &err);
04fd61ab36ec06 Alexei Starovoitov      2015-05-19  2357  	if (err < 0)
04fd61ab36ec06 Alexei Starovoitov      2015-05-19  2358  		goto free_used_maps;
09756af46893c1 Alexei Starovoitov      2014-09-26  2359  
dc4bb0e2356149 Martin KaFai Lau        2017-06-05  2360  	err = bpf_prog_alloc_id(prog);
dc4bb0e2356149 Martin KaFai Lau        2017-06-05  2361  	if (err)
dc4bb0e2356149 Martin KaFai Lau        2017-06-05  2362  		goto free_used_maps;
dc4bb0e2356149 Martin KaFai Lau        2017-06-05  2363  
c751798aa224fa Daniel Borkmann         2019-08-23  2364  	/* Upon success of bpf_prog_alloc_id(), the BPF prog is
c751798aa224fa Daniel Borkmann         2019-08-23  2365  	 * effectively publicly exposed. However, retrieving via
c751798aa224fa Daniel Borkmann         2019-08-23  2366  	 * bpf_prog_get_fd_by_id() will take another reference,
c751798aa224fa Daniel Borkmann         2019-08-23  2367  	 * therefore it cannot be gone underneath us.
c751798aa224fa Daniel Borkmann         2019-08-23  2368  	 *
c751798aa224fa Daniel Borkmann         2019-08-23  2369  	 * Only for the time /after/ successful bpf_prog_new_fd()
c751798aa224fa Daniel Borkmann         2019-08-23  2370  	 * and before returning to userspace, we might just hold
c751798aa224fa Daniel Borkmann         2019-08-23  2371  	 * one reference and any parallel close on that fd could
c751798aa224fa Daniel Borkmann         2019-08-23  2372  	 * rip everything out. Hence, below notifications must
c751798aa224fa Daniel Borkmann         2019-08-23  2373  	 * happen before bpf_prog_new_fd().
c751798aa224fa Daniel Borkmann         2019-08-23  2374  	 *
c751798aa224fa Daniel Borkmann         2019-08-23  2375  	 * Also, any failure handling from this point onwards must
c751798aa224fa Daniel Borkmann         2019-08-23  2376  	 * be using bpf_prog_put() given the program is exposed.
b16d9aa4c2b90a Martin KaFai Lau        2017-06-05  2377  	 */
74451e66d516c5 Daniel Borkmann         2017-02-16  2378  	bpf_prog_kallsyms_add(prog);
6ee52e2a3fe4ea Song Liu                2019-01-17  2379  	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
bae141f54be83b Daniel Borkmann         2019-12-06  2380  	bpf_audit_prog(prog, BPF_AUDIT_LOAD);
c751798aa224fa Daniel Borkmann         2019-08-23  2381  
c751798aa224fa Daniel Borkmann         2019-08-23  2382  	err = bpf_prog_new_fd(prog);
c751798aa224fa Daniel Borkmann         2019-08-23  2383  	if (err < 0)
c751798aa224fa Daniel Borkmann         2019-08-23  2384  		bpf_prog_put(prog);
09756af46893c1 Alexei Starovoitov      2014-09-26  2385  	return err;
09756af46893c1 Alexei Starovoitov      2014-09-26  2386  
09756af46893c1 Alexei Starovoitov      2014-09-26  2387  free_used_maps:
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2388  	/* In case we have subprogs, we need to wait for a grace
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2389  	 * period before we can tear down JIT memory since symbols
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2390  	 * are already exposed under kallsyms.
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2391  	 */
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2392  	__bpf_prog_put_noref(prog, prog->aux->func_cnt);
cd7455f1013ef9 Daniel Borkmann         2019-10-22  2393  	return err;
afdb09c720b62b Chenbo Feng             2017-10-18  2394  free_prog_sec:
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2395  	free_uid(prog->aux->user);
afdb09c720b62b Chenbo Feng             2017-10-18  2396  	security_bpf_prog_free(prog->aux);
3ac1f01b43b6e2 Roman Gushchin          2020-12-01  2397  free_prog:
22dc4a0f5ed11b Andrii Nakryiko         2020-12-03  2398  	if (prog->aux->attach_btf)
22dc4a0f5ed11b Andrii Nakryiko         2020-12-03  2399  		btf_put(prog->aux->attach_btf);
09756af46893c1 Alexei Starovoitov      2014-09-26  2400  	bpf_prog_free(prog);
09756af46893c1 Alexei Starovoitov      2014-09-26  2401  	return err;
09756af46893c1 Alexei Starovoitov      2014-09-26 @2402  }
09756af46893c1 Alexei Starovoitov      2014-09-26  2403  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#19 lore raw

Re: [PATCH bpf-next RFC v1 8/8] bpf, selftests: Add basic test for module kfunc call

In reply to Kumar Kartikeya Dwivedi

tools/testing/selftests/bpf/prog_tests/ksyms_module.c line 7

Quoted 12 lines · Kumar Kartikeya Dwivedi
tools/testing/selftests/bpf/prog_tests/ksyms_module.c +1 -3
index 2cd5cded543f..d3b0adc2a495 100644
@@ -4,21 +4,19 @@
 #include <test_progs.h>
 #include <bpf/libbpf.h>
 #include <bpf/btf.h>
-#include "test_ksyms_module.lskel.h"
-
-static int duration;
+#include "test_ksyms_module.skel.h"

bpf_btf_find_by_name_kind supports searching in modules,
so adding support for kfunc in modules to lskel shouldn't be hard to add.

#4 lore raw

Re: [PATCH bpf-next RFC v1 1/8] bpf: Introduce BPF support for kernel module function calls

In reply to Kumar Kartikeya Dwivedi
reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/e868250a992dc2f10616aa6e2882072bb42bb1c5 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…

Hi Kumar,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: um-x86_64_defconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/e868250a992dc2f10616aa6e2882072bb42bb1c5
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531 git checkout e868250a992dc2f10616aa6e2882072bb42bb1c5
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

/usr/bin/ld: kernel/bpf/core.o: in function `bpf_prog_free_deferred':

>> core.c:(.text+0x7b0): undefined reference to `btf_put'

collect2: error: ld returned 1 exit status

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#16 lore raw

Re: [PATCH bpf-next RFC v1 7/8] bpf: enable TCP congestion control kfunc from modules

In reply to Kumar Kartikeya Dwivedi
reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/5bc18bc51d4d66d55910a9829c2a6b0a5a6b61ea git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…

Hi Kumar,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: um-x86_64_defconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/5bc18bc51d4d66d55910a9829c2a6b0a5a6b61ea
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Support-kernel-module-function-calls-from-eBPF/20210831-013531 git checkout 5bc18bc51d4d66d55910a9829c2a6b0a5a6b61ea
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

/usr/bin/ld: net/ipv4/tcp_cubic.o: in function `cubictcp_unregister':

>> tcp_cubic.c:(.exit.text+0xd): undefined reference to `unregister_bpf_tcp_ca_kfunc_btf_set'
   /usr/bin/ld: net/ipv4/tcp_cubic.o: in function `cubictcp_register':
>> tcp_cubic.c:(.init.text+0x8e): undefined reference to `register_bpf_tcp_ca_kfunc_btf_set'

/usr/bin/ld: kernel/bpf/core.o: in function `bpf_prog_free_deferred':
core.c:(.text+0x7b0): undefined reference to `btf_put'
collect2: error: ld returned 1 exit status

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#12 lore raw

Re: [PATCH bpf-next RFC v1 4/8] libbpf: Resolve invalid kfunc calls with imm = 0, off = 0

In reply to Kumar Kartikeya Dwivedi
Quoted 6 lines · Kumar Kartikeya Dwivedi

Preserve these calls as it allows verifier to succeed in loading the program if they are determined to be unreachable after dead code elimination during program load. If not, the verifier will fail at runtime.

This should be controlled by whether extern for func is weak or not, just like we do for variables (see bpf_object__resolve_ksym_var_btf_id()).

Quoted 50 lines
---
 tools/lib/bpf/libbpf.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +16 -5
index c4677ef97caa..9df90098f111 100644
@@ -6736,9 +6736,14 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
        kfunc_id = find_ksym_btf_id(obj, ext->name, BTF_KIND_FUNC,
                                    &kern_btf, &kern_btf_fd);
        if (kfunc_id < 0) {
-               pr_warn("extern (func ksym) '%s': not found in kernel BTF\n",
+               pr_warn("extern (func ksym) '%s': not found in kernel BTF, encoding btf_id as 0\n",
                        ext->name);
-               return kfunc_id;
+               /* keep invalid kfuncs, so that verifier can load the program if
+                * they get removed during DCE pass in the verifier.
+                * The encoding must be insn->imm = 0, insn->off = 0.
+                */
+               kfunc_id = kern_btf_fd = 0;
+               goto resolve;
        }
 
        if (kern_btf != obj->btf_vmlinux) {
@@ -6798,11 +6803,18 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
                return -EINVAL;
        }
 
+resolve:
        ext->is_set = true;
        ext->ksym.kernel_btf_obj_fd = kern_btf_fd;
        ext->ksym.kernel_btf_id = kfunc_id;
-       pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
-                ext->name, kfunc_id);
+       if (kfunc_id) {
+               pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
+                        ext->name, kfunc_id);
+       } else {
+               ext->ksym.offset = 0;
+               pr_debug("extern (func ksym) '%s': added special invalid kfunc with imm = 0\n",
+                        ext->name);
+       }
 
        return 0;
 }
--

2.33.0

#17 lore raw

Re: [PATCH bpf-next RFC v1 7/8] bpf: enable TCP congestion control kfunc from modules

In reply to Kumar Kartikeya Dwivedi
Quoted 30 lines · Kumar Kartikeya Dwivedi

This commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the bpf_tcp_ca kfunc_btf_set list, while the ones not dependent on modules are looked up from the wrapper function.

This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify link-vmlinux.sh to resolve_btfids in TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.

See following commits for background on use of:

CONFIG_X86 ifdef:
569c484f9995 (bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)

CONFIG_DYNAMIC_FTRACE ifdef:
7aae231ac93b (bpf: tcp: Limit calling some tcp cc functions to CONFIG_DYNAMIC_FTRACE)

---
 include/linux/btf.h       |  2 ++
 kernel/bpf/btf.c          |  2 ++
 net/ipv4/bpf_tcp_ca.c     | 34 +++-------------------------------
 net/ipv4/tcp_bbr.c        | 28 +++++++++++++++++++++++++++-
 net/ipv4/tcp_cubic.c      | 26 +++++++++++++++++++++++++-
 net/ipv4/tcp_dctcp.c      | 26 +++++++++++++++++++++++++-
 scripts/Makefile.modfinal |  1 +
 7 files changed, 85 insertions(+), 34 deletions(-)

[...]

scripts/Makefile.modfinal quiet_cmd_btf_ko = BTF [M] $@ · line 61

Quoted 9 lines
scripts/Makefile.modfinal +1 -0
index 5e9b8057fb24..0755d4b8b74a 100644
@@ -58,6 +58,7 @@ quiet_cmd_btf_ko = BTF [M] $@
       cmd_btf_ko =                                                     \
        if [ -f vmlinux ]; then                                         \
                LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \
+               $(RESOLVE_BTFIDS) --no-fail -s vmlinux $@;              \

why is this --no-fail?

Quoted 6 lines
scripts/Makefile.modfinal +0 -1
        else                                                            \
                printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \
        fi;
--

2.33.0

#7 lore raw

Re: [PATCH bpf-next RFC v1 3/8] libbpf: Support kernel module function calls

In reply to Kumar Kartikeya Dwivedi
Quoted 1 line · Kumar Kartikeya Dwivedi

-ENOCOMMITMESSAGE?

Quoted 7 lines
---
 tools/lib/bpf/bpf.c             |  3 ++
 tools/lib/bpf/libbpf.c          | 71 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf_internal.h |  2 +
 3 files changed, 73 insertions(+), 3 deletions(-)

[...]

Quoted 15 lines
tools/lib/bpf/libbpf.c +7 -0
@@ -515,6 +521,13 @@ struct bpf_object {
        void *priv;
        bpf_object_clear_priv_t clear_priv;
 
+       struct {
+               struct hashmap *map;
+               int *fds;
+               size_t cap_cnt;
+               __u32 n_fds;
+       } kfunc_btf_fds;
+
        char path[];
 };
 #define obj_elf_valid(o)       ((o)->efile.elf)

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 5343

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5327,6 +5340,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                        ext = &obj->externs[relo->sym_off];
                        insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
                        insn[0].imm = ext->ksym.kernel_btf_id;
+                       insn[0].off = ext->ksym.offset;

Just a few lines above we use insn[1].imm = ext->ksym.kernel_btf_obj_fd; for EXT_KSYM (for variables). Why are you inventing a new form if we already have a pretty consistent pattern?

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
                        break;
                case RELO_SUBPROG_ADDR:
                        if (insn[0].src_reg != BPF_PSEUDO_FUNC) {

[...]

author maintainer
#8 lore raw

Re: [PATCH bpf-next RFC v1 3/8] libbpf: Support kernel module function calls

Quoted 31 lines · Andrii Nakryiko
Quoted 1 line · Kumar Kartikeya Dwivedi

-ENOCOMMITMESSAGE?

Quoted 7 lines
---
 tools/lib/bpf/bpf.c             |  3 ++
 tools/lib/bpf/libbpf.c          | 71 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf_internal.h |  2 +
 3 files changed, 73 insertions(+), 3 deletions(-)

[...]

Quoted 15 lines
tools/lib/bpf/libbpf.c +7 -0
@@ -515,6 +521,13 @@ struct bpf_object {
        void *priv;
        bpf_object_clear_priv_t clear_priv;
 
+       struct {
+               struct hashmap *map;
+               int *fds;
+               size_t cap_cnt;
+               __u32 n_fds;
+       } kfunc_btf_fds;
+
        char path[];
 };
 #define obj_elf_valid(o)       ((o)->efile.elf)
@@ -5327,6 +5340,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
Quoted 9 lines

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 5343

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
                        ext = &obj->externs[relo->sym_off];
                        insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
                        insn[0].imm = ext->ksym.kernel_btf_id;
+                       insn[0].off = ext->ksym.offset;

Just a few lines above we use insn[1].imm = ext->ksym.kernel_btf_obj_fd; for EXT_KSYM (for variables). Why are you inventing a new form if we already have a pretty consistent pattern?

That makes sense. This is all new to me, so I went with what was described in e6ac2450d6de (bpf: Support bpf program calling kernel function), but I'll rework it to encode the btf fd like that in the next spin. It also makes the everything far simpler.

Quoted 5 lines
Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
                        break;
                case RELO_SUBPROG_ADDR:
                        if (insn[0].src_reg != BPF_PSEUDO_FUNC) {

[...]

--
Kartikeya

#9 lore raw

Re: [PATCH bpf-next RFC v1 3/8] libbpf: Support kernel module function calls

Quoted 11 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 5343

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5327,6 +5340,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                        ext = &obj->externs[relo->sym_off];
                        insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
                        insn[0].imm = ext->ksym.kernel_btf_id;
+                       insn[0].off = ext->ksym.offset;

Just a few lines above we use insn[1].imm = ext->ksym.kernel_btf_obj_fd; for EXT_KSYM (for variables). Why are you inventing a new form if we already have a pretty consistent pattern?

Quoted 4 lines

That makes sense. This is all new to me, so I went with what was described in e6ac2450d6de (bpf: Support bpf program calling kernel function), but I'll rework it to encode the btf fd like that in the next spin. It also makes the everything far simpler.

Hmm. kfunc call is a call insn. There is no imm[1].

#10 lore raw

Re: [PATCH bpf-next RFC v1 3/8] libbpf: Support kernel module function calls

Quoted 14 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines

tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 5343

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5327,6 +5340,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
                        ext = &obj->externs[relo->sym_off];
                        insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
                        insn[0].imm = ext->ksym.kernel_btf_id;
+                       insn[0].off = ext->ksym.offset;

Just a few lines above we use insn[1].imm = ext->ksym.kernel_btf_obj_fd; for EXT_KSYM (for variables). Why are you inventing a new form if we already have a pretty consistent pattern?

Quoted 6 lines
Quoted 4 lines

That makes sense. This is all new to me, so I went with what was described in e6ac2450d6de (bpf: Support bpf program calling kernel function), but I'll rework it to encode the btf fd like that in the next spin. It also makes the everything far simpler.

Hmm. kfunc call is a call insn. There is no imm[1].

Doh, right :( Never mind, we'll need to use fd_array for this.

Either way, I don't think hashmap use is warranted here to find a BTF slot. Let's just do linear search, it's not like we are going to have thousands of module BTFs used by any single BPF program, right?