From patchwork Fri May 13 18:55:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849257 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 659CDC433F5 for ; Fri, 13 May 2022 18:56:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383432AbiEMSz7 (ORCPT ); Fri, 13 May 2022 14:55:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383446AbiEMSz5 (ORCPT ); Fri, 13 May 2022 14:55:57 -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 D9D386B7F6 for ; Fri, 13 May 2022 11:55:55 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id q76so8275903pgq.10 for ; Fri, 13 May 2022 11:55:55 -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=fzEa6nva0+NwD1fIN06Dr6wsem1/Ui1FVlT+hEtrN0A=; b=g4/97bapCp7Qjyt8rszUzjhFdvBdemdXJoEJtpgP5OeP1DM3V5RV3Dpz4T0bU03f4X uV12VqGs0PQ/ubn6LZFatAQgvB/ClRf5ddyyk7mIzlK5ctGrxT2hHrhSyq2v6PW4gZxP CxwJz33i7p2veU5aYYL2Y9+GMiQ4AY4/QGDCUs1V3KyMab+VpC1w66h8SPvv95+yG7pJ +7SuuEaBqUjJ6fTuTWPvfbe+ovawgf5UYpYPUMeFUzuGvjMeLhtvC3rW7+uVB5uGsNMm TPZgm2QEeSTsmyV3ka3BFV2ZzjuZ1TZgyE2BDgePrDN0iOrWWU+wSSDmpNdG9W59w+1K CIMQ== 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=fzEa6nva0+NwD1fIN06Dr6wsem1/Ui1FVlT+hEtrN0A=; b=YddnSKC7DmzBfs4k6tRrUuvui76z/E2BXxl7VqQpdsOyctVKcVRe+CgOrArH9Ahf/Z s9cIsrnPcKARWAL2iOBlkxh95Bhcl7mEfP0xHLcaWpy+b4Cf3h07Ba4nIBLJj9/cISad b1bktibzX8sxqSZICxkG69aGRBai+xpOa6JfMkHN99+fDXYsytbvolvaIYUJu4PlNDDR ktNnCoQ04MDmQINk0qNLw7G6yFyvkvuKO/YySopdJePuaiPYVoedAVPF0sQOgcRWIj6w XWfkbS+OhoOB1z5OuW9KmX//qgB/KSP58CMspnZjWL9LpI4+/Wc4KE1SN8W3mA3vkgoS FbTQ== X-Gm-Message-State: AOAM532RWyx9UUHwtD2rHeaSbiMWSP79WZXsZ++u8Cu3LijzQ0s+muo5 Q52hc6OGPh/kIPdkmmD5y+k= X-Google-Smtp-Source: ABdhPJxLg4kI4DWF4f56zkpqEuK72WOSbE6LvaiLHLC+JbRwTD+K8J5KY/KdfNrHYL1/uyiiWd8dWg== X-Received: by 2002:aa7:8256:0:b0:4e0:78ad:eb81 with SMTP id e22-20020aa78256000000b004e078adeb81mr5804043pfn.30.1652468155385; Fri, 13 May 2022 11:55:55 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.55.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:55:55 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet , syzbot Subject: [PATCH v2 net-next 01/10] net: annotate races around sk->sk_bound_dev_if Date: Fri, 13 May 2022 11:55:41 -0700 Message-Id: <20220513185550.844558-2-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 01edfde4257d697f2a2c88ef704a3849af4e5305..72ca97ccb46072491179c1225f4e8bab85a7994f 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2875,13 +2875,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 Fri May 13 18:55:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849258 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 AED76C4332F for ; Fri, 13 May 2022 18:56:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383439AbiEMS4C (ORCPT ); Fri, 13 May 2022 14:56:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383441AbiEMSz6 (ORCPT ); Fri, 13 May 2022 14:55:58 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D2B113F0F for ; Fri, 13 May 2022 11:55:57 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 31so8289874pgp.8 for ; Fri, 13 May 2022 11:55:57 -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=lajSQFrAmQ6OJRNALq9XvXIlm86H0i8f/Z4pUjJ7cG0=; b=jtymue7j/IrNNp/BIMttKlb2mEU17RfRq0a06OLxNhXfA12XEvd2366TfuSsghMvJj TOSwt8DIaXFKYLx7S6uIugjCSiiOoHqz2ENHFBABR/MPPSU+At5elKmkCIne7gBoFyyA 1F+vrEQnWQqg27VNHtXdFTGHA2EntY8SidlrbNoeRTSkeCDVD1KPJhZrcxtIIfv3tRax 4YoOUpIjR+k9FbHzShqc7NY3fgfHau7OyJ1s5M51wTdgKFy0lY7HexjhVsKKn18xLuw0 YawUIkxYLV1fkwlY+PBNCuleRMlcyarW944z3ZSBfYdlKkpG8NO/EUWAPLRnS5puI5eB gk9Q== 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=lajSQFrAmQ6OJRNALq9XvXIlm86H0i8f/Z4pUjJ7cG0=; b=50UNZr2pohsYrOWZxQrkSvB7Ucpcv/OtzFP5rERoN4b9qSy9S80Jvt2tgu1fAd6x62 KinIXRpg+iBVVi6WMXl7F3HTFk6rlskY6GuT4Tvp7oCRrerMQUXQZW5s2n5QPcEmr4gs mgNDQ4reOCijVKPZXRFvqFcmQDcoQpgCIZsKIteVWofs920U2qZSI2aSwrTNRQLkhEF+ uELFoCSlR2M0DGg+9U9GT+91u+IKKU32B8q5YPvyxxvfazlm9RlpxieNGn790UjzMSb1 5ZJ2TyuM3y9AZogHVodAqJ/KY1Xy3aqeCF6dNhZz5Ek+3ONeaIZfqWAetRXF7oMHzhqm f49g== X-Gm-Message-State: AOAM5335cRPEoi8S8B07aBBTImmU2nUYQKESGoWZKzsmeZf/r0tqaBTz LEiz+LlPpBcv4B61CuiY90w= X-Google-Smtp-Source: ABdhPJygy36GADMAjIOXu+zCX1EZyb3tLP1q6qwgrVHJ0Z56rJKWZk1CugwYP/QpwBYYzgsAZ6CIMg== X-Received: by 2002:a62:1d09:0:b0:50d:fa91:a4c5 with SMTP id d9-20020a621d09000000b0050dfa91a4c5mr5950146pfd.25.1652468157054; Fri, 13 May 2022 11:55:57 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:55:56 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet , Neil Horman , Vlad Yasevich , Marcelo Ricardo Leitner Subject: [PATCH v2 net-next 02/10] sctp: read sk->sk_bound_dev_if once in sctp_rcv() Date: Fri, 13 May 2022 11:55:42 -0700 Message-Id: <20220513185550.844558-3-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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. Fixes: 0fd9a65a76e8 ("[SCTP] Support SO_BINDTODEVICE socket option on incoming packets.") Signed-off-by: Eric Dumazet Cc: Neil Horman Cc: Vlad Yasevich Cc: Marcelo Ricardo Leitner Acked-by: Marcelo Ricardo Leitner --- 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 Fri May 13 18:55:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849259 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 A7728C433F5 for ; Fri, 13 May 2022 18:56:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383446AbiEMS4E (ORCPT ); Fri, 13 May 2022 14:56:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383442AbiEMS4C (ORCPT ); Fri, 13 May 2022 14:56:02 -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 716686B7DD for ; Fri, 13 May 2022 11:55:58 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id l11so8274280pgt.13 for ; Fri, 13 May 2022 11:55:58 -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=d/E96Ds0GUwoQ5W7o3zVaOAL/f9+dTNzBDNkL2TLe2c=; b=MI/RKOccQaX92yRkITDRiLQyOz+EI3d1YM9m3/Soc6bGTGzljFGlCOm+c0nROnSDLj ak31jouH9B6tJPR9w3FKxfty6KQiPT/Ac3eY1WHEZ5FBFmoX5wb+mWcccXM9SFxCaECX A5rxf/rUACRtZ5dpwvQbd8KbAsMJEdnZLv+ZgTt8e2rw+jhfzHjBjnRxQDdxtnkmcy4b ssxgp2K39Z7Hav/Rw7bKWosUdTRVTA/XwZFZVdB4CrUiHk53RvxIS5SFFw/ClCW408Z0 kTecF6CS1EARnyq6aGG/HPCTLK7mrfBRPtG6YrgmRI8r0TQMXEBr5ZMtnbLrg2BaHLBF wo+w== 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=d/E96Ds0GUwoQ5W7o3zVaOAL/f9+dTNzBDNkL2TLe2c=; b=q7Q6+W7duyq+CkzYaXrCvH4NUOLzE41k6Ogu4lIg8ns/7l395CJyekM2GVbQR2hUAB c0SBkUVOEJP4W0Fh2C+6u3S6SZmBRKKNUJeXKd/OdzXxL9fE7Bi3YzL1KgGh+JjKB0ZY 5VUZyboQ9A3ckeR3MylpSyN1TpSngToMIi55SyRTM+0V//CbUFXMY3NEPALuj+i5KxFL Pl2hYiSBx5Z0iAC5bHWxC35xNW1rkiWJQwqmtU2qmewFwghCy+1ZktSPMxIjNY8389pX CSLOfxIRoHTXXCaLQhom6c+TdtniBd7zk3usDyJBJDUZmH15VR9E6RwTqBKo2aw9e/gF UcaQ== X-Gm-Message-State: AOAM533E4vkZYSr25WZYEfCO5B3Y7MOgsHXuQY7EGV0ch4zqZD1R0wwh vDjIkWBGz8AfCMuYSJytCtY= X-Google-Smtp-Source: ABdhPJzUrVPw2GCOF96IPPkNkvUxOx0TyTsRADaFknYJTGZhvMFKhApR5rTkXFw4vosR4JMG529Ovg== X-Received: by 2002:a05:6a02:287:b0:3c6:b341:e3ba with SMTP id bk7-20020a056a02028700b003c6b341e3bamr4968555pgb.271.1652468158519; Fri, 13 May 2022 11:55:58 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.55.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:55:58 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 03/10] tcp: sk->sk_bound_dev_if once in inet_request_bound_dev_if() Date: Fri, 13 May 2022 11:55:43 -0700 Message-Id: <20220513185550.844558-4-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849260 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 82E52C433F5 for ; Fri, 13 May 2022 18:56:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377285AbiEMS4O (ORCPT ); Fri, 13 May 2022 14:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383444AbiEMS4C (ORCPT ); Fri, 13 May 2022 14:56:02 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BB616B7EE for ; Fri, 13 May 2022 11:56:00 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id n10so8813830pjh.5 for ; Fri, 13 May 2022 11:56:00 -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=kshGRh47Lv/RpWJVN8MRf307VuzALDrIBwl//zwEEZU=; b=Qxy73m+rmcukqNo/rc1h9c+6dwBct8LqvXAM/9mQxCNWC9Fmj/5P31QRlfHflzI8On swjmO4z3nQsDVlm6i0Cl8FohyNqx9BtlO6VFowJmCS7eU1MJXQtRUcMn2cH5Jrk0Lxcw 8nMkOL5oX4KcXA3Qy1c90lCBBZqtNqZ6nWYXcXDwZPRnDHdsv2tVn3vRv+F5QdUxc4UJ K9JsIPc/9dBHOrPrTjawM0o2FtbJ0xGR1wNXXIgMjPHxrHKWVSSEnFiMakLUUq6VmCnE jZhJqLpNGPtrIwDprfOP4E8yNW+7gB79+/SXGwVUuRrLR84oXJ9MQWEkVptJPQ9Yi7mJ z35Q== 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=kshGRh47Lv/RpWJVN8MRf307VuzALDrIBwl//zwEEZU=; b=HUKStaPhno9DAbDvfLYFvEgBaR7uYj6XpF4pfwXxQQ3EDadfmQM1/5Sw/kn0IvA0pA puAuRI953P+l8tOwfyu2ErA+oPt7pGmLDrX2xHRoikXGA50v4pjANlTbezVWvPPGbJTD YAWPK+PoxyBHN5nzWrG3yA2hRKxSOoOR/hh7txYbn53UPhWKyonfaUa047lSALX90DYz dTlreyxQwPvaUfxxp8IRnjoPGix9f1yq0JuzWi4Pru5xPXJnNsREtoCbxwbgXb8j0201 zRukusNavqi5HE1t35RbJtY7fAu4E/otoZfU03nLzcN9pqS8TAWN0WT/tzKaPqpEimDD S6wQ== X-Gm-Message-State: AOAM530AZwRb1NwXAFCFw1F6KnafEC0xAMnddsDRLRh4YBR4DHw5yrHm EwPm5al5i4zgOR2UK+EDBuQ= X-Google-Smtp-Source: ABdhPJzyGmqKo3Y7RFt6ZikUmtGYtIbafKUlctgsFLVxsFDP/VsCrzTdRlqKdbqkVT1jUsgnKt2WlQ== X-Received: by 2002:a17:90b:3812:b0:1dc:8502:2479 with SMTP id mq18-20020a17090b381200b001dc85022479mr6287668pjb.97.1652468159802; Fri, 13 May 2022 11:55:59 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:55:59 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 04/10] net: core: add READ_ONCE/WRITE_ONCE annotations for sk->sk_bound_dev_if Date: Fri, 13 May 2022 11:55:44 -0700 Message-Id: <20220513185550.844558-5-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849261 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 E8CAEC433F5 for ; Fri, 13 May 2022 18:56:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383459AbiEMS4P (ORCPT ); Fri, 13 May 2022 14:56:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383447AbiEMS4E (ORCPT ); Fri, 13 May 2022 14:56:04 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE3986BFCB for ; Fri, 13 May 2022 11:56:01 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id 137so8299177pgb.5 for ; Fri, 13 May 2022 11:56:01 -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=81G4T+QmKwuwMVNq2fbxbqAa3jbgn5JPe3T0rgFTknQ=; b=I6J6i4CU+WxNHLNysJLLGudVxa1buZIIWqgN4YB7eS21LdrSPMLDBQTuzS5TdGFKk7 +Vl5bDAieSUM5X4jl7mbuHf1443gn8aK7FJ63Y3tA2hhZZQoDWtfvuEdR+4LDbumF6jb z1OEUFEE3fIrsbqw2fKfOCSlw89LLkUO1bbmWEdlHFlEidKhHYPM8aVNMtocUl/N8TUZ rgRZFptWhyl6EdavxKKB6TkK1eY6NdKaG/IyrtQ3dviiSkgVdskH/f1BOCvY1D0lzswz ing5qWv+ybzFdkzt7aoKG3r8WRlpVfy/99xs5gggKKV7H6BYh8nWy8RPBcpR167cKtyB CPxg== 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=81G4T+QmKwuwMVNq2fbxbqAa3jbgn5JPe3T0rgFTknQ=; b=ogUsl9f3eqLW7wuKOSprDza1KDUuI1F05Rdfv+3ariJ5K7Xlo4xIfdlF16m78UbP5V ROQec1LLU0rZI00T2bJVWA8lAoTJPOytNbu6sG/mEsb4OsTyr/luLSWOaC7sBIcTOi84 bylbx/6bj0ZKqOcqMHkEwkvju56uojL+u9VVscCTodqvGWr/6SYT98vsG+5Q8oNcsenH rElDg/+qTedM1xLjJU1+aPcX4zUMXu1PqpuwlPBAy8jbT47ybvJY9CNJ85/OxX8FIJbM pSkqq/miRf+6Zrmoehvy17eb1r/R7Ue89BH1ZO2jhyZ58fK3Z5nZ5O0hEvsA1A9eTd8p 4l7Q== X-Gm-Message-State: AOAM531vKaCkj10ju/tDPCSjZJzGFmIHLUDXHu8bntaNx+8ET6/y9spo j4VgKS/4KDRVKvW5Kr36AuxbyEOYO84= X-Google-Smtp-Source: ABdhPJxMHHtG59pPGpruuB1htwux8AZfFavR7849v5iFFP9N+w6JQJJ2HzQUulxJ5hn4aMjBOIIBiw== X-Received: by 2002:a62:e80f:0:b0:50d:3693:43df with SMTP id c15-20020a62e80f000000b0050d369343dfmr6067466pfi.36.1652468161390; Fri, 13 May 2022 11:56:01 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:01 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 05/10] dccp: use READ_ONCE() to read sk->sk_bound_dev_if Date: Fri, 13 May 2022 11:55:45 -0700 Message-Id: <20220513185550.844558-6-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849263 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 56488C433EF for ; Fri, 13 May 2022 18:56:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383444AbiEMS4U (ORCPT ); Fri, 13 May 2022 14:56:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383454AbiEMS4N (ORCPT ); Fri, 13 May 2022 14:56:13 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B38E6B7F6 for ; Fri, 13 May 2022 11:56:03 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id p12so8492441pfn.0 for ; Fri, 13 May 2022 11:56: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=A19XDJDBYsixko11EpPg6VqehJxYk2XrmEZVWrbhfVk=; b=gBrPj/tZbX/3GK5iRqyczckOTmNGHak2YxDw3OeWDRhPZgVQUNy72l33XAmlO7Y1NY xycLKoxfXLtzglmE8YaaTK+pOQwVPGJGpb4K7JRsx+Z8G1K0jbdrhtO82mCwQdK3Lzap N0pA7As2f1XkE7Cb1b5KEEy2epUGbz9OXRK4dTRGEc7TgJtSyjgrRQRYtuzYP8nsQuS9 OrL9+IEX8dLzfA8/88VeSd56KowEqLaroeGk8hQOz9c4+BnwUQdkaslOxP8xKa7itYj0 MV/i/HODdDvEmj7S4iowaEa3N8xRVYAGCyAwTrxGvUrjt/L08RtVdcqDq48t+YfA1ScB ucvw== 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=A19XDJDBYsixko11EpPg6VqehJxYk2XrmEZVWrbhfVk=; b=w7Y0k5TmKtjSmHBEPptQ5vsj3C+I5qrMb5/3twRTHbm+dnxgzGqmq3u7JPSibEKRSp Ua1QoCkXSLLQVR2aaV908SV9aKXEzPyFqmx2FlTF8xM1n1grvQdrj5g2PKv/MDQxVkzD 4X2vR0xfhWBRP5hPgW25+ZLlf1GNJbEFoeDgZ03X4pwcf4j4OQ6Z6s4h+1smBmj9a72n 98+CNHUUDJLysBY27GQYS/lU4JCHRc0OYgz+NDM8rEuFTQ7hc6PbAprYAebWdiuh9z70 u3VOX/IKXProQR5nEblmvIYr6bN6e5sqyyhY90yj6dVIPX/X0KOwb/lPpFe4dVcZriue OjpQ== X-Gm-Message-State: AOAM5300+7cRQpGtJFSuxt8efVOkwQd11og9bki8zSPjSq8vCBk5A9fQ ssMrG8sNqKnt4LIYIwCnI1U= X-Google-Smtp-Source: ABdhPJzceOAS1blBMRSvLjlJcqY1SP0aUEgz7A5H6nSmVvHnsSDd3DVGADEuAvYxxQDYCtGSEilMMA== X-Received: by 2002:a63:8ac7:0:b0:3aa:fa62:5a28 with SMTP id y190-20020a638ac7000000b003aafa625a28mr5117082pgd.400.1652468162665; Fri, 13 May 2022 11:56:02 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:02 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 06/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in inet_csk_bind_conflict() Date: Fri, 13 May 2022 11:55:46 -0700 Message-Id: <20220513185550.844558-7-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 12849262 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 CE538C433EF for ; Fri, 13 May 2022 18:56:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383464AbiEMS4S (ORCPT ); Fri, 13 May 2022 14:56:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383455AbiEMS4N (ORCPT ); Fri, 13 May 2022 14:56:13 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7975C6BFDC for ; Fri, 13 May 2022 11:56:04 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id 202so8278443pgc.9 for ; Fri, 13 May 2022 11:56:04 -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=d5D8Q+LOxDZKyXKyV0E9bG7L/yCxQeBI9J1yCtvXJSs=; b=GgZS4fFANbszLsJCc7K5kbqv27tdOgwjsTQg+1U6biokOUtdqTjVcc8FHF74uT2OWv 9baz9t7Av9ytiYaOFdXxXCYJpyGOPBgm5/AVw7qGTGucCX3HXfSIs1XVkb31AlKfbRAf qPC8KB8ZlzIBQgCm8zKFNZNZOo9byLZGqDlf3QzI1W9lXIkQ5ABhD2VX5oFBl0LbelJl A7i/18WXXtmQEX0Ts92Bmw8wMa2ZJ3C1WH/+lcTydFO5jYb9e7h249jk0AuAjUsJgs3f SODrjeQ13UTn+CsogTjH6ac+g/Le4rC5QomeY2uysaEdDaPww4DujCv32K6wYkQn/InO jQkA== 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=d5D8Q+LOxDZKyXKyV0E9bG7L/yCxQeBI9J1yCtvXJSs=; b=jRJVdG6C8/SxS+rmk90ywouZ5QzeW6mHiAUttRwGWHKRu4yFdk16jFuvOvon0rHsz0 xKnRHjrF5Jed731arhl2Ky1m3jRAFxeVa8ppDvJ3vbFz4+PF5X6shR90F5qh7QkLI9jm x0t3HhZXvdNC6Lr6CigSW33UrMe/rWwpJUPheAO7OVFCFCvfDUrITkEQb9ez7azAIqgk vodgZ4iA7WOgWNOMtscVN2nOjtaUGv0xh9tpfCeJxdNoDRMfDpOHDVh6NgCG6CBJ4lf6 cPeorLtQd+sKO1bRlO1o9vK/D3oyx4L6bm6cls3magkTx61il1l0iTYoWyEcROXvmIqM xyUw== X-Gm-Message-State: AOAM530oXADyWaGzFghBDbV2KbBhmW1+2JVaPuemOmaXe9jPF7dSPDFu r3LdbiyWFqaNAwNG+6HnAV0= X-Google-Smtp-Source: ABdhPJwhMfrR2M7qu9ddY2U+G2f77d6APb2MpxAVy0+EQcBYvIT2LcL0tVqzGP2zyc6Fbzs4nIBaag== X-Received: by 2002:a05:6a00:10cc:b0:4fe:3f1c:2d1 with SMTP id d12-20020a056a0010cc00b004fe3f1c02d1mr5802765pfu.0.1652468163998; Fri, 13 May 2022 11:56:03 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:03 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 07/10] net_sched: em_meta: add READ_ONCE() in var_sk_bound_if() Date: Fri, 13 May 2022 11:55:47 -0700 Message-Id: <20220513185550.844558-8-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55: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: 12849264 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 C33ADC433F5 for ; Fri, 13 May 2022 18:56:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383450AbiEMS4V (ORCPT ); Fri, 13 May 2022 14:56:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383463AbiEMS4N (ORCPT ); Fri, 13 May 2022 14:56:13 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA7846BFFB for ; Fri, 13 May 2022 11:56:05 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id y41so8416643pfw.12 for ; Fri, 13 May 2022 11:56: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=bbzJfH0PErMCbd5F/OJ6qwc9WUXsd3JJD9CMV2tx8VI=; b=J2zQ4e75hegVgqM2zyZZ6BPdnRgLY/8f70HMgTRyHfxd1bLe0y73pi4rAdJ/QZsInm 3AQykfUMMjhAnLfoJkn+GtNQEKoTeCBIaaDMl8FcJTFHC6vmgzq8lpwMdsAeVbw4hvdS WkBDR2+rd3zzANjsEeciLsKeW/ZOMC2O5aLyBb+eLKiIHMuoqTwoU2yjcXsPI421xWK0 TZJw0qwvMl/qCbhMHUDUZtXwf7ZtDfVPfjl6yV6AwYvb4QyfXRHA3QIZe6ivQk3PPRVU i5kxPMdQxtEogxHHfDVkMu5o1kq5HVf2naIhfVYbijtR/vo8yxMsT0sgeMMnDqB/u8jN mIXA== 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=bbzJfH0PErMCbd5F/OJ6qwc9WUXsd3JJD9CMV2tx8VI=; b=w4+iWlB0JjNZEPbEzMIYooyScyKhHTgL6LoRnOTkSZl0bJUIxkvVXYOGa2C9PmZjLj POXgYVWlLI+oP5CUzDNqsqAUK82RrQ7Z73IdvEb5QFpVpwlLwS4OLFFB/0+nTyKzxGcD dYb3j/YZiqhsrYICCG6NMJW2bFW5507g/Iyl4auBHxa8PjcycdNEHBKV+n4P736VZA8E LBHoMD6QX+JSXbvZLjuSb3Kbmm+k2OzvXDP5CDZFSiWYQiNolmx0n9I0jHdD1KmS0Skw CxttG9HBG6i+yDo4VPuK1sd1yy6UJGsxvUhYM4CFmgAPaM/Q7/gRtZLkyn0Wrbki36EQ 8fgQ== X-Gm-Message-State: AOAM533ytPS+GAlwBodLB5khRBm2khJiLeFRSuDYZ9TkfQQ5I6X+iJhn hoStHAKuE8Ggg4Xb0Ip3Xpw= X-Google-Smtp-Source: ABdhPJz6vv1A4SkqwzHkSys+zcoj7DX5OMSwXNHCGdP438h85ajeW79sajzMRo8WghhpkFZK2tWR7g== X-Received: by 2002:a05:6a00:882:b0:510:a043:d4bc with SMTP id q2-20020a056a00088200b00510a043d4bcmr6010337pfj.64.1652468165292; Fri, 13 May 2022 11:56:05 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:05 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 08/10] l2tp: use add READ_ONCE() to fetch sk->sk_bound_dev_if Date: Fri, 13 May 2022 11:55:48 -0700 Message-Id: <20220513185550.844558-9-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 Fri May 13 18:55: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: 12849266 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 6EBE9C433F5 for ; Fri, 13 May 2022 18:56:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383455AbiEMS4X (ORCPT ); Fri, 13 May 2022 14:56:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383442AbiEMS4N (ORCPT ); Fri, 13 May 2022 14:56:13 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A4156C0C9 for ; Fri, 13 May 2022 11:56:07 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id l7-20020a17090aaa8700b001dd1a5b9965so8573168pjq.2 for ; Fri, 13 May 2022 11:56: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=te+u4FfgicEd58cMsjJAmr9WZSRf/aUyr9MHF39R4zI=; b=qZYQ9mVAalUkB3anjAFE9hUcbXlkJUzRCuTdur0ihkUIi2V8jjq5FJlTHjGe7zwXMS qt0mioqrY1RWYLbUDwyi24KrkXaBXtTU+rzVWf3BLn4ZSwyz6Geinw+NZXFzxvACe2P1 pN8soSAa1jtAYOdlrZrMXK4GdpNBT8VJ/LvbokrK0s2nE5H9/cslPizUH2Dxdp+EWBZT FRByB7yPXMQtgxX1gq7nkiTQQby2Ojt94wYdhYWQDTPQQibAUqshpEy2kG4By3VAnxYF EWRSvWV10IY33nFYRV9p2BNIDlUz0JHySlIirc0sVn+YKyIRSLIjbzNOrJEP+GpY4yaJ cnFA== 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=te+u4FfgicEd58cMsjJAmr9WZSRf/aUyr9MHF39R4zI=; b=Cfvj92rZz9kqTANHII3P+m1PA1hSvWnz8dX+AzeCtzZKtwmOm0GjToH+Tm2IolI0Gc xsXbqqH2MGSVihe6llgSDeSRyYA3IduXIgiQ4AdhouvBrpzj+QjdSUbqvPRBabu0+1+5 p/RGqp+q20qNyNo41/8LOtxmxRbcnalfwKxwJ2dRFcc6QBF2gvhIi7MZCoPb81i2qSPT ofHjt+Kj5+Y61yi7Eg52fq2HxxE5RS/Z3TNsh8jocJQbObzW41yaGcoueST5bWQlJZkp ZmAvoAPzHh43U7vFXVmqx27Q72SlFwtCatJfnPhpUBKen+RTKIkUoOvnvETyQ1ccWM7G vIoQ== X-Gm-Message-State: AOAM531Wlve7EqdRD4n8Bod+Wd2IE05UEQQDwWDQVVLDHt9/We6vf4Aw WI7NnT4IK1me/P+ArYd8ovU= X-Google-Smtp-Source: ABdhPJyhjAyK+JC5l22IwX2xG5pbhJ1f/8/PQn3kn64Keeq8ql0iFHXWe7rbm9MQBeFJg4WszncbEA== X-Received: by 2002:a17:90a:7645:b0:1dd:2482:e4b3 with SMTP id s5-20020a17090a764500b001dd2482e4b3mr6216602pjl.204.1652468166752; Fri, 13 May 2022 11:56:06 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:06 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet Subject: [PATCH v2 net-next 09/10] ipv6: add READ_ONCE(sk->sk_bound_dev_if) in INET6_MATCH() Date: Fri, 13 May 2022 11:55:49 -0700 Message-Id: <20220513185550.844558-10-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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. v2: inline function only defined if IS_ENABLED(CONFIG_IPV6) Change the name to inet6_match(), this is no longer a macro. Signed-off-by: Eric Dumazet --- include/net/inet6_hashtables.h | 28 +++++++++++++++++++--------- net/ipv4/inet_hashtables.c | 2 +- net/ipv6/inet6_hashtables.c | 6 +++--- net/ipv6/udp.c | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h index 81b96595303680dee9c43f8c64d97b71fb4c4977..f259e1ae14ba012f9c91a406b5ca83c6876f4934 100644 --- a/include/net/inet6_hashtables.h +++ b/include/net/inet6_hashtables.h @@ -103,15 +103,25 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, const int dif); int inet6_hash(struct sock *sk); + +static inline bool inet6_match(struct net *net, const struct sock *sk, + 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 /* 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))) - #endif /* _INET6_HASHTABLES_H */ diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 5257a75756497e08b504d92e76ac604904f9c3a0..acec83ef8220025f129194dee83c3a465bf3915e 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -499,7 +499,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, } #if IS_ENABLED(CONFIG_IPV6) else if (sk->sk_family == AF_INET6) { - if (unlikely(INET6_MATCH(esk, net, + if (unlikely(inet6_match(net, esk, &sk->sk_v6_daddr, &sk->sk_v6_rcv_saddr, ports, dif, sdif))) { diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index a758f2ab7b519e0aecf19ad2b6d5d3f91a9957cc..7d53d62783b1bb3f2ec4318526d06da16a7d0c28 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c @@ -71,12 +71,12 @@ struct sock *__inet6_lookup_established(struct net *net, sk_nulls_for_each_rcu(sk, node, &head->chain) { if (sk->sk_hash != hash) continue; - if (!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif)) + if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif)) continue; if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) goto out; - if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))) { + if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) { sock_gen_put(sk); goto begin; } @@ -268,7 +268,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row, if (sk2->sk_hash != hash) continue; - if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports, + if (likely(inet6_match(net, sk2, saddr, daddr, ports, dif, sdif))) { if (sk2->sk_state == TCP_TIME_WAIT) { tw = inet_twsk(sk2); diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 960cfea820160614f3606ce4f407b7aa89fc70e1..55afd7f39c0450ff442a0499b7f8e42bf1a613bc 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1044,7 +1044,7 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net, udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { if (sk->sk_state == TCP_ESTABLISHED && - INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif)) + inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif)) return sk; /* Only check first socket in chain */ break; From patchwork Fri May 13 18:55: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: 12849265 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 ABD86C433FE for ; Fri, 13 May 2022 18:56:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383453AbiEMS4W (ORCPT ); Fri, 13 May 2022 14:56:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383469AbiEMS4O (ORCPT ); Fri, 13 May 2022 14:56:14 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E80986C0DA for ; Fri, 13 May 2022 11:56:08 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id p8so8424980pfh.8 for ; Fri, 13 May 2022 11:56: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=1eVW6kVW/vGRNhMVNOqzUJcLuY6jXULfLVuE7Ejkm8I=; b=TQ+rh0C4qL2yBrrhDpjW6U+pmT/+rOn3u89TsXUCCq2oeMw57GrtpiGTtuFUrmwwjF maxpvcBhMGpYW5FQ36VYW0tESGYykReN8lck4XxiyMAlUx2IJNl+z8kblgbUSKveKbtL L/Ejv524NhkuOvxirfoS/CdpfNe/LBU9OrLpdGjw+7apvFxF7fJsQAoT/kOAf+j3R429 tHkeRvGQzc9ReL7G7dlZKSCBtwdqXVlgQzMMPmMoY3PNZ2n8xscTCS/4LcuvjWOFML5q RlhR7dt/tDsJ9g4glDDHCPRT039Yt0YIIboRLSRPQsqTKs6bFgDntT8n7BiLE+rEHsLx DDsA== 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=1eVW6kVW/vGRNhMVNOqzUJcLuY6jXULfLVuE7Ejkm8I=; b=HSHWKok5y+yXXkEBiNuOEMxsA4M/TEnNkfmEZrSpvWzlT0cVmy+uHYrp+KyEgV1sWY 0/21gZBR/Mb3h27PtNDewK9UmGxMySd5xcM3sppqc7U17U2XMEysSoC/pd5mGven22g2 KONzXCXSfgqud4jggzrQoygl9rurxWgWGMAZt51bPfkpcYsXqKEBFyR+KdrwRir4sJMg G1xAuF9ueq6qoMInmr3KBmEsnUcJjBFTV8/UZALWiSHkJLg4jFNHwioDT8s8OeH2Dnri ekWVfczcJb3V8tRt6OLycYk4b8lBvC2afG6eiZRYHVNG3Tfds1vmkptwPNYoh4ZkVl+S v5yg== X-Gm-Message-State: AOAM530n+QX7o0KvM/PUQ9GOdBm2oOoODFzApCHIt8YyBQOAGyWPXUrT 5+HqbOMdxTafusIzbHHcHZY= X-Google-Smtp-Source: ABdhPJxrfNM3wXFQ9Rh3eg8eJj6ifdkwTN6lMYA4rEwbITmYGpdrwv3mHvuGNly86csev8EHtQJOvA== X-Received: by 2002:a63:5510:0:b0:3db:8bb3:6059 with SMTP id j16-20020a635510000000b003db8bb36059mr4964316pgb.328.1652468168237; Fri, 13 May 2022 11:56:08 -0700 (PDT) Received: from edumazet1.svl.corp.google.com ([2620:15c:2c4:201:c436:3fa3:479f:a7a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c10200b0015e8d4eb2absm2159537pli.245.2022.05.13.11.56.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 11:56:07 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev , Eric Dumazet , Eric Dumazet , Olivier Hartkopp Subject: [PATCH v2 net-next 10/10] inet: rename INET_MATCH() Date: Fri, 13 May 2022 11:55:50 -0700 Message-Id: <20220513185550.844558-11-eric.dumazet@gmail.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220513185550.844558-1-eric.dumazet@gmail.com> References: <20220513185550.844558-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 This is no longer a macro, but an inlined function. INET_MATCH() -> inet_match() Signed-off-by: Eric Dumazet Suggested-by: Olivier Hartkopp Suggested-by: Jakub Kicinski --- include/net/inet_hashtables.h | 2 +- net/ipv4/inet_hashtables.c | 8 ++++---- net/ipv4/udp.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index 59d72024ad1d6fe34342309191b46fbb247b125b..ebfa3df6f8dc365b4ce5f4c4fb573c37193492ab 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h @@ -267,7 +267,7 @@ static inline struct sock *inet_lookup_listener(struct net *net, ((__force __u64)(__be32)(__saddr))) #endif /* __BIG_ENDIAN */ -static inline bool INET_MATCH(struct net *net, const struct sock *sk, +static inline bool inet_match(struct net *net, const struct sock *sk, const __addrpair cookie, const __portpair ports, int dif, int sdif) { diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index acec83ef8220025f129194dee83c3a465bf3915e..87354e20009a6e03c9efdca9c0b51dea17ad71d5 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -373,10 +373,10 @@ struct sock *__inet_lookup_established(struct net *net, sk_nulls_for_each_rcu(sk, node, &head->chain) { if (sk->sk_hash != hash) continue; - if (likely(INET_MATCH(net, sk, acookie, ports, dif, sdif))) { + if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) { if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) goto out; - if (unlikely(!INET_MATCH(net, sk, acookie, + if (unlikely(!inet_match(net, sk, acookie, ports, dif, sdif))) { sock_gen_put(sk); goto begin; @@ -426,7 +426,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row, if (sk2->sk_hash != hash) continue; - if (likely(INET_MATCH(net, sk2, acookie, ports, dif, sdif))) { + if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) { if (sk2->sk_state == TCP_TIME_WAIT) { tw = inet_twsk(sk2); if (twsk_unique(sk, sk2, twp)) @@ -492,7 +492,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, if (esk->sk_hash != sk->sk_hash) continue; if (sk->sk_family == AF_INET) { - if (unlikely(INET_MATCH(net, esk, acookie, + if (unlikely(inet_match(net, esk, acookie, ports, dif, sdif))) { return true; } diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 53342ce17172722d51a5db34ca9f1d5c61fb82de..aa9f2ec3dc4681f767e8be9d580096ba8b439327 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2563,7 +2563,7 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net, struct sock *sk; udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { - if (INET_MATCH(net, sk, acookie, ports, dif, sdif)) + if (inet_match(net, sk, acookie, ports, dif, sdif)) return sk; /* Only check first socket in chain */ break;