netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Fastabend <john.fastabend@gmail.com>
To: xiyou.wangcong@gmail.com, jhs@mojatatu.com
Cc: netdev@vger.kernel.org, davem@davemloft.net
Subject: [RCU PATCH 11/14] net: make cls_bpf rcu safe
Date: Mon, 10 Mar 2014 10:07:51 -0700	[thread overview]
Message-ID: <20140310170749.3011.2927.stgit@nitbit.x32> (raw)
In-Reply-To: <20140310170008.3011.73599.stgit@nitbit.x32>

This patch makes the cls_bpf classifier RCU safe. The tcf_lock
was being used to protect a list of cls_bpf_prog now this list
is RCU safe and updates occur with rcu_replace.

Signed-off-by: John Fastabend <john.r.fastabend@intel.com>
---
 net/sched/cls_bpf.c |   79 ++++++++++++++++++++++++++-------------------------
 1 file changed, 40 insertions(+), 39 deletions(-)

diff --git a/net/sched/cls_bpf.c b/net/sched/cls_bpf.c
index 8e3cf49..7ec87e0 100644
--- a/net/sched/cls_bpf.c
+++ b/net/sched/cls_bpf.c
@@ -25,8 +25,9 @@ MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
 MODULE_DESCRIPTION("TC BPF based classifier");
 
 struct cls_bpf_head {
-	struct list_head plist;
+	struct list_head __rcu plist;
 	u32 hgen;
+	struct rcu_head rcu;
 };
 
 struct cls_bpf_prog {
@@ -37,6 +38,8 @@ struct cls_bpf_prog {
 	struct list_head link;
 	u32 handle;
 	u16 bpf_len;
+	struct tcf_proto *tp;
+	struct rcu_head rcu;
 };
 
 static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
@@ -49,11 +52,11 @@ static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			    struct tcf_result *res)
 {
-	struct cls_bpf_head *head = tp->root;
+	struct cls_bpf_head *head = rcu_dereference(tp->root);
 	struct cls_bpf_prog *prog;
 	int ret;
 
-	list_for_each_entry(prog, &head->plist, link) {
+	list_for_each_entry_rcu(prog, &head->plist, link) {
 		int filter_res = SK_RUN_FILTER(prog->filter, skb);
 
 		if (filter_res == 0)
@@ -81,8 +84,8 @@ static int cls_bpf_init(struct tcf_proto *tp)
 	if (head == NULL)
 		return -ENOBUFS;
 
-	INIT_LIST_HEAD(&head->plist);
-	tp->root = head;
+	INIT_LIST_HEAD_RCU(&head->plist);
+	rcu_assign_pointer(tp->root, head);
 
 	return 0;
 }
@@ -98,6 +101,13 @@ static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
 	kfree(prog);
 }
 
+void __cls_bpf_delete_prog(struct rcu_head *rcu)
+{
+	struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
+
+	cls_bpf_delete_prog(prog->tp, prog);
+}
+
 static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
 {
 	struct cls_bpf_head *head = tp->root;
@@ -105,11 +115,8 @@ static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
 
 	list_for_each_entry(prog, &head->plist, link) {
 		if (prog == todel) {
-			tcf_tree_lock(tp);
-			list_del(&prog->link);
-			tcf_tree_unlock(tp);
-
-			cls_bpf_delete_prog(tp, prog);
+			list_del_rcu(&prog->link);
+			call_rcu(&prog->rcu, __cls_bpf_delete_prog);
 			return 0;
 		}
 	}
@@ -123,11 +130,12 @@ static void cls_bpf_destroy(struct tcf_proto *tp)
 	struct cls_bpf_prog *prog, *tmp;
 
 	list_for_each_entry_safe(prog, tmp, &head->plist, link) {
-		list_del(&prog->link);
-		cls_bpf_delete_prog(tp, prog);
+		list_del_rcu(&prog->link);
+		call_rcu(&prog->rcu, __cls_bpf_delete_prog);
 	}
 
-	kfree(head);
+	rcu_assign_pointer(tp->root, NULL);
+	kfree_rcu(head, rcu);
 }
 
 static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
@@ -158,10 +166,10 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
 				   unsigned long base, struct nlattr **tb,
 				   struct nlattr *est)
 {
-	struct sock_filter *bpf_ops, *bpf_old;
+	struct sock_filter *bpf_ops;
 	struct tcf_exts exts;
 	struct sock_fprog tmp;
-	struct sk_filter *fp, *fp_old;
+	struct sk_filter *fp;
 	u16 bpf_size, bpf_len;
 	u32 classid;
 	int ret;
@@ -197,24 +205,13 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
 	if (ret)
 		goto errout_free;
 
-	tcf_tree_lock(tp);
-	fp_old = prog->filter;
-	bpf_old = prog->bpf_ops;
-
 	prog->bpf_len = bpf_len;
 	prog->bpf_ops = bpf_ops;
 	prog->filter = fp;
 	prog->res.classid = classid;
-	tcf_tree_unlock(tp);
 
 	tcf_bind_filter(tp, &prog->res, base);
 	tcf_exts_change(tp, &prog->exts, &exts);
-
-	if (fp_old)
-		sk_unattached_filter_destroy(fp_old);
-	if (bpf_old)
-		kfree(bpf_old);
-
 	return 0;
 
 errout_free:
@@ -245,8 +242,9 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 			  unsigned long *arg)
 {
 	struct cls_bpf_head *head = tp->root;
-	struct cls_bpf_prog *prog = (struct cls_bpf_prog *) *arg;
+	struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
 	struct nlattr *tb[TCA_BPF_MAX + 1];
+	struct cls_bpf_prog *prog;
 	int ret;
 
 	if (tca[TCA_OPTIONS] == NULL)
@@ -256,18 +254,19 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 	if (ret < 0)
 		return ret;
 
-	if (prog != NULL) {
-		if (handle && prog->handle != handle)
-			return -EINVAL;
-		return cls_bpf_modify_existing(net, tp, prog, base, tb,
-					       tca[TCA_RATE]);
-	}
-
 	prog = kzalloc(sizeof(*prog), GFP_KERNEL);
 	if (prog == NULL)
 		return -ENOBUFS;
 
 	tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
+
+	if (oldprog) {
+		if (handle && oldprog->handle != handle) {
+			ret = -EINVAL;
+			goto errout;
+		}
+	}
+
 	if (handle == 0)
 		prog->handle = cls_bpf_grab_new_handle(tp, head);
 	else
@@ -281,15 +280,17 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 	if (ret < 0)
 		goto errout;
 
-	tcf_tree_lock(tp);
-	list_add(&prog->link, &head->plist);
-	tcf_tree_unlock(tp);
+	if (oldprog) {
+		list_replace_rcu(&prog->link, &oldprog->link);
+		call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
+	} else {
+		list_add_rcu(&prog->link, &head->plist);
+	}
 
 	*arg = (unsigned long) prog;
-
 	return 0;
 errout:
-	if (*arg == 0UL && prog)
+	if (prog)
 		kfree(prog);
 
 	return ret;

  parent reply	other threads:[~2014-03-10 17:08 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-10 17:03 [RCU PATCH 00/14] Remove qdisc lock around ingress Qdisc John Fastabend
2014-03-10 17:03 ` [RCU PATCH 01/14] net: qdisc: use rcu prefix and silence sparse warnings John Fastabend
2014-03-10 17:20   ` Eric Dumazet
2014-03-10 17:04 ` [RCU PATCH 02/14] net: rcu-ify tcf_proto John Fastabend
2014-03-10 17:30   ` Eric Dumazet
2014-03-10 17:04 ` [RCU PATCH 03/14] net: sched: cls_basic use RCU John Fastabend
2014-03-10 17:33   ` Eric Dumazet
2014-03-10 17:04 ` [RCU PATCH 04/14] net: sched: cls_cgroup " John Fastabend
2014-03-10 17:36   ` Eric Dumazet
2014-03-10 17:05 ` [RCU PATCH 05/14] net: sched: cls_flow " John Fastabend
2014-03-10 17:38   ` Eric Dumazet
2014-03-10 17:05 ` [RCU PATCH 06/14] net: sched: fw " John Fastabend
2014-03-10 17:41   ` Eric Dumazet
2014-03-12 16:41     ` John Fastabend
2014-03-12 17:01       ` Eric Dumazet
2014-03-13 20:22         ` Paul E. McKenney
2014-03-13 20:56           ` Eric Dumazet
2014-03-13 21:15             ` Paul E. McKenney
2014-03-14  5:43               ` John Fastabend
2014-03-14 13:28                 ` Paul E. McKenney
2014-03-14 13:46                   ` Eric Dumazet
2014-03-14 15:38                     ` Paul E. McKenney
2014-03-14 18:50                       ` Paul E. McKenney
2014-03-14 18:59                         ` Paul E. McKenney
2014-03-14 19:55                           ` Eric Dumazet
2014-03-14 20:35                             ` Paul E. McKenney
2014-03-16 16:06                             ` [PATCH net-next] net: sched: use no more than one page in struct fw_head Eric Dumazet
2014-03-17 13:51                               ` Thomas Graf
2014-03-17 14:13                                 ` Eric Dumazet
2014-03-17 14:29                                   ` David Laight
2014-03-17 15:16                                     ` Eric Dumazet
2014-03-17 15:30                                       ` Thomas Graf
2014-03-17 15:33                                         ` Eric Dumazet
2014-03-17 15:43                                       ` David Laight
2014-03-17 15:52                                         ` Eric Dumazet
2014-03-17 15:28                                   ` Thomas Graf
2014-03-17 15:50                                     ` Thomas Graf
2014-03-17 16:00                                       ` David Laight
2014-03-17 16:16                                         ` Eric Dumazet
2014-03-18  2:31                               ` David Miller
2014-03-18  3:02                                 ` Eric Dumazet
2014-03-18  3:20                                   ` [PATCH v2 " Eric Dumazet
2014-03-18  9:19                                     ` Thomas Graf
2014-03-18 18:18                                     ` David Miller
2014-03-10 17:06 ` [RCU PATCH 07/14] net: sched: RCU cls_route John Fastabend
2014-03-10 17:45   ` Eric Dumazet
2014-03-10 19:36     ` John Fastabend
2014-03-10 17:06 ` [RCU PATCH 08/14] net: sched: RCU cls_tcindex John Fastabend
2014-03-10 17:07 ` [RCU PATCH 09/14] net: sched: make cls_u32 lockless John Fastabend
2014-03-10 17:58   ` Eric Dumazet
2014-03-10 17:07 ` [RCU PATCH 10/14] net: sched: rcu'ify cls_rsvp John Fastabend
2014-03-10 17:07 ` John Fastabend [this message]
2014-03-10 17:08 ` [RCU PATCH 12/14] net: sched: make tc_action safe to walk under RCU John Fastabend
2014-03-10 17:08 ` [RCU PATCH 13/14] net: sched: make bstats per cpu and estimator RCU safe John Fastabend
2014-03-10 18:06   ` Eric Dumazet
2014-03-10 19:36     ` John Fastabend
2014-03-10 17:09 ` [RCU PATCH 14/14] net: sched: drop ingress qdisc lock John Fastabend
2014-03-11 20:36 ` [RCU PATCH 00/14] Remove qdisc lock around ingress Qdisc David Miller
2014-03-11 20:53   ` Eric Dumazet
2014-03-12  6:58 ` Jamal Hadi Salim
2014-03-12 16:45   ` John Fastabend
2014-03-13  8:44     ` Jamal Hadi Salim
2014-03-14  7:28       ` John Fastabend
2014-03-14  7:45         ` Jamal Hadi Salim
2014-03-12 18:25 ` Cong Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140310170749.3011.2927.stgit@nitbit.x32 \
    --to=john.fastabend@gmail.com \
    --cc=davem@davemloft.net \
    --cc=jhs@mojatatu.com \
    --cc=netdev@vger.kernel.org \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).