From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: kadlec@netfilter.org, Florian Westphal <fw@strlen.de>
Subject: [PATCH RFC nf-next 12/12] netfilter: ipset: re-add forceadd support for rhashtable
Date: Tue, 14 Jul 2026 15:18:28 +0200 [thread overview]
Message-ID: <20260714131828.10685-13-fw@strlen.de> (raw)
In-Reply-To: <20260714131828.10685-1-fw@strlen.de>
The rhashtable conversion removed the SET_WITH_FORCEADD eviction logic.
Sets created with the forceadd flag got IPSET_ERR_HASH_FULL instead of
evicting an existing element to make room.
Add mtype_remove_random() helper that walks the rhashtable to pick an
element to evict, and call it from mtype_add() when the set is full and
forceadd is enabled.
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Florian Westphal <fw@strlen.de>
---
net/netfilter/ipset/ip_set_hash_gen.h | 43 +++++++++++++++++++++++----
1 file changed, 38 insertions(+), 5 deletions(-)
diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 12d3bc5acc81..df73c1ebb3f0 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -116,6 +116,7 @@ static const union nf_inet_addr zeromask = {};
#undef mtype_bucket_size
#undef mtype_hash_size
+#undef mtype_remove_random
#undef mtype_add
#undef mtype_del
#undef mtype_test_cidrs
@@ -165,6 +166,7 @@ static const union nf_inet_addr zeromask = {};
#define mtype_bucket_size IPSET_TOKEN(MTYPE, _bucket_size)
#define mtype_hash_size IPSET_TOKEN(MTYPE, _hash_size)
+#define mtype_remove_random IPSET_TOKEN(MTYPE, _remove_random)
#define mtype_add IPSET_TOKEN(MTYPE, _add)
#define mtype_del IPSET_TOKEN(MTYPE, _del)
#define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
@@ -511,6 +513,33 @@ mtype_ext_size(struct ip_set *set, u32 *elements, size_t *ext_size)
(offsetof(struct mtype_rht_elem, elem) + set->dsize);
}
+/* Evict one element from the set to make room for a new one (forceadd) */
+static void __maybe_unused
+mtype_remove_random(struct ip_set *set, struct htype *h)
+{
+ struct rhashtable_iter hti;
+ struct mtype_rht_elem *e;
+ bool removed = false;
+
+ rhashtable_walk_enter(&h->ht, &hti);
+ rhashtable_walk_start(&hti);
+ e = rhashtable_walk_next(&hti);
+ if (IS_ERR(e))
+ e = NULL;
+
+ if (e && !rhashtable_remove_fast(&h->ht, &e->node, mtype_rht_params))
+ removed = true;
+
+ rhashtable_walk_stop(&hti);
+ rhashtable_walk_exit(&hti);
+
+ if (removed) {
+ mtype_del_cidr_all(set, h, &e->elem);
+ ip_set_ext_destroy_slow(set, &e->elem);
+ kfree_rcu(e, rcu);
+ }
+}
+
/* Add an element to a hash and update the internal counters when succeeded,
* otherwise report the proper error code.
*/
@@ -573,11 +602,15 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
rcu_read_unlock();
if (atomic_read(&h->ht.nelems) >= h->maxelem) {
- if (net_ratelimit())
- pr_warn("Set %s is full, maxelem %u reached\n",
- set->name, h->maxelem);
- mtype_data_next(&h->next, d);
- return -IPSET_ERR_HASH_FULL;
+ if (SET_WITH_FORCEADD(set)) {
+ mtype_remove_random(set, h);
+ } else {
+ if (net_ratelimit())
+ pr_warn("Set %s is full, maxelem %u reached\n",
+ set->name, h->maxelem);
+ mtype_data_next(&h->next, d);
+ return -IPSET_ERR_HASH_FULL;
+ }
}
e = kzalloc(offsetof(struct mtype_rht_elem, elem) + set->dsize,
--
2.54.0
next prev parent reply other threads:[~2026-07-14 13:19 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 13:18 [PATCH RFC nf-next 00/12] netfilter: ipset: convert to rhashtable Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 01/12] netfilter: ipset: rework cidr bookkeeping Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 02/12] netfilter: ipset: rework cidr bookkeeping fixups Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 03/12] netfilter: ipset: add small wrappers for hash and bucket sizes Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 04/12] netfilter: ipset: add and use mtype_del_cidr_all helper Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 05/12] netfilter: ipset: add and use ip_set_init_comment_slow Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 06/12] netfilter: ipset: add and use ip_set_ext_destroy_slow Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 07/12] netfilter: ipset: add rhashtable boilerplate stubs Florian Westphal
2026-07-16 12:53 ` Jozsef Kadlecsik
2026-07-16 13:00 ` Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 08/12] netfilter: ipset: replace internal hash table with rhashtable Florian Westphal
2026-07-16 13:22 ` Jozsef Kadlecsik
2026-07-16 14:04 ` Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 09/12] netfilter: ipset: use plain rcu_read_lock Florian Westphal
2026-07-16 13:41 ` Jozsef Kadlecsik
2026-07-16 14:05 ` Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 10/12] netfilter: ipset: use correct lockdep annotation in ipset_dereference Florian Westphal
2026-07-16 13:56 ` Jozsef Kadlecsik
2026-07-16 14:07 ` Florian Westphal
2026-07-14 13:18 ` [PATCH RFC nf-next 11/12] netfilter: ipset: remove last region lock usage Florian Westphal
2026-07-16 14:01 ` Jozsef Kadlecsik
2026-07-16 14:08 ` Florian Westphal
2026-07-16 14:17 ` Jozsef Kadlecsik
2026-07-16 14:52 ` Florian Westphal
2026-07-14 13:18 ` Florian Westphal [this message]
2026-07-16 14:06 ` [PATCH RFC nf-next 12/12] netfilter: ipset: re-add forceadd support for rhashtable Jozsef Kadlecsik
2026-07-14 15:52 ` [PATCH RFC nf-next 00/12] netfilter: ipset: convert to rhashtable Jozsef Kadlecsik
2026-07-15 5:54 ` [syzbot ci] " syzbot ci
2026-07-16 13:02 ` Florian Westphal
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=20260714131828.10685-13-fw@strlen.de \
--to=fw@strlen.de \
--cc=kadlec@netfilter.org \
--cc=netfilter-devel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.