🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #21868

Re: [PATCH net-next 15/15] netfilter: ipset: re-add forceadd support

#21868
From: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 14:39
222 lines
15441 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] On CONFIG_SMP=n kernels without CONFIG_DEBUG_SPINLOCK,
  mtype_remove_random() in the hash:net,iface (IP_SET_HASH_WITH_MULTI)…
- [Medium] Forceadd on a full set now fails a large fraction of the time,
  which does not match the commit message's claim that the add is…
- [Low] mtype_new_elem() evicts a random element through
  mtype_remove_random() before steps that can still fail, and nothing…

--

netfilter: ipset: re-add forceadd support

This patch brings back forceadd eviction for the ipset hash types, which
the rhashtable conversion dropped. It adds mtype_remove_random(). That
helper looks up one random bucket using an always-matching compare
function, then evicts the element it finds. For IP_SET_HASH_WITH_MULTI
sets it evicts the first element of the group it finds. mtype_new_elem()
calls the helper when a forceadd set has reached maxelem.

> diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
> index 39ef51c44df87..eab1e2bd85ff2 100644
> --- a/net/netfilter/ipset/ip_set_hash_gen.h
> +++ b/net/netfilter/ipset/ip_set_hash_gen.h

[ ... ]

> @@ -950,6 +954,93 @@ mtype_rht_size(struct ip_set *set, u32 *elements, size_t *ext_size)

[ ... ]

> +#ifdef IP_SET_HASH_WITH_MULTI
> +	{

[ ... ]

> +		grp = rhashtable_lookup(&h->ht, k, ip_set_hash_grp_rnd_params);
> +		if (!grp)
> +			return false;
> +
> +		/* mtype_add() can call us while already holding this exact
> +		 * group's lock (maxelem reached while inserting into it);
> +		 * trylock to avoid self-deadlock. Best-effort: no linear
> +		 * scan, 'return false' is fine either way.
> +		 */
> +		if (!spin_trylock_bh(&grp->lock))
> +			return false;
> +		e = hlist_entry_safe(grp->iface_list.first,
> +				     struct mtype_rht_elem, node);
> +		if (!e) {
> +			spin_unlock_bh(&grp->lock);
> +			return false;
> +		}
> +		mtype_group_remove_elem(set, h, grp, e);
> +		mtype_group_unlock(h, grp);
> +		return true;

[Severity: High]
Does this trylock ever fail on CONFIG_SMP=n kernels without
CONFIG_DEBUG_SPINLOCK? On those builds, include/linux/spinlock_api_up.h
has:

static __always_inline int _raw_spin_trylock_bh(raw_spinlock_t *lock)
	__cond_acquires(true, lock)
{
	__LOCK_BH(lock);
	return 1;
}

If the random probe lands on the group that mtype_add() already holds,
the trylock succeeds instead of returning false.

If that group has exactly one element, this sequence looks possible:

mtype_add()
  mtype_group_get_or_create()        /* grp locked */
  mtype_new_elem(..., old == NULL)
    mtype_remove_random()
      spin_trylock_bh(&grp->lock)    /* succeeds on UP */
      mtype_group_remove_elem()
      mtype_group_unlock()
        rhashtable_remove_fast(&h->ht, &grp->node, ...)
        grp->dead = true;
        kfree_rcu(grp, rcu);

Back in mtype_add(), the new element is then linked in without any
grp->dead check:

		} else {
			hlist_add_tail_rcu(&e->node, &grp->iface_list);
			atomic_inc(&h->nelems);
		}
		spin_unlock_bh(&grp->lock);

Would the new element end up in a group that is no longer in h->ht and
is freed after the grace period?

If so, test, del, gc and flush could not reach that element, so the
element and its comment extension would leak. h->nelems and the cidr
prefix counts would never be decremented, not even by flush, because
mtype_flush_group() only accounts for elements it can reach. The add
would also be reported as success.

mtype_group_get_or_create() already refuses to insert into dead groups
to avoid this state.

This is reachable from the SET target on the packet path. Could repeated
hits keep reducing the set's usable capacity until it stays full?

> +	}
> +#else
> +	{

[ ... ]

> +		e = rhashtable_lookup(&h->ht, &k, ip_set_hash_rnd_params);
> +		if (!e || rhashtable_remove_fast(&h->ht, &e->node, mtype_rht_params))
> +			return false;

[Severity: Medium]
How often does this single probe find an element once the set is full?

mtype_create() sets params.max_size = maxelem, and rhashtable_init()
rounds it down to a power of two:

	ht->p.max_size = rounddown_pow_of_two(params->max_size);

That gives a full set a load factor between 1 and 2. With the default
maxelem of 65536 there are 65536 buckets for 65536 elements, so roughly
e^-1, about 37%, of the buckets are empty.

Each probe that hits an empty bucket makes mtype_new_elem() return
-EMFILE (-IPSET_ERR_HASH_FULL) and log "Set %s is full", even though the
set was created with forceadd.

For hash:net,iface (IP_SET_HASH_WITH_MULTI) the table holds groups, not
elements. The load factor there is lower, so the MULTI probe above would
miss roughly 47-69% of the time.

The commit message says:

    If there is one, try to evict it and allow add of the new element
    just like before the rhashtable conversion.

The old code only failed when the new key's own bucket was empty, and
its buckets were dense (up to bucketsize entries each), so forceadd
almost always succeeded.

Would a bounded number of extra random probes, or falling back to
evicting from the new key's own bucket, get closer to the old behaviour?

> +
> +		ipset_hash_elem_destroy_free(set, e);
> +		return true;
> +	}
> +#endif
> +}

[ ... ]

> @@ -993,7 +1084,7 @@ mtype_new_elem(struct ip_set *set, struct htype *h, const struct mtype_elem *d,
>  		 * its pretty much pointless, userspace can just retry once
>  		 * gc has fired, resp. decrease the gc interval.
>  		 */
> -		if (1) {
> +		if (!SET_WITH_FORCEADD(set) || !mtype_remove_random(set)) {
>  			if (net_ratelimit())
>  				pr_warn("Set %s is full, maxelem %u reached\n",
>  					set->name, h->maxelem);

[Severity: Low]
The eviction happens here, before steps that can still fail. What
happens to the evicted entry if the add fails after that?

Once mtype_remove_random() has returned true, mtype_new_elem() can still
fail here:

	e = kzalloc(offsetof(struct mtype_rht_elem, elem) + set->dsize,
		    GFP_ATOMIC);
	if (!e)
		return ERR_PTR(-ENOMEM);

It can also fail in mtype_add_cidr() with -ENOMEM or -EOVERFLOW. In the
non-MULTI path of mtype_add(), rhashtable_lookup_insert_key() can return
-EEXIST, -ENOMEM, -E2BIG or -EBUSY.

The error path in mtype_add() only destroys the new element and undoes
its cidr accounting:

	if (ret) {
		ip_set_ext_destroy(set, &e->elem);
		kfree_rcu(e, rcu);
		/* error: Undo mtype_add_cidr(). */
		mtype_del_cidr_all(set, h, d);
	}

So the set loses a random entry even though the add fails.

Hash types use region_lock = true, so ip_set_lock() does not serialize
adds. Two CPUs adding the same new key to a full forceadd set (for
example via the SET target) could each evict an element while only one
insert succeeds. The other CPU gets -EEXIST, which is reported as
success with IPSET_FLAG_EXIST.

The pre-rhashtable code reused the slot in place and had no failure
point after dropping the old entry.

Could the eviction be moved after the allocation and cidr steps, or is
losing an extra entry acceptable for forceadd sets?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123135726.3940688-2-pablo%40netfilter.org
Technical Details
Message-ID: <179553118759.3135.6585926513331590703@kernel.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370471-linux-kernel-netdev=dewy.ru
References: <20261123135726.3940688-16-pablo@netfilter.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370471-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next 15/15] netfilter: ipset: re-add forceadd support
Date: Tue, 24 Nov 2026 14:39:47 +0000
Organization: Usenet Network
Message-ID: <179553118759.3135.6585926513331590703@kernel.org>
References: <20261123135726.3940688-16-pablo@netfilter.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="4160270"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:ZDBW5Wyr50nt3wwgom4Jm8RazeY= sha256:1p74ob1iFvF+0attBofq48SXFgiEFcAeNs2TSPWvahc=
	sha1:ScBoPu9bm36NcMoT9TzEkKEcdE8= sha256:KCBlX87cT5ZxfTOhdTL1Dg5sp2ck69pwvaSf6eRCa0A=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 65035125E1A; Tue, 24 Nov 2026 17:40:41 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.4
X-Spam-Status: No, score=-3.4 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id P2lYTJL81Jju;
	Tue, 24 Nov 2026 17:40:37 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-370471-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 57CBF125E18; Tue, 24 Nov 2026 17:40:29 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 5A08C35DF6; Tue, 24 Nov 2026 14:39:59 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795531189;
	b=knvYpnIZtOKQickc1iwihOo4jfOou0ttRJjcy2MMKDs1AOAvtUqxcfi5rDir1CU8TrRS
	 dNsY/gOSYcxJqzdKaMFxw9Ao5y57H7H7pvpXhl/9rJvuoNqOjHyDG2AXx+6jJ3y8G+o9h
	 Lrazf6WqnxMzvf/pXf29trzzxzog5ZPBnBUZppVaggCIjQcX8ejH+ieyNd5KDrUkeOAhX
	 wp0BKw0vqADC2Lfer7T/tiPt9z8Y8QVYhWTqxD2QR91UoGYN7+wsTrtOLt0r7pY/A/AEe
	 hNrsBJCC5lljonP7ZImx4ZguFnKrpIR9VUhVjC8o18CImudaRqAGfS5T2vyvLsNa/8g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795531189;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=AE/ZMRJm1vER60qJmgDTOQ8c0clB4v20aDU6EMVQxYg=;
	b=EuBpVAXhWOXLaP5chkRFqIVq2XDry8Vgw26gSpYBj+cEBDuKTXQawVLahXrPM9yzz5Yq
	 EKPHyodwI/BPQin2rJu5xueLfPHcu9uQu27o0F6c37DZwd2YUTIh+3mkaIWfoWdXFA2EU
	 XG47MG/ooivcTDR/48c6ZV51xprM9kKeqL7TJhtqleh+n42NKY2qLZedGK0TzOFpLm48o
	 KMtEtG1oJAdv3rNOWrjuMx6cYhZxBL8It7e8XzsS4t4SqN8n3BeOycLEpuXjUi/mRolAm
	 OmlipnHxXnQvVEkSixClw2eUVSm1R+PMS/1COVkHg4pg8GHBZQ6kZO5o9/vuV7smflw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=V5qdWybp;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B67F1606A6BA;
	Tue, 24 Nov 2026 14:39:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=V5qdWybp
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795531188;
	b=iZBey5Y21nlLHDPZttiVL/N+ORLf1voiEfcWy53ghpqx9zK5MarlBBzQHgnxz6Nce7Wh
	 etR/UdIqpmNd8YyySSYcLktwkJqdIyDPqV31LVNJnfkOKQIK9c3XmkVdaE0uJCQJqe7jd
	 soKu6geMlSiRvaHVwbPdnK+VdYrhVKBb+FuVs/H4uCal1jiVfrguoYVoLwDtBEs1wgckv
	 iGSyBJk/x/pIhvSlkJXOqAF9N+PlB120aI6+sT5Bjm8xyV+Ran0DBM0k6zHA7LVBv7tsx
	 YzVaIwiY7iPYe9I+MIZGsC08lzmAlLC0hFv6I6kdY+U7MaELj+D4QDqvRiG/j9TiezA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795531188;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=AE/ZMRJm1vER60qJmgDTOQ8c0clB4v20aDU6EMVQxYg=;
	b=hpgFmI9u6wgR7ZWwBBiEdjfr2VB1jJKU7SlszP1sD8qJxxR/LzK5ceX3Sm6lEQ2SRdXi
	 YFAk0OT1+qInJ78yMeQu6uUQ+9D9nz9edmLoSlySMx1cmJU0MinWeIDuPfC5tQZNfX4bv
	 ft4PK2zwhBoA58R/9fHNJ/XPzRvYfsAUTBFFvBe8M8gpR6X5Pnx6pX72u0S4wyRSVCV/T
	 a3WoL2h5eY/rcMsx8uABeV17hFJg7wwNhk1mtqu8QFUaF12Al2IqU3FvBRKyBfI8XnwqT
	 RZuQIJSsYwP2FHNPDSXAdbuMyCee1OZfykA8wRGLONhlxfGnD3PxFAqy+8Dvm770L9Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=V5qdWybp;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id D76C3606A331;
	Tue, 24 Nov 2026 14:39:48 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14F261F000ED;
	Tue, 24 Nov 2026 14:39:48 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795531188;
	bh=AE/ZMRJm1vER60qJmgDTOQ8c0clB4v20aDU6EMVQxYg=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=V5qdWybpHrc/peSxrqGtmYwmpK8dhXFYEHy7Y9bFLb7WMBw7ZwpC4ccJUPRklTtPC
	 kEZP8anBDBN887s76PWMDpv0VNHydW2F9KpzQmjuF7G9giGtaGNCCv5k7OTf+kL8YJ
	 +KQg3lhGDPfHJ4vLhoOCwdndABqQEe8Q5NpbqxSaSRS/U+6Mo/6f23zXI/NcS3d9eM
	 yyIOo8CxE86jFty1rodtSfqUplNmYBM+B4Ut+IMw5pbGa+9UWX4GuGOx3H4ipWfUfy
	 vfpxw693QZeFmtTRCKaHgn7lr0jfxqVvvjnm4C7qHZGa+RJNPtKHL/rK3u/hVzj4IG
	 zMwpsN+zV+DaA==
X-Cc: netfilter-devel@vger.kernel.org, davem@davemloft.net,
 netdev@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com,
 edumazet@kernel.org, horms@kernel.org, fw@strlen.de, ja@ssi.bg
X-In-Reply-To: <20261123135726.3940688-16-pablo@netfilter.org>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list