Skip to contentSkip to conversation

netfilter: nf_tables: don't skip expired elements during walk

PATCH New Pablo Neira Ayuso 2 participants 6 messages Aug 9, 2023

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/5 netfilter: nf_tables: don't skip expired elements during walk

From: Florian Westphal <[email protected]>

There is an asymmetry between commit/abort and preparation phase if the following conditions are met:

1. set is a verdict map ("1.2.3.4 : jump foo")
2. timeouts are enabled

In this case, following sequence is problematic:

1. element E in set S refers to chain C
2. userspace requests removal of set S
3. kernel does a set walk to decrement chain->use count for all elements from preparation phase
4. kernel does another set walk to remove elements from the commit phase (or another walk to do a chain->use increment for all elements from abort phase)

If E has already expired in 1), it will be ignored during list walk, so its use count won't have been changed.

Then, when set is culled, ->destroy callback will zap the element via nf_tables_set_elem_destroy(), but this function is only safe for elements that have been deactivated earlier from the preparation phase:
lack of earlier deactivate removes the element but leaks the chain use count, which results in a WARN splat when the chain gets removed later, plus a leak of the nft_chain structure.

Update pipapo_get() not to skip expired elements, otherwise flush command reports bogus ENOENT errors.

Fixes 3c4287f62044 ("nf_tables: Add set type for arbitrary concatenation of ranges")
Fixes 8d8540c4f5e0 ("netfilter: nft_set_rbtree: add timeout support")
Fixes 9d0982927e79 ("netfilter: nft_hash: add support for timeouts")
---
 net/netfilter/nf_tables_api.c  |  4 ++++
 net/netfilter/nft_set_hash.c   |  2 --
 net/netfilter/nft_set_pipapo.c | 18 ++++++++++++------
 net/netfilter/nft_set_rbtree.c |  2 --
 4 files changed, 16 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +4 -0
index d3c6ecd1f5a6..b4321869e5c6 100644
@@ -5602,8 +5602,12 @@ static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
 				  const struct nft_set_iter *iter,
 				  struct nft_set_elem *elem)
 {
+	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
 	struct nft_set_dump_args *args;
 
+	if (nft_set_elem_expired(ext))
+		return 0;
+
 	args = container_of(iter, struct nft_set_dump_args, iter);
 	return nf_tables_fill_setelem(args->skb, set, elem, args->reset);
 }
net/netfilter/nft_set_hash.c +0 -2
index 0b73cb0e752f..24caa31fa231 100644
@@ -278,8 +278,6 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
 
 		if (iter->count < iter->skip)
 			goto cont;
-		if (nft_set_elem_expired(&he->ext))
-			goto cont;
 		if (!nft_set_elem_active(&he->ext, iter->genmask))
 			goto cont;
 
net/netfilter/nft_set_pipapo.c +12 -6
index 49915a2a58eb..d54784ea465b 100644
@@ -566,8 +566,7 @@ static struct nft_pipapo_elem *pipapo_get(const struct net *net,
 			goto out;
 
 		if (last) {
-			if (nft_set_elem_expired(&f->mt[b].e->ext) ||
-			    (genmask &&
+			if ((genmask &&
 			     !nft_set_elem_active(&f->mt[b].e->ext, genmask)))
 				goto next_match;
 
@@ -601,8 +600,17 @@ static struct nft_pipapo_elem *pipapo_get(const struct net *net,
 static void *nft_pipapo_get(const struct net *net, const struct nft_set *set,
 			    const struct nft_set_elem *elem, unsigned int flags)
 {
-	return pipapo_get(net, set, (const u8 *)elem->key.val.data,
-			  nft_genmask_cur(net));
+	struct nft_pipapo_elem *ret;
+
+	ret = pipapo_get(net, set, (const u8 *)elem->key.val.data,
+			 nft_genmask_cur(net));
+	if (IS_ERR(ret))
+		return ret;
+
+	if (nft_set_elem_expired(&ret->ext))
+		return ERR_PTR(-ENOENT);
+
+	return ret;
 }
 
 /**
@@ -2005,8 +2013,6 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,
 			goto cont;
 
 		e = f->mt[r].e;
-		if (nft_set_elem_expired(&e->ext))
-			goto cont;
 
 		elem.priv = e;
 
net/netfilter/nft_set_rbtree.c +0 -2
index 8d73fffd2d09..39956e5341c9 100644
@@ -552,8 +552,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,
 
 		if (iter->count < iter->skip)
 			goto cont;
-		if (nft_set_elem_expired(&rbe->ext))
-			goto cont;
 		if (!nft_set_elem_active(&rbe->ext, iter->genmask))
 			goto cont;
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/5 netfilter: nf_tables: GC transaction API to avoid race with control plane

The set types 'rhashtable' and 'rbtree' use a GC worker to reclaim memory. From system work queue, in periodic intervals, a scan of the table is done.

The major caveat here is that the nft transaction mutex is not held. This causes a race between control plane and GC when they attempt to delete the same element.

We cannot grab the netlink mutex from the work queue, because the GC work queue. Else we would run into following deadlock:

cpu 1 cpu2
GC work transaction comes in , lock nft mutex
`acquire nft mutex // BLOCKS
transaction asks to remove the set set destruction calls cancel_work_sync()

cancel_work_sync will now block forever, because its waiting for the mutex the caller already owns.

This patch adds a new API that deals with garbage collection in two steps:

1) Lockless GC of expired elements sets on the NFT_SET_ELEM_DEAD_BIT so they are not visible via lookup. Annotate current GC sequence in the GC transaction. Enqueue GC transaction work as soon as it is full. If ruleset is updated, then GC is aborted and retried later.

2) When work callback runs then grab the mutex. If GC sequence has changed then this GC translation lost race with control plane, abort it as it contains stale references to objects and let GC try again later. If the ruleset is intact, then this GC transaction deactivates and removes the elements and it uses call_rcu() to destroy elements.

Note that no elements are removed from GC lockless path, the _DEAD bit is set and pointers are collected. GC catchall does not remove the elements anymore too. There is a new set->dead flag that is set on to abort the GC transaction to deal with set->ops->destroy() path which removes the remaining elements in the set from commit_release, where no mutex is held.

To deal with GC when mutex is held, which allows safe deactivate and removal, add sync GC API which releases the set element object via call_rcu(). This is used by rbtree and pipapo backends which also perform garbage collection from control plane path.

Since element removal from sets can happen from control plane and element garbage collection/timeout, it is necessary to keep the set structure alive until all elements have been deactivated and destroyed.

We cannot do a cancel_work_sync or flush_work in nft_set_destroy because its called with the transaction mutex held, but the aforementioned async work queue might be blocked on the very mutex that nft_set_destroy() callchain is sitting on.

This gives us the choice of ABBA deadlock or UaF.

To avoid both, add set->refs refcount_t member. The GC API can then increment the set refcount and release it once the elements have been free'd.

Set backends are adapted to use the GC transaction API in a follow up patch entitled:

("netfilter: nf_tables: use gc transaction API in set backends")

This is joint work with Florian Westphal.

Fixes cfed7e1b1f8e ("netfilter: nf_tables: add set garbage collection helpers")
---
 include/net/netfilter/nf_tables.h |  64 +++++++-
 net/netfilter/nf_tables_api.c     | 248 ++++++++++++++++++++++++++++--
 2 files changed, 300 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +63 -1
index 640441a2f926..7256e9c80477 100644
@@ -512,6 +512,7 @@ struct nft_set_elem_expr {
  *
  *	@list: table set list node
  *	@bindings: list of set bindings
+ *	@refs: internal refcounting for async set destruction
  *	@table: table this set belongs to
  *	@net: netnamespace this set belongs to
  * 	@name: name of the set
@@ -541,6 +542,7 @@ struct nft_set_elem_expr {
 struct nft_set {
 	struct list_head		list;
 	struct list_head		bindings;
+	refcount_t			refs;
 	struct nft_table		*table;
 	possible_net_t			net;
 	char				*name;
@@ -562,7 +564,8 @@ struct nft_set {
 	struct list_head		pending_update;
 	/* runtime data below here */
 	const struct nft_set_ops	*ops ____cacheline_aligned;
-	u16				flags:14,
+	u16				flags:13,
+					dead:1,
 					genmask:2;
 	u8				klen;
 	u8				dlen;
@@ -1592,6 +1595,32 @@ static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
 	clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
 }
 
+#define NFT_SET_ELEM_DEAD_MASK	(1 << 3)
+
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+#define NFT_SET_ELEM_DEAD_BIT	3
+#elif defined(__BIG_ENDIAN_BITFIELD)
+#define NFT_SET_ELEM_DEAD_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 3)
+#else
+#error
+#endif
+
+static inline void nft_set_elem_dead(struct nft_set_ext *ext)
+{
+	unsigned long *word = (unsigned long *)ext;
+
+	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
+	set_bit(NFT_SET_ELEM_DEAD_BIT, word);
+}
+
+static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
+{
+	unsigned long *word = (unsigned long *)ext;
+
+	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
+	return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
+}
+
 /**
  *	struct nft_trans - nf_tables object update in transaction
  *
@@ -1732,6 +1761,38 @@ struct nft_trans_flowtable {
 #define nft_trans_flowtable_flags(trans)	\
 	(((struct nft_trans_flowtable *)trans->data)->flags)
 
+#define NFT_TRANS_GC_BATCHCOUNT	256
+
+struct nft_trans_gc {
+	struct list_head	list;
+	struct net		*net;
+	struct nft_set		*set;
+	u32			seq;
+	u8			count;
+	void			*priv[NFT_TRANS_GC_BATCHCOUNT];
+	struct rcu_head		rcu;
+};
+
+struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
+					unsigned int gc_seq, gfp_t gfp);
+void nft_trans_gc_destroy(struct nft_trans_gc *trans);
+
+struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
+					      unsigned int gc_seq, gfp_t gfp);
+void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
+
+struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
+void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
+
+void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
+
+struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc,
+					   unsigned int gc_seq);
+
+void nft_setelem_data_deactivate(const struct net *net,
+				 const struct nft_set *set,
+				 struct nft_set_elem *elem);
+
 int __init nft_chain_filter_init(void);
 void nft_chain_filter_fini(void);
 
@@ -1758,6 +1819,7 @@ struct nftables_pernet {
 	struct mutex		commit_mutex;
 	u64			table_handle;
 	unsigned int		base_seq;
+	unsigned int		gc_seq;
 };
 
 extern unsigned int nf_tables_net_id;
net/netfilter/nf_tables_api.c +237 -11

353 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 3/5 netfilter: nf_tables: adapt set backend to use GC transaction API

In reply to Pablo Neira Ayuso

Use the GC transaction API to replace the old and buggy gc API and the busy mark approach.

No set elements are removed from async garbage collection anymore, instead the _DEAD bit is set on so the set element is not visible from lookup path anymore. Async GC enqueues transaction work that might be aborted and retried later.

rbtree and pipapo set backends does not set on the _DEAD bit from the sync GC path since this runs in control plane path where mutex is held. In this case, set elements are deactivated, removed and then released via RCU callback, sync GC never fails.

Fixes 3c4287f62044 ("nf_tables: Add set type for arbitrary concatenation of ranges")
Fixes 8d8540c4f5e0 ("netfilter: nft_set_rbtree: add timeout support")
Fixes 9d0982927e79 ("netfilter: nft_hash: add support for timeouts")
---
 net/netfilter/nf_tables_api.c  |   7 +-
 net/netfilter/nft_set_hash.c   |  77 +++++++++++-------
 net/netfilter/nft_set_pipapo.c |  48 ++++++++---
 net/netfilter/nft_set_rbtree.c | 142 ++++++++++++++++++++-------------
 4 files changed, 173 insertions(+), 101 deletions(-)
net/netfilter/nf_tables_api.c +2 -5
index c28bacb9479b..fd4b5da7ac3c 100644
@@ -6380,7 +6380,6 @@ static void nft_setelem_activate(struct net *net, struct nft_set *set,
 
 	if (nft_setelem_is_catchall(set, elem)) {
 		nft_set_elem_change_active(net, set, ext);
-		nft_set_elem_clear_busy(ext);
 	} else {
 		set->ops->activate(net, set, elem);
 	}
@@ -6395,8 +6394,7 @@ static int nft_setelem_catchall_deactivate(const struct net *net,
 
 	list_for_each_entry(catchall, &set->catchall_list, list) {
 		ext = nft_set_elem_ext(set, catchall->elem);
-		if (!nft_is_active(net, ext) ||
-		    nft_set_elem_mark_busy(ext))
+		if (!nft_is_active(net, ext))
 			continue;
 
 		kfree(elem->priv);
@@ -7109,8 +7107,7 @@ static int nft_set_catchall_flush(const struct nft_ctx *ctx,
 
 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
 		ext = nft_set_elem_ext(set, catchall->elem);
-		if (!nft_set_elem_active(ext, genmask) ||
-		    nft_set_elem_mark_busy(ext))
+		if (!nft_set_elem_active(ext, genmask))
 			continue;
 
 		elem.priv = catchall->elem;
net/netfilter/nft_set_hash.c +48 -29

140 lines not on this page · raw message

net/netfilter/nft_set_pipapo.c +36 -12

83 lines not on this page · raw message

net/netfilter/nft_set_rbtree.c +87 -55

231 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH nf 3/5] netfilter: nf_tables: adapt set backend to use GC transaction API

kernel test robot noticed the following build warnings:

Hi Pablo,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.5-rc5 next-20230809]
[cannot apply to nf/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-nf_tables-GC-transaction-API-to-avoid-race-with-control-plane/20230809-211751
base:   linus/master
patch link:    https://lore.kernel.org/r/20230809131546.8598-3-pablo%40netfilter.org
patch subject: [PATCH nf 3/5] netfilter: nf_tables: adapt set backend to use GC transaction API
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230809/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230809/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

net/netfilter/nft_set_rbtree.c: In function 'nft_rbtree_gc':

>> net/netfilter/nft_set_rbtree.c:607:12: warning: variable 'genmask' set but not used [-Wunused-but-set-variable]
     607 |         u8 genmask;
         |            ^~~~~~~

vim +/genmask +607 net/netfilter/nft_set_rbtree.c

20a69341f2d00c net/netfilter/nft_rbtree.c     Patrick McHardy   2013-10-11  596  
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  597  static void nft_rbtree_gc(struct work_struct *work)
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  598  {
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  599  	struct nft_rbtree_elem *rbe, *rbe_end = NULL;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  600  	struct nftables_pernet *nft_net;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  601  	struct nft_rbtree *priv;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  602  	struct nft_trans_gc *gc;
a13f814a67b12a net/netfilter/nft_set_rbtree.c Taehee Yoo        2018-08-30  603  	struct rb_node *node;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  604  	struct nft_set *set;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  605  	unsigned int gc_seq;
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  606  	struct net *net;
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14 @607  	u8 genmask;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  608  
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  609  	priv = container_of(work, struct nft_rbtree, gc_work.work);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  610  	set  = nft_set_container_of(priv);
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  611  	net  = read_pnet(&set->net);
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  612  	genmask = nft_genmask_cur(net);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  613  	nft_net = nft_pernet(net);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  614  	gc_seq  = READ_ONCE(nft_net->gc_seq);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  615  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  616  	gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  617  	if (!gc)
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  618  		goto done;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  619  
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  620  	write_lock_bh(&priv->lock);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  621  	write_seqcount_begin(&priv->count);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  622  	for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  623  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  624  		/* Ruleset has been updated, try later. */
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  625  		if (READ_ONCE(nft_net->gc_seq) != gc_seq) {
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  626  			nft_trans_gc_destroy(gc);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  627  			gc = NULL;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  628  			goto try_later;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  629  		}
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  630  
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  631  		rbe = rb_entry(node, struct nft_rbtree_elem, node);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  632  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  633  		if (nft_set_elem_is_dead(&rbe->ext))
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  634  			goto dead_elem;
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  635  
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  636  		/* elements are reversed in the rbtree for historical reasons,
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  637  		 * from highest to lowest value, that is why end element is
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  638  		 * always visited before the start element.
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  639  		 */
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  640  		if (nft_rbtree_interval_end(rbe)) {
a13f814a67b12a net/netfilter/nft_set_rbtree.c Taehee Yoo        2018-08-30  641  			rbe_end = rbe;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  642  			continue;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  643  		}
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  644  		if (!nft_set_elem_expired(&rbe->ext))
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  645  			continue;
5d235d6ce75c12 net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-01-14  646  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  647  		nft_set_elem_dead(&rbe->ext);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  648  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  649  		if (!rbe_end)
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  650  			continue;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  651  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  652  		nft_set_elem_dead(&rbe_end->ext);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  653  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  654  		gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  655  		if (!gc)
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  656  			goto try_later;
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  657  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  658  		nft_trans_gc_elem_add(gc, rbe_end);
a13f814a67b12a net/netfilter/nft_set_rbtree.c Taehee Yoo        2018-08-30  659  		rbe_end = NULL;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  660  dead_elem:
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  661  		gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  662  		if (!gc)
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  663  			goto try_later;
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  664  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  665  		nft_trans_gc_elem_add(gc, rbe);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  666  	}
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  667  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  668  	gc = nft_trans_gc_catchall(gc, gc_seq);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  669  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  670  try_later:
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  671  	write_seqcount_end(&priv->count);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  672  	write_unlock_bh(&priv->lock);
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  673  
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  674  	if (gc)
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  675  		nft_trans_gc_queue_async_done(gc);
9cbd4eae38ba6c net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2023-08-09  676  done:
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  677  	queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  678  			   nft_set_gc_interval(set));
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  679  }
8d8540c4f5e03d net/netfilter/nft_set_rbtree.c Pablo Neira Ayuso 2018-05-16  680  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#4 lore raw

PATCH 4/5 netfilter: nft_set_hash: mark set element as dead when deleting from packet path

In reply to Pablo Neira Ayuso

Set on the NFT_SET_ELEM_DEAD_BIT flag on this element, instead of performing element removal which might race with an ongoing transaction. Enable gc when dynamic flag is set on since dynset deletion requires garbage collection after this patch.

Fixes d0a8d877da97 ("netfilter: nft_dynset: support for element deletion")
---
 net/netfilter/nft_set_hash.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nft_set_hash.c +4 -2
index 2f067e4596b0..cef5df846000 100644
@@ -249,7 +249,9 @@ static bool nft_rhash_delete(const struct nft_set *set,
 	if (he == NULL)
 		return false;
 
-	return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0;
+	nft_set_elem_dead(&he->ext);
+
+	return true;
 }
 
 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
@@ -412,7 +414,7 @@ static int nft_rhash_init(const struct nft_set *set,
 		return err;
 
 	INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
-	if (set->flags & NFT_SET_TIMEOUT)
+	if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL))
 		nft_rhash_gc_init(set);
 
 	return 0;
Signature · 1 line
2.30.2
author maintainer
#5 lore raw

PATCH 5/5 netfilter: nf_tables: remove busy mark and gc batch API

In reply to Pablo Neira Ayuso

Ditch it, it has been replace it by the GC transaction API and it has no clients anymore.

---
 include/net/netfilter/nf_tables.h | 98 +------------------------------
 net/netfilter/nf_tables_api.c     | 48 +--------------
 2 files changed, 4 insertions(+), 142 deletions(-)
include/net/netfilter/nf_tables.h +3 -95
index 7256e9c80477..35870858ddf2 100644
@@ -599,7 +599,6 @@ struct nft_set *nft_set_lookup_global(const struct net *net,
 
 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
 					    const struct nft_set *set);
-void *nft_set_catchall_gc(const struct nft_set *set);
 
 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
 {
@@ -816,62 +815,6 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem,
 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
 				const struct nft_set *set, void *elem);
 
-/**
- *	struct nft_set_gc_batch_head - nf_tables set garbage collection batch
- *
- *	@rcu: rcu head
- *	@set: set the elements belong to
- *	@cnt: count of elements
- */
-struct nft_set_gc_batch_head {
-	struct rcu_head			rcu;
-	const struct nft_set		*set;
-	unsigned int			cnt;
-};
-
-#define NFT_SET_GC_BATCH_SIZE	((PAGE_SIZE -				  \
-				  sizeof(struct nft_set_gc_batch_head)) / \
-				 sizeof(void *))
-
-/**
- *	struct nft_set_gc_batch - nf_tables set garbage collection batch
- *
- * 	@head: GC batch head
- * 	@elems: garbage collection elements
- */
-struct nft_set_gc_batch {
-	struct nft_set_gc_batch_head	head;
-	void				*elems[NFT_SET_GC_BATCH_SIZE];
-};
-
-struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
-						gfp_t gfp);
-void nft_set_gc_batch_release(struct rcu_head *rcu);
-
-static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
-{
-	if (gcb != NULL)
-		call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
-}
-
-static inline struct nft_set_gc_batch *
-nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
-		       gfp_t gfp)
-{
-	if (gcb != NULL) {
-		if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
-			return gcb;
-		nft_set_gc_batch_complete(gcb);
-	}
-	return nft_set_gc_batch_alloc(set, gfp);
-}
-
-static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
-					void *elem)
-{
-	gcb->elems[gcb->head.cnt++] = elem;
-}
-
 struct nft_expr_ops;
 /**
  *	struct nft_expr_type - nf_tables expression type
@@ -1560,47 +1503,12 @@ static inline void nft_set_elem_change_active(const struct net *net,
 
 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
 
-/*
- * We use a free bit in the genmask field to indicate the element
- * is busy, meaning it is currently being processed either by
- * the netlink API or GC.
- *
- * Even though the genmask is only a single byte wide, this works
- * because the extension structure if fully constant once initialized,
- * so there are no non-atomic write accesses unless it is already
- * marked busy.
- */
-#define NFT_SET_ELEM_BUSY_MASK	(1 << 2)
-
-#if defined(__LITTLE_ENDIAN_BITFIELD)
-#define NFT_SET_ELEM_BUSY_BIT	2
-#elif defined(__BIG_ENDIAN_BITFIELD)
-#define NFT_SET_ELEM_BUSY_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 2)
-#else
-#error
-#endif
-
-static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
-{
-	unsigned long *word = (unsigned long *)ext;
-
-	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
-	return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
-}
-
-static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
-{
-	unsigned long *word = (unsigned long *)ext;
-
-	clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
-}
-
-#define NFT_SET_ELEM_DEAD_MASK	(1 << 3)
+#define NFT_SET_ELEM_DEAD_MASK	(1 << 2)
 
 #if defined(__LITTLE_ENDIAN_BITFIELD)
-#define NFT_SET_ELEM_DEAD_BIT	3
+#define NFT_SET_ELEM_DEAD_BIT	2
 #elif defined(__BIG_ENDIAN_BITFIELD)
-#define NFT_SET_ELEM_DEAD_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 3)
+#define NFT_SET_ELEM_DEAD_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 2)
 #else
 #error
 #endif
net/netfilter/nf_tables_api.c +1 -47

66 lines not on this page · raw message

Signature · 1 line
2.30.2