Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37260359rwd; Tue, 11 Jul 2023 11:37:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlEWAictFY27Ym4Wlsa3Qmh72rRx3XlNoeU16mBR4BnyC8JMZV1pmNLlk3SNY+5zSV0UNUjs X-Received: by 2002:a17:90b:253:b0:262:d8e7:abfc with SMTP id fz19-20020a17090b025300b00262d8e7abfcmr27569466pjb.17.1689100647103; Tue, 11 Jul 2023 11:37:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689100647; cv=none; d=google.com; s=arc-20160816; b=QH4eIvyZ8vMWaziDf+VGxVDwGz8/1eygEByyhhrjMIOXkTljBo2JHL//ffu0h6S/Vh +6KS4YL4/t2Co74QLsWvSYl7ZJj4bmaSUzdQ9/EGRzmFgFJtnxgCrIGmphdCvoEMObsv WuKYoT+5ALfHtrpJu7rIXivru/qOhyDPcfRNdDo+7m0z0Ei2aTk2JwrkkJ4L5F+Hc8xz DPlxHwmQ5shB0xioAOjhSZxVt2gAV3veqv2O2s3IB07pVB8xZzpq32Wf9hDD8clRsqEc qJ6vywEebSzvt0uGBXM4NvbZ73flEMjIzNPRRvCwOfUPB/p1Kw47rJ0knQq2y3qFoDjj NH2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ejSRqyLc1keD4J84EQBZZKFA9D4wAPd5a7wHIeEh9lk=; fh=uX93oDSK2USwrk3UKWJI9B5Go5W2toUUNoPCfgm/ACY=; b=xR1OqUO7LwT/dByVGKkb31oNtLdQ+Abs5okXKGcqIPQ5QkRbu1DU2L100zWuIKBZbZ xeBFrB/9Kg8e+okBDYK/W3beo9AOolEb2V8YyXR6qK26fNAu5czFc4BrDb4u4SQIA5fE 0ckvyWR4p/p95I4pX+ASmBaZSQt3uhhc0aI/v3i+fRtPRr0+R6dg/GE08tqXOP33P2cx w/UmklLWF8Wz4EHaBagdRsBunBIKa2XMUQh7pQSELuU3Hap0EpQZRUfPPmuEB+sCg1D7 qspugBWvqWJbvoxQJArfQHt5AGvzK2kj3WuBaHfZ7zNMI4fWlBh32kvE6XqMOwex5nIK XYMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lJagRR05; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a64-20020a639043000000b00553c2f8509esi1705325pge.706.2023.07.11.11.37.11; Tue, 11 Jul 2023 11:37:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lJagRR05; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232118AbjGKSRO (ORCPT + 99 others); Tue, 11 Jul 2023 14:17:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230264AbjGKSRN (ORCPT ); Tue, 11 Jul 2023 14:17:13 -0400 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A0779C; Tue, 11 Jul 2023 11:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1689099433; x=1720635433; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ejSRqyLc1keD4J84EQBZZKFA9D4wAPd5a7wHIeEh9lk=; b=lJagRR05LfgvbN+CSWEOnAGSk8pNI9WmVTrfDH2JBjGCU3DglCK+d10s oZZqn9quKgNEzo9FdcykHLTuiyGtoK/iar9JWY0GDhrgp6Npee9aL2PYe 3ezezIEsJIZlCgE0zF8m9y2s42n3V9MMS9beRr3qaYedK3G6oQCzXMqcF Y=; X-IronPort-AV: E=Sophos;i="6.01,197,1684800000"; d="scan'208";a="592379661" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-3554bfcf.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2023 18:17:09 +0000 Received: from EX19MTAUWB002.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1e-m6i4x-3554bfcf.us-east-1.amazon.com (Postfix) with ESMTPS id 5AE7C805D4; Tue, 11 Jul 2023 18:17:03 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Tue, 11 Jul 2023 18:16:49 +0000 Received: from 88665a182662.ant.amazon.com.com (10.187.170.35) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.30; Tue, 11 Jul 2023 18:16:44 +0000 From: Kuniyuki Iwashima To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , 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 Message-ID: <20230711181634.52860-1-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.187.170.35] X-ClientProxiedBy: EX19D045UWA004.ant.amazon.com (10.13.139.91) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE,T_SPF_PERMERROR autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lorenz Bauer Date: Tue, 11 Jul 2023 17:15:06 +0100 > On Tue, Jul 4, 2023 at 2:46 PM Lorenz Bauer 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!