nf_tables: nft_rule_dump_ctx fits into netlink_callback
Subsystems (2)
Phil Sutter
reviewercover nf_tables: nft_rule_dump_ctx fits into netlink_callback
Struct netlink_callback has a 48byte scratch area for use by dump callbacks to keep personal stuff.
In rule dumps set up by nf_tables_getrule(), this is used only to store a cursor into the list of rules being dumped. Other data is allocated and the pointer value assigned to struct netlink_callback::data.
Since the allocated data structure is small and fits into the scratch area even after adding some more fields, move it there.
Patch 1 "simplifies" nf_tables_dump_rules_start() a bit, but actually exists only to reduce patch 5's size.
Patch 2 is more or less fallout: The memset would mess things up after this series, but it was pointless in the first place.
Patches 3 and 4 extend struct nft_rule_dump_ctx and make struct netlink_callback's scratch area unused.
Patch 5 then finally eliminates the allocation.
All this is early preparation for reset command locking but unrelated enough to go alone.
Phil Sutter (5):
netfilter: nf_tables: Always allocate nft_rule_dump_ctx
netfilter: nf_tables: Drop pointless memset when dumping rules
netfilter: nf_tables: Carry reset flag in nft_rule_dump_ctx
netfilter: nf_tables: Carry s_idx in nft_rule_dump_ctx
netfilter: nf_tables: Don't allocate nft_rule_dump_ctx
net/netfilter/nf_tables_api.c | 80 ++++++++++++++--------------------- 1 file changed, 31 insertions(+), 49 deletions(-)
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 1/5 netfilter: nf_tables: Always allocate nft_rule_dump_ctx
It will move into struct netlink_callback's scratch area later, just put nf_tables_dump_rules_start in shape to reduce churn later.
--- net/netfilter/nf_tables_api.c | 48 +++++++++++++++-------------------- 1 file changed, 21 insertions(+), 27 deletions(-)
net/netfilter/nf_tables_api.c +21 -27
@@ -3521,10 +3521,10 @@ static int nf_tables_dump_rules(struct sk_buff *skb,if (family !=NFPROTO_UNSPEC && family != table->family)continue ; -if (ctx && ctx->table &&strcmp (ctx->table, table->name) !=0 ) +if (ctx->table &&strcmp (ctx->table, table->name) !=0 )continue ; -if (ctx && ctx->table && ctx->chain) { +if (ctx->table && ctx->chain) {struct rhlist_head *list, *tmp; list = rhltable_lookup(&table->chains_ht, ctx->chain,
@@ -3548,7 +3548,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, goto done; } -if (ctx && ctx->table) +if (ctx->table)break ; } done:
@@ -3563,27 +3563,23 @@ static int nf_tables_dump_rules_start(struct netlink_callback *cb)const struct nlattr *const *nla = cb->data;struct nft_rule_dump_ctx *ctx =NULL ; -if (nla[NFTA_RULE_TABLE ] || nla[NFTA_RULE_CHAIN ]) { - ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); -if (!ctx) -return -ENOMEM ; + ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); +if (!ctx) +return -ENOMEM ; -if (nla[NFTA_RULE_TABLE ]) { - ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ], -GFP_ATOMIC ); -if (!ctx->table) { -kfree (ctx); -return -ENOMEM ; - } +if (nla[NFTA_RULE_TABLE ]) { + ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ],GFP_ATOMIC ); +if (!ctx->table) { +kfree (ctx); +return -ENOMEM ; } -if (nla[NFTA_RULE_CHAIN ]) { - ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ], -GFP_ATOMIC ); -if (!ctx->chain) { -kfree (ctx->table); -kfree (ctx); -return -ENOMEM ; - } + } +if (nla[NFTA_RULE_CHAIN ]) { + ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ],GFP_ATOMIC ); +if (!ctx->chain) { +kfree (ctx->table); +kfree (ctx); +return -ENOMEM ; } }
@@ -3595,11 +3591,9 @@ static int nf_tables_dump_rules_done(struct netlink_callback *cb) {struct nft_rule_dump_ctx *ctx = cb->data; -if (ctx) { -kfree (ctx->table); -kfree (ctx->chain); -kfree (ctx); - } +kfree (ctx->table); +kfree (ctx->chain); +kfree (ctx);return 0 ; }
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 2/5 netfilter: nf_tables: Drop pointless memset when dumping rules
In reply to Phil SutterNone of the dump callbacks uses netlink_callback::args beyond the first element, no need to zero the data.
--- net/netfilter/nf_tables_api.c | 4 ---- 1 file changed, 4 deletions(-)
net/netfilter/nf_tables_api.c +0 -4
@@ -3465,10 +3465,6 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, goto cont_skip;if (*idx < s_idx) goto cont; -if (*idx > s_idx) { -memset (&cb->args[1 ],0 , -sizeof (cb->args) -sizeof (cb->args[0 ])); - }if (prule) handle = prule->handle;else
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 3/5 netfilter: nf_tables: Carry reset flag in nft_rule_dump_ctx
In reply to Phil SutterThis relieves the dump callback from having to check nlmsg_type upon each call and instead performs the check once in .start callback.
--- net/netfilter/nf_tables_api.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +9 -10
@@ -3443,15 +3443,16 @@ static void audit_log_rule_reset(const struct nft_table *table,struct nft_rule_dump_ctx {char *table;char *chain; +bool reset; };static int __nf_tables_dump_rules (struct sk_buff *skb,unsigned int *idx,struct netlink_callback *cb,const struct nft_table *table, -const struct nft_chain *chain, -bool reset) +const struct nft_chain *chain) { +struct nft_rule_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule;unsigned int s_idx = cb->args[0 ];
@@ -3475,7 +3476,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, NFT_MSG_NEWRULE, NLM_F_MULTI | NLM_F_APPEND, table->family, - table, chain, rule, handle, reset) < 0) { + table, chain, rule, handle, ctx->reset) < 0) { ret =1 ;break ; }
@@ -3487,7 +3488,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, (*idx)++; } -if (reset && entries) +if (ctx->reset && entries)audit_log_rule_reset (table, cb->seq, entries);return ret;
@@ -3504,10 +3505,6 @@ static int nf_tables_dump_rules(struct sk_buff *skb,struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nftables_pernet *nft_net; -bool reset = false; - -if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) - reset = true;rcu_read_lock (); nft_net =nft_pernet (net);
@@ -3532,7 +3529,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,if (!nft_is_active (net, chain))continue ;__nf_tables_dump_rules (skb, &idx, - cb, table, chain, reset); + cb, table, chain);break ; } goto done;
@@ -3540,7 +3537,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry_rcu (chain, &table->chains, list) {if (__nf_tables_dump_rules (skb, &idx, - cb, table, chain, reset)) + cb, table, chain)) goto done; }
@@ -3578,6 +3575,8 @@ static int nf_tables_dump_rules_start(struct netlink_callback *cb)return -ENOMEM ; } } +if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) + ctx->reset = true; cb->data = ctx;return 0 ;
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 4/5 netfilter: nf_tables: Carry s_idx in nft_rule_dump_ctx
In reply to Phil SutterIn order to move the context into struct netlink_callback's scratch area, the latter must be unused first.
--- net/netfilter/nf_tables_api.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +4 -4
@@ -3441,6 +3441,7 @@ static void audit_log_rule_reset(const struct nft_table *table, }struct nft_rule_dump_ctx { +unsigned int s_idx;char *table;char *chain;bool reset;
@@ -3455,7 +3456,6 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,struct nft_rule_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule; -unsigned int s_idx = cb->args[0 ];unsigned int entries =0 ;int ret =0 ;u64 handle;
@@ -3464,7 +3464,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry_rcu (rule, &chain->rules, list) {if (!nft_is_active (net, rule)) goto cont_skip; -if (*idx < s_idx) +if (*idx < ctx->s_idx) goto cont;if (prule) handle = prule->handle;
@@ -3498,7 +3498,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -const struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = cb->data;struct nft_table *table;const struct nft_chain *chain;unsigned int idx =0 ;
@@ -3547,7 +3547,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, done:rcu_read_unlock (); - cb->args[0 ] = idx; + ctx->s_idx = idx;return skb->len; }
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 5/5 netfilter: nf_tables: Don't allocate nft_rule_dump_ctx
In reply to Phil SutterSince struct netlink_callback::args is not used by rule dumpers anymore, use it to hold nft_rule_dump_ctx. Add a build-time check to make sure it won't ever exceed the available space.
--- net/netfilter/nf_tables_api.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +6 -13
@@ -3453,7 +3453,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,const struct nft_table *table, const struct nft_chain *chain) { - struct nft_rule_dump_ctx *ctx = cb->data; + struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule;unsigned int entries =0 ;
@@ -3498,7 +3498,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;struct nft_table *table;const struct nft_chain *chain;unsigned int idx =0 ;
@@ -3553,42 +3553,35 @@ static int nf_tables_dump_rules(struct sk_buff *skb,static int nf_tables_dump_rules_start (struct netlink_callback *cb) { +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;const struct nlattr *const *nla = cb->data; -struct nft_rule_dump_ctx *ctx =NULL ; - ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); -if (!ctx) -return -ENOMEM ; +BUILD_BUG_ON (sizeof (*ctx) >sizeof (cb->ctx));if (nla[NFTA_RULE_TABLE ]) { ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ],GFP_ATOMIC ); -if (!ctx->table) { -kfree (ctx); +if (!ctx->table)return -ENOMEM ; - } }if (nla[NFTA_RULE_CHAIN ]) { ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ],GFP_ATOMIC );if (!ctx->chain) {kfree (ctx->table); -kfree (ctx);return -ENOMEM ; } }if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) ctx->reset = true; - cb->data = ctx;return 0 ; }static int nf_tables_dump_rules_done (struct netlink_callback *cb) { -struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;kfree (ctx->table);kfree (ctx->chain); -kfree (ctx);return 0 ; }
Signature · 1 line
2.41.0
Florian Westphal
maintainerQuoted 16 lines · Phil Sutter
Struct
netlink_callbackhas a 48byte scratch area for use by dump callbacks to keep personal stuff.In rule dumps set up by
nf_tables_getrule(), this is used only to store a cursor into the list of rules being dumped. Other data is allocated and the pointer value assigned to structnetlink_callback::data.Since the allocated data structure is small and fits into the scratch area even after adding some more fields, move it there.
Patch 1 "simplifies"
nf_tables_dump_rules_start()a bit, but actually exists only to reduce patch 5's size.Patch 2 is more or less fallout: The memset would mess things up after this series, but it was pointless in the first place.
Quoted 7 lines
Patches 3 and 4 extend struct
nft_rule_dump_ctxand make struct netlink_callback's scratch area unused.Patch 5 then finally eliminates the allocation.
All this is early preparation for reset command locking but unrelated enough to go alone.
LGTM, I've applied this to nf-next:testing to have the buildbots check it out.