Skip to contentSkip to conversation

net_sched: only create filter chains for new filters/actions

PATCH Applied Cong Wang 3 participants 11 messages May 23, 2017 – May 30, 2017

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 net_sched: only create filter chains for new filters/actions

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 include/net/pkt_cls.h |  3 ++-
 net/sched/act_api.c   |  2 +-
 net/sched/cls_api.c   | 13 +++++++++----
 3 files changed, 12 insertions(+), 6 deletions(-)
include/net/pkt_cls.h +2 -1
index 2c213a6..f776229 100644
@@ -18,7 +18,8 @@ int register_tcf_proto_ops(struct tcf_proto_ops *ops);
 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
 
 #ifdef CONFIG_NET_CLS
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index);
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+				bool create);
 void tcf_chain_put(struct tcf_chain *chain);
 int tcf_block_get(struct tcf_block **p_block,
 		  struct tcf_proto __rcu **p_filter_chain);
net/sched/act_api.c +1 -1
index 0ecf2a8..aed6cf2 100644
@@ -34,7 +34,7 @@ static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
 
 	if (!tp)
 		return -EINVAL;
-	a->goto_chain = tcf_chain_get(tp->chain->block, chain_index);
+	a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
 	if (!a->goto_chain)
 		return -ENOMEM;
 	return 0;
net/sched/cls_api.c +9 -4
index 01a8b8b..23d2236 100644
@@ -220,7 +220,8 @@ static void tcf_chain_destroy(struct tcf_chain *chain)
 	kfree(chain);
 }
 
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+				bool create)
 {
 	struct tcf_chain *chain;
 
@@ -230,7 +231,10 @@ struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
 			return chain;
 		}
 	}
-	return tcf_chain_create(block, chain_index);
+	if (create)
+		return tcf_chain_create(block, chain_index);
+	else
+		return NULL;
 }
 EXPORT_SYMBOL(tcf_chain_get);
 
@@ -509,9 +513,10 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
 		err = -EINVAL;
 		goto errout;
 	}
-	chain = tcf_chain_get(block, chain_index);
+	chain = tcf_chain_get(block, chain_index,
+			      n->nlmsg_type == RTM_NEWTFILTER);
 	if (!chain) {
-		err = -ENOMEM;
+		err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
 		goto errout;
 	}
 
Signature · 1 line
2.5.5
maintainer
#1 lore raw
Quoted 69 lines · Tue, May 23, 2017 at 06:42:37PM CEST, [email protected] wrote:

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

---

include/net/pkt_cls.h |  3 ++-
net/sched/act_api.c   |  2 +-
net/sched/cls_api.c   | 13 +++++++++----
3 files changed, 12 insertions(+), 6 deletions(-)
include/net/pkt_cls.h +2 -1
index 2c213a6..f776229 100644
@@ -18,7 +18,8 @@ int register_tcf_proto_ops(struct tcf_proto_ops *ops);
 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
 
 #ifdef CONFIG_NET_CLS
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index);
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+				bool create);
 void tcf_chain_put(struct tcf_chain *chain);
 int tcf_block_get(struct tcf_block **p_block,
 		  struct tcf_proto __rcu **p_filter_chain);
net/sched/act_api.c +1 -1
index 0ecf2a8..aed6cf2 100644
@@ -34,7 +34,7 @@ static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
 
 	if (!tp)
 		return -EINVAL;
-	a->goto_chain = tcf_chain_get(tp->chain->block, chain_index);
+	a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
 	if (!a->goto_chain)
 		return -ENOMEM;
 	return 0;
net/sched/cls_api.c +6 -2
index 01a8b8b..23d2236 100644
@@ -220,7 +220,8 @@ static void tcf_chain_destroy(struct tcf_chain *chain)
 	kfree(chain);
 }
 
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+				bool create)
 {
 	struct tcf_chain *chain;
 
@@ -230,7 +231,10 @@ struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
 			return chain;
 		}
 	}
-	return tcf_chain_create(block, chain_index);
+	if (create)
+		return tcf_chain_create(block, chain_index);
+	else
+		return NULL;
 }
 EXPORT_SYMBOL(tcf_chain_get);
 

net/sched/cls_api.c static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, · line 517

Quoted 6 lines
net/sched/cls_api.c +2 -1
@@ -509,9 +513,10 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
 		err = -EINVAL;
 		goto errout;
 	}
-	chain = tcf_chain_get(block, chain_index);
+	chain = tcf_chain_get(block, chain_index,
+			      n->nlmsg_type == RTM_NEWTFILTER);

First of all, I really hate all these true/false arg dances. Totaly confusing all the time.

net/sched/cls_api.c static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, · line 519

Quoted 3 lines
net/sched/cls_api.c +1 -1
 	if (!chain) {
-		err = -ENOMEM;
+		err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;

Confusing. Please do not obfuscate the code for a corner cases. Thanks.

Quoted 6 lines
net/sched/cls_api.c +0 -0
 		goto errout;
 	}
 
Signature · 1 line
2.5.5
author
#2 lore raw
Quoted 69 lines · Jiri Pirko
Quoted 68 lines · Tue, May 23, 2017 at 06:42:37PM CEST, [email protected] wrote:

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

---

include/net/pkt_cls.h |  3 ++-
net/sched/act_api.c   |  2 +-
net/sched/cls_api.c   | 13 +++++++++----
3 files changed, 12 insertions(+), 6 deletions(-)
include/net/pkt_cls.h +2 -1
index 2c213a6..f776229 100644
@@ -18,7 +18,8 @@ int register_tcf_proto_ops(struct tcf_proto_ops *ops);
 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
 
 #ifdef CONFIG_NET_CLS
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index);
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+                              bool create);
 void tcf_chain_put(struct tcf_chain *chain);
 int tcf_block_get(struct tcf_block **p_block,
                struct tcf_proto __rcu **p_filter_chain);
net/sched/act_api.c +1 -1
index 0ecf2a8..aed6cf2 100644
@@ -34,7 +34,7 @@ static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
 
      if (!tp)
              return -EINVAL;
-      a->goto_chain = tcf_chain_get(tp->chain->block, chain_index);
+      a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
      if (!a->goto_chain)
              return -ENOMEM;
      return 0;
net/sched/cls_api.c +6 -2
index 01a8b8b..23d2236 100644
@@ -220,7 +220,8 @@ static void tcf_chain_destroy(struct tcf_chain *chain)
      kfree(chain);
 }
 
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+                              bool create)
 {
      struct tcf_chain *chain;
 
@@ -230,7 +231,10 @@ struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
                      return chain;
              }
      }
-      return tcf_chain_create(block, chain_index);
+      if (create)
+              return tcf_chain_create(block, chain_index);
+      else
+              return NULL;
 }
 EXPORT_SYMBOL(tcf_chain_get);
 
Quoted 10 lines

net/sched/cls_api.c static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, · line 517

Quoted 7 lines
net/sched/cls_api.c +2 -1
@@ -509,9 +513,10 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
              err = -EINVAL;
              goto errout;
      }
-      chain = tcf_chain_get(block, chain_index);
+      chain = tcf_chain_get(block, chain_index,
+                            n->nlmsg_type == RTM_NEWTFILTER);

First of all, I really hate all these true/false arg dances. Totaly confusing all the time.

Sounds like you are able to understand the code at all. Sigh, I bet you never even read the changelog. ;)

Quoted 8 lines

net/sched/cls_api.c static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, · line 519

Quoted 6 lines
net/sched/cls_api.c +1 -1
      if (!chain) {
-              err = -ENOMEM;
+              err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;

Confusing. Please do not obfuscate the code for a corner cases. Thanks.

Either you don't understand the changelog or you don't understand ternary conditional operator. I can't help you if the latter.

Sorry.

maintainer
#3 lore raw
Quoted 92 lines · Wed, May 24, 2017 at 05:53:42PM CEST, [email protected] wrote:
Quoted 69 lines · Jiri Pirko
Quoted 68 lines · Tue, May 23, 2017 at 06:42:37PM CEST, [email protected] wrote:

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

---

include/net/pkt_cls.h |  3 ++-
net/sched/act_api.c   |  2 +-
net/sched/cls_api.c   | 13 +++++++++----
3 files changed, 12 insertions(+), 6 deletions(-)
include/net/pkt_cls.h +2 -1
index 2c213a6..f776229 100644
@@ -18,7 +18,8 @@ int register_tcf_proto_ops(struct tcf_proto_ops *ops);
 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
 
 #ifdef CONFIG_NET_CLS
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index);
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+                              bool create);
 void tcf_chain_put(struct tcf_chain *chain);
 int tcf_block_get(struct tcf_block **p_block,
                struct tcf_proto __rcu **p_filter_chain);
net/sched/act_api.c +1 -1
index 0ecf2a8..aed6cf2 100644
@@ -34,7 +34,7 @@ static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
 
      if (!tp)
              return -EINVAL;
-      a->goto_chain = tcf_chain_get(tp->chain->block, chain_index);
+      a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
      if (!a->goto_chain)
              return -ENOMEM;
      return 0;
net/sched/cls_api.c +6 -2
index 01a8b8b..23d2236 100644
@@ -220,7 +220,8 @@ static void tcf_chain_destroy(struct tcf_chain *chain)
      kfree(chain);
 }
 
-struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
+struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
+                              bool create)
 {
      struct tcf_chain *chain;
 
@@ -230,7 +231,10 @@ struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
                      return chain;
              }
      }
-      return tcf_chain_create(block, chain_index);
+      if (create)
+              return tcf_chain_create(block, chain_index);
+      else
+              return NULL;
 }
 EXPORT_SYMBOL(tcf_chain_get);
 
Quoted 10 lines

net/sched/cls_api.c static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, · line 517

Quoted 7 lines
net/sched/cls_api.c +2 -1
@@ -509,9 +513,10 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
              err = -EINVAL;
              goto errout;
      }
-      chain = tcf_chain_get(block, chain_index);
+      chain = tcf_chain_get(block, chain_index,
+                            n->nlmsg_type == RTM_NEWTFILTER);

First of all, I really hate all these true/false arg dances. Totaly confusing all the time.

Sounds like you are able to understand the code at all. Sigh, I bet you never even read the changelog. ;)

Quoted 7 lines
Quoted 6 lines
net/sched/cls_api.c +1 -1
      if (!chain) {
-              err = -ENOMEM;
+              err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Quoted 6 lines
Quoted 1 line

Confusing. Please do not obfuscate the code for a corner cases. Thanks.

Either you don't understand the changelog or you don't understand ternary conditional operator. I can't help you if the latter.

Sorry.

Heh. Really? All I say is, your patch is not needed at all. All it adds makes to code harder to understand and no benefit.

maintainer Applied
#4 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Tue, 23 May 2017 09:42:37 -0700

Quoted 9 lines

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

Indeed, get and delete requests should not create new objects, ever.

I have pretty much no idea why Jiri is making such a big fuss about this change, to be quite honest. :-)

Applied, thanks Cong.

maintainer
#5 lore raw
Quoted 13 lines · Thu, May 25, 2017 at 06:14:56PM CEST, [email protected] wrote:

From: Cong Wang <[email protected]>
Date: Tue, 23 May 2017 09:42:37 -0700

Quoted 9 lines

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
Quoted 4 lines

Indeed, get and delete requests should not create new objects, ever.

I have pretty much no idea why Jiri is making such a big fuss about this change, to be quite honest. :-)

Because it makes already hard to read code even worse, for *no* benefit. That's why.

Quoted 2 lines

Applied, thanks Cong.

maintainer
#6 lore raw

From: Jiri Pirko <[email protected]>
Date: Fri, 26 May 2017 07:53:52 +0200

Quoted 14 lines
Quoted 13 lines · Thu, May 25, 2017 at 06:14:56PM CEST, [email protected] wrote:

From: Cong Wang <[email protected]>
Date: Tue, 23 May 2017 09:42:37 -0700

Quoted 9 lines

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
Quoted 7 lines
Quoted 4 lines

Indeed, get and delete requests should not create new objects, ever.

I have pretty much no idea why Jiri is making such a big fuss about this change, to be quite honest. :-)

Because it makes already hard to read code even worse, for *no* benefit. That's why.

Jiri, if you say the same thing 100 times, it doesn't help anyone understand your arguments any better.

Creating new objects when a GET or a DEL is requested is flat out wrong.

And Cong is fixing that.

And I also didn't find the boolean logic hard to understand at all.

It is in fact a very common pattern to pass a "create" boolean into lookup functions, to tell them whether to create a new object on lookup failure or not. And then also to control that boolean via what kind of netlink request we are processing.

So you tell me what's so bad about his change given the above?

Give me details and real facts, like I just did, rather than vague statements about "benefit" and "hard to read".

Thank you.

author
#7 lore raw
Quoted 6 lines · David Miller

And I also didn't find the boolean logic hard to understand at all.

It is in fact a very common pattern to pass a "create" boolean into lookup functions, to tell them whether to create a new object on lookup failure or not. And then also to control that boolean via what kind of netlink request we are processing.

+10

It is a widely used pattern among the kernel source code. I'd be surprised if an experienced kernel developer is not aware of this pattern. ;)

maintainer
#10 lore raw
In reply to David Miller
Quoted 22 lines · Fri, May 26, 2017 at 04:54:43PM CEST, [email protected] wrote:

From: Jiri Pirko <[email protected]>
Date: Fri, 26 May 2017 07:53:52 +0200

Quoted 19 lines
Quoted 17 lines · Thu, May 25, 2017 at 06:14:56PM CEST, [email protected] wrote:

From: Cong Wang <[email protected]>
Date: Tue, 23 May 2017 09:42:37 -0700

Quoted 9 lines

tcf_chain_get() always creates a new filter chain if not found in existing ones. This is totally unnecessary when we get or delete filters, new chain should be only created for new filters (or new actions).

Fixes 5bc1701881e3 ("net: sched: introduce multichain support for filters")
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>

Indeed, get and delete requests should not create new objects, ever.

I have pretty much no idea why Jiri is making such a big fuss about this change, to be quite honest. :-)

Quoted 8 lines
Quoted 2 lines

Because it makes already hard to read code even worse, for *no* benefit. That's why.

Jiri, if you say the same thing 100 times, it doesn't help anyone understand your arguments any better.

Creating new objects when a GET or a DEL is requested is flat out wrong.

Allright. I ack that.

Quoted 10 lines

And Cong is fixing that.

And I also didn't find the boolean logic hard to understand at all.

It is in fact a very common pattern to pass a "create" boolean into lookup functions, to tell them whether to create a new object on lookup failure or not. And then also to control that boolean via what kind of netlink request we are processing.

Quoted 4 lines

So you tell me what's so bad about his change given the above?

Give me details and real facts, like I just did, rather than vague statements about "benefit" and "hard to read".

What I don't like is the double "n->nlmsg_type == RTM_NEWTFILTER" check and return value decusion according to the latter check. The code logic is split into tcf_chain_get function and its caller. That is at least odd.

Since you don't like the PTR_ERR approach, I'll try to figure out how to do this another way.

maintainer
#8 lore raw
In reply to Cong Wang
Quoted 8 lines · Fri, May 26, 2017 at 06:55:25PM CEST, [email protected] wrote:
Quoted 6 lines · David Miller

And I also didn't find the boolean logic hard to understand at all.

It is in fact a very common pattern to pass a "create" boolean into lookup functions, to tell them whether to create a new object on lookup failure or not. And then also to control that boolean via what kind of netlink request we are processing.

Quoted 5 lines

+10

It is a widely used pattern among the kernel source code. I'd be surprised if an experienced kernel developer is not aware of this pattern. ;)

Cong, as you wisely put, I'm not aware of this pattern and I'm also unaware of existence of ternary operator. Are this notes necessary? Does that make you feel better?

author
#9 lore raw
Quoted 4 lines · Jiri Pirko

Cong, as you wisely put, I'm not aware of this pattern and I'm also unaware of existence of ternary operator. Are this notes necessary? Does that make you feel better?

You are more than just welcome to redirect my email to your /dev/null. I am quite sure my email is waste for you.

Thanks!!