From patchwork Thu Feb 29 11:40:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576986 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A0B67C0AF for ; Thu, 29 Feb 2024 11:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206823; cv=none; b=SRWWwVK0Ox4uTp39mCqIo6BTteT011tYBHg66hQtsdgr/A6ZFOd6FzYi9zFPUUtorqbxZDbWeNjcrcKq/UM95x2Sn5yeDncQSxcy0okIMrApP/2p4oMrByAG7X+1Hs8TaR1vOIK0kGDJJi+Is91yONU97v4GP1bqt1QWDvEaWe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206823; c=relaxed/simple; bh=cVyyoC8uRMiNRa3QRFMIsF1AmOsna8lSTCGY6/0I67o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kQohmNOv//XNp3I9YYEv23LQ+CxEkqtfg6w/Vp1aKCxnDpHfGxn3gi7EmezIPaID1cMHlGt9pXVELYoS8S8WlcdmJiZSFC5Sm7sJeppovKzt5AwVaBV9q8bLtnusR9RIeMzvBQ2Oc0UYUfET1SgkCag+GhxLm1xp6cxSGflOUr4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3M9PoHcW; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3M9PoHcW" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60904453110so10153477b3.2 for ; Thu, 29 Feb 2024 03:40:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206820; x=1709811620; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hGkMaRngnW9wJm+sPsEK9bpLE7Z7kQJwZ6Bj2PHOdBE=; b=3M9PoHcWoPZxXjzO5+DhMMA98i8KYNuugQJnoCAmn+PVtGJUsLRxwfzrEIv7pVxm5k jzKjbN/IxPNbldWDqGWydGdIet1EDjEiFgXwhJiBDVscX/v+9NGkkGLtPOLnfmlbmNC/ RI09/YT7RAMlPYZvPNXNu7W+PHM+AsQ2P7bN9qV0OEFVUJYvqaZQEPn8Qo9kaEbif086 kfZvGMNp5yAulvsWA48voWq5n6WfibQQSrG8QEhF/aP8j+SqB3WHHpgCm7r1dfyz1LAE zxwZWf1+KaPC1TV/qYw/gumAKMJEJ/YmNhCsMuYVnQZ1n43DAw/hg4+xEd7icBkLMwi5 LcxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206820; x=1709811620; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hGkMaRngnW9wJm+sPsEK9bpLE7Z7kQJwZ6Bj2PHOdBE=; b=QyBQ3FhswdrrE8ASorsyUhvS0OOLo5+e9jaMWZVhcSm9q8n25ZFwU96kdRX7UXc/Qc i06b4sGh97tQpTZJNilYzc1b/l1ZArHKEMeP3Znoz6b5jNbne1QDfbcyZIFwuB0WmKom /PPwOl/w7sga7INgdAFjEkrR/R1CAiK5oFqt/6FtteyhpJdvqjaRO7M5ZnBPyBwnjTQc Sw4Dge8XdVrhLw18ADwI/tKKrAs1mT5y44gT0VkzHosET8Qwvq0wUIkhEc9qrx+Rj212 yz7KKE/3GoZQRHP2bIQwbxW/hZ+fHApYXAgQHLlape/dNeQkOI7epxlPfvE+uJHWJiLn uAJQ== X-Forwarded-Encrypted: i=1; AJvYcCVjP70LguWVk4m0/5mUYbtipHwUHyIy3PV6moXyLY8nL5D1ZXCQgMMwJVZf8L/UhSjlMh7zZevzSS8vkkgIM+r23o14mMtd X-Gm-Message-State: AOJu0Yy8nXy7XjMI4RwVqNneofLF+DU8AVLcQHCp+3vDvvWEM06cFc2N KwbkY7uyLeiKR1cFfZfvbSjGd/YdWnDEG0QMtSO13rc70L899S8aMEbaJgXvE+7uYZKvpOtJl4q GQJsBFGjImw== X-Google-Smtp-Source: AGHT+IHGUPWZeZfbfmghHKLFz20FXGw5V1Rq9nRs6cQHIAqXEzdiZo93bD4NwqlZq9Vz4HX1UJIZ17yZ7JF7tA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:690c:c0d:b0:609:3834:e0f4 with SMTP id cl13-20020a05690c0c0d00b006093834e0f4mr455681ywb.7.1709206820342; Thu, 29 Feb 2024 03:40:20 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:11 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-2-edumazet@google.com> Subject: [PATCH net-next 1/6] inet: annotate data-races around ifa->ifa_tstamp and ifa->ifa_cstamp From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org ifa->ifa_tstamp can be read locklessly. Add appropriate READ_ONCE()/WRITE_ONCE() annotations. Do the same for ifa->ifa_cstamp to prepare upcoming changes. Signed-off-by: Eric Dumazet --- net/ipv4/devinet.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index af741af61830aeb695e7e75608515547dade8f39..1316046d5f28955376091d9e02ab4594e19fbd09 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -713,13 +713,14 @@ static void check_lifetime(struct work_struct *work) rcu_read_lock(); hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) { - unsigned long age; + unsigned long age, tstamp; if (ifa->ifa_flags & IFA_F_PERMANENT) continue; + tstamp = READ_ONCE(ifa->ifa_tstamp); /* We try to batch several events at once. */ - age = (now - ifa->ifa_tstamp + + age = (now - tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && @@ -729,17 +730,17 @@ static void check_lifetime(struct work_struct *work) INFINITY_LIFE_TIME) { continue; } else if (age >= ifa->ifa_preferred_lft) { - if (time_before(ifa->ifa_tstamp + + if (time_before(tstamp + ifa->ifa_valid_lft * HZ, next)) - next = ifa->ifa_tstamp + + next = tstamp + ifa->ifa_valid_lft * HZ; if (!(ifa->ifa_flags & IFA_F_DEPRECATED)) change_needed = true; - } else if (time_before(ifa->ifa_tstamp + + } else if (time_before(tstamp + ifa->ifa_preferred_lft * HZ, next)) { - next = ifa->ifa_tstamp + + next = tstamp + ifa->ifa_preferred_lft * HZ; } } @@ -819,9 +820,9 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, ifa->ifa_flags |= IFA_F_DEPRECATED; ifa->ifa_preferred_lft = timeout; } - ifa->ifa_tstamp = jiffies; + WRITE_ONCE(ifa->ifa_tstamp, jiffies); if (!ifa->ifa_cstamp) - ifa->ifa_cstamp = ifa->ifa_tstamp; + WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp); } static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh, @@ -1676,6 +1677,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, { struct ifaddrmsg *ifm; struct nlmsghdr *nlh; + unsigned long tstamp; u32 preferred, valid; nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm), @@ -1694,11 +1696,12 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) goto nla_put_failure; + tstamp = READ_ONCE(ifa->ifa_tstamp); if (!(ifm->ifa_flags & IFA_F_PERMANENT)) { preferred = ifa->ifa_preferred_lft; valid = ifa->ifa_valid_lft; if (preferred != INFINITY_LIFE_TIME) { - long tval = (jiffies - ifa->ifa_tstamp) / HZ; + long tval = (jiffies - tstamp) / HZ; if (preferred > tval) preferred -= tval; @@ -1728,7 +1731,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) || (ifa->ifa_rt_priority && nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) || - put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp, + put_cacheinfo(skb, READ_ONCE(ifa->ifa_cstamp), tstamp, preferred, valid)) goto nla_put_failure; From patchwork Thu Feb 29 11:40:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576987 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 140F47C091 for ; Thu, 29 Feb 2024 11:40:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206824; cv=none; b=BIxFvsUt4ABMbUu3LP2IN8BmX5/jR+2sKr9ll7/3gpzBmhaKVD4LmAJWZ9OQCUrocUAcEGJ/mJWn99U5ZTKInxrq+8vNT9FY9bW9czfMlOSSikIf4RNrXZ64RIgUwy5bUnPv9OGxgk7Mm6XhxsMx3nKHDRdRQhnCUE29HICqlxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206824; c=relaxed/simple; bh=C4PmBXoSoBa6O7y4Dd0rpk2vmISWK2TWMUla+sPPANw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SxaZ3Q/CUgrJJJumiH1u74YFa/J3TljCV76/9n7czGM7gLJ1MligEa3bthzpkes3eCnPlofVMqHNTFMHg1V1395Pg0lEnJymhekjVC7E6QuofATf3x/RGw1fdP2VesuWGwMxPzHPJu+26gqSO8lmaFoiscO47YyUqDcLOdDqP/c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gaFgSSfe; arc=none smtp.client-ip=209.85.222.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gaFgSSfe" Received: by mail-qk1-f202.google.com with SMTP id af79cd13be357-787ba4f2b68so92695285a.2 for ; Thu, 29 Feb 2024 03:40:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206822; x=1709811622; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0OUOZny0/QFYcWKXYQ6rJPZIn/Ju/wuUkzay864jA94=; b=gaFgSSfed0GGu7+L7wVhG7EiVEAJgE9+5CdvuKbxCd/u+MogoguNlOyZ+B+fPqOMBF Fva27nEOwqlwo25ZarXnIEKT+KN+ggjqiLUePKKlHMbF35w23+J2Mzak7c69mzXZ0len 0Jx3Tv0+1r0MiY/fqDAoPU0JG8NW47rBjC6rmvWC/CflI6pVpvv6vavqo693GId2l4cy p2zuEuUgdEzUdMLlTnX6QN9nE3aQU9UiPD0vt8hkUW29CrMdyH6kmL3ELWGOxJoH8U54 C8Yje9isJcgC9keYNDZOKuG77jXbc/GKAhMOTSYWDEvMH77qNz/v0mlgriAC6OF43ujY QEBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206822; x=1709811622; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0OUOZny0/QFYcWKXYQ6rJPZIn/Ju/wuUkzay864jA94=; b=o39afDLCFvINbbJk+dU/spBsPwNxZmHr5Ysi1hMmMLGqSZMzI3Xds2GQs7Pv0oL0Su CZykGL2CQ6rRJYQEzIBfTK/y2CxBeAQdm1OO2abiou7GAEkDlchv9GUg0XuiJFAmtVyK acRA8NgbLyj2/ZcULqmxFigKqGfwpigWzX2je2NAzAwqsWrzyxRbwDblOWDX6do7Hdeu TKRObJlNHQf/fOrKSeqRF27u/btobvmV2IVwqBIIhDKqUnILySPQJMBPpczC+YrDue+k pz0sXKLPBakUu1D4b0D4tcJMMF5/CeqdcpiM3vl3hJUnJtLO5Jz5HieR5Dj2LASryDi+ a3fg== X-Forwarded-Encrypted: i=1; AJvYcCXYfDjA8GgB/zD2FJXVzSGv2SReZOe66qrQm4Jpy4Gt1SPitr0JTibDVoBcYsqm6kN5cSwLIiqtlekvUTISwDCVPDUjgL8r X-Gm-Message-State: AOJu0YzXS2jsgq7YjaPhUxBt7X4CBy7FF/QWYU1faCf62flNIYfbim1U EeE+qUKjGhSqYFmaUr9M+CWBBqNiims+3n0qEnCrBjDUCephjFWMV4LgRNIdAn04aGGCWEyBRr+ +KXwnuE06Qg== X-Google-Smtp-Source: AGHT+IFtm+VTVqGyyvOt4pvpxf6gE+YeIbNAO15ZyHYUPSXdabqhOyvBqi5S5NxhiThrEDRK2fXdQAqNBlHUZA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:620a:2948:b0:787:d89b:cf61 with SMTP id n8-20020a05620a294800b00787d89bcf61mr20151qkp.5.1709206822029; Thu, 29 Feb 2024 03:40:22 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:12 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-3-edumazet@google.com> Subject: [PATCH net-next 2/6] inet: annotate data-races around ifa->ifa_valid_lft From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org ifa->ifa_valid_lft can be read locklessly. Add appropriate READ_ONCE()/WRITE_ONCE() annotations. Signed-off-by: Eric Dumazet --- net/ipv4/devinet.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index 1316046d5f28955376091d9e02ab4594e19fbd09..99f3e7c57d36ff028edadd4efd66d996ddc5d9b4 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -714,26 +714,26 @@ static void check_lifetime(struct work_struct *work) rcu_read_lock(); hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) { unsigned long age, tstamp; + u32 valid_lft; if (ifa->ifa_flags & IFA_F_PERMANENT) continue; + valid_lft = READ_ONCE(ifa->ifa_valid_lft); tstamp = READ_ONCE(ifa->ifa_tstamp); /* We try to batch several events at once. */ age = (now - tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; - if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && - age >= ifa->ifa_valid_lft) { + if (valid_lft != INFINITY_LIFE_TIME && + age >= valid_lft) { change_needed = true; } else if (ifa->ifa_preferred_lft == INFINITY_LIFE_TIME) { continue; } else if (age >= ifa->ifa_preferred_lft) { - if (time_before(tstamp + - ifa->ifa_valid_lft * HZ, next)) - next = tstamp + - ifa->ifa_valid_lft * HZ; + if (time_before(tstamp + valid_lft * HZ, next)) + next = tstamp + valid_lft * HZ; if (!(ifa->ifa_flags & IFA_F_DEPRECATED)) change_needed = true; @@ -810,7 +810,7 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, timeout = addrconf_timeout_fixup(valid_lft, HZ); if (addrconf_finite_timeout(timeout)) - ifa->ifa_valid_lft = timeout; + WRITE_ONCE(ifa->ifa_valid_lft, timeout); else ifa->ifa_flags |= IFA_F_PERMANENT; @@ -1699,7 +1699,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, tstamp = READ_ONCE(ifa->ifa_tstamp); if (!(ifm->ifa_flags & IFA_F_PERMANENT)) { preferred = ifa->ifa_preferred_lft; - valid = ifa->ifa_valid_lft; + valid = READ_ONCE(ifa->ifa_valid_lft); if (preferred != INFINITY_LIFE_TIME) { long tval = (jiffies - tstamp) / HZ; From patchwork Thu Feb 29 11:40:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576988 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 674AF7C6EE for ; Thu, 29 Feb 2024 11:40:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206826; cv=none; b=qABxB/fOQ2CmyBwX7MiHS25X+DPu61vbbHOAGEt7KfOmDyHL6DVrLvXSDH4iDYpL6KyGXMYsTw+LSwduCLyT34wAy5TZvIwajU4pJpY7PztJRZDdYJJyGhnVFh/r6JDoeL4hlmu+vq6WwhbLewzfzCveBR/9mC8MhsTW5Rng3ms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206826; c=relaxed/simple; bh=+gHYWJhj0cAfCmOQkc3zktPP+uYTv3Vnj3ST7p8Yiq0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IdWr/ncKQLfIBE/QHAjeKrcPjAufwVRtpNWzFH2ioSJVIk/FHHG1JeX0SnsHPi0RIHuWzv8bXvlCy1GfEbma6ekdJ3OTAZ6gTHF9v02vKvkyFsNDnX5SL1w59uloRVoiLZnE0jY6wZxEBkmv+gRvxuFfiE1k2yb+CIJ5H0SZnVY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iuunyJdO; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iuunyJdO" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcbfe1a42a4so1555276276.2 for ; Thu, 29 Feb 2024 03:40:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206823; x=1709811623; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R23jv+b4v7pcWmxlj/OuBFH2S+pqQwn8TVi6lbSuZ1A=; b=iuunyJdOeAMGhw+DMIi+Danr1dfPbtqLdkCCX3s8itc3aGD/ot5zrCTxHTsoIWshoX MBriRjA9ZBAyqIvvXjqAPrVO3d1llD37Eoe5BcuaC+wv49ZeUgKZiFxDeOm6tOydloav 2anOcbttP79hihX7dNX74oA6Rebnxwcbc1uVnikMjR4WRFKxRLX65zlY0JPnO82MUoy/ XYei2J8psBYYALLDOtGy1O5+ijVngfxCtlV/45uUYyyh+49OxL2MXYR0EYvzBBKdJ1sD 3lurLBVp2hVwhUuenoX5CGBmfFLKTifdcUpvZMWn4fP6sqo6bVoyqLe56qgGwzli63N9 kNQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206823; x=1709811623; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R23jv+b4v7pcWmxlj/OuBFH2S+pqQwn8TVi6lbSuZ1A=; b=McABvbVPOapCLQkY2bDJBOphrq0euRjNRHFfT6SgMZTFbddk/6oFhnW6PIIvMzeO43 HPCHuQjWq5xTR1JxOoY23Z680loz+Jt9rwqGtgA3eTxHTGQSC7id64TjzV8J9ihlIeEZ av8+6Cg2Q13T6+bgwWP6dsk0Mx0TuKJNtiiBA3QUoRNdnqCMDCtAG6B/QrsyGF+hcm+W QkAlf2KDuGTRTb3MQQfJkwDhoQppiU9KMnfao76b7Z/1ZB5UQIzAvCATv1h3fCeMI98l ETp2S9dsiStFhY7m/IzAVuATbqAXvRLCnhU0RmCBTHR3paWrvcQY1048bJjeyq9iNj5s EZzQ== X-Forwarded-Encrypted: i=1; AJvYcCWupXUDcrljMC/w4Kh/DdxC86zthugK1Bk8t4flyAzJD0OmR5iyFQcF8mRV/kcUx885p7xtdlTTQK9u0iJ6Jns8aYA2JWQv X-Gm-Message-State: AOJu0YwlnnPijT2c7Jspd0cvSePYQ7MtuS8g7jEKwT9hBQf++HNJE7UL F7DagT+jBs7vnzLqPlur1M3Av+dZDOnRA0oDirF/Mo96eVIeTfuLD+CUrKqcGBJfM0DnIA5y26a RaP+SbLj5HA== X-Google-Smtp-Source: AGHT+IEQthekvX7tNV3MwE5K+8LyBEo9tw7AVeEk+4itItQFXrWKswQXtVEg8kb0V3y2LCgoPKnYs+kHu1EaZA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:1102:b0:dbd:b165:441 with SMTP id o2-20020a056902110200b00dbdb1650441mr524047ybu.0.1709206823470; Thu, 29 Feb 2024 03:40:23 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:13 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-4-edumazet@google.com> Subject: [PATCH net-next 3/6] inet: annotate data-races around ifa->ifa_preferred_lft From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org ifa->ifa_preferred_lft can be read locklessly. Add appropriate READ_ONCE()/WRITE_ONCE() annotations. Signed-off-by: Eric Dumazet --- net/ipv4/devinet.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index 99f3e7c57d36ff028edadd4efd66d996ddc5d9b4..368fb56e1f1b2e3b7888f611a3f113f3bd5fc2af 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -714,11 +714,13 @@ static void check_lifetime(struct work_struct *work) rcu_read_lock(); hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) { unsigned long age, tstamp; + u32 preferred_lft; u32 valid_lft; if (ifa->ifa_flags & IFA_F_PERMANENT) continue; + preferred_lft = READ_ONCE(ifa->ifa_preferred_lft); valid_lft = READ_ONCE(ifa->ifa_valid_lft); tstamp = READ_ONCE(ifa->ifa_tstamp); /* We try to batch several events at once. */ @@ -728,20 +730,18 @@ static void check_lifetime(struct work_struct *work) if (valid_lft != INFINITY_LIFE_TIME && age >= valid_lft) { change_needed = true; - } else if (ifa->ifa_preferred_lft == + } else if (preferred_lft == INFINITY_LIFE_TIME) { continue; - } else if (age >= ifa->ifa_preferred_lft) { + } else if (age >= preferred_lft) { if (time_before(tstamp + valid_lft * HZ, next)) next = tstamp + valid_lft * HZ; if (!(ifa->ifa_flags & IFA_F_DEPRECATED)) change_needed = true; - } else if (time_before(tstamp + - ifa->ifa_preferred_lft * HZ, + } else if (time_before(tstamp + preferred_lft * HZ, next)) { - next = tstamp + - ifa->ifa_preferred_lft * HZ; + next = tstamp + preferred_lft * HZ; } } rcu_read_unlock(); @@ -818,7 +818,7 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, if (addrconf_finite_timeout(timeout)) { if (timeout == 0) ifa->ifa_flags |= IFA_F_DEPRECATED; - ifa->ifa_preferred_lft = timeout; + WRITE_ONCE(ifa->ifa_preferred_lft, timeout); } WRITE_ONCE(ifa->ifa_tstamp, jiffies); if (!ifa->ifa_cstamp) @@ -1698,7 +1698,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, tstamp = READ_ONCE(ifa->ifa_tstamp); if (!(ifm->ifa_flags & IFA_F_PERMANENT)) { - preferred = ifa->ifa_preferred_lft; + preferred = READ_ONCE(ifa->ifa_preferred_lft); valid = READ_ONCE(ifa->ifa_valid_lft); if (preferred != INFINITY_LIFE_TIME) { long tval = (jiffies - tstamp) / HZ; From patchwork Thu Feb 29 11:40:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576989 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D7447CF10 for ; Thu, 29 Feb 2024 11:40:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206828; cv=none; b=nEdXV3CH1tgQtYBdvBoR/B15FcBxPYovGwP6TqXvc6kd9cz0DtH4ExPoWroLEZYq0Z2N5fK2vd1AZKSoBVHrmTpKO2YjQSVTYwDz+KyNb8F4x6DQ9f7SvedNnAgzYwsShory8nRX+aUWm9EhYnyEIBd1Dr5HhwvYhdQai7gpMWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206828; c=relaxed/simple; bh=X3fIBea3nDCpfhmsoE78Zjk407CjrUMlH3E2Ui0sje8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D1o4KGv+eWQg9VPIktZh2ny7VxSym3idUWgEMTJ8Pp1L/gKDWTxaLJe344o4q5N0bmO6bDsVwuT9rJ2bxvSuRpgqhJdXJQzDDXIuELz9JSv2r4KBTlRaP/mzanqHotetnsqkuZR/FNEm3L9OvfamwCgJd3y6GlzOjQhFNzRIZpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0DtQoDPH; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0DtQoDPH" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc74ac7d015so982532276.0 for ; Thu, 29 Feb 2024 03:40:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206825; x=1709811625; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zw60ERsK7mwNepnjWiQI55oc8/dMLCRYKkYFuuWsyf8=; b=0DtQoDPH7K6J+vowYd8x7160ubUfammUQBTq/vJwB7TLRLuR7UM+ZCF94oPWyU4Zts R4kFGcwptWip5ivu3Y3I9nhYF5YQgbPpM/XH49mMN7nus5MogypqpRgVdiiEVg1Aabke iZ5xzXOMvErc+vg5FrsUDIE6WWJCdv26jsgAO1s7VkL9NBXL7jK9RPtON0SH5OPpwldP 4bo0VJYC7jMNhArBQteVus1cLnRjfWYoa9n3xV4a+5DUj9uvYaoqT8tTRkLoaPD16o5p yfynvO+kSqhWRqIC1ZAHKWn9sc6ZLAv4I4hSexV6xKvf4TzRJTqEfKx5/tAetDH67/uc 9C9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206825; x=1709811625; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zw60ERsK7mwNepnjWiQI55oc8/dMLCRYKkYFuuWsyf8=; b=sjanN/4NU0SWOdSabiwy/CpbzpKIq5ZDKXNbuJkt3AVUUmBDyT/cz/IRgNoVvwjyI9 b8zayKCikIEoRyVVkLrLmlK0/QUF42qtwE5nZC6blYmWbT6hZTFeduACl2rBsEwao1mb i7tpqGnafedE3sJjFAEPvfDG1W+aAW6gObH2MfMHHgqlyUuxKimRh364b51sePVG6vBg +TS5VGuapGozxdsYdIN+QuYZ2Zipik2HZ2ilrZYyfeTAHG1/LS1RrBfsrunTaVu91Mnf PRNY6axYVk6KyNGynChC2VOJWIkab2Z5l0PaBhv+Sb5C7WsQR7gpNYesFc7s+Dy5PuY0 NuNQ== X-Forwarded-Encrypted: i=1; AJvYcCUp/uM5zOVXeALKtXmjlb4VJMvCmcHa7l3q3mgzy0sLIsMP+GFKaEvxXP8UZUXW6BZMcR6UcPyqAELwjANyuw7b8l1wtL+P X-Gm-Message-State: AOJu0YwffMXIL7S6DmqAvOwfmFmWpu7mkuDhfVM/36cGxCSxR9hb9KTI C5Z3o/X3TuYTs1o9A2gmzIYSLWKXcXsm+2TX7Sr2hI1pVUfkFryXmqOdb02fApRbV4d2TOjul1v J3rYaI3HAWQ== X-Google-Smtp-Source: AGHT+IGIQ9Cb1gygYNxUwPVzdYxuLXpcYuwLyPeIuCmT1aATR4TPoHbdxloPfR2TQJ6ddcUVs2TkpqQKENdV3A== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:e90:b0:dcb:c2c0:b319 with SMTP id dg16-20020a0569020e9000b00dcbc2c0b319mr66651ybb.9.1709206825036; Thu, 29 Feb 2024 03:40:25 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:14 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-5-edumazet@google.com> Subject: [PATCH net-next 4/6] inet: annotate data-races around ifa->ifa_flags From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org ifa->ifa_flags can be read locklessly. Add appropriate READ_ONCE()/WRITE_ONCE() annotations. Signed-off-by: Eric Dumazet --- net/ipv4/devinet.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index 368fb56e1f1b2e3b7888f611a3f113f3bd5fc2af..550b775cbbf3c140c66e224c69996df7051b3d36 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -716,8 +716,10 @@ static void check_lifetime(struct work_struct *work) unsigned long age, tstamp; u32 preferred_lft; u32 valid_lft; + u32 flags; - if (ifa->ifa_flags & IFA_F_PERMANENT) + flags = READ_ONCE(ifa->ifa_flags); + if (flags & IFA_F_PERMANENT) continue; preferred_lft = READ_ONCE(ifa->ifa_preferred_lft); @@ -737,7 +739,7 @@ static void check_lifetime(struct work_struct *work) if (time_before(tstamp + valid_lft * HZ, next)) next = tstamp + valid_lft * HZ; - if (!(ifa->ifa_flags & IFA_F_DEPRECATED)) + if (!(flags & IFA_F_DEPRECATED)) change_needed = true; } else if (time_before(tstamp + preferred_lft * HZ, next)) { @@ -805,21 +807,23 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, __u32 prefered_lft) { unsigned long timeout; + u32 flags; - ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED); + flags = ifa->ifa_flags & ~(IFA_F_PERMANENT | IFA_F_DEPRECATED); timeout = addrconf_timeout_fixup(valid_lft, HZ); if (addrconf_finite_timeout(timeout)) WRITE_ONCE(ifa->ifa_valid_lft, timeout); else - ifa->ifa_flags |= IFA_F_PERMANENT; + flags |= IFA_F_PERMANENT; timeout = addrconf_timeout_fixup(prefered_lft, HZ); if (addrconf_finite_timeout(timeout)) { if (timeout == 0) - ifa->ifa_flags |= IFA_F_DEPRECATED; + flags |= IFA_F_DEPRECATED; WRITE_ONCE(ifa->ifa_preferred_lft, timeout); } + WRITE_ONCE(ifa->ifa_flags, flags); WRITE_ONCE(ifa->ifa_tstamp, jiffies); if (!ifa->ifa_cstamp) WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp); @@ -1313,7 +1317,7 @@ static __be32 in_dev_select_addr(const struct in_device *in_dev, const struct in_ifaddr *ifa; in_dev_for_each_ifa_rcu(ifa, in_dev) { - if (ifa->ifa_flags & IFA_F_SECONDARY) + if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) continue; if (ifa->ifa_scope != RT_SCOPE_LINK && ifa->ifa_scope <= scope) @@ -1341,7 +1345,7 @@ __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) localnet_scope = RT_SCOPE_LINK; in_dev_for_each_ifa_rcu(ifa, in_dev) { - if (ifa->ifa_flags & IFA_F_SECONDARY) + if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) continue; if (min(ifa->ifa_scope, localnet_scope) > scope) continue; @@ -1688,7 +1692,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, ifm = nlmsg_data(nlh); ifm->ifa_family = AF_INET; ifm->ifa_prefixlen = ifa->ifa_prefixlen; - ifm->ifa_flags = ifa->ifa_flags; + ifm->ifa_flags = READ_ONCE(ifa->ifa_flags); ifm->ifa_scope = ifa->ifa_scope; ifm->ifa_index = ifa->ifa_dev->dev->ifindex; @@ -1728,7 +1732,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) || (ifa->ifa_proto && nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) || - nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) || + nla_put_u32(skb, IFA_FLAGS, ifm->ifa_flags) || (ifa->ifa_rt_priority && nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) || put_cacheinfo(skb, READ_ONCE(ifa->ifa_cstamp), tstamp, From patchwork Thu Feb 29 11:40:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576990 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3F0C7C6EE for ; Thu, 29 Feb 2024 11:40:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206829; cv=none; b=OkkJPGg2M9ipRRppOORD9ubqjbiZne/Mtq+4Q++VciA99jyVZLjJSDrlvj4yU1LchxN+BFORSBXZDTXj5KSB3+XjuuS3Z05l3EMvzxpg7pltzliBJncWetzqkkMOSfe2++49RczpTCBlsShw8tr2zoPkJEbzoTZM6SnsUSvD/q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206829; c=relaxed/simple; bh=0YBJA22pQ9qE1ccUmGRibhk+TabLTZK0752CMfNwRyM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mUOLTASgSOeFX1NHbH7PwjjOfuyEl5Xp8mZkloiwvR24QvSHCjKxNoMNH+hX4MD4+d1+rVI0OVNy3IdiLPARj6Z84M4nKCe4bTMW/JJ5mNrh8lXGkUlUHGCsMK8F8A3mUAymD9RztXSuoc/4JUqxfQaIj0exu1uw172Hik5SyxA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ADYsdvWU; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ADYsdvWU" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcf22e5b70bso1425330276.1 for ; Thu, 29 Feb 2024 03:40:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206826; x=1709811626; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bweT+L7/wL6TdY/2iHKZCZXbLKyR1rcYmUPmhmttVEE=; b=ADYsdvWU1hSjS1XYUucpBwMuPHSW2EmJMCKCUae0CbNy1tvsJDWNTKEThPHXOSHg35 gtyjJ11J+isE/JKTO5y8ZDEbhLyiu6yuXajTB91te2uz8Lw/Yj28CeduGdaUHbie63iw 8r0Km0266iOzFHpx/TxPcLI5Q+wnyxXBMeDh3DE8AKcKXOz6omwW+pMlByxAXxq8gYsh WY3DwwOjFxhLw43LkPLOGA9coRK7IYV+25Vv9SkjNxW9BKL15V0Z9VGhvZbY4krhY4FP WSFbBxktLW1jSEH9xcrJRel3E17ux6+ZFoYaqQ/U51iiuAJhoakaPV8hhEo/NwEqSAiq AzTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206826; x=1709811626; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bweT+L7/wL6TdY/2iHKZCZXbLKyR1rcYmUPmhmttVEE=; b=tbBmC9fcs8d8hqMDDkNBkUv8hzgbUM4f5ug1Yhe6vOIxzpdzHl8sWFlvPVFBQVEsdL Qbzodz3csgG+u+eNohUuUqF3Sm9+SGXGcfiYmoZcOn2WheJ4ZlL32NaKSfrzpTZOINe1 MmA6IFtzncHluoOeHAj3nSQct9FIuYWtzC49FlwgdAhETeC4YFkOCAZUp/Hc5wegdDc5 kKAD2qlJxpDkSvoYwe5c2A67VNx2ESXxohxi/tWd3+D+p0cCBx1MwFjZCyAoNGnmUKAP 0aj2eykSza6akCe1cE+PBhsezgzRazVCwgPmR7KARBXPYCnVlI9ZBKDHHPgJLKC182s+ PO2w== X-Forwarded-Encrypted: i=1; AJvYcCUD8hy2MN7jfLMG9x95u5IH7nq33NK7MvElqy1APnDotGig740JlT9z7Q25tiD/xounUSWpWXCNm3mE9JP5aZ4v/woDcJCN X-Gm-Message-State: AOJu0YwopdLg2A0FqBqWY4QJNx1DnxPm2BgYb+AqFM7YcPgeDw1jl0zU 3SoAsuE1lDzMURLHIz9GnQMskb+rT4RKTuVVKjSHUkiRoYk5qhPRPOQAVCxsYIGj528DwHofi0/ Rj+2rOqil+A== X-Google-Smtp-Source: AGHT+IGOqsF0ldtQvO+F7+VmQ1WQuQHAfrYPddGNVdFARqyn/VSTDo6faDmemcuCpH5t5qzkv1gEMukN5Xv0Sw== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:a8a:b0:dc6:44d4:bee0 with SMTP id cd10-20020a0569020a8a00b00dc644d4bee0mr75218ybb.7.1709206826712; Thu, 29 Feb 2024 03:40:26 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:15 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-6-edumazet@google.com> Subject: [PATCH net-next 5/6] inet: prepare inet_base_seq() to run without RTNL From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org In the following patch, inet_base_seq() will no longer be called with RTNL held. Add READ_ONCE()/WRITE_ONCE() annotations in dev_base_seq_inc() and inet_base_seq(). Signed-off-by: Eric Dumazet --- net/core/dev.c | 5 +++-- net/ipv4/devinet.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/net/core/dev.c b/net/core/dev.c index 053fac78305c7322b894ceb07a925f7e64ed70aa..873e095e141db3e631e51763435ddafbf0d280af 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -180,8 +180,9 @@ static DECLARE_RWSEM(devnet_rename_sem); static inline void dev_base_seq_inc(struct net *net) { - while (++net->dev_base_seq == 0) - ; + unsigned int val = net->dev_base_seq + 1; + + WRITE_ONCE(net->dev_base_seq, val ?: 1); } static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index 550b775cbbf3c140c66e224c69996df7051b3d36..2afe78dfc3c2f6c0394925f1c35532a2dfd26d71 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -1837,7 +1837,7 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, static u32 inet_base_seq(const struct net *net) { u32 res = atomic_read(&net->ipv4.dev_addr_genid) + - net->dev_base_seq; + READ_ONCE(net->dev_base_seq); /* Must not return 0 (see nl_dump_check_consistent()). * Chose a value far away from 0. From patchwork Thu Feb 29 11:40:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13576991 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21D497CF14 for ; Thu, 29 Feb 2024 11:40:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206830; cv=none; b=K1/Gb5DczGwMXZ6QBEn4IQFW87GtfP3k+4Mjkp9A8i0P1MwO2I0fgZwv4ce35yD91srUQQ0gA0+2J2h8WgvDHe202XXb3yWkcetejZyVc8FJM3voTGI7cbGN6pVDuhp+u71BIQTDWHgIYaZf+nZv4v43yUw86xfxrUNaeiqWAXg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709206830; c=relaxed/simple; bh=Q/d6r2e7neNbuMCijf5MwA6RNUg58qGEZ3RyhMV5DsI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bXwIqhTCZUCgxrWVIxDc8jEiZiXMg9MR5ux2K0sB79j9NqeZ07MCGGiCDzfA7zP1tQs0niLjZIIt1G7Rv4RMe0UTqj8xlHETR+oHSrJxXw0OvSvRyz3tDl5++7Ig6NCVNr8hG7BiQPAAxHDxsY8YxCZb/G0QHzEzT/Pj6FR9R2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oGjab8e8; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oGjab8e8" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcbee93a3e1so1321303276.3 for ; Thu, 29 Feb 2024 03:40:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709206828; x=1709811628; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qMnvZ5q5D73jPy3slV8eCUFNP+BTjJ5VxCVihBSQwCc=; b=oGjab8e8SZpdvmAllGB2YmerNalgLiw9OHCimX5k91i4zD3XWGtReU9CR6tPq5HYAF 2mumqZMuzcUsoFnHmU8awGZVuZz6xeytWLFtsd/+7P0GPQ/NEOIWSrk5QrZkFdINuoWP AZ7G4wdNBF3b+WcFQHG4MuQ1g6fO2DUYjeIMOp8W+i/GSeY775v6tJe66wz9bOtA66Kc UvQWf5sTM5Lbs4OJ70ilUqNoQXjFADUb2gSzRrv873xYfL2KtlH3ZZjYeGvS+R3Mwm9m g5tduA35bNMcVFCHTlC4HDtZJUiOuMp2Xsg889rWv+Rxxvt6iMuno5FZE7eLPUUP5f5B FCjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709206828; x=1709811628; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qMnvZ5q5D73jPy3slV8eCUFNP+BTjJ5VxCVihBSQwCc=; b=r9XAFy/oAu4D0jc4DAaVTsTriTcItAwlgcxDKOFLxXh91CereTZRaC4VHItXS5m8qm bfBZLacL9TElvF7v0ZX12nxbV+qnxmwTksekcxShxKty8SPnP7vjGsPMvjJrcSdu+Txd DKLnAxCH2K/5VKOsg8AC+Q2ixg0XFbpNAJh7YbEDnIcp1b+0PtKdGqrxOdkNAijlInlL xXD08IoVkvuodMhNsVFruQWVrZ2OGDyTyi25OtEnXzOXC+YvK//zKXhc8vZN/9APmtdD 1xDadtV9KMdTRcpk93UffhzsChx5zMhnzgq5zpsBJ+QIQxe/lECeNoMoO/QRsIstpdKc BX5w== X-Forwarded-Encrypted: i=1; AJvYcCXnEkE5OEZw/XoTaWRmMjNAA2WF83veCjaPwfwZJU6RQLbhxVc1GkPLQ4tYS+Ni1Cfr+Psx/j5e7U5SUKHDrcuJrU6BP6eu X-Gm-Message-State: AOJu0YwkseASpSQh/I89lim0fYDVqpNth+CDpVwzYUdDzO7W60c/bAZd uLowkJTn0JeuZAcuciZyKArIehe3ApdUWqefopHSGcLd9uYxDccXfeSIEGA0Z1H7LueeEusOCA/ JtIAoomtJew== X-Google-Smtp-Source: AGHT+IE6dGq28hgtJQ9z22W6R9+8cRRmMj6fIuNi3TVltQgiVgVLhmpVj0ew/Qtlln6wuWVW3T7NsMF/LjpdFg== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:1244:b0:dc6:e20f:80cb with SMTP id t4-20020a056902124400b00dc6e20f80cbmr75598ybu.3.1709206828330; Thu, 29 Feb 2024 03:40:28 -0800 (PST) Date: Thu, 29 Feb 2024 11:40:16 +0000 In-Reply-To: <20240229114016.2995906-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240229114016.2995906-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240229114016.2995906-7-edumazet@google.com> Subject: [PATCH net-next 6/6] inet: use xa_array iterator to implement inet_dump_ifaddr() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Jiri Pirko , David Ahern , netdev@vger.kernel.org, Florian Westphal , eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org 1) inet_dump_ifaddr() can can run under RCU protection instead of RTNL. 2) properly return 0 at the end of a dump, avoiding an an extra recvmsg() system call. Signed-off-by: Eric Dumazet --- net/ipv4/devinet.c | 95 ++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index 2afe78dfc3c2f6c0394925f1c35532a2dfd26d71..4daa8124f247c256c4f8c1ff29ac621570af0755 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -1676,7 +1676,7 @@ static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); } -static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, +static int inet_fill_ifaddr(struct sk_buff *skb, const struct in_ifaddr *ifa, struct inet_fill_args *args) { struct ifaddrmsg *ifm; @@ -1805,15 +1805,15 @@ static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, } static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, - struct netlink_callback *cb, int s_ip_idx, + struct netlink_callback *cb, int *s_ip_idx, struct inet_fill_args *fillargs) { struct in_ifaddr *ifa; int ip_idx = 0; int err; - in_dev_for_each_ifa_rtnl(ifa, in_dev) { - if (ip_idx < s_ip_idx) { + in_dev_for_each_ifa_rcu(ifa, in_dev) { + if (ip_idx < *s_ip_idx) { ip_idx++; continue; } @@ -1825,9 +1825,9 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, ip_idx++; } err = 0; - + ip_idx = 0; done: - cb->args[2] = ip_idx; + *s_ip_idx = ip_idx; return err; } @@ -1859,75 +1859,53 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) }; struct net *net = sock_net(skb->sk); struct net *tgt_net = net; - int h, s_h; - int idx, s_idx; - int s_ip_idx; - struct net_device *dev; + struct { + unsigned long ifindex; + int ip_idx; + } *ctx = (void *)cb->ctx; struct in_device *in_dev; - struct hlist_head *head; + struct net_device *dev; int err = 0; - s_h = cb->args[0]; - s_idx = idx = cb->args[1]; - s_ip_idx = cb->args[2]; - + rcu_read_lock(); if (cb->strict_check) { err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, skb->sk, cb); if (err < 0) - goto put_tgt_net; + goto done; - err = 0; if (fillargs.ifindex) { - dev = __dev_get_by_index(tgt_net, fillargs.ifindex); - if (!dev) { - err = -ENODEV; - goto put_tgt_net; - } - - in_dev = __in_dev_get_rtnl(dev); - if (in_dev) { - err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx, - &fillargs); - } - goto put_tgt_net; - } - } - - for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { - idx = 0; - head = &tgt_net->dev_index_head[h]; - rcu_read_lock(); - cb->seq = inet_base_seq(tgt_net); - hlist_for_each_entry_rcu(dev, head, index_hlist) { - if (idx < s_idx) - goto cont; - if (h > s_h || idx > s_idx) - s_ip_idx = 0; + err = -ENODEV; + dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex); + if (!dev) + goto done; in_dev = __in_dev_get_rcu(dev); if (!in_dev) - goto cont; - - err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx, - &fillargs); - if (err < 0) { - rcu_read_unlock(); goto done; - } -cont: - idx++; + err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, + &fillargs); + goto done; } - rcu_read_unlock(); } + cb->seq = inet_base_seq(tgt_net); + + for_each_netdev_dump(net, dev, ctx->ifindex) { + in_dev = __in_dev_get_rcu(dev); + if (!in_dev) + continue; + err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, + &fillargs); + if (err < 0) + goto done; + } done: - cb->args[0] = h; - cb->args[1] = idx; -put_tgt_net: + if (err < 0 && likely(skb->len)) + err = skb->len; if (fillargs.netnsid >= 0) put_net(tgt_net); - - return skb->len ? : err; + rcu_read_unlock(); + return err; } static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, @@ -2818,7 +2796,8 @@ void __init devinet_init(void) rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0); rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0); - rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0); + rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, + RTNL_FLAG_DUMP_UNLOCKED); rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf, inet_netconf_dump_devconf, RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED);