From patchwork Wed Jul 27 06:09:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin KaFai Lau X-Patchwork-Id: 12930091 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9196C19F21 for ; Wed, 27 Jul 2022 06:09:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbiG0GJf (ORCPT ); Wed, 27 Jul 2022 02:09:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230115AbiG0GJc (ORCPT ); Wed, 27 Jul 2022 02:09:32 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 627BA3FA26 for ; Tue, 26 Jul 2022 23:09:31 -0700 (PDT) Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26QNDGcZ000955 for ; Tue, 26 Jul 2022 23:09:30 -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=UKIPQVu6eKMnk726mpApj3QnXSFYtxMB3wm5kiaDCTI=; b=Y/GyFbucYeuaY89Gr/1rYn+OtaxvRAkcYzJ0uc6auH7x9B7WeByk+x0rrlbeofwWZdAN pcLVKPnFyPAHIfxPbcF1gd5a/EsTJBnO1u4jMjfDmcJPWK5OAKEbAVaP8+UVfWkNncXv NLL5MF1Vip3rn74rktgxYbRYjWUHf/YUXEE= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3hj592rgq8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 26 Jul 2022 23:09:30 -0700 Received: from twshared34609.14.frc2.facebook.com (2620:10d:c085:208::11) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Tue, 26 Jul 2022 23:09:29 -0700 Received: by devbig933.frc1.facebook.com (Postfix, from userid 6611) id BEB39757CC81; Tue, 26 Jul 2022 23:09:21 -0700 (PDT) From: Martin KaFai Lau To: , CC: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , David Miller , Eric Dumazet , Jakub Kicinski , , Paolo Abeni Subject: [PATCH bpf-next 04/14] bpf: net: Avoid do_tcp_setsockopt() taking sk lock when called from bpf Date: Tue, 26 Jul 2022 23:09:21 -0700 Message-ID: <20220727060921.2373314-1-kafai@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220727060856.2370358-1-kafai@fb.com> References: <20220727060856.2370358-1-kafai@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-ORIG-GUID: IqEzap8C1wnvUaioF_taz76pivWEKbom X-Proofpoint-GUID: IqEzap8C1wnvUaioF_taz76pivWEKbom X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-07-26_07,2022-07-26_01,2022-06-22_01 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Similar to the earlier patch that avoids sock_setsockopt() from taking sk lock when called from bpf. This patch changes do_tcp_setsockopt() to use the {lock,release}_sock_sockopt(). This patch also changes do_tcp_setsockopt() to check optval.is_bpf when passing the cap_net_admin arg to tcp_set_congestion_control(..., cap_net_admin). It is the same as how bpf_setsockopt(TCP_CONGESTION) is calling tcp_set_congestion_control() now. Signed-off-by: Martin KaFai Lau --- net/ipv4/tcp.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ba2bdc811374..7f8d81befa8e 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3462,11 +3462,12 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, return -EFAULT; name[val] = 0; - lock_sock(sk); - err = tcp_set_congestion_control(sk, name, true, - ns_capable(sock_net(sk)->user_ns, - CAP_NET_ADMIN)); - release_sock(sk); + lock_sock_sockopt(sk, optval); + err = tcp_set_congestion_control(sk, name, !optval.is_bpf, + optval.is_bpf ? + true : ns_capable(sock_net(sk)->user_ns, + CAP_NET_ADMIN)); + release_sock_sockopt(sk, optval); return err; } case TCP_ULP: { @@ -3482,9 +3483,9 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, return -EFAULT; name[val] = 0; - lock_sock(sk); + lock_sock_sockopt(sk, optval); err = tcp_set_ulp(sk, name); - release_sock(sk); + release_sock_sockopt(sk, optval); return err; } case TCP_FASTOPEN_KEY: { @@ -3517,7 +3518,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, if (copy_from_sockptr(&val, optval, sizeof(val))) return -EFAULT; - lock_sock(sk); + lock_sock_sockopt(sk, optval); switch (optname) { case TCP_MAXSEG: @@ -3739,7 +3740,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, break; } - release_sock(sk); + release_sock_sockopt(sk, optval); return err; }