|
27 | 27 | static LIST_HEAD(nf_tables_expressions); |
28 | 28 | static LIST_HEAD(nf_tables_objects); |
29 | 29 | static LIST_HEAD(nf_tables_flowtables); |
| 30 | +static LIST_HEAD(nf_tables_destroy_list); |
| 31 | +static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); |
30 | 32 | static u64 table_handle; |
31 | 33 |
|
32 | 34 | enum { |
@@ -64,6 +66,8 @@ static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
64 | 66 |
|
65 | 67 | net->nft.validate_state = new_validate_state; |
66 | 68 | } |
| 69 | +static void nf_tables_trans_destroy_work(struct work_struct *w); |
| 70 | +static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); |
67 | 71 |
|
68 | 72 | static void nft_ctx_init(struct nft_ctx *ctx, |
69 | 73 | struct net *net, |
@@ -2453,7 +2457,6 @@ static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
2453 | 2457 | { |
2454 | 2458 | struct nft_expr *expr; |
2455 | 2459 |
|
2456 | | - lockdep_assert_held(&ctx->net->nft.commit_mutex); |
2457 | 2460 | /* |
2458 | 2461 | * Careful: some expressions might not be initialized in case this |
2459 | 2462 | * is called on error from nf_tables_newrule(). |
@@ -6224,19 +6227,28 @@ static void nft_commit_release(struct nft_trans *trans) |
6224 | 6227 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); |
6225 | 6228 | break; |
6226 | 6229 | } |
| 6230 | + |
| 6231 | + if (trans->put_net) |
| 6232 | + put_net(trans->ctx.net); |
| 6233 | + |
6227 | 6234 | kfree(trans); |
6228 | 6235 | } |
6229 | 6236 |
|
6230 | | -static void nf_tables_commit_release(struct net *net) |
| 6237 | +static void nf_tables_trans_destroy_work(struct work_struct *w) |
6231 | 6238 | { |
6232 | 6239 | 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); |
6233 | 6245 |
|
6234 | | - if (list_empty(&net->nft.commit_list)) |
| 6246 | + if (list_empty(&head)) |
6235 | 6247 | return; |
6236 | 6248 |
|
6237 | 6249 | synchronize_rcu(); |
6238 | 6250 |
|
6239 | | - list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { |
| 6251 | + list_for_each_entry_safe(trans, next, &head, list) { |
6240 | 6252 | list_del(&trans->list); |
6241 | 6253 | nft_commit_release(trans); |
6242 | 6254 | } |
@@ -6367,6 +6379,37 @@ static void nft_chain_del(struct nft_chain *chain) |
6367 | 6379 | list_del_rcu(&chain->list); |
6368 | 6380 | } |
6369 | 6381 |
|
| 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 | + |
6370 | 6413 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
6371 | 6414 | { |
6372 | 6415 | struct nft_trans *trans, *next; |
@@ -6528,9 +6571,8 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
6528 | 6571 | } |
6529 | 6572 | } |
6530 | 6573 |
|
6531 | | - nf_tables_commit_release(net); |
6532 | 6574 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
6533 | | - mutex_unlock(&net->nft.commit_mutex); |
| 6575 | + nf_tables_commit_release(net); |
6534 | 6576 |
|
6535 | 6577 | return 0; |
6536 | 6578 | } |
@@ -7304,6 +7346,7 @@ static int __init nf_tables_module_init(void) |
7304 | 7346 | { |
7305 | 7347 | int err; |
7306 | 7348 |
|
| 7349 | + spin_lock_init(&nf_tables_destroy_list_lock); |
7307 | 7350 | err = register_pernet_subsys(&nf_tables_net_ops); |
7308 | 7351 | if (err < 0) |
7309 | 7352 | return err; |
@@ -7343,6 +7386,7 @@ static void __exit nf_tables_module_exit(void) |
7343 | 7386 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
7344 | 7387 | nft_chain_filter_fini(); |
7345 | 7388 | unregister_pernet_subsys(&nf_tables_net_ops); |
| 7389 | + cancel_work_sync(&trans_destroy_work); |
7346 | 7390 | rcu_barrier(); |
7347 | 7391 | nf_tables_core_module_exit(); |
7348 | 7392 | } |
|
0 commit comments