bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Andrii Nakryiko <andrii@kernel.org>,
	bpf@vger.kernel.org, netdev@vger.kernel.org, ast@fb.com
Cc: kernel-team@fb.com, Hao Luo <haoluo@google.com>,
	Yonghong Song <yhs@fb.com>
Subject: Re: [PATCH v3 bpf-next 5/7] bpf: support BPF ksym variables in kernel modules
Date: Tue, 12 Jan 2021 17:27:40 +0100	[thread overview]
Message-ID: <4155ef59-9e5e-f596-f22b-ecd4bde73e85@iogearbox.net> (raw)
In-Reply-To: <20210112075520.4103414-6-andrii@kernel.org>

On 1/12/21 8:55 AM, Andrii Nakryiko wrote:
> Add support for directly accessing kernel module variables from BPF programs
> using special ldimm64 instructions. This functionality builds upon vmlinux
> ksym support, but extends ldimm64 with src_reg=BPF_PSEUDO_BTF_ID to allow
> specifying kernel module BTF's FD in insn[1].imm field.
> 
> During BPF program load time, verifier will resolve FD to BTF object and will
> take reference on BTF object itself and, for module BTFs, corresponding module
> as well, to make sure it won't be unloaded from under running BPF program. The
> mechanism used is similar to how bpf_prog keeps track of used bpf_maps.
> 
> One interesting change is also in how per-CPU variable is determined. The
> logic is to find .data..percpu data section in provided BTF, but both vmlinux
> and module each have their own .data..percpu entries in BTF. So for module's
> case, the search for DATASEC record needs to look at only module's added BTF
> types. This is implemented with custom search function.
> 
> Acked-by: Yonghong Song <yhs@fb.com>
> Acked-by: Hao Luo <haoluo@google.com>
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
[...]
> +
> +struct module *btf_try_get_module(const struct btf *btf)
> +{
> +	struct module *res = NULL;
> +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
> +	struct btf_module *btf_mod, *tmp;
> +
> +	mutex_lock(&btf_module_mutex);
> +	list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
> +		if (btf_mod->btf != btf)
> +			continue;
> +
> +		if (try_module_get(btf_mod->module))
> +			res = btf_mod->module;

One more thought (follow-up would be okay I'd think) ... when a module references
a symbol from another module, it similarly needs to bump the refcount of the module
that is owning it and thus disallowing to unload for that other module's lifetime.
That usage dependency is visible via /proc/modules however, so if unload doesn't work
then lsmod allows a way to introspect that to the user. This seems to be achieved via
resolve_symbol() where it records its dependency/usage. Would be great if we could at
some point also include the BPF prog name into that list so that this is more obvious.
Wdyt?

> +		break;
> +	}
> +	mutex_unlock(&btf_module_mutex);
> +#endif
> +
> +	return res;
> +}
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 261f8692d0d2..69c3c308de5e 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -2119,6 +2119,28 @@ static void bpf_free_used_maps(struct bpf_prog_aux *aux)
>   	kfree(aux->used_maps);
>   }
>   
> +void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
> +			  struct btf_mod_pair *used_btfs, u32 len)
> +{
> +#ifdef CONFIG_BPF_SYSCALL
> +	struct btf_mod_pair *btf_mod;
> +	u32 i;
> +
> +	for (i = 0; i < len; i++) {
> +		btf_mod = &used_btfs[i];
> +		if (btf_mod->module)
> +			module_put(btf_mod->module);
> +		btf_put(btf_mod->btf);
> +	}
> +#endif
> +}

  reply	other threads:[~2021-01-12 16:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-12  7:55 [PATCH v3 bpf-next 0/7] Support kernel module ksym variables Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 1/7] bpf: add bpf_patch_call_args prototype to include/linux/bpf.h Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 2/7] bpf: avoid warning when re-casting __bpf_call_base into __bpf_call_base_args Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 3/7] bpf: declare __bpf_free_used_maps() unconditionally Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 4/7] selftests/bpf: sync RCU before unloading bpf_testmod Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 5/7] bpf: support BPF ksym variables in kernel modules Andrii Nakryiko
2021-01-12 16:27   ` Daniel Borkmann [this message]
2021-01-12 20:38     ` Andrii Nakryiko
2021-01-12 23:18     ` Alexei Starovoitov
2021-01-13 22:55       ` Daniel Borkmann
2021-01-12  7:55 ` [PATCH v3 bpf-next 6/7] libbpf: support kernel module ksym externs Andrii Nakryiko
2021-01-12  7:55 ` [PATCH v3 bpf-next 7/7] selftests/bpf: test " Andrii Nakryiko
2021-01-13  1:29   ` Alexei Starovoitov

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=4155ef59-9e5e-f596-f22b-ecd4bde73e85@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii@kernel.org \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).