Skip to contentSkip to conversation

(no subject)

B.A.T.M.A.N. New Marek Lindner 1 participant 6 messages Sep 30, 2010 – Oct 9, 2010

lore
Share & tools
lore
#0 lore raw

Hi,

since the vis code and the bridge loop avoidance code already use the kref library functions, I converted the remaining refcounting code to use kref as well.

Regards,
Marek

Marek Lindner (4):
batman-adv: convert gw_node custom refcounting to kref functions
batman-adv: use rcu callbacks when freeing gw_nodes
batman-adv: convert batman_if custom refcounting to kref functions
batman-adv: use rcu callbacks when freeing batman_if

 batman-adv/bat_sysfs.c      |   12 ++++++------
 batman-adv/gateway_client.c |   30 ++++++++++++++++--------------
 batman-adv/hard-interface.c |   41 +++++++++++++++++++++++------------------
 batman-adv/hard-interface.h |   13 ++++---------
 batman-adv/types.h          |    6 ++++--
 5 files changed, 53 insertions(+), 49 deletions(-)
#1 lore raw

PATCH 1/4 batman-adv: convert gw_node custom refcounting to kref functions

---
 batman-adv/gateway_client.c |   24 ++++++++++--------------
 batman-adv/types.h          |    2 +-
 2 files changed, 11 insertions(+), 15 deletions(-)
batman-adv/gateway_client.c +10 -14
index e1264ba..d275560 100644
@@ -28,15 +28,12 @@
 #include <linux/udp.h>
 #include <linux/if_vlan.h>
 
-static void gw_node_hold(struct gw_node *gw_node)
+static void gw_node_free_ref(struct kref *refcount)
 {
-	atomic_inc(&gw_node->refcnt);
-}
+	struct gw_node *gw_node;
 
-static void gw_node_put(struct gw_node *gw_node)
-{
-	if (atomic_dec_and_test(&gw_node->refcnt))
-		kfree(gw_node);
+	gw_node = container_of(refcount, struct gw_node, refcount);
+	kfree(gw_node);
 }
 
 void *gw_get_selected(struct bat_priv *bat_priv)
@@ -56,7 +53,7 @@ void gw_deselect(struct bat_priv *bat_priv)
 	bat_priv->curr_gw = NULL;
 
 	if (gw_node)
-		gw_node_put(gw_node);
+		kref_put(&gw_node->refcount, gw_node_free_ref);
 }
 
 static struct gw_node *gw_select(struct bat_priv *bat_priv,
@@ -65,7 +62,7 @@ static struct gw_node *gw_select(struct bat_priv *bat_priv,
 	struct gw_node *curr_gw_node = bat_priv->curr_gw;
 
 	if (new_gw_node)
-		gw_node_hold(new_gw_node);
+		kref_get(&new_gw_node->refcount);
 
 	bat_priv->curr_gw = new_gw_node;
 	return curr_gw_node;
@@ -176,7 +173,7 @@ void gw_election(struct bat_priv *bat_priv)
 
 	/* the kfree() has to be outside of the rcu lock */
 	if (old_gw_node)
-		gw_node_put(old_gw_node);
+		kref_put(&old_gw_node->refcount, gw_node_free_ref);
 }
 
 void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
@@ -238,8 +235,7 @@ static void gw_node_add(struct bat_priv *bat_priv,
 	memset(gw_node, 0, sizeof(struct gw_node));
 	INIT_HLIST_NODE(&gw_node->list);
 	gw_node->orig_node = orig_node;
-	atomic_set(&gw_node->refcnt, 0);
-	gw_node_hold(gw_node);
+	kref_init(&gw_node->refcount);
 
 	spin_lock_irqsave(&bat_priv->gw_list_lock, flags);
 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
@@ -319,7 +315,7 @@ void gw_node_purge_deleted(struct bat_priv *bat_priv)
 
 			hlist_del_rcu(&gw_node->list);
 			synchronize_rcu();
-			gw_node_put(gw_node);
+			kref_put(&gw_node->refcount, gw_node_free_ref);
 		}
 	}
 
@@ -338,7 +334,7 @@ void gw_node_list_free(struct bat_priv *bat_priv)
 				 &bat_priv->gw_list, list) {
 		hlist_del_rcu(&gw_node->list);
 		synchronize_rcu();
-		gw_node_put(gw_node);
+		kref_put(&gw_node->refcount, gw_node_free_ref);
 	}
 
 	gw_deselect(bat_priv);
batman-adv/types.h +1 -1
index a609100..f5d29e6 100644
@@ -96,7 +96,7 @@ struct gw_node {
 	struct hlist_node list;
 	struct orig_node *orig_node;
 	unsigned long deleted;
-	atomic_t refcnt;
+	struct kref refcount;
 };
 
 /**
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/4 batman-adv: use rcu callbacks when freeing gw_nodes

In reply to Marek Lindner
---
 batman-adv/gateway_client.c |   14 ++++++++++----
 batman-adv/types.h          |    1 +
 2 files changed, 11 insertions(+), 4 deletions(-)
batman-adv/gateway_client.c +10 -4
index d275560..ae1ab1e 100644
@@ -36,6 +36,14 @@ static void gw_node_free_ref(struct kref *refcount)
 	kfree(gw_node);
 }
 
+static void gw_node_free_rcu(struct rcu_head *rcu)
+{
+	struct gw_node *gw_node;
+
+	gw_node = container_of(rcu, struct gw_node, rcu);
+	kref_put(&gw_node->refcount, gw_node_free_ref);
+}
+
 void *gw_get_selected(struct bat_priv *bat_priv)
 {
 	struct gw_node *curr_gateway_tmp = bat_priv->curr_gw;
@@ -314,8 +322,7 @@ void gw_node_purge_deleted(struct bat_priv *bat_priv)
 		    (time_after(jiffies, gw_node->deleted + timeout))) {
 
 			hlist_del_rcu(&gw_node->list);
-			synchronize_rcu();
-			kref_put(&gw_node->refcount, gw_node_free_ref);
+			call_rcu(&gw_node->rcu, gw_node_free_rcu);
 		}
 	}
 
@@ -333,8 +340,7 @@ void gw_node_list_free(struct bat_priv *bat_priv)
 	hlist_for_each_entry_safe(gw_node, node, node_tmp,
 				 &bat_priv->gw_list, list) {
 		hlist_del_rcu(&gw_node->list);
-		synchronize_rcu();
-		kref_put(&gw_node->refcount, gw_node_free_ref);
+		call_rcu(&gw_node->rcu, gw_node_free_rcu);
 	}
 
 	gw_deselect(bat_priv);
batman-adv/types.h +1 -0
index f5d29e6..11f1017 100644
@@ -97,6 +97,7 @@ struct gw_node {
 	struct orig_node *orig_node;
 	unsigned long deleted;
 	struct kref refcount;
+	struct rcu_head rcu;
 };
 
 /**
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/4 batman-adv: convert batman_if custom refcounting to kref functions

In reply to Marek Lindner
---
 batman-adv/bat_sysfs.c      |   12 ++++++------
 batman-adv/hard-interface.c |   30 ++++++++++++++----------------
 batman-adv/hard-interface.h |   13 ++++---------
 batman-adv/types.h          |    2 +-
 4 files changed, 25 insertions(+), 32 deletions(-)
batman-adv/bat_sysfs.c +6 -6
index 9ab2bfe..3f551f3 100644
@@ -461,7 +461,7 @@ static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
 	length = sprintf(buff, "%s\n", batman_if->if_status == IF_NOT_IN_USE ?
 			 "none" : batman_if->soft_iface->name);
 
-	hardif_put(batman_if);
+	kref_put(&batman_if->refcount, hardif_free_ref);
 
 	return length;
 }
@@ -484,7 +484,7 @@ static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
 	if (strlen(buff) >= IFNAMSIZ) {
 		pr_err("Invalid parameter for 'mesh_iface' setting received: "
 		       "interface name too long '%s'\n", buff);
-		hardif_put(batman_if);
+		kref_put(&batman_if->refcount, hardif_free_ref);
 		return -EINVAL;
 	}
 
@@ -495,7 +495,7 @@ static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
 
 	if ((batman_if->if_status == status_tmp) || ((batman_if->soft_iface) &&
 	    (strncmp(batman_if->soft_iface->name, buff, IFNAMSIZ) == 0))) {
-		hardif_put(batman_if);
+		kref_put(&batman_if->refcount, hardif_free_ref);
 		return count;
 	}
 
@@ -503,7 +503,7 @@ static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
 		rtnl_lock();
 		hardif_disable_interface(batman_if);
 		rtnl_unlock();
-		hardif_put(batman_if);
+		kref_put(&batman_if->refcount, hardif_free_ref);
 		return count;
 	}
 
@@ -515,7 +515,7 @@ static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
 	}
 
 	ret = hardif_enable_interface(batman_if, buff);
-	hardif_put(batman_if);
+	kref_put(&batman_if->refcount, hardif_free_ref);
 
 	return ret;
 }
@@ -550,7 +550,7 @@ static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
 		break;
 	}
 
-	hardif_put(batman_if);
+	kref_put(&batman_if->refcount, hardif_free_ref);
 
 	return length;
 }
batman-adv/hard-interface.c +14 -16
index def74cf..87693db 100644
@@ -52,7 +52,7 @@ struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
 
 out:
 	if (batman_if)
-		hardif_hold(batman_if);
+		kref_get(&batman_if->refcount);
 
 	rcu_read_unlock();
 	return batman_if;
@@ -102,7 +102,7 @@ static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
 
 out:
 	if (batman_if)
-		hardif_hold(batman_if);
+		kref_get(&batman_if->refcount);
 
 	rcu_read_unlock();
 	return batman_if;
@@ -127,13 +127,13 @@ static void set_primary_if(struct bat_priv *bat_priv,
 	struct batman_if *old_if;
 
 	if (batman_if)
-		hardif_hold(batman_if);
+		kref_get(&batman_if->refcount);
 
 	old_if = bat_priv->primary_if;
 	bat_priv->primary_if = batman_if;
 
 	if (old_if)
-		hardif_put(old_if);
+		kref_put(&old_if->refcount, hardif_free_ref);
 
 	if (!bat_priv->primary_if)
 		return;
@@ -314,7 +314,7 @@ int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
 	batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
 	batman_if->batman_adv_ptype.func = batman_skb_recv;
 	batman_if->batman_adv_ptype.dev = batman_if->net_dev;
-	hardif_hold(batman_if);
+	kref_get(&batman_if->refcount);
 	dev_add_pack(&batman_if->batman_adv_ptype);
 
 	atomic_set(&batman_if->seqno, 1);
@@ -373,7 +373,7 @@ void hardif_disable_interface(struct batman_if *batman_if)
 	bat_info(batman_if->soft_iface, "Removing interface: %s\n",
 		 batman_if->net_dev->name);
 	dev_remove_pack(&batman_if->batman_adv_ptype);
-	hardif_put(batman_if);
+	kref_put(&batman_if->refcount, hardif_free_ref);
 
 	bat_priv->num_ifaces--;
 	orig_hash_del_if(batman_if, bat_priv->num_ifaces);
@@ -385,7 +385,7 @@ void hardif_disable_interface(struct batman_if *batman_if)
 		set_primary_if(bat_priv, new_if);
 
 		if (new_if)
-			hardif_put(new_if);
+			kref_put(&new_if->refcount, hardif_free_ref);
 	}
 
 	kfree(batman_if->packet_buff);
@@ -431,8 +431,7 @@ static struct batman_if *hardif_add_interface(struct net_device *net_dev)
 	batman_if->soft_iface = NULL;
 	batman_if->if_status = IF_NOT_IN_USE;
 	INIT_LIST_HEAD(&batman_if->list);
-	atomic_set(&batman_if->refcnt, 0);
-	hardif_hold(batman_if);
+	kref_init(&batman_if->refcount);
 
 	check_known_mac_addr(batman_if->net_dev->dev_addr);
 
@@ -441,7 +440,7 @@ static struct batman_if *hardif_add_interface(struct net_device *net_dev)
 	spin_unlock(&if_list_lock);
 
 	/* extra reference for return */
-	hardif_hold(batman_if);
+	kref_get(&batman_if->refcount);
 	return batman_if;
 
 free_if:
@@ -467,7 +466,7 @@ static void hardif_remove_interface(struct batman_if *batman_if)
 	list_del_rcu(&batman_if->list);
 	synchronize_rcu();
 	sysfs_del_hardif(&batman_if->hardif_obj);
-	hardif_put(batman_if);
+	kref_put(&batman_if->refcount, hardif_free_ref);
 }
 
 void hardif_remove_interfaces(void)
@@ -514,10 +513,8 @@ static int hard_if_event(struct notifier_block *this,
 			update_min_mtu(batman_if->soft_iface);
 		break;
 	case NETDEV_CHANGEADDR:
-		if (batman_if->if_status == IF_NOT_IN_USE) {
-			hardif_put(batman_if);
-			goto out;
-		}
+		if (batman_if->if_status == IF_NOT_IN_USE)
+			goto hardif_put;
 
 		check_known_mac_addr(batman_if->net_dev->dev_addr);
 		update_mac_addresses(batman_if);
@@ -529,8 +526,9 @@ static int hard_if_event(struct notifier_block *this,
 	default:
 		break;
 	};
-	hardif_put(batman_if);
 
+hardif_put:
+	kref_put(&batman_if->refcount, hardif_free_ref);
 out:
 	return NOTIFY_DONE;
 }
batman-adv/hard-interface.h +4 -9
index d550889..30ec3b8 100644
@@ -42,17 +42,12 @@ int batman_skb_recv(struct sk_buff *skb,
 int hardif_min_mtu(struct net_device *soft_iface);
 void update_min_mtu(struct net_device *soft_iface);
 
-static inline void hardif_hold(struct batman_if *batman_if)
+static inline void hardif_free_ref(struct kref *refcount)
 {
-	atomic_inc(&batman_if->refcnt);
-}
+	struct batman_if *batman_if;
 
-static inline void hardif_put(struct batman_if *batman_if)
-{
-	if (atomic_dec_and_test(&batman_if->refcnt)) {
-		dev_put(batman_if->net_dev);
-		kfree(batman_if);
-	}
+	batman_if = container_of(refcount, struct batman_if, refcount);
+	kfree(batman_if);
 }
 
 #endif /* _NET_BATMAN_ADV_HARD_INTERFACE_H_ */
batman-adv/types.h +1 -1
index 11f1017..585647b 100644
@@ -44,7 +44,7 @@ struct batman_if {
 	unsigned char *packet_buff;
 	int packet_len;
 	struct kobject *hardif_obj;
-	atomic_t refcnt;
+	struct kref refcount;
 	struct packet_type batman_adv_ptype;
 	struct net_device *soft_iface;
 };
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/4 batman-adv: use rcu callbacks when freeing batman_if

In reply to Marek Lindner
---
 batman-adv/hard-interface.c |   13 ++++++++++---
 batman-adv/types.h          |    1 +
 2 files changed, 11 insertions(+), 3 deletions(-)
batman-adv/hard-interface.c +10 -3
index 87693db..ec16dc9 100644
@@ -38,6 +38,15 @@
 /* protect update critical side of if_list - but not the content */
 static DEFINE_SPINLOCK(if_list_lock);
 
+static void hardif_free_rcu(struct rcu_head *rcu)
+{
+	struct batman_if *batman_if;
+
+	batman_if = container_of(rcu, struct batman_if, rcu);
+	sysfs_del_hardif(&batman_if->hardif_obj);
+	kref_put(&batman_if->refcount, hardif_free_ref);
+}
+
 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
 {
 	struct batman_if *batman_if;
@@ -464,9 +473,7 @@ static void hardif_remove_interface(struct batman_if *batman_if)
 
 	/* caller must take if_list_lock */
 	list_del_rcu(&batman_if->list);
-	synchronize_rcu();
-	sysfs_del_hardif(&batman_if->hardif_obj);
-	kref_put(&batman_if->refcount, hardif_free_ref);
+	call_rcu(&batman_if->rcu, hardif_free_rcu);
 }
 
 void hardif_remove_interfaces(void)
batman-adv/types.h +1 -0
index 585647b..9a6f464 100644
@@ -47,6 +47,7 @@ struct batman_if {
 	struct kref refcount;
 	struct packet_type batman_adv_ptype;
 	struct net_device *soft_iface;
+	struct rcu_head rcu;
 };
 
 /**
Signature · 1 line
1.7.1
#5 lore raw
In reply to Marek Lindner
Quoted 3 lines · On Thursday 30 September 2010 14:08:40 Marek Lindner wrote:

since the vis code and the bridge loop avoidance code already use the kref library functions, I converted the remaining refcounting code to use kref as well.

These patches also got applied (revision 1818-1821).

Regards,
Marek