Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / net / batman-adv / routing.c
index 7602c00..7e8dc64 100644 (file)
@@ -74,11 +74,23 @@ static void _batadv_update_route(struct batadv_priv *bat_priv,
        if (!orig_ifinfo)
                return;
 
-       rcu_read_lock();
-       curr_router = rcu_dereference(orig_ifinfo->router);
-       if (curr_router && !kref_get_unless_zero(&curr_router->refcount))
-               curr_router = NULL;
-       rcu_read_unlock();
+       spin_lock_bh(&orig_node->neigh_list_lock);
+       /* curr_router used earlier may not be the current orig_ifinfo->router
+        * anymore because it was dereferenced outside of the neigh_list_lock
+        * protected region. After the new best neighbor has replace the current
+        * best neighbor the reference counter needs to decrease. Consequently,
+        * the code needs to ensure the curr_router variable contains a pointer
+        * to the replaced best neighbor.
+        */
+       curr_router = rcu_dereference_protected(orig_ifinfo->router, true);
+
+       /* increase refcount of new best neighbor */
+       if (neigh_node)
+               kref_get(&neigh_node->refcount);
+
+       rcu_assign_pointer(orig_ifinfo->router, neigh_node);
+       spin_unlock_bh(&orig_node->neigh_list_lock);
+       batadv_orig_ifinfo_put(orig_ifinfo);
 
        /* route deleted */
        if ((curr_router) && (!neigh_node)) {
@@ -100,27 +112,6 @@ static void _batadv_update_route(struct batadv_priv *bat_priv,
                           curr_router->addr);
        }
 
-       if (curr_router)
-               batadv_neigh_node_put(curr_router);
-
-       spin_lock_bh(&orig_node->neigh_list_lock);
-       /* curr_router used earlier may not be the current orig_ifinfo->router
-        * anymore because it was dereferenced outside of the neigh_list_lock
-        * protected region. After the new best neighbor has replace the current
-        * best neighbor the reference counter needs to decrease. Consequently,
-        * the code needs to ensure the curr_router variable contains a pointer
-        * to the replaced best neighbor.
-        */
-       curr_router = rcu_dereference_protected(orig_ifinfo->router, true);
-
-       /* increase refcount of new best neighbor */
-       if (neigh_node)
-               kref_get(&neigh_node->refcount);
-
-       rcu_assign_pointer(orig_ifinfo->router, neigh_node);
-       spin_unlock_bh(&orig_node->neigh_list_lock);
-       batadv_orig_ifinfo_put(orig_ifinfo);
-
        /* decrease refcount of previous best neighbor */
        if (curr_router)
                batadv_neigh_node_put(curr_router);
@@ -469,6 +460,29 @@ static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
        return 0;
 }
 
+/**
+ * batadv_last_bonding_get - Get last_bonding_candidate of orig_node
+ * @orig_node: originator node whose last bonding candidate should be retrieved
+ *
+ * Return: last bonding candidate of router or NULL if not found
+ *
+ * The object is returned with refcounter increased by 1.
+ */
+static struct batadv_orig_ifinfo *
+batadv_last_bonding_get(struct batadv_orig_node *orig_node)
+{
+       struct batadv_orig_ifinfo *last_bonding_candidate;
+
+       spin_lock_bh(&orig_node->neigh_list_lock);
+       last_bonding_candidate = orig_node->last_bonding_candidate;
+
+       if (last_bonding_candidate)
+               kref_get(&last_bonding_candidate->refcount);
+       spin_unlock_bh(&orig_node->neigh_list_lock);
+
+       return last_bonding_candidate;
+}
+
 /**
  * batadv_last_bonding_replace - Replace last_bonding_candidate of orig_node
  * @orig_node: originator node whose bonding candidates should be replaced
@@ -539,7 +553,7 @@ batadv_find_router(struct batadv_priv *bat_priv,
         * router - obviously there are no other candidates.
         */
        rcu_read_lock();
-       last_candidate = orig_node->last_bonding_candidate;
+       last_candidate = batadv_last_bonding_get(orig_node);
        if (last_candidate)
                last_cand_router = rcu_dereference(last_candidate->router);
 
@@ -631,6 +645,9 @@ next:
                batadv_orig_ifinfo_put(next_candidate);
        }
 
+       if (last_candidate)
+               batadv_orig_ifinfo_put(last_candidate);
+
        return router;
 }