mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linkui Xiao <xiaolinkui@126.com>
To: horms@verge.net.au, ja@ssi.bg, dsahern@kernel.org,
	idosch@nvidia.com, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com
Cc: netdev@vger.kernel.org, lvs-devel@vger.kernel.org,
	linux-kernel@vger.kernel.org, Linkui Xiao <xiaolinkui@kylinos.cn>
Subject: [PATCH net-next] ipvs: annotate the lockless read of sysctl_sync_threshold[0]
Date: Mon, 26 Oct 2026 16:37:08 +0800	[thread overview]
Message-ID: <20261026083708.617278-1-xiaolinkui@126.com> (raw)

From: Linkui Xiao <xiaolinkui@kylinos.cn>

sysctl_sync_threshold() reads ipvs->sysctl_sync_threshold[0] from the
packet path without any synchronization: ip_vs_sync_conn_needed(),
ip_vs_sync_conn_v0() and ip_vs_sync_conn() run in softirq context on
behalf of ip_vs_in_hook(), ip_vs_conn_expire() is a timer callback and
ip_vs_proc_conn() runs in the backup sync kthread. The only writer,
proc_do_sync_threshold(), updates the array with a plain memcpy() while
holding ipvs->sync_mutex, a mutex none of the readers ever take.

Commit 749c42b620a9 ("ipvs: reduce sync rate with time thresholds")
already recognized this race, but only for the second element of the
array: "As the sysctl_sync_period now can be 0 use ACCESS_ONCE to avoid
division by zero". sysctl_sync_threshold() was left with a plain access
even though both elements live in the same array and are written by the
same memcpy().

A stale value is not dangerous here: the element is only compared
against a packet counter and stored into cp->in_pkts, and an aligned int
cannot tear on any supported architecture. What the missing annotation
does allow is for the compiler to reload the element, so the two
comparisons in ip_vs_sync_conn_needed() can observe different values
for one and the same packet, and KCSAN reports the unsynchronized
access.

Read the element once with READ_ONCE(), exactly like the adjacent
sysctl_sync_period() has done since 749c42b620a9.

Fixes: 749c42b620a9 ("ipvs: reduce sync rate with time thresholds")
Signed-off-by: Linkui Xiao <xiaolinkui@kylinos.cn>
---
 include/net/ip_vs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/net/ip_vs.h b/include/net/ip_vs.h
index 9a83bff32569..5aa6a94ed104 100644
--- a/include/net/ip_vs.h
+++ b/include/net/ip_vs.h
@@ -1353,7 +1353,7 @@ static inline int sysctl_conn_max(struct netns_ipvs *ipvs)
 
 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
 {
-	return ipvs->sysctl_sync_threshold[0];
+	return READ_ONCE(ipvs->sysctl_sync_threshold[0]);
 }
 
 static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
-- 
2.25.1


                 reply	other threads:[~2026-10-26  8:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026083708.617278-1-xiaolinkui@126.com \
    --to=xiaolinkui@126.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@verge.net.au \
    --cc=idosch@nvidia.com \
    --cc=ja@ssi.bg \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvs-devel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=xiaolinkui@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®