Skip to contentSkip to conversation

netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations

PATCH In review Patrick McHardy 2 participants 4 messages Mar 7, 2014 – Mar 26, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations

We currently have a limit of 8 * PAGE_SIZE anonymous sets. Lift that limit by continuing the scan if the entire page is exhausted.

---
 net/netfilter/nf_tables_api.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +10 -5
index f25d011..89d7ec4 100644
@@ -2004,7 +2004,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 	const struct nft_set *i;
 	const char *p;
 	unsigned long *inuse;
-	unsigned int n = 0;
+	unsigned int n = 0, min = 0;
 
 	p = strnchr(name, IFNAMSIZ, '%');
 	if (p != NULL) {
@@ -2014,23 +2014,28 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
 		if (inuse == NULL)
 			return -ENOMEM;
-
+cont:
 		list_for_each_entry(i, &ctx->table->sets, list) {
 			int tmp;
 
 			if (!sscanf(i->name, name, &tmp))
 				continue;
-			if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
+			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
 				continue;
 
-			set_bit(tmp, inuse);
+			set_bit(tmp - min, inuse);
 		}
 
 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
+		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
+			min += BITS_PER_BYTE * PAGE_SIZE;
+			memset(inuse, 0, PAGE_SIZE);
+			goto cont;
+		}
 		free_page((unsigned long)inuse);
 	}
 
-	snprintf(set->name, sizeof(set->name), name, n);
+	snprintf(set->name, sizeof(set->name), name, min + n);
 	list_for_each_entry(i, &ctx->table->sets, list) {
 		if (!strcmp(set->name, i->name))
 			return -ENFILE;
Signature · 1 line
1.8.5.3
#1 lore raw
Quoted 24 lines · Patrick McHardy

We currently have a limit of 8 * PAGE_SIZE anonymous sets. Lift that limit by continuing the scan if the entire page is exhausted.

---
 net/netfilter/nf_tables_api.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +1 -1
index f25d011..89d7ec4 100644
@@ -2004,7 +2004,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 	const struct nft_set *i;
 	const char *p;
 	unsigned long *inuse;
-	unsigned int n = 0;
+	unsigned int n = 0, min = 0;
 
 	p = strnchr(name, IFNAMSIZ, '%');
 	if (p != NULL) {
@@ -2014,23 +2014,28 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
 		if (inuse == NULL)

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2023

Quoted 10 lines
net/netfilter/nf_tables_api.c +2 -2
 			return -ENOMEM;
-
+cont:
 		list_for_each_entry(i, &ctx->table->sets, list) {
 			int tmp;
 
 			if (!sscanf(i->name, name, &tmp))
 				continue;
-			if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
+			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)

I think we can break looping if tmp >= min + BITS_PER_BYTE * PAGE_SIZE fulfills (assuming the list of sets is ordered).

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2033

Quoted 11 lines
net/netfilter/nf_tables_api.c +5 -1
 				continue;
 
-			set_bit(tmp, inuse);
+			set_bit(tmp - min, inuse);
 		}
 
 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
+		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
+			min += BITS_PER_BYTE * PAGE_SIZE;
+			memset(inuse, 0, PAGE_SIZE);
+			goto cont;

If the page is full, we start iterating over the set list from the beginning.

Quoted 12 lines
net/netfilter/nf_tables_api.c +2 -1
+		}
 		free_page((unsigned long)inuse);
 	}
 
-	snprintf(set->name, sizeof(set->name), name, n);
+	snprintf(set->name, sizeof(set->name), name, min + n);
 	list_for_each_entry(i, &ctx->table->sets, list) {
 		if (!strcmp(set->name, i->name))
 			return -ENFILE;
Signature · 1 line
1.8.5.3
#2 lore raw
Quoted 31 lines · Pablo Neira Ayuso
Quoted 30 lines · Patrick McHardy

We currently have a limit of 8 * PAGE_SIZE anonymous sets. Lift that limit by continuing the scan if the entire page is exhausted.

---
 net/netfilter/nf_tables_api.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +2 -2
index f25d011..89d7ec4 100644
@@ -2004,7 +2004,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 	const struct nft_set *i;
 	const char *p;
 	unsigned long *inuse;
-	unsigned int n = 0;
+	unsigned int n = 0, min = 0;
 
 	p = strnchr(name, IFNAMSIZ, '%');
 	if (p != NULL) {
@@ -2014,23 +2014,28 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
 		if (inuse == NULL)
 			return -ENOMEM;
-
+cont:
 		list_for_each_entry(i, &ctx->table->sets, list) {
 			int tmp;
 
Quoted 7 lines

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2023

Quoted 4 lines
net/netfilter/nf_tables_api.c +1 -1
 			if (!sscanf(i->name, name, &tmp))
 				continue;
-			if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
+			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)

I think we can break looping if tmp >= min + BITS_PER_BYTE * PAGE_SIZE fulfills (assuming the list of sets is ordered).

It's not, we really need to continue here.

Quoted 7 lines
Quoted 7 lines
net/netfilter/nf_tables_api.c +1 -1
 				continue;
 
-			set_bit(tmp, inuse);
+			set_bit(tmp - min, inuse);
 		}
 
Quoted 8 lines

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2033

Quoted 5 lines
net/netfilter/nf_tables_api.c +4 -0
 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
+		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
+			min += BITS_PER_BYTE * PAGE_SIZE;
+			memset(inuse, 0, PAGE_SIZE);
+			goto cont;

If the page is full, we start iterating over the set list from the beginning.

Yes, also necessary since the list is not ordered. We always pick the first free set name and add it at the end.

Quoted 13 lines
Quoted 13 lines
net/netfilter/nf_tables_api.c +2 -1
+		}
 		free_page((unsigned long)inuse);
 	}
 
-	snprintf(set->name, sizeof(set->name), name, n);
+	snprintf(set->name, sizeof(set->name), name, min + n);
 	list_for_each_entry(i, &ctx->table->sets, list) {
 		if (!strcmp(set->name, i->name))
 			return -ENFILE;
Signature · 1 line
1.8.5.3
#3 lore raw
Quoted 58 lines · Patrick McHardy
Quoted 31 lines · Pablo Neira Ayuso
Quoted 30 lines · Patrick McHardy

We currently have a limit of 8 * PAGE_SIZE anonymous sets. Lift that limit by continuing the scan if the entire page is exhausted.

---
 net/netfilter/nf_tables_api.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +2 -2
index f25d011..89d7ec4 100644
@@ -2004,7 +2004,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 	const struct nft_set *i;
 	const char *p;
 	unsigned long *inuse;
-	unsigned int n = 0;
+	unsigned int n = 0, min = 0;
 
 	p = strnchr(name, IFNAMSIZ, '%');
 	if (p != NULL) {
@@ -2014,23 +2014,28 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
 		if (inuse == NULL)
 			return -ENOMEM;
-
+cont:
 		list_for_each_entry(i, &ctx->table->sets, list) {
 			int tmp;
 
Quoted 7 lines

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2023

Quoted 4 lines
net/netfilter/nf_tables_api.c +1 -1
 			if (!sscanf(i->name, name, &tmp))
 				continue;
-			if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
+			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)

I think we can break looping if tmp >= min + BITS_PER_BYTE * PAGE_SIZE fulfills (assuming the list of sets is ordered).

It's not, we really need to continue here.

Quoted 7 lines
Quoted 7 lines
net/netfilter/nf_tables_api.c +1 -1
 				continue;
 
-			set_bit(tmp, inuse);
+			set_bit(tmp - min, inuse);
 		}
 
Quoted 8 lines

net/netfilter/nf_tables_api.c static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, · line 2033

Quoted 5 lines
net/netfilter/nf_tables_api.c +4 -0
 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
+		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
+			min += BITS_PER_BYTE * PAGE_SIZE;
+			memset(inuse, 0, PAGE_SIZE);
+			goto cont;

If the page is full, we start iterating over the set list from the beginning.

Quoted 2 lines

Yes, also necessary since the list is not ordered. We always pick the first free set name and add it at the end.

Right, I misunderstood the logic behind the patch. I'll push this patch, thanks Patrick.