From patchwork Thu Dec 30 02:09:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Wunderlich X-Patchwork-Id: 689 Return-Path: Received: from jack.hrz.tu-chemnitz.de (jack.hrz.tu-chemnitz.de [134.109.132.46]) by open-mesh.org (Postfix) with ESMTPS id D082A154088 for ; Thu, 30 Dec 2010 03:09:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tu-chemnitz.de; s=dkim2010; h=In-Reply-To:Content-Transfer-Encoding:Content-Type:MIME-Version:References:Message-ID:Subject:To:From:Date; bh=HDX5k17t37cQ6VQmNKXW4PMBZTe/46uY7FbzHf7Svd8=; b=dNSL5ihge/+9OmXqoECU28G3SOp01aNrrtCo29+PD2ph9P/sc2EEbMGkzTf3C7SGMI3wLPs+ajRfp/ERTyEqwotCpvSARj3IdrvLFkgztNKdBlYrRbUCQ1WxjDyzpi33NYTksXHZBqbBES4ysDhy7XZdRq5P/l8ru5VaDMsGs1k=; Received: from p57aa08e5.dip0.t-ipconnect.de ([87.170.8.229] helo=pandem0nium) by jack.hrz.tu-chemnitz.de with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1PY7x8-0002Fa-Fz for b.a.t.m.a.n@lists.open-mesh.org; Thu, 30 Dec 2010 03:09:20 +0100 Received: from dotslash by pandem0nium with local (Exim 4.69) (envelope-from ) id 1PY7x7-0001Ex-5L for b.a.t.m.a.n@lists.open-mesh.org; Thu, 30 Dec 2010 03:09:17 +0100 Date: Thu, 30 Dec 2010 03:09:17 +0100 From: Simon Wunderlich To: The list for a Better Approach To Mobile Ad-hoc Networking Message-ID: <20101230020917.GA4707@pandem0nium> References: <201012130118.14949.lindner_marek@yahoo.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <201012130118.14949.lindner_marek@yahoo.de> Precedence: first-class Priority: normal User-Agent: Mutt/1.5.18 (2008-05-17) X-Spam-Score: -1.0 (-) X-Spam-Report: --- Textanalyse SpamAssassin 3.3.1 (-1.0 Punkte) Fragen an/questions to: Postmaster TU Chemnitz * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP --- Ende Textanalyse X-Scan-Signature: 2fbd3519bf4367593df912bf577c0966 Subject: [B.A.T.M.A.N.] [PATCH] batman-adv: protect bonding with rcu locks X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.11 Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Dec 2010 02:09:22 -0000 bonding / alternating candidates need to be secured by rcu locks as well. This patch therefore converts the bonding list from a plain pointer list to a rcu securable lists and references the bonding candidates. Signed-off-by: Simon Wunderlich --- originator.c | 17 +++++++- routing.c | 140 +++++++++++++++++++++++++++++++++------------------------- types.h | 4 +- unicast.c | 9 +--- 4 files changed, 100 insertions(+), 70 deletions(-) diff --git a/batman-adv/originator.c b/batman-adv/originator.c index 899ab0b..3e18488 100644 --- a/batman-adv/originator.c +++ b/batman-adv/originator.c @@ -88,6 +88,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node, return NULL; INIT_HLIST_NODE(&neigh_node->list); + INIT_LIST_HEAD(&neigh_node->bonding_list); memcpy(neigh_node->addr, neigh, ETH_ALEN); neigh_node->orig_node = orig_neigh_node; @@ -103,13 +104,20 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node, void orig_node_free_ref(struct kref *refcount) { struct hlist_node *node, *node_tmp; - struct neigh_node *neigh_node; + struct neigh_node *neigh_node, *tmp_neigh_node; struct orig_node *orig_node; orig_node = container_of(refcount, struct orig_node, refcount); spin_lock_bh(&orig_node->neigh_list_lock); + /* for all bonding members ... */ + list_for_each_entry_safe(neigh_node, tmp_neigh_node, + &orig_node->bond.selected, bonding_list) { + list_del_rcu(&neigh_node->bonding_list); + call_rcu(&neigh_node->rcu, neigh_node_free_rcu); + } + /* for all neighbors towards this originator ... */ hlist_for_each_entry_safe(neigh_node, node, node_tmp, &orig_node->neigh_list, list) { @@ -202,6 +210,7 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr) return NULL; INIT_HLIST_HEAD(&orig_node->neigh_list); + INIT_LIST_HEAD(&orig_node->bond.selected); spin_lock_init(&orig_node->ogm_cnt_lock); spin_lock_init(&orig_node->neigh_list_lock); kref_init(&orig_node->refcount); @@ -285,6 +294,12 @@ static bool purge_orig_neighbors(struct bat_priv *bat_priv, neigh_purged = true; hlist_del_rcu(&neigh_node->list); + + if (!list_empty(&neigh_node->bonding_list)) { + orig_node->bond.candidates--; + list_del_rcu(&neigh_node->bonding_list); + call_rcu(&neigh_node->rcu, neigh_node_free_rcu); + } call_rcu(&neigh_node->rcu, neigh_node_free_rcu); } else { if ((!*best_neigh_node) || diff --git a/batman-adv/routing.c b/batman-adv/routing.c index 557e7d7..ad8d237 100644 --- a/batman-adv/routing.c +++ b/batman-adv/routing.c @@ -517,7 +517,6 @@ void update_bonding_candidates(struct bat_priv *bat_priv, int best_tq; struct hlist_node *node, *node2; struct neigh_node *tmp_neigh_node, *tmp_neigh_node2; - struct neigh_node *first_candidate, *last_candidate; /* update the candidates for this originator */ if (!orig_node->router) { @@ -525,6 +524,7 @@ void update_bonding_candidates(struct bat_priv *bat_priv, return; } + spin_lock_bh(&orig_node->neigh_list_lock); best_tq = orig_node->router->tq_avg; /* update bond.candidates */ @@ -535,19 +535,14 @@ void update_bonding_candidates(struct bat_priv *bat_priv, * as "bonding partner" */ /* first, zero the list */ - rcu_read_lock(); - hlist_for_each_entry_rcu(tmp_neigh_node, node, - &orig_node->neigh_list, list) { - tmp_neigh_node->next_bond_candidate = NULL; + list_for_each_entry_safe(tmp_neigh_node, tmp_neigh_node2, + &orig_node->bond.selected, bonding_list) { + list_del_rcu(&tmp_neigh_node->bonding_list); + kref_put(&tmp_neigh_node->refcount, neigh_node_free_ref); } - rcu_read_unlock(); - first_candidate = NULL; - last_candidate = NULL; - - rcu_read_lock(); hlist_for_each_entry_rcu(tmp_neigh_node, node, - &orig_node->neigh_list, list) { + &orig_node->neigh_list, list) { /* only consider if it has the same primary address ... */ if (memcmp(orig_node->orig, @@ -572,7 +567,7 @@ void update_bonding_candidates(struct bat_priv *bat_priv, /* we only care if the other candidate is even * considered as candidate. */ - if (!tmp_neigh_node2->next_bond_candidate) + if (!list_empty(&tmp_neigh_node2->bonding_list)) continue; @@ -589,24 +584,16 @@ void update_bonding_candidates(struct bat_priv *bat_priv, if (interference_candidate) continue; - if (!first_candidate) { - first_candidate = tmp_neigh_node; - tmp_neigh_node->next_bond_candidate = first_candidate; - } else - tmp_neigh_node->next_bond_candidate = last_candidate; - - last_candidate = tmp_neigh_node; + list_add_rcu(&tmp_neigh_node->bonding_list, + &orig_node->bond.selected); + kref_get(&tmp_neigh_node->refcount); candidates++; } - rcu_read_unlock(); - - if (candidates > 0) { - first_candidate->next_bond_candidate = last_candidate; - orig_node->bond.selected = first_candidate; - } - orig_node->bond.candidates = candidates; + + spin_unlock_bh(&orig_node->neigh_list_lock); + } void receive_bat_packet(struct ethhdr *ethhdr, @@ -1110,16 +1097,18 @@ out: } /* find a suitable router for this originator, and use - * bonding if possible. */ + * bonding if possible. increases the found neighbors + * refcount.*/ struct neigh_node *find_router(struct bat_priv *bat_priv, struct orig_node *orig_node, struct batman_if *recv_if) { struct orig_node *primary_orig_node; struct orig_node *router_orig; - struct neigh_node *router, *first_candidate, *best_router; + struct neigh_node *router, *first_candidate, *tmp_neigh_node; static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; int bonding_enabled; + int best_router_tq; if (!orig_node) return NULL; @@ -1132,15 +1121,23 @@ struct neigh_node *find_router(struct bat_priv *bat_priv, bonding_enabled = atomic_read(&bat_priv->bonding); - if ((!recv_if) && (!bonding_enabled)) - return orig_node->router; - + rcu_read_lock(); + /* select default router to output */ + router = orig_node->router; router_orig = orig_node->router->orig_node; + if (!router_orig) { + rcu_read_unlock(); + return NULL; + } + + + if ((!recv_if) && (!bonding_enabled)) + goto return_router; /* if we have something in the primary_addr, we can search * for a potential bonding candidate. */ if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0) - return orig_node->router; + goto return_router; /* find the orig_node which has the primary interface. might * even be the same as our router_orig in many cases */ @@ -1149,60 +1146,83 @@ struct neigh_node *find_router(struct bat_priv *bat_priv, router_orig->orig, ETH_ALEN) == 0) { primary_orig_node = router_orig; } else { - rcu_read_lock(); primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig, choose_orig, router_orig->primary_addr); - rcu_read_unlock(); - if (!primary_orig_node) - return orig_node->router; + goto return_router; } - /* with less than 2 candidates, we can't do any * bonding and prefer the original router. */ if (primary_orig_node->bond.candidates < 2) - return orig_node->router; + goto return_router; /* all nodes between should choose a candidate which * is is not on the interface where the packet came * in. */ - first_candidate = primary_orig_node->bond.selected; - router = first_candidate; + + first_candidate = NULL; + router = NULL; if (bonding_enabled) { /* in the bonding case, send the packets in a round * robin fashion over the remaining interfaces. */ - do { + + list_for_each_entry_rcu(tmp_neigh_node, + &primary_orig_node->bond.selected, bonding_list) { + if (!first_candidate) + first_candidate = tmp_neigh_node; /* recv_if == NULL on the first node. */ - if (router->if_incoming != recv_if) + if (tmp_neigh_node->if_incoming != recv_if) { + router = tmp_neigh_node; break; + } + } - router = router->next_bond_candidate; - } while (router != first_candidate); + /* use the first candidate if nothing was found. */ + if (!router) + router = first_candidate; - primary_orig_node->bond.selected = router->next_bond_candidate; + /* selected should point to the next element + * after the current router */ + spin_lock_bh(&primary_orig_node->neigh_list_lock); + /* this is a list_move(), which unfortunately + * does not exist as rcu version */ + list_del_rcu(&primary_orig_node->bond.selected); + list_add_rcu(&primary_orig_node->bond.selected, + &router->bonding_list); + spin_unlock_bh(&primary_orig_node->neigh_list_lock); } else { /* if bonding is disabled, use the best of the * remaining candidates which are not using * this interface. */ - best_router = first_candidate; + best_router_tq = 0; + list_for_each_entry_rcu(tmp_neigh_node, + &primary_orig_node->bond.selected, bonding_list) { + if (!first_candidate) + first_candidate = tmp_neigh_node; - do { /* recv_if == NULL on the first node. */ - if ((router->if_incoming != recv_if) && - (router->tq_avg > best_router->tq_avg)) - best_router = router; + if (tmp_neigh_node->if_incoming != recv_if) + /* if we don't have a router yet + * or this one is better, choose it. */ + if ((!router) || + (tmp_neigh_node->tq_avg > router->tq_avg)) { + router = tmp_neigh_node; + best_router_tq = 0; + } + } - router = router->next_bond_candidate; - } while (router != first_candidate); - - router = best_router; + /* use the first candidate if nothing was found. */ + if (!router) + router = first_candidate; } - +return_router: + kref_get(&router->refcount); + rcu_read_unlock(); return router; } @@ -1210,7 +1230,7 @@ static int check_unicast_packet(struct sk_buff *skb, int hdr_size) { struct ethhdr *ethhdr; - /* drop packet if it has not necessary minimum size */ +/* drop packet if it has not necessary minimum size */ if (unlikely(!pskb_may_pull(skb, hdr_size))) return -1; @@ -1262,13 +1282,13 @@ int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if, goto unlock; kref_get(&orig_node->refcount); + rcu_read_unlock(); + + /* find_router() increases neigh_nodes refcount if found. */ neigh_node = find_router(bat_priv, orig_node, recv_if); if (!neigh_node) - goto unlock; - - kref_get(&neigh_node->refcount); - rcu_read_unlock(); + goto out; /* create a copy of the skb, if needed, to modify it. */ if (skb_cow(skb, sizeof(struct ethhdr)) < 0) diff --git a/batman-adv/types.h b/batman-adv/types.h index 52b6b08..8264050 100644 --- a/batman-adv/types.h +++ b/batman-adv/types.h @@ -92,7 +92,7 @@ struct orig_node { spinlock_t ogm_cnt_lock; /* protects ogm counter */ struct { uint8_t candidates; - struct neigh_node *selected; + struct list_head selected; } bond; }; @@ -116,7 +116,7 @@ struct neigh_node { uint8_t tq_index; uint8_t tq_avg; uint8_t last_ttl; - struct neigh_node *next_bond_candidate; + struct list_head bonding_list; unsigned long last_valid; unsigned long real_bits[NUM_WORDS]; struct kref refcount; diff --git a/batman-adv/unicast.c b/batman-adv/unicast.c index 67bed2d..cfe08dd 100644 --- a/batman-adv/unicast.c +++ b/batman-adv/unicast.c @@ -314,14 +314,11 @@ trans_search: orig_node = transtable_search(bat_priv, ethhdr->h_dest); find_router: - rcu_read_lock(); + /* find_router() increases neigh_nodes refcount if found. */ neigh_node = find_router(bat_priv, orig_node, NULL); if (!neigh_node) - goto unlock; - - kref_get(&neigh_node->refcount); - rcu_read_unlock(); + goto out; if (neigh_node->if_incoming->if_status != IF_ACTIVE) goto out; @@ -353,8 +350,6 @@ find_router: ret = 0; goto out; -unlock: - rcu_read_unlock(); out: if (neigh_node) kref_put(&neigh_node->refcount, neigh_node_free_ref);