Skip to contentSkip to conversation

netfilter: nft_set_rbtree: allocate same array size on updates

PATCH In review Pablo Neira Ayuso 1 participant 2 messages Mar 8

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/1 netfilter: nft_set_rbtree: allocate same array size on updates

The array resize function increments the size of the array in NFT_ARRAY_EXTRA_SIZE slots for each update, this is unnecesarily increasing the array size.

To determine the number of array slots:

- Use NFT_ARRAY_EXTRA_SIZE for new sets.
- Use the current maximum number of intervals in the live array.

Fixes 7e43e0a1141d ("netfilter: nft_set_rbtree: translate rbtree to array for binary search")

---
v2: fix crash with new sets, reported by Florian.

 net/netfilter/nft_set_rbtree.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nft_set_rbtree.c +6 -1
index 853ff30a208c..bdcea649467f 100644
@@ -646,7 +646,12 @@ static int nft_array_may_resize(const struct nft_set *set)
 	struct nft_array *array;
 
 	if (!priv->array_next) {
-		array = nft_array_alloc(nelems + NFT_ARRAY_EXTRA_SIZE);
+		if (priv->array)
+			new_max_intervals = priv->array->max_intervals;
+		else
+			new_max_intervals = NFT_ARRAY_EXTRA_SIZE;
+
+		array = nft_array_alloc(new_max_intervals);
 		if (!array)
 			return -ENOMEM;
 
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

This is an incorrect resend, apologies.

Quoted 40 lines · Pablo Neira Ayuso

The array resize function increments the size of the array in NFT_ARRAY_EXTRA_SIZE slots for each update, this is unnecesarily increasing the array size.

To determine the number of array slots:

- Use NFT_ARRAY_EXTRA_SIZE for new sets.
- Use the current maximum number of intervals in the live array.

Fixes 7e43e0a1141d ("netfilter: nft_set_rbtree: translate rbtree to array for binary search")

---
v2: fix crash with new sets, reported by Florian.

 net/netfilter/nft_set_rbtree.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nft_set_rbtree.c +6 -1
index 853ff30a208c..bdcea649467f 100644
@@ -646,7 +646,12 @@ static int nft_array_may_resize(const struct nft_set *set)
 	struct nft_array *array;
 
 	if (!priv->array_next) {
-		array = nft_array_alloc(nelems + NFT_ARRAY_EXTRA_SIZE);
+		if (priv->array)
+			new_max_intervals = priv->array->max_intervals;
+		else
+			new_max_intervals = NFT_ARRAY_EXTRA_SIZE;
+
+		array = nft_array_alloc(new_max_intervals);
 		if (!array)
 			return -ENOMEM;
 
Signature · 1 line
2.47.3