batman-adv: Make orig_node->router an rcu protected pointer
PATCH 1/2 batman-adv: Make orig_node->router an rcu protected pointer
The rcu protected macros rcu_dereference() and rcu_assign_pointer() for the orig_node->router need to be used, as well as spin/rcu locking.
Otherwise we might end up using a router pointer pointing to already freed memory.
In update_orig() the functions update_route_rcu() (former update_route()) and update_HNA() are now called directly instead of update_routes() in both cases. This made keeping the rcu-locking neat easier update_routes() would have called the according functions at these positions always anyway.
Also two small code blocks in receive_bat_packet() were swapped to keep the time for rcu-locking low.
--- gateway_client.c | 39 +++++++++++-------- icmp_socket.c | 2 +- originator.c | 6 +- routing.c | 110 ++++++++++++++++++++++++++++++++---------------------- types.h | 4 +- 5 files changed, 93 insertions(+), 68 deletions(-)
gateway_client.c +22 -17
@@ -97,6 +97,7 @@ void gw_election(struct bat_priv *bat_priv) {struct hlist_node *node;struct gw_node *gw_node, *curr_gw, *curr_gw_tmp =NULL ; +struct neigh_node *router;uint8_t max_tq =0 ;uint32_t max_gw_factor =0 , tmp_gw_factor =0 ;int down, up;
@@ -132,7 +133,8 @@ void gw_election(struct bat_priv *bat_priv) }hlist_for_each_entry_rcu (gw_node, node, &bat_priv->gw_list, list) { -if (!gw_node->orig_node->router) + router =rcu_dereference (gw_node->orig_node->router); +if (!router)continue ;if (gw_node->deleted)
@@ -143,15 +145,14 @@ void gw_election(struct bat_priv *bat_priv)gw_bandwidth_to_kbit (gw_node->orig_node->gw_flags, &down, &up); - tmp_gw_factor = (gw_node->orig_node->router->tq_avg * - gw_node->orig_node->router->tq_avg * + tmp_gw_factor = (router->tq_avg * router->tq_avg * down *100 *100 ) / (TQ_LOCAL_WINDOW_SIZE *TQ_LOCAL_WINDOW_SIZE *64 );if ((tmp_gw_factor > max_gw_factor) || ((tmp_gw_factor == max_gw_factor) && - (gw_node->orig_node->router->tq_avg > max_tq))) + (router->tq_avg > max_tq))) curr_gw_tmp = gw_node;break ;
@@ -163,19 +164,20 @@ void gw_election(struct bat_priv *bat_priv)* soon as a better gateway appears which has * $routing_class more tq points) **/ -if (gw_node->orig_node->router->tq_avg > max_tq) +if (router->tq_avg > max_tq) curr_gw_tmp = gw_node;break ; } -if (gw_node->orig_node->router->tq_avg > max_tq) - max_tq = gw_node->orig_node->router->tq_avg; +if (router->tq_avg > max_tq) + max_tq = router->tq_avg;if (tmp_gw_factor > max_gw_factor) max_gw_factor = tmp_gw_factor; }if (curr_gw != curr_gw_tmp) { + router =rcu_dereference (curr_gw_tmp->orig_node->router);if ((curr_gw) && (!curr_gw_tmp))bat_dbg (DBG_BATMAN , bat_priv, "Removing selected gateway - "
@@ -186,14 +188,14 @@ void gw_election(struct bat_priv *bat_priv)"(gw_flags: %i, tq: %i)\n" , curr_gw_tmp->orig_node->orig, curr_gw_tmp->orig_node->gw_flags, - curr_gw_tmp->orig_node->router->tq_avg); + router->tq_avg); else bat_dbg(DBG_BATMAN, bat_priv,"Changing route to gateway %pM " "(gw_flags: %i, tq: %i)\n" , curr_gw_tmp->orig_node->orig, curr_gw_tmp->orig_node->gw_flags, - curr_gw_tmp->orig_node->router->tq_avg); + router->tq_avg);gw_select (bat_priv, curr_gw_tmp); }
@@ -204,6 +206,7 @@ void gw_election(struct bat_priv *bat_priv)void gw_check_election (struct bat_priv *bat_priv,struct orig_node *orig_node) {struct gw_node *curr_gateway_tmp; +struct neigh_node *router;uint8_t gw_tq_avg, orig_tq_avg;rcu_read_lock ();
@@ -214,20 +217,21 @@ void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)if (!curr_gateway_tmp->orig_node) goto deselect_rcu; -if (!curr_gateway_tmp->orig_node->router) + router =rcu_dereference (curr_gateway_tmp->orig_node->router); +if (!router) goto deselect_rcu;/* this node already is the gateway */ if (curr_gateway_tmp->orig_node == orig_node) goto out_rcu; -if (!orig_node->router) +if (!rcu_dereference (orig_node->router)) goto out_rcu; - gw_tq_avg = curr_gateway_tmp->orig_node->router->tq_avg; + gw_tq_avg = router->tq_avg; + orig_tq_avg =rcu_dereference (orig_node->router)->tq_avg;rcu_read_unlock (); - orig_tq_avg = orig_node->router->tq_avg;/* the TQ value has to be better */ if (orig_tq_avg < gw_tq_avg)
@@ -365,19 +369,20 @@ static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq, struct gw_node *gw_node) { struct gw_node *curr_gw; +struct neigh_node *router;int down, up, ret;gw_bandwidth_to_kbit (gw_node->orig_node->gw_flags, &down, &up);rcu_read_lock (); curr_gw =rcu_dereference (bat_priv->curr_gw); + router =rcu_dereference (gw_node->orig_node->router); ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n", (curr_gw == gw_node ? "=>" : " "), gw_node->orig_node->orig, - gw_node->orig_node->router->tq_avg, - gw_node->orig_node->router->addr, - gw_node->orig_node->router->if_incoming->net_dev->name, + router->tq_avg, router->addr, + router->if_incoming->net_dev->name, gw_node->orig_node->gw_flags, (down > 2048 ? down / 1024 : down), (down > 2048 ? "MBit" : "KBit"),
@@ -422,7 +427,7 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)if (gw_node->deleted)continue ; -if (!gw_node->orig_node->router) +if (!rcu_dereference (gw_node->orig_node->router))continue ;_write_buffer_text (bat_priv, seq, gw_node);
icmp_socket.c +1 -1
@@ -224,7 +224,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,if (!orig_node) goto unlock; - neigh_node = orig_node->router; + neigh_node =rcu_dereference (orig_node->router);if (!neigh_node) goto unlock;
originator.c +3 -3
@@ -421,10 +421,11 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)rcu_read_lock ();hlist_for_each_entry_rcu (orig_node, node, head, hash_entry) { -if (!orig_node->router) + neigh_node =rcu_dereference (orig_node->router); +if (!neigh_node)continue ; -if (orig_node->router->tq_avg ==0 ) +if (neigh_node->tq_avg ==0 )continue ; last_seen_secs = jiffies_to_msecs(jiffies -
@@ -432,7 +433,6 @@ int orig_seq_print_text(struct seq_file *seq, void *offset) last_seen_msecs =jiffies_to_msecs (jiffies - orig_node->last_valid) %1000 ; - neigh_node = orig_node->router; seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", orig_node->orig, last_seen_secs, last_seen_msecs, neigh_node->tq_avg,
routing.c +65 -45
228 lines not on this page · raw message
types.h +2 -2
@@ -67,7 +67,7 @@ struct hard_iface {struct orig_node {uint8_t orig[ETH_ALEN ];uint8_t primary_addr[ETH_ALEN ]; -struct neigh_node *router; +struct neigh_node __rcu *router;/* rcu protected pointer */ unsigned long *bcast_own;uint8_t *bcast_own_sum;unsigned long last_valid;
@@ -83,7 +83,7 @@ struct orig_node {uint32_t last_bcast_seqno;struct hlist_head neigh_list;struct list_head frag_list; -spinlock_t neigh_list_lock;/* protects neighbor list */ +spinlock_t neigh_list_lock;/* protects neigh_list and router */ atomic_t refcount;struct rcu_head rcu;struct hlist_node hash_entry;
Signature · 1 line
1.7.4.1
PATCH 2/2 batman-adv: Protect global TQ value and window with a spinlock
--- originator.c | 1 + routing.c | 4 ++++ types.h | 1 + 3 files changed, 6 insertions(+), 0 deletions(-)
originator.c +1 -0
@@ -87,6 +87,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,INIT_HLIST_NODE (&neigh_node->list);INIT_LIST_HEAD (&neigh_node->bonding_list); +spin_lock_init (&neigh_node->tq_lock);memcpy (neigh_node->addr, neigh,ETH_ALEN ); neigh_node->orig_node = orig_neigh_node;
routing.c +4 -0
@@ -408,10 +408,12 @@ static void update_orig(struct bat_priv *bat_priv,if (is_duplicate)continue ; +spin_lock_bh (&tmp_neigh_node->tq_lock);ring_buffer_set (tmp_neigh_node->tq_recv, &tmp_neigh_node->tq_index,0 ); tmp_neigh_node->tq_avg =ring_buffer_avg (tmp_neigh_node->tq_recv); +spin_unlock_bh (&tmp_neigh_node->tq_lock); }if (!neigh_node) {
@@ -436,10 +438,12 @@ static void update_orig(struct bat_priv *bat_priv, orig_node->flags = batman_packet->flags; neigh_node->last_valid = jiffies; +spin_lock_bh (&neigh_node->tq_lock);ring_buffer_set (neigh_node->tq_recv, &neigh_node->tq_index, batman_packet->tq); neigh_node->tq_avg =ring_buffer_avg (neigh_node->tq_recv); +spin_unlock_bh (&neigh_node->tq_lock);if (!is_duplicate) { orig_node->last_ttl = batman_packet->ttl;
types.h +1 -0
@@ -125,6 +125,7 @@ struct neigh_node {struct rcu_head rcu;struct orig_node *orig_node;struct hard_iface *if_incoming; +spinlock_t tq_lock;/* protects: tq_recv, tq_index, tq_avg */ };
Signature · 1 line
1.7.4.1
[B.A.T.M.A.N.] Locking fixes v2
In reply to Linus Lüssingchanges:
* added one missing rcu_read_unlock() in bonding_candidate_add() [1/2]
* removed tq_avg from spinlock comment [2/2]
* added [a,b]/batman-adv/ patch prefixes
Cheers, Linus
PATCH 1/2 batman-adv: Make orig_node->router an rcu protected pointer
The rcu protected macros rcu_dereference() and rcu_assign_pointer() for the orig_node->router need to be used, as well as spin/rcu locking.
Otherwise we might end up using a router pointer pointing to already freed memory.
In update_orig() the functions update_route_rcu() (former update_route()) and update_HNA() are now called directly instead of update_routes() in both cases. This made keeping the rcu-locking neat easier update_routes() would have called the according functions at these positions always anyway.
Also two small code blocks in receive_bat_packet() were swapped to keep the time for rcu-locking low.
--- gateway_client.c | 39 +++++++++++-------- icmp_socket.c | 2 +- originator.c | 6 +- routing.c | 112 ++++++++++++++++++++++++++++++++---------------------- types.h | 4 +- 5 files changed, 95 insertions(+), 68 deletions(-)
batman-adv/gateway_client.c +22 -17
@@ -97,6 +97,7 @@ void gw_election(struct bat_priv *bat_priv) {struct hlist_node *node;struct gw_node *gw_node, *curr_gw, *curr_gw_tmp =NULL ; +struct neigh_node *router;uint8_t max_tq =0 ;uint32_t max_gw_factor =0 , tmp_gw_factor =0 ;int down, up;
@@ -132,7 +133,8 @@ void gw_election(struct bat_priv *bat_priv) }hlist_for_each_entry_rcu (gw_node, node, &bat_priv->gw_list, list) { -if (!gw_node->orig_node->router) + router =rcu_dereference (gw_node->orig_node->router); +if (!router)continue ;if (gw_node->deleted)
@@ -143,15 +145,14 @@ void gw_election(struct bat_priv *bat_priv)gw_bandwidth_to_kbit (gw_node->orig_node->gw_flags, &down, &up); - tmp_gw_factor = (gw_node->orig_node->router->tq_avg * - gw_node->orig_node->router->tq_avg * + tmp_gw_factor = (router->tq_avg * router->tq_avg * down *100 *100 ) / (TQ_LOCAL_WINDOW_SIZE *TQ_LOCAL_WINDOW_SIZE *64 );if ((tmp_gw_factor > max_gw_factor) || ((tmp_gw_factor == max_gw_factor) && - (gw_node->orig_node->router->tq_avg > max_tq))) + (router->tq_avg > max_tq))) curr_gw_tmp = gw_node;break ;
@@ -163,19 +164,20 @@ void gw_election(struct bat_priv *bat_priv)* soon as a better gateway appears which has * $routing_class more tq points) **/ -if (gw_node->orig_node->router->tq_avg > max_tq) +if (router->tq_avg > max_tq) curr_gw_tmp = gw_node;break ; } -if (gw_node->orig_node->router->tq_avg > max_tq) - max_tq = gw_node->orig_node->router->tq_avg; +if (router->tq_avg > max_tq) + max_tq = router->tq_avg;if (tmp_gw_factor > max_gw_factor) max_gw_factor = tmp_gw_factor; }if (curr_gw != curr_gw_tmp) { + router =rcu_dereference (curr_gw_tmp->orig_node->router);if ((curr_gw) && (!curr_gw_tmp))bat_dbg (DBG_BATMAN , bat_priv, "Removing selected gateway - "
@@ -186,14 +188,14 @@ void gw_election(struct bat_priv *bat_priv)"(gw_flags: %i, tq: %i)\n" , curr_gw_tmp->orig_node->orig, curr_gw_tmp->orig_node->gw_flags, - curr_gw_tmp->orig_node->router->tq_avg); + router->tq_avg); else bat_dbg(DBG_BATMAN, bat_priv,"Changing route to gateway %pM " "(gw_flags: %i, tq: %i)\n" , curr_gw_tmp->orig_node->orig, curr_gw_tmp->orig_node->gw_flags, - curr_gw_tmp->orig_node->router->tq_avg); + router->tq_avg);gw_select (bat_priv, curr_gw_tmp); }
@@ -204,6 +206,7 @@ void gw_election(struct bat_priv *bat_priv)void gw_check_election (struct bat_priv *bat_priv,struct orig_node *orig_node) {struct gw_node *curr_gateway_tmp; +struct neigh_node *router;uint8_t gw_tq_avg, orig_tq_avg;rcu_read_lock ();
@@ -214,20 +217,21 @@ void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)if (!curr_gateway_tmp->orig_node) goto deselect_rcu; -if (!curr_gateway_tmp->orig_node->router) + router =rcu_dereference (curr_gateway_tmp->orig_node->router); +if (!router) goto deselect_rcu;/* this node already is the gateway */ if (curr_gateway_tmp->orig_node == orig_node) goto out_rcu; -if (!orig_node->router) +if (!rcu_dereference (orig_node->router)) goto out_rcu; - gw_tq_avg = curr_gateway_tmp->orig_node->router->tq_avg; + gw_tq_avg = router->tq_avg; + orig_tq_avg =rcu_dereference (orig_node->router)->tq_avg;rcu_read_unlock (); - orig_tq_avg = orig_node->router->tq_avg;/* the TQ value has to be better */ if (orig_tq_avg < gw_tq_avg)
@@ -365,19 +369,20 @@ static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq, struct gw_node *gw_node) { struct gw_node *curr_gw; +struct neigh_node *router;int down, up, ret;gw_bandwidth_to_kbit (gw_node->orig_node->gw_flags, &down, &up);rcu_read_lock (); curr_gw =rcu_dereference (bat_priv->curr_gw); + router =rcu_dereference (gw_node->orig_node->router); ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n", (curr_gw == gw_node ? "=>" : " "), gw_node->orig_node->orig, - gw_node->orig_node->router->tq_avg, - gw_node->orig_node->router->addr, - gw_node->orig_node->router->if_incoming->net_dev->name, + router->tq_avg, router->addr, + router->if_incoming->net_dev->name, gw_node->orig_node->gw_flags, (down > 2048 ? down / 1024 : down), (down > 2048 ? "MBit" : "KBit"),
@@ -422,7 +427,7 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)if (gw_node->deleted)continue ; -if (!gw_node->orig_node->router) +if (!rcu_dereference (gw_node->orig_node->router))continue ;_write_buffer_text (bat_priv, seq, gw_node);
batman-adv/icmp_socket.c +1 -1
@@ -224,7 +224,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,if (!orig_node) goto unlock; - neigh_node = orig_node->router; + neigh_node =rcu_dereference (orig_node->router);if (!neigh_node) goto unlock;
batman-adv/originator.c +3 -3
@@ -421,10 +421,11 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)rcu_read_lock ();hlist_for_each_entry_rcu (orig_node, node, head, hash_entry) { -if (!orig_node->router) + neigh_node =rcu_dereference (orig_node->router); +if (!neigh_node)continue ; -if (orig_node->router->tq_avg ==0 ) +if (neigh_node->tq_avg ==0 )continue ; last_seen_secs = jiffies_to_msecs(jiffies -
@@ -432,7 +433,6 @@ int orig_seq_print_text(struct seq_file *seq, void *offset) last_seen_msecs =jiffies_to_msecs (jiffies - orig_node->last_valid) %1000 ; - neigh_node = orig_node->router; seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", orig_node->orig, last_seen_secs, last_seen_msecs, neigh_node->tq_avg,
batman-adv/routing.c +67 -45
231 lines not on this page · raw message
batman-adv/types.h +2 -2
@@ -67,7 +67,7 @@ struct hard_iface {struct orig_node {uint8_t orig[ETH_ALEN ];uint8_t primary_addr[ETH_ALEN ]; -struct neigh_node *router; +struct neigh_node __rcu *router;/* rcu protected pointer */ unsigned long *bcast_own;uint8_t *bcast_own_sum;unsigned long last_valid;
@@ -83,7 +83,7 @@ struct orig_node {uint32_t last_bcast_seqno;struct hlist_head neigh_list;struct list_head frag_list; -spinlock_t neigh_list_lock;/* protects neighbor list */ +spinlock_t neigh_list_lock;/* protects neigh_list and router */ atomic_t refcount;struct rcu_head rcu;struct hlist_node hash_entry;
Signature · 1 line
1.7.4.1
PATCH 2/2 batman-adv: Protect global TQ value and window with a spinlock
In reply to Linus Lüssing--- originator.c | 1 + routing.c | 4 ++++ types.h | 1 + 3 files changed, 6 insertions(+), 0 deletions(-)
batman-adv/originator.c +1 -0
@@ -87,6 +87,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,INIT_HLIST_NODE (&neigh_node->list);INIT_LIST_HEAD (&neigh_node->bonding_list); +spin_lock_init (&neigh_node->tq_lock);memcpy (neigh_node->addr, neigh,ETH_ALEN ); neigh_node->orig_node = orig_neigh_node;
batman-adv/routing.c +4 -0
@@ -410,10 +410,12 @@ static void update_orig(struct bat_priv *bat_priv,if (is_duplicate)continue ; +spin_lock_bh (&tmp_neigh_node->tq_lock);ring_buffer_set (tmp_neigh_node->tq_recv, &tmp_neigh_node->tq_index,0 ); tmp_neigh_node->tq_avg =ring_buffer_avg (tmp_neigh_node->tq_recv); +spin_unlock_bh (&tmp_neigh_node->tq_lock); }if (!neigh_node) {
@@ -438,10 +440,12 @@ static void update_orig(struct bat_priv *bat_priv, orig_node->flags = batman_packet->flags; neigh_node->last_valid = jiffies; +spin_lock_bh (&neigh_node->tq_lock);ring_buffer_set (neigh_node->tq_recv, &neigh_node->tq_index, batman_packet->tq); neigh_node->tq_avg =ring_buffer_avg (neigh_node->tq_recv); +spin_unlock_bh (&neigh_node->tq_lock);if (!is_duplicate) { orig_node->last_ttl = batman_packet->ttl;
batman-adv/types.h +1 -0
@@ -125,6 +125,7 @@ struct neigh_node {struct rcu_head rcu;struct orig_node *orig_node;struct hard_iface *if_incoming; +spinlock_t tq_lock;/* protects: tq_recv, tq_index */ };
Signature · 1 line
1.7.4.1