[v2] net: netfilter: nf_tables_api: Use id allocation.

Message ID 20180401044322.3082-1-rvarsha016@gmail.com
State Under Review
Delegated to: Pablo Neira
Headers show
Series
  • [v2] net: netfilter: nf_tables_api: Use id allocation.
Related show

Commit Message

Varsha Rao April 1, 2018, 4:43 a.m.
In nf_tables_set_alloc_name function, remove get_zeroed_page
find_first_zero_bit and set_bit functions. Instead use ida_simple_get
function as it simplifies the code.

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
---
Changes in v2:
- Modified the upper limit of page size.

 net/netfilter/nf_tables_api.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

Patch

diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 6e93782bbe4f..944a8bd255b9 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -2657,17 +2657,14 @@  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, min = 0;
+	int n = 0, min = 0;
+	DEFINE_IDA(inuse);
 
 	p = strchr(name, '%');
 	if (p != NULL) {
 		if (p[1] != 'd' || strchr(p + 2, '%'))
 			return -EINVAL;
 
-		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;
@@ -2676,22 +2673,28 @@  static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
 				continue;
 			if (!sscanf(i->name, name, &tmp))
 				continue;
-			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
-				continue;
 
-			set_bit(tmp - min, inuse);
+			n = ida_simple_get(&inuse, tmp, min + BITS_PER_BYTE *
+					   PAGE_SIZE, GFP_KERNEL);
+			if (n == -ENOSPC)
+				continue;
+			if (n == -ENOMEM)
+				return n;
 		}
-
-		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
-		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
+		n = ida_simple_get(&inuse, 0, min + BITS_PER_BYTE * PAGE_SIZE,
+				   GFP_KERNEL);
+		if (n == -ENOSPC) {
 			min += BITS_PER_BYTE * PAGE_SIZE;
-			memset(inuse, 0, PAGE_SIZE);
+			ida_destroy(&inuse);
 			goto cont;
 		}
-		free_page((unsigned long)inuse);
+		if (n == -ENOMEM)
+			return n;
+
+		ida_destroy(&inuse);
 	}
 
-	set->name = kasprintf(GFP_KERNEL, name, min + n);
+	set->name = kasprintf(GFP_KERNEL, name, n);
 	if (!set->name)
 		return -ENOMEM;