From: Jiri Olsa <jolsa@redhat.com>
To: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jiri Olsa <jolsa@kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andriin@fb.com>,
netdev@vger.kernel.org, bpf@vger.kernel.org,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [PATCHv2] bpf: Take module reference for trampoline in module
Date: Thu, 25 Mar 2021 22:39:30 +0100 [thread overview]
Message-ID: <YF0DEq7KBuk79bBq@krava> (raw)
In-Reply-To: <7cc5c310-c764-99bb-ad59-5ac04402bd5d@iogearbox.net>
On Thu, Mar 25, 2021 at 10:26:32PM +0100, Daniel Borkmann wrote:
> On 3/24/21 6:40 PM, Jiri Olsa wrote:
> > Currently module can be unloaded even if there's a trampoline
> > register in it. It's easily reproduced by running in parallel:
> >
> > # while :; do ./test_progs -t module_attach; done
> > # while :; do rmmod bpf_testmod; sleep 0.5; done
> >
> > Taking the module reference in case the trampoline's ip is
> > within the module code. Releasing it when the trampoline's
> > ip is unregistered.
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> > v2 changes:
> > - fixed ip_module_put to do preempt_disable/preempt_enable
> >
> > kernel/bpf/trampoline.c | 31 +++++++++++++++++++++++++++++++
> > 1 file changed, 31 insertions(+)
> >
> > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> > index 1f3a4be4b175..39e4280f94e4 100644
> > --- a/kernel/bpf/trampoline.c
> > +++ b/kernel/bpf/trampoline.c
> > @@ -87,6 +87,26 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
> > return tr;
> > }
> > +static struct module *ip_module_get(unsigned long ip)
> > +{
> > + struct module *mod;
> > + int err = 0;
> > +
> > + preempt_disable();
> > + mod = __module_text_address(ip);
> > + if (mod && !try_module_get(mod))
> > + err = -ENOENT;
> > + preempt_enable();
> > + return err ? ERR_PTR(err) : mod;
> > +}
> > +
> > +static void ip_module_put(unsigned long ip)
> > +{
> > + preempt_disable();
> > + module_put(__module_text_address(ip));
> > + preempt_enable();
>
> Could we cache the mod pointer in tr instead of doing another addr search
> for dropping the ref?
right.. I moved it from the ftrace layer where this was not an option,
so I did not realize bpf_trampoline could get extended, will send new
version
thanks,
jirka
>
> > +}
> > +
> > static int is_ftrace_location(void *ip)
> > {
> > long addr;
> > @@ -108,6 +128,9 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
> > ret = unregister_ftrace_direct((long)ip, (long)old_addr);
> > else
> > ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
> > +
> > + if (!ret)
> > + ip_module_put((unsigned long) ip);
> > return ret;
> > }
> > @@ -126,6 +149,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad
> > /* first time registering */
> > static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
> > {
> > + struct module *mod;
> > void *ip = tr->func.addr;
> > int ret;
> > @@ -134,10 +158,17 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
> > return ret;
> > tr->func.ftrace_managed = ret;
> > + mod = ip_module_get((unsigned long) ip);
> > + if (IS_ERR(mod))
> > + return -ENOENT;
> > +
> > if (tr->func.ftrace_managed)
> > ret = register_ftrace_direct((long)ip, (long)new_addr);
> > else
> > ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
> > +
> > + if (ret)
> > + module_put(mod);
> > return ret;
> > }
> >
>
prev parent reply other threads:[~2021-03-25 21:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-24 17:40 [PATCHv2] bpf: Take module reference for trampoline in module Jiri Olsa
2021-03-25 21:26 ` Daniel Borkmann
2021-03-25 21:39 ` Jiri Olsa [this message]
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=YF0DEq7KBuk79bBq@krava \
--to=jolsa@redhat.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=netdev@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=songliubraving@fb.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 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).