net_sched: destroy proto tp when all filters are gone
authorCong Wang <cwang@twopensource.com>
Fri, 6 Mar 2015 19:47:59 +0000 (11:47 -0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 9 Mar 2015 19:35:55 +0000 (15:35 -0400)
Kernel automatically creates a tp for each
(kind, protocol, priority) tuple, which has handle 0,
when we add a new filter, but it still is left there
after we remove our own, unless we don't specify the
handle (literally means all the filters under
the tuple). For example this one is left:

  # tc filter show dev eth0
  filter parent 8001: protocol arp pref 49152 basic

The user-space is hard to clean up these for kernel
because filters like u32 are organized in a complex way.
So kernel is responsible to remove it after all filters
are gone.  Each type of filter has its own way to
store the filters, so each type has to provide its
way to check if all filters are gone.

Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <cwang@twopensource.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Acked-by: Jamal Hadi Salim<jhs@mojatatu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
12 files changed:
include/net/sch_generic.h
net/sched/cls_api.c
net/sched/cls_basic.c
net/sched/cls_bpf.c
net/sched/cls_cgroup.c
net/sched/cls_flow.c
net/sched/cls_fw.c
net/sched/cls_route.c
net/sched/cls_rsvp.h
net/sched/cls_tcindex.c
net/sched/cls_u32.c
net/sched/sch_api.c

index c605d305c577074d11bee6f19479dda8a4949ee3..6d778efcfdfd6c8a3973e03424625667ec350c3e 100644 (file)
@@ -213,7 +213,7 @@ struct tcf_proto_ops {
                                            const struct tcf_proto *,
                                            struct tcf_result *);
        int                     (*init)(struct tcf_proto*);
-       void                    (*destroy)(struct tcf_proto*);
+       bool                    (*destroy)(struct tcf_proto*, bool);
 
        unsigned long           (*get)(struct tcf_proto*, u32 handle);
        int                     (*change)(struct net *net, struct sk_buff *,
@@ -399,7 +399,7 @@ struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
                                const struct Qdisc_ops *ops, u32 parentid);
 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
                               const struct qdisc_size_table *stab);
-void tcf_destroy(struct tcf_proto *tp);
+bool tcf_destroy(struct tcf_proto *tp, bool force);
 void tcf_destroy_chain(struct tcf_proto __rcu **fl);
 
 /* Reset all TX qdiscs greater then index of a device.  */
index baef987fe2c036ae61f7108455ce1d828ec40e6c..8b0470e418dc6e9475464768d629969087e66b37 100644 (file)
@@ -286,7 +286,7 @@ replay:
                        RCU_INIT_POINTER(*back, next);
 
                        tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
-                       tcf_destroy(tp);
+                       tcf_destroy(tp, true);
                        err = 0;
                        goto errout;
                }
@@ -301,14 +301,20 @@ replay:
                        err = -EEXIST;
                        if (n->nlmsg_flags & NLM_F_EXCL) {
                                if (tp_created)
-                                       tcf_destroy(tp);
+                                       tcf_destroy(tp, true);
                                goto errout;
                        }
                        break;
                case RTM_DELTFILTER:
                        err = tp->ops->delete(tp, fh);
-                       if (err == 0)
+                       if (err == 0) {
                                tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
+                               if (tcf_destroy(tp, false)) {
+                                       struct tcf_proto *next = rtnl_dereference(tp->next);
+
+                                       RCU_INIT_POINTER(*back, next);
+                               }
+                       }
                        goto errout;
                case RTM_GETTFILTER:
                        err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
@@ -329,7 +335,7 @@ replay:
                tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
        } else {
                if (tp_created)
-                       tcf_destroy(tp);
+                       tcf_destroy(tp, true);
        }
 
 errout:
index fc399db86f11b17cb05536df8f211fe79c7a3232..0b8c3ace671f1fff47cf2a12f7e6428bb5704b9f 100644 (file)
@@ -96,11 +96,14 @@ static void basic_delete_filter(struct rcu_head *head)
        kfree(f);
 }
 
-static void basic_destroy(struct tcf_proto *tp)
+static bool basic_destroy(struct tcf_proto *tp, bool force)
 {
        struct basic_head *head = rtnl_dereference(tp->root);
        struct basic_filter *f, *n;
 
+       if (!force && !list_empty(&head->flist))
+               return false;
+
        list_for_each_entry_safe(f, n, &head->flist, link) {
                list_del_rcu(&f->link);
                tcf_unbind_filter(tp, &f->res);
@@ -108,6 +111,7 @@ static void basic_destroy(struct tcf_proto *tp)
        }
        RCU_INIT_POINTER(tp->root, NULL);
        kfree_rcu(head, rcu);
+       return true;
 }
 
 static int basic_delete(struct tcf_proto *tp, unsigned long arg)
index 6f7ed8f8e6ee76379381955c0fff5a67599254bd..243c9f225a734799cde949a1ff91c1a554e5d584 100644 (file)
@@ -137,11 +137,14 @@ static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
        return 0;
 }
 
-static void cls_bpf_destroy(struct tcf_proto *tp)
+static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
 {
        struct cls_bpf_head *head = rtnl_dereference(tp->root);
        struct cls_bpf_prog *prog, *tmp;
 
+       if (!force && !list_empty(&head->plist))
+               return false;
+
        list_for_each_entry_safe(prog, tmp, &head->plist, link) {
                list_del_rcu(&prog->link);
                tcf_unbind_filter(tp, &prog->res);
@@ -150,6 +153,7 @@ static void cls_bpf_destroy(struct tcf_proto *tp)
 
        RCU_INIT_POINTER(tp->root, NULL);
        kfree_rcu(head, rcu);
+       return true;
 }
 
 static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
index 221697ab0247c5e786c70e181a43c1145a30748b..ea611b21641241737223f34334c0189df00d11e7 100644 (file)
@@ -143,14 +143,18 @@ errout:
        return err;
 }
 
-static void cls_cgroup_destroy(struct tcf_proto *tp)
+static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
 {
        struct cls_cgroup_head *head = rtnl_dereference(tp->root);
 
+       if (!force)
+               return false;
+
        if (head) {
                RCU_INIT_POINTER(tp->root, NULL);
                call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
        }
+       return true;
 }
 
 static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
index 461410394d085917ee8b8039ab5cfd5f5a6cf7c3..a620c4e288a51f55771399f6c1f81328bab9f7c7 100644 (file)
@@ -557,17 +557,21 @@ static int flow_init(struct tcf_proto *tp)
        return 0;
 }
 
-static void flow_destroy(struct tcf_proto *tp)
+static bool flow_destroy(struct tcf_proto *tp, bool force)
 {
        struct flow_head *head = rtnl_dereference(tp->root);
        struct flow_filter *f, *next;
 
+       if (!force && !list_empty(&head->filters))
+               return false;
+
        list_for_each_entry_safe(f, next, &head->filters, list) {
                list_del_rcu(&f->list);
                call_rcu(&f->rcu, flow_destroy_filter);
        }
        RCU_INIT_POINTER(tp->root, NULL);
        kfree_rcu(head, rcu);
+       return true;
 }
 
 static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
index 9d9aa3e82b108d17fbb168310e60ba913e5f50fc..715e01e5910a94a9af40534ec5c0e820b96adc99 100644 (file)
@@ -133,14 +133,20 @@ static void fw_delete_filter(struct rcu_head *head)
        kfree(f);
 }
 
-static void fw_destroy(struct tcf_proto *tp)
+static bool fw_destroy(struct tcf_proto *tp, bool force)
 {
        struct fw_head *head = rtnl_dereference(tp->root);
        struct fw_filter *f;
        int h;
 
        if (head == NULL)
-               return;
+               return true;
+
+       if (!force) {
+               for (h = 0; h < HTSIZE; h++)
+                       if (rcu_access_pointer(head->ht[h]))
+                               return false;
+       }
 
        for (h = 0; h < HTSIZE; h++) {
                while ((f = rtnl_dereference(head->ht[h])) != NULL) {
@@ -152,6 +158,7 @@ static void fw_destroy(struct tcf_proto *tp)
        }
        RCU_INIT_POINTER(tp->root, NULL);
        kfree_rcu(head, rcu);
+       return true;
 }
 
 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
index bb8a60235d01c90e6de3a433c25559edd070b549..08a3b0a6f5abd3fd674d7bca32c62c2626608b15 100644 (file)
@@ -277,13 +277,20 @@ route4_delete_filter(struct rcu_head *head)
        kfree(f);
 }
 
-static void route4_destroy(struct tcf_proto *tp)
+static bool route4_destroy(struct tcf_proto *tp, bool force)
 {
        struct route4_head *head = rtnl_dereference(tp->root);
        int h1, h2;
 
        if (head == NULL)
-               return;
+               return true;
+
+       if (!force) {
+               for (h1 = 0; h1 <= 256; h1++) {
+                       if (rcu_access_pointer(head->table[h1]))
+                               return false;
+               }
+       }
 
        for (h1 = 0; h1 <= 256; h1++) {
                struct route4_bucket *b;
@@ -308,6 +315,7 @@ static void route4_destroy(struct tcf_proto *tp)
        }
        RCU_INIT_POINTER(tp->root, NULL);
        kfree_rcu(head, rcu);
+       return true;
 }
 
 static int route4_delete(struct tcf_proto *tp, unsigned long arg)
index edd8ade3fbc1f4358b4275940e6f62b3d814b3dd..02fa82792dab8334d1dc14408f7ed42a4db0c141 100644 (file)
@@ -291,13 +291,20 @@ rsvp_delete_filter(struct tcf_proto *tp, struct rsvp_filter *f)
        kfree_rcu(f, rcu);
 }
 
-static void rsvp_destroy(struct tcf_proto *tp)
+static bool rsvp_destroy(struct tcf_proto *tp, bool force)
 {
        struct rsvp_head *data = rtnl_dereference(tp->root);
        int h1, h2;
 
        if (data == NULL)
-               return;
+               return true;
+
+       if (!force) {
+               for (h1 = 0; h1 < 256; h1++) {
+                       if (rcu_access_pointer(data->ht[h1]))
+                               return false;
+               }
+       }
 
        RCU_INIT_POINTER(tp->root, NULL);
 
@@ -319,6 +326,7 @@ static void rsvp_destroy(struct tcf_proto *tp)
                }
        }
        kfree_rcu(data, rcu);
+       return true;
 }
 
 static int rsvp_delete(struct tcf_proto *tp, unsigned long arg)
index bd49bf547a479f139b25e0507b090d51c137c519..a557dbaf5afedaa7a3a3a18c53a83238f6d32420 100644 (file)
@@ -468,11 +468,14 @@ static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
        }
 }
 
-static void tcindex_destroy(struct tcf_proto *tp)
+static bool tcindex_destroy(struct tcf_proto *tp, bool force)
 {
        struct tcindex_data *p = rtnl_dereference(tp->root);
        struct tcf_walker walker;
 
+       if (!force)
+               return false;
+
        pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
        walker.count = 0;
        walker.skip = 0;
@@ -481,6 +484,7 @@ static void tcindex_destroy(struct tcf_proto *tp)
 
        RCU_INIT_POINTER(tp->root, NULL);
        call_rcu(&p->rcu, __tcindex_destroy);
+       return true;
 }
 
 
index 09487afbfd5187a312ab155df5da43548d0c326b..375e51b71c80560b8acf37079725e63d7786cdbf 100644 (file)
@@ -460,13 +460,35 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
        return -ENOENT;
 }
 
-static void u32_destroy(struct tcf_proto *tp)
+static bool ht_empty(struct tc_u_hnode *ht)
+{
+       unsigned int h;
+
+       for (h = 0; h <= ht->divisor; h++)
+               if (rcu_access_pointer(ht->ht[h]))
+                       return false;
+
+       return true;
+}
+
+static bool u32_destroy(struct tcf_proto *tp, bool force)
 {
        struct tc_u_common *tp_c = tp->data;
        struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
 
        WARN_ON(root_ht == NULL);
 
+       if (!force) {
+               if (root_ht) {
+                       if (root_ht->refcnt > 1)
+                               return false;
+                       if (root_ht->refcnt == 1) {
+                               if (!ht_empty(root_ht))
+                                       return false;
+                       }
+               }
+       }
+
        if (root_ht && --root_ht->refcnt == 0)
                u32_destroy_hnode(tp, root_ht);
 
@@ -491,6 +513,7 @@ static void u32_destroy(struct tcf_proto *tp)
        }
 
        tp->data = NULL;
+       return true;
 }
 
 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
index 243b7d169d6183f662ab7f30d0e93492b29e79e3..ad9eed70bc8f8e16c3118c6527374a952823e2c0 100644 (file)
@@ -1858,11 +1858,15 @@ reclassify:
 }
 EXPORT_SYMBOL(tc_classify);
 
-void tcf_destroy(struct tcf_proto *tp)
+bool tcf_destroy(struct tcf_proto *tp, bool force)
 {
-       tp->ops->destroy(tp);
-       module_put(tp->ops->owner);
-       kfree_rcu(tp, rcu);
+       if (tp->ops->destroy(tp, force)) {
+               module_put(tp->ops->owner);
+               kfree_rcu(tp, rcu);
+               return true;
+       }
+
+       return false;
 }
 
 void tcf_destroy_chain(struct tcf_proto __rcu **fl)
@@ -1871,7 +1875,7 @@ void tcf_destroy_chain(struct tcf_proto __rcu **fl)
 
        while ((tp = rtnl_dereference(*fl)) != NULL) {
                RCU_INIT_POINTER(*fl, tp->next);
-               tcf_destroy(tp);
+               tcf_destroy(tp, true);
        }
 }
 EXPORT_SYMBOL(tcf_destroy_chain);