BPF List
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <alexei.starovoitov@gmail.com>
Cc: <andrii@kernel.org>, <ast@kernel.org>, <bpf@vger.kernel.org>,
	<daniel@iogearbox.net>, <kuni1840@gmail.com>, <kuniyu@amazon.com>,
	<netdev@vger.kernel.org>
Subject: Re: [PATCH v1 bpf 1/4] bpf: Fix data-races around bpf_jit_enable.
Date: Thu, 18 Aug 2022 20:46:35 -0700	[thread overview]
Message-ID: <20220819034635.67875-1-kuniyu@amazon.com> (raw)
In-Reply-To: <CAADnVQJHdxu43rPgpfQ-ezR-Vt3xW2YP7SXUayfoEg+3BCps5w@mail.gmail.com>

From:   Alexei Starovoitov <alexei.starovoitov@gmail.com>
Date:   Thu, 18 Aug 2022 20:27:49 -0700
> On Thu, Aug 18, 2022 at 6:15 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > From:   Alexei Starovoitov <alexei.starovoitov@gmail.com>
> > Date:   Thu, 18 Aug 2022 18:05:44 -0700
> > > On Thu, Aug 18, 2022 at 5:56 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > > >
> > > > From:   Alexei Starovoitov <alexei.starovoitov@gmail.com>
> > > > Date:   Thu, 18 Aug 2022 17:13:25 -0700
> > > > > On Thu, Aug 18, 2022 at 5:07 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > > > > >
> > > > > > From:   Alexei Starovoitov <alexei.starovoitov@gmail.com>
> > > > > > Date:   Thu, 18 Aug 2022 15:49:46 -0700
> > > > > > > On Wed, Aug 17, 2022 at 9:24 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > > > > > > >
> > > > > > > > A sysctl variable bpf_jit_enable is accessed concurrently, and there is
> > > > > > > > always a chance of data-race.  So, all readers and a writer need some
> > > > > > > > basic protection to avoid load/store-tearing.
> > > > > > > >
> > > > > > > > Fixes: 0a14842f5a3c ("net: filter: Just In Time compiler for x86-64")
> > > > > > > > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > > > > > > > ---
> > > > > > > >  arch/arm/net/bpf_jit_32.c        | 2 +-
> > > > > > > >  arch/arm64/net/bpf_jit_comp.c    | 2 +-
> > > > > > > >  arch/mips/net/bpf_jit_comp.c     | 2 +-
> > > > > > > >  arch/powerpc/net/bpf_jit_comp.c  | 5 +++--
> > > > > > > >  arch/riscv/net/bpf_jit_core.c    | 2 +-
> > > > > > > >  arch/s390/net/bpf_jit_comp.c     | 2 +-
> > > > > > > >  arch/sparc/net/bpf_jit_comp_32.c | 5 +++--
> > > > > > > >  arch/sparc/net/bpf_jit_comp_64.c | 5 +++--
> > > > > > > >  arch/x86/net/bpf_jit_comp.c      | 2 +-
> > > > > > > >  arch/x86/net/bpf_jit_comp32.c    | 2 +-
> > > > > > > >  include/linux/filter.h           | 2 +-
> > > > > > > >  net/core/sysctl_net_core.c       | 4 ++--
> > > > > > > >  12 files changed, 19 insertions(+), 16 deletions(-)
> > > > > > > >
> > > > > > > > diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c
> > > > > > > > index 6a1c9fca5260..4b6b62a6fdd4 100644
> > > > > > > > --- a/arch/arm/net/bpf_jit_32.c
> > > > > > > > +++ b/arch/arm/net/bpf_jit_32.c
> > > > > > > > @@ -1999,7 +1999,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
> > > > > > > >         }
> > > > > > > >         flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
> > > > > > > >
> > > > > > > > -       if (bpf_jit_enable > 1)
> > > > > > > > +       if (READ_ONCE(bpf_jit_enable) > 1)
> > > > > > >
> > > > > > > Nack.
> > > > > > > Even if the compiler decides to use single byte loads for some
> > > > > > > odd reason there is no issue here.
> > > > > >
> > > > > > I see, and same for 2nd/3rd patches, right?
> > > > > >
> > > > > > Then how about this part?
> > > > > > It's not data-race nor problematic in practice, but should the value be
> > > > > > consistent in the same function?
> > > > > > The 2nd/3rd patches also have this kind of part.
> > > > >
> > > > > The bof_jit_enable > 1 is unsupported and buggy.
> > > > > It will be removed eventually.
> > > >
> > > > Ok, then I'm fine with no change.
> > > >
> > > > >
> > > > > Why are you doing these changes if they're not fixing any bugs ?
> > > > > Just to shut up some race sanitizer?
> > > >
> > > > For data-race, it's one of reason.  I should have made sure the change fixes
> > > > an actual bug, my apologies.
> > > >
> > > > For two reads, I feel buggy that there's an inconsitent snapshot.
> > > > e.g.) in the 2nd patch, bpf_jit_harden == 0 in bpf_jit_blinding_enabled()
> > > > could return true.  Thinking the previous value was 1, it seems to be timing
> > > > issue, but not intuitive.
> > >
> > > it's also used in bpf_jit_kallsyms_enabled.
> > > So the patch 2 doesn't make anything 'intuitive'.
> >
> > Exactly...
> >
> > So finally, should I repost 4th patch or drop it?
> 
> This?
> -       if (atomic_long_add_return(size, &bpf_jit_current) > bpf_jit_limit) {
> +       if (atomic_long_add_return(size, &bpf_jit_current) >
> READ_ONCE(bpf_jit_limit)) {
> 
> same question. What does it fix?

Its size is long, and load tearing [0] could occur by compiler
optimisation.  So, concurrent writes & a teared-read could get
a bigger limit than intended.

        write 0xFFFFFFFF00000000
  teared-read 0xFFFFFFFF
        write 0x00000000FFFFFFFF
  teared-read 0xFFFFFFFFFFFFFFFF

[0]: https://lwn.net/Articles/793253/#Load%20Tearing

  reply	other threads:[~2022-08-19  3:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-18  4:23 [PATCH v1 bpf 0/4] bpf: sysctl: Fix data-races around net.core.bpf_XXX Kuniyuki Iwashima
2022-08-18  4:23 ` [PATCH v1 bpf 1/4] bpf: Fix data-races around bpf_jit_enable Kuniyuki Iwashima
2022-08-18 22:49   ` Alexei Starovoitov
2022-08-19  0:06     ` Kuniyuki Iwashima
2022-08-19  0:13       ` Alexei Starovoitov
2022-08-19  0:55         ` Kuniyuki Iwashima
2022-08-19  1:05           ` Alexei Starovoitov
2022-08-19  1:15             ` Kuniyuki Iwashima
2022-08-19  3:27               ` Alexei Starovoitov
2022-08-19  3:46                 ` Kuniyuki Iwashima [this message]
2022-08-19 22:56                   ` Alexei Starovoitov
2022-08-18  4:23 ` [PATCH v1 bpf 2/4] bpf: Fix data-races around bpf_jit_harden Kuniyuki Iwashima
2022-08-18  4:23 ` [PATCH v1 bpf 3/4] bpf: Fix data-races around bpf_jit_kallsyms Kuniyuki Iwashima
2022-08-18  4:23 ` [PATCH v1 bpf 4/4] bpf: Fix a data-race around bpf_jit_limit Kuniyuki Iwashima
2022-08-24  1:10 ` [PATCH v1 bpf 0/4] bpf: sysctl: Fix data-races around net.core.bpf_XXX dongdwdw

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=20220819034635.67875-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    /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