Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2597155rwb; Sun, 15 Jan 2023 18:46:15 -0800 (PST) X-Google-Smtp-Source: AMrXdXtLP/GkK7aNr1wsvOr1HcDHn5xHusByS+kNzH1K6HY+ETsHLTHZL+1QnYwkv56cJNs2FsSp X-Received: by 2002:a17:902:aa85:b0:189:894c:6b48 with SMTP id d5-20020a170902aa8500b00189894c6b48mr93519822plr.59.1673837175642; Sun, 15 Jan 2023 18:46:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673837175; cv=none; d=google.com; s=arc-20160816; b=Pz1u0vyJz56Q+4sldMyBJtdD9BBrVyU9+eeN6/9A9odiX/1d3JIRjLoYmzfH5w6Mxo UGcFFXtib1M8riEczpB+FlU7Jgjz7IRty97dGUh8Dy8kAlt4kVQ2yZaKECj2150+bHgX Zb60IYjvn51ZkDhtV1jmRMb17Dqh9B0LgmDh9kAnjm0Xh7Ywe7NRA9ZbDjh36horaCtK 1P9/WEUkycE9QSIB+yUvqBRPdetw0HpHigOWyrH95oq1+reiR+MIWh27FDiKSvahj1e7 1ujuTwwwL2NONPB9o/Tyv15yvDn8TGDUXQ3oPbjl/kfO62bnOedGv9pRRvsuZVQxJ1qr b2Lw== 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=ieAvDpUTB6fur3o/45AvL4GwmJeyZ8yDVKySYt8c7jc=; b=gaM6hNT8XP3I7DtJSU8vvKoXnd86q3Scc/UB7npCh5N+rYfVExBNZLMBVBogoGLW+u QSbfnr14gazaWJ+v+NwFANdxiF15ePkYW8BY8bXteS96sOENsEDD8q83ckoE+dBIcDYM i7GTyLJ6WVsmbtsLKXbP6qGE6P4pEot0LIoXshRxljuZs6Zfhb1/B/e5QL/jQ3MFeGFZ R7B2Kvbh2twD6Ppy+X5Db9lv3XgHpNcrzu7UQcG59Zx3PUYZ301mjsDGwT+H8PWJBWzR UaOMpY7YozLCaSn5EKINW2w8unYTKRtMzTvMV9LQwU0GUN1yc9K+CEyIdMXmfXAYE4nh 509Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JoyqPEAW; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y4-20020a170902ed4400b00178170100fasi26627875plb.337.2023.01.15.18.46.09; Sun, 15 Jan 2023 18:46:15 -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=@gmail.com header.s=20210112 header.b=JoyqPEAW; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231660AbjAPCZB (ORCPT + 52 others); Sun, 15 Jan 2023 21:25:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231791AbjAPCYq (ORCPT ); Sun, 15 Jan 2023 21:24:46 -0500 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FD026A78; Sun, 15 Jan 2023 18:24:43 -0800 (PST) Received: by mail-ej1-x62c.google.com with SMTP id v6so21852738ejg.6; Sun, 15 Jan 2023 18:24:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.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=ieAvDpUTB6fur3o/45AvL4GwmJeyZ8yDVKySYt8c7jc=; b=JoyqPEAWZwkWZ+yWyHE4C0FUaBXwaiRjUz9F4eByEMphBQChIHNMLOYXBUjwIg1BzX TrKZ53Dnaa3t01P0prcslC9whuuAr1+ecD6nbP8lYPHB8GdExskzRpsPd2NNZLhxgFbz SSilhEnH/eEd/vE0hHyCL1BGoY2C//nb3h4Z7nQ/Cv5hpiSO3hcXwCcFFyz5NEyEzIS4 HnIgUWLgnwVsQ+fCiyzxJI8jdOztb9AYHGJpY2F2CLbgS+ABpLzwfIgDiL/52EwY49K8 Ocxr6BeJ98Taf4cHfltokR2t3gASuaq/4ssMaKK6sAxqJkk0JSZ/oi2OMRF2LclHjBba ka9g== 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=ieAvDpUTB6fur3o/45AvL4GwmJeyZ8yDVKySYt8c7jc=; b=2jOAnc/nCgKDTip0jjDIqlQHAepTF2a8XgMzvkU8X/o9eJ4kC4ERRpIUwYmn8FQ2T2 c5KjFS6N6gFuLmNGb5MDnQvKclFH6RRS42mkUX2k/rJqmD33llWwezqu6ZEbdt6PwNTG 7i7FTYuekJ7dz6xwQhMn//ZKPdR0/zjxwCw/OF8qiA93P2VIBX+mupJw0w23QCc15EfN vqJb8ZQLjNcE/ob5DSFPgzuafrhWeN6DRDEEBVVUWDXPXcaVPloAoq/ZpkeBc24YMGK4 fjAXhLlesNpq7RBij4Eh8zZ7hySupeXC+mFlYzvEQ3HyrOioZjncgsjZA5zQcRFiqhuX MqGg== X-Gm-Message-State: AFqh2kosDzphkjeZB3t5dKixrjBKREcsjKOgJ9v7rJziZA0N5Z8pDtTS oQzAiGOijfF4SOfyg6PRqFZEKBlfqvXKdZSF1pA= X-Received: by 2002:a17:906:c0c9:b0:86d:ec8c:5b3f with SMTP id bn9-20020a170906c0c900b0086dec8c5b3fmr290445ejb.50.1673835882109; Sun, 15 Jan 2023 18:24:42 -0800 (PST) MIME-Version: 1.0 References: <20230114132705.78400-1-kerneljasonxing@gmail.com> In-Reply-To: From: Jason Xing Date: Mon, 16 Jan 2023 10:24:05 +0800 Message-ID: Subject: Re: [PATCH v2 net] tcp: avoid the lookup process failing to get sk in ehash table To: Eric Dumazet 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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Mon, Jan 16, 2023 at 8:36 AM Jason Xing wrote: > > On Mon, Jan 16, 2023 at 12:12 AM Eric Dumazet wrote: > > > > On Sat, Jan 14, 2023 at 2:27 PM Jason Xing wrote: > > > > > > From: Jason Xing > > > > > > > > > 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/ > > > --- > > > 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 | 10 ++++++++++ > > > net/ipv4/inet_timewait_sock.c | 6 +++--- > > > 2 files changed, 13 insertions(+), 3 deletions(-) > > > > > > diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c > > > index 24a38b56fab9..b0b54ad55507 100644 > > > --- a/net/ipv4/inet_hashtables.c > > > +++ b/net/ipv4/inet_hashtables.c > > > @@ -650,7 +650,16 @@ 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); > > > + if (sk_hashed(osk)) > > > > > > nit: this should be: > > > > if (sk_hashed(osk)) { [1] > > /* multi-line .... > > * .... comment. > > */ > > ret = sk_nulls_del_node_init_rcu(osk); > > goto unlock; > > } Well, after I dug into this part, I found something as below. If we enter into the 'if (osk) {', we should always skip the next if-statement which is 'if (found_dup_sk) {' and return a proper value depending on if the osk is hashed. However, the code as above would leave variable @ret to be true if the sk_hashed(osk) returned false, then It would not go to unlock and then add the node to the list and at last return true which is unexpected. > > if (found_dup_sk) { [2] > > > > 1) parentheses needed in [1] > > 2) No else if in [2], since you added a "goto unlock;" I think this modification is fine and makes the code clearer. Thanks, Jason > > > > I'll do that. It looks much better. > > Thanks, > Jason > > > > + /* 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); > > > ret = sk_nulls_del_node_init_rcu(osk); > > > + goto unlock; > > > } else if (found_dup_sk) { > > > *found_dup_sk = inet_ehash_lookup_by_sk(sk, list); > > > if (*found_dup_sk) > > > @@ -660,6 +669,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); > > > > Thanks.