Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.245.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796058217; b=FZWcWawmvxPa1ivQ0roErgWXOyRejXd3gPqwH4ilwuAPsLvcDeKIvBstuJ9VWh3AqpsQ 7geIbKbvYneu0rZ15P89lvEbKFz7TvMK6jXbklYWmvlrX5fShIWeFDJNmaSK7CQgMH/CN hIPMOiYJLJfF94nQMLfGYLBoA1ZGsoC5j8Kj9xoXh/ndwC4H1KgE2I6QEMQ/4ARm2ON9U ctIyNuZlaJOJqAgwBbBd8+n88eZPv7xKBFcpONL/vgAYvNs1WD0U07Cka4ZUrbS7R6VTV Kpfj4fvpXecrcrNft648QPRn8AzaZa+CtXzMJ2YEU0Fcbo54j2+hPYv4DngBI+nlRmw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796058217; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=22g6zOmIs3NXGz0GU5ul9eWYk5cAIpRHxewePb2wcg0=; b=qnZ9JECBRqCdI7kr26QdIN4H+gO48wOkI09hWLrVIAIgcsqfz0BfC+SLMaN2ZENlN7kl gOl0pEqmedM5ld8WJnUWaldceuWiyu2vz9yv7Uslp+ucKOCqBXXwI5GVmQ4UB9isEvPGI DEAHvHyOFfG4SjEtsBdroOdq1jqqtk5+qmzdhdId1uYsSUqc7O8Y+yT5XDNyHwMXvPYaC ogQi0v4YlBUuW0fb+9Y0MFx888mfmV87bWq8SsVGnkVAFt/zTNJ6pWAp7D5iNpcrS7i8f EzCkUGXyqJuBqMWNUHL26LjR4ZUMkSl49G0Z2CQIZ02IL/aDM+o/e4qUYbap+NYXF/w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=strlen.de; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc; arc=none smtp.remote-ip=91.216.245.30 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [91.216.245.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7FFA606A31C for ; Mon, 30 Nov 2026 17:03:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7FFA606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=strlen.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc Received: by Chamillionaire.breakpoint.cc (Postfix, from userid 1003) id 5A4D36045B; Mon, 30 Nov 2026 18:03:35 +0100 (CET) From: Florian Westphal To: Cc: Florian Westphal Subject: [PATCH nf-next v12 5/5] netfilter: ipset: re-add forceadd support Date: Mon, 30 Nov 2026 18:00:17 +0100 Message-ID: <20261130170017.28897-6-fw@strlen.de> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130170017.28897-1-fw@strlen.de> References: <20261130170017.28897-1-fw@strlen.de> X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The rhashtable conversion removed the SET_WITH_FORCEADD eviction logic. Add mtype_remove_random() helper to look up a random key slot: a group for IP_SET_HASH_WITH_MULTI sets, or a plain element otherwise. If there is one, try to evict it and allow add of the new element just like before the rhashtable conversion. ipset "forceadd" documentation says: "When sets created with this option become full the next addition to the set may succeed and evict a random entry from the set." IOW, there is no guarantee of sucessful eviction or probability. Arguments could be made both for higher or lower eviction probability. Due to lack of specification simple one-shot-random-draw is used. Signed-off-by: Florian Westphal --- No changes. net/netfilter/ipset/ip_set_hash_gen.h | 101 +++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 4 deletions(-) diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h index f362ccbafd76..193a5e87989f 100644 --- a/net/netfilter/ipset/ip_set_hash_gen.h +++ b/net/netfilter/ipset/ip_set_hash_gen.h @@ -165,6 +165,8 @@ static const union nf_inet_addr zeromask = {}; #undef mtype_kadt #undef mtype_uadt +#undef mtype_remove_random +#undef mtype_remove_cmpfn #undef mtype_warn_full #undef mtype_new_elem #undef mtype_add_replace_old @@ -235,6 +237,8 @@ static const union nf_inet_addr zeromask = {}; #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt) #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt) +#define mtype_remove_random IPSET_TOKEN(MTYPE, _remove_random) +#define mtype_remove_cmpfn IPSET_TOKEN(MTYPE, _remove_cmpfn) #define mtype_warn_full IPSET_TOKEN(MTYPE, _warn_full) #define mtype_new_elem IPSET_TOKEN(MTYPE, _new_elem) #define mtype_add_replace_old IPSET_TOKEN(MTYPE, _add_replace_old) @@ -994,6 +998,93 @@ mtype_rht_size(struct ip_set *set, u32 *elements, size_t *ext_size) #endif } +static int mtype_remove_cmpfn(struct rhashtable_compare_arg *arg, const void *obj) +{ + return 0; /* always match */ +} + +/** + * mtype_remove_random() - Remove a random element from the set (forceadd) + * @set: Pointer to the ip_set + * @locked: Group whose lock the caller holds, or NULL. + * + * When sets created with forceadd option become full the next addition + * to the set may succeed and evict a random entry from the set. + * Best-effort: no linear scan; 'return false' is fine. + * + * Return: true if an element was evicted, false otherwise. + */ +static bool +mtype_remove_random(struct ip_set *set, const void *locked) +{ + u32 k[HKEY_DATALEN / sizeof(u32)]; + struct htype *h = set->data; + + /* make sure k[] doesn't eat too much stack space */ + BUILD_BUG_ON(sizeof(k) > 64); + + /* The real hashfn is needed instead of a "get_random_u32" stub, + * because rhashtable_lookup() might fail to constant-fold the + * params below, in which case it would fall back to h->params + * hashfn. + */ + get_random_bytes(k, sizeof(k)); + +#ifdef IP_SET_HASH_WITH_MULTI + { + static const struct rhashtable_params ip_set_hash_grp_rnd_params = { + .head_offset = offsetof(struct mtype_group, node), + .key_offset = offsetof(struct mtype_group, key), + .hashfn = mtype_rht_hashfn, + .obj_hashfn = mtype_group_obj_hashfn, + .obj_cmpfn = mtype_remove_cmpfn, + .key_len = HKEY_DATALEN, + }; + struct mtype_group *grp; + struct mtype_rht_elem *e; + + grp = rhashtable_lookup(&h->ht, k, ip_set_hash_grp_rnd_params); + if (!grp || grp == locked) + return false; + + /* The group the caller holds is skipped above. Another + * group's lock may be held by a concurrent add: don't wait. + * Best-effort: no linear scan, 'return false' is fine. + */ + 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; + } +#else + { + static const struct rhashtable_params ip_set_hash_rnd_params = { + .head_offset = offsetof(struct mtype_rht_elem, node), + .key_offset = offsetof(struct mtype_rht_elem, elem), + .hashfn = mtype_rht_hashfn, + .obj_hashfn = mtype_rht_obj_hashfn, + .obj_cmpfn = mtype_remove_cmpfn, + .key_len = HKEY_DATALEN, + }; + struct mtype_rht_elem *e; + + 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; + + ipset_hash_elem_destroy_free(set, e); + return true; + } +#endif +} + /** * mtype_new_elem() - allocate and initialize a replacement/new element * @set: Pointer to the ip_set @@ -1003,6 +1094,7 @@ mtype_rht_size(struct ip_set *set, u32 *elements, size_t *ext_size) * @flags: Add flags (IPSET_FLAG_*) * @old: The element we're replacing (can be NULL) * @flag_exist: Whether the request carried IPSET_FLAG_EXIST + * @locked: Group whose lock the caller holds (multi types), or NULL * * Common to both the plain rhashtable and the "multi" group element * insertion paths: validates the maxelem condition, allocates @@ -1015,7 +1107,8 @@ mtype_rht_size(struct ip_set *set, u32 *elements, size_t *ext_size) static struct mtype_rht_elem * mtype_new_elem(struct ip_set *set, struct htype *h, const struct mtype_elem *d, const struct ip_set_ext *ext, u32 flags, - const struct mtype_rht_elem *old, bool flag_exist) + const struct mtype_rht_elem *old, bool flag_exist, + const void *locked) { bool flag_replace = !!old; struct mtype_rht_elem *e; @@ -1037,7 +1130,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, locked)) { mtype_warn_full(set, h); return ERR_PTR(-EMFILE); } @@ -1180,7 +1273,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, goto out_rcu_unlock; } - e = mtype_new_elem(set, h, d, ext, flags, old, flag_exist); + e = mtype_new_elem(set, h, d, ext, flags, old, flag_exist, grp); if (IS_ERR(e)) { mtype_group_unlock(h, grp); ret = PTR_ERR(e); @@ -1208,7 +1301,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, old = NULL; } - e = mtype_new_elem(set, h, d, ext, flags, old, flag_exist); + e = mtype_new_elem(set, h, d, ext, flags, old, flag_exist, NULL); if (IS_ERR(e)) { ret = PTR_ERR(e); goto out_rcu_unlock; -- 2.56.1