Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: pablo@netfilter.org
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
Subject: Re: [PATCH net-next 15/15] netfilter: ipset: re-add forceadd support
Date: Tue, 24 Nov 2026 14:39:47 +0000	[thread overview]
Message-ID: <179553118759.3135.6585926513331590703@kernel.org> (raw)
In-Reply-To: <20261123135726.3940688-16-pablo@netfilter.org>

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

  reply	other threads:[~2026-11-24 14:39 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:57 [PATCH net-next,v2 00/15] Netfilter updates for net-next Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 01/15] netfilter: conntrack: prevent nf_conntrack_buckets race condition Pablo Neira Ayuso
2026-11-23 14:02   ` netdev-bot+sinfo
2026-11-23 14:17     ` Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 02/15] netfilter: conntrack: validate skb->_nfct and packet headers Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 03/15] netfilter: conntrack: refactor helper call logic in nf_confirm() Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 04/15] netfilter: conntrack: verify L4 protocol before calling helper Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 05/15] netfilter: conntrack: replace open-coded helper invocation Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 06/15] netfilter: conntrack: harden helper invocation with tuple revalidation Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 07/15] netfilter: nft_ct: validate timeout object protocol Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 08/15] netfilter: nft_synproxy: only handle pure SYN and ACK packets Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 09/15] netfilter: nft_ct: validate hook and priority for ct zone set Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 10/15] rhashtable: Add rhashtable_flush_and_free helper Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 11/15] netfilter: ipset: add rhashtable boilerplate stubs Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 12/15] netfilter: ipset: replace internal hash table with rhashtable Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 13/15] netfilter: ipset: also report mem size for cidr storage to userspace Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 14/15] netfilter: ipset: re-add IP_SET_HASH_WITH_MULTI support Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko
2026-11-23 13:57 ` [PATCH net-next 15/15] netfilter: ipset: re-add forceadd support Pablo Neira Ayuso
2026-11-24 14:39   ` netdev-bot+sashiko [this message]
2026-11-24 12:27 ` [PATCH net-next,v2 00/15] Netfilter updates for net-next Paolo Abeni
2026-11-24 12:36   ` Florian Westphal
2026-11-24 13:47     ` Paolo Abeni
2026-11-24 13:33   ` Pablo Neira Ayuso
2026-11-24 13:43     ` Paolo Abeni

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=179553118759.3135.6585926513331590703@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=ja@ssi.bg \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    /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