Skip to content

net_sched: fix races with RCU callbacks

Cong Wang netdev 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest16 patches 19 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/16net_sched: introduce a workqueue for RCU callbacks of tc filterinclude/net/pkt_cls.h include/net/sch_generic.h net/sched/cls_api.c+56 -177 replies
2/16net_sched: use tcf_queue_work() in basic filternet/sched/cls_basic.c+17 -30 replies
3/16net_sched: use tcf_queue_work() in bpf filternet/sched/cls_bpf.c+17 -20 replies
4/16net_sched: use tcf_queue_work() in cgroup filternet/sched/cls_cgroup.c+18 -40 replies
5/16net_sched: use tcf_queue_work() in flow filternet/sched/cls_flow.c+16 -30 replies
6/16net_sched: use tcf_queue_work() in flower filternet/sched/cls_flower.c+16 -30 replies
7/16net_sched: use tcf_queue_work() in fw filternet/sched/cls_fw.c+16 -30 replies
8/16net_sched: use tcf_queue_work() in matchall filternet/sched/cls_matchall.c+16 -30 replies
9/16net_sched: use tcf_queue_work() in u32 filternet/sched/cls_u32.c+26 -30 replies
10/16net_sched: use tcf_queue_work() in route filternet/sched/cls_route.c+16 -30 replies
11/16net_sched: use tcf_queue_work() in rsvp filternet/sched/cls_rsvp.h+16 -30 replies
12/16net_sched: use tcf_queue_work() in tcindex filternet/sched/cls_tcindex.c+33 -50 replies
13/16net_sched: add rtnl assertion to tcf_exts_destroy()net/sched/cls_api.c+1 -00 replies
14/16net_sched: fix call_rcu() race on act_sample module removalnet/sched/act_sample.c+1 -00 replies
15/16selftests: Introduce a new script to generate tc batch filetools/testing/selftests/tc-testing/tdc_batch.py+62 -00 replies
16/16selftests: Introduce a new test case to tc testsuitetools/testing/selftests/tc-testing/tc-tests/filters/tests.json tools/testing/selftests/tc-testing/tdc.py tools/testing/selftests/tc-testing/tdc_config.py+40 -50 replies

Reviews trailers per patch

2 of 16 patches have a Reviewed-by or Acked-by.

PatchJamal Hadi SalimLucas Bates
1/16
2/16
3/16
4/16
5/16
6/16
7/16
8/16
9/16
10/16
11/16
12/16
13/16
14/16
15/16A-b A-b
16/16A-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [Patch net 00/16] 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 c78e1746d3ad7d548bdf3fe491898cc453911a49

Author: Daniel Borkmann <[email protected]>
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 1697c4bb5245649a23f06a144cc38c06715e1b65

Author: Cong Wang <[email protected]>
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 1697c4bb5245 ("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.

Cc Daniel Borkmann <[email protected]>
Cc Jiri Pirko <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jamal Hadi Salim <[email protected]>
Cc "Paul E. McKenney" <[email protected]>

Chris Mi (2):
selftests: Introduce a new script to generate tc batch file
selftests: Introduce a new test case to tc testsuite

Cong Wang (14):
net_sched: introduce a workqueue for RCU callbacks of tc filter
net_sched: use tcf_queue_work() in basic filter
net_sched: use tcf_queue_work() in bpf filter
net_sched: use tcf_queue_work() in cgroup filter
net_sched: use tcf_queue_work() in flow filter
net_sched: use tcf_queue_work() in flower filter
net_sched: use tcf_queue_work() in fw filter
net_sched: use tcf_queue_work() in matchall filter
net_sched: use tcf_queue_work() in u32 filter
net_sched: use tcf_queue_work() in route filter
net_sched: use tcf_queue_work() in rsvp filter
net_sched: use tcf_queue_work() in tcindex filter
net_sched: add rtnl assertion to tcf_exts_destroy()
net_sched: fix call_rcu() race on act_sample module removal

 include/net/pkt_cls.h                              |  3 +
 include/net/sch_generic.h                          |  2 +
 net/sched/act_sample.c                             |  1 +
 net/sched/cls_api.c                                | 69 ++++++++++++++++------
 net/sched/cls_basic.c                              | 20 ++++++-
 net/sched/cls_bpf.c                                | 19 +++++-
 net/sched/cls_cgroup.c                             | 22 +++++--
 net/sched/cls_flow.c                               | 19 +++++-
 net/sched/cls_flower.c                             | 19 +++++-
 net/sched/cls_fw.c                                 | 19 +++++-
 net/sched/cls_matchall.c                           | 19 +++++-
 net/sched/cls_route.c                              | 19 +++++-
 net/sched/cls_rsvp.h                               | 19 +++++-
 net/sched/cls_tcindex.c                            | 38 ++++++++++--
 net/sched/cls_u32.c                                | 29 ++++++++-
 .../tc-testing/tc-tests/filters/tests.json         | 23 +++++++-
 tools/testing/selftests/tc-testing/tdc.py          | 20 +++++--
 tools/testing/selftests/tc-testing/tdc_batch.py    | 62 +++++++++++++++++++
 tools/testing/selftests/tc-testing/tdc_config.py   |  2 +
 19 files changed, 367 insertions(+), 57 deletions(-)
 create mode 100755 tools/testing/selftests/tc-testing/tdc_batch.py
Signature · 1 line
2.13.0