Skip to content

Commit

Permalink
Merge branch 'net_sched-fix-races-with-RCU-callbacks'
Browse files Browse the repository at this point in the history
Cong Wang says:

====================
net_sched: fix races with RCU callbacks

Recently, the RCU callbacks used in TC filters and TC actions keep
drawing my attention, they introduce at least 4 race condition bugs:

1. A simple one fixed by Daniel:

commit c78e174
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Wed May 20 17:13:33 2015 +0200

    net: sched: fix call_rcu() race on classifier module unloads

2. A very nasty one fixed by me:

commit 1697c4b
Author: Cong Wang <xiyou.wangcong@gmail.com>
Date:   Mon Sep 11 16:33:32 2017 -0700

    net_sched: carefully handle tcf_block_put()

3. Two more bugs found by Chris:
https://patchwork.ozlabs.org/patch/826696/
https://patchwork.ozlabs.org/patch/826695/

Usually RCU callbacks are simple, however for TC filters and actions,
they are complex because at least TC actions could be destroyed
together with the TC filter in one callback. And RCU callbacks are
invoked in BH context, without locking they are parallel too. All of
these contribute to the cause of these nasty bugs.

Alternatively, we could also:

a) Introduce a spinlock to serialize these RCU callbacks. But as I
said in commit 1697c4b ("net_sched: carefully handle
tcf_block_put()"), it is very hard to do because of tcf_chain_dump().
Potentially we need to do a lot of work to make it possible (if not
impossible).

b) Just get rid of these RCU callbacks, because they are not
necessary at all, callers of these call_rcu() are all on slow paths
and holding RTNL lock, so blocking is allowed in their contexts.
However, David and Eric dislike adding synchronize_rcu() here.

As suggested by Paul, we could defer the work to a workqueue and
gain the permission of holding RTNL again without any performance
impact, however, in tcf_block_put() we could have a deadlock when
flushing workqueue while hodling RTNL lock, the trick here is to
defer the work itself in workqueue and make it queued after all
other works so that we keep the same ordering to avoid any
use-after-free. Please see the first patch for details.

Patch 1 introduces the infrastructure, patch 2~12 move each
tc filter to the new tc filter workqueue, patch 13 adds
an assertion to catch potential bugs like this, patch 14
closes another rcu callback race, patch 15 and patch 16 add
new test cases.
====================

Reported-by: Chris Mi <chrism@mellanox.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jiri Pirko <jiri@resnulli.us>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Oct 29, 2017
2 parents 8c83c88 + 31c2611 commit 6c325f4
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 57 deletions.
3 changes: 3 additions & 0 deletions include/net/pkt_cls.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __NET_PKT_CLS_H

#include <linux/pkt_cls.h>
#include <linux/workqueue.h>
#include <net/sch_generic.h>
#include <net/act_api.h>

Expand All @@ -17,6 +18,8 @@ struct tcf_walker {
int register_tcf_proto_ops(struct tcf_proto_ops *ops);
int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);

bool tcf_queue_work(struct work_struct *work);

#ifdef CONFIG_NET_CLS
struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
bool create);
Expand Down
2 changes: 2 additions & 0 deletions include/net/sch_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/dynamic_queue_limits.h>
#include <linux/list.h>
#include <linux/refcount.h>
#include <linux/workqueue.h>
#include <net/gen_stats.h>
#include <net/rtnetlink.h>

Expand Down Expand Up @@ -271,6 +272,7 @@ struct tcf_chain {

struct tcf_block {
struct list_head chain_list;
struct work_struct work;
};

static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
Expand Down
1 change: 1 addition & 0 deletions net/sched/act_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ static int __init sample_init_module(void)

static void __exit sample_cleanup_module(void)
{
rcu_barrier();
tcf_unregister_action(&act_sample_ops, &sample_net_ops);
}

Expand Down
69 changes: 52 additions & 17 deletions net/sched/cls_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ int register_tcf_proto_ops(struct tcf_proto_ops *ops)
}
EXPORT_SYMBOL(register_tcf_proto_ops);

static struct workqueue_struct *tc_filter_wq;

int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
{
struct tcf_proto_ops *t;
Expand All @@ -86,6 +88,7 @@ int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
* tcf_proto_ops's destroy() handler.
*/
rcu_barrier();
flush_workqueue(tc_filter_wq);

write_lock(&cls_mod_lock);
list_for_each_entry(t, &tcf_proto_base, head) {
Expand All @@ -100,6 +103,12 @@ int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
}
EXPORT_SYMBOL(unregister_tcf_proto_ops);

bool tcf_queue_work(struct work_struct *work)
{
return queue_work(tc_filter_wq, work);
}
EXPORT_SYMBOL(tcf_queue_work);

/* Select new prio value from the range, managed by kernel. */

static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Expand Down Expand Up @@ -266,23 +275,30 @@ int tcf_block_get(struct tcf_block **p_block,
}
EXPORT_SYMBOL(tcf_block_get);

void tcf_block_put(struct tcf_block *block)
static void tcf_block_put_final(struct work_struct *work)
{
struct tcf_block *block = container_of(work, struct tcf_block, work);
struct tcf_chain *chain, *tmp;

if (!block)
return;

/* XXX: Standalone actions are not allowed to jump to any chain, and
* bound actions should be all removed after flushing. However,
* filters are destroyed in RCU callbacks, we have to hold the chains
* first, otherwise we would always race with RCU callbacks on this list
* without proper locking.
*/
/* At this point, all the chains should have refcnt == 1. */
rtnl_lock();
list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
tcf_chain_put(chain);
rtnl_unlock();
kfree(block);
}

/* Wait for existing RCU callbacks to cool down. */
rcu_barrier();
/* XXX: Standalone actions are not allowed to jump to any chain, and bound
* actions should be all removed after flushing. However, filters are destroyed
* in RCU callbacks, we have to hold the chains first, otherwise we would
* always race with RCU callbacks on this list without proper locking.
*/
static void tcf_block_put_deferred(struct work_struct *work)
{
struct tcf_block *block = container_of(work, struct tcf_block, work);
struct tcf_chain *chain;

rtnl_lock();
/* Hold a refcnt for all chains, except 0, in case they are gone. */
list_for_each_entry(chain, &block->chain_list, list)
if (chain->index)
Expand All @@ -292,13 +308,27 @@ void tcf_block_put(struct tcf_block *block)
list_for_each_entry(chain, &block->chain_list, list)
tcf_chain_flush(chain);

/* Wait for RCU callbacks to release the reference count. */
INIT_WORK(&block->work, tcf_block_put_final);
/* Wait for RCU callbacks to release the reference count and make
* sure their works have been queued before this.
*/
rcu_barrier();
tcf_queue_work(&block->work);
rtnl_unlock();
}

/* At this point, all the chains should have refcnt == 1. */
list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
tcf_chain_put(chain);
kfree(block);
void tcf_block_put(struct tcf_block *block)
{
if (!block)
return;

INIT_WORK(&block->work, tcf_block_put_deferred);
/* Wait for existing RCU callbacks to cool down, make sure their works
* have been queued before this. We can not flush pending works here
* because we are holding the RTNL lock.
*/
rcu_barrier();
tcf_queue_work(&block->work);
}
EXPORT_SYMBOL(tcf_block_put);

Expand Down Expand Up @@ -879,6 +909,7 @@ void tcf_exts_destroy(struct tcf_exts *exts)
#ifdef CONFIG_NET_CLS_ACT
LIST_HEAD(actions);

ASSERT_RTNL();
tcf_exts_to_list(exts, &actions);
tcf_action_destroy(&actions, TCA_ACT_UNBIND);
kfree(exts->actions);
Expand Down Expand Up @@ -1030,6 +1061,10 @@ EXPORT_SYMBOL(tcf_exts_get_dev);

static int __init tc_filter_init(void)
{
tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
if (!tc_filter_wq)
return -ENOMEM;

rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Expand Down
20 changes: 17 additions & 3 deletions net/sched/cls_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ struct basic_filter {
struct tcf_result res;
struct tcf_proto *tp;
struct list_head link;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
};

static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Expand Down Expand Up @@ -82,15 +85,26 @@ static int basic_init(struct tcf_proto *tp)
return 0;
}

static void basic_delete_filter(struct rcu_head *head)
static void basic_delete_filter_work(struct work_struct *work)
{
struct basic_filter *f = container_of(head, struct basic_filter, rcu);
struct basic_filter *f = container_of(work, struct basic_filter, work);

rtnl_lock();
tcf_exts_destroy(&f->exts);
tcf_em_tree_destroy(&f->ematches);
rtnl_unlock();

kfree(f);
}

static void basic_delete_filter(struct rcu_head *head)
{
struct basic_filter *f = container_of(head, struct basic_filter, rcu);

INIT_WORK(&f->work, basic_delete_filter_work);
tcf_queue_work(&f->work);
}

static void basic_destroy(struct tcf_proto *tp)
{
struct basic_head *head = rtnl_dereference(tp->root);
Expand Down
19 changes: 17 additions & 2 deletions net/sched/cls_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ struct cls_bpf_prog {
struct sock_filter *bpf_ops;
const char *bpf_name;
struct tcf_proto *tp;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
};

static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
Expand Down Expand Up @@ -257,9 +260,21 @@ static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog)
kfree(prog);
}

static void cls_bpf_delete_prog_work(struct work_struct *work)
{
struct cls_bpf_prog *prog = container_of(work, struct cls_bpf_prog, work);

rtnl_lock();
__cls_bpf_delete_prog(prog);
rtnl_unlock();
}

static void cls_bpf_delete_prog_rcu(struct rcu_head *rcu)
{
__cls_bpf_delete_prog(container_of(rcu, struct cls_bpf_prog, rcu));
struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);

INIT_WORK(&prog->work, cls_bpf_delete_prog_work);
tcf_queue_work(&prog->work);
}

static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog)
Expand Down
22 changes: 18 additions & 4 deletions net/sched/cls_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ struct cls_cgroup_head {
struct tcf_exts exts;
struct tcf_ematch_tree ematches;
struct tcf_proto *tp;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
};

static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Expand Down Expand Up @@ -57,15 +60,26 @@ static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
[TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
};

static void cls_cgroup_destroy_work(struct work_struct *work)
{
struct cls_cgroup_head *head = container_of(work,
struct cls_cgroup_head,
work);
rtnl_lock();
tcf_exts_destroy(&head->exts);
tcf_em_tree_destroy(&head->ematches);
kfree(head);
rtnl_unlock();
}

static void cls_cgroup_destroy_rcu(struct rcu_head *root)
{
struct cls_cgroup_head *head = container_of(root,
struct cls_cgroup_head,
rcu);

tcf_exts_destroy(&head->exts);
tcf_em_tree_destroy(&head->ematches);
kfree(head);
INIT_WORK(&head->work, cls_cgroup_destroy_work);
tcf_queue_work(&head->work);
}

static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
Expand Down
19 changes: 16 additions & 3 deletions net/sched/cls_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ struct flow_filter {
u32 divisor;
u32 baseclass;
u32 hashrnd;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
};

static inline u32 addr_fold(void *addr)
Expand Down Expand Up @@ -369,14 +372,24 @@ static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
[TCA_FLOW_PERTURB] = { .type = NLA_U32 },
};

static void flow_destroy_filter(struct rcu_head *head)
static void flow_destroy_filter_work(struct work_struct *work)
{
struct flow_filter *f = container_of(head, struct flow_filter, rcu);
struct flow_filter *f = container_of(work, struct flow_filter, work);

rtnl_lock();
del_timer_sync(&f->perturb_timer);
tcf_exts_destroy(&f->exts);
tcf_em_tree_destroy(&f->ematches);
kfree(f);
rtnl_unlock();
}

static void flow_destroy_filter(struct rcu_head *head)
{
struct flow_filter *f = container_of(head, struct flow_filter, rcu);

INIT_WORK(&f->work, flow_destroy_filter_work);
tcf_queue_work(&f->work);
}

static int flow_change(struct net *net, struct sk_buff *in_skb,
Expand Down
19 changes: 16 additions & 3 deletions net/sched/cls_flower.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ struct cls_fl_filter {
struct list_head list;
u32 handle;
u32 flags;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
struct net_device *hw_dev;
};

Expand Down Expand Up @@ -215,12 +218,22 @@ static int fl_init(struct tcf_proto *tp)
return 0;
}

static void fl_destroy_filter(struct rcu_head *head)
static void fl_destroy_filter_work(struct work_struct *work)
{
struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);

rtnl_lock();
tcf_exts_destroy(&f->exts);
kfree(f);
rtnl_unlock();
}

static void fl_destroy_filter(struct rcu_head *head)
{
struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);

INIT_WORK(&f->work, fl_destroy_filter_work);
tcf_queue_work(&f->work);
}

static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
Expand Down
19 changes: 16 additions & 3 deletions net/sched/cls_fw.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ struct fw_filter {
#endif /* CONFIG_NET_CLS_IND */
struct tcf_exts exts;
struct tcf_proto *tp;
struct rcu_head rcu;
union {
struct work_struct work;
struct rcu_head rcu;
};
};

static u32 fw_hash(u32 handle)
Expand Down Expand Up @@ -119,12 +122,22 @@ static int fw_init(struct tcf_proto *tp)
return 0;
}

static void fw_delete_filter(struct rcu_head *head)
static void fw_delete_filter_work(struct work_struct *work)
{
struct fw_filter *f = container_of(head, struct fw_filter, rcu);
struct fw_filter *f = container_of(work, struct fw_filter, work);

rtnl_lock();
tcf_exts_destroy(&f->exts);
kfree(f);
rtnl_unlock();
}

static void fw_delete_filter(struct rcu_head *head)
{
struct fw_filter *f = container_of(head, struct fw_filter, rcu);

INIT_WORK(&f->work, fw_delete_filter_work);
tcf_queue_work(&f->work);
}

static void fw_destroy(struct tcf_proto *tp)
Expand Down
Loading

0 comments on commit 6c325f4

Please sign in to comment.