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 14/15] netfilter: ipset: re-add IP_SET_HASH_WITH_MULTI support
Date: Tue, 24 Nov 2026 14:39:46 +0000 [thread overview]
Message-ID: <179553118607.3135.12377785999784372927@kernel.org> (raw)
In-Reply-To: <20261123135726.3940688-15-pablo@netfilter.org>
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · 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] In net/netfilter/ipset/ip_set_hash_gen.h, mtype_group_find()
(used by mtype_del()) and mtype_group_test() choose the wrong element…
- [Medium] The commit message does not disclose a user-visible change to
hash:net,iface add semantics compared with released kernels, and the…
- [Medium] mtype_group_list() resumes a paused group by element identity
in two separate lockless RCU passes over grp->iface_list.
- [Medium] In the IP_SET_HASH_WITH_MULTI path of mtype_add(), the group
is allocated and hashed before the maxelem limit is checked.
- [Low] The comment at the mtype_group_test() call site in
mtype_test_cidrs() does not match the implementation.
--
netfilter: ipset: re-add IP_SET_HASH_WITH_MULTI support
This commit reimplements the hash:net,iface set type on top of
rhashtable. Each net/cidr/physdev key maps to a struct mtype_group, which
owns an RCU-protected list of elements that differ by interface name.
Exact elements get priority over wildcard elements in del and test, and
each group is capped at IPSET_GROUP_MAX_ENTRIES elements.
> diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
> index e7f2e584a7a50..39ef51c44df87 100644
> --- a/net/netfilter/ipset/ip_set_hash_gen.h
> +++ b/net/netfilter/ipset/ip_set_hash_gen.h
[ ... ]
> @@ -454,6 +567,164 @@ mtype_del_cidr_all(struct ip_set *set, struct htype *h, const struct mtype_elem
> #endif
> }
>
> +#ifdef IP_SET_HASH_WITH_MULTI
[ ... ]
> +static struct mtype_group *
> +mtype_group_get_or_create(struct htype *h, const struct mtype_elem *d, int *errp)
> +{
[ ... ]
> + grp = kzalloc(sizeof(*grp), GFP_ATOMIC);
> + if (!grp) {
> + *errp = -ENOMEM;
> + return NULL;
> + }
> + INIT_HLIST_HEAD(&grp->iface_list);
> + spin_lock_init(&grp->lock);
> + memcpy(&grp->key, d, sizeof(grp->key));
> +
> + spin_lock_bh(&grp->lock);
> + err = rhashtable_lookup_insert_key(&h->ht, d, &grp->node, mtype_group_rht_params);
[Severity: Medium]
Once the set has reached maxelem, does every add for a new key still
allocate and hash a group here before it gets rejected?
The maxelem check only runs later, in mtype_new_elem():
mtype_add()
mtype_group_get_or_create() kzalloc(GFP_ATOMIC), rhashtable insert
mtype_new_elem() returns -EMFILE
mtype_group_unlock() rhashtable_remove_fast(), kfree_rcu(grp)
Take a full hash:net,iface set and a rule using the SET target with
--add-set. Each packet with a new source address would then cost a
GFP_ATOMIC allocation in softirq, an insert/remove pair on h->ht and an
RCU callback.
The non-multi path returns -EMFILE from mtype_new_elem() before any
kzalloc().
Could the maxelem check run before a new group is created?
[ ... ]
> +/**
> + * mtype_group_find() - pick the element a non-wildcard request affects
> + * @set: Pointer to the ip_set.
> + * @grp: Group to search, with @grp->lock held
> + * @d: Lookup key (net/cidr/physdev and interface)
> + *
> + * An exact (non-wildcard) element always takes priority over a wildcard
> + * element, independent of @grp's list order. Must be called with
> + * @grp->lock held: the result is only valid as long as the lock prevents
> + * a concurrent mtype_add() from linking a higher-priority element into
> + * @grp after this scan but before the caller acts on its result.
> + *
> + * Intentionally does not use mtype_data_isequal() (strict identity,
> + * including @d's wildcard flag): "ipset del" must be able to remove a
> + * wildcard element even if @d->wildcard is false.
> + *
> + * Return: the matching element, or NULL.
> + */
> +static struct mtype_rht_elem *
> +mtype_group_find(struct ip_set *set, struct mtype_group *grp, const struct mtype_elem *d)
> +{
> + struct mtype_rht_elem *e, *best = NULL;
> +
> + lockdep_assert_held(&grp->lock);
> +
> + hlist_for_each_entry_rcu(e, &grp->iface_list, node) {
> + if (!mtype_data_equal(&e->elem, d) ||
> + SET_ELEM_EXPIRED(set, &e->elem))
> + continue;
> + if (mtype_data_isexact(&e->elem))
> + return e;
> + if (!best)
> + best = e;
> + }
> + return best;
> +}
[Severity: High]
Does mtype_group_find() pick the right element when a group holds
overlapping entries?
mtype_add() now deduplicates with mtype_data_isequal(). One group can
therefore hold an exact "eth" together with a wildcard "eth+", or two
overlapping wildcards such as "et+" and "eth+". The array code rejected
both combinations with IPSET_ERR_EXIST.
For "ipset del S NET,eth+", hash_netiface4_uadt() sets e.wildcard = 1
and iface = "eth", and mtype_del() passes that request here unchanged.
The stored exact "eth" passes mtype_data_equal() (strcmp("eth", "eth")
== 0) and mtype_data_isexact(), so it is returned and removed. The "eth+"
the user named stays in the set.
If "et+" was added before "eth+", the same del matches "et+" first
(strncmp("et", "eth", 2) == 0). "et+" becomes best and is removed
instead of "eth+".
mtype_group_test() depends on list order in the same way. For a packet
on eth0 with no exact element, its second loop returns the first
wildcard in insertion order:
hlist_for_each_entry_rcu(e, &grp->iface_list, node) {
if (e == exact || SET_ELEM_EXPIRED(set, &e->elem) ||
!mtype_data_equal(&e->elem, d))
continue;
ret = mtype_data_match(&e->elem, ext, mext, set, flags);
if (ret)
return ret;
}
So an "eth+ nomatch" added after "et+" never applies, but it does apply
when it is added first.
The commit message says:
mtype_del() must not remove a wildcard element instead of
the specific element named in the request.
It also says the precedence is "independent of list order". The
kernel-doc above says the helper picks "the element a non-wildcard
request affects", yet mtype_del() also calls it for wildcard requests.
Should mtype_group_find() check for an mtype_data_isequal() match before
falling back to pattern matching? Should wildcard-vs-wildcard precedence
in mtype_group_test() also be independent of insertion order?
[ ... ]
> @@ -779,6 +1095,61 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
> int ret = 0;
>
> rcu_read_lock_bh();
> +#ifdef IP_SET_HASH_WITH_MULTI
> + {
> + struct mtype_group *grp;
> + unsigned int seen = 0;
> +
> + grp = mtype_group_get_or_create(h, d, &ret);
> + if (!grp)
> + goto out_rcu_unlock;
> +
> + old = NULL;
> + hlist_for_each_entry_rcu(e, &grp->iface_list, node) {
> + if (SET_ELEM_EXPIRED(set, &e->elem)) {
> + mtype_group_remove_elem(set, h, grp, e);
> + continue;
> + }
[ ... ]
> + if (mtype_data_isequal(&e->elem, d)) {
> + old = e;
> + break;
> + }
[Severity: Medium]
Is this change in add semantics intended? The array-based implementation
did this in mtype_add():
if (mtype_data_equal(data, d, &multi)) {
if (flag_exist || SET_ELEM_EXPIRED(set, data)) {
/* Just the extensions could be overwritten */
j = i;
goto overwrite_extensions;
}
ret = -IPSET_ERR_EXIST;
goto unlock;
}
With "X,eth+" stored, "ipset add S X,eth0" failed with IPSET_ERR_EXIST.
With -exist, it refreshed the wildcard element's extensions instead.
With mtype_data_isequal(), the same request now silently creates a
second element. That changes element counts, maxelem accounting and
ipset save output for existing rule sets.
mtype_del() still matches by prefix through mtype_group_find(), so add
and del now disagree about identity. "add X,eth0" treats eth0 as absent,
but "del X,eth0" with no exact eth0 present removes the unrelated
"X,eth+".
The subject "re-add IP_SET_HASH_WITH_MULTI support" suggests the
original semantics come back. Should the commit message describe this
change, or should add keep the old behaviour?
> + ++seen;
> + }
[ ... ]
> @@ -923,13 +1401,25 @@ mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
> #else
> mtype_data_netmask(d, p0.cidr);
> #endif
> +#ifdef IP_SET_HASH_WITH_MULTI
> + /* mtype_group_test() sets @stop itself: the outer loop
> + * stops here when some other interface's entry is
> + * registered at this netmask, but still falls through if
> + * nothing here matches our interface at all, or if it did
> + * but got filtered by its extensions (e.g. timed out).
> + */
> + ret = mtype_group_test(h, d, &stop, ext, mext, set, flags);
[Severity: Low]
This isn't a bug, but does this comment match mtype_group_test()? It
skips expired elements before it sets any_live or iface_live:
if (SET_ELEM_EXPIRED(set, &e->elem))
continue;
any_live = true;
if (!mtype_data_equal(&e->elem, d))
continue;
iface_live = true;
A timed-out element for our interface therefore never sets iface_live.
If a live element for another interface exists at the same prefix, stop
is set and broader prefixes are not tried. The "e.g. timed out" case
described here does not fall through.
[ ... ]
> @@ -1074,6 +1580,84 @@ mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)
[ ... ]
> + * Resuming by element identity (mtype_data_isequal(), rather than a
> + * plain position count) keeps the resume point correct even if a
> + * concurrent mtype_del() removes an earlier, already dumped element
> + * from @grp's iface_list while the dump was paused: entries are only
> + * ever appended at the tail, so no concurrent insert can land before
> + * the resume point either. If the previously emitted element itself
> + * was removed in the meantime, there is no reliable resume point left,
> + * so this group is instead dumped again from the start, i.e. userspace
> + * will see duplicates rather than skipping entries.
> + *
> + * Return: 0 once @grp is fully dumped, -EMSGSIZE if an element didn't fit.
> + */
> +static int
> +mtype_group_list(const struct ip_set *set, struct mtype_group *grp,
> + struct sk_buff *skb, u32 *emitted, struct mtype_list_iter *iter)
> +{
> + struct mtype_rht_elem *e;
> + struct nlattr *nested;
> + bool skipping = false;
> + void *incomplete;
> +
> + if (iter->have_last) {
> + hlist_for_each_entry_rcu(e, &grp->iface_list, node) {
> + if (mtype_data_isequal(&e->elem, &iter->last)) {
> + skipping = true;
> + break;
> + }
> + }
> + }
> +
> + hlist_for_each_entry_rcu(e, &grp->iface_list, node) {
> + if (skipping) {
> + if (mtype_data_isequal(&e->elem, &iter->last))
> + skipping = false;
> + continue;
> + }
[Severity: Medium]
Can this leave live elements out of the dump?
The two passes over grp->iface_list are lockless RCU walks. There is no
grp->lock, generation counter or NLM_F_DUMP_INTR between them.
iter->last can be unlinked between pass 1 and pass 2, by gc, a del, a
kernel-side --del-set, or mtype_add() reaping an expired entry. Pass 2
then never finds it, so skipping stays true for the rest of the list.
Nothing is emitted and 0 is returned. mtype_list() then moves past the
group:
if (mtype_group_list(set, grp, skb, &emitted, iter))
goto paused;
...
grp = rhashtable_walk_next(hti);
so the remaining elements of this group are never dumped.
There is also a case that needs no tight race. Take a group [A, B, C, D]
with the dump paused after B. If B expires and is re-added from the
packet path between two recvmsg calls, mtype_add() removes the expired
B and appends the new copy at the tail:
[A, C, D, B']
On resume, pass 1 finds B' through mtype_data_isequal(), and pass 2 then
skips A, C, D and B'. C and D were present for the whole dump but are
never emitted.
This contradicts the kernel-doc above ("no concurrent insert can land
before the resume point", "userspace will see duplicates rather than
skipping entries"). Would holding grp->lock across both passes, or a
generation counter with NLM_F_DUMP_INTR, address this?
> +
> + if (SET_ELEM_EXPIRED(set, &e->elem))
> + continue;
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123135726.3940688-2-pablo%40netfilter.org
next prev parent reply other threads:[~2026-11-24 14:39 UTC|newest]
Thread overview: 37+ 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 [this message]
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
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
-- strict thread matches above, loose matches on Subject: below --
2026-11-23 13:54 [PATCH net-next " Pablo Neira Ayuso
2026-11-23 13:54 ` [PATCH net-next 14/15] netfilter: ipset: re-add IP_SET_HASH_WITH_MULTI support Pablo Neira Ayuso
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=179553118607.3135.12377785999784372927@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