Skip to contentSkip to conversation

mac80211: refactor sta_info_insert_rcu to 3 main stages

PATCH Superseded Guy Eilam 2 participants 7 messages Jul 26, 2011 – Aug 16, 2011 mac80211

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Guy Eilam

author
#0 lore raw

PATCH 1/2 mac80211: refactor sta_info_insert_rcu to 3 main stages

Divided the sta_info_insert_rcu function to 3 mini-functions:
sta_info_insert_check - the initial checks done when inserting a new station
sta_info_insert_adhoc - the function that handles the station addition in ad-hoc mode
sta_info_insert_mgd - the function that handles the station addition in the other modes

The outer API was not changed.
The refactoring was done for better usage of the different stages in the station addition in new scenarios added in the next commit

---
 net/mac80211/sta_info.c |  144 +++++++++++++++++++++++++++++-----------------
 1 files changed, 91 insertions(+), 53 deletions(-)
net/mac80211/sta_info.c +91 -53
index b83870b..b4549f2 100644
@@ -371,93 +371,91 @@ static void sta_info_finish_work(struct work_struct *work)
 	mutex_unlock(&local->sta_mtx);
 }
 
-int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+static int sta_info_insert_check(struct sta_info *sta)
 {
-	struct ieee80211_local *local = sta->local;
 	struct ieee80211_sub_if_data *sdata = sta->sdata;
-	unsigned long flags;
-	int err = 0;
 
 	/*
 	 * Can't be a WARN_ON because it can be triggered through a race:
 	 * something inserts a STA (on one CPU) without holding the RTNL
 	 * and another CPU turns off the net device.
 	 */
-	if (unlikely(!ieee80211_sdata_running(sdata))) {
-		err = -ENETDOWN;
-		rcu_read_lock();
-		goto out_free;
-	}
+	if (unlikely(!ieee80211_sdata_running(sdata)))
+		return -ENETDOWN;
 
 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
-		    is_multicast_ether_addr(sta->sta.addr))) {
-		err = -EINVAL;
+		    is_multicast_ether_addr(sta->sta.addr)))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int sta_info_insert_adhoc(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	unsigned long flags;
+
+	spin_lock_irqsave(&local->sta_lock, flags);
+	/* check if STA exists already */
+	if (sta_info_get_bss(sdata, sta->sta.addr)) {
+		spin_unlock_irqrestore(&local->sta_lock, flags);
 		rcu_read_lock();
-		goto out_free;
+		__sta_info_free(local, sta);
+		return -EEXIST;
 	}
 
-	/*
-	 * In ad-hoc mode, we sometimes need to insert stations
-	 * from tasklet context from the RX path. To avoid races,
-	 * always do so in that case -- see the comment below.
-	 */
-	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
-		spin_lock_irqsave(&local->sta_lock, flags);
-		/* check if STA exists already */
-		if (sta_info_get_bss(sdata, sta->sta.addr)) {
-			spin_unlock_irqrestore(&local->sta_lock, flags);
-			rcu_read_lock();
-			err = -EEXIST;
-			goto out_free;
-		}
-
-		local->num_sta++;
-		local->sta_generation++;
-		smp_mb();
-		sta_info_hash_add(local, sta);
+	local->num_sta++;
+	local->sta_generation++;
+	smp_mb();
+	sta_info_hash_add(local, sta);
 
-		list_add_tail(&sta->list, &local->sta_pending_list);
+	list_add_tail(&sta->list, &local->sta_pending_list);
 
-		rcu_read_lock();
-		spin_unlock_irqrestore(&local->sta_lock, flags);
+	rcu_read_lock();
+	spin_unlock_irqrestore(&local->sta_lock, flags);
 
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
-		wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
-			    sta->sta.addr);
+	wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
+			sta->sta.addr);
 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
-		ieee80211_queue_work(&local->hw, &local->sta_finish_work);
+	ieee80211_queue_work(&local->hw, &local->sta_finish_work);
 
-		return 0;
-	}
+	return 0;
+}
+
+/*
+ * should be called with sta_mtx locked
+ * this function replaces the mutex lock
+ * with a RCU lock
+ */
+static int sta_info_insert_mgd(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	unsigned long flags;
+	int err = 0;
+
+	lockdep_assert_held(&local->sta_mtx);
 
 	/*
 	 * On first glance, this will look racy, because the code
-	 * below this point, which inserts a station with sleeping,
+	 * in this function, which inserts a station with sleeping,
 	 * unlocks the sta_lock between checking existence in the
 	 * hash table and inserting into it.
 	 *
 	 * However, it is not racy against itself because it keeps
-	 * the mutex locked. It still seems to race against the
-	 * above code that atomically inserts the station... That,
-	 * however, is not true because the above code can only
-	 * be invoked for IBSS interfaces, and the below code will
-	 * not be -- and the two do not race against each other as
-	 * the hash table also keys off the interface.
+	 * the mutex locked.
 	 */
 
-	might_sleep();
-
-	mutex_lock(&local->sta_mtx);
-
 	spin_lock_irqsave(&local->sta_lock, flags);
 	/* check if STA exists already */
 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
 		spin_unlock_irqrestore(&local->sta_lock, flags);
 		mutex_unlock(&local->sta_mtx);
 		rcu_read_lock();
-		err = -EEXIST;
-		goto out_free;
+		return -EEXIST;
 	}
 
 	spin_unlock_irqrestore(&local->sta_lock, flags);
@@ -466,7 +464,7 @@ int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
 	if (err) {
 		mutex_unlock(&local->sta_mtx);
 		rcu_read_lock();
-		goto out_free;
+		return err;
 	}
 
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
@@ -481,6 +479,46 @@ int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
 		mesh_accept_plinks_update(sdata);
 
 	return 0;
+}
+
+int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	int err = 0;
+
+	err = sta_info_insert_check(sta);
+	if (err) {
+		rcu_read_lock();
+		goto out_free;
+	}
+
+	/*
+	 * In ad-hoc mode, we sometimes need to insert stations
+	 * from tasklet context from the RX path. To avoid races,
+	 * always do so in that case -- see the comment below.
+	 */
+	if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
+		return sta_info_insert_adhoc(sta);
+
+	/*
+	 * It might seem that the function called below is in race against
+	 * the function call above that atomically inserts the station... That,
+	 * however, is not true because the above code can only
+	 * be invoked for IBSS interfaces, and the below code will
+	 * not be -- and the two do not race against each other as
+	 * the hash table also keys off the interface.
+	 */
+
+	might_sleep();
+
+	mutex_lock(&local->sta_mtx);
+
+	err = sta_info_insert_mgd(sta);
+	if (err)
+		goto out_free;
+
+	return 0;
  out_free:
 	BUG_ON(!err);
 	__sta_info_free(local, sta);
Signature · 1 line
1.7.4.1
author
#1 lore raw

PATCH 2/2 mac80211: fix race condition between assoc_done and first EAP packet

When associating to an AP, the station might miss the first EAP packet that the AP sends due to a race condition between the association success procedure and the rx flow in mac80211.
In such cases, the packet might fall in ieee80211_rx_h_check due to the fact that the relevant rx->sta wasn't allocated yet. Allocation of the relevant station info struct before actually sending the association request and setting it with a new dummy_sta flag solve this problem.
The station will accept only EAP packets from the AP while it is in the pre-association/dummy state.
This dummy station entry is not seen by normal sta_info_get() calls, only by sta_info_get_bss_rx().
The driver is not notified for the first insertion of the dummy station. The driver is notified only after the association is complete and the dummy flag is removed from the station entry. That way, all the rest of the code flow should be untouched by this change.

---
 net/mac80211/mlme.c     |   60 +++++++++++++--
 net/mac80211/rx.c       |   20 ++++-
 net/mac80211/sta_info.c |  192 ++++++++++++++++++++++++++++++++++-------------
 net/mac80211/sta_info.h |   30 +++++++-
 4 files changed, 240 insertions(+), 62 deletions(-)
net/mac80211/mlme.c +54 -6
index a497718..8a2fff1 100644
@@ -1495,10 +1495,14 @@ static bool ieee80211_assoc_success(struct ieee80211_work *wk,
 
 	ifmgd->aid = aid;
 
-	sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
-	if (!sta) {
-		printk(KERN_DEBUG "%s: failed to alloc STA entry for"
-		       " the AP\n", sdata->name);
+	mutex_lock(&sdata->local->sta_mtx);
+	/*
+	 * station info was already allocated and inserted before
+	 * the association and should be available to us
+	 */
+	sta = sta_info_get_rx(sdata, cbss->bssid);
+	if (WARN_ON(!sta)) {
+		mutex_unlock(&sdata->local->sta_mtx);
 		return false;
 	}
 
@@ -1569,8 +1573,10 @@ static bool ieee80211_assoc_success(struct ieee80211_work *wk,
 	if (elems.wmm_param)
 		set_sta_flags(sta, WLAN_STA_WME);
 
-	err = sta_info_insert(sta);
+	err = sta_info_insert_notify(sta);
 	sta = NULL;
+	/* sta_info_insert_notify changed the mutex lock with rcu lock */
+	rcu_read_unlock();
 	if (err) {
 		printk(KERN_DEBUG "%s: failed to insert STA entry for"
 		       " the AP (error %d)\n", sdata->name, err);
@@ -2387,15 +2393,43 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 	return 0;
 }
 
+/* create and insert a dummy station entry */
+static int ieee80211_pre_assoc(struct ieee80211_sub_if_data *sdata,
+				u8 *bssid) {
+	struct sta_info *sta;
+	int err;
+
+	sta = sta_info_alloc(sdata, bssid, GFP_KERNEL);
+	if (!sta) {
+		printk(KERN_DEBUG "%s: failed to alloc STA entry for"
+			   " the AP\n", sdata->name);
+		return -ENOMEM;
+	}
+
+	sta->dummy = true;
+
+	err = sta_info_insert(sta);
+	sta = NULL;
+	if (err) {
+		printk(KERN_DEBUG "%s: failed to insert Dummy STA entry for"
+		       " the AP (error %d)\n", sdata->name, err);
+		return err;
+	}
+
+	return 0;
+}
+
 static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
 						  struct sk_buff *skb)
 {
 	struct ieee80211_mgmt *mgmt;
 	struct ieee80211_rx_status *rx_status;
 	struct ieee802_11_elems elems;
+	struct cfg80211_bss *cbss = wk->assoc.bss;
 	u16 status;
 
 	if (!skb) {
+		sta_info_destroy_addr(wk->sdata, cbss->bssid);
 		cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
 		return WORK_DONE_DESTROY;
 	}
@@ -2421,12 +2455,16 @@ static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
 		if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
 			mutex_unlock(&wk->sdata->u.mgd.mtx);
 			/* oops -- internal error -- send timeout for now */
+			sta_info_destroy_addr(wk->sdata, cbss->bssid);
 			cfg80211_send_assoc_timeout(wk->sdata->dev,
 						    wk->filter_ta);
 			return WORK_DONE_DESTROY;
 		}
 
 		mutex_unlock(&wk->sdata->u.mgd.mtx);
+	} else {
+		/* assoc failed - destroy the dummy station entry */
+		sta_info_destroy_addr(wk->sdata, cbss->bssid);
 	}
 
 	cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
@@ -2440,7 +2478,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 	struct ieee80211_bss *bss = (void *)req->bss->priv;
 	struct ieee80211_work *wk;
 	const u8 *ssid;
-	int i;
+	int i, err;
 
 	mutex_lock(&ifmgd->mtx);
 	if (ifmgd->associated) {
@@ -2465,6 +2503,16 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 	if (!wk)
 		return -ENOMEM;
 
+	/*
+	 * create a dummy station info entry in order
+	 * to start accepting incoming EAPOL packets from the station
+	 */
+	err = ieee80211_pre_assoc(sdata, req->bss->bssid);
+	if (err) {
+		kfree(wk);
+		return err;
+	}
+
 	ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
 	ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
 
net/mac80211/rx.c +16 -4
index e8e67dc..3146891 100644
@@ -842,8 +842,20 @@ ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
 		      ieee80211_is_pspoll(hdr->frame_control)) &&
 		     rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
 		     rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
-		     (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC))))
+		     (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
+		if (rx->sta->dummy && ieee80211_is_data(hdr->frame_control)) {
+			u16 ethertype;
+			u8 *payload;
+
+			payload = rx->skb->data +
+				ieee80211_hdrlen(hdr->frame_control);
+			ethertype = (payload[6] << 8) | payload[7];
+			if (cpu_to_be16(ethertype) ==
+				rx->sdata->control_port_protocol)
+				return RX_CONTINUE;
+		}
 		return RX_DROP_MONITOR;
+	}
 
 	return RX_CONTINUE;
 }
@@ -2782,7 +2794,7 @@ static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
 	if (ieee80211_is_data(fc)) {
 		prev_sta = NULL;
 
-		for_each_sta_info(local, hdr->addr2, sta, tmp) {
+		for_each_sta_info_rx(local, hdr->addr2, sta, tmp) {
 			if (!prev_sta) {
 				prev_sta = sta;
 				continue;
@@ -2826,7 +2838,7 @@ static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
 			continue;
 		}
 
-		rx.sta = sta_info_get_bss(prev, hdr->addr2);
+		rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
 		rx.sdata = prev;
 		ieee80211_prepare_and_rx_handle(&rx, skb, false);
 
@@ -2834,7 +2846,7 @@ static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
 	}
 
 	if (prev) {
-		rx.sta = sta_info_get_bss(prev, hdr->addr2);
+		rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
 		rx.sdata = prev;
 
 		if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
net/mac80211/sta_info.c +141 -51

293 lines not on this page · raw message

net/mac80211/sta_info.h +29 -1
index a06d64e..b1b104b 100644
@@ -238,10 +238,12 @@ struct sta_ampdu_mlme {
  * @plink_timer: peer link watch timer
  * @plink_timer_was_running: used by suspend/resume to restore timers
  * @debugfs: debug filesystem info
- * @sta: station information we share with the driver
  * @dead: set to true when sta is unlinked
  * @uploaded: set to true when sta is uploaded to the driver
  * @lost_packets: number of consecutive lost packets
+ * @dummy: indicate a dummy station created for receiving
+ *	EAP frames before association
+ * @sta: station information we share with the driver
  */
 struct sta_info {
 	/* General information, mostly static */
@@ -335,6 +337,9 @@ struct sta_info {
 
 	unsigned int lost_packets;
 
+	/* should be right in front of sta to be in the same cache line */
+	bool dummy;
+
 	/* keep last! */
 	struct ieee80211_sta sta;
 };
@@ -435,9 +440,15 @@ rcu_dereference_protected_tid_tx(struct sta_info *sta, int tid)
 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
 			      const u8 *addr);
 
+struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
+			      const u8 *addr);
+
 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
 				  const u8 *addr);
 
+struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
+				  const u8 *addr);
+
 static inline
 void for_each_sta_info_type_check(struct ieee80211_local *local,
 				  const u8 *addr,
@@ -458,6 +469,22 @@ void for_each_sta_info_type_check(struct ieee80211_local *local,
 		_sta = nxt,						\
 		nxt = _sta ? rcu_dereference(_sta->hnext) : NULL	\
 	     )								\
+	/* run code only if address matches and it's not a dummy sta */	\
+	if (memcmp(_sta->sta.addr, (_addr), ETH_ALEN) == 0 &&\
+		!_sta->dummy)
+
+#define for_each_sta_info_rx(local, _addr, _sta, nxt)			\
+	for (/* initialise loop */\
+		_sta = rcu_dereference(local->sta_hash[STA_HASH(_addr)]),\
+		nxt = _sta ? rcu_dereference(_sta->hnext) : NULL;	\
+		/* typecheck */						\
+		for_each_sta_info_type_check(local, (_addr), _sta, nxt),\
+		/* continue condition */				\
+		_sta;							\
+		/* advance loop */					\
+		_sta = nxt,						\
+		nxt = _sta ? rcu_dereference(_sta->hnext) : NULL	\
+		)							   \
 	/* compare address and run code only if it matches */		\
 	if (memcmp(_sta->sta.addr, (_addr), ETH_ALEN) == 0)
 
@@ -483,6 +510,7 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
 int sta_info_insert(struct sta_info *sta);
 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU);
 int sta_info_insert_atomic(struct sta_info *sta);
+int sta_info_insert_notify(struct sta_info *sta)  __acquires(RCU);
 
 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata,
 			  const u8 *addr);
Signature · 1 line
1.7.4.1
maintainer
#2 lore raw
In reply to Guy Eilam

Just a cursory look, I'll send a more detailed review later, but I wanted to ask John to not merge this for now;

Quoted 1 line

+static int sta_info_insert_adhoc(struct sta_info *sta) __acquires(RCU)

_ibss would be preferred

Quoted 1 line

+static int sta_info_insert_mgd(struct sta_info *sta) __acquires(RCU)

_mgd is terribly misleading, this will be done for all modes other than IBSS.

johannes

author
#3 lore raw
Quoted 11 lines · Johannes Berg

Just a cursory look, I'll send a more detailed review later, but I wanted to ask John to not merge this for now;

Quoted 1 line

+static int sta_info_insert_adhoc(struct sta_info *sta) __acquires(RCU)

_ibss would be preferred

Quoted 1 line

+static int sta_info_insert_mgd(struct sta_info *sta) __acquires(RCU)

_mgd is terribly misleading, this will be done for all modes other than IBSS.

Fair enough. the _ibss change is logical and understood. The problem with the _mgd function is that it will be silly to call it _non_ibss (or is it?)
Do you have a suggestion for a better name?

Quoted 5 lines

johannes

maintainer
#4 lore raw
In reply to Guy Eilam
Quoted 19 lines · Guy Eilam
net/mac80211/sta_info.c +2 -9
-int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+static int sta_info_insert_check(struct sta_info *sta)
 {
-	struct ieee80211_local *local = sta->local;
 	struct ieee80211_sub_if_data *sdata = sta->sdata;
-	unsigned long flags;
-	int err = 0;
 
 	/*
 	 * Can't be a WARN_ON because it can be triggered through a race:
 	 * something inserts a STA (on one CPU) without holding the RTNL
 	 * and another CPU turns off the net device.
 	 */
-	if (unlikely(!ieee80211_sdata_running(sdata))) {
-		err = -ENETDOWN;
-		rcu_read_lock();
-		goto out_free;
-	}
+	if (unlikely(!ieee80211_sdata_running(sdata)))

net/mac80211/sta_info.c static void sta_info_finish_work(struct work_struct *work) · line 391

Quoted 10 lines
net/mac80211/sta_info.c +6 -2
+		return -ENETDOWN;
 
 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
-		    is_multicast_ether_addr(sta->sta.addr))) {
-		err = -EINVAL;
+		    is_multicast_ether_addr(sta->sta.addr)))
+		return -EINVAL;
+
+	return 0;
+}

That seems nice, even simplifying the code :)

Quoted 52 lines
net/mac80211/sta_info.c +20 -25
+static int sta_info_insert_adhoc(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	unsigned long flags;
+
+	spin_lock_irqsave(&local->sta_lock, flags);
+	/* check if STA exists already */
+	if (sta_info_get_bss(sdata, sta->sta.addr)) {
+		spin_unlock_irqrestore(&local->sta_lock, flags);
 		rcu_read_lock();
-		goto out_free;
+		__sta_info_free(local, sta);
+		return -EEXIST;
 	}
 
-	/*
-	 * In ad-hoc mode, we sometimes need to insert stations
-	 * from tasklet context from the RX path. To avoid races,
-	 * always do so in that case -- see the comment below.
-	 */
-	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
-		spin_lock_irqsave(&local->sta_lock, flags);
-		/* check if STA exists already */
-		if (sta_info_get_bss(sdata, sta->sta.addr)) {
-			spin_unlock_irqrestore(&local->sta_lock, flags);
-			rcu_read_lock();
-			err = -EEXIST;
-			goto out_free;
-		}
-
-		local->num_sta++;
-		local->sta_generation++;
-		smp_mb();
-		sta_info_hash_add(local, sta);
+	local->num_sta++;
+	local->sta_generation++;
+	smp_mb();
+	sta_info_hash_add(local, sta);
 
-		list_add_tail(&sta->list, &local->sta_pending_list);
+	list_add_tail(&sta->list, &local->sta_pending_list);
 
-		rcu_read_lock();
-		spin_unlock_irqrestore(&local->sta_lock, flags);
+	rcu_read_lock();
+	spin_unlock_irqrestore(&local->sta_lock, flags);
 
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
-		wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
-			    sta->sta.addr);
+	wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",

net/mac80211/sta_info.c static void sta_info_finish_work(struct work_struct *work) · line 426

Quoted 10 lines
net/mac80211/sta_info.c +4 -3
+			sta->sta.addr);
 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
-		ieee80211_queue_work(&local->hw, &local->sta_finish_work);
+	ieee80211_queue_work(&local->hw, &local->sta_finish_work);
 
-		return 0;
-	}
+	return 0;
+}

Yup, that looks good too, just the patch is a bit odd due to the code indentation change.

Quoted 34 lines
net/mac80211/sta_info.c +15 -10
+/*
+ * should be called with sta_mtx locked
+ * this function replaces the mutex lock
+ * with a RCU lock
+ */
+static int sta_info_insert_mgd(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	unsigned long flags;
+	int err = 0;
+
+	lockdep_assert_held(&local->sta_mtx);
 
 	/*
 	 * On first glance, this will look racy, because the code
-	 * below this point, which inserts a station with sleeping,
+	 * in this function, which inserts a station with sleeping,
 	 * unlocks the sta_lock between checking existence in the
 	 * hash table and inserting into it.
 	 *
 	 * However, it is not racy against itself because it keeps
-	 * the mutex locked. It still seems to race against the
-	 * above code that atomically inserts the station... That,
-	 * however, is not true because the above code can only
-	 * be invoked for IBSS interfaces, and the below code will
-	 * not be -- and the two do not race against each other as
-	 * the hash table also keys off the interface.
+	 * the mutex locked.
 	 */
 
-	might_sleep();
-
-	mutex_lock(&local->sta_mtx);

net/mac80211/sta_info.c static void sta_info_finish_work(struct work_struct *work) · line 458

Quoted 10 lines
net/mac80211/sta_info.c +1 -3
-
 	spin_lock_irqsave(&local->sta_lock, flags);
 	/* check if STA exists already */
 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
 		spin_unlock_irqrestore(&local->sta_lock, flags);
 		mutex_unlock(&local->sta_mtx);
 		rcu_read_lock();
-		err = -EEXIST;
-		goto out_free;
+		return -EEXIST;

Isn't that missing the free now?

net/mac80211/sta_info.c int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) · line 467

Quoted 9 lines
net/mac80211/sta_info.c +1 -1
 	}
 
 	spin_unlock_irqrestore(&local->sta_lock, flags);
@@ -466,7 +464,7 @@ int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
 	if (err) {
 		mutex_unlock(&local->sta_mtx);
 		rcu_read_lock();
-		goto out_free;
+		return err;

and here?

Quoted 31 lines
net/mac80211/sta_info.c +31 -0
+int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+{
+	struct ieee80211_local *local = sta->local;
+	struct ieee80211_sub_if_data *sdata = sta->sdata;
+	int err = 0;
+
+	err = sta_info_insert_check(sta);
+	if (err) {
+		rcu_read_lock();
+		goto out_free;
+	}
+
+	/*
+	 * In ad-hoc mode, we sometimes need to insert stations
+	 * from tasklet context from the RX path. To avoid races,
+	 * always do so in that case -- see the comment below.
+	 */
+	if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
+		return sta_info_insert_adhoc(sta);
+
+	/*
+	 * It might seem that the function called below is in race against
+	 * the function call above that atomically inserts the station... That,
+	 * however, is not true because the above code can only
+	 * be invoked for IBSS interfaces, and the below code will
+	 * not be -- and the two do not race against each other as
+	 * the hash table also keys off the interface.
+	 */
+
+	might_sleep();
+

net/mac80211/sta_info.c int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) · line 524

Quoted 10 lines
net/mac80211/sta_info.c +7 -0
+	mutex_lock(&local->sta_mtx);
+
+	err = sta_info_insert_mgd(sta);
+	if (err)
+		goto out_free;
+
+	return 0;
  out_free:
 	BUG_ON(!err);
 	__sta_info_free(local, sta);

Ah, ok, I guess not.

Hmm. Can we make the ibss vs. non-IBSS code paths more similar wrt. freeing? The _ibss one will free it, but the _mgd one won't, that seems a bit strange to me.

Also, I'd prefer to move the might_sleep/locking into _mgd() if possible?

johannes

author
#5 lore raw
Quoted 203 lines · Johannes Berg
Quoted 26 lines · Guy Eilam
net/mac80211/sta_info.c +1 -1
-int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+static int sta_info_insert_check(struct sta_info *sta)

 {
-     struct ieee80211_local *local = sta->local; struct ieee80211_sub_if_data *sdata = sta->sdata;
-     unsigned long flags;
-     int err = 0;

      /*
       * Can't be a WARN_ON because it can be triggered through a race:
       * something inserts a STA (on one CPU) without holding the RTNL
       * and another CPU turns off the net device.
       */
-     if (unlikely(!ieee80211_sdata_running(sdata))) {
-             err = -ENETDOWN;
-             rcu_read_lock();
-             goto out_free;
-     }
+     if (unlikely(!ieee80211_sdata_running(sdata)))
+             return -ENETDOWN;

      if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
-                 is_multicast_ether_addr(sta->sta.addr))) {
-             err = -EINVAL;
+                 is_multicast_ether_addr(sta->sta.addr)))
+             return -EINVAL;

net/mac80211/sta_info.c static void sta_info_finish_work(struct work_struct *work) · line 391

Quoted 3 lines
net/mac80211/sta_info.c +3 -0
+
+     return 0;
+}

That seems nice, even simplifying the code :)

Quoted 55 lines
net/mac80211/sta_info.c +10 -0
+static int sta_info_insert_adhoc(struct sta_info *sta) __acquires(RCU)
+{
+     struct ieee80211_local *local = sta->local;
+     struct ieee80211_sub_if_data *sdata = sta->sdata;
+     unsigned long flags;
+
+     spin_lock_irqsave(&local->sta_lock, flags);
+     /* check if STA exists already */
+     if (sta_info_get_bss(sdata, sta->sta.addr)) {
+             spin_unlock_irqrestore(&local->sta_lock, flags);

              rcu_read_lock();
-             goto out_free;
+             __sta_info_free(local, sta);
+             return -EEXIST;
      }

-     /*
-      * In ad-hoc mode, we sometimes need to insert stations
-      * from tasklet context from the RX path. To avoid races,
-      * always do so in that case -- see the comment below.
-      */
-     if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
-             spin_lock_irqsave(&local->sta_lock, flags);
-             /* check if STA exists already */
-             if (sta_info_get_bss(sdata, sta->sta.addr)) {
-                     spin_unlock_irqrestore(&local->sta_lock, flags);
-                     rcu_read_lock();
-                     err = -EEXIST;
-                     goto out_free;
-             }
-
-             local->num_sta++;
-             local->sta_generation++;
-             smp_mb();
-             sta_info_hash_add(local, sta);
+     local->num_sta++;
+     local->sta_generation++;
+     smp_mb();
+     sta_info_hash_add(local, sta);

-             list_add_tail(&sta->list, &local->sta_pending_list);
+     list_add_tail(&sta->list, &local->sta_pending_list);

-             rcu_read_lock();
-             spin_unlock_irqrestore(&local->sta_lock, flags);
+     rcu_read_lock();
+     spin_unlock_irqrestore(&local->sta_lock, flags);

 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
-             wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
-                         sta->sta.addr);
+     wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
+                     sta->sta.addr);
 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */

Quoted 7 lines

-             ieee80211_queue_work(&local->hw, &local->sta_finish_work);
+     ieee80211_queue_work(&local->hw, &local->sta_finish_work);

-             return 0;
-     }
+     return 0;
+}

Yup, that looks good too, just the patch is a bit odd due to the code indentation change.

Quoted 36 lines
net/mac80211/sta_info.c +13 -0
+/*
+ * should be called with sta_mtx locked
+ * this function replaces the mutex lock
+ * with a RCU lock
+ */
+static int sta_info_insert_mgd(struct sta_info *sta) __acquires(RCU)
+{
+     struct ieee80211_local *local = sta->local;
+     struct ieee80211_sub_if_data *sdata = sta->sdata;
+     unsigned long flags;
+     int err = 0;
+
+     lockdep_assert_held(&local->sta_mtx);

      /*
       * On first glance, this will look racy, because the code
-      * below this point, which inserts a station with sleeping,
+      * in this function, which inserts a station with sleeping,
       * unlocks the sta_lock between checking existence in the
       * hash table and inserting into it.
       *
       * However, it is not racy against itself because it keeps
-      * the mutex locked. It still seems to race against the
-      * above code that atomically inserts the station... That,
-      * however, is not true because the above code can only
-      * be invoked for IBSS interfaces, and the below code will
-      * not be -- and the two do not race against each other as
-      * the hash table also keys off the interface.
+      * the mutex locked.
       */

-     might_sleep();

net/mac80211/sta_info.c +0 -3
-
-     mutex_lock(&local->sta_mtx);
-

      spin_lock_irqsave(&local->sta_lock, flags);

Quoted 8 lines

      /* check if STA exists already */
      if (sta_info_get_bss(sdata, sta->sta.addr)) { spin_unlock_irqrestore(&local->sta_lock, flags); mutex_unlock(&local->sta_mtx); rcu_read_lock();
-             err = -EEXIST;
-             goto out_free;
+             return -EEXIST;

Isn't that missing the free now?

net/mac80211/sta_info.c int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) · line 467

Quoted 9 lines

      }

      spin_unlock_irqrestore(&local->sta_lock, flags);

net/mac80211/sta_info.c +1 -1
@@ -466,7 +464,7 @@ int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
       if (err) {
               mutex_unlock(&local->sta_mtx);
               rcu_read_lock();
-             goto out_free;
+             return err;

and here?

Quoted 38 lines
net/mac80211/sta_info.c +38 -0
+int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
+{
+     struct ieee80211_local *local = sta->local;
+     struct ieee80211_sub_if_data *sdata = sta->sdata;
+     int err = 0;
+
+     err = sta_info_insert_check(sta);
+     if (err) {
+             rcu_read_lock();
+             goto out_free;
+     }
+
+     /*
+      * In ad-hoc mode, we sometimes need to insert stations
+      * from tasklet context from the RX path. To avoid races,
+      * always do so in that case -- see the comment below.
+      */
+     if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
+             return sta_info_insert_adhoc(sta);
+
+     /*
+      * It might seem that the function called below is in race against
+      * the function call above that atomically inserts the station... That,
+      * however, is not true because the above code can only
+      * be invoked for IBSS interfaces, and the below code will
+      * not be -- and the two do not race against each other as
+      * the hash table also keys off the interface.
+      */
+
+     might_sleep();
+
+     mutex_lock(&local->sta_mtx);
+
+     err = sta_info_insert_mgd(sta);
+     if (err)
+             goto out_free;
+
+     return 0;
Quoted 3 lines

  out_free:
      BUG_ON(!err);
      __sta_info_free(local, sta);

Ah, ok, I guess not.

Quoted 6 lines

Hmm. Can we make the ibss vs. non-IBSS code paths more similar wrt. freeing? The _ibss one will free it, but the _mgd one won't, that seems a bit strange to me.

Also, I'd prefer to move the might_sleep/locking into _mgd() if possible?

I'll make sure that the _ibss and _non_ibss code paths will have similar freeing (the freeinng in both cases will be handled outside the function like it is in the _non_ibss now).
The reason I have the might_sleep/locking outside the _non_ibss function is because the additions in the next patch. The whole point of this refactoring from my point of view was the option to have a similar insert function that won't have the locking inside of it.

Quoted 4 lines

johannes

maintainer
#6 lore raw
Quoted 7 lines · Guy Eilam

I'll make sure that the _ibss and _non_ibss code paths will have similar freeing (the freeinng in both cases will be handled outside the function like it is in the _non_ibss now).
The reason I have the might_sleep/locking outside the _non_ibss function is because the additions in the next patch. The whole point of this refactoring from my point of view was the option to have a similar insert function that won't have the locking inside of it.

Ok, fair enough, so it's not possible to move the locking :)

johannes