netfilter: nf_tables: shrink memory consumption of set elements
Subsystems (2)
Pablo Neira Ayuso
maintainerRFC PATCH 1/1 netfilter: nf_tables: shrink memory consumption of set elements
Instead of copying struct nft_set_elem into struct nft_trans_elem, store the pointer to the opaque set element object in the transaction. Adapt set backend API (and set backend implementations) to take the pointer to opaque set element representation whenever required.
This patch deconstifies .remove() and .activate() set backend API since these modify the set element opaque object. And it also constify nft_set_elem_ext() since this provides access to the nft_set_ext struct without updating the object.
According to pahole on x86_64, this patch shrinks struct nft_trans_elem size from 216 to 24 bytes.
This patch also reduces stack memory consumption by removing the template struct nft_set_elem object which consumes 200 bytes of stack memory according to pahole. Use the opaque set element object instead from the set iterator API, catchall elements and the get element command paths to benefit from this memory consumption reduction.
---
I tagged this as RFC because it based on nf.git, but targeted at nf-next.git, because of missing dependencies, I have kept in here for a while in my local pile waiting for the dependencies to land, but I prefer to post it now for review. So it cannot be considered for integration into the nf-next.git tree yet because of these details.
This patch depends on ("netfilter: nf_tables: do not remove elements if set backend implements .abort") which will take time to propagate to nf-next. This also slightly clashes with a other existing pending patches for nf-next floating in the mailing list, but that should be easy to fix with a rebase.
I started with an initial patch to make the const updates, but it is triggering more churning than expected (since follow up patch will again update the same line when changing from struct nft_set_elem to void). I believe this patch should be relatively easy to review, but maybe that is just my bias.
Main issue is (and it was still before patch) is that this opaque object from the nf_tables frontend is void *, which makes it harder for the compiler to catch stupid mistakes such as passing elem instead of elem.priv or even &trans->elem, that is, type checking is defeated so careful inspection is needed. Instrumention and existing tests also help catch issues of course.
include/net/netfilter/nf_tables.h | 28 +++-- net/netfilter/nf_tables_api.c | 188 +++++++++++++----------------- net/netfilter/nft_set_bitmap.c | 17 +-- net/netfilter/nft_set_hash.c | 28 ++--- net/netfilter/nft_set_pipapo.c | 20 +--- net/netfilter/nft_set_rbtree.c | 21 ++-- 6 files changed, 125 insertions(+), 177 deletions(-)
include/net/netfilter/nf_tables.h +13 -15
@@ -306,7 +306,7 @@ struct nft_set_iter {int (*fn)(const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter, -struct nft_set_elem *elem); +void *elem_priv); };/**
@@ -445,16 +445,16 @@ struct nft_set_ops { struct nft_set_ext **ext);void (*activate)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +void *elem_priv);void * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);bool (*flush)(const struct net *net,const struct nft_set *set, -void *priv); +void *elem_priv);void (*remove)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +void *elem_priv);void (*walk)(const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_iter *iter);
@@ -796,9 +796,9 @@ static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) }static inline struct nft_set_ext *nft_set_elem_ext (const struct nft_set *set, -void *elem) +const void *elem) { -return elem + set->ops->elemsize; +return (struct nft_set_ext *)(elem + set->ops->elemsize); }static inline struct nft_object **nft_set_ext_obj (const struct nft_set_ext *ext)
@@ -816,10 +816,10 @@ void *nft_set_elem_init(const struct nft_set *set, u64 timeout, u64 expiration, gfp_t gfp);int nft_set_elem_expr_clone (const struct nft_ctx *ctx,struct nft_set *set,struct nft_expr *expr_array[]); -void nft_set_elem_destroy (const struct nft_set *set,void *elem, +void nft_set_elem_destroy (const struct nft_set *set,const void *elem_priv,bool destroy_expr);void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, -const struct nft_set *set,void *elem); +const struct nft_set *set,const void *elem);struct nft_expr_ops ;/**
@@ -1060,8 +1060,7 @@ struct nft_chain {int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain);int nft_setelem_validate (const struct nft_ctx *ctx,struct nft_set *set, -const struct nft_set_iter *iter, -struct nft_set_elem *elem); +const struct nft_set_iter *iter,void *elem_priv);int nft_set_catchall_validate (const struct nft_ctx *ctx,struct nft_set *set);int nf_tables_bind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);void nf_tables_unbind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);
@@ -1635,14 +1634,14 @@ struct nft_trans_table {struct nft_trans_elem {struct nft_set *set; -struct nft_set_elem elem; +void *elem_priv;bool bound; };#define nft_trans_elem_set (trans) \ (((struct nft_trans_elem *)trans->data)->set) -#define nft_trans_elem (trans) \ - (((struct nft_trans_elem *)trans->data)->elem) +#define nft_trans_elem_priv (trans) \ + (((struct nft_trans_elem *)trans->data)->elem_priv)#define nft_trans_elem_set_bound (trans) \ (((struct nft_trans_elem *)trans->data)->bound)
@@ -1705,8 +1704,7 @@ struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,struct nft_trans_gc *nft_trans_gc_catchall_sync (struct nft_trans_gc *gc);void nft_setelem_data_deactivate (const struct net *net, -const struct nft_set *set, -struct nft_set_elem *elem); +const struct nft_set *set,void *elem_priv);int __init nft_chain_filter_init (void );void nft_chain_filter_fini (void );
net/netfilter/nf_tables_api.c +83 -105
541 lines not on this page · raw message
net/netfilter/nft_set_bitmap.c +6 -11
@@ -144,12 +144,11 @@ static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,return 0 ; } -static void nft_bitmap_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +static void nft_bitmap_remove (const struct net *net,const struct nft_set *set, +void *elem_priv) {struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv; +struct nft_bitmap_elem *be = elem_priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -160,11 +159,10 @@ static void nft_bitmap_remove(const struct net *net, }static void nft_bitmap_activate (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) {struct nft_bitmap *priv =nft_set_priv (set); -struct nft_bitmap_elem *be = elem->priv; +struct nft_bitmap_elem *be = elem_priv;u8 genmask =nft_genmask_next (net);u32 idx, off;
@@ -218,7 +216,6 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx, {const struct nft_bitmap *priv =nft_set_priv (set);struct nft_bitmap_elem *be; -struct nft_set_elem elem;list_for_each_entry_rcu (be, &priv->list, head) {if (iter->count < iter->skip)
@@ -226,9 +223,7 @@ static void nft_bitmap_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&be->ext, iter->genmask)) goto cont; - elem.priv = be; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, be);if (iter->err <0 )return ;
net/netfilter/nft_set_hash.c +10 -18
@@ -185,9 +185,9 @@ static int nft_rhash_insert(const struct net *net, const struct nft_set *set, }static void nft_rhash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +void *elem_priv) { -struct nft_rhash_elem *he = elem->priv; +struct nft_rhash_elem *he = elem_priv;nft_set_elem_change_active (net, set, &he->ext); }
@@ -225,11 +225,10 @@ static void *nft_rhash_deactivate(const struct net *net, }static void nft_rhash_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) {struct nft_rhash *priv =nft_set_priv (set); -struct nft_rhash_elem *he = elem->priv; +struct nft_rhash_elem *he = elem_priv;rhashtable_remove_fast (&priv->ht, &he->node, nft_rhash_params); }
@@ -260,7 +259,6 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,struct nft_rhash *priv =nft_set_priv (set);struct nft_rhash_elem *he;struct rhashtable_iter hti; -struct nft_set_elem elem;rhashtable_walk_enter (&priv->ht, &hti);rhashtable_walk_start (&hti);
@@ -280,9 +278,7 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = he; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, he);if (iter->err <0 )break ;
@@ -583,9 +579,9 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set, }static void nft_hash_activate (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +void *elem_priv) { -struct nft_hash_elem *he = elem->priv; +struct nft_hash_elem *he = elem_priv;nft_set_elem_change_active (net, set, &he->ext); }
@@ -621,10 +617,9 @@ static void *nft_hash_deactivate(const struct net *net, }static void nft_hash_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) { -struct nft_hash_elem *he = elem->priv; +struct nft_hash_elem *he = elem_priv;hlist_del_rcu (&he->node); }
@@ -634,7 +629,6 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he; -struct nft_set_elem elem;int i;for (i =0 ; i < priv->buckets; i++) {
@@ -644,9 +638,7 @@ static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_set_elem_active (&he->ext, iter->genmask)) goto cont; - elem.priv = he; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, he);if (iter->err <0 )return ; cont:
net/netfilter/nft_set_pipapo.c +6 -14
@@ -1540,11 +1540,7 @@ static void nft_pipapo_gc_deactivate(struct net *net, struct nft_set *set, struct nft_pipapo_elem *e) { -struct nft_set_elem elem = { - .priv = e, - }; - -nft_setelem_data_deactivate (net, set, &elem); +nft_setelem_data_deactivate (net, set, e); }/**
@@ -1740,10 +1736,9 @@ static void nft_pipapo_abort(const struct nft_set *set)* element, hence we can't purpose either one as a real commit operation. */ static void nft_pipapo_activate (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) { -struct nft_pipapo_elem *e = elem->priv; +struct nft_pipapo_elem *e = elem_priv;nft_set_elem_change_active (net, set, &e->ext); }
@@ -1947,11 +1942,11 @@ static bool pipapo_match_field(struct nft_pipapo_field *f,* the matched element here, if any, and commit the updated matching data. */ static void nft_pipapo_remove (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +void *elem_priv) {struct nft_pipapo *priv =nft_set_priv (set);struct nft_pipapo_match *m = priv->clone; -struct nft_pipapo_elem *e = elem->priv; +struct nft_pipapo_elem *e = elem_priv;int rules_f0, first_rule =0 ;const u8 *data;
@@ -2031,7 +2026,6 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,for (r =0 ; r < f->rules; r++) {struct nft_pipapo_elem *e; -struct nft_set_elem elem;if (r < f->rules -1 && f->mt[r +1 ].e == f->mt[r].e)continue ;
@@ -2041,9 +2035,7 @@ static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set, e = f->mt[r].e; - elem.priv = e; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, e);if (iter->err <0 ) goto out;
net/netfilter/nft_set_rbtree.c +7 -14
@@ -225,11 +225,9 @@ static void nft_rbtree_gc_remove(struct net *net, struct nft_set *set,struct nft_rbtree *priv, struct nft_rbtree_elem *rbe) { - struct nft_set_elem elem = { - .priv = rbe, - }; + void *elem_priv = (void *)rbe; -nft_setelem_data_deactivate (net, set, &elem); +nft_setelem_data_deactivate (net, set, elem_priv);rb_erase (&rbe->node, &priv->root); }
@@ -508,11 +506,10 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set, }static void nft_rbtree_remove (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) {struct nft_rbtree *priv =nft_set_priv (set); -struct nft_rbtree_elem *rbe = elem->priv; +struct nft_rbtree_elem *rbe = elem_priv;write_lock_bh (&priv->lock);write_seqcount_begin (&priv->count);
@@ -522,10 +519,9 @@ static void nft_rbtree_remove(const struct net *net, }static void nft_rbtree_activate (const struct net *net, -const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set *set,void *elem_priv) { -struct nft_rbtree_elem *rbe = elem->priv; +struct nft_rbtree_elem *rbe = elem_priv;nft_set_elem_change_active (net, set, &rbe->ext); }
@@ -585,7 +581,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx, {struct nft_rbtree *priv =nft_set_priv (set);struct nft_rbtree_elem *rbe; -struct nft_set_elem elem;struct rb_node *node;read_lock_bh (&priv->lock);
@@ -597,9 +592,7 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,if (!nft_set_elem_active (&rbe->ext, iter->genmask)) goto cont; - elem.priv = rbe; - - iter->err = iter->fn (ctx, set, iter, &elem); + iter->err = iter->fn (ctx, set, iter, rbe);if (iter->err <0 ) {read_unlock_bh (&priv->lock);return ;
Signature · 1 line
2.30.2
Florian Westphal
maintainerQuoted 10 lines · Pablo Neira Ayuso
Instead of copying struct
nft_set_eleminto structnft_trans_elem, store the pointer to the opaque set element object in the transaction. Adapt set backend API (and set backend implementations) to take the pointer to opaque set element representation whenever required.This patch deconstifies .remove() and .activate() set backend API since these modify the set element opaque object. And it also constify
nft_set_elem_ext()since this provides access to thenft_set_extstruct without updating the object.
Quoted 8 lines
According to pahole on
x86_64, this patch shrinks structnft_trans_elemsize from 216 to 24 bytes.This patch also reduces stack memory consumption by removing the template struct
nft_set_elemobject which consumes 200 bytes of stack memory according to pahole. Use the opaque set element object instead from the set iterator API, catchall elements and the get element command paths to benefit from this memory consumption reduction.
Is there a request for this? Or is the memory consumption a concern on your end?
Quoted 20 lines
Signed-off-by Pablo Neira Ayuso <[email protected]>---
I tagged this as RFC because it based on nf.git, but targeted at nf-next.git, because of missing dependencies, I have kept in here for a while in my local pile waiting for the dependencies to land, but I prefer to post it now for review. So it cannot be considered for integration into the nf-next.git tree yet because of these details.This patch depends on ("netfilter:
nf_tables: do not remove elements if set backend implements .abort") which will take time to propagate to nf-next. This also slightly clashes with a other existing pending patches for nf-next floating in the mailing list, but that should be easy to fix with a rebase.I started with an initial patch to make the const updates, but it is triggering more churning than expected (since follow up patch will again update the same line when changing from struct
nft_set_elemto void). I believe this patch should be relatively easy to review, but maybe that is just my bias.
Quoted 6 lines
Main issue is (and it was still before patch) is that this opaque object from the
nf_tablesfrontend is void *, which makes it harder for the compiler to catch stupid mistakes such as passing elem instead of elem.priv or even&trans->elem, that is, type checking is defeated so careful inspection is needed. Instrumention and existing tests also help catch issues of course.
The void * is bad, and I dislike that this gets spread. Could you add a "struct nft_set_elem_priv" that serves as a proxy object?
All the priv elements would include it as first member, so we can pass that around instead of void *?
Pablo Neira Ayuso
maintainerQuoted 14 lines · Florian Westphal
Quoted 13 lines · Pablo Neira Ayuso
Instead of copying struct
nft_set_eleminto structnft_trans_elem, store the pointer to the opaque set element object in the transaction. Adapt set backend API (and set backend implementations) to take the pointer to opaque set element representation whenever required.This patch deconstifies .remove() and .activate() set backend API since these modify the set element opaque object. And it also constify
nft_set_elem_ext()since this provides access to thenft_set_extstruct without updating the object.According to pahole on
x86_64, this patch shrinks structnft_trans_elemsize from 216 to 24 bytes.
Quoted 8 lines
Quoted 5 lines
This patch also reduces stack memory consumption by removing the template struct
nft_set_elemobject which consumes 200 bytes of stack memory according to pahole. Use the opaque set element object instead from the set iterator API, catchall elements and the get element command paths to benefit from this memory consumption reduction.Is there a request for this? Or is the memory consumption a concern on your end?
This takes element transaction from kmalloc-512 pool to kmalloc-128, elements use a lot of this temporary objects.
Quoted 27 lines
Quoted 20 lines
Signed-off-by Pablo Neira Ayuso <[email protected]>---
I tagged this as RFC because it based on nf.git, but targeted at nf-next.git, because of missing dependencies, I have kept in here for a while in my local pile waiting for the dependencies to land, but I prefer to post it now for review. So it cannot be considered for integration into the nf-next.git tree yet because of these details.This patch depends on ("netfilter:
nf_tables: do not remove elements if set backend implements .abort") which will take time to propagate to nf-next. This also slightly clashes with a other existing pending patches for nf-next floating in the mailing list, but that should be easy to fix with a rebase.I started with an initial patch to make the const updates, but it is triggering more churning than expected (since follow up patch will again update the same line when changing from struct
nft_set_elemto void). I believe this patch should be relatively easy to review, but maybe that is just my bias.Quoted 6 lines
Main issue is (and it was still before patch) is that this opaque object from the
nf_tablesfrontend is void *, which makes it harder for the compiler to catch stupid mistakes such as passing elem instead of elem.priv or even&trans->elem, that is, type checking is defeated so careful inspection is needed. Instrumention and existing tests also help catch issues of course.
Quoted 6 lines
The void * is bad, and I dislike that this gets spread. Could you add a "struct
nft_set_elem_priv" that serves as a proxy object?All the priv elements would include it as first member, so we can pass that around instead of void *?
This is a great idea, I am preparing a v2.
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Pablo,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on netfilter-nf/main]
[also build test WARNING on next-20231017]
[cannot apply to linus/master v6.6-rc6]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-nf_tables-shrink-memory-consumption-of-set-elements/20231017-123501 base: git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main patch link: https://lore.kernel.org/r/20231013160924.119273-1-pablo%40netfilter.org patch subject: [PATCH nf-next,RFC] netfilter: nf_tables: shrink memory consumption of set elements config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231017/[email protected]/config) compiler: m68k-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231017/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> net/netfilter/nft_set_pipapo.c:1740: warning: Function parameter or member 'elem_priv' not described in 'nft_pipapo_activate' >> net/netfilter/nft_set_pipapo.c:1740: warning: Excess function parameter 'elem' description in 'nft_pipapo_activate' >> net/netfilter/nft_set_pipapo.c:1946: warning: Function parameter or member 'elem_priv' not described in 'nft_pipapo_remove' >> net/netfilter/nft_set_pipapo.c:1946: warning: Excess function parameter 'elem' description in 'nft_pipapo_remove'
vim +1740 net/netfilter/nft_set_pipapo.c
212ed75dc5fb9d Pablo Neira Ayuso 2023-06-07 1726
3c4287f62044a9 Stefano Brivio 2020-01-22 1727 /**
3c4287f62044a9 Stefano Brivio 2020-01-22 1728 * nft_pipapo_activate() - Mark element reference as active given key, commit
3c4287f62044a9 Stefano Brivio 2020-01-22 1729 * @net: Network namespace
3c4287f62044a9 Stefano Brivio 2020-01-22 1730 * @set: nftables API set representation
3c4287f62044a9 Stefano Brivio 2020-01-22 1731 * @elem: nftables API element representation containing key data
3c4287f62044a9 Stefano Brivio 2020-01-22 1732 *
3c4287f62044a9 Stefano Brivio 2020-01-22 1733 * On insertion, elements are added to a copy of the matching data currently
212ed75dc5fb9d Pablo Neira Ayuso 2023-06-07 1734 * in use for lookups, and not directly inserted into current lookup data. Both
3c4287f62044a9 Stefano Brivio 2020-01-22 1735 * nft_pipapo_insert() and nft_pipapo_activate() are called once for each
3c4287f62044a9 Stefano Brivio 2020-01-22 1736 * element, hence we can't purpose either one as a real commit operation.
3c4287f62044a9 Stefano Brivio 2020-01-22 1737 */
3c4287f62044a9 Stefano Brivio 2020-01-22 1738 static void nft_pipapo_activate(const struct net *net,
dfe8f3c1db12ef Pablo Neira Ayuso 2023-10-13 1739 const struct nft_set *set, void *elem_priv)
3c4287f62044a9 Stefano Brivio 2020-01-22 @1740 {
dfe8f3c1db12ef Pablo Neira Ayuso 2023-10-13 1741 struct nft_pipapo_elem *e = elem_priv;
3c4287f62044a9 Stefano Brivio 2020-01-22 1742
3c4287f62044a9 Stefano Brivio 2020-01-22 1743 nft_set_elem_change_active(net, set, &e->ext);
3c4287f62044a9 Stefano Brivio 2020-01-22 1744 }
3c4287f62044a9 Stefano Brivio 2020-01-22 1745 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki