[{"id":3679800,"web_url":"http://patchwork.ozlabs.org/comment/3679800/","msgid":"<42f9cfbf-8189-2bba-0541-acf7a3536968@ssi.bg>","list_archive_url":null,"date":"2026-04-21T10:35:34","subject":"Re: [PATCH net] ipvs: fix races around est_mutex and est_cpulist","submitter":{"id":2825,"url":"http://patchwork.ozlabs.org/api/people/2825/","name":"Julian Anastasov","email":"ja@ssi.bg"},"content":"Hello,\n\nOn Mon, 20 Apr 2026, Julian Anastasov wrote:\n\n> Sashiko reports for races and possible crash around\n> the usage of est_cpulist_valid and sysctl_est_cpulist.\n> The problem is that we do not lock est_mutex in some\n> places which can lead to wrong write ordering and\n> as result problems when calling cpumask_weight()\n> and cpumask_empty().\n> \n> Fix them by moving the est_max_threads read/write under\n> locked est_mutex. Do the same for one ip_vs_est_reload_start()\n> call to protect the cpumask_empty() usage of sysctl_est_cpulist.\n> \n> Link: https://sashiko.dev/#/patchset/20260331165015.2777765-1-longman%40redhat.com\n> Fixes: f0be83d54217 (\"ipvs: add est_cpulist and est_nice sysctl vars\")\n> Signed-off-by: Julian Anastasov <ja@ssi.bg>\n\n\tAccording to Sashiko, this patch needs more\nwork, I'll send new version when I'm ready...\n\npw-bot: changes-requested\n\n\n> ---\n> \n>  Note that this patch complements v2 of patchset from 31-MAR-26\n>  \"ipvs: Fix incorrect use of HK_TYPE_KTHREAD housekeeping cpumask\"\n>  and can be applied before it to avoid the bad AI reviews:\n> \n>  https://lore.kernel.org/all/20260331165015.2777765-1-longman@redhat.com/\n> \n>  net/netfilter/ipvs/ip_vs_ctl.c |  5 +++++\n>  net/netfilter/ipvs/ip_vs_est.c | 22 +++++++++++++++-------\n>  2 files changed, 20 insertions(+), 7 deletions(-)\n> \n> diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c\n> index caec516856e9..8778e174ef56 100644\n> --- a/net/netfilter/ipvs/ip_vs_ctl.c\n> +++ b/net/netfilter/ipvs/ip_vs_ctl.c\n> @@ -1812,11 +1812,16 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,\n>  \t*svc_p = svc;\n>  \n>  \tif (!READ_ONCE(ipvs->enable)) {\n> +\t\tmutex_lock(&ipvs->est_mutex);\n> +\n>  \t\t/* Now there is a service - full throttle */\n>  \t\tWRITE_ONCE(ipvs->enable, 1);\n>  \n> +\t\tipvs->est_max_threads = ip_vs_est_max_threads(ipvs);\n> +\n>  \t\t/* Start estimation for first time */\n>  \t\tip_vs_est_reload_start(ipvs);\n> +\t\tmutex_unlock(&ipvs->est_mutex);\n>  \t}\n>  \n>  \treturn 0;\n> diff --git a/net/netfilter/ipvs/ip_vs_est.c b/net/netfilter/ipvs/ip_vs_est.c\n> index 433ba3cab58c..6c9981d5611e 100644\n> --- a/net/netfilter/ipvs/ip_vs_est.c\n> +++ b/net/netfilter/ipvs/ip_vs_est.c\n> @@ -68,6 +68,10 @@\n>      and the limit of estimators per kthread\n>    - est_add_ktid: ktid where to add new ests, can point to empty slot where\n>      we should add kt data\n> +  - data protected by service_mutex: est_temp_list, est_add_ktid\n> +  - data protected by est_mutex: est_kt_count, est_kt_arr, est_max_threads,\n> +    sysctl_est_cpulist, est_cpulist_valid, sysctl_est_nice, est_stopped,\n> +    sysctl_run_estimation\n>   */\n>  \n>  static struct lock_class_key __ipvs_est_key;\n> @@ -229,6 +233,8 @@ static int ip_vs_estimation_kthread(void *data)\n>  /* Schedule stop/start for kthread tasks */\n>  void ip_vs_est_reload_start(struct netns_ipvs *ipvs)\n>  {\n> +\tlockdep_assert_held(&ipvs->est_mutex);\n> +\n>  \t/* Ignore reloads before first service is added */\n>  \tif (!READ_ONCE(ipvs->enable))\n>  \t\treturn;\n> @@ -304,12 +310,17 @@ static int ip_vs_est_add_kthread(struct netns_ipvs *ipvs)\n>  \tvoid *arr = NULL;\n>  \tint i;\n>  \n> -\tif ((unsigned long)ipvs->est_kt_count >= ipvs->est_max_threads &&\n> -\t    READ_ONCE(ipvs->enable) && ipvs->est_max_threads)\n> -\t\treturn -EINVAL;\n> -\n>  \tmutex_lock(&ipvs->est_mutex);\n>  \n> +\t/* Allow kt 0 data to be created before the services are added\n> +\t * and limit the kthreads when services are present.\n> +\t */\n> +\tif ((unsigned long)ipvs->est_kt_count >= ipvs->est_max_threads &&\n> +\t    READ_ONCE(ipvs->enable) && ipvs->est_max_threads) {\n> +\t\tret = -EINVAL;\n> +\t\tgoto out;\n> +\t}\n> +\n>  \tfor (i = 0; i < id; i++) {\n>  \t\tif (!ipvs->est_kt_arr[i])\n>  \t\t\tbreak;\n> @@ -485,9 +496,6 @@ int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats)\n>  \tstruct ip_vs_estimator *est = &stats->est;\n>  \tint ret;\n>  \n> -\tif (!ipvs->est_max_threads && READ_ONCE(ipvs->enable))\n> -\t\tipvs->est_max_threads = ip_vs_est_max_threads(ipvs);\n> -\n>  \test->ktid = -1;\n>  \test->ktrow = IPVS_EST_NTICKS - 1;\t/* Initial delay */\n>  \n> -- \n> 2.53.0\n\nRegards\n\n--\nJulian Anastasov <ja@ssi.bg>","headers":{"Return-Path":"\n <netfilter-devel+bounces-12103-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","netfilter-devel@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (4096-bit key;\n unprotected) header.d=ssi.bg header.i=@ssi.bg header.a=rsa-sha256\n header.s=ssi header.b=aWEtfu05;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=172.234.253.10; helo=sea.lore.kernel.org;\n envelope-from=netfilter-devel+bounces-12103-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (4096-bit key) header.d=ssi.bg header.i=@ssi.bg header.b=\"aWEtfu05\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=193.238.174.39","smtp.subspace.kernel.org;\n dmarc=pass (p=reject dis=none) header.from=ssi.bg","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=ssi.bg"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g0JgV0RMPz1yCv\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 20:36:37 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id 7348F302BA40\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 21 Apr 2026 10:35:47 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id C4E393A1E7F;\n\tTue, 21 Apr 2026 10:35:46 +0000 (UTC)","from mx.ssi.bg (mx.ssi.bg [193.238.174.39])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B83837FF60;\n\tTue, 21 Apr 2026 10:35:40 +0000 (UTC)","from mx.ssi.bg (localhost [127.0.0.1])\n\tby mx.ssi.bg (Potsfix) with ESMTP id E68AD210D7;\n\tTue, 21 Apr 2026 13:35:37 +0300 (EEST)","from box.ssi.bg (box.ssi.bg [193.238.174.46])\n\tby mx.ssi.bg (Potsfix) with ESMTPS;\n\tTue, 21 Apr 2026 13:35:36 +0300 (EEST)","from ja.ssi.bg (unknown [213.16.62.126])\n\tby box.ssi.bg (Potsfix) with ESMTPSA id 1A44A60A90;\n\tTue, 21 Apr 2026 13:35:35 +0300 (EEST)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby ja.ssi.bg (8.18.1/8.18.1) with ESMTP id 63LAZYBs024962;\n\tTue, 21 Apr 2026 13:35:34 +0300"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1776767745; cv=none;\n b=NZS3xDZEvXM16Q9hq7tPEe3BzeRQucS0E1F46U+R45hJWChHeKCRbZFk16bhxrk0gYWZb5KvnHjYxoxPLRGQlu8r4WdyvvuiSu3I5xyrGS2GFSrH8hGYI19cou/ZrwuupMauFH998kZgO7Kkr23HQbjBctscGD9m4erRoahqkk4=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1776767745; c=relaxed/simple;\n\tbh=SFVGvlRu1+a2J1JiFNEEhv3LzRbzb1F3f/NQXWu1S+Q=;\n\th=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References:\n\t MIME-Version:Content-Type;\n b=gxeTvnbQ2oJArZntIOZlb/TGia8HMhKZs+aDSzg/q/Nu7+IUnEJiVSz5mZNWteiULn9A6Apq2ZRJHRpLSTyJEE155UtiepDIm9BheDW8yW4x9UzK54adnpQm64W1VUptwCi1+VhQg3bA3iw7DdD04uAo69/MhTFkCm78cIIUZL4=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=reject dis=none) header.from=ssi.bg;\n spf=pass smtp.mailfrom=ssi.bg;\n dkim=pass (4096-bit key) header.d=ssi.bg header.i=@ssi.bg header.b=aWEtfu05;\n arc=none smtp.client-ip=193.238.174.39","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=ssi.bg; h=cc:cc\n\t:content-type:content-type:date:from:from:in-reply-to:message-id\n\t:mime-version:references:reply-to:subject:subject:to:to; s=ssi;\n\t bh=VggmHWZBy2TpVFmdo5qcHAI/a4r6mPtbzHzgYEUpatA=; b=aWEtfu05j+yf\n\tRuyPixbAMfZFd202vdXvhOjlbngKXH7nJUbV0f2dCEPdAGBq4qUKfvBpaSkDlC7p\n\t3rCcuUMw5TbYQxj95CVI11++1ztcjG93DiQ6N/reZnfHXsY1nlsrZu4M40hX7MFg\n\txYO7w+FgbVCml3shsE/TE0iTXNkh044+y3yXkWW99eg3tJlc1M/f99hECSbRYEQt\n\tCD41SgWvSEbP+cg+Vng1yvw+HgZ8HweZ0Vsq6A1COYvSOohcm2azMHI7aAyW+UE2\n\tSze7dYf85gxAu0fVm2dsP9fpD0x0yD1Jk3o3+ID2oocrNCljzAPvYhDGH3fatEOX\n\tdfyWj7d0ASy44kUNQ0Og2UNicLFDxz3mzas5nGQwRmCDu+c8P6XVMOM7LbmiVZGx\n\tT0pauxdM6MLqSH0dX5zkTZC0bP+kUbQaupZNxI27cYtDz8MjkpOUamAib+HaU/x+\n\tEdtlNWM7iXU3u0ff1FesfzUSH0SxF6x5F5kFXJ3GQNnrdJbqU8XJPmX1WuW/1TTc\n\tGbuCt+MuKnm4R7LJ9ntcKNwO62GfxJQhnGUOL4sDRmezR59cFPi96lMYDIVQXI/y\n\tESH5pvhiIvNizMb4QvfVZpncewn/lB/KG/6pPiYs9dr6b38jLgudMVk6D1Ontjxb\n\tPhL+ripcVk06ctFTEZczGwvBhd3/WCA=","Date":"Tue, 21 Apr 2026 13:35:34 +0300 (EEST)","From":"Julian Anastasov <ja@ssi.bg>","To":"Simon Horman <horms@verge.net.au>","cc":"Pablo Neira Ayuso <pablo@netfilter.org>, Florian Westphal <fw@strlen.de>,\n        lvs-devel@vger.kernel.org, netfilter-devel@vger.kernel.org","Subject":"Re: [PATCH net] ipvs: fix races around est_mutex and est_cpulist","In-Reply-To":"<20260420171308.87192-1-ja@ssi.bg>","Message-ID":"<42f9cfbf-8189-2bba-0541-acf7a3536968@ssi.bg>","References":"<20260420171308.87192-1-ja@ssi.bg>","Precedence":"bulk","X-Mailing-List":"netfilter-devel@vger.kernel.org","List-Id":"<netfilter-devel.vger.kernel.org>","List-Subscribe":"<mailto:netfilter-devel+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:netfilter-devel+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=US-ASCII"}}]