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 07/12] netfilter: ipset: add rhashtable boilerplate stubs
Date: Tue, 14 Jul 2026 15:18:23 +0200 [thread overview]
Message-ID: <20260714131828.10685-8-fw@strlen.de> (raw)
In-Reply-To: <20260714131828.10685-1-fw@strlen.de>
Preparation patch. Adds an rhashtable to the set and initialised
and destroys it. No elements are ever added to this hashtable.
This change is supposed to be devoid of side effects and is
separate to reduce size of the conversion patch.
Assisted-by: Claude:claude-sonnet-4-6
Signed-off-by: Florian Westphal <fw@strlen.de>
---
net/netfilter/ipset/ip_set_hash_gen.h | 104 +++++++++++++++++++++++++-
1 file changed, 102 insertions(+), 2 deletions(-)
diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 1eff2c065bb3..8f5f15fdf61b 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -8,6 +8,7 @@
#include <linux/rcupdate_wait.h>
#include <linux/jhash.h>
#include <linux/types.h>
+#include <linux/rhashtable.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/ipset/ip_set.h>
@@ -192,9 +193,16 @@ static const union nf_inet_addr zeromask = {};
#undef mtype_ahash_destroy
#undef mtype_ext_cleanup
+#undef mtype_rht_elem
+#undef mtype_rht_hashfn
+#undef mtype_rht_obj_hashfn
+#undef mtype_rht_cmpfn
+#undef mtype_rht_params
+
#undef mtype_add_cidr
#undef mtype_del_cidr
#undef mtype_del_cidr_all
+#undef mtype_flush_elem
#undef mtype_ahash_memsize
#undef mtype_flush
#undef mtype_destroy
@@ -240,9 +248,17 @@ static const union nf_inet_addr zeromask = {};
#define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
#define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
+
+#define mtype_rht_elem IPSET_TOKEN(MTYPE, _rht_elem)
+#define mtype_rht_hashfn IPSET_TOKEN(MTYPE, _rht_hashfn)
+#define mtype_rht_obj_hashfn IPSET_TOKEN(MTYPE, _rht_obj_hashfn)
+#define mtype_rht_cmpfn IPSET_TOKEN(MTYPE, _rht_cmpfn)
+#define mtype_rht_params IPSET_TOKEN(MTYPE, _rht_params)
+
#define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
#define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
#define mtype_del_cidr_all IPSET_TOKEN(MTYPE, _del_cidr_all)
+#define mtype_flush_elem IPSET_TOKEN(MTYPE, _flush_elem)
#define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
#define mtype_flush IPSET_TOKEN(MTYPE, _flush)
#define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
@@ -275,6 +291,60 @@ static const union nf_inet_addr zeromask = {};
#define htype MTYPE
+/* Per-element rhashtable object. Extensions follow the elem field inline;
+ * allocate as offsetof(struct mtype_rht_elem, elem) + set->dsize bytes.
+ */
+struct mtype_rht_elem {
+ struct rhash_head node;
+ struct rcu_head rcu; /* deferred free after removal */
+ struct mtype_elem elem; /* element data; extensions follow */
+};
+
+/* jhash of the lookup key */
+static u32 mtype_rht_hashfn(const void *data, u32 len, u32 seed)
+{
+ BUILD_BUG_ON(HKEY_DATALEN % sizeof(u32) != 0);
+ return jhash2((const u32 *)data, HKEY_DATALEN / sizeof(u32), seed);
+}
+
+/* jhash of an existing element object */
+static u32 mtype_rht_obj_hashfn(const void *obj, u32 len, u32 seed)
+{
+ const struct mtype_rht_elem *e = obj;
+#ifdef IP_SET_HASH_WITH_NETS
+ /* Reset transient flags (e.g. nomatch) before hashing so that the
+ * object hash always equals the lookup-key hash computed by hashfn.
+ */
+ struct mtype_elem tmp;
+ u8 flags = 0;
+
+ memcpy(&tmp, &e->elem, HKEY_DATALEN);
+ mtype_data_reset_flags(&tmp, &flags);
+ return jhash2((const u32 *)&tmp, HKEY_DATALEN / sizeof(u32), seed);
+#else
+ return jhash2((const u32 *)&e->elem, HKEY_DATALEN / sizeof(u32), seed);
+#endif
+}
+
+/* 0 = key matches object (equal), non-zero = not equal */
+static int mtype_rht_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
+{
+ const struct mtype_rht_elem *e = obj;
+ u32 multi = 0;
+
+ return !mtype_data_equal(&e->elem,
+ (const struct mtype_elem *)arg->key, &multi);
+}
+
+static const struct rhashtable_params mtype_rht_params = {
+ .head_offset = offsetof(struct mtype_rht_elem, node),
+ .hashfn = mtype_rht_hashfn,
+ .obj_hashfn = mtype_rht_obj_hashfn,
+ .obj_cmpfn = mtype_rht_cmpfn,
+ .key_len = HKEY_DATALEN,
+ .automatic_shrinking = true,
+};
+
#define HKEY(data, initval, htable_bits) \
({ \
const u32 *__k = (const u32 *)data; \
@@ -288,6 +358,7 @@ static const union nf_inet_addr zeromask = {};
/* The generic hash structure */
struct htype {
struct htable __rcu *table; /* the hash table */
+ struct rhashtable ht; /* the hash table */
struct net_prefixes __rcu *rnets[IPSET_NET_COUNT]; /* cidr prefixes */
struct htable_gc gc; /* gc workqueue */
u32 maxelem; /* max elements in the hash */
@@ -424,6 +495,16 @@ mtype_del_cidr_all(struct ip_set *set, struct htype *h, const struct mtype_elem
#endif
}
+/* Free one element: called by rhashtable_free_and_destroy */
+static void
+mtype_flush_elem(void *ptr, void *arg)
+{
+ struct ip_set *set = arg;
+ struct mtype_rht_elem *e = ptr;
+
+ ip_set_ext_destroy(set, &e->elem);
+ kfree_rcu(e, rcu);
+}
/* Calculate the actual memory size of the set data */
static size_t
mtype_ahash_memsize(const struct htype *h, const struct htable *t)
@@ -458,6 +539,9 @@ mtype_flush(struct ip_set *set)
struct hbucket *n;
u32 r, i;
+ rhashtable_free_and_destroy(&h->ht, mtype_flush_elem, set);
+ rhashtable_init(&h->ht, &mtype_rht_params);
+
t = ipset_dereference_nfnl(h->table);
for (r = 0; r < ahash_numof_locks(t->htable_bits); r++) {
spin_lock_bh(&t->hregion[r].lock);
@@ -530,6 +614,8 @@ mtype_destroy(struct ip_set *set)
struct htable *t = (__force struct htable *)h->table;
struct list_head *l, *lt;
+ rhashtable_free_and_destroy(&h->ht, mtype_flush_elem, set);
+
list_for_each_safe(l, lt, &t->ad) {
list_del(l);
kfree(l);
@@ -1580,6 +1666,7 @@ static int
IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
struct nlattr *tb[], u32 flags)
{
+ struct rhashtable_params params;
u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
#ifdef IP_SET_HASH_WITH_MARKMASK
u32 markmask;
@@ -1596,6 +1683,7 @@ IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
size_t hsize;
struct htype *h;
struct htable *t;
+ int err;
u32 i;
pr_debug("Create set %s with family %s\n",
@@ -1686,6 +1774,16 @@ IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
if (!h)
return -ENOMEM;
+ /* Initialize rhashtable with the user-requested size as hint */
+ params = mtype_rht_params;
+ params.nelem_hint = hashsize;
+ /* maxsize: maximum bucket table size to expand to */
+ params.max_size = maxelem;
+
+ err = rhashtable_init(&h->ht, ¶ms);
+ if (err)
+ goto free_h;
+
/* Compute htable_bits from the user input parameter hashsize.
* Assume that hashsize == 2^htable_bits,
* otherwise round up to the first 2^n value.
@@ -1693,10 +1791,10 @@ IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
hbits = fls(hashsize - 1);
hsize = htable_size(hbits);
if (hsize == 0)
- goto free_h;
+ goto free_rht;
t = ip_set_alloc(hsize);
if (!t)
- goto free_h;
+ goto free_rht;
t->hregion = ip_set_alloc(ahash_sizeof_regions(hbits));
if (!t->hregion)
goto free_t;
@@ -1782,6 +1880,8 @@ IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
#endif
free_t:
ip_set_free(t);
+free_rht:
+ rhashtable_free_and_destroy(&h->ht, mtype_flush_elem, set);
free_h:
kfree(h);
return -ENOMEM;
--
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 ` Florian Westphal [this message]
2026-07-16 12:53 ` [PATCH RFC nf-next 07/12] netfilter: ipset: add rhashtable boilerplate stubs 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 ` [PATCH RFC nf-next 12/12] netfilter: ipset: re-add forceadd support for rhashtable Florian Westphal
2026-07-16 14:06 ` 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-8-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.