Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp3056473rwb; Mon, 16 Jan 2023 03:10:43 -0800 (PST) X-Google-Smtp-Source: AMrXdXsBHFmaVGa0hhYjp4m+mgqcD3m5UCecp8pEcR32wJI81GFnmoyuW3/KxOmxlSfqcB1LtTeW X-Received: by 2002:a17:902:8481:b0:193:e20:a5a9 with SMTP id c1-20020a170902848100b001930e20a5a9mr34912138plo.15.1673867442804; Mon, 16 Jan 2023 03:10:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673867442; cv=none; d=google.com; s=arc-20160816; b=gj/MHr4yBXqZFhhuF2H2nUt4I+L3VjoKs9XhBlVBD+q8tLW7MLJRi+0LMk+E6Iuu2g o3/yK8VBnSPZMWaPpx5vqCBjXdrEan1mzKMeAvt4Y3kfKQ1ONmnBxZ35nUVDmrp8OQLX iFRzNuDMOO7H5TSUCg0vB5QLSrrLwWPOU7sDtBsAG09HFsV7hD3++9JBAwrxn3ZRFy16 ywyx/j+ZxLOWVbHMI6M3rbdM4jZ8Vpv6nSgsSQ7nRfbD03XZx2laDfU/71a+puvmTQ/A OTOOg2ZpWKhNSdxYRjC8pJDNnL9tTgfS24HHofiYL7JMiJThIvgPKMzyI02oBLNGrmIV 6Hew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wu9td4ZRw+3rlrum00pYyaH6eULDTV19+cq5J6k5AKE=; b=WdlHKenx+ErYp575dpxiZg+/n0156NtIGdVUtyFI8MU76IzHWtqpubsAAReGNF8F9B G0TKQEb8k8EM0eSFdkNGRveaGVtHIZS/65UxFvY1zEsh1SvPWR0RPhNTyc8M3p9uSYZk IvGQAq/8QcxE8e9elZMCki+QD95mcK1mIpSYsO8zlyt58MRJOutkCznsalZq2CHqNsfW L1GBcg9awQCfQw6gw03yBUO9aP5deq+ppndcmA7IoIZ7BWT0dVAO8Crqth96V1XVLb39 rgsQzPow/88kac6blFOi3Ju8F90EtJ6bwka5xD5o4bTfjELZZrwQOD9C8jYNI4gVWVG5 nsmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Br4MTMaS; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u185-20020a6385c2000000b00498fda6b663si6125061pgd.36.2023.01.16.03.10.36; Mon, 16 Jan 2023 03:10:42 -0800 (PST) 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=@google.com header.s=20210112 header.b=Br4MTMaS; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229600AbjAPJz4 (ORCPT + 51 others); Mon, 16 Jan 2023 04:55:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229836AbjAPJz2 (ORCPT ); Mon, 16 Jan 2023 04:55:28 -0500 Received: from mail-yb1-xb2c.google.com (mail-yb1-xb2c.google.com [IPv6:2607:f8b0:4864:20::b2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80DAF17CD4 for ; Mon, 16 Jan 2023 01:54:55 -0800 (PST) Received: by mail-yb1-xb2c.google.com with SMTP id a9so12965517ybb.3 for ; Mon, 16 Jan 2023 01:54:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=wu9td4ZRw+3rlrum00pYyaH6eULDTV19+cq5J6k5AKE=; b=Br4MTMaSVSXIVN1w+T0iS6EMQBtrqn8dhHIWOvvDY2TFrFtCVOWPsCb7WG366TSIzY 9N6KakVUJkyXGTLeOJ4XSJvTNZzazACjXsI4rxsXiLxmZlgqIDZCvPo5RGMBLY0jybPm uLbFcuxtrseJmHu6iAtB/tJw1KN9usHhNhRoWq2OPSinjs61rlKklt7UmrRqH9Wu3z+p Z1E7Cjv/9KZ/eC8JNY1iJkhEHORH5LVDEmsatdcpOxjyL0zVx8ab7lOpcjWpcqALpDNV DIsahZEy7qsZ53Bb8CVea/1VrGse9VFp7P5PUCeDgXT/9UL/k99vv5cFOZ2BEsiOCLS/ TG7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wu9td4ZRw+3rlrum00pYyaH6eULDTV19+cq5J6k5AKE=; b=jCUZw9gn2iIrRZn3Q94kWC5dqB78bwesgJXlqkDHc55whPSP+ak45oU7JRafXj1/kn /kdZ4wXBs1ZYah1kjocPlaxP2pDhlmIthhdnyxKz5oq1i5ylvcGo/j0nHmOrf1HCW3NC p5j0RspBpH57OUqpya7bKQGIj/fnp/3H8mokq5YELsLb2GcYMLmNWpqMxLhcT8j+SiIV XFi6ZbBM55ug6i3lDishOzLPdlhQSygnYADUngdeGWwHgulBwTzaElb1pPPcdZyHnbCC 71iZFLN2mVd4pC0EGa3pl0rjri5ASrya9x4Cd35waQ6FSn8jb+BV1owDTFj5MzUuysII c5jQ== X-Gm-Message-State: AFqh2kqG55Do93I8s3DevtnxxMbn4/C/8h/7w32XVUfwEj1urdQg9dNA YYUCuQzdDcriGPEsh04OVNP1kZFj4+8RFczAbK5c1g== X-Received: by 2002:a25:b7cb:0:b0:7e7:7ad8:2ee8 with SMTP id u11-20020a25b7cb000000b007e77ad82ee8mr120309ybj.55.1673862894433; Mon, 16 Jan 2023 01:54:54 -0800 (PST) MIME-Version: 1.0 References: <20230116073813.24097-1-kerneljasonxing@gmail.com> In-Reply-To: <20230116073813.24097-1-kerneljasonxing@gmail.com> From: Eric Dumazet Date: Mon, 16 Jan 2023 10:54:43 +0100 Message-ID: Subject: Re: [PATCH v4 net] tcp: avoid the lookup process failing to get sk in ehash table To: Jason Xing Cc: davem@davemloft.net, yoshfuji@linux-ipv6.org, dsahern@kernel.org, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Jason Xing Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 On Mon, Jan 16, 2023 at 8:38 AM Jason Xing wrote: > > From: Jason Xing > > While one cpu is working on looking up the right socket from ehash > table, another cpu is done deleting the request socket and is about > to add (or is adding) the big socket from the table. It means that > we could miss both of them, even though it has little chance. > > > Fixes: 5e0724d027f0 ("tcp/dccp: fix hashdance race for passive sessions") > Suggested-by: Eric Dumazet > Signed-off-by: Jason Xing > Link: https://lore.kernel.org/lkml/20230112065336.41034-1-kerneljasonxing@gmail.com/ > --- > v4: > 1) adjust the code style and make it easier to read. > > v3: > 1) get rid of else-if statement. > > v2: > 1) adding the sk node into the tail of list to prevent the race. > 2) fix the race condition when handling time-wait socket hashdance. > --- > net/ipv4/inet_hashtables.c | 18 ++++++++++++++++-- > net/ipv4/inet_timewait_sock.c | 6 +++--- > 2 files changed, 19 insertions(+), 5 deletions(-) > > diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c > index 24a38b56fab9..c64eec874b31 100644 > --- a/net/ipv4/inet_hashtables.c > +++ b/net/ipv4/inet_hashtables.c > @@ -650,8 +650,21 @@ bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk) > spin_lock(lock); > if (osk) { > WARN_ON_ONCE(sk->sk_hash != osk->sk_hash); > - ret = sk_nulls_del_node_init_rcu(osk); > - } else if (found_dup_sk) { > + if (sk_hashed(osk)) { > + /* Before deleting the node, we insert a new one to make > + * sure that the look-up-sk process would not miss either > + * of them and that at least one node would exist in ehash > + * table all the time. Otherwise there's a tiny chance > + * that lookup process could find nothing in ehash table. > + */ > + __sk_nulls_add_node_tail_rcu(sk, list); > + sk_nulls_del_node_init_rcu(osk); > + } else { > + ret = false; Well, you added another 'else' statement... What about the following ? diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 24a38b56fab9e9d7d893e23b30d26e275359ec70..1bcf5ce8dd1317b2144bcb47a2ad238532b9accf 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -650,8 +650,14 @@ bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk) spin_lock(lock); if (osk) { WARN_ON_ONCE(sk->sk_hash != osk->sk_hash); - ret = sk_nulls_del_node_init_rcu(osk); - } else if (found_dup_sk) { + ret = sk_hashed(osk); + if (ret) { + __sk_nulls_add_node_tail_rcu(sk, list); + sk_nulls_del_node_init_rcu(osk); + } + goto unlock; + } + if (found_dup_sk) { *found_dup_sk = inet_ehash_lookup_by_sk(sk, list); if (*found_dup_sk) ret = false; @@ -659,7 +665,7 @@ bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk) if (ret) __sk_nulls_add_node_rcu(sk, list); - +unlock: spin_unlock(lock); return ret;