netfilter: nf_tables: reduce set element transaction size
Subsystems (3)
Florian Westphal
maintainerv3 cover netfilter: nf_tables: reduce set element transaction size
v3:
I failed to realize that nft_audit leaks one implementation detail to userspace: the length of the transaction log.
This is bad, but I do not know if we can change things to make nft_audit NOT do that. Hence add a new workaround patch that inflates the length based on the number of set elements in the container structure.
Also fix up notifications, for update case, notifications were skipped but currently newsetelem notifications are done even if existing set element is updated.
Most patches are unchanged.
"prefer nft_trans_elem_alloc helper" is already upstreamed so its dropped from this batch.
v2: only change is in patch 3, and by extension, the last one:
During transaction abort, we need to handle an aggregate container to contain both new set elements and updates. The latter must be skipped, else we remove element that already existed at start of the transaction.
original cover letter:
When doing a flush on a set or mass adding/removing elements from a set, each element needs to allocate 96 bytes to hold the transactional state.
In such cases, virtually all the information in struct nft_trans_elem is the same.
Change nft_trans_elem to a flex-array, i.e. a single nft_trans_elem can hold multiple set element pointers.
The number of elements that can be stored in one nft_trans_elem is limited by the slab allocator, this series limits the compaction to at most 62 elements as it caps the reallocation to 2048 bytes of memory.
Florian Westphal (5):
netfilter: nf_tables: add nft_trans_commit_list_add_elem helper
netfilter: nf_tables: prepare for multiple elements in nft_trans_elem
structure
netfiler: nf_tables: preemitve fix for audit failure
netfilter: nf_tables: switch trans_elem to real flex array
netfilter: nf_tables: allocate element update information dynamically
include/net/netfilter/nf_tables.h | 25 +- net/netfilter/nf_tables_api.c | 368 +++++++++++++++++++++++------- 2 files changed, 304 insertions(+), 89 deletions(-)
Signature · 1 line
2.45.2
Florian Westphal
maintainerPATCH v3 1/5 netfilter: nf_tables: add nft_trans_commit_list_add_elem helper
Add and use a wrapper to append trans_elem structures to the transaction log.
Unlike the existing helper, pass a gfp_t to indicate if sleeping is allowed.
This will be used by a followup patch to realloc nft_trans_elem structures after they gain a flexible array member to reduce number of such container structures on the transaction list.
---
No changes in v3.
net/netfilter/nf_tables_api.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +16 -5
@@ -421,6 +421,17 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *tr } } +static void nft_trans_commit_list_add_elem (struct net *net,struct nft_trans *trans, +gfp_t gfp) +{ +WARN_ON_ONCE (trans->msg_type !=NFT_MSG_NEWSETELEM && + trans->msg_type !=NFT_MSG_DELSETELEM ); + +might_alloc (gfp); + +nft_trans_commit_list_add_tail (net, trans); +} +static int nft_trans_table_add (struct nft_ctx *ctx,int msg_type) {struct nft_trans *trans;
@@ -7183,7 +7194,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (update_flags) {nft_trans_elem_priv (trans) = elem_priv;nft_trans_elem_update_flags (trans) = update_flags; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL ); goto err_elem_free; } }
@@ -7207,7 +7218,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, }nft_trans_elem_priv (trans) = elem.priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; err_set_full:
@@ -7424,7 +7435,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,nft_setelem_data_deactivate (ctx->net, set, elem.priv);nft_trans_elem_priv (trans) = elem.priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; fail_ops:
@@ -7460,7 +7471,7 @@ static int nft_setelem_flush(const struct nft_ctx *ctx,nft_setelem_data_deactivate (ctx->net, set, elem_priv);nft_trans_elem_set (trans) = set;nft_trans_elem_priv (trans) = elem_priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_ATOMIC );return 0 ; }
@@ -7477,7 +7488,7 @@ static int __nft_set_catchall_flush(const struct nft_ctx *ctx,nft_setelem_data_deactivate (ctx->net, set, elem_priv);nft_trans_elem_priv (trans) = elem_priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; }
Signature · 1 line
2.45.2
Florian Westphal
maintainerPATCH v3 2/5 netfilter: nf_tables: prepare for multiple elements in nft_trans_elem structure
In reply to Florian WestphalAdd helpers to release the individual elements contained in the trans_elem container structure.
No functional change intended.
Followup patch will add 'nelems' member and will turn 'priv' into a flexible array.
These helpers can then loop over all elements.
Care needs to be taken to handle a mix of new elements and existing elements that are being updated (e.g. timeout refresh).
Before this patch, NEWSETELEM transaction with update is released early so nft_trans_set_elem_destroy() won't get called, so we need to skip elements marked as update.
---
Pull one hunk from next patch into this one to prevent a kasan splat when running tests on this patch. Also emit notiifcations also for element updates, this got broken in earlier iterations.
include/net/netfilter/nf_tables.h | 21 ++- net/netfilter/nf_tables_api.c | 228 +++++++++++++++++++++--------- 2 files changed, 173 insertions(+), 76 deletions(-)
include/net/netfilter/nf_tables.h +9 -12
@@ -1754,28 +1754,25 @@ enum nft_trans_elem_flags { NFT_TRANS_UPD_EXPIRATION = (1 << 1), }; -struct nft_trans_elem { -struct nft_trans nft_trans; -struct nft_set *set; -struct nft_elem_priv *elem_priv; +struct nft_trans_one_elem { +struct nft_elem_priv *priv;u64 timeout;u64 expiration;u8 update_flags; +}; + +struct nft_trans_elem { +struct nft_trans nft_trans; +struct nft_set *set;bool bound; +unsigned int nelems; + struct nft_trans_one_elem elems[] __counted_by(nelems); };#define nft_trans_container_elem (t) \ container_of(t, struct nft_trans_elem, nft_trans)#define nft_trans_elem_set (trans) \ nft_trans_container_elem(trans)->set -#define nft_trans_elem_priv (trans) \ - nft_trans_container_elem(trans)->elem_priv -#define nft_trans_elem_update_flags (trans) \ - nft_trans_container_elem(trans)->update_flags -#define nft_trans_elem_timeout (trans) \ - nft_trans_container_elem(trans)->timeout -#define nft_trans_elem_expiration (trans) \ - nft_trans_container_elem(trans)->expiration#define nft_trans_elem_set_bound (trans) \ nft_trans_container_elem(trans)->bound
net/netfilter/nf_tables_api.c +164 -64
372 lines not on this page · raw message
Signature · 1 line
2.45.2
Florian Westphal
maintainerPATCH v3 3/5 netfiler: nf_tables: preemitve fix for audit failure
In reply to Florian WestphalNftables audit log unfortunately leaks implementation detail, the transaction log size, to userspace.
Without this, nft_audit.sh selftest fails once subsequenct NEW/DELELEM transactions can be compressed.
Thus increment the audit counter by the number of elements to keep the output identical.
---
v3: this patch is new to prevent nft_audit.sh from breaking after next patch.
net/netfilter/nf_tables_api.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +20 -3
@@ -10373,9 +10373,26 @@ static void nf_tables_commit_audit_free(struct list_head *adl) } } +/* Silly, but existing test audit test cases require a count +* value derived from the (INTERNAL!) transaction log length. +* +* Thus, compaction of NEW/DELSETELEM breaks such tests. +*/ +static unsigned int nf_tables_commit_audit_entrycount (const struct nft_trans *trans) +{ +switch (trans->msg_type) { +case NFT_MSG_NEWSETELEM : +case NFT_MSG_DELSETELEM : +return nft_trans_container_elem (trans)->nelems; + } + +return 1 ; +} +static void nf_tables_commit_audit_collect (struct list_head *adl, -struct nft_table *table,u32 op) +const struct nft_trans *trans,u32 op) { +const struct nft_table *table = trans->table;struct nft_audit_data *adp;list_for_each_entry (adp, adl, list) {
@@ -10385,7 +10402,7 @@ static void nf_tables_commit_audit_collect(struct list_head *adl,WARN_ONCE (1 ,"table=%s not expected in commit list" , table->name);return ; found: - adp->entries++; + adp->entries +=nf_tables_commit_audit_entrycount (trans);if (!adp->op || adp->op > op) adp->op = op; }
@@ -10544,7 +10561,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)nft_ctx_update (&ctx, trans); -nf_tables_commit_audit_collect (&adl, table, trans->msg_type); +nf_tables_commit_audit_collect (&adl, trans, trans->msg_type);switch (trans->msg_type) {case NFT_MSG_NEWTABLE :if (nft_trans_table_update (trans)) {
Signature · 1 line
2.45.2
Florian Westphal
maintainerPATCH v3 4/5 netfilter: nf_tables: switch trans_elem to real flex array
In reply to Florian WestphalWhen queueing a set element add or removal operation to the transaction log, check if the previous operation already asks for a the identical operation on the same set.
If so, store the element reference in the preceding operation. This significantlty reduces memory consumption when many set add/delete operations appear in a single transaction.
Example: 10k elements require 937kb of memory (10k allocations from kmalloc-96 slab).
Assuming we can compact 4 elements in the same set, 468 kbytes are needed (64 bytes for base struct, nft_trans_elemn, 32 bytes for nft_trans_one_elem structure, so 2500 allocations from kmalloc-192 slab).
For large batch updates we can compact up to 62 elements
into one single nft_trans_elem structure (~65% mem reduction):
(64 bytes for base struct, nft_trans_elem, 32 byte for nft_trans_one_elem
struct).
We can halve size of nft_trans_one_elem struct by moving timeout/expire/update_flags into a dynamically allocated structure, this allows to store 124 elements in a 2k slab nft_trans_elem struct. This is done in a followup patch.
---
no changes in this version.
net/netfilter/nf_tables_api.c | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
net/netfilter/nf_tables_api.c +71 -0
@@ -25,6 +25,7 @@#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))#define NFT_SET_MAX_ANONLEN 16 +#define NFT_MAX_SET_NELEMS ((2048 - sizeof(struct nft_trans_elem)) / sizeof(struct nft_trans_one_elem))unsigned int nf_tables_net_id__read_mostly ;
@@ -391,6 +392,69 @@ static void nf_tables_unregister_hook(struct net *net,return __nf_tables_unregister_hook (net, table, chain, false); } +static bool nft_trans_collapse_set_elem_allowed (const struct nft_trans_elem *a,const struct nft_trans_elem *b) +{ +return a->set == b->set && a->bound == b->bound && a->nelems <NFT_MAX_SET_NELEMS ; +} + +static bool nft_trans_collapse_set_elem (struct nftables_pernet *nft_net, +struct nft_trans_elem *tail, +struct nft_trans_elem *trans, +gfp_t gfp) +{ +unsigned int nelems, old_nelems = tail->nelems; +struct nft_trans_elem *new_trans; + +if (!nft_trans_collapse_set_elem_allowed (tail, trans)) +return false; + +if (WARN_ON_ONCE (trans->nelems !=1 )) +return false; + +if (check_add_overflow (old_nelems, trans->nelems, &nelems)) +return false; + +/* krealloc might free tail which invalidates list pointers */ +list_del_init (&tail->nft_trans.list); + + new_trans =krealloc (tail,struct_size (tail, elems, nelems), gfp); +if (!new_trans) { +list_add_tail (&tail->nft_trans.list, &nft_net->commit_list); +return false; + } + +INIT_LIST_HEAD (&new_trans->nft_trans.list); + new_trans->nelems = nelems; + new_trans->elems[old_nelems] = trans->elems[0 ]; +list_add_tail (&new_trans->nft_trans.list, &nft_net->commit_list); + +return true; +} + +static bool nft_trans_try_collapse (struct nftables_pernet *nft_net, +struct nft_trans *trans,gfp_t gfp) +{ +struct nft_trans *tail; + +if (list_empty (&nft_net->commit_list)) +return false; + + tail = list_last_entry(&nft_net->commit_list, struct nft_trans, list); + +if (tail->msg_type != trans->msg_type) +return false; + +switch (trans->msg_type) { +case NFT_MSG_NEWSETELEM : +case NFT_MSG_DELSETELEM : +return nft_trans_collapse_set_elem (nft_net, +nft_trans_container_elem (tail), +nft_trans_container_elem (trans), gfp); + } + +return false; +} +static void nft_trans_commit_list_add_tail (struct net *net,struct nft_trans *trans) {struct nftables_pernet *nft_net =nft_pernet (net);
@@ -424,11 +488,18 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trstatic void nft_trans_commit_list_add_elem (struct net *net,struct nft_trans *trans,gfp_t gfp) { +struct nftables_pernet *nft_net =nft_pernet (net); +WARN_ON_ONCE (trans->msg_type !=NFT_MSG_NEWSETELEM && trans->msg_type !=NFT_MSG_DELSETELEM );might_alloc (gfp); +if (nft_trans_try_collapse (nft_net, trans, gfp)) { +kfree (trans); +return ; + } +nft_trans_commit_list_add_tail (net, trans); }
Signature · 1 line
2.45.2
Florian Westphal
maintainerPATCH v3 5/5 netfilter: nf_tables: allocate element update information dynamically
In reply to Florian WestphalMove the timeout/expire/flag members from nft_trans_one_elem struct into a dybamically allocated structure, only needed when timeout update was requested.
This halves size of nft_trans_one_elem struct and allows to compact up to 124 elements in one transaction container rather than 62.
This halves memory requirements for a large flush or insert transaction, where ->update remains NULL.
Care has to be taken to release the extra data in all spots, including abort path.
---
v3: adjust for changes in earlier patch, nft_trans_elems_add() can now kfree() the update pointer unconditionally.
include/net/netfilter/nf_tables.h | 10 ++++-- net/netfilter/nf_tables_api.c | 57 +++++++++++++++++++------------ 2 files changed, 43 insertions(+), 24 deletions(-)
include/net/netfilter/nf_tables.h +7 -3
@@ -1754,11 +1754,15 @@ enum nft_trans_elem_flags { NFT_TRANS_UPD_EXPIRATION = (1 << 1), }; -struct nft_trans_one_elem { -struct nft_elem_priv *priv; +struct nft_elem_update {u64 timeout;u64 expiration; -u8 update_flags; +u8 flags; +}; + +struct nft_trans_one_elem { +struct nft_elem_priv *priv; +struct nft_elem_update *update; };struct nft_trans_elem {
net/netfilter/nf_tables_api.c +36 -21
123 lines not on this page · raw message
Signature · 1 line
2.45.2
Pablo Neira Ayuso
maintainerQuoted 8 lines · Florian Westphal
v3:
I failed to realize thatnft_auditleaks one implementation detail to userspace: the length of the transaction log.This is bad, but I do not know if we can change things to make
nft_auditNOT do that. Hence add a new workaround patch that inflates the length based on the number of set elements in the container structure.
It actually shows the number of entries that have been updated, right?
Before this series, there was a 1:1 mapping between transaction and objects so it was easier to infer it from the number of transaction objects.
Quoted 49 lines
Also fix up notifications, for update case, notifications were skipped but currently newsetelem notifications are done even if existing set element is updated.
Most patches are unchanged.
"prefernft_trans_elem_allochelper" is already upstreamed so its dropped from this batch.v2: only change is in patch 3, and by extension, the last one:
During transaction abort, we need to handle an aggregate container to contain both new set elements and updates. The latter must be skipped, else we remove element that already existed at start of the transaction.original cover letter:
When doing a flush on a set or mass adding/removing elements from a set, each element needs to allocate 96 bytes to hold the transactional state.
In such cases, virtually all the information in struct
nft_trans_elemis the same.Change
nft_trans_elemto a flex-array, i.e. a singlenft_trans_elemcan hold multiple set element pointers.The number of elements that can be stored in one
nft_trans_elemis limited by the slab allocator, this series limits the compaction to at most 62 elements as it caps the reallocation to 2048 bytes of memory.Florian Westphal (5):
netfilter:nf_tables: addnft_trans_commit_list_add_elemhelper
netfilter:nf_tables: prepare for multiple elements innft_trans_elem
structure
netfiler:nf_tables: preemitve fix for audit failure
netfilter:nf_tables: switchtrans_elemto real flex array
netfilter:nf_tables: allocate element update information dynamicallyinclude/net/netfilter/nf_tables.h | 25 +- net/netfilter/nf_tables_api.c | 368 +++++++++++++++++++++++------- 2 files changed, 304 insertions(+), 89 deletions(-)Signature · 1 line
2.45.2
Florian Westphal
maintainerQuoted 10 lines · Pablo Neira Ayuso
Quoted 4 lines
This is bad, but I do not know if we can change things to make
nft_auditNOT do that. Hence add a new workaround patch that inflates the length based on the number of set elements in the container structure.It actually shows the number of entries that have been updated, right?
Before this series, there was a 1:1 mapping between transaction and objects so it was easier to infer it from the number of transaction objects.
Yes, but... for element add (but not create), we used to not do anything (no-op), so we did not allocate a new transaction and pretend request did not exist.
Now we can enter update path, so we do allocate a transaction, hence, audit record changes.
What if we add an internal special-case 'flush' op in the future? It will break, and the workaround added in this series needs to be extended.
Same for an other change that could elide a transaction request, or, add expand something to multiple ones (as flush currently does).
Its doesn't *break* audit, but it changes the output.
Pablo Neira Ayuso
maintainerCc'ing audit ML.
Quoted 8 lines · Florian Westphal
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines
This is bad, but I do not know if we can change things to make
nft_auditNOT do that. Hence add a new workaround patch that inflates the length based on the number of set elements in the container structure.It actually shows the number of entries that have been updated, right?
Quoted 7 lines
Quoted 3 lines
Before this series, there was a 1:1 mapping between transaction and objects so it was easier to infer it from the number of transaction objects.
Yes, but... for element add (but not create), we used to not do anything (no-op), so we did not allocate a new transaction and pretend request did not exist.
You refer to element updates above, those used to be elided, yes. Now they are shown. I think that is correct.
Quoted 4 lines
Now we can enter update path, so we do allocate a transaction, hence, audit record changes.
What if we add an internal special-case 'flush' op in the future?
You mean, if 'flush' does not get expanded to one delete transaction for each element. Yes, that would require to look at ->nelems as in this patch.
Quoted 7 lines
It will break, and the workaround added in this series needs to be extended.
Same for an other change that could elide a transaction request, or, add expand something to multiple ones (as flush currently does).
Its doesn't *break* audit, but it changes the output.
My understanding is that audit is exposing the entries that have been added/updated/deleted, which is already sparse: Note that nftables audit works at 'table' granurality.
IIRC, one of the audit maintainers mentioned it should be possible to add chain and set to the audit logs in the future, such change would necessarily change the audit logs output.
Thanks.
Paul Moore
maintainerQuoted 2 lines · Pablo Neira Ayuso
Cc'ing audit ML.
Thank you.
Quoted 37 lines · Florian Westphal
Quoted 8 lines
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines
This is bad, but I do not know if we can change things to make
nft_auditNOT do that. Hence add a new workaround patch that inflates the length based on the number of set elements in the container structure.It actually shows the number of entries that have been updated, right?
Quoted 7 lines
Quoted 3 lines
Before this series, there was a 1:1 mapping between transaction and objects so it was easier to infer it from the number of transaction objects.
Yes, but... for element add (but not create), we used to not do anything (no-op), so we did not allocate a new transaction and pretend request did not exist.
You refer to element updates above, those used to be elided, yes. Now they are shown. I think that is correct.
Quoted 4 lines
Now we can enter update path, so we do allocate a transaction, hence, audit record changes.
What if we add an internal special-case 'flush' op in the future?
You mean, if 'flush' does not get expanded to one delete transaction for each element. Yes, that would require to look at ->nelems as in this patch.
Quoted 7 lines
It will break, and the workaround added in this series needs to be extended.
Same for an other change that could elide a transaction request, or, add expand something to multiple ones (as flush currently does).
Its doesn't *break* audit, but it changes the output.
Quoted 7 lines
My understanding is that audit is exposing the entries that have been added/updated/deleted, which is already sparse: Note that nftables audit works at 'table' granurality.
IIRC, one of the audit maintainers mentioned it should be possible to add chain and set to the audit logs in the future, such change would necessarily change the audit logs output.
For those of us joining the conversation late, can you provide a quick summary of what you want to change in audit and why?
Signature · 1 line
paul-moore.com
Pablo Neira Ayuso
maintainerQuoted 3 lines · Paul Moore
Quoted 1 line · Florian Westphal
For those of us joining the conversation late, can you provide a quick summary of what you want to change in audit and why?
Florian said:
"I failed to realize that nft_audit leaks one implementation detail to userspace: the length of the transaction log."
table=t1 family=2 entries=4 op=nft_register_set
He is referring to the 'entries' key.
So far, every object gets one transaction, but now batching several objects in one transaction is possible.
We have been discussing what the expected semantics for this audit log key is:
- If this is the transaction log length, then the internal update of the transaction logic results in a smaller number of 'entries' in the audit log.
- If 'entries' refers to the number of "affected" objects by this operation, then this means we have to carry a "workaround" in the kernel.
This is because:
1) Element updates are now supported, this currently handles it as a _REGISTER operation according to enum audit_nfcfgop. This changed the semantics of the add command, now it is "add if it does not exist, otherwise update what it already exists". Before, updates where simply elided (not counted by 'entries' key) because they were not supported. That is, 'entries' now tell how many set element has been added OR updated. I think this is fine, this is consistent with chain updates where 'entries' also report added OR updated chains. The difference is that old kernel do not count updates (because they are elided).
2) There is ongoing work to add more internal transaction batching, ie. use one single transaction for several elements. This requires a special case to bump the 'entries' key to add the elements that the transaction batch contains, see:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/
There is a nft_audit.sh selftest and Florian thinks this is a "workaround" patch only to make this test happy, because 'entries' refers to the transaction log length (which is now smaller given the internal transaction batching approach to accumulate several elements is used).