From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <kafai@fb.com>,
"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
"Jesper Dangaard Brouer" <brouer@redhat.com>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
netdev@vger.kernel.org
Subject: [PATCH bpf-next v2 07/10] libbpf: Support kernel module function calls
Date: Tue, 14 Sep 2021 18:07:46 +0530 [thread overview]
Message-ID: <20210914123750.460750-8-memxor@gmail.com> (raw)
In-Reply-To: <20210914123750.460750-1-memxor@gmail.com>
This patch adds libbpf support for kernel module function call support.
The fd_array parameter is used during BPF program load is used to pass
module BTFs referenced by the program. insn->off is set to index into
this array + 1, because insn->off as 0 is reserved for btf_vmlinux. The
kernel subtracts 1 from insn->off when indexing into env->fd_array.
We try to use existing insn->off for a module, since the kernel limits
the maximum distinct module BTFs for kfuncs to 256, and also because
index must never exceed the maximum allowed value that can fit in
insn->off (INT16_MAX). In the future, if kernel interprets signed offset
as unsigned for kfunc calls, this limit can be increased to UINT16_MAX.
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
tools/lib/bpf/bpf.c | 1 +
tools/lib/bpf/libbpf.c | 56 +++++++++++++++++++++++++++++++--
tools/lib/bpf/libbpf_internal.h | 1 +
3 files changed, 55 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 2401fad090c5..7d1741ceaa32 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -264,6 +264,7 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr)
attr.line_info_rec_size = load_attr->line_info_rec_size;
attr.line_info_cnt = load_attr->line_info_cnt;
attr.line_info = ptr_to_u64(load_attr->line_info);
+ attr.fd_array = ptr_to_u64(load_attr->fd_array);
if (load_attr->name)
memcpy(attr.prog_name, load_attr->name,
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6ecfdc1fa7ba..e78ae57e4379 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -419,6 +419,12 @@ struct extern_desc {
/* local btf_id of the ksym extern's type. */
__u32 type_id;
+ /* offset to be patched in for insn->off,
+ * this is 0 for btf_vmlinux, and index + 1
+ * for module BTF, where index is BTF index in
+ * obj->fd_array
+ */
+ __s16 offset;
} ksym;
};
};
@@ -515,6 +521,10 @@ struct bpf_object {
void *priv;
bpf_object_clear_priv_t clear_priv;
+ int *fd_array;
+ size_t fd_cap_cnt;
+ int nr_fds;
+
char path[];
};
#define obj_elf_valid(o) ((o)->efile.elf)
@@ -5333,6 +5343,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;
break;
case RELO_SUBPROG_ADDR:
if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
@@ -6127,6 +6138,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
}
load_attr.log_level = prog->log_level;
load_attr.prog_flags = prog->prog_flags;
+ load_attr.fd_array = prog->obj->fd_array;
if (prog->obj->gen_loader) {
bpf_gen__prog_load(prog->obj->gen_loader, &load_attr,
@@ -6729,9 +6741,44 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
}
if (kern_btf != obj->btf_vmlinux) {
- pr_warn("extern (func ksym) '%s': function in kernel module is not supported\n",
- ext->name);
- return -ENOTSUP;
+ int index = -1;
+
+ if (!obj->fd_array) {
+ obj->fd_array = calloc(8, sizeof(*obj->fd_array));
+ if (!obj->fd_array)
+ return -ENOMEM;
+ obj->fd_cap_cnt = 8;
+ }
+
+ for (int i = 0; i < obj->nr_fds; i++) {
+ if (obj->fd_array[i] == kern_btf_fd) {
+ index = i;
+ break;
+ }
+ }
+
+ if (index == -1) {
+ if (obj->nr_fds == obj->fd_cap_cnt) {
+ ret = libbpf_ensure_mem((void **)&obj->fd_array,
+ &obj->fd_cap_cnt, sizeof(int),
+ obj->fd_cap_cnt + 1);
+ if (ret)
+ return ret;
+ }
+
+ index = obj->nr_fds;
+ obj->fd_array[obj->nr_fds++] = kern_btf_fd;
+ }
+
+ if (index >= INT16_MAX) {
+ /* insn->off is s16 */
+ pr_warn("extern (func ksym) '%s': module btf fd index too big\n",
+ ext->name);
+ return -E2BIG;
+ }
+ ext->ksym.offset = index + 1;
+ } else {
+ ext->ksym.offset = 0;
}
kern_func = btf__type_by_id(kern_btf, kfunc_id);
@@ -6907,6 +6954,9 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
err = bpf_gen__finish(obj->gen_loader);
}
+ /* clean up fd_array */
+ zfree(&obj->fd_array);
+
/* clean up module BTFs */
for (i = 0; i < obj->btf_module_cnt; i++) {
close(obj->btf_modules[i].fd);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 4f6ff5c23695..4a948318aa89 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -289,6 +289,7 @@ struct bpf_prog_load_params {
__u32 log_level;
char *log_buf;
size_t log_buf_sz;
+ int *fd_array;
};
int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr);
--
2.33.0
next prev parent reply other threads:[~2021-09-14 12:38 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-14 12:37 [PATCH bpf-next v2 00/10] Support kernel module function calls from eBPF Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 01/10] bpf: Introduce BPF support for kernel module function calls Kumar Kartikeya Dwivedi
2021-09-14 19:31 ` kernel test robot
2021-09-14 19:31 ` kernel test robot
2021-09-14 22:01 ` kernel test robot
2021-09-14 22:01 ` kernel test robot
2021-09-14 12:37 ` [PATCH bpf-next v2 02/10] bpf: Be conservative while processing invalid kfunc calls Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 03/10] bpf: btf: Introduce helpers for dynamic BTF set registration Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 04/10] tools: Allow specifying base BTF file in resolve_btfids Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 05/10] bpf: Enable TCP congestion control kfunc from modules Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 06/10] bpf: Bump MAX_BPF_STACK size to 768 bytes Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` Kumar Kartikeya Dwivedi [this message]
2021-09-14 12:37 ` [PATCH bpf-next v2 08/10] libbpf: Resolve invalid weak kfunc calls with imm = 0, off = 0 Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 09/10] libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations Kumar Kartikeya Dwivedi
2021-09-14 12:37 ` [PATCH bpf-next v2 10/10] bpf, selftests: Add basic test for module kfunc call Kumar Kartikeya Dwivedi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210914123750.460750-8-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brouer@redhat.com \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=toke@redhat.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.