From: Leon Hwang <leon.hwang@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, "Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
"Martin KaFai Lau" <martin.lau@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Puranjay Mohan" <puranjay@kernel.org>,
"Xu Kuohai" <xukuohai@huaweicloud.com>,
"Eddy Z" <eddyz87@gmail.com>,
"Ilya Leoshkevich" <iii@linux.ibm.com>,
kernel-patches-bot@fb.com
Subject: Re: [PATCH bpf-next v7 1/2] bpf: Prevent tailcall infinite loop caused by freplace
Date: Fri, 11 Oct 2024 11:27:41 +0800 [thread overview]
Message-ID: <c7e49c48-7644-40c3-a4a2-664cc16a702c@linux.dev> (raw)
In-Reply-To: <CAADnVQL8ie=xxCXt7td=ZhQwyY_hKtig-y9kHwWYwBG9MdfRQA@mail.gmail.com>
On 11/10/24 01:09, Alexei Starovoitov wrote:
> On Thu, Oct 10, 2024 at 8:39 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>
>> -static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
>> +static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link,
>> + struct bpf_trampoline *tr,
>> + struct bpf_prog *tgt_prog)
>> {
>> enum bpf_tramp_prog_type kind;
>> struct bpf_tramp_link *link_exiting;
>> @@ -544,6 +546,17 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_tr
>> /* Cannot attach extension if fentry/fexit are in use. */
>> if (cnt)
>> return -EBUSY;
>> + guard(mutex)(&tgt_prog->aux->ext_mutex);
>> + if (tgt_prog->aux->prog_array_member_cnt)
>> + /* Program extensions can not extend target prog when
>> + * the target prog has been updated to any prog_array
>> + * map as tail callee. It's to prevent a potential
>> + * infinite loop like:
>> + * tgt prog entry -> tgt prog subprog -> freplace prog
>> + * entry --tailcall-> tgt prog entry.
>> + */
>> + return -EBUSY;
>> + tgt_prog->aux->is_extended = true;
>> tr->extension_prog = link->link.prog;
>> return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
>> link->link.prog->bpf_func);
>
> The suggestion to use guard(mutex) shouldn't be applied mindlessly.
> Here you extend the mutex holding range all the way through
> bpf_arch_text_poke().
> This is wrong.
>
Understood. The guard(mutex) should indeed limit the mutex holding range
to as small as possible. I’ll adjust accordingly.
>> if (kind == BPF_TRAMP_REPLACE) {
>> WARN_ON_ONCE(!tr->extension_prog);
>> + guard(mutex)(&tgt_prog->aux->ext_mutex);
>> err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
>> tr->extension_prog->bpf_func, NULL);
>> tr->extension_prog = NULL;
>> + tgt_prog->aux->is_extended = false;
>> return err;
>
> Same here. Clearly wrong to grab the mutex for the duration of poke.
>
Ack.
> Also Xu's suggestion makes sense to me.
> "extension prog should not be tailcalled independently"
>
> So I would disable such case as a part of this patch as well.
>
I’m fine with adding this restriction.
However, it will break a use case that works on the 5.15 kernel:
libxdp XDP dispatcher --> subprog --> freplace A --tailcall-> freplace B.
With this limitation, the chain 'freplace A --tailcall-> freplace B'
will no longer work.
To comply with the new restriction, the use case would need to be
updated to:
libxdp XDP dispatcher --> subprog --> freplace A --tailcall-> XDP B.
> pw-bot: cr
Thanks,
Leon
next prev parent reply other threads:[~2024-10-11 3:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-10 15:38 [PATCH bpf-next v7 0/2] bpf: Fix tailcall infinite loop caused by freplace Leon Hwang
2024-10-10 15:38 ` [PATCH bpf-next v7 1/2] bpf: Prevent " Leon Hwang
2024-10-10 17:09 ` Alexei Starovoitov
2024-10-11 3:27 ` Leon Hwang [this message]
2024-10-11 15:50 ` Alexei Starovoitov
2024-10-14 13:17 ` Leon Hwang
2024-10-15 2:00 ` Alexei Starovoitov
2024-10-10 15:38 ` [PATCH bpf-next v7 2/2] selftests/bpf: Add test to verify tailcall and freplace restrictions Leon Hwang
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=c7e49c48-7644-40c3-a4a2-664cc16a702c@linux.dev \
--to=leon.hwang@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=iii@linux.ibm.com \
--cc=kernel-patches-bot@fb.com \
--cc=martin.lau@kernel.org \
--cc=puranjay@kernel.org \
--cc=toke@redhat.com \
--cc=xukuohai@huaweicloud.com \
--cc=yonghong.song@linux.dev \
/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