To allocate a name for an anonymous set, nf_tables_set_alloc_name() parses the name of every set in the table with sscanf() to find the smallest number not in use for the given format. Loading a ruleset with many anonymous sets therefore takes time quadratic in the number of sets. Take the number from a per-table counter instead and probe the candidate name with a set lookup, skipping names that are still in use. This is similar to anonymous chains, which are named after a counter as well. The counter wraps around at INT_MAX, since the name is formatted with %d. If all numbers are in use, -ENFILE is returned. Numbers are no longer reused, and different formats share the same counter. Userspace does not depend on the actual number: nft refers to a new anonymous set by its set ID within the batch and never parses the number from the name. Signed-off-by: Stefan Hanreich --- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 52 ++++++++++++++----------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h index d09959eea8ba..be129db1b82d 100644 --- a/include/net/netfilter/nf_tables.h +++ b/include/net/netfilter/nf_tables.h @@ -1303,6 +1303,7 @@ static inline void nft_use_inc_restore(u32 *use) * @flags: table flag (see enum nft_table_flags) * @genmask: generation mask * @nlpid: netlink port ID + * @anon_set_num: next number for anonymous set names * @name: name of the table * @udlen: length of the user data * @udata: user data @@ -1324,6 +1325,7 @@ struct nft_table { flags:8, genmask:2; u32 nlpid; + u32 anon_set_num; char *name; u16 udlen; u8 *udata; diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index c9b0070846b7..b46e32192f41 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -4950,13 +4950,30 @@ struct nft_set *nft_set_lookup_global(const struct net *net, return set; } +static int nft_set_alloc_anon_num(struct nft_ctx *ctx, const char *name) +{ + struct nft_table *table = ctx->table; + char buf[NFT_SET_MAXNAMELEN]; + u32 i, n; + + for (i = 0; i <= INT_MAX; i++) { + n = table->anon_set_num; + table->anon_set_num = n < INT_MAX ? n + 1 : 0; + + snprintf(buf, sizeof(buf), name, n); + if (IS_ERR(__nft_set_lookup(ctx->net, table, buf, + nft_genmask_next(ctx->net)))) + return n; + } + + return -ENFILE; +} + static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, const char *name) { - const struct nft_set *i; const char *p; - unsigned long *inuse; - unsigned int n = 0, min = 0; + int n = 0; p = strchr(name, '%'); if (p != NULL) { @@ -4966,33 +4983,12 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, if (strnlen(name, NFT_SET_MAX_ANONLEN) >= NFT_SET_MAX_ANONLEN) 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; - - if (!nft_is_active_next(ctx->net, i)) - 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 = 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); + n = nft_set_alloc_anon_num(ctx, name); + if (n < 0) + return n; } - set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); + set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, n); if (!set->name) return -ENOMEM; -- 2.47.3