Skip to contentSkip to conversation

wireless: Add memory usage debugging.

RFC In review [email protected] 2 participants 6 messages Jun 17, 2013 – Jun 19, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/2 wireless: Add memory usage debugging.

From: Ben Greear <[email protected]>

The bss objects are reference counted, and the ies are also tricky to keep track of. Add option to track allocation and freeing of the ies and bss objects, and add debugfs files to show the current objects.

---
 net/wireless/Kconfig   |   13 +++++
 net/wireless/core.c    |    5 +-
 net/wireless/core.h    |   17 ++++++
 net/wireless/debugfs.c |  117 +++++++++++++++++++++++++++++++++++++++++++
 net/wireless/debugfs.h |    2 +
 net/wireless/scan.c    |  130 ++++++++++++++++++++++++++++++++++++++++++------
 6 files changed, 267 insertions(+), 17 deletions(-)
net/wireless/Kconfig +13 -0
index 16d08b3..43ec2cd 100644
@@ -115,6 +115,19 @@ config CFG80211_DEBUGFS
 
 	  If unsure, say N.
 
+config CFG80211_MEM_DEBUGGING
+	bool "cfg80211 memory debugging logic"
+	default n
+	depends on CFG80211_DEBUGFS
+	---help---
+	  Enable this if you want to debug memory handling for bss and ies
+	  objects.  New debugfs files: ieee80211/all_ies and all_bss will
+	  be created to display these objects.  This has a moderate CPU cost
+	  and uses a bit more memory than normal, but otherwise is not very
+	  expensive.
+
+	  If unsure, say N.
+
 config CFG80211_INTERNAL_REGDB
 	bool "use statically compiled regulatory rules database" if EXPERT
 	default n
net/wireless/core.c +3 -2
index 9f08203..eb3e1de 100644
@@ -1123,6 +1123,7 @@ static int __init cfg80211_init(void)
 		goto out_fail_nl80211;
 
 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
+	ieee80211_debugfs_add_glbl(ieee80211_debugfs_dir);
 
 	err = regulatory_init();
 	if (err)
@@ -1137,7 +1138,7 @@ static int __init cfg80211_init(void)
 out_fail_wq:
 	regulatory_exit();
 out_fail_reg:
-	debugfs_remove(ieee80211_debugfs_dir);
+	debugfs_remove_recursive(ieee80211_debugfs_dir);
 out_fail_nl80211:
 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
 out_fail_notifier:
@@ -1151,7 +1152,7 @@ subsys_initcall(cfg80211_init);
 
 static void __exit cfg80211_exit(void)
 {
-	debugfs_remove(ieee80211_debugfs_dir);
+	debugfs_remove_recursive(ieee80211_debugfs_dir);
 	nl80211_exit();
 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
 	wiphy_sysfs_exit();
net/wireless/core.h +17 -0
index 71b7285..e75be56 100644
@@ -126,6 +126,23 @@ static inline void assert_cfg80211_lock(void)
 	lockdep_assert_held(&cfg80211_mutex);
 }
 
+#ifdef CONFIG_CFG80211_MEM_DEBUGGING
+
+struct wifi_mem_tracker {
+	struct list_head mylist;
+	char buf[40];
+	void *ptr;
+};
+extern struct list_head ies_list;
+extern spinlock_t ies_lock;
+extern atomic_t ies_count;
+
+extern struct list_head bss_list;
+extern spinlock_t bss_lock;
+extern atomic_t bss_count;
+
+#endif
+
 struct cfg80211_internal_bss {
 	struct list_head list;
 	struct list_head hidden_list;
net/wireless/debugfs.c +117 -0

133 lines not on this page · raw message

net/wireless/debugfs.h +2 -0
index 74fdd38..f644869 100644
@@ -3,9 +3,11 @@
 
 #ifdef CONFIG_CFG80211_DEBUGFS
 void cfg80211_debugfs_rdev_add(struct cfg80211_registered_device *rdev);
+void ieee80211_debugfs_add_glbl(struct dentry *dir);
 #else
 static inline
 void cfg80211_debugfs_rdev_add(struct cfg80211_registered_device *rdev) {}
+static inline void ieee80211_debugfs_add_glbl(struct dentry *dir) { }
 #endif
 
 #endif /* __CFG80211_DEBUGFS_H */
net/wireless/scan.c +115 -15

191 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#1 lore raw

RFC 2/2 mac80211: Fix bss ref leak.

From: Ben Greear <[email protected]>

Some of the calls to ieee80211_destroy_assoc_data should be putting the bss reference and were not. Add boolean argument to tell that method whether or not it should put the reference and fix calling code appropriately.

Grab the bss reference where the pointer is assigned to make it easier to properly do reference counting.

Also add some comments to help clarify the bss ref-counting logic.

---
 net/mac80211/mlme.c |   34 +++++++++++++++++++++++++---------
 net/mac80211/scan.c |    6 ++++++
 2 files changed, 31 insertions(+), 9 deletions(-)
net/mac80211/mlme.c +25 -9
index 6510790..732eda0 100644
@@ -2400,7 +2400,7 @@ static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
 }
 
 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
-					 bool assoc)
+					 bool assoc, bool put_bss)
 {
 	struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
 
@@ -2415,6 +2415,10 @@ static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
 		ieee80211_vif_release_channel(sdata);
 	}
 
+	if (put_bss)
+		cfg80211_put_bss(sdata->local->hw.wiphy, assoc_data->bss);
+
+
 	kfree(assoc_data);
 	sdata->u.mgd.assoc_data = NULL;
 }
@@ -2587,6 +2591,7 @@ static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
 	return true;
 }
 
+/** Calling code must dispose of bss reference if it is not NULL. */
 static enum rx_mgmt_action __must_check
 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 			     struct ieee80211_mgmt *mgmt, size_t len,
@@ -2643,17 +2648,20 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 		return RX_MGMT_NONE;
 	}
 
+	/** bss will be passed back to calling code, and that code must
+	 * deal with properly putting the reference.
+	 */
 	*bss = assoc_data->bss;
 
 	if (status_code != WLAN_STATUS_SUCCESS) {
 		sdata_info(sdata, "%pM denied association (code=%d)\n",
 			   mgmt->sa, status_code);
-		ieee80211_destroy_assoc_data(sdata, false);
+		ieee80211_destroy_assoc_data(sdata, false, false);
 	} else {
 		if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
 			/* oops -- internal error -- send timeout for now */
-			ieee80211_destroy_assoc_data(sdata, false);
-			cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
+			ieee80211_destroy_assoc_data(sdata, false, true);
+			*bss = NULL; /* Ensure no stale references */
 			return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
 		}
 		sdata_info(sdata, "associated\n");
@@ -2663,7 +2671,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 		 * recalc after assoc_data is NULL but before associated
 		 * is set can cause the interface to go idle
 		 */
-		ieee80211_destroy_assoc_data(sdata, true);
+		ieee80211_destroy_assoc_data(sdata, true, false);
 	}
 
 	return RX_MGMT_CFG80211_RX_ASSOC;
@@ -3105,6 +3113,9 @@ void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
 		break;
 	case IEEE80211_STYPE_ASSOC_RESP:
 	case IEEE80211_STYPE_REASSOC_RESP:
+		/* One way or another, must eventually put bss reference
+		 * if it is not NULL.
+		 */
 		rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
 		break;
 	case IEEE80211_STYPE_ACTION:
@@ -3136,6 +3147,9 @@ void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
 		cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
 		break;
 	case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
+		/* bss reference is already put at this point, see
+		 * 'internal-error' comment in ieee80211_rx_mgmt_assoc_resp
+		 */
 		cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
 		break;
 	case RX_MGMT_CFG80211_TX_DEAUTH:
@@ -3385,7 +3399,7 @@ void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
 
 			memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
 
-			ieee80211_destroy_assoc_data(sdata, false);
+			ieee80211_destroy_assoc_data(sdata, false, true);
 
 			mutex_unlock(&ifmgd->mtx);
 			cfg80211_send_assoc_timeout(sdata->dev, bssid);
@@ -3935,6 +3949,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 		return -ENOMEM;
 
 	auth_data->bss = req->bss;
+	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
 
 	if (req->sae_data_len >= 4) {
 		__le16 *pos = (__le16 *) req->sae_data;
@@ -3998,8 +4013,6 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 		goto err_clear;
 	}
 
-	/* hold our own reference */
-	cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
 	err = 0;
 	goto out_unlock;
 
@@ -4008,6 +4021,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
 	ifmgd->auth_data = NULL;
  err_free:
+	cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
 	kfree(auth_data);
  out_unlock:
 	mutex_unlock(&ifmgd->mtx);
@@ -4129,6 +4143,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 	}
 
 	assoc_data->bss = req->bss;
+	cfg80211_ref_bss(sdata->local->hw.wiphy, assoc_data->bss);
 
 	if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
 		if (ifmgd->powersave)
@@ -4259,6 +4274,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  err_clear:
 	memset(ifmgd->bssid, 0, ETH_ALEN);
 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
+	cfg80211_put_bss(sdata->local->hw.wiphy, assoc_data->bss);
 	ifmgd->assoc_data = NULL;
  err_free:
 	kfree(assoc_data);
@@ -4364,7 +4380,7 @@ void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&ifmgd->mtx);
 	if (ifmgd->assoc_data)
-		ieee80211_destroy_assoc_data(sdata, false);
+		ieee80211_destroy_assoc_data(sdata, false, true);
 	if (ifmgd->auth_data)
 		ieee80211_destroy_auth_data(sdata, false);
 	del_timer_sync(&ifmgd->timer);
net/mac80211/scan.c +6 -0
index 0b6434b..e0fcb4a 100644
@@ -55,6 +55,9 @@ static bool is_uapsd_supported(struct ieee802_11_elems *elems)
 	return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
 }
 
+/** Must (eventually) put the returned value to keep from leaking
+ * a reference to the bss.
+ */
 struct ieee80211_bss *
 ieee80211_bss_info_update(struct ieee80211_local *local,
 			  struct ieee80211_rx_status *rx_status,
@@ -73,6 +76,9 @@ ieee80211_bss_info_update(struct ieee80211_local *local,
 	else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
 		signal = (rx_status->signal * 100) / local->hw.max_signal;
 
+	/* We now own a reference to cbss, have to make sure we
+	 * put it later.
+	 */
 	cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
 					 mgmt, len, signal, GFP_ATOMIC);
 	if (!cbss)
Signature · 1 line
1.7.3.4
maintainer
#2 lore raw

Re: [RFC 2/2] mac80211: Fix bss ref leak.

net/mac80211/mlme.c static void ieee80211_get_rates(struct ieee80211_supported_band *sband, · line 2403

Quoted 3 lines · On Mon, 2013-06-17 at 14:32 -0700, [email protected] wrote:

static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,

net/mac80211/mlme.c +1 -1
-					 bool assoc)
+					 bool assoc, bool put_bss)

Do we _really_ need another argument? Shouldn't it always be put in the non-assoc case anyway, at least if non-NULL?

net/mac80211/mlme.c static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata, · line 2421

Quoted 8 lines
net/mac80211/mlme.c +4 -0
@@ -2415,6 +2415,10 @@ static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
 		ieee80211_vif_release_channel(sdata);
 	}
 
+	if (put_bss)
+		cfg80211_put_bss(sdata->local->hw.wiphy, assoc_data->bss);
+
+

just one blank line

net/mac80211/mlme.c ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata, · line 2653

Quoted 3 lines
net/mac80211/mlme.c +3 -0
+	/** bss will be passed back to calling code, and that code must
+	 * deal with properly putting the reference.
+	 */

/** is for kernel-doc only

Quoted 1 line

return RX_MGMT_CFG80211_RX_ASSOC;

You're working on some pretty old code here ... If you want this to be in stable the patch really needs to be smaller, I think. And for mac80211-next this can't apply.

johannes

author
#3 lore raw

Re: [RFC 2/2] mac80211: Fix bss ref leak.

Quoted 8 lines · Johannes Berg

net/mac80211/mlme.c static void ieee80211_get_rates(struct ieee80211_supported_band *sband, · line 2403

Quoted 3 lines · On Mon, 2013-06-17 at 14:32 -0700, [email protected] wrote:

static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,

net/mac80211/mlme.c +1 -1
-					 bool assoc)
+					 bool assoc, bool put_bss)

Do we _really_ need another argument? Shouldn't it always be put in the non-assoc case anyway, at least if non-NULL?

I can check if that is the case...

Quoted 18 lines

net/mac80211/mlme.c static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata, · line 2421

Quoted 9 lines
net/mac80211/mlme.c +4 -0
@@ -2415,6 +2415,10 @@ static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  		ieee80211_vif_release_channel(sdata);
  	}
 
+	if (put_bss)
+		cfg80211_put_bss(sdata->local->hw.wiphy, assoc_data->bss);
+
+

just one blank line

net/mac80211/mlme.c ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata, · line 2653

Quoted 3 lines
net/mac80211/mlme.c +3 -0
+	/** bss will be passed back to calling code, and that code must
+	 * deal with properly putting the reference.
+	 */

/** is for kernel-doc only

Quoted 5 lines
Quoted 1 line

return RX_MGMT_CFG80211_RX_ASSOC;

You're working on some pretty old code here ... If you want this to be in stable the patch really needs to be smaller, I think. And for mac80211-next this can't apply.

I'm working on 3.9.6. When I get the problems fixed here, I can help port this to whatever is the upstream kernel.

I'm not certain this is worth bothering with for stable anyway. It seems the leaks are not too bad in general use cases, and piddling around with code this tricky code could introduce worse bugs that we may not immediately notice.

Thanks,
Ben

Quoted 3 lines

johannes

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
author
#4 lore raw

Re: [RFC 2/2] mac80211: Fix bss ref leak.

In reply to Johannes Berg
Quoted 8 lines · Johannes Berg

net/mac80211/mlme.c static void ieee80211_get_rates(struct ieee80211_supported_band *sband, · line 2403

Quoted 3 lines · On Mon, 2013-06-17 at 14:32 -0700, [email protected] wrote:

static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,

net/mac80211/mlme.c +1 -1
-					 bool assoc)
+					 bool assoc, bool put_bss)

Do we _really_ need another argument? Shouldn't it always be put in the non-assoc case anyway, at least if non-NULL?

I don't think so. Check out the ieee80211_rx_mgmt_assoc_resp method.

 	if (status_code != WLAN_STATUS_SUCCESS) {
		sdata_info(sdata, "%pM denied association (code=%d)\n",
			   mgmt->sa, status_code);
		ieee80211_destroy_assoc_data(sdata, false, false);

This passes in false as 'assoc', but we should not free bss here because it is being passed back to the calling method, and the return code of RX_MGMT_CFG80211_RX_ASSOC means bss should eventually be consumed by the cfg80211 logic.

Of course, this is all 'as far as I can tell'.

I sort of like the second boolean because it forces the caller to think about whether bss should be freed or not...

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
maintainer
#5 lore raw

Re: [RFC 2/2] mac80211: Fix bss ref leak.

Quoted 22 lines · Ben Greear
Quoted 8 lines · Johannes Berg

net/mac80211/mlme.c static void ieee80211_get_rates(struct ieee80211_supported_band *sband, · line 2403

Quoted 3 lines · On Mon, 2013-06-17 at 14:32 -0700, [email protected] wrote:

static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,

net/mac80211/mlme.c +1 -1
-					 bool assoc)
+					 bool assoc, bool put_bss)

Do we _really_ need another argument? Shouldn't it always be put in the non-assoc case anyway, at least if non-NULL?

I don't think so. Check out the ieee80211_rx_mgmt_assoc_resp method.

  	if (status_code != WLAN_STATUS_SUCCESS) {
		sdata_info(sdata, "%pM denied association (code=%d)\n",
			   mgmt->sa, status_code);
		ieee80211_destroy_assoc_data(sdata, false, false);

This passes in false as 'assoc', but we should not free bss here because it is being passed back to the calling method, and the return code of RX_MGMT_CFG80211_RX_ASSOC means bss should eventually be consumed by the cfg80211 logic.

Quoted 4 lines

Of course, this is all 'as far as I can tell'.

I sort of like the second boolean because it forces the caller to think about whether bss should be freed or not...

I just posted a patch to hand it back and never free it in mac80211 at all, that also allows cfg80211_hold_bss() it across the assoc process which fixes a (possibly more theoretical) expiry issue.

johannes