Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / net / ipv6 / ip6_fib.c
index 332f1e0ff8e2c8363151cb7d15dad7ddc4eb3045..b2d1838897c933f6f1ceb2a6f64a2f67495ce3c9 100644 (file)
@@ -50,6 +50,7 @@ struct fib6_cleaner {
        struct fib6_walker w;
        struct net *net;
        int (*func)(struct rt6_info *, void *arg);
+       int sernum;
        void *arg;
 };
 
@@ -74,8 +75,6 @@ static int fib6_walk_continue(struct fib6_walker *w);
  *     result of redirects, path MTU changes, etc.
  */
 
-static atomic_t rt_sernum = ATOMIC_INIT(1);
-
 static void fib6_gc_timer_cb(unsigned long arg);
 
 static LIST_HEAD(fib6_walkers);
@@ -95,17 +94,22 @@ static void fib6_walker_unlink(struct fib6_walker *w)
        write_unlock_bh(&fib6_walker_lock);
 }
 
-static int fib6_new_sernum(void)
+static int fib6_new_sernum(struct net *net)
 {
        int new, old;
 
        do {
-               old = atomic_read(&rt_sernum);
+               old = atomic_read(&net->ipv6.fib6_sernum);
                new = old < INT_MAX ? old + 1 : 1;
-       } while (atomic_cmpxchg(&rt_sernum, old, new) != old);
+       } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
+                               old, new) != old);
        return new;
 }
 
+enum {
+       FIB6_NO_SERNUM_CHANGE = 0,
+};
+
 /*
  *     Auxiliary address test functions for the radix tree.
  *
@@ -417,14 +421,13 @@ out:
 static struct fib6_node *fib6_add_1(struct fib6_node *root,
                                     struct in6_addr *addr, int plen,
                                     int offset, int allow_create,
-                                    int replace_required)
+                                    int replace_required, int sernum)
 {
        struct fib6_node *fn, *in, *ln;
        struct fib6_node *pn = NULL;
        struct rt6key *key;
        int     bit;
        __be32  dir = 0;
-       int     sernum = fib6_new_sernum();
 
        RT6_TRACE("fib6_add_1\n");
 
@@ -842,6 +845,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
        int err = -ENOMEM;
        int allow_create = 1;
        int replace_required = 0;
+       int sernum = fib6_new_sernum(info->nl_net);
 
        if (info->nlh) {
                if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
@@ -854,7 +858,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
 
        fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
                        offsetof(struct rt6_info, rt6i_dst), allow_create,
-                       replace_required);
+                       replace_required, sernum);
        if (IS_ERR(fn)) {
                err = PTR_ERR(fn);
                fn = NULL;
@@ -888,14 +892,14 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
                        sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
                        atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
                        sfn->fn_flags = RTN_ROOT;
-                       sfn->fn_sernum = fib6_new_sernum();
+                       sfn->fn_sernum = sernum;
 
                        /* Now add the first leaf node to new subtree */
 
                        sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
                                        rt->rt6i_src.plen,
                                        offsetof(struct rt6_info, rt6i_src),
-                                       allow_create, replace_required);
+                                       allow_create, replace_required, sernum);
 
                        if (IS_ERR(sn)) {
                                /* If it is failed, discard just allocated
@@ -914,7 +918,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
                        sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
                                        rt->rt6i_src.plen,
                                        offsetof(struct rt6_info, rt6i_src),
-                                       allow_create, replace_required);
+                                       allow_create, replace_required, sernum);
 
                        if (IS_ERR(sn)) {
                                err = PTR_ERR(sn);
@@ -1515,6 +1519,16 @@ static int fib6_clean_node(struct fib6_walker *w)
                .nl_net = c->net,
        };
 
+       if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
+           w->node->fn_sernum != c->sernum)
+               w->node->fn_sernum = c->sernum;
+
+       if (!c->func) {
+               WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
+               w->leaf = NULL;
+               return 0;
+       }
+
        for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
                res = c->func(rt, c->arg);
                if (res < 0) {
@@ -1548,7 +1562,7 @@ static int fib6_clean_node(struct fib6_walker *w)
 
 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
                            int (*func)(struct rt6_info *, void *arg),
-                           bool prune, void *arg)
+                           bool prune, int sernum, void *arg)
 {
        struct fib6_cleaner c;
 
@@ -1558,14 +1572,16 @@ static void fib6_clean_tree(struct net *net, struct fib6_node *root,
        c.w.count = 0;
        c.w.skip = 0;
        c.func = func;
+       c.sernum = sernum;
        c.arg = arg;
        c.net = net;
 
        fib6_walk(&c.w);
 }
 
-void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
-                   void *arg)
+static void __fib6_clean_all(struct net *net,
+                            int (*func)(struct rt6_info *, void *),
+                            int sernum, void *arg)
 {
        struct fib6_table *table;
        struct hlist_head *head;
@@ -1577,13 +1593,19 @@ void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
                hlist_for_each_entry_rcu(table, head, tb6_hlist) {
                        write_lock_bh(&table->tb6_lock);
                        fib6_clean_tree(net, &table->tb6_root,
-                                       func, false, arg);
+                                       func, false, sernum, arg);
                        write_unlock_bh(&table->tb6_lock);
                }
        }
        rcu_read_unlock();
 }
 
+void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
+                   void *arg)
+{
+       __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
+}
+
 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
 {
        if (rt->rt6i_flags & RTF_CACHE) {
@@ -1596,25 +1618,15 @@ static int fib6_prune_clone(struct rt6_info *rt, void *arg)
 
 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
 {
-       fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
-}
-
-static int fib6_update_sernum(struct rt6_info *rt, void *arg)
-{
-       int sernum = *(int *)arg;
-
-       if (rt->rt6i_node &&
-           rt->rt6i_node->fn_sernum != sernum)
-               rt->rt6i_node->fn_sernum = sernum;
-
-       return 0;
+       fib6_clean_tree(net, fn, fib6_prune_clone, true,
+                       FIB6_NO_SERNUM_CHANGE, NULL);
 }
 
 static void fib6_flush_trees(struct net *net)
 {
-       int new_sernum = fib6_new_sernum();
+       int new_sernum = fib6_new_sernum(net);
 
-       fib6_clean_all(net, fib6_update_sernum, &new_sernum);
+       __fib6_clean_all(net, NULL, new_sernum, NULL);
 }
 
 /*