Skip to contentSkip to conversation

mac80211: correctly close cancelled scans

RFC In review Emmanuel Grumbach 3 participants 5 messages Sep 17, 2013 – Sep 30, 2013 mac80211

lore
Share & tools
lore
#0 lore raw

RFC 1/1 mac80211: correctly close cancelled scans

__ieee80211_scan_completed is called from a worker. This means that the following flow is possible.

* driver calls ieee80211_scan_completed
* mac80211 cancels the scan (that is already complete)
* __ieee80211_scan_complete runs

When scan_work will finally run, it will see that the scan hasn't been aborted and might even trigger another scan on another band. This leads to a situation where cfg80211's scan is not done and no further scan can be issued.

Fix this by setting a new flag when a HW scan is being cancelled so that no other scan will be triggered.

---
 net/mac80211/ieee80211_i.h |    3 +++
 net/mac80211/scan.c        |   22 +++++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
net/mac80211/ieee80211_i.h +3 -0
index d604e16..a523e5a 100644
@@ -893,6 +893,8 @@ struct tpt_led_trigger {
  *	that the scan completed.
  * @SCAN_ABORTED: Set for our scan work function when the driver reported
  *	a scan complete for an aborted scan.
+ * @SCAN_HW_CANCELLED: Set for our scan work function when the scan is being
+ *	cancelled.
  */
 enum {
 	SCAN_SW_SCANNING,
@@ -900,6 +902,7 @@ enum {
 	SCAN_ONCHANNEL_SCANNING,
 	SCAN_COMPLETED,
 	SCAN_ABORTED,
+	SCAN_HW_CANCELLED,
 };
 
 /**
net/mac80211/scan.c +21 -1
index 08afe74..67f81a4 100644
@@ -238,6 +238,9 @@ static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
 	enum ieee80211_band band;
 	int i, ielen, n_chans;
 
+	if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
+		return false;
+
 	do {
 		if (local->hw_scan_band == IEEE80211_NUM_BANDS)
 			return false;
@@ -940,11 +943,28 @@ void ieee80211_scan_cancel(struct ieee80211_local *local)
 	if (!local->scan_req)
 		goto out;
 
+	/*
+	 * We have a scan running and the driver already reported completion,
+	 * but the worker hasn't run yet or is stuck on the mutex - mark it as
+	 * cancelled.
+	 */
+	if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
+	    test_bit(SCAN_COMPLETED, &local->scanning)) {
+		set_bit(SCAN_HW_CANCELLED, &local->scanning);
+		goto out;
+	}
+
 	if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
-		if (local->ops->cancel_hw_scan)
+		/*
+		 * Make sure that __ieee80211_scan_completed doesn't trigger a
+		 * scan on another band.
+		 */
+		set_bit(SCAN_HW_CANCELLED, &local->scanning);
+		if (local->ops->cancel_hw_scan) {
 			drv_cancel_hw_scan(local,
 				rcu_dereference_protected(local->scan_sdata,
 						lockdep_is_held(&local->mtx)));
+		}
 		goto out;
 	}
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 17 lines · Emmanuel Grumbach

__ieee80211_scan_completed is called from a worker. This means that the following flow is possible.

* driver calls ieee80211_scan_completed
* mac80211 cancels the scan (that is already complete)
* __ieee80211_scan_complete runs

When scan_work will finally run, it will see that the scan hasn't been aborted and might even trigger another scan on another band. This leads to a situation where cfg80211's scan is not done and no further scan can be issued.

Fix this by setting a new flag when a HW scan is being cancelled so that no other scan will be triggered.

---

[...]

Quoted 4 lines
        if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
net/mac80211/scan.c +2 -1
-               if (local->ops->cancel_hw_scan)
+               /*
+                * Make sure that __ieee80211_scan_completed doesn't trigger a

net/mac80211/scan.c void ieee80211_scan_cancel(struct ieee80211_local *local) · line 969

Quoted 10 lines
net/mac80211/scan.c +5 -0
+                * scan on another band.
+                */
+               set_bit(SCAN_HW_CANCELLED, &local->scanning);
+               if (local->ops->cancel_hw_scan) {
                        drv_cancel_hw_scan(local,
                                rcu_dereference_protected(local->scan_sdata,
                                                lockdep_is_held(&local->mtx)));
+               }
                goto out;
        }

you don't seem to clear this flag anywhere...

Eliad.

#2 lore raw
Quoted 32 lines
Quoted 17 lines · Emmanuel Grumbach

__ieee80211_scan_completed is called from a worker. This means that the following flow is possible.

* driver calls ieee80211_scan_completed
* mac80211 cancels the scan (that is already complete)
* __ieee80211_scan_complete runs

When scan_work will finally run, it will see that the scan hasn't been aborted and might even trigger another scan on another band. This leads to a situation where cfg80211's scan is not done and no further scan can be issued.

Fix this by setting a new flag when a HW scan is being cancelled so that no other scan will be triggered.

---

[...]

Quoted 11 lines
        if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
net/mac80211/scan.c +6 -1
-               if (local->ops->cancel_hw_scan)
+               /*
+                * Make sure that __ieee80211_scan_completed doesn't trigger a
+                * scan on another band.
+                */
+               set_bit(SCAN_HW_CANCELLED, &local->scanning);
+               if (local->ops->cancel_hw_scan) {
                        drv_cancel_hw_scan(local,
                                rcu_dereference_protected(local->scan_sdata,
Quoted 7 lines
Quoted 4 lines

lockdep_is_held(&local->mtx)));
+ }

                goto out;
        }

you don't seem to clear this flag anywhere...

Yeah - just like SCAN_HW_SCANNING isn't cleared anywhere... but... in __ieee80211_scan_completed:

	local->scanning = 0;
	local->scan_chandef.chan = NULL;

yes I know. Don't ask.

#3 lore raw
Quoted 40 lines · Emmanuel
Quoted 32 lines
Quoted 17 lines · Emmanuel Grumbach

__ieee80211_scan_completed is called from a worker. This means that the following flow is possible.

* driver calls ieee80211_scan_completed
* mac80211 cancels the scan (that is already complete)
* __ieee80211_scan_complete runs

When scan_work will finally run, it will see that the scan hasn't been aborted and might even trigger another scan on another band. This leads to a situation where cfg80211's scan is not done and no further scan can be issued.

Fix this by setting a new flag when a HW scan is being cancelled so that no other scan will be triggered.

---

[...]

Quoted 11 lines
        if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
net/mac80211/scan.c +6 -1
-               if (local->ops->cancel_hw_scan)
+               /*
+                * Make sure that __ieee80211_scan_completed doesn't trigger a
+                * scan on another band.
+                */
+               set_bit(SCAN_HW_CANCELLED, &local->scanning);
+               if (local->ops->cancel_hw_scan) {
                        drv_cancel_hw_scan(local,
                                rcu_dereference_protected(local->scan_sdata,
Quoted 7 lines
Quoted 4 lines

lockdep_is_held(&local->mtx)));
+ }

                goto out;
        }

you don't seem to clear this flag anywhere...

Quoted 6 lines

Yeah - just like SCAN_HW_SCANNING isn't cleared anywhere... but... in __ieee80211_scan_completed:

        local->scanning = 0;
        local->scan_chandef.chan = NULL;

yes I know. Don't ask.

oh, right, i overlooked it :)

Eliad.

maintainer
#4 lore raw
In reply to Emmanuel Grumbach
Quoted 7 lines · Emmanuel Grumbach

__ieee80211_scan_completed is called from a worker. This means that the following flow is possible.

* driver calls ieee80211_scan_completed
* mac80211 cancels the scan (that is already complete)
* __ieee80211_scan_complete runs

Quoted 7 lines

When scan_work will finally run, it will see that the scan hasn't been aborted and might even trigger another scan on another band. This leads to a situation where cfg80211's scan is not done and no further scan can be issued.

Fix this by setting a new flag when a HW scan is being cancelled so that no other scan will be triggered.

I'll apply this

net/mac80211/scan.c void ieee80211_scan_cancel(struct ieee80211_local *local) · line 967

Quoted 5 lines
net/mac80211/scan.c +2 -0
+		if (local->ops->cancel_hw_scan) {
 			drv_cancel_hw_scan(local,
 				rcu_dereference_protected(local->scan_sdata,
 						lockdep_is_held(&local->mtx)));
+		}

WIthout the extra braces.

johannes