From patchwork Wed May 11 23:37:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846715 X-Patchwork-Delegate: kuba@kernel.org 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 73F3BC433F5 for ; Wed, 11 May 2022 23:38:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349385AbiEKXiK (ORCPT ); Wed, 11 May 2022 19:38:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbiEKXiG (ORCPT ); Wed, 11 May 2022 19:38:06 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF5CA62237 for ; Wed, 11 May 2022 16:38:03 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id p8so3264812pfh.8 for ; Wed, 11 May 2022 16:38:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=h0IOGqevoL4tbD+Eqh27YIST+kiKHUWghSlTlLJdS0U=; b=nWtgYgZ6iiHcprYbGamzQnlWooBcaZ9PVlDl4gKbOJJ1En7aAawjwN1iZ2U172i6rk B7GyOvXZ7VCh3theVn/P6WUn5llGg0kJM0DeWarsou9fRCg/SJrGKyiH1Qc6TUJyQbK0 obfUOs2+yD1TqETlklevVIH3n/+GLg0cGJcRyF8ENI2xNUj4lGwE/bcBZxpJmXmv1JFf G9fdpv57A7Zb0YGl9jbspiGv37DN59HfANJmvM02Giqwfrod0MA+KUEG4sJxq+2H/v4f vkbxzgigge3Gt0TQAE+mj3+nbJiFZqCEE4iJndQsIvxL6qyj49cBQOGDAsuszmHlOwyS zm1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=h0IOGqevoL4tbD+Eqh27YIST+kiKHUWghSlTlLJdS0U=; b=VNcqFLmrHO55jkkCy9m5XVRtdLsff5sB2l5VSptuyPAOeBnuINUZKGTCqhQDZdtTxj +x+r4NLNy7g6fVGncb2/M+t18+FsReqe/7FNFLWkzdNjM32Xp1IxlkoLSh/IWL/Y7gRM SaJXRmU07jwKeg3DV7DlJ0+jViEmcWHxKhfSTY1PI/wOtfbyB0rVL4L6fQW1AvxkMYQA CAinvrEsQcIxPpT+/z2ZQZypWNAdYG7WMWlvtpBsjPhElHswmy/8O79mVGNdXeCJkUFm xQIR2vZrVIDaSXNzI3QkSkjnnqnBurly9pjrfeSL1yWBhrNK69+oJjzUWTwaXYCeOQ7E rS8g== X-Gm-Message-State: AOAM532f5zWntI9hTzDdMldoVCsqywiue0r7fL9hKwBOh3m3vSgq039T +rG/fB18Q7f1hbM5tvwPVm4= X-Google-Smtp-Source: ABdhPJz1J+BJVe0bD0T/5eEH2RLlVvKvPZDyzm9LacBWZB1jI5/LwtC0BWhsSbtaMbHHUD1xXmhXfA== X-Received: by 2002:a05:6a00:8c2:b0:510:98ac:96c9 with SMTP id s2-20020a056a0008c200b0051098ac96c9mr20311229pfu.18.1652312283484; Wed, 11 May 2022 16:38:03 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:03 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet , syzbot Subject: [PATCH net-next 01/10] net: annotate races around sk->sk_bound_dev_if Date: Wed, 11 May 2022 16:37:48 -0700 Message-Id: <20220511233757.2001218-2-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet UDP sendmsg() is lockless, and reads sk->sk_bound_dev_if while this field can be changed by another thread. Adds minimal annotations to avoid KCSAN splats for UDP. Following patches will add more annotations to potential lockless readers. BUG: KCSAN: data-race in __ip6_datagram_connect / udpv6_sendmsg write to 0xffff888136d47a94 of 4 bytes by task 7681 on cpu 0: __ip6_datagram_connect+0x6e2/0x930 net/ipv6/datagram.c:221 ip6_datagram_connect+0x2a/0x40 net/ipv6/datagram.c:272 inet_dgram_connect+0x107/0x190 net/ipv4/af_inet.c:576 __sys_connect_file net/socket.c:1900 [inline] __sys_connect+0x197/0x1b0 net/socket.c:1917 __do_sys_connect net/socket.c:1927 [inline] __se_sys_connect net/socket.c:1924 [inline] __x64_sys_connect+0x3d/0x50 net/socket.c:1924 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x2b/0x50 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffff888136d47a94 of 4 bytes by task 7670 on cpu 1: udpv6_sendmsg+0xc60/0x16e0 net/ipv6/udp.c:1436 inet6_sendmsg+0x5f/0x80 net/ipv6/af_inet6.c:652 sock_sendmsg_nosec net/socket.c:705 [inline] sock_sendmsg net/socket.c:725 [inline] ____sys_sendmsg+0x39a/0x510 net/socket.c:2413 ___sys_sendmsg net/socket.c:2467 [inline] __sys_sendmmsg+0x267/0x4c0 net/socket.c:2553 __do_sys_sendmmsg net/socket.c:2582 [inline] __se_sys_sendmmsg net/socket.c:2579 [inline] __x64_sys_sendmmsg+0x53/0x60 net/socket.c:2579 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x2b/0x50 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x00000000 -> 0xffffff9b Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 7670 Comm: syz-executor.3 Tainted: G W 5.18.0-rc1-syzkaller-dirty #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 I chose to not add Fixes: tag because race has minor consequences and stable teams busy enough. Signed-off-by: Eric Dumazet Reported-by: syzbot --- include/net/ip.h | 2 +- include/net/sock.h | 5 +++-- net/ipv6/datagram.c | 6 +++--- net/ipv6/udp.c | 11 ++++++----- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/net/ip.h b/include/net/ip.h index 3984f2c39c4ba8b4d2a4e4dab6d743f0c9faf798..8ad04f60b4132e163381b3a051c3f3d13e57c2ba 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -93,7 +93,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm, ipcm->sockc.mark = inet->sk.sk_mark; ipcm->sockc.tsflags = inet->sk.sk_tsflags; - ipcm->oif = inet->sk.sk_bound_dev_if; + ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if); ipcm->addr = inet->inet_saddr; } diff --git a/include/net/sock.h b/include/net/sock.h index 73063c88a2499b31c1e8d25dc157d21f93b02bf5..f6663e1d881d16014cc1439f7bb04f5d16b3db4f 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2878,13 +2878,14 @@ static inline void sk_pacing_shift_update(struct sock *sk, int val) */ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif) { + int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); int mdif; - if (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif) + if (!bound_dev_if || bound_dev_if == dif) return true; mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif); - if (mdif && mdif == sk->sk_bound_dev_if) + if (mdif && mdif == bound_dev_if) return true; return false; diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c index 39b2327edc4e993d5a74a03eae8986a9152dea2b..df665d4e8f0f130f1d65e368f1b495fed794b70a 100644 --- a/net/ipv6/datagram.c +++ b/net/ipv6/datagram.c @@ -218,11 +218,11 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, err = -EINVAL; goto out; } - sk->sk_bound_dev_if = usin->sin6_scope_id; + WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id); } if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) - sk->sk_bound_dev_if = np->mcast_oif; + WRITE_ONCE(sk->sk_bound_dev_if, np->mcast_oif); /* Connect to link-local address requires an interface */ if (!sk->sk_bound_dev_if) { @@ -798,7 +798,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, if (src_idx) { if (fl6->flowi6_oif && src_idx != fl6->flowi6_oif && - (sk->sk_bound_dev_if != fl6->flowi6_oif || + (READ_ONCE(sk->sk_bound_dev_if) != fl6->flowi6_oif || !sk_dev_equal_l3scope(sk, src_idx))) return -EINVAL; fl6->flowi6_oif = src_idx; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 3fc97d4621ac4a1f86de1f20375b33afffd0a2e6..960cfea820160614f3606ce4f407b7aa89fc70e1 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -105,7 +105,7 @@ static int compute_score(struct sock *sk, struct net *net, const struct in6_addr *daddr, unsigned short hnum, int dif, int sdif) { - int score; + int bound_dev_if, score; struct inet_sock *inet; bool dev_match; @@ -132,10 +132,11 @@ static int compute_score(struct sock *sk, struct net *net, score++; } - dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif); + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif); if (!dev_match) return -1; - if (sk->sk_bound_dev_if) + if (bound_dev_if) score++; if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) @@ -789,7 +790,7 @@ static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, (inet->inet_dport && inet->inet_dport != rmt_port) || (!ipv6_addr_any(&sk->sk_v6_daddr) && !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || - !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) || + !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) || (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) return false; @@ -1433,7 +1434,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) } if (!fl6->flowi6_oif) - fl6->flowi6_oif = sk->sk_bound_dev_if; + fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if); if (!fl6->flowi6_oif) fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; From patchwork Wed May 11 23:37:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846716 X-Patchwork-Delegate: kuba@kernel.org 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 599F7C433EF for ; Wed, 11 May 2022 23:38:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349386AbiEKXiL (ORCPT ); Wed, 11 May 2022 19:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349372AbiEKXiG (ORCPT ); Wed, 11 May 2022 19:38:06 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B98EE980B8 for ; Wed, 11 May 2022 16:38:05 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id 204so3284573pfx.3 for ; Wed, 11 May 2022 16:38:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yOgD8n5meg7ofEW5JqI3SftRDBRm5coeT04EbI5g5IQ=; b=Rq0T+l3sO2dSmDhqKahCuq5GYyNU92IlSrKnI2GtCSjd+KvR4WgRz7Eej1ZaGw7OL2 wSDYnZVm7ydKP54H96aRDMYyNAzzEoCXqyLw4vlNMqQveCPNpapEvm0xr95Z9btGXGqg dMUSs+G2qCok2IOfATdtnclxGem/Wj5ptedMwP7Ryqwpzzn8eBcDppHU3nf1qxBGffvz dXE70e+HFv8X3JLmU+lULAi47vnuhaVVStCDwWI4truR8eYdZZRnTXo8rq0c6AVcJLZr Qye680UUW80llTjf5taitbs2UFSCqCvjAOEMFgyAb9askkmfuCRIQAhsQzL3Hvreoknq cUUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yOgD8n5meg7ofEW5JqI3SftRDBRm5coeT04EbI5g5IQ=; b=2zQx8sN9S6yXuTi4BF0wb0jpdX5wFgD23SMHQm9P8JK7fko2ZruNSG9cnHV2hHSbT3 N4yBqX+wfXNbSoeDm3iaFv0dn2SCPh4WuZZiS9cWQGPwYmSdp21TFBREbuUpR9zZPrLc cJKF2BcRksmS0cKIrm1GRO+/mtlzMneRUN/AasJajLkkXqhLaWx7L8PuhiCNeiom2B6W 51NSAN5nXzelI4J9H+N8S4WPPZ1LFbHzebg3LfhhDpr5R4jfBrb1Z52LDD4V0eUEKpsC fS3Qf//H8KeP5HEgBUY8EjAKww7KybT6Z0kPDojcvPflYZ7B4wzILKb5TSOK4nPLJcdG tKsg== X-Gm-Message-State: AOAM530u9rGKqpZ6gOR1BFI9X0sS4OlIyAdTFIOjFJuVGqSTJwKmcpeA uzsZJM3oMdCMW+106t45SO4= X-Google-Smtp-Source: ABdhPJzz41DjgBULgr5CrIiHY379idlSTdKnbzcuZOoec8mFH5iFtzhINPjBn3slm0FgVMeW0qTRRg== X-Received: by 2002:a05:6a00:450e:b0:510:88c8:7d33 with SMTP id cw14-20020a056a00450e00b0051088c87d33mr24901377pfb.27.1652312285210; Wed, 11 May 2022 16:38:05 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:04 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 02/10] sctp: read sk->sk_bound_dev_if once in sctp_rcv() Date: Wed, 11 May 2022 16:37:49 -0700 Message-Id: <20220511233757.2001218-3-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet sctp_rcv() reads sk->sk_bound_dev_if twice while the socket is not locked. Another cpu could change this field under us. Signed-off-by: Eric Dumazet --- net/sctp/input.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/sctp/input.c b/net/sctp/input.c index 90e12bafdd4894624b3a63d625ed21c85b60a4f0..4f43afa8678f9febf2f02c2ce1a840ce3ab6a07d 100644 --- a/net/sctp/input.c +++ b/net/sctp/input.c @@ -92,6 +92,7 @@ int sctp_rcv(struct sk_buff *skb) struct sctp_chunk *chunk; union sctp_addr src; union sctp_addr dest; + int bound_dev_if; int family; struct sctp_af *af; struct net *net = dev_net(skb->dev); @@ -169,7 +170,8 @@ int sctp_rcv(struct sk_buff *skb) * If a frame arrives on an interface and the receiving socket is * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB */ - if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) { + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + if (bound_dev_if && (bound_dev_if != af->skb_iif(skb))) { if (transport) { sctp_transport_put(transport); asoc = NULL; From patchwork Wed May 11 23:37:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846717 X-Patchwork-Delegate: kuba@kernel.org 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 36EE9C433EF for ; Wed, 11 May 2022 23:38:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349396AbiEKXiP (ORCPT ); Wed, 11 May 2022 19:38:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349380AbiEKXiK (ORCPT ); Wed, 11 May 2022 19:38:10 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E070BCE83 for ; Wed, 11 May 2022 16:38:07 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id q18so3288382pln.12 for ; Wed, 11 May 2022 16:38:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wXdqDbMn3niNRtOSNFnZfRQzdIcOYSmVNknYpbOe3iY=; b=MBYEPTxIiKg6sBUWoUWeZbuoWw6uR3uBMxfOIutZNGbPtGKGZ/7u5e3GAL73kcvWkf uEL1p/xI0Nv+EBu7wiZZJwsALHkwNwNjPPPp+/HlPiYrOB1CBITlkvXH/ozdbWOBZq5t 5b/YTr8FMzEJwEOh3ERY2R3ekO2/kVzxD7DVY/WhMgbt+8J0ydUiz2MXMUYKx+2UZKpS yw/qX0bcP+08Al1g0lUus5Pup2ZEmpSZqvUfhYWOEf2SUhQX8wOiKWN7CPAbI0y7v54L I4b9zHm9JcFLfyq01g4ZcALf8FaGfYKN9ocvI0P3N0LStfDXIq4TxweIKOpC60WbcGEc 36pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wXdqDbMn3niNRtOSNFnZfRQzdIcOYSmVNknYpbOe3iY=; b=l8QgEw+NqLjHKVvc6Ki7BY25mvrZgK75y0BIQlQX6X7UO1YwVXTbyOBLYPsIcNY2T9 /7DcaxdwRUp9etFZJ0XfFVhjAqA4srqoS1Kzx5IYFiNan0CyB2sgmWri7arhM9fOKmix iRJXDD+61OngOXVXEpqYgN7wpEbBaefyIA57SCtCo1LFMQojaiyaW/lrhoT1R7vArxKV shLUVDVHBLqMcY1MGTJfrBF6FYdWg9T70O6KefSqyZbyB5sOBACiFVbzyAMlfE7yQGEo A4l9puErp1MPHVmTy96Urw5nrqoWaF8oJyew3r/FlNnnTrbdtE6jsg8sQG1KKI5axQvp QOeg== X-Gm-Message-State: AOAM5320K2HM3uW6IHDyMcNx6YfYq4MfmQX9zVBk3EqUwRuuhXdVV37c EmTO17xi516A/7wTMiInqNo/fyQSlkQ= X-Google-Smtp-Source: ABdhPJzxvkb6xw1xV9j6UVfi1+2s9WczjzyaBmZHLwr83/dWxFVSF/STS1cECJd97CpxynbRnxJRmg== X-Received: by 2002:a17:902:ea06:b0:15e:d3a0:d443 with SMTP id s6-20020a170902ea0600b0015ed3a0d443mr27788412plg.10.1652312286876; Wed, 11 May 2022 16:38:06 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:06 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 03/10] tcp: sk->sk_bound_dev_if once in inet_request_bound_dev_if() Date: Wed, 11 May 2022 16:37:50 -0700 Message-Id: <20220511233757.2001218-4-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet inet_request_bound_dev_if() reads sk->sk_bound_dev_if twice while listener socket is not locked. Another cpu could change this field under us. Signed-off-by: Eric Dumazet --- include/net/inet_sock.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h index 234d70ae5f4cbdc3b2caaf10df81495439a101a5..c1b5dcd6597c622dfea3d4a646f97c87acb7ea32 100644 --- a/include/net/inet_sock.h +++ b/include/net/inet_sock.h @@ -116,14 +116,15 @@ static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb) static inline int inet_request_bound_dev_if(const struct sock *sk, struct sk_buff *skb) { + int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); #ifdef CONFIG_NET_L3_MASTER_DEV struct net *net = sock_net(sk); - if (!sk->sk_bound_dev_if && net->ipv4.sysctl_tcp_l3mdev_accept) + if (!bound_dev_if && net->ipv4.sysctl_tcp_l3mdev_accept) return l3mdev_master_ifindex_by_index(net, skb->skb_iif); #endif - return sk->sk_bound_dev_if; + return bound_dev_if; } static inline int inet_sk_bound_l3mdev(const struct sock *sk) From patchwork Wed May 11 23:37:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846718 X-Patchwork-Delegate: kuba@kernel.org 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 0604FC433F5 for ; Wed, 11 May 2022 23:38:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349397AbiEKXiQ (ORCPT ); Wed, 11 May 2022 19:38:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349382AbiEKXiK (ORCPT ); Wed, 11 May 2022 19:38:10 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE8726D951 for ; Wed, 11 May 2022 16:38:08 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id e24so3625490pjt.2 for ; Wed, 11 May 2022 16:38:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Kd9mGCouSC4r1agU0tm4NQVZAdOkhAaqRidEJfSBRdw=; b=F6Qp3ctXJB3s4iaTMP7gcgFyX20A/Q1v9s7uIiFZwKL+VsmztqyFgE7akLHyxZHUvi Ljv1e+GgPKZksorNFYfa93kia63w0Vn6Bt3CPdXBnn9K38pA9KkCTwA00W2LgX2pxvLB q+kOpoK9AbIe2VxPbbtDgxmXrxhqnhDF1rPBxKoRTL43livEhhijtGHbO14LfZXHOdbm HtKgq2ugaTi4Js9tehvA51ySOltemmwxVwa+T9so8j4vRl6cNmz69Q80oPLg9z/6Zbgk EpjgM5A3RdXqw4JzD2xan0nY6lzJJoyWnPXFc8/I4B9II2qasp3dJcNIdHgliwkj7XO+ ZpXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Kd9mGCouSC4r1agU0tm4NQVZAdOkhAaqRidEJfSBRdw=; b=sZoN+10RCSRwQswV7L49xh3FA0Z0toIsNvi26ki6162mOt9Oi5TvNR+UqY9El/wF0f /1hazbu2QkNxwA2UTLfHWOFwlZjzOBSiP9Tw5MhtkxDoB/K+BQLdNi8a7h5tt2yWsBXf uk0HnoYQ8Kc6nMcjjer86jmPWu7n5FWh2FeZNzEQpAB8AIBCEaeGrU8vdcO6wgG1DEdy vWWuw9l06S6Bh0Kch2oWEnO6RcKRnBpZXbRvHuGia8Btnx+2s6fsKGDbCFvELHlPnFzA 2DDKks4iWwGVT6qLJdQP4iagVsW+GwIfibbsZqeMq1osehoxU4n5pqZnakzutHl/Hz4V xbzg== X-Gm-Message-State: AOAM5337cw5XVycTnTP/e+w9IAT3jkFyzM8QCWt1zhkqnkCUix56YUEY PAcc3rMRTarB1seNUkLAMZ4= X-Google-Smtp-Source: ABdhPJxwRxwsrLKVlamUtlSdglWxycRtv9CK+6eFR7eyPFrE+kl9IZYRmM6KWCjDfs/HoG/CcQ3Fmg== X-Received: by 2002:a17:902:7783:b0:15f:21f8:a1d6 with SMTP id o3-20020a170902778300b0015f21f8a1d6mr11233956pll.66.1652312288307; Wed, 11 May 2022 16:38:08 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:08 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 04/10] net: core: add READ_ONCE/WRITE_ONCE annotations for sk->sk_bound_dev_if Date: Wed, 11 May 2022 16:37:51 -0700 Message-Id: <20220511233757.2001218-5-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet sock_bindtoindex_locked() needs to use WRITE_ONCE(sk->sk_bound_dev_if, val), because other cpus/threads might locklessly read this field. sock_getbindtodevice(), sock_getsockopt() need READ_ONCE() because they run without socket lock held. Signed-off-by: Eric Dumazet --- net/core/sock.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 6b287eb5427b32865d25fc22122fefeff3a4ccf5..2500f9989117441a67ce2c457af25bf8f780b110 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -635,7 +635,9 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex) if (ifindex < 0) goto out; - sk->sk_bound_dev_if = ifindex; + /* Paired with all READ_ONCE() done locklessly. */ + WRITE_ONCE(sk->sk_bound_dev_if, ifindex); + if (sk->sk_prot->rehash) sk->sk_prot->rehash(sk); sk_dst_reset(sk); @@ -713,10 +715,11 @@ static int sock_getbindtodevice(struct sock *sk, char __user *optval, { int ret = -ENOPROTOOPT; #ifdef CONFIG_NETDEVICES + int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); struct net *net = sock_net(sk); char devname[IFNAMSIZ]; - if (sk->sk_bound_dev_if == 0) { + if (bound_dev_if == 0) { len = 0; goto zero; } @@ -725,7 +728,7 @@ static int sock_getbindtodevice(struct sock *sk, char __user *optval, if (len < IFNAMSIZ) goto out; - ret = netdev_get_name(net, devname, sk->sk_bound_dev_if); + ret = netdev_get_name(net, devname, bound_dev_if); if (ret) goto out; @@ -1861,7 +1864,7 @@ int sock_getsockopt(struct socket *sock, int level, int optname, break; case SO_BINDTOIFINDEX: - v.val = sk->sk_bound_dev_if; + v.val = READ_ONCE(sk->sk_bound_dev_if); break; case SO_NETNS_COOKIE: From patchwork Wed May 11 23:37:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846719 X-Patchwork-Delegate: kuba@kernel.org 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 31B56C433F5 for ; Wed, 11 May 2022 23:38:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349408AbiEKXiU (ORCPT ); Wed, 11 May 2022 19:38:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349389AbiEKXiL (ORCPT ); Wed, 11 May 2022 19:38:11 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F34A62237 for ; Wed, 11 May 2022 16:38:10 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id x88so3629388pjj.1 for ; Wed, 11 May 2022 16:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YUlv7qqnItbyYlHDMUTX5gH4mFMMzUBtaLmUQsed56k=; b=UGTZbbMJXcPaxFHQBmYVNQzRdD3gR4ksiOZqmSlsvHXf5IUywN8KrpyvGQRBhMFwK9 GvWgrOzweLWXGA7A7t0IHj9ac8VvfMG148bDnII5llF8YBm4QDAKFdQuGA8fbpW68GB0 zADIzE3L4TOm43NxKl1jdO6ZnZZmZHtBOyopQPczOXBSzqGRRup6HNZofe+/OeX15mZW k9c6TAi2QastbMVpz0YYZkNGBikhRo2uYHF5kmQSgH/uns4+GwNEofGI6hk7qfKIXob9 VupDHPesAINpSRC3Wci60ocnK/29jk1NU8R6Uz2qwdWuclPeyAsqQFRV44I4noOlfZmB SiKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YUlv7qqnItbyYlHDMUTX5gH4mFMMzUBtaLmUQsed56k=; b=fuBrAnHGqsgmkBBoXBIyDDdCCZRIV8zZGgEyawrN+gS9SWmp+lT0pDBU9XD9bRPKdc d+Xv1HXyCwh6Z5dRptEUn1VCOdvcQlKftWRTzika+q81+I940A3sMKUPBygjWbRFN8iV q4UHArnEyf9iaOqrM/n+DbzboGnwMuoT8fi9QNCkmHftqZF0tdtsVDC9wpqAT+bIqDax w5Ii59iE4HGbLJvr7Nbi6lQWsiOXevEn9xdQu4OuLABNqUYMPjsVNE88AqCBwGDRKUWQ MIJTeV/Xfm0AS9sHxlKzJWxqM3wGzr91Re1D/BTaIBKN9/4T7op7WMTN01vXgwKtAwjw PzDA== X-Gm-Message-State: AOAM530iuN8V/AS31EDGWiD+dUsH+iZBanDFcWf6St4WJk94hOx8mvSW hJrOikEyAuP+B+v5kKcrACocsAe5UmU= X-Google-Smtp-Source: ABdhPJzvst2j6DC3upxO2uPYJsZAms3HOiyNrPsPYrpH69tKkt/nLRTc8gsxDvcB4wcZ+LpUCaeemA== X-Received: by 2002:a17:902:eb90:b0:15e:b55f:d9c5 with SMTP id q16-20020a170902eb9000b0015eb55fd9c5mr27781405plg.33.1652312289893; Wed, 11 May 2022 16:38:09 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:09 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 05/10] dccp: use READ_ONCE() to read sk->sk_bound_dev_if Date: Wed, 11 May 2022 16:37:52 -0700 Message-Id: <20220511233757.2001218-6-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet When reading listener sk->sk_bound_dev_if locklessly, we must use READ_ONCE(). Signed-off-by: Eric Dumazet --- net/dccp/ipv4.c | 2 +- net/dccp/ipv6.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c index 82696ab86f74fd61aae5f60a3e14e769fb21abf9..3074248721607541a707d2e27dc0dfb9ff68463f 100644 --- a/net/dccp/ipv4.c +++ b/net/dccp/ipv4.c @@ -628,7 +628,7 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb) sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr); ireq->ir_mark = inet_request_mark(sk, skb); ireq->ireq_family = AF_INET; - ireq->ir_iif = sk->sk_bound_dev_if; + ireq->ir_iif = READ_ONCE(sk->sk_bound_dev_if); /* * Step 3: Process LISTEN state diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c index 4d95b6400915db56e1058099e6d7015d2d64647e..d717ef0def64a9f3321fc53107f421b70a21bd16 100644 --- a/net/dccp/ipv6.c +++ b/net/dccp/ipv6.c @@ -374,10 +374,10 @@ static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb) refcount_inc(&skb->users); ireq->pktopts = skb; } - ireq->ir_iif = sk->sk_bound_dev_if; + ireq->ir_iif = READ_ONCE(sk->sk_bound_dev_if); /* So that link locals have meaning */ - if (!sk->sk_bound_dev_if && + if (!ireq->ir_iif && ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL) ireq->ir_iif = inet6_iif(skb); From patchwork Wed May 11 23:37:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846720 X-Patchwork-Delegate: kuba@kernel.org 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 4E84BC433F5 for ; Wed, 11 May 2022 23:38:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349401AbiEKXiX (ORCPT ); Wed, 11 May 2022 19:38:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349398AbiEKXiT (ORCPT ); Wed, 11 May 2022 19:38:19 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB559179435 for ; Wed, 11 May 2022 16:38:11 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 7so3070283pga.12 for ; Wed, 11 May 2022 16:38:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qvDoj7L5lsRvMJV9dNuhFJcDkWTXkUCEAxq9FkhBxoE=; b=dLqqLlbpP1BUjwQbwxlxD0tertYzS9jU7ovSYsdKpPUzQ2XZQlHPwueeY1I8mUAUjr hpT3LCoXM0fnwNklUnxhE6wZiBX7zmPgrJZ3rrjWIF02L820WO7/mQ/Yuko9qIvNamvb DuZnUUkkdPws1aSg7zsEs59cUkGprlFxLkmMoHoMSCTJAlBt2r9vGLSERMAZtuOLgmYv AUcB1HGNP/D3KnI+ziiuJuYpdNZPbXXoxW1hKDL5jp7jmcP9PFgYUzDiOO9IGjnmdCqx M8DEpimTWmFSl0p2/oPVcOz2bXagv9+05e8sqDGYuCfqn5b2Mys3bC/FaWro7bugmi79 mMXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qvDoj7L5lsRvMJV9dNuhFJcDkWTXkUCEAxq9FkhBxoE=; b=3UdY+I3765VJqOuVYo0Q5ms7jw9xz5JJZkuwj4iVYxzzK+ha1E6K8PaTV89JePutBO z5TaTGxRfAViDGjI3AOVr5TwMeApRXrpB2rNFBL1Faiq2AKG8h/3ZUOVtK/VCzxPCdG2 bIHtqj9VGw8e2GW8X+VmTHzJXclMqaeaF+TgqmIYWy+Kj2QXcya7LhOQUxYNFZYnKzm+ 8SPzg0UiQQkJkd5un4vF/7FBQFWGKDTcmL0cmVaTFcTK/2ejCbtwKTNobsy4I66jam29 w2oaiNeixx4SzG8+k+V3WN8Hm2u5Yg2nynpMK20/c6oO4bqMbXd3oaXih3TpPuT9GTZH o6Xw== X-Gm-Message-State: AOAM530hMMbBZ2eH5B6DTmeOEDu9s9es2zaD8oVmSzLezsgwaKbRlQSy H10YyOQIBIIxPYTPMO0YiCk= X-Google-Smtp-Source: ABdhPJyls1WrIhsg4wNuvKRY1B2g59jXxiYvClGLKiGTxNGbRZ/RusrST/Vrmbr+1R3jxQo4q8778Q== X-Received: by 2002:a05:6a00:4197:b0:510:671d:709c with SMTP id ca23-20020a056a00419700b00510671d709cmr26948854pfb.61.1652312291242; Wed, 11 May 2022 16:38:11 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:10 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 06/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in inet_csk_bind_conflict() Date: Wed, 11 May 2022 16:37:53 -0700 Message-Id: <20220511233757.2001218-7-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet inet_csk_bind_conflict() can access sk->sk_bound_dev_if for unlocked sockets. Signed-off-by: Eric Dumazet --- net/ipv4/inet_connection_sock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index 1e5b53c2bb2670fc90b789e853458f5c86a00c27..53f5f956d9485df5cb863c8287c1fa9989bb29c9 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -155,10 +155,14 @@ static int inet_csk_bind_conflict(const struct sock *sk, */ sk_for_each_bound(sk2, &tb->owners) { - if (sk != sk2 && - (!sk->sk_bound_dev_if || - !sk2->sk_bound_dev_if || - sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { + int bound_dev_if2; + + if (sk == sk2) + continue; + bound_dev_if2 = READ_ONCE(sk2->sk_bound_dev_if); + if ((!sk->sk_bound_dev_if || + !bound_dev_if2 || + sk->sk_bound_dev_if == bound_dev_if2)) { if (reuse && sk2->sk_reuse && sk2->sk_state != TCP_LISTEN) { if ((!relax || From patchwork Wed May 11 23:37:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846721 X-Patchwork-Delegate: kuba@kernel.org 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 05F1DC433EF for ; Wed, 11 May 2022 23:38:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349402AbiEKXiY (ORCPT ); Wed, 11 May 2022 19:38:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349387AbiEKXiT (ORCPT ); Wed, 11 May 2022 19:38:19 -0400 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2995D19949C for ; Wed, 11 May 2022 16:38:13 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id x12so3077260pgj.7 for ; Wed, 11 May 2022 16:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=U26yQ/QwZQZC3YgBNZ7hODbV1h7xyPcCap1q8PQwiGo=; b=A+o/dBS6NwTMEfLGSDGh7XeKMjlpL/Zf5rXZjcgD7JIUbwpfexiUaj+NYw+iM75vuU iAHjg47DBAlR6bt5N6L92SvXO7JvHLNUp3bmvdiOO5o29fkYv2+Ry4G0P7rbJHiXV/yB KAuQBDLSQkazIY0S5V1A/Axt3NJUnnwV4n6zq7lDTs/zFPp77jQ2HExNkWvaX68YR3IK a2yk3dAYGlp/nK+P7AdVRwjZVpZPq+5V1XsCjutJYXzvcThTXr04qeNBwU67H+kUzUqs kpwGlhLccMS3JAEQp9iXyIFjIlbYCt8u8dEkZyIfG+x9qX0/qO1dP3so4369GX3XEGkF FJZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=U26yQ/QwZQZC3YgBNZ7hODbV1h7xyPcCap1q8PQwiGo=; b=l2JeYA+JfuWfCTibFLov0AVeOK2eoi0LVMLAJW5UTUUy/k2ZLcvsisclNPU66WHpp+ S0v+yuLVK174Uk8eYgF1ujdIO/w4oi609eG+z35JYu+QUGlqs2VkWTLT4NzuD0r8dQLx g8IQokqVVkWd9ni1JOfCLt7JpZKKSGGOHti+JNxj8Z7FmwMWepGi/+fd5+OYvZXbt2fM Z2mBDUyM+sKCXViQcMonbwMs/5+Fl878VWdnqZEQoHV3uG5jOH+1V+M91Qto2dK9VRt6 yFFfvR2Pem0RRP9bI0qOYZKW6pQ/brBpu3+EBq/fcv/iaaTWN/DYgJ0XM4H7hS21YZMM 1xZw== X-Gm-Message-State: AOAM531sFyh6KuKq3HDib/BFt7uEB0uXsIitGzwRQd6DdomF2nbrN8M2 GByABmmJJWKfrIdQdWVR1Dw= X-Google-Smtp-Source: ABdhPJzY5D6Q5YYLuWh+Vx3wTKOJ688KSrzL0sHyEGu0+YDA/FQD5oEuSl40Ll25ozWC01IvIZ9PfQ== X-Received: by 2002:a63:1c5:0:b0:39c:c779:b480 with SMTP id 188-20020a6301c5000000b0039cc779b480mr22394235pgb.311.1652312292582; Wed, 11 May 2022 16:38:12 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:12 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 07/10] net_sched: em_meta: add READ_ONCE() in var_sk_bound_if() Date: Wed, 11 May 2022 16:37:54 -0700 Message-Id: <20220511233757.2001218-8-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet sk->sk_bound_dev_if can change under us, use READ_ONCE() annotation. Signed-off-by: Eric Dumazet --- net/sched/em_meta.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/sched/em_meta.c b/net/sched/em_meta.c index 0a04468b73145546097788db0645ef9e5b459bca..49bae3d5006b0f83330b4cbe30344c0741743575 100644 --- a/net/sched/em_meta.c +++ b/net/sched/em_meta.c @@ -311,12 +311,15 @@ META_COLLECTOR(int_sk_bound_if) META_COLLECTOR(var_sk_bound_if) { + int bound_dev_if; + if (skip_nonlocal(skb)) { *err = -1; return; } - if (skb->sk->sk_bound_dev_if == 0) { + bound_dev_if = READ_ONCE(skb->sk->sk_bound_dev_if); + if (bound_dev_if == 0) { dst->value = (unsigned long) "any"; dst->len = 3; } else { @@ -324,7 +327,7 @@ META_COLLECTOR(var_sk_bound_if) rcu_read_lock(); dev = dev_get_by_index_rcu(sock_net(skb->sk), - skb->sk->sk_bound_dev_if); + bound_dev_if); *err = var_dev(dev, dst); rcu_read_unlock(); } From patchwork Wed May 11 23:37:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846723 X-Patchwork-Delegate: kuba@kernel.org 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 6406AC433EF for ; Wed, 11 May 2022 23:38:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349418AbiEKXid (ORCPT ); Wed, 11 May 2022 19:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349404AbiEKXiT (ORCPT ); Wed, 11 May 2022 19:38:19 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 694441ACF9A for ; Wed, 11 May 2022 16:38:14 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id c14so3288224pfn.2 for ; Wed, 11 May 2022 16:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nLFHvhNFhb/qf+DZNUwjAIpYvQd26ySARtfAXo7uBqs=; b=YnZz3kNr5IfqtIN4R7ERGxXS4xetzkqoxBYD+f+qUWNw5xeeMpEaBDreoJLQqJIxRF ez9fmk/OljTot/KODWr2FMyJA5D7K2oGi46CygrocUuHZFk7b18o1lxa5DhVxNfM/a1f rlIXfpiA/vtHemhF6w2ILLbMWv7hgzxWls2s9rgK/ULfOreHUmFPBsOHywHBRrg44XhI tOppDrdi5iQAz4mY5SHUD6GuKZGiaJDtJi4ZGoMzYzftbWnQCbLAlbofPmeAYkLd1TEA eEJF7IIkrt2Cpm7s0hvyPRrNPBO/JGaImBT2SD1SQW4JslH/CK93gygB1EUmDdGnw4A0 Fcmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nLFHvhNFhb/qf+DZNUwjAIpYvQd26ySARtfAXo7uBqs=; b=yTzHwTAo1pB6uSfWIMNVbouQfwwGQnb2T6NmYrDoHAsuKOKQEvlhrAs0HmJNvraZsq 7GfCD4we4eMr8Bq7JHu6Lx7rqcEmMjhkqYGPN2WO1v/BZvbUQxMjVgQNTGWKD9HtoxL3 SmDUhW+9OxdYFEZB/npJkHMet1OJgrslidXsPycwWp1ZuOnYRp+RlnaFOFvMYdT2QpGU cGTlvLmaficSn4PxsWrSe8zh3Yx5+tV3N/XMlS13GtZwJ/JBIRd4kLnUGTdRzpYVFyXZ usEetmF0S/oATFkUDj1PD+kmCbmLDW2mKQrO4GYveh6OSq7y72TbNIhFzIZSPORyFGlm ieAQ== X-Gm-Message-State: AOAM533QMmvTvktmYMu9Gu0m1N/iKtGOXXsI3UAlrE5bsFURd3LROXbO 7i9Jj6Zvzhm1nTL2f8v4jSLhVCM5C0g= X-Google-Smtp-Source: ABdhPJwMIxl1OHsh8v7HpLw46u3MqNK321FJsOSIlWXjM9HFxn4UsY2thCyTIuh8iXaWYN5ZYoq0xg== X-Received: by 2002:a63:8ac7:0:b0:3aa:f853:4f62 with SMTP id y190-20020a638ac7000000b003aaf8534f62mr23079436pgd.205.1652312294012; Wed, 11 May 2022 16:38:14 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:13 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 08/10] l2tp: use add READ_ONCE() to fetch sk->sk_bound_dev_if Date: Wed, 11 May 2022 16:37:55 -0700 Message-Id: <20220511233757.2001218-9-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet Use READ_ONCE() in paths not holding the socket lock. Signed-off-by: Eric Dumazet --- net/l2tp/l2tp_ip.c | 4 +++- net/l2tp/l2tp_ip6.c | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c index 6af09e188e52cfd84defb42ad34aea25f66f1e25..4db5a554bdbd9e80eb697a88cb7208e15d7931bc 100644 --- a/net/l2tp/l2tp_ip.c +++ b/net/l2tp/l2tp_ip.c @@ -50,11 +50,13 @@ static struct sock *__l2tp_ip_bind_lookup(const struct net *net, __be32 laddr, sk_for_each_bound(sk, &l2tp_ip_bind_table) { const struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk); const struct inet_sock *inet = inet_sk(sk); + int bound_dev_if; if (!net_eq(sock_net(sk), net)) continue; - if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif) + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + if (bound_dev_if && dif && bound_dev_if != dif) continue; if (inet->inet_rcv_saddr && laddr && diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c index 217c7192691e160e9727afd78126006aba3736b4..c6ff8bf9b55f916e80380bb2e4ea81b11e544a32 100644 --- a/net/l2tp/l2tp_ip6.c +++ b/net/l2tp/l2tp_ip6.c @@ -62,11 +62,13 @@ static struct sock *__l2tp_ip6_bind_lookup(const struct net *net, const struct in6_addr *sk_laddr = inet6_rcv_saddr(sk); const struct in6_addr *sk_raddr = &sk->sk_v6_daddr; const struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk); + int bound_dev_if; if (!net_eq(sock_net(sk), net)) continue; - if (sk->sk_bound_dev_if && dif && sk->sk_bound_dev_if != dif) + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + if (bound_dev_if && dif && bound_dev_if != dif) continue; if (sk_laddr && !ipv6_addr_any(sk_laddr) && @@ -445,7 +447,7 @@ static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr, lsa->l2tp_conn_id = lsk->conn_id; } if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL) - lsa->l2tp_scope_id = sk->sk_bound_dev_if; + lsa->l2tp_scope_id = READ_ONCE(sk->sk_bound_dev_if); return sizeof(*lsa); } @@ -560,7 +562,7 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) } if (fl6.flowi6_oif == 0) - fl6.flowi6_oif = sk->sk_bound_dev_if; + fl6.flowi6_oif = READ_ONCE(sk->sk_bound_dev_if); if (msg->msg_controllen) { opt = &opt_space; From patchwork Wed May 11 23:37:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846722 X-Patchwork-Delegate: kuba@kernel.org 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 8CAADC433FE for ; Wed, 11 May 2022 23:38:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349417AbiEKXib (ORCPT ); Wed, 11 May 2022 19:38:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349405AbiEKXiT (ORCPT ); Wed, 11 May 2022 19:38:19 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9DEA17706D for ; Wed, 11 May 2022 16:38:15 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d17so3355389plg.0 for ; Wed, 11 May 2022 16:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xts06NB1ydWBfIKgI/h7q/I8j+UuwUGTyL4OWnYAlUU=; b=Muoa8ue2s3tG1ePujmHjMRZcU+zPwJEPPCnTbpLrOPfRkCPxGxhT5NYddWoGaVOd6O T9I2htPp/G2CGZGBs+wVoxHN6kKnauHX8ICa8W7N8DL7oapZQ67tpUy2ZzDj//RrMMY+ wVKryoYTpn88aTXO6RlfmehWxfSPP7JSw0mdRM7T96plekKSdryFC3nhZhLCnl28kn7V 1Vsb0f8MxrKu4yCgjp1rZsQQjEMaiddMypXRR/m6bSs4sEkXR8jUeOjMJ3iRiCJgfyIF KS7ye1g6hsPKF6B3JRhv27Th4cKnL/DKDQIqIorW8Mu/Ob8lFNjry/kutiDxZ9/GM2KS qQTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Xts06NB1ydWBfIKgI/h7q/I8j+UuwUGTyL4OWnYAlUU=; b=qMI/5rT5Fjwvl/bsMCir3S44jyoYq45Gwm5IN86cPSyFbe/4USjZYG3+Ii4iPhWULz z91x2rvSNE7WBEY9WihefzZJo9ObdVF9OW3k/X6CaTcJKv7f0/LKcVeEuYykhb1tlPOq hFU24Z8x9KjcHSSgVFQf1MPr2jpPfvrssP4v3TeCnFXA5hCRJUncu+Nv82M3dGFIgWOn mtjPzFmFDMWn4DKMrGbRuapJ5zQHSybDAddcbf9M3iwaX84kc/zzLONtNJUokKDCqxc8 0Lle1EF3mjOxVwnGvXQIyXsNXRxFVZCoT3rGT1eSDqty1MzpwI2pqw975P7ySLw2Kv07 k0qg== X-Gm-Message-State: AOAM531uRkqaHj+GrGa5kTxfG6ihZzIs8MG5yERmiqKh4qb0wrAhkazB PIOzTlK1Bq+ZzWCreElxEqQ= X-Google-Smtp-Source: ABdhPJw7ja6m1iChG+BqzHfhuVqYXe+VfMqCwMoN6+l8hrx8foinBfAxLWqPr42w6jIyhPbzndUCEA== X-Received: by 2002:a17:90b:3944:b0:1dc:1ddb:e25c with SMTP id oe4-20020a17090b394400b001dc1ddbe25cmr7721942pjb.124.1652312295363; Wed, 11 May 2022 16:38:15 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:15 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 09/10] ipv6: add READ_ONCE(sk->sk_bound_dev_if) in INET6_MATCH() Date: Wed, 11 May 2022 16:37:56 -0700 Message-Id: <20220511233757.2001218-10-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet INET6_MATCH() runs without holding a lock on the socket. We probably need to annotate most reads. This patch makes INET6_MATCH() an inline function to ease our changes. Signed-off-by: Eric Dumazet Reported-by: kernel test robot Reported-by: kernel test robot --- include/net/inet6_hashtables.h | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h index 81b96595303680dee9c43f8c64d97b71fb4c4977..a7523e2e7ce50564c3ef1b3563a55dc80a03927d 100644 --- a/include/net/inet6_hashtables.h +++ b/include/net/inet6_hashtables.h @@ -105,13 +105,22 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, int inet6_hash(struct sock *sk); #endif /* IS_ENABLED(CONFIG_IPV6) */ -#define INET6_MATCH(__sk, __net, __saddr, __daddr, __ports, __dif, __sdif) \ - (((__sk)->sk_portpair == (__ports)) && \ - ((__sk)->sk_family == AF_INET6) && \ - ipv6_addr_equal(&(__sk)->sk_v6_daddr, (__saddr)) && \ - ipv6_addr_equal(&(__sk)->sk_v6_rcv_saddr, (__daddr)) && \ - (((__sk)->sk_bound_dev_if == (__dif)) || \ - ((__sk)->sk_bound_dev_if == (__sdif))) && \ - net_eq(sock_net(__sk), (__net))) +static inline bool INET6_MATCH(const struct sock *sk, struct net *net, + const struct in6_addr *saddr, + const struct in6_addr *daddr, + const __portpair ports, + const int dif, const int sdif) +{ + int bound_dev_if; + if (!net_eq(sock_net(sk), net) || + sk->sk_family != AF_INET6 || + sk->sk_portpair != ports || + !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) || + !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) + return false; + + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + return bound_dev_if == dif || bound_dev_if == sdif; +} #endif /* _INET6_HASHTABLES_H */ From patchwork Wed May 11 23:37:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12846724 X-Patchwork-Delegate: kuba@kernel.org 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 31B89C433EF for ; Wed, 11 May 2022 23:38:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349406AbiEKXil (ORCPT ); Wed, 11 May 2022 19:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349399AbiEKXiU (ORCPT ); Wed, 11 May 2022 19:38:20 -0400 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3A9F6D951 for ; Wed, 11 May 2022 16:38:17 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id l11so3064824pgt.13 for ; Wed, 11 May 2022 16:38:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6hoL+dAnDcEwYoBSkV/u3wIt5TDKEu6TFdOf6hMtRsE=; b=Bs3LQuc0bLHWNsB6qWe7uoRnhBQWFfW5gFzWO4JewKDxDBF52J7d/qAxjWlbXzklt0 88/NCSDiL/7AEfVRMhVUorbTiCvyaZ/oFMKyr8Dcfk2Ox7/1JoVKUPnlVZX9ZUj029Mi PipL0vrNrXLE8G3QsPy9j4RG+zmg8DKbcZYCuZ9MHKQF2aye7fcPBFLcoVVdh3U15rRs HMauzII+dqtLXzn3MYEXLlDRK9Ze3C3wsoebxnhr/2SNAIcNT1IU2r6Kwzl7YEB4xHFx z5EN/eQtsSvp6RyA5Rg9CHAehfvv0hrLM5EehebyQKp2pzoUd7xiqSW3iQ7w0f8K/uhl 1ssQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6hoL+dAnDcEwYoBSkV/u3wIt5TDKEu6TFdOf6hMtRsE=; b=K8FAHFptYLwmYhXtJrGHG0FY0vQMZ22Qq78lQIgFUBftvLAlqwAIaGaZE0f4MMuVvE ke/wc0DoTP0tp6YlxFUePGbtf9wP6DCEmZBS+PFhLq8nNDNI5SXVgyy5q3schmL/aIHP 1rUS8NwFQUBZX6i7zYMJ5HQo+pCK4iIg968G0AxlWKBS5LwngNjFTnycj6qia6NJMeIH HpMN21iN6VMg5c52V7/4cdoklQdxf+qOSvGDWpWOoaBAAsSBSxRrVD3CIK/gqkH8oxl7 EChhxDaM8+6Lj5mT1Ynt2CbhGeB1lybmvwtbJnYdr6oZ1P1pkQSooVEqQEW0Z3aYa7FK CLUg== X-Gm-Message-State: AOAM533YteSc3IRjeDVs8yMEIMXlJ/xwvwJJ25iKX2Xy8ruti99IO3wB njcBlKLCYm2F6zjdoWYrvPQ= X-Google-Smtp-Source: ABdhPJxB8oBLVYQIgwkvnu8g2FRtiMpCBsQo+ovoCGcyHrtFvqGOW7D/AAtbkyD166YOTXthL8f+nw== X-Received: by 2002:a65:4503:0:b0:382:aad5:ad7d with SMTP id n3-20020a654503000000b00382aad5ad7dmr22972706pgq.488.1652312296878; Wed, 11 May 2022 16:38:16 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:4ded:7658:34ff:528e]) by smtp.gmail.com with ESMTPSA id x6-20020a623106000000b0050dc76281acsm2308668pfx.134.2022.05.11.16.38.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 16:38:16 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH net-next 10/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in INET_MATCH() Date: Wed, 11 May 2022 16:37:57 -0700 Message-Id: <20220511233757.2001218-11-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.512.ge40c2bad7a-goog In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com> References: <20220511233757.2001218-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eric Dumazet INET_MATCH() runs without holding a lock on the socket. We probably need to annotate most reads. This patch makes INET_MATCH() an inline function to ease our changes. This also allows us to add some __always_unused qualifiers. Signed-off-by: Eric Dumazet Reported-by: kernel test robot --- include/net/inet_hashtables.h | 52 +++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index 98e1ec1a14f0382d1f4f8e85fe5ac2a056d2d6bc..5d3fa071d754601149c9ad0dd559f074ac58deaa 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h @@ -307,23 +307,47 @@ static inline struct sock *inet_lookup_listener(struct net *net, (((__force __u64)(__be32)(__daddr)) << 32) | \ ((__force __u64)(__be32)(__saddr))) #endif /* __BIG_ENDIAN */ -#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif, __sdif) \ - (((__sk)->sk_portpair == (__ports)) && \ - ((__sk)->sk_addrpair == (__cookie)) && \ - (((__sk)->sk_bound_dev_if == (__dif)) || \ - ((__sk)->sk_bound_dev_if == (__sdif))) && \ - net_eq(sock_net(__sk), (__net))) +static inline bool INET_MATCH(const struct sock *sk, struct net *net, + const __addrpair cookie, + const __be32 __always_unused saddr, + const __be32 __always_unused daddr, + const __portpair ports, + const int dif, + const int sdif) +{ + int bound_dev_if; + + if (!net_eq(sock_net(sk), net) || + sk->sk_portpair != ports || + sk->sk_addrpair != cookie) + return false; + + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + return bound_dev_if == dif || bound_dev_if == sdif; +} #else /* 32-bit arch */ #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ - const int __name __deprecated __attribute__((unused)) + const int __name __deprecated __always_unused -#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif, __sdif) \ - (((__sk)->sk_portpair == (__ports)) && \ - ((__sk)->sk_daddr == (__saddr)) && \ - ((__sk)->sk_rcv_saddr == (__daddr)) && \ - (((__sk)->sk_bound_dev_if == (__dif)) || \ - ((__sk)->sk_bound_dev_if == (__sdif))) && \ - net_eq(sock_net(__sk), (__net))) +static inline bool INET_MATCH(const struct sock *sk, struct net *net, + const __addrpair __always_unused cookie, + const __be32 saddr, + const __be32 daddr, + const __portpair ports, + const int dif, + const int sdif) +{ + int bound_dev_if; + + if (!net_eq(sock_net(sk), net) || + sk->sk_portpair != ports || + sk->sk_daddr != saddr || + sk->sk_rcv_saddr != daddr) + return false; + + bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); + return bound_dev_if == dif || bound_dev_if == sdif; +} #endif /* 64-bit arch */ /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need