From: Maxim Mikityanskiy <maxtram95@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Eduard Zingerman <eddyz87@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
Shuah Khan <shuah@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Jesper Dangaard Brouer <hawk@kernel.org>,
bpf <bpf@vger.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>,
Network Development <netdev@vger.kernel.org>,
Maxim Mikityanskiy <maxim@isovalent.com>
Subject: Re: [PATCH bpf-next 08/15] bpf: Assign ID to scalars on spill
Date: Mon, 25 Dec 2023 23:11:30 +0200 [thread overview]
Message-ID: <ZYnwAt941SOohbzx@mail.gmail.com> (raw)
In-Reply-To: <CAADnVQ+6MjSLRq5hFy=kHosoWR=RDOSuU1znCrkcRp-WeD5CMw@mail.gmail.com>
On Sun, 24 Dec 2023 at 19:15:42 -0800, Alexei Starovoitov wrote:
> On Wed, Dec 20, 2023 at 1:40 PM Maxim Mikityanskiy <maxtram95@gmail.com> wrote:
> >
> > From: Maxim Mikityanskiy <maxim@isovalent.com>
> >
> > Currently, when a scalar bounded register is spilled to the stack, its
> > ID is preserved, but only if was already assigned, i.e. if this register
> > was MOVed before.
> >
> > Assign an ID on spill if none is set, so that equal scalars could be
> > tracked if a register is spilled to the stack and filled into another
> > register.
> >
> > One test is adjusted to reflect the change in register IDs.
> >
> > Signed-off-by: Maxim Mikityanskiy <maxim@isovalent.com>
> > ---
> > kernel/bpf/verifier.c | 8 +++++++-
> > .../selftests/bpf/progs/verifier_direct_packet_access.c | 2 +-
> > 2 files changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > index b757fdbbbdd2..caa768f1e369 100644
> > --- a/kernel/bpf/verifier.c
> > +++ b/kernel/bpf/verifier.c
> > @@ -4503,9 +4503,15 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,
> >
> > mark_stack_slot_scratched(env, spi);
> > if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && env->bpf_capable) {
> > + bool reg_value_fits;
> > +
> > + reg_value_fits = get_reg_width(reg) <= BITS_PER_BYTE * size;
> > + /* Make sure that reg had an ID to build a relation on spill. */
> > + if (reg_value_fits)
> > + assign_scalar_id_before_mov(env, reg);
>
> Thanks.
> I just debugged this issue as part of my bpf_cmp series.
>
> llvm generated:
>
> 1093: (7b) *(u64 *)(r10 -96) = r0 ;
> R0_w=scalar(smin=smin32=-4095,smax=smax32=256) R10=fp0
> fp-96_w=scalar(smin=smin32=-4095,smax=smax32=256)
> ; if (bpf_cmp(filepart_length, >, MAX_PATH))
> 1094: (25) if r0 > 0x100 goto pc+903 ;
> R0_w=scalar(id=53,smin=smin32=0,smax=umax=smax32=umax32=256,var_off=(0x0;
> 0x1ff))
>
> the verifier refined the range of 'r0' here,
> but the code just read spilled value from stack:
>
> 1116: (79) r1 = *(u64 *)(r10 -64) ; R1_w=map_value
> ; payload += filepart_length;
> 1117: (79) r2 = *(u64 *)(r10 -96) ;
> R2_w=scalar(smin=smin32=-4095,smax=smax32=256) R10=fp0
> fp-96=scalar(smin=smin32=-4095,smax=smax32=256)
> 1118: (0f) r1 += r2 ;
> R1_w=map_value(map=data_heap,ks=4,vs=23040,off=148,smin=smin32=-4095,smax=smax32=3344)
>
> And later errors as:
> "R1 min value is negative, either use unsigned index or do a if (index
> >=0) check."
>
> This verifier improvement is certainly necessary.
Glad that you found it useful!
> Since you've analyzed this issue did you figure out a workaround
> for C code on existing and older kernels?
Uhm... in my case (Cilium, it was a while ago) I did some big change
(reorganized function calls and revalidate_data() calls) that changed
codegen significantly, and the problematic pattern disappeared.
I can suggest trying to play with volatile, e.g., declare
filepart_length as volatile; if it doesn't help, create another volatile
variable and copy filepart_length to it before doing bpf_cmp (copying
reg->reg will assign an ID, but I'm not sure if they'll still be in
registers after being declared as volatile).
Unfortunately, I couldn't reproduce your issue locally, so I couldn't
try these suggestions myself. Is this the right code, or should I take
it from elsewhere?
https://patchwork.kernel.org/project/netdevbpf/list/?series=812010
What LLVM version do you see the issue on? I can try to look for a
specific C workaround if I reproduce it locally.
BTW, the asm workaround is obvious (copy reg to another reg to assign an
ID), so maybe an inline asm like this would do the thing?
asm volatile("r8 = %0" :: "r"(filepart_length) : "r8");
next prev parent reply other threads:[~2023-12-25 21:11 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-20 21:39 [PATCH bpf-next 00/15] Improvements for tracking scalars in the BPF verifier Maxim Mikityanskiy
2023-12-20 21:39 ` [PATCH bpf-next 01/15] selftests/bpf: Fix the u64_offset_to_skb_data test Maxim Mikityanskiy
2023-12-26 9:52 ` Shung-Hsi Yu
2023-12-26 10:38 ` Maxim Mikityanskiy
2023-12-26 13:22 ` Shung-Hsi Yu
2023-12-20 21:40 ` [PATCH bpf-next 02/15] bpf: make infinite loop detection in is_state_visited() exact Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 03/15] selftests/bpf: check if imprecise stack spills confuse infinite loop detection Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 04/15] bpf: Make bpf_for_each_spilled_reg consider narrow spills Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 05/15] selftests/bpf: Add a test case for 32-bit spill tracking Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 06/15] bpf: Add the assign_scalar_id_before_mov function Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 07/15] bpf: Add the get_reg_width function Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 08/15] bpf: Assign ID to scalars on spill Maxim Mikityanskiy
2023-12-25 3:15 ` Alexei Starovoitov
2023-12-25 21:11 ` Maxim Mikityanskiy [this message]
2023-12-25 21:26 ` Alexei Starovoitov
2023-12-20 21:40 ` [PATCH bpf-next 09/15] selftests/bpf: Test assigning " Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 10/15] bpf: Track spilled unbounded scalars Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 11/15] selftests/bpf: Test tracking " Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 12/15] bpf: Preserve boundaries and track scalars on narrowing fill Maxim Mikityanskiy
2023-12-26 5:29 ` Shung-Hsi Yu
[not found] ` <a4c8b7b9f03ff3455fbf430862b370abe9337bc9.camel@gmail.com>
2024-01-05 17:48 ` Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 13/15] selftests/bpf: Add test cases for " Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 14/15] bpf: Optimize state pruning for spilled scalars Maxim Mikityanskiy
2023-12-20 21:40 ` [PATCH bpf-next 15/15] selftests/bpf: states pruning checks for scalar vs STACK_{MISC,ZERO} Maxim Mikityanskiy
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=ZYnwAt941SOohbzx@mail.gmail.com \
--to=maxtram95@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=maxim@isovalent.com \
--cc=mykolal@fb.com \
--cc=netdev@vger.kernel.org \
--cc=sdf@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--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;
as well as URLs for NNTP newsgroup(s).