Skip to contentSkip to conversation

mac80211: Fixing races for hw crypto skipping tailroom

PATCH In review Yogesh Ashok Powar 4 participants 25 messages Jun 16, 2011 – Jun 27, 2011 mac80211

lore
Share & tools
lore
#0 lore raw

cover mac80211: Fixing races for hw crypto skipping tailroom

Yogesh Ashok Powar (2):
Revert "Revert "mac80211: Skip tailroom reservation for full
HW-crypto devices""
mac80211: Fixing Races for skipping tailroom reservation

 net/mac80211/ieee80211_i.h |    7 ++++++
 net/mac80211/key.c         |   46 ++++++++++++++++++++++++++++++++++++++++++-
 net/mac80211/main.c        |    2 +
 net/mac80211/tx.c          |   13 ++++++-----
 4 files changed, 60 insertions(+), 8 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/2 Revert "Revert "mac80211: Skip tailroom reservation for full HW-crypto devices""

This reverts commit ab6a44ce1da48d35fe7ec95fa068aa617bd7e8dd.

---
 net/mac80211/ieee80211_i.h |    3 +++
 net/mac80211/key.c         |   21 +++++++++++++++++++--
 net/mac80211/tx.c          |    7 +------
 3 files changed, 23 insertions(+), 8 deletions(-)
net/mac80211/ieee80211_i.h +3 -0
index 090b0ec..2025af5 100644
@@ -775,6 +775,9 @@ struct ieee80211_local {
 
 	int tx_headroom; /* required headroom for hardware/radiotap */
 
+	/* count for keys needing tailroom space allocation */
+	int crypto_tx_tailroom_needed_cnt;
+
 	/* Tasklet and skb queue to process calls from IRQ mode. All frames
 	 * added to skb_queue will be processed, but frames in
 	 * skb_queue_unreliable may be dropped if the total length of these
net/mac80211/key.c +19 -2
index f825e2f..31afd712 100644
@@ -101,6 +101,11 @@ static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
 
 	if (!ret) {
 		key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
+
+		if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
+		      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
+			key->local->crypto_tx_tailroom_needed_cnt--;
+
 		return 0;
 	}
 
@@ -156,6 +161,10 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
 			  key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
 
 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
+
+	if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
+	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
+		key->local->crypto_tx_tailroom_needed_cnt++;
 }
 
 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
@@ -394,8 +403,10 @@ static void __ieee80211_key_destroy(struct ieee80211_key *key)
 		ieee80211_aes_key_free(key->u.ccmp.tfm);
 	if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
-	if (key->local)
+	if (key->local) {
 		ieee80211_debugfs_key_remove(key);
+		key->local->crypto_tx_tailroom_needed_cnt--;
+	}
 
 	kfree(key);
 }
@@ -457,6 +468,8 @@ int ieee80211_key_link(struct ieee80211_key *key,
 
 	ieee80211_debugfs_key_add(key);
 
+	key->local->crypto_tx_tailroom_needed_cnt++;
+
 	ret = ieee80211_key_enable_hw_accel(key);
 
 	mutex_unlock(&sdata->local->key_mtx);
@@ -498,8 +511,12 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	list_for_each_entry(key, &sdata->key_list, list)
+	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
+
+	list_for_each_entry(key, &sdata->key_list, list) {
+		sdata->local->crypto_tx_tailroom_needed_cnt++;
 		ieee80211_key_enable_hw_accel(key);
+	}
 
 	mutex_unlock(&sdata->local->key_mtx);
 }
net/mac80211/tx.c +1 -6
index 3104c84..64e0f75 100644
@@ -1480,12 +1480,7 @@ static int ieee80211_skb_resize(struct ieee80211_local *local,
 {
 	int tail_need = 0;
 
-	/*
-	 * This could be optimised, devices that do full hardware
-	 * crypto (including TKIP MMIC) need no tailroom... But we
-	 * have no drivers for such devices currently.
-	 */
-	if (may_encrypt) {
+	if (may_encrypt && local->crypto_tx_tailroom_needed_cnt) {
 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
 		tail_need -= skb_tailroom(skb);
 		tail_need = max_t(int, tail_need, 0);
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 2/2 mac80211: Fixing Races for skipping tailroom reservation

In reply to Yogesh Ashok Powar

Following warning was observed after the commit aac6af5534fade2b18682a0b9efad1a6c04c34c6

Quoted 1 line
WARNING: at net/mac80211/wpa.c:397 ccmp_encrypt_skb+0xc4/0x1f0

Consider a scenario where reserving skb tailroom is skipped because software encryption is not enabled. SW encryption can be disabled because of a) All the keys are hardware planted b) No key has been created. But, before actual transmit if hardware encryption is disabled or software encryption is started, there will not be enough tailroom space to accommodate the sw crypto's MMIC or IV and WARN_ON will be hit.

This race between updations of hw keys and skipping & using tailroom, is fixed by protecting critical regions (code accessing crypto_tx_tailroom_needed_cnt and code from tailroom skipping to skb_put for IV or MMIC) with the spinlock.

---
 net/mac80211/ieee80211_i.h |    6 +++++-
 net/mac80211/key.c         |   27 ++++++++++++++++++++++++++-
 net/mac80211/main.c        |    2 ++
 net/mac80211/tx.c          |    6 ++++++
 4 files changed, 39 insertions(+), 2 deletions(-)
net/mac80211/ieee80211_i.h +5 -1
index 2025af5..844d385 100644
@@ -775,8 +775,12 @@ struct ieee80211_local {
 
 	int tx_headroom; /* required headroom for hardware/radiotap */
 
-	/* count for keys needing tailroom space allocation */
+	/*
+	 * count for keys needing tailroom space allocation,
+	 * its access is protected by spinlock tailroom_skp (see below)
+	 */
 	int crypto_tx_tailroom_needed_cnt;
+	spinlock_t tailroom_skip;
 
 	/* Tasklet and skb queue to process calls from IRQ mode. All frames
 	 * added to skb_queue will be processed, but frames in
net/mac80211/key.c +26 -1
index 31afd712..b78ef6f 100644
@@ -104,8 +104,13 @@ static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
 
 		if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
 		      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
+
+			spin_lock(&key->local->tailroom_skip);
+
 			key->local->crypto_tx_tailroom_needed_cnt--;
 
+			spin_unlock(&key->local->tailroom_skip);
+
 		return 0;
 	}
 
@@ -163,8 +168,14 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
 
 	if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
-	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
+	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))) {
+
+		spin_lock(&key->local->tailroom_skip);
+
 		key->local->crypto_tx_tailroom_needed_cnt++;
+
+		spin_unlock(&key->local->tailroom_skip);
+	}
 }
 
 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
@@ -405,7 +416,12 @@ static void __ieee80211_key_destroy(struct ieee80211_key *key)
 		ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
 	if (key->local) {
 		ieee80211_debugfs_key_remove(key);
+
+		spin_lock(&key->local->tailroom_skip);
+
 		key->local->crypto_tx_tailroom_needed_cnt--;
+
+		spin_unlock(&key->local->tailroom_skip);
 	}
 
 	kfree(key);
@@ -468,8 +484,13 @@ int ieee80211_key_link(struct ieee80211_key *key,
 
 	ieee80211_debugfs_key_add(key);
 
+
+	spin_lock(&key->local->tailroom_skip);
+
 	key->local->crypto_tx_tailroom_needed_cnt++;
 
+	spin_unlock(&key->local->tailroom_skip);
+
 	ret = ieee80211_key_enable_hw_accel(key);
 
 	mutex_unlock(&sdata->local->key_mtx);
@@ -511,6 +532,8 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
+	spin_lock(&sdata->local->tailroom_skip);
+
 	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
 
 	list_for_each_entry(key, &sdata->key_list, list) {
@@ -518,6 +541,8 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 		ieee80211_key_enable_hw_accel(key);
 	}
 
+	spin_unlock(&sdata->local->tailroom_skip);
+
 	mutex_unlock(&sdata->local->key_mtx);
 }
 
net/mac80211/main.c +2 -0
index 866f269..4506ed3 100644
@@ -625,6 +625,8 @@ struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
 	spin_lock_init(&local->filter_lock);
 	spin_lock_init(&local->queue_stop_reason_lock);
 
+	spin_lock_init(&local->tailroom_skip);
+
 	/*
 	 * The rx_skb_queue is only accessed from tasklets,
 	 * but other SKB queues are used from within IRQ
net/mac80211/tx.c +6 -0
index 64e0f75..5ea1baa 100644
@@ -1573,8 +1573,11 @@ static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
 	headroom -= skb_headroom(skb);
 	headroom = max_t(int, 0, headroom);
 
+	spin_lock(&local->tailroom_skip);
+
 	if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
 		dev_kfree_skb(skb);
+		spin_unlock(&local->tailroom_skip);
 		rcu_read_unlock();
 		return;
 	}
@@ -1587,12 +1590,15 @@ static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
 		!is_multicast_ether_addr(hdr->addr1))
 			if (mesh_nexthop_lookup(skb, sdata)) {
 				/* skb queued: don't free */
+				spin_unlock(&local->tailroom_skip);
 				rcu_read_unlock();
 				return;
 			}
 
 	ieee80211_set_qos_hdr(local, skb);
 	ieee80211_tx(sdata, skb, false);
+
+	spin_unlock(&local->tailroom_skip);
 	rcu_read_unlock();
 }
 
Signature · 1 line
1.7.5.4
maintainer
#3 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 14 lines · Yogesh Ashok Powar

Following warning was observed after the commit aac6af5534fade2b18682a0b9efad1a6c04c34c6

Quoted 1 line
WARNING: at net/mac80211/wpa.c:397 ccmp_encrypt_skb+0xc4/0x1f0

Consider a scenario where reserving skb tailroom is skipped because software encryption is not enabled. SW encryption can be disabled because of a) All the keys are hardware planted b) No key has been created. But, before actual transmit if hardware encryption is disabled or software encryption is started, there will not be enough tailroom space to accommodate the sw crypto's MMIC or IV and WARN_ON will be hit.

Quoted 5 lines

This race between updations of hw keys and skipping & using tailroom, is fixed by protecting critical regions (code accessing crypto_tx_tailroom_needed_cnt and code from tailroom skipping to skb_put for IV or MMIC) with the spinlock.

Haha, good joke. You've got to be kidding. NACK. No inserting spinlocks into the TX/RX paths.

johannes

#4 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 22 lines · Johannes Berg
Quoted 14 lines · Yogesh Ashok Powar

Following warning was observed after the commit aac6af5534fade2b18682a0b9efad1a6c04c34c6

Quoted 1 line
WARNING: at net/mac80211/wpa.c:397 ccmp_encrypt_skb+0xc4/0x1f0

Consider a scenario where reserving skb tailroom is skipped because software encryption is not enabled. SW encryption can be disabled because of a) All the keys are hardware planted b) No key has been created. But, before actual transmit if hardware encryption is disabled or software encryption is started, there will not be enough tailroom space to accommodate the sw crypto's MMIC or IV and WARN_ON will be hit.

Quoted 5 lines

This race between updations of hw keys and skipping & using tailroom, is fixed by protecting critical regions (code accessing crypto_tx_tailroom_needed_cnt and code from tailroom skipping to skb_put for IV or MMIC) with the spinlock.

Quoted 2 lines

Haha, good joke. You've got to be kidding. NACK. No inserting spinlocks into the TX/RX paths.

Using spinlocks in TX/RX path is not allowed because spinlocks will make TX/RX path to block and its illegal to block while in an RCU read-side critical section. I think, I got the joke here :(.

In my opinion, following should be the fix for the races explained above

a) The call synchronize_rcu() should be present before enabling sw encryption :
This will make pre-existing RCU readers in RCU readside critical section to complete. Or, SKBs which have already skipped tailroom to xmit before the end of synchronize_rcu's grace period.

b) Avoid tailroom skip check for RCU readside critical sections that begin inside the grace period :
This will make new entrants to mandatory allocating tailroom space irrespective of sw encryption (or hw encryption with generate IV/MMIC flags set) and hence avoid the WARN_ON.

Implementing first part is straight forward.
For later part, RCU readers must be aware of on going grace period. I am currently not sure of any way to implement this. May be iff atomic read is allowed inside RCU read-side critical sections, then before entering the grace period, updater can set a atomic flag and unset it once done; and RCU reader can check this atomic flag to avoid the __tailroom skip check__.

Please suggest your opinion.

Thanks
Yogesh

maintainer
#5 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 3 lines

Using spinlocks in TX/RX path is not allowed because spinlocks will make TX/RX path to block and its illegal to block while in an RCU read-side critical section. I think, I got the joke here :(.

No, that'd be allowed, but you can't spinlock all the TX path because of performance.

I think the way to solve it would be to use synchronize_net() somehow maybe; not really sure.

Johannes

#6 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 7 lines · Johannes Berg
Quoted 3 lines

Using spinlocks in TX/RX path is not allowed because spinlocks will make TX/RX path to block and its illegal to block while in an RCU read-side critical section. I think, I got the joke here :(.

No, that'd be allowed, but you can't spinlock all the TX path because of performance.

I think the way to solve it would be to use synchronize_net() somehow maybe; not really sure.

Following patch avoids tailroom skip check for RCU readside critical sections that begin inside the synchronize_rcu's grace period, without grabbing any lock in the TX patch and there by avoiding the race conditions.

I will request Andreas to test this patch on his testbed if the changes are fine with you.

Thanks
Yogesh

net/mac80211/ieee80211_i.h +1 -0
index 2025af5..56c7751b 100644
@@ -777,6 +777,7 @@ struct ieee80211_local {
 
 	/* count for keys needing tailroom space allocation */
 	int crypto_tx_tailroom_needed_cnt;
+	struct mutex tailroom_skip;
 
 	/* Tasklet and skb queue to process calls from IRQ mode. All frames
 	 * added to skb_queue will be processed, but frames in
net/mac80211/key.c +21 -1
index 31afd712..d59837a 100644
@@ -390,14 +390,20 @@ static void __ieee80211_key_destroy(struct ieee80211_key *key)
 	if (!key)
 		return;
 
+	if (key->local && !(key->local->crypto_tx_tailroom_needed_cnt))
+		mutex_lock(&key->local->tailroom_skip);
+
 	/*
 	 * Synchronize so the TX path can no longer be using
 	 * this key before we free/remove it.
 	 */
 	synchronize_rcu();
 
-	if (key->local)
+	if (key->local) {
 		ieee80211_key_disable_hw_accel(key);
+		if (mutex_is_locked(&key->local->tailroom_skip))
+			mutex_unlock(&key->local->tailroom_skip);
+	}
 
 	if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
 		ieee80211_aes_key_free(key->u.ccmp.tfm);
@@ -468,8 +474,16 @@ int ieee80211_key_link(struct ieee80211_key *key,
 
 	ieee80211_debugfs_key_add(key);
 
+	if (!key->local->crypto_tx_tailroom_needed_cnt) {
+		mutex_lock(&key->local->tailroom_skip);
+		synchronize_rcu();
+	}
+
 	key->local->crypto_tx_tailroom_needed_cnt++;
 
+	if (mutex_is_locked(&key->local->tailroom_skip))
+		mutex_unlock(&key->local->tailroom_skip);
+
 	ret = ieee80211_key_enable_hw_accel(key);
 
 	mutex_unlock(&sdata->local->key_mtx);
@@ -513,11 +527,17 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
 
+	mutex_lock(&sdata->local->tailroom_skip);
+
+	synchronize_rcu();
+
 	list_for_each_entry(key, &sdata->key_list, list) {
 		sdata->local->crypto_tx_tailroom_needed_cnt++;
 		ieee80211_key_enable_hw_accel(key);
 	}
 
+	mutex_unlock(&sdata->local->tailroom_skip);
+
 	mutex_unlock(&sdata->local->key_mtx);
 }
 
net/mac80211/main.c +2 -0
index 866f269..c70b26d 100644
@@ -625,6 +625,8 @@ struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
 	spin_lock_init(&local->filter_lock);
 	spin_lock_init(&local->queue_stop_reason_lock);
 
+	mutex_init(&local->tailroom_skip);
+
 	/*
 	 * The rx_skb_queue is only accessed from tasklets,
 	 * but other SKB queues are used from within IRQ
net/mac80211/tx.c +2 -1
index 64e0f75..ce2ee4a 100644
@@ -1480,7 +1480,8 @@ static int ieee80211_skb_resize(struct ieee80211_local *local,
 {
 	int tail_need = 0;
 
-	if (may_encrypt && local->crypto_tx_tailroom_needed_cnt) {
+	if (may_encrypt && (local->crypto_tx_tailroom_needed_cnt ||
+				mutex_is_locked(&local->tailroom_skip))) {
 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
 		tail_need -= skb_tailroom(skb);
 		tail_need = max_t(int, tail_need, 0);
maintainer
#7 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 9 lines · Yogesh Ashok Powar
Quoted 7 lines · Johannes Berg
Quoted 3 lines

Using spinlocks in TX/RX path is not allowed because spinlocks will make TX/RX path to block and its illegal to block while in an RCU read-side critical section. I think, I got the joke here :(.

No, that'd be allowed, but you can't spinlock all the TX path because of performance.

I think the way to solve it would be to use synchronize_net() somehow maybe; not really sure.

Quoted 7 lines

Following patch avoids tailroom skip check for RCU readside critical sections that begin inside the synchronize_rcu's grace period, without grabbing any lock in the TX patch and there by avoiding the race conditions.

I will request Andreas to test this patch on his testbed if the changes are fine with you.

No, they're not, the mutex trickery seems completely pointless, and the conditional (on _is_locked no less) locking is horrible.

johannes

#8 lore raw

RE: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 2 lines

No, they're not, the mutex trickery seems completely pointless, and the conditional (on _is_locked no less) locking is horrible.

Ok.

Will spend some more time on exploring solutions using rcu primitives only (synchronize_net or something similar).

If nothing of only-RCU is feasible then, I think, we need to resize the skb if it has already skipped the tailroom instead of WARN_ON. This will come in to picuture only during the race cases. But this wont get rid of race.

Thanks
Yogesh

maintainer
#9 lore raw

RE: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 10 lines · Yogesh Powar
Quoted 2 lines

No, they're not, the mutex trickery seems completely pointless, and the conditional (on _is_locked no less) locking is horrible.

Ok.

Will spend some more time on exploring solutions using rcu primitives only (synchronize_net or something similar).

If nothing of only-RCU is feasible then, I think, we need to resize the skb if it has already skipped the tailroom instead of WARN_ON. This will come in to picuture only during the race cases. But this wont get rid of race.

Ahrg, seriously, just analyse the situation for once. Do I really need to do that?

There are two race cases, during transitions:
1) need to resize -> no need to (key moved to HW)
2) no need to resize -> need to (new key, ...)

The first is uninteresting, it's fine if the race happens and the skb is still resized even if it didn't need to.

The second one is what's causing issues. But the race happens like this:
- counter is 0
- skb goes through tx start, no tailroom allocated
- key added, counter > 0
- key lookup while TX processing is successful

So ... how can you solve it? Clearly, the locking attempts you made were pretty useless. But now that we understand the race, can we fix it? I think we can, by doing something like this:

	counter++;
	/* flush packets being processed */
	synchronize_net();
	/* do whatever causes the key to be found by sw crypto */

That should be it. The only overhead is a little bit in control paths for key settings which are delayed a bit by synchronize_net(), but who cares, we don't change keys every packet.

Of course you need to encapsulate that code into a small function and write comments about why it's necessary and how it works.

Was that so hard? :-)

johannes

#10 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 37 lines · Johannes Berg
Quoted 10 lines · Yogesh Powar
Quoted 2 lines

No, they're not, the mutex trickery seems completely pointless, and the conditional (on _is_locked no less) locking is horrible.

Ok.

Will spend some more time on exploring solutions using rcu primitives only (synchronize_net or something similar).

If nothing of only-RCU is feasible then, I think, we need to resize the skb if it has already skipped the tailroom instead of WARN_ON. This will come in to picuture only during the race cases. But this wont get rid of race.

Ahrg, seriously, just analyse the situation for once. Do I really need to do that?

There are two race cases, during transitions:
1) need to resize -> no need to (key moved to HW)
2) no need to resize -> need to (new key, ...)

The first is uninteresting, it's fine if the race happens and the skb is still resized even if it didn't need to.

The second one is what's causing issues. But the race happens like this:
- counter is 0
- skb goes through tx start, no tailroom allocated
- key added, counter > 0
- key lookup while TX processing is successful

So ... how can you solve it? Clearly, the locking attempts you made were pretty useless. But now that we understand the race, can we fix it? I think we can, by doing something like this:

	counter++;
	/* flush packets being processed */
	synchronize_net();
	/* do whatever causes the key to be found by sw crypto */
Quoted 8 lines

That should be it. The only overhead is a little bit in control paths for key settings which are delayed a bit by synchronize_net(), but who cares, we don't change keys every packet.

Of course you need to encapsulate that code into a small function and write comments about why it's necessary and how it works.

Was that so hard? :-)

:(

Following is the implementation based on your design.

Let me know if I miss anything here.

Thanks
Yogesh

---
 net/mac80211/key.c |   43 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 37 insertions(+), 6 deletions(-)
net/mac80211/key.c +37 -6
index 31afd71..4e0be17 100644
@@ -61,6 +61,36 @@ static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
 	return NULL;
 }
 
+static void increment_tailroom_need_count(struct ieee80211_local *local)
+{
+	/*
+	 * When this count is zero, SKB resizing for allocating tailroom
+	 * for IV or MMIC is skipped. But, this check has created two race
+	 * cases in xmit path while transiting from zero count to one:
+	 *
+	 * 1. SKB resize was skipped because no key was added but just before
+	 * the xmit key is added and SW encryption kicks off.
+	 *
+	 * 2. SKB resize was skipped because all the keys were hw planted but
+	 * just before xmit one of the key is deleted and SW encryption kicks
+	 * off.
+	 *
+	 * In both the above case SW encryption will find not enough space for
+	 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
+	 *
+	 * Solution has been explained at
+	 * http://markmail.org/message/c3ykchyv6azzmdum
+	 */
+
+	if (!local->crypto_tx_tailroom_needed_cnt++) {
+		/*
+		 * Flush all XMIT packets currently using HW encryption or no
+		 * encryption at all if the count transition is from 0 -> 1.
+		 */
+		synchronize_net();
+	}
+}
+
 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
 {
 	struct ieee80211_sub_if_data *sdata;
@@ -144,6 +174,10 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
 	if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
 		return;
 
+	if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
+	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
+		increment_tailroom_need_count(key->local);
+
 	sta = get_sta_for_key(key);
 	sdata = key->sdata;
 
@@ -162,9 +196,6 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
 
 	key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
 
-	if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
-	      (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
-		key->local->crypto_tx_tailroom_needed_cnt++;
 }
 
 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
@@ -466,9 +497,9 @@ int ieee80211_key_link(struct ieee80211_key *key,
 	__ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
 	__ieee80211_key_destroy(old_key);
 
-	ieee80211_debugfs_key_add(key);
+	increment_tailroom_need_count(key->local);
 
-	key->local->crypto_tx_tailroom_needed_cnt++;
+	ieee80211_debugfs_key_add(key);
 
 	ret = ieee80211_key_enable_hw_accel(key);
 
@@ -514,7 +545,7 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
 
 	list_for_each_entry(key, &sdata->key_list, list) {
-		sdata->local->crypto_tx_tailroom_needed_cnt++;
+		increment_tailroom_need_count(sdata->local);
 		ieee80211_key_enable_hw_accel(key);
 	}
 
Signature · 1 line
1.5.4.1
maintainer
#11 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 3 lines · Yogesh Ashok Powar

Following is the implementation based on your design.

Let me know if I miss anything here.

Since your original code was reverted you should send a full patch. But overall it looks fine. Came to think of it yesterday though, for the 0->1 case it's of course necessary to do the adjustment before the key can be found, but also for the 1->0 case it'll be necessary to do the counter adjustment after the key adjustments.

johannes

#12 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 8 lines · Johannes Berg
Quoted 3 lines · Yogesh Ashok Powar

Following is the implementation based on your design.

Let me know if I miss anything here.

Since your original code was reverted you should send a full patch. But overall it looks fine.

Thanks

Quoted 4 lines

Came to think of it yesterday though, for the
0->1 case it's of course necessary to do the adjustment before the key can be found, but also for the 1->0 case it'll be necessary to do the counter adjustment after the key adjustments.

Right.
I reconfirmed that the current code handles these cases.

Following is the complete patch.

Andreas will it be possible for you to test this patch on your setup?

Will send an official patch once Andreas confirms things are working.

Thanks
Yogesh

---
 net/mac80211/ieee80211_i.h |    3 ++
 net/mac80211/key.c         |   51 ++++++++++++++++++++++++++++++++++++++++++-
 net/mac80211/tx.c          |    7 +-----
 3 files changed, 53 insertions(+), 8 deletions(-)
net/mac80211/ieee80211_i.h +3 -0
index 090b0ec..2025af5 100644
@@ -775,6 +775,9 @@ struct ieee80211_local {
 
 	int tx_headroom; /* required headroom for hardware/radiotap */
 
+	/* count for keys needing tailroom space allocation */
+	int crypto_tx_tailroom_needed_cnt;
+
 	/* Tasklet and skb queue to process calls from IRQ mode. All frames
 	 * added to skb_queue will be processed, but frames in
 	 * skb_queue_unreliable may be dropped if the total length of these
net/mac80211/key.c +49 -2

89 lines not on this page · raw message

net/mac80211/tx.c +1 -6
index 3104c84..64e0f75 100644
@@ -1480,12 +1480,7 @@ static int ieee80211_skb_resize(struct ieee80211_local *local,
 {
 	int tail_need = 0;
 
-	/*
-	 * This could be optimised, devices that do full hardware
-	 * crypto (including TKIP MMIC) need no tailroom... But we
-	 * have no drivers for such devices currently.
-	 */
-	if (may_encrypt) {
+	if (may_encrypt && local->crypto_tx_tailroom_needed_cnt) {
 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
 		tail_need -= skb_tailroom(skb);
 		tail_need = max_t(int, tail_need, 0);
Signature · 1 line
1.5.4.1
maintainer
#13 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

net/mac80211/key.c int ieee80211_key_link(struct ieee80211_key *key, · line 500

Quoted 6 lines · Yogesh Ashok Powar
net/mac80211/key.c +2 -0
@@ -455,6 +496,8 @@ int ieee80211_key_link(struct ieee80211_key *key,
 	__ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
 	__ieee80211_key_destroy(old_key);
 
+	increment_tailroom_need_count(key->local);
+

This doesn't seem right -- it links the key in first and then does the update, the mechanism I described relies on doing it the other way around.

net/mac80211/key.c void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) · line 545

Quoted 7 lines
net/mac80211/key.c +2 -1
@@ -498,8 +541,12 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	list_for_each_entry(key, &sdata->key_list, list)
+	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
+

That doesn't seem right either -- only if you have a single sdata that will work, I think?

johannes

#14 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 6 lines · Johannes Berg
Quoted 4 lines · Yogesh Ashok Powar
(quoted diff) +0 -0
@@ -455,6 +496,8 @@ int ieee80211_key_link(struct ieee80211_key *key,
 	__ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
 	__ieee80211_key_destroy(old_key);
 
Quoted 6 lines

Review int ieee80211_key_link(struct ieee80211_key *key, · line 500

Quoted 2 lines
(quoted diff) +2 -0
+	increment_tailroom_need_count(key->local);
+

This doesn't seem right -- it links the key in first and then does the update, the mechanism I described relies on doing it the other way around.

In that case we should have something like this

@@ -493,11 +493,11 @@ int ieee80211_key_link(struct ieee80211_key *key,
else
old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);

+ increment_tailroom_need_count(key->local);
+
__ieee80211_key_replace(sdata, sta, pairwise, old_key, key); __ieee80211_key_destroy(old_key);

- increment_tailroom_need_count(key->local);
-
ieee80211_debugfs_key_add(key);

ret = ieee80211_key_enable_hw_accel(key);

Quoted 11 lines

Review void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) · line 545

Quoted 8 lines
(quoted diff) +2 -1
@@ -498,8 +541,12 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	list_for_each_entry(key, &sdata->key_list, list)
+	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
+

That doesn't seem right either -- only if you have a single sdata that will work, I think?

Right. For multiple sdata count will be over written to 0.

I think, following should fix this

Thanks
Yogesh

net/mac80211/key.c +0 -2
index edf9f40..0bf450d 100644
@@ -541,8 +541,6 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
-
 	list_for_each_entry(key, &sdata->key_list, list) {
 		increment_tailroom_need_count(sdata->local);
 		ieee80211_key_enable_hw_accel(key);
net/mac80211/util.c +3 -0
index 05e3fb8..bef3bdd 100644
@@ -1271,6 +1271,9 @@ int ieee80211_reconfig(struct ieee80211_local *local)
 		mutex_unlock(&local->sta_mtx);
 	}
 
+	/* Reset tailroom skip count */
+	local->crypto_tx_tailroom_needed_cnt = 0;
+
 	/* add back keys */
 	list_for_each_entry(sdata, &local->interfaces, list)
 		if (ieee80211_sdata_running(sdata))
#15 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Hi,

I'll test the patch as far as it is ready :-). Could you please create a complete patch, which runs against a clean compat-wireless-3.0-rc1-1 (or compat-wireless-2011-06-15)? And maybe, for testing purpose, a log entry, which makes it possible to check, whether the code is really used (if it doesn't flood messages)?

Thanks,
Andreas

#16 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

In reply to Yogesh Ashok Powar
Quoted 37 lines
Quoted 10 lines

net/mac80211/key.c void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) · line 545

Quoted 7 lines
net/mac80211/key.c +2 -1
@@ -498,8 +541,12 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	list_for_each_entry(key, &sdata->key_list, list)
+	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
+

That doesn't seem right either -- only if you have a single sdata that will work, I think?

Right. For multiple sdata count will be over written to 0.

I think, following should fix this

Thanks
Yogesh

net/mac80211/key.c +0 -2
index edf9f40..0bf450d 100644
@@ -541,8 +541,6 @@ void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
 
 	mutex_lock(&sdata->local->key_mtx);
 
-	sdata->local->crypto_tx_tailroom_needed_cnt = 0;
-
 	list_for_each_entry(key, &sdata->key_list, list) {
 		increment_tailroom_need_count(sdata->local);
 		ieee80211_key_enable_hw_accel(key);
net/mac80211/util.c +0 -0
index 05e3fb8..bef3bdd 100644

net/mac80211/util.c int ieee80211_reconfig(struct ieee80211_local *local) · line 1279

Quoted 9 lines
net/mac80211/util.c +3 -0
@@ -1271,6 +1271,9 @@ int ieee80211_reconfig(struct ieee80211_local *local)
 		mutex_unlock(&local->sta_mtx);
 	}
 
+	/* Reset tailroom skip count */
+	local->crypto_tx_tailroom_needed_cnt = 0;
+
 	/* add back keys */
 	list_for_each_entry(sdata, &local->interfaces, list)
 		if (ieee80211_sdata_running(sdata))

Above logic will not work if one the sdatas fails to enable keys may be because of sdata not running.

Will work on some other logic.

Thanks
Yogesh

#17 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 1 line

Will work on some other logic.

Following is the complete V2-patch

v2 changes: a) Moved counter++ before __ieee80211_key_replace in
key_link()
b) Moved crypto_tx_tailroom_needed_cnt to sdata resolve issue with multiple sdata instances in hw reset.

Thanks
Yogesh

---
 net/mac80211/ieee80211_i.h |    3 ++
 net/mac80211/key.c         |   51 ++++++++++++++++++++++++++++++++++++++++++-
 net/mac80211/tx.c          |   14 ++++-------
 3 files changed, 57 insertions(+), 11 deletions(-)
net/mac80211/ieee80211_i.h +3 -0
index 090b0ec..74c2f62 100644
@@ -544,6 +544,9 @@ struct ieee80211_sub_if_data {
 	/* keys */
 	struct list_head key_list;
 
+	/* count for keys needing tailroom space allocation */
+	int crypto_tx_tailroom_needed_cnt;
+
 	struct net_device *dev;
 	struct ieee80211_local *local;
 
net/mac80211/key.c +49 -2

89 lines not on this page · raw message

net/mac80211/tx.c +5 -9
index 3104c84..e8d0d2d 100644
@@ -1474,18 +1474,14 @@ static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
 
 /* device xmit handlers */
 
-static int ieee80211_skb_resize(struct ieee80211_local *local,
+static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
 				struct sk_buff *skb,
 				int head_need, bool may_encrypt)
 {
+	struct ieee80211_local *local = sdata->local;
 	int tail_need = 0;
 
-	/*
-	 * This could be optimised, devices that do full hardware
-	 * crypto (including TKIP MMIC) need no tailroom... But we
-	 * have no drivers for such devices currently.
-	 */
-	if (may_encrypt) {
+	if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
 		tail_need -= skb_tailroom(skb);
 		tail_need = max_t(int, tail_need, 0);
@@ -1578,7 +1574,7 @@ static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
 	headroom -= skb_headroom(skb);
 	headroom = max_t(int, 0, headroom);
 
-	if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
+	if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
 		dev_kfree_skb(skb);
 		rcu_read_unlock();
 		return;
@@ -1945,7 +1941,7 @@ netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
 		head_need += IEEE80211_ENCRYPT_HEADROOM;
 		head_need += local->tx_headroom;
 		head_need = max_t(int, 0, head_need);
-		if (ieee80211_skb_resize(local, skb, head_need, true))
+		if (ieee80211_skb_resize(sdata, skb, head_need, true))
 			goto fail;
 	}
 
Signature · 1 line
1.5.4.1
maintainer
#18 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 8 lines · Yogesh Ashok Powar
Quoted 1 line

Will work on some other logic.

Following is the complete V2-patch

v2 changes: a) Moved counter++ before __ieee80211_key_replace in
key_link()
b) Moved crypto_tx_tailroom_needed_cnt to sdata resolve issue with multiple sdata instances in hw reset.

Looks good. Now I'm just worried about memory and compiler barriers that may be needed so the counter update doesn't move after anything else... Hmm.

johannes

#19 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 5 lines · Johannes Berg
Quoted 4 lines · Yogesh Ashok Powar
Quoted 1 line

Will work on some other logic.

Following is the complete V2-patch

Quoted 8 lines
Quoted 4 lines

v2 changes: a) Moved counter++ before __ieee80211_key_replace in
key_link()
b) Moved crypto_tx_tailroom_needed_cnt to sdata resolve issue with multiple sdata instances in hw reset.

Looks good. Now I'm just worried about memory and compiler barriers that may be needed so the counter update doesn't move after anything else... Hmm.

I some how feel that synchronize_net may be replaced with synchronize_rcu and still the race wont happen.

So the new logic would be
- counter++ <-- Here keys are not added or deleted
so rcu readers wont have problem with extra space allocated.

- synchronize_rcu <-- This will flush existing readers. Again
new readers will have no problem with extra space allocated.

Let me know your opinion on this.

Thanks
Yogesh

maintainer
#20 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 23 lines · Yogesh Ashok Powar
Quoted 5 lines · Johannes Berg
Quoted 4 lines · Yogesh Ashok Powar
Quoted 1 line

Will work on some other logic.

Following is the complete V2-patch

Quoted 8 lines
Quoted 4 lines

v2 changes: a) Moved counter++ before __ieee80211_key_replace in
key_link()
b) Moved crypto_tx_tailroom_needed_cnt to sdata resolve issue with multiple sdata instances in hw reset.

Looks good. Now I'm just worried about memory and compiler barriers that may be needed so the counter update doesn't move after anything else... Hmm.

I some how feel that synchronize_net may be replaced with synchronize_rcu and still the race wont happen.

So the new logic would be
- counter++ <-- Here keys are not added or deleted
so rcu readers wont have problem with extra space allocated.

Quoted 5 lines

- synchronize_rcu <-- This will flush existing readers. Again
new readers will have no problem with extra space allocated.

Let me know your opinion on this.

It doesn't really matter -- synchronize_net() is exactly the same as synchronize_rcu().

What I'm worried about is that there's no memory barrier after the counter update, so how do we know it cannot happen after synchronize_rcu()? I think we need something like rcu_assign_pointer() but I don't see rcu_assign_index() (any more?)

johannes

#21 lore raw

RE: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 4 lines

What I'm worried about is that there's no memory barrier after the counter update, so how do we know it cannot happen after synchronize_rcu()? I think we need something like rcu_assign_pointer() but I don't see rcu_assign_index() (any more?)

I did not find much on rcu_assign_index other than
http://markmail.org/thread/h53fuecyo5odg6xv which has this function defined as

+#define rcu_assign_index(p, v) ({ \
+ smp_wmb(); \
+ (p) = (v); \
+ })

Will smp_wmb() do the trick?
counter++;
smp_wmb();
synchronize_rcu();

or will have to move to pointer logic? And use rcu_assign_pointer at key.c and rcu_dereference at tx.c with overhead of kmalloc and kfree?

Thanks
Yogesh

author
#22 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

In reply to Johannes Berg
Quoted 30 lines · Johannes Berg
Quoted 23 lines · Yogesh Ashok Powar
Quoted 5 lines · Johannes Berg
Quoted 4 lines · Yogesh Ashok Powar
Quoted 1 line

Will work on some other logic.

Following is the complete V2-patch

Quoted 8 lines
Quoted 4 lines

v2 changes: a) Moved counter++ before __ieee80211_key_replace in
key_link()
b) Moved crypto_tx_tailroom_needed_cnt to sdata resolve issue with multiple sdata instances in hw reset.

Looks good. Now I'm just worried about memory and compiler barriers that may be needed so the counter update doesn't move after anything else... Hmm.

I some how feel that synchronize_net may be replaced with synchronize_rcu and still the race wont happen.

So the new logic would be
- counter++ <-- Here keys are not added or deleted
so rcu readers wont have problem with extra space allocated.

Quoted 5 lines

- synchronize_rcu <-- This will flush existing readers. Again
new readers will have no problem with extra space allocated.

Let me know your opinion on this.

Quoted 7 lines

It doesn't really matter -- synchronize_net() is exactly the same as synchronize_rcu().

What I'm worried about is that there's no memory barrier after the counter update, so how do we know it cannot happen after synchronize_rcu()? I think we need something like rcu_assign_pointer() but I don't see rcu_assign_index() (any more?)

Hi Johannes,
Andreas has tested the current implementation on his setup and haven't
seen any WARN_ONs being hit. Should I send the final patch?

Thanks
Yogesh

maintainer
#23 lore raw

Re: [PATCH 2/2] mac80211: Fixing Races for skipping tailroom reservation

Quoted 2 lines · yogeshp

Andreas has tested the current implementation on his setup and haven't
seen any WARN_ONs being hit. Should I send the final patch?

Yeah, sure, thanks everyone. I'll ask around about the memory barrier things independently.

johannes

#24 lore raw

Re: [PATCH] nl80211: use netlink consistent dump feature for BSS dumps

Hi Johannes,

This patch breaks a compilation of a compat-wireless tar ball:

nl80211.c:3641:2: error: implicit declaration of function ‘genl_dump_check_consistent’

&

nl80211.c:3731:4: error: ‘struct netlink_callback’ has no member named ‘seq’

Walter