netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <lmb@isovalent.com>
Cc: <andrii@kernel.org>, <ast@kernel.org>, <bpf@vger.kernel.org>,
	<daniel@iogearbox.net>, <davem@davemloft.net>,
	<dsahern@kernel.org>, <edumazet@google.com>, <haoluo@google.com>,
	<hemanthmalla@gmail.com>, <joe@cilium.io>, <joe@wand.net.nz>,
	<john.fastabend@gmail.com>, <jolsa@kernel.org>,
	<kpsingh@kernel.org>, <kuba@kernel.org>, <kuniyu@amazon.com>,
	<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
	<martin.lau@linux.dev>, <mykolal@fb.com>,
	<netdev@vger.kernel.org>, <pabeni@redhat.com>, <sdf@google.com>,
	<shuah@kernel.org>, <song@kernel.org>,
	<willemdebruijn.kernel@gmail.com>, <yhs@fb.com>
Subject: Re: [PATCH bpf-next v5 6/7] bpf, net: Support SO_REUSEPORT sockets with bpf_sk_assign
Date: Tue, 11 Jul 2023 11:16:34 -0700	[thread overview]
Message-ID: <20230711181634.52860-1-kuniyu@amazon.com> (raw)
In-Reply-To: <CAN+4W8gs84r+PVWgMbic29Opj2EviNMh7AzcP=BR3CLvYHiQWg@mail.gmail.com>

From: Lorenz Bauer <lmb@isovalent.com>
Date: Tue, 11 Jul 2023 17:15:06 +0100
> On Tue, Jul 4, 2023 at 2:46 PM Lorenz Bauer <lmb@isovalent.com> wrote:
> >
> > +static inline
> > +struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff,
> > +                             const struct in6_addr *saddr, const __be16 sport,
> > +                             const struct in6_addr *daddr, const __be16 dport,
> > +                             bool *refcounted, inet6_ehashfn_t *ehashfn)
> > +{
> > +       struct sock *sk, *reuse_sk;
> > +       bool prefetched;
> > +
> > +       sk = skb_steal_sock(skb, refcounted, &prefetched);
> > +       if (!sk)
> > +               return NULL;
> > +
> > +       if (!prefetched)
> > +               return sk;
> > +
> > +       if (sk->sk_protocol == IPPROTO_TCP) {
> > +               if (sk->sk_state != TCP_LISTEN)
> > +                       return sk;
> > +       } else if (sk->sk_protocol == IPPROTO_UDP) {
> > +               if (sk->sk_state != TCP_CLOSE)
> > +                       return sk;
> > +       } else {
> > +               return sk;
> > +       }
> > +
> > +       reuse_sk = inet6_lookup_reuseport(net, sk, skb, doff,
> > +                                         saddr, sport, daddr, ntohs(dport),
> > +                                         ehashfn);
> > +       if (!reuse_sk)
> > +               return sk;
> > +
> > +       /* We've chosen a new reuseport sock which is never refcounted. This
> > +        * implies that sk also isn't refcounted.
> > +        */
> > +       WARN_ON_ONCE(*refcounted);
> > +
> > +       return reuse_sk;
> > +}
> 
> Hi Kuniyuki,
> 
> Continuing the conversation from v5 of the patch set, you wrote:
> 
> In inet6?_steal_sock(), we call inet6?_lookup_reuseport() only for
> sk that was a TCP listener or UDP non-connected socket until just before
> the sk_state checks.  Then, we know *refcounted should be false for such
> sockets even before inet6?_lookup_reuseport().
> 
> This makes sense for me in the TCP listener case. I understand UDP
> less, so I'll have to rely on your input. I tried to convince myself
> that all UDP sockets in TCP_CLOSE have SOCK_RCU_FREE set. However, the
> only place I see sock_set_flag(sk, SOCK_RCU_FREE) in the UDP case is
> in udp_lib_get_port(). That in turn seems to be called during bind.
> So, what if BPF does bpf_sk_assign() of an unbound and unconnected
> socket?  Wouldn't that trigger the warning?

Ah sorry, I assumed it would not happen, but if we can put unbound
TCP/UDP socket into a map and select it, then yes, it hits the warning.

Let's say we can select a non-RCU sk in bpf_sk_assign() and then the
socket is converted to RCU by bind(udp_sk) or listen(tcp_sk).

The sk_is_refcounted() in bpf_sk_assign() returns true and sk_refcnt
is incremented.  Then, I think of two scenarios:

  1) RCU conversion is done before sk_is_refcounted() in skb_steal_sock().
     -> *refcounted is false

  2) RCU conversion is done after skb_steal_sock().
     -> *refcounted is true

In both cases, we need to decrement the refcnt that is bumped up
by bpf_sk_assign().  The sock_put() in the v1 series does not catch
the former case.

How should we track it ?


> 
> To maybe sidestep this question: do you think the location of the
> WARN_ON_ONCE has to prevent this patch set from going in? I've been
> noodling at it for quite a while already and it would be good to see
> it land.

If the issue above happened, I think it could be a blocker.

Thanks!

  reply	other threads:[~2023-07-11 18:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-04 13:46 [PATCH bpf-next v5 0/7] Add SO_REUSEPORT support for TC bpf_sk_assign Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 1/7] udp: re-score reuseport groups when connected sockets are present Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 2/7] net: export inet_lookup_reuseport and inet6_lookup_reuseport Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 3/7] net: remove duplicate reuseport_lookup functions Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 4/7] net: document inet[6]_lookup_reuseport sk_state requirements Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 5/7] net: remove duplicate sk_lookup helpers Lorenz Bauer
2023-07-04 13:46 ` [PATCH bpf-next v5 6/7] bpf, net: Support SO_REUSEPORT sockets with bpf_sk_assign Lorenz Bauer
2023-07-11 16:15   ` Lorenz Bauer
2023-07-11 18:16     ` Kuniyuki Iwashima [this message]
2023-07-04 13:46 ` [PATCH bpf-next v5 7/7] selftests/bpf: Test that SO_REUSEPORT can be used with sk_assign helper Lorenz Bauer

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=20230711181634.52860-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=haoluo@google.com \
    --cc=hemanthmalla@gmail.com \
    --cc=joe@cilium.io \
    --cc=joe@wand.net.nz \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lmb@isovalent.com \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=willemdebruijn.kernel@gmail.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).