Skip to contentSkip to conversation

net_sched: some fixes and cleanup for tcindex

PATCH Applied Cong Wang 3 participants 17 messages Sep 15, 2014

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

cover net_sched: some fixes and cleanup for tcindex

Cong Wang (5):
net_sched: fix an allocation bug in tcindex_set_parms()
net_sched: fix memory leak in cls_tcindex
net_sched: fix suspicious RCU usage in tcindex_classify()
net_sched: use tcindex_filter_result_init()
net_sched: clean up tcindex_set_parms()

 net/sched/cls_tcindex.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/5 net_sched: fix an allocation bug in tcindex_set_parms()

Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/cls_tcindex.c +1 -1
index a9f4279..a02ca72 100644
@@ -241,7 +241,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	 * allocate new tcindex data and RCU assign it onto root. Keeping
 	 * perfect hash and hash pointers from old data.
 	 */
-	cp = kzalloc(sizeof(cp), GFP_KERNEL);
+	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 2/5 net_sched: fix memory leak in cls_tcindex

In reply to Cong Wang
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
net/sched/cls_tcindex.c +7 -6
index a02ca72..16ec1ed 100644
@@ -243,7 +243,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	 */
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
-		return -ENOMEM;
+		goto errout;
 
 	cp->mask = p->mask;
 	cp->shift = p->shift;
@@ -257,6 +257,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 				      sizeof(*r) * cp->hash, GFP_KERNEL);
 		if (!cp->perfect)
 			goto errout;
+		balloc = 1;
 	}
 	cp->h = p->h;
 
@@ -285,9 +286,9 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	if (cp->perfect) {
 		if (!valid_perfect_hash(cp) ||
 		    cp->hash > cp->alloc_hash)
-			goto errout;
+			goto errout_alloc;
 	} else if (cp->h && cp->hash != cp->alloc_hash) {
-		goto errout;
+		goto errout_alloc;
 	}
 
 	err = -EINVAL;
@@ -314,7 +315,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	 */
 	if (cp->perfect || valid_perfect_hash(cp))
 		if (handle >= cp->alloc_hash)
-			goto errout;
+			goto errout_alloc;
 
 
 	err = -ENOMEM;
@@ -324,7 +325,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 
 			cp->perfect = kcalloc(cp->hash, sizeof(*r), GFP_KERNEL);
 			if (!cp->perfect)
-				goto errout;
+				goto errout_alloc;
 			for (i = 0; i < cp->hash; i++)
 				tcf_exts_init(&cp->perfect[i].exts,
 					      TCA_TCINDEX_ACT,
@@ -338,7 +339,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 				       GFP_KERNEL);
 
 			if (!hash)
-				goto errout;
+				goto errout_alloc;
 
 			cp->h = hash;
 			balloc = 2;
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [Patch net-next 2/5] net_sched: fix memory leak in cls_tcindex

Quoted 12 lines · Cong Wang
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
net/sched/cls_tcindex.c +0 -0
index a02ca72..16ec1ed 100644

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 246

Quoted 5 lines
net/sched/cls_tcindex.c +1 -1
@@ -243,7 +243,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	 */
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
-		return -ENOMEM;
+		goto errout;

but you need to set 'err = -ENOMEM' then.

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 260

Quoted 8 lines
net/sched/cls_tcindex.c +1 -0
 	cp->mask = p->mask;
 	cp->shift = p->shift;
@@ -257,6 +257,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 				      sizeof(*r) * cp->hash, GFP_KERNEL);
 		if (!cp->perfect)
 			goto errout;
+		balloc = 1;

Actually can we just get rid of the balloc here altogether and remove the checks in errout_alloc so that cp->perfect and cp->h are freed unconditionally? They should be NULL if they are not being used because of the kzalloc.

Quoted 43 lines
net/sched/cls_tcindex.c +5 -5
 	}
 	cp->h = p->h;
 
@@ -285,9 +286,9 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	if (cp->perfect) {
 		if (!valid_perfect_hash(cp) ||
 		    cp->hash > cp->alloc_hash)
-			goto errout;
+			goto errout_alloc;
 	} else if (cp->h && cp->hash != cp->alloc_hash) {
-		goto errout;
+		goto errout_alloc;
 	}
 
 	err = -EINVAL;
@@ -314,7 +315,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	 */
 	if (cp->perfect || valid_perfect_hash(cp))
 		if (handle >= cp->alloc_hash)
-			goto errout;
+			goto errout_alloc;
 
 
 	err = -ENOMEM;
@@ -324,7 +325,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 
 			cp->perfect = kcalloc(cp->hash, sizeof(*r), GFP_KERNEL);
 			if (!cp->perfect)
-				goto errout;
+				goto errout_alloc;
 			for (i = 0; i < cp->hash; i++)
 				tcf_exts_init(&cp->perfect[i].exts,
 					      TCA_TCINDEX_ACT,
@@ -338,7 +339,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 				       GFP_KERNEL);
 
 			if (!hash)
-				goto errout;
+				goto errout_alloc;
 
 			cp->h = hash;
 			balloc = 2;
author
#5 lore raw

Re: [Patch net-next 2/5] net_sched: fix memory leak in cls_tcindex

Quoted 8 lines · John Fastabend
Quoted 7 lines · Cong Wang
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Quoted 12 lines

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 246

Quoted 10 lines
net/sched/cls_tcindex.c +1 -1
index a02ca72..16ec1ed 100644
@@ -243,7 +243,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
       */
      cp = kzalloc(sizeof(*cp), GFP_KERNEL);
      if (!cp)
-             return -ENOMEM;
+             goto errout;

but you need to set 'err = -ENOMEM' then.

Yeah, I thought err is initialized to ENOMEM.

Quoted 15 lines

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 260

Quoted 9 lines
net/sched/cls_tcindex.c +1 -0
      cp->mask = p->mask;
      cp->shift = p->shift;
@@ -257,6 +257,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
                                    sizeof(*r) * cp->hash, GFP_KERNEL);
              if (!cp->perfect)
                      goto errout;
+             balloc = 1;

Actually can we just get rid of the balloc here altogether and remove the checks in errout_alloc so that cp->perfect and cp->h are freed unconditionally? They should be NULL if they are not being used because of the kzalloc.

Hmm, but for cp->h which you don't duplicate if we free it we free p->h too, which seems not expected. I think we probably need to cp->h = kmemdup(p->h) as well.

author
#6 lore raw

Re: [Patch net-next 2/5] net_sched: fix memory leak in cls_tcindex

Quoted 11 lines · Cong Wang
Quoted 5 lines · John Fastabend

Actually can we just get rid of the balloc here altogether and remove the checks in errout_alloc so that cp->perfect and cp->h are freed unconditionally? They should be NULL if they are not being used because of the kzalloc.

Hmm, but for cp->h which you don't duplicate if we free it we free p->h too, which seems not expected. I think we probably need to cp->h = kmemdup(p->h) as well.

I can see why you didn't use kmemdup() there, because p->h is accessible for readers since we now use RCU read lock. I think that means we probably have to duplicate the whole hash table including the chained elements. But it deserves another patch, to fix this memory leak I think we should just keep the balloc logic.

I will just fix the missing err = -ENOMEM.

Thanks.

author
#8 lore raw

PATCH 3/5 net_sched: fix suspicious RCU usage in tcindex_classify()

In reply to Cong Wang

This patch fixes the following kernel warning:

[   44.805900] [ INFO: suspicious RCU usage. ]
[   44.808946] 3.17.0-rc4+ #610 Not tainted
[   44.811831] -------------------------------
[   44.814873] net/sched/cls_tcindex.c:84 suspicious rcu_dereference_check() usage!
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/cls_tcindex.c +1 -1
index 16ec1ed..f276de5 100644
@@ -81,7 +81,7 @@ tcindex_lookup(struct tcindex_data *p, u16 key)
 static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			    struct tcf_result *res)
 {
-	struct tcindex_data *p = rcu_dereference(tp->root);
+	struct tcindex_data *p = rcu_dereference_bh(tp->root);
 	struct tcindex_filter_result *f;
 	int key = (skb->tc_index & p->mask) >> p->shift;
 
Signature · 1 line
1.8.3.1
Acked-by on v1 3/5
#9 lore raw

Re: [Patch net-next 3/5] net_sched: fix suspicious RCU usage in tcindex_classify()

Quoted 20 lines · Cong Wang

This patch fixes the following kernel warning:

[   44.805900] [ INFO: suspicious RCU usage. ]
[   44.808946] 3.17.0-rc4+ #610 Not tainted
[   44.811831] -------------------------------
[   44.814873] net/sched/cls_tcindex.c:84 suspicious rcu_dereference_check() usage!
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/cls_tcindex.c +0 -0
index 16ec1ed..f276de5 100644

net/sched/cls_tcindex.c tcindex_lookup(struct tcindex_data *p, u16 key) · line 87

Quoted 8 lines
net/sched/cls_tcindex.c +1 -1
@@ -81,7 +81,7 @@ tcindex_lookup(struct tcindex_data *p, u16 key)
 static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			    struct tcf_result *res)
 {
-	struct tcindex_data *p = rcu_dereference(tp->root);
+	struct tcindex_data *p = rcu_dereference_bh(tp->root);
 	struct tcindex_filter_result *f;
 	int key = (skb->tc_index & p->mask) >> p->shift;
 

Thanks for catching/fixing. I also missed cls_bpf.c case I'll send a fix in a moment.

maintainer Applied
#10 lore raw

Re: [Patch net-next 3/5] net_sched: fix suspicious RCU usage in tcindex_classify()

In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 15 Sep 2014 14:06:48 -0700

Quoted 10 lines

This patch fixes the following kernel warning:

[   44.805900] [ INFO: suspicious RCU usage. ]
[   44.808946] 3.17.0-rc4+ #610 Not tainted
[   44.811831] -------------------------------
[   44.814873] net/sched/cls_tcindex.c:84 suspicious rcu_dereference_check() usage!
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>

Applied.

author
#11 lore raw

PATCH 4/5 net_sched: use tcindex_filter_result_init()

In reply to Cong Wang
Fixes commit 331b72922c5f58d48fd ("net: sched: RCU cls_tcindex")
Cc John Fastabend <[email protected]>
---
 net/sched/cls_tcindex.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
net/sched/cls_tcindex.c +1 -4
index f276de5..4ec99a6 100644
@@ -261,10 +261,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	}
 	cp->h = p->h;
 
-	memset(&new_filter_result, 0, sizeof(new_filter_result));
-	tcf_exts_init(&new_filter_result.exts,
-		      TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
-
+	tcindex_filter_result_init(&new_filter_result);
 	tcindex_filter_result_init(&cr);
 	if (old_r)
 		cr.res = r->res;
Signature · 1 line
1.8.3.1
author
#13 lore raw

PATCH 5/5 net_sched: clean up tcindex_set_parms()

In reply to Cong Wang

We can move the allocation down.

---
 net/sched/cls_tcindex.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/sched/cls_tcindex.c +5 -7
index 4ec99a6..688ab75 100644
@@ -229,7 +229,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	struct tcindex_filter_result new_filter_result, *old_r = r;
 	struct tcindex_filter_result cr;
 	struct tcindex_data *cp, *oldp;
-	struct tcindex_filter *f = NULL; /* make gcc behave */
 	struct tcf_exts e;
 
 	tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
@@ -348,12 +347,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	else
 		r = tcindex_lookup(cp, handle) ? : &new_filter_result;
 
-	if (r == &new_filter_result) {
-		f = kzalloc(sizeof(*f), GFP_KERNEL);
-		if (!f)
-			goto errout_alloc;
-	}
-
 	if (tb[TCA_TCINDEX_CLASSID]) {
 		cr.res.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
 		tcf_bind_filter(tp, &cr.res, base);
@@ -374,6 +367,11 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	if (r == &new_filter_result) {
 		struct tcindex_filter *nfp;
 		struct tcindex_filter __rcu **fp;
+		struct tcindex_filter *f;
+
+		f = kzalloc(sizeof(*f), GFP_KERNEL);
+		if (!f)
+			goto errout_alloc;
 
 		f->key = handle;
 		f->result = new_filter_result;
Signature · 1 line
1.8.3.1
#15 lore raw

Re: [Patch net-next 5/5] net_sched: clean up tcindex_set_parms()

In reply to Cong Wang
Quoted 20 lines · Cong Wang

We can move the allocation down.

---
 net/sched/cls_tcindex.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/sched/cls_tcindex.c +0 -1
index 4ec99a6..688ab75 100644
@@ -229,7 +229,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	struct tcindex_filter_result new_filter_result, *old_r = r;
 	struct tcindex_filter_result cr;
 	struct tcindex_data *cp, *oldp;
-	struct tcindex_filter *f = NULL; /* make gcc behave */
 	struct tcf_exts e;
 
 	tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 356

Quoted 9 lines
net/sched/cls_tcindex.c +0 -6
@@ -348,12 +347,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	else
 		r = tcindex_lookup(cp, handle) ? : &new_filter_result;
 
-	if (r == &new_filter_result) {
-		f = kzalloc(sizeof(*f), GFP_KERNEL);
-		if (!f)
-			goto errout_alloc;
-	}
-

It was put here to avoid having logic to unwind the state when/if the malloc fails.

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 374

Quoted 12 lines
net/sched/cls_tcindex.c +5 -0
 	if (tb[TCA_TCINDEX_CLASSID]) {
 		cr.res.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
 		tcf_bind_filter(tp, &cr.res, base);
@@ -374,6 +367,11 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
 	if (r == &new_filter_result) {
 		struct tcindex_filter *nfp;
 		struct tcindex_filter __rcu **fp;
+		struct tcindex_filter *f;
+
+		f = kzalloc(sizeof(*f), GFP_KERNEL);
+		if (!f)
+			goto errout_alloc;

I don't think its safe to bail out here, you've already done the tcf_exts_change and rcu_assign_pointer() then you free it down below leaving tp->root corrupted?

Thanks,
John

Quoted 4 lines
net/sched/cls_tcindex.c +0 -0
 		f->key = handle;
 		f->result = new_filter_result;
 
author
#16 lore raw

Re: [Patch net-next 5/5] net_sched: clean up tcindex_set_parms()

Quoted 11 lines · John Fastabend

net/sched/cls_tcindex.c tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, · line 356

Quoted 6 lines · Cong Wang
net/sched/cls_tcindex.c +0 -6
-     if (r == &new_filter_result) {
-             f = kzalloc(sizeof(*f), GFP_KERNEL);
-             if (!f)
-                     goto errout_alloc;
-     }
-

It was put here to avoid having logic to unwind the state when/if the malloc fails.

Right, some operation after that is not undoable so we have to keep it there. So drop this one.