Skip to content

Commit 0935d55

Browse files
Florian Westphalummakynes
Florian Westphal
authored andcommitted
netfilter: nf_tables: asynchronous release
Release the committed transaction log from a work queue, moving expensive synchronize_rcu out of the locked section and providing opportunity to batch this. On my test machine this cuts runtime of nft-test.py in half. Based on earlier patch from Pablo Neira Ayuso. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 0ef235c commit 0935d55

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

include/net/netfilter/nf_tables.h

+2
Original file line numberDiff line numberDiff line change
@@ -1298,12 +1298,14 @@ static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
12981298
*
12991299
* @list: used internally
13001300
* @msg_type: message type
1301+
* @put_net: ctx->net needs to be put
13011302
* @ctx: transaction context
13021303
* @data: internal information related to the transaction
13031304
*/
13041305
struct nft_trans {
13051306
struct list_head list;
13061307
int msg_type;
1308+
bool put_net;
13071309
struct nft_ctx ctx;
13081310
char data[0];
13091311
};

net/netfilter/nf_tables_api.c

+50-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
static LIST_HEAD(nf_tables_expressions);
2828
static LIST_HEAD(nf_tables_objects);
2929
static LIST_HEAD(nf_tables_flowtables);
30+
static LIST_HEAD(nf_tables_destroy_list);
31+
static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
3032
static u64 table_handle;
3133

3234
enum {
@@ -64,6 +66,8 @@ static void nft_validate_state_update(struct net *net, u8 new_validate_state)
6466

6567
net->nft.validate_state = new_validate_state;
6668
}
69+
static void nf_tables_trans_destroy_work(struct work_struct *w);
70+
static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
6771

6872
static void nft_ctx_init(struct nft_ctx *ctx,
6973
struct net *net,
@@ -2453,7 +2457,6 @@ static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
24532457
{
24542458
struct nft_expr *expr;
24552459

2456-
lockdep_assert_held(&ctx->net->nft.commit_mutex);
24572460
/*
24582461
* Careful: some expressions might not be initialized in case this
24592462
* is called on error from nf_tables_newrule().
@@ -6224,19 +6227,28 @@ static void nft_commit_release(struct nft_trans *trans)
62246227
nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
62256228
break;
62266229
}
6230+
6231+
if (trans->put_net)
6232+
put_net(trans->ctx.net);
6233+
62276234
kfree(trans);
62286235
}
62296236

6230-
static void nf_tables_commit_release(struct net *net)
6237+
static void nf_tables_trans_destroy_work(struct work_struct *w)
62316238
{
62326239
struct nft_trans *trans, *next;
6240+
LIST_HEAD(head);
6241+
6242+
spin_lock(&nf_tables_destroy_list_lock);
6243+
list_splice_init(&nf_tables_destroy_list, &head);
6244+
spin_unlock(&nf_tables_destroy_list_lock);
62336245

6234-
if (list_empty(&net->nft.commit_list))
6246+
if (list_empty(&head))
62356247
return;
62366248

62376249
synchronize_rcu();
62386250

6239-
list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6251+
list_for_each_entry_safe(trans, next, &head, list) {
62406252
list_del(&trans->list);
62416253
nft_commit_release(trans);
62426254
}
@@ -6367,6 +6379,37 @@ static void nft_chain_del(struct nft_chain *chain)
63676379
list_del_rcu(&chain->list);
63686380
}
63696381

6382+
static void nf_tables_commit_release(struct net *net)
6383+
{
6384+
struct nft_trans *trans;
6385+
6386+
/* all side effects have to be made visible.
6387+
* For example, if a chain named 'foo' has been deleted, a
6388+
* new transaction must not find it anymore.
6389+
*
6390+
* Memory reclaim happens asynchronously from work queue
6391+
* to prevent expensive synchronize_rcu() in commit phase.
6392+
*/
6393+
if (list_empty(&net->nft.commit_list)) {
6394+
mutex_unlock(&net->nft.commit_mutex);
6395+
return;
6396+
}
6397+
6398+
trans = list_last_entry(&net->nft.commit_list,
6399+
struct nft_trans, list);
6400+
get_net(trans->ctx.net);
6401+
WARN_ON_ONCE(trans->put_net);
6402+
6403+
trans->put_net = true;
6404+
spin_lock(&nf_tables_destroy_list_lock);
6405+
list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list);
6406+
spin_unlock(&nf_tables_destroy_list_lock);
6407+
6408+
mutex_unlock(&net->nft.commit_mutex);
6409+
6410+
schedule_work(&trans_destroy_work);
6411+
}
6412+
63706413
static int nf_tables_commit(struct net *net, struct sk_buff *skb)
63716414
{
63726415
struct nft_trans *trans, *next;
@@ -6528,9 +6571,8 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)
65286571
}
65296572
}
65306573

6531-
nf_tables_commit_release(net);
65326574
nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
6533-
mutex_unlock(&net->nft.commit_mutex);
6575+
nf_tables_commit_release(net);
65346576

65356577
return 0;
65366578
}
@@ -7304,6 +7346,7 @@ static int __init nf_tables_module_init(void)
73047346
{
73057347
int err;
73067348

7349+
spin_lock_init(&nf_tables_destroy_list_lock);
73077350
err = register_pernet_subsys(&nf_tables_net_ops);
73087351
if (err < 0)
73097352
return err;
@@ -7343,6 +7386,7 @@ static void __exit nf_tables_module_exit(void)
73437386
unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
73447387
nft_chain_filter_fini();
73457388
unregister_pernet_subsys(&nf_tables_net_ops);
7389+
cancel_work_sync(&trans_destroy_work);
73467390
rcu_barrier();
73477391
nf_tables_core_module_exit();
73487392
}

0 commit comments

Comments
 (0)