From: Jiri Olsa <olsajiri@gmail.com>
To: Song Liu <song@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Josh Poimboeuf <jpoimboe@kernel.org>,
Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
Petr Mladek <pmladek@suse.com>,
Zhen Lei <thunder.leizhen@huawei.com>,
bpf@vger.kernel.org, live-patching@vger.kernel.org,
linux-modules@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>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Joe Lawrence <joe.lawrence@redhat.com>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Luis Chamberlain <mcgrof@kernel.org>
Subject: Re: [PATCHv2 bpf-next 3/3] bpf: Change modules resolving for kprobe multi link
Date: Fri, 13 Jan 2023 23:22:08 +0100 [thread overview]
Message-ID: <Y8HZkKmJyT6dgYKS@krava> (raw)
In-Reply-To: <CAPhsuW5uHZxuhpEZ-_r8piRwUykx4+f9eumw8L8hqdAhmi5CvQ@mail.gmail.com>
On Fri, Jan 13, 2023 at 12:41:50PM -0800, Song Liu wrote:
> On Fri, Jan 13, 2023 at 6:44 AM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > We currently use module_kallsyms_on_each_symbol that iterates all
> > modules/symbols and we try to lookup each such address in user
> > provided symbols/addresses to get list of used modules.
> >
> > This fix instead only iterates provided kprobe addresses and calls
> > __module_address on each to get list of used modules. This turned
> > out to be simpler and also bit faster.
> >
> > On my setup with workload being (executed 10 times):
> >
> > # test_progs -t kprobe_multi_bench_attach_module
> >
> > Current code:
> >
> > Performance counter stats for './test.sh' (5 runs):
> >
> > 76,081,161,596 cycles:k ( +- 0.47% )
> >
> > 18.3867 +- 0.0992 seconds time elapsed ( +- 0.54% )
> >
> > With the fix:
> >
> > Performance counter stats for './test.sh' (5 runs):
> >
> > 74,079,889,063 cycles:k ( +- 0.04% )
> >
> > 17.8514 +- 0.0218 seconds time elapsed ( +- 0.12% )
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> > kernel/trace/bpf_trace.c | 95 +++++++++++++++++++++-------------------
> > 1 file changed, 49 insertions(+), 46 deletions(-)
> >
> > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> > index 095f7f8d34a1..90c5d5026831 100644
> > --- a/kernel/trace/bpf_trace.c
> > +++ b/kernel/trace/bpf_trace.c
> > @@ -2682,69 +2682,79 @@ static void symbols_swap_r(void *a, void *b, int size, const void *priv)
> > }
> > }
> >
> > -struct module_addr_args {
> > - unsigned long *addrs;
> > - u32 addrs_cnt;
> > +struct modules_array {
> > struct module **mods;
> > int mods_cnt;
> > int mods_cap;
> > };
> >
> > -static int module_callback(void *data, const char *name,
> > - struct module *mod, unsigned long addr)
> > +static int add_module(struct modules_array *arr, struct module *mod)
> > {
> > - struct module_addr_args *args = data;
> > struct module **mods;
> >
> > - /* We iterate all modules symbols and for each we:
> > - * - search for it in provided addresses array
> > - * - if found we check if we already have the module pointer stored
> > - * (we iterate modules sequentially, so we can check just the last
> > - * module pointer)
> > - * - take module reference and store it
> > - */
> > - if (!bsearch(&addr, args->addrs, args->addrs_cnt, sizeof(addr),
> > - bpf_kprobe_multi_addrs_cmp))
> > - return 0;
> > -
> > - if (args->mods && args->mods[args->mods_cnt - 1] == mod)
> > - return 0;
> > -
> > - if (args->mods_cnt == args->mods_cap) {
> > - args->mods_cap = max(16, args->mods_cap * 3 / 2);
> > - mods = krealloc_array(args->mods, args->mods_cap, sizeof(*mods), GFP_KERNEL);
> > + if (arr->mods_cnt == arr->mods_cap) {
> > + arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
> > + mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
> > if (!mods)
> > return -ENOMEM;
> > - args->mods = mods;
> > + arr->mods = mods;
> > }
> >
> > - if (!try_module_get(mod))
> > - return -EINVAL;
> > -
> > - args->mods[args->mods_cnt] = mod;
> > - args->mods_cnt++;
> > + arr->mods[arr->mods_cnt] = mod;
> > + arr->mods_cnt++;
> > return 0;
> > }
> >
> > +static bool has_module(struct modules_array *arr, struct module *mod)
> > +{
> > + int i;
> > +
> > + if (!arr->mods)
> > + return false;
> > + for (i = arr->mods_cnt; i >= 0; i--) {
>
> This should be "i = arr->mods_cnt - 1", no?
right
>
> > + if (arr->mods[i] == mod)
> > + return true;
> > + }
> > + return false;
> > +}
> > +
> > static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
> > {
> > - struct module_addr_args args = {
> > - .addrs = addrs,
> > - .addrs_cnt = addrs_cnt,
> > - };
> > - int err;
> > + struct modules_array arr = {};
> > + u32 i, err = 0;
> > +
> > + for (i = 0; i < addrs_cnt; i++) {
> > + struct module *mod;
> > +
> > + preempt_disable();
> > + mod = __module_address(addrs[i]);
> > + /* Either no module or we it's already stored */
> > + if (!mod || (mod && has_module(&arr, mod))) {
>
> nit: This can be simplified:
>
> if (!mod || has_module(&arr, mod)) {
yep, will change
thanks,
jirka
prev parent reply other threads:[~2023-01-13 22:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-13 14:33 [PATCHv2 bpf-next 0/3] kallsyms: Optimize the search for module symbols by livepatch and bpf Jiri Olsa
2023-01-13 14:33 ` [PATCHv2 bpf-next 1/3] livepatch: Improve the search performance of module_kallsyms_on_each_symbol() Jiri Olsa
2023-01-13 20:24 ` Song Liu
2023-01-13 14:33 ` [PATCHv2 bpf-next 2/3] selftests/bpf: Add serial_test_kprobe_multi_bench_attach_kernel/module tests Jiri Olsa
2023-01-13 20:27 ` Song Liu
2023-01-13 22:43 ` Andrii Nakryiko
2023-01-15 21:49 ` Jiri Olsa
2023-01-13 14:33 ` [PATCHv2 bpf-next 3/3] bpf: Change modules resolving for kprobe multi link Jiri Olsa
2023-01-13 20:41 ` Song Liu
2023-01-13 22:22 ` 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=Y8HZkKmJyT6dgYKS@krava \
--to=olsajiri@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=john.fastabend@gmail.com \
--cc=jpoimboe@kernel.org \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=linux-modules@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mbenes@suse.cz \
--cc=mcgrof@kernel.org \
--cc=mhiramat@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=songliubraving@fb.com \
--cc=thunder.leizhen@huawei.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).