From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43FBDC11F67 for ; Thu, 1 Jul 2021 20:06:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2697D6140D for ; Thu, 1 Jul 2021 20:06:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232628AbhGAUIj (ORCPT ); Thu, 1 Jul 2021 16:08:39 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:46888 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233478AbhGAUIi (ORCPT ); Thu, 1 Jul 2021 16:08:38 -0400 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.43/8.16.0.43) with SMTP id 161K0TVe029753 for ; Thu, 1 Jul 2021 13:06:07 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=fKzEvD488zdMvKvdVK5tew9J1ea9zhywAedqAsDgoDg=; b=lTmm8rVAUKAuVd0B8zvBTackCHK3Fi6EISA2QUJ04AeuFUmobZKveKK8LKZYCfanQ74B Dd9Kuyk1hD/3tvCrWsJpf/YhsU3dUnRJgqF6OPRqU6zHRmgLAZjuolC0o3D8Om1oIOJ4 SdOXBcK8USXAJbfmoxeDZgQ18PuyhGaU4JM= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0089730.ppops.net with ESMTP id 39h1y1x7tk-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 01 Jul 2021 13:06:07 -0700 Received: from intmgw001.38.frc1.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::e) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 1 Jul 2021 13:06:05 -0700 Received: by devbig005.ftw2.facebook.com (Postfix, from userid 6611) id 9718D2940BB9; Thu, 1 Jul 2021 13:06:00 -0700 (PDT) From: Martin KaFai Lau To: CC: Alexei Starovoitov , Daniel Borkmann , Eric Dumazet , , Neal Cardwell , , Yonghong Song , Yuchung Cheng Subject: [PATCH v2 bpf-next 4/8] tcp: seq_file: Add listening_get_first() Date: Thu, 1 Jul 2021 13:06:00 -0700 Message-ID: <20210701200600.1035353-1-kafai@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210701200535.1033513-1-kafai@fb.com> References: <20210701200535.1033513-1-kafai@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: 5gofbYjQHc-j84cGkBlM7-SxT0fd2HXe X-Proofpoint-ORIG-GUID: 5gofbYjQHc-j84cGkBlM7-SxT0fd2HXe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-07-01_12:2021-07-01,2021-07-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 phishscore=0 adultscore=0 suspectscore=0 priorityscore=1501 bulkscore=0 malwarescore=0 spamscore=0 mlxlogscore=933 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107010117 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The current listening_get_next() is overloaded by passing NULL to the 2nd arg, like listening_get_next(seq, NULL), to mean get_first(). This patch moves some logic from the listening_get_next() into a new function listening_get_first(). It will be equivalent to the current established_get_first() and established_get_next() setup. get_first() is to find a non empty bucket and return the first sk. get_next() is to find the next sk of the current bucket and then resorts to get_first() if the current bucket is exhausted. The next patch is to move the listener seq_file iteration from listening_hash (port only) to lhash2 (port+addr). Separating out listening_get_first() from listening_get_next() here will make the following lhash2 changes cleaner and easier to follow. Acked-by: Yonghong Song Signed-off-by: Martin KaFai Lau --- net/ipv4/tcp_ipv4.c | 59 ++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 6071391b9c0f..fc2c2ecd10e1 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2288,10 +2288,38 @@ static bool seq_sk_match(struct seq_file *seq, co= nst struct sock *sk) net_eq(sock_net(sk), seq_file_net(seq))); } =20 -/* - * Get next listener socket follow cur. If cur is NULL, get first socke= t - * starting from bucket given in st->bucket; when st->bucket is zero the - * very first socket in the hash table is returned. +/* Find a non empty bucket (starting from st->bucket) + * and return the first sk from it. + */ +static void *listening_get_first(struct seq_file *seq) +{ + struct tcp_iter_state *st =3D seq->private; + + st->offset =3D 0; + for (; st->bucket < INET_LHTABLE_SIZE; st->bucket++) { + struct inet_listen_hashbucket *ilb; + struct hlist_nulls_node *node; + struct sock *sk; + + ilb =3D &tcp_hashinfo.listening_hash[st->bucket]; + if (hlist_nulls_empty(&ilb->nulls_head)) + continue; + + spin_lock(&ilb->lock); + sk_nulls_for_each(sk, node, &ilb->nulls_head) { + if (seq_sk_match(seq, sk)) + return sk; + } + spin_unlock(&ilb->lock); + } + + return NULL; +} + +/* Find the next sk of "cur" within the same bucket (i.e. st->bucket). + * If "cur" is the last one in the st->bucket, + * call listening_get_first() to return the first sk of the next + * non empty bucket. */ static void *listening_get_next(struct seq_file *seq, void *cur) { @@ -2300,29 +2328,20 @@ static void *listening_get_next(struct seq_file *= seq, void *cur) struct hlist_nulls_node *node; struct sock *sk =3D cur; =20 - if (!sk) { -get_head: - ilb =3D &tcp_hashinfo.listening_hash[st->bucket]; - spin_lock(&ilb->lock); - sk =3D sk_nulls_head(&ilb->nulls_head); - st->offset =3D 0; - goto get_sk; - } - ilb =3D &tcp_hashinfo.listening_hash[st->bucket]; ++st->num; ++st->offset; =20 sk =3D sk_nulls_next(sk); -get_sk: + sk_nulls_for_each_from(sk, node) { if (seq_sk_match(seq, sk)) return sk; } + + ilb =3D &tcp_hashinfo.listening_hash[st->bucket]; spin_unlock(&ilb->lock); - st->offset =3D 0; - if (++st->bucket < INET_LHTABLE_SIZE) - goto get_head; - return NULL; + ++st->bucket; + return listening_get_first(seq); } =20 static void *listening_get_idx(struct seq_file *seq, loff_t *pos) @@ -2332,7 +2351,7 @@ static void *listening_get_idx(struct seq_file *seq= , loff_t *pos) =20 st->bucket =3D 0; st->offset =3D 0; - rc =3D listening_get_next(seq, NULL); + rc =3D listening_get_first(seq); =20 while (rc && *pos) { rc =3D listening_get_next(seq, rc); @@ -2440,7 +2459,7 @@ static void *tcp_seek_last_pos(struct seq_file *seq= ) if (st->bucket >=3D INET_LHTABLE_SIZE) break; st->state =3D TCP_SEQ_STATE_LISTENING; - rc =3D listening_get_next(seq, NULL); + rc =3D listening_get_first(seq); while (offset-- && rc && bucket =3D=3D st->bucket) rc =3D listening_get_next(seq, rc); if (rc) --=20 2.30.2