From: Johannes Berg The TX path reserves tailroom if crypto_tx_tailroom_needed_cnt is non-zero, and key control paths make sure this is always set if needed. However, this adds quite a bit of complexity and, in most cases, latency to the key control paths. In order to remove that latency (which is due to waiting for any concurrent TX to complete while changing the counter), prepare the software crypto path to just expand the tailroom if needed. In most cases, this actually can't hit: new keys will mostly be offloaded to hardware and then it's not needed, sometimes they can't actually be used by the TX path at all, etc. Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/ieee80211_i.h | 11 +++++++++++ net/mac80211/wep.c | 4 ++-- net/mac80211/wpa.c | 38 +++++++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h index d186d1274303..0cd276fb8133 100644 --- a/net/mac80211/ieee80211_i.h +++ b/net/mac80211/ieee80211_i.h @@ -54,6 +54,17 @@ struct ieee80211_mesh_fast_tx; #define IEEE80211_ENCRYPT_HEADROOM 8 #define IEEE80211_ENCRYPT_TAILROOM 26 /* current max: GMAC/CMAC-256 MME */ +/* tailroom accounting can race with TX, expand if needed */ +static inline int ieee80211_crypto_tx_tailroom(struct sk_buff *skb, int tail) +{ + int tail_need = tail - skb_tailroom(skb); + + if (!tail || likely(tail_need <= 0 && !skb_cloned(skb))) + return 0; + + return pskb_expand_head(skb, 0, max(tail_need, 0), GFP_ATOMIC); +} + /* power level hasn't been configured (or set to automatic) */ #define IEEE80211_UNSET_POWER_LEVEL INT_MIN diff --git a/net/mac80211/wep.c b/net/mac80211/wep.c index 93b8668079a7..cb966ce2b5bc 100644 --- a/net/mac80211/wep.c +++ b/net/mac80211/wep.c @@ -3,7 +3,7 @@ * Software WEP encryption implementation * Copyright 2002, Jouni Malinen * Copyright 2003, Instant802 Networks, Inc. - * Copyright (C) 2023 Intel Corporation + * Copyright (C) 2023, 2026 Intel Corporation */ #include @@ -137,7 +137,7 @@ int ieee80211_wep_encrypt(struct ieee80211_local *local, size_t len; u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; - if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN)) + if (ieee80211_crypto_tx_tailroom(skb, IEEE80211_WEP_ICV_LEN)) return -1; iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx); diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c index be3a2e95303c..a906bdc2169a 100644 --- a/net/mac80211/wpa.c +++ b/net/mac80211/wpa.c @@ -3,7 +3,7 @@ * Copyright 2002-2004, Instant802 Networks, Inc. * Copyright 2008, Jouni Malinen * Copyright (C) 2016-2017 Intel Deutschland GmbH - * Copyright (C) 2020-2023 Intel Corporation + * Copyright (C) 2020-2023, 2026 Intel Corporation */ #include @@ -45,9 +45,6 @@ ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx) if (skb->len < hdrlen) return TX_DROP; - data = skb->data + hdrlen; - data_len = skb->len - hdrlen; - if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) { /* Need to use software crypto for the test */ info->control.hw_key = NULL; @@ -66,13 +63,18 @@ ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx) if (!info->control.hw_key) tail += IEEE80211_TKIP_ICV_LEN; - if (WARN(skb_tailroom(skb) < tail || - skb_headroom(skb) < IEEE80211_TKIP_IV_LEN, - "mmic: not enough head/tail (%d/%d,%d/%d)\n", - skb_headroom(skb), IEEE80211_TKIP_IV_LEN, - skb_tailroom(skb), tail)) + if (ieee80211_crypto_tx_tailroom(skb, tail)) return TX_DROP; + if (WARN(skb_headroom(skb) < IEEE80211_TKIP_IV_LEN, + "mmic: not enough head (%d/%d)\n", + skb_headroom(skb), IEEE80211_TKIP_IV_LEN)) + return TX_DROP; + + hdr = (struct ieee80211_hdr *)skb->data; + data = skb->data + hdrlen; + data_len = skb->len - hdrlen; + mic = skb_put(skb, MICHAEL_MIC_LEN); if (tx->key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) { @@ -215,8 +217,10 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) else tail = IEEE80211_TKIP_ICV_LEN; - if (WARN_ON(skb_tailroom(skb) < tail || - skb_headroom(skb) < IEEE80211_TKIP_IV_LEN)) + if (ieee80211_crypto_tx_tailroom(skb, tail)) + return -1; + + if (WARN_ON(skb_headroom(skb) < IEEE80211_TKIP_IV_LEN)) return -1; pos = skb_push(skb, IEEE80211_TKIP_IV_LEN); @@ -459,8 +463,10 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb, else tail = mic_len; - if (WARN_ON(skb_tailroom(skb) < tail || - skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN)) + if (ieee80211_crypto_tx_tailroom(skb, tail)) + return -1; + + if (WARN_ON(skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN)) return -1; pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN); @@ -674,8 +680,10 @@ static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) else tail = IEEE80211_GCMP_MIC_LEN; - if (WARN_ON(skb_tailroom(skb) < tail || - skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN)) + if (ieee80211_crypto_tx_tailroom(skb, tail)) + return -1; + + if (WARN_ON(skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN)) return -1; pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN); -- 2.56.0 From: Johannes Berg When crypto_tx_tailroom_needed_cnt newly indicates tailroom is needed, we currently flush the TX path so SW crypto never sees frames without enough tailroom. This can be expensive when new keys are installed, etc., and while it can be improved, doing so would lead to quite a bit of complexity. Since the previous patch the crypto code just expands tailroom in the (unlikely) case this race happened, so we now no longer need to account this as carefully and can remove the waiting. Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/key.c | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/net/mac80211/key.c b/net/mac80211/key.c index d32c61a90d93..6904b519ef37 100644 --- a/net/mac80211/key.c +++ b/net/mac80211/key.c @@ -75,36 +75,10 @@ update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta) static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata) { - /* - * 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://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net - */ - lockdep_assert_wiphy(sdata->local->hw.wiphy); update_vlan_tailroom_need_count(sdata, 1); - - if (!sdata->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(); - } + sdata->crypto_tx_tailroom_needed_cnt++; } static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata, -- 2.56.0 From: Johannes Berg Now that we're no longer synchronizing against the TX path, quite a bit of complexity can go: there's no need to try to optimise the transitions as carefully anymore to avoid the transitions from 0 to 1. We can also simplify hardware key offload management and accept a temporary increment. This also enables further optimisations depending on key type. Also fix an issue that could leave the count wrong during HW restart. Assisted-by: LLM Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/cfg.c | 2 +- net/mac80211/ieee80211_i.h | 2 - net/mac80211/iface.c | 3 - net/mac80211/key.c | 176 +++++++++++++------------------------ net/mac80211/key.h | 5 +- net/mac80211/main.c | 2 - net/mac80211/pm.c | 4 +- 7 files changed, 63 insertions(+), 131 deletions(-) diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c index 08ee5ae64939..d92b8cf8e9f2 100644 --- a/net/mac80211/cfg.c +++ b/net/mac80211/cfg.c @@ -773,7 +773,7 @@ static int ieee80211_del_key(struct wiphy *wiphy, struct wireless_dev *wdev, if (!rcu_access_pointer(*slot.key)) return -ENOENT; - ieee80211_key_free(sdata, &slot, sdata->vif.type == NL80211_IFTYPE_STATION); + ieee80211_key_free(sdata, &slot); return 0; } diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h index 0cd276fb8133..9906a62ee99c 100644 --- a/net/mac80211/ieee80211_i.h +++ b/net/mac80211/ieee80211_i.h @@ -1207,8 +1207,6 @@ struct ieee80211_sub_if_data { /* count for keys needing tailroom space allocation */ int crypto_tx_tailroom_needed_cnt; - int crypto_tx_tailroom_pending_dec; - struct wiphy_delayed_work dec_tailroom_needed_wk; struct net_device *dev; struct ieee80211_local *local; diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c index d0b0a503b229..581a5d57e560 100644 --- a/net/mac80211/iface.c +++ b/net/mac80211/iface.c @@ -2374,9 +2374,6 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name, ieee80211_init_frag_cache(&sdata->frags); - wiphy_delayed_work_init(&sdata->dec_tailroom_needed_wk, - ieee80211_delayed_tailroom_dec); - for (i = 0; i < NUM_NL80211_BANDS; i++) { struct ieee80211_supported_band *sband; sband = local->hw.wiphy->bands[i]; diff --git a/net/mac80211/key.c b/net/mac80211/key.c index 6904b519ef37..8d04fba60e17 100644 --- a/net/mac80211/key.c +++ b/net/mac80211/key.c @@ -81,15 +81,14 @@ static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata) sdata->crypto_tx_tailroom_needed_cnt++; } -static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata, - int delta) +static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata) { lockdep_assert_wiphy(sdata->local->hw.wiphy); - WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta); + WARN_ON_ONCE(!sdata->crypto_tx_tailroom_needed_cnt); - update_vlan_tailroom_need_count(sdata, -delta); - sdata->crypto_tx_tailroom_needed_cnt -= delta; + update_vlan_tailroom_need_count(sdata, -1); + sdata->crypto_tx_tailroom_needed_cnt--; } static bool ieee80211_key_needs_tailroom(struct ieee80211_key *key) @@ -102,6 +101,25 @@ static bool ieee80211_key_needs_tailroom(struct ieee80211_key *key) return !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE); } +static void ieee80211_key_set_uploaded(struct ieee80211_key *key, + bool uploaded) +{ + bool needed = ieee80211_key_needs_tailroom(key); + bool needs; + + if (uploaded) + key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; + else + key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; + + needs = ieee80211_key_needs_tailroom(key); + if (needs && !needed) + increment_tailroom_need_count(key->sdata); + else if (!needs && needed) + decrease_tailroom_need_count(key->sdata); +} + +/* the key must already be accounted for in the tailroom count */ static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) { struct ieee80211_sub_if_data *sdata = key->sdata; @@ -112,17 +130,8 @@ static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) lockdep_assert_wiphy(key->local->hw.wiphy); if (key->flags & KEY_FLAG_TAINTED) { - /* If we get here, it's during resume and the key is - * tainted so shouldn't be used/programmed any more. - * However, its flags may still indicate that it was - * programmed into the device (since we're in resume) - * so clear that flag now to avoid trying to remove - * it again later. - */ - if (!ieee80211_key_needs_tailroom(key)) - increment_tailroom_need_count(sdata); - - key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; + /* only during reconfig, it shouldn't be programmed any more */ + ieee80211_key_set_uploaded(key, false); return -EINVAL; } @@ -183,10 +192,7 @@ static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) sta ? &sta->sta : NULL, &key->conf); if (!ret) { - key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; - - if (!ieee80211_key_needs_tailroom(key)) - decrease_tailroom_need_count(sdata, 1); + ieee80211_key_set_uploaded(key, true); WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)); @@ -255,9 +261,6 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) !(sdata->vif.active_links & BIT(key->conf.link_id))) return; - if (!ieee80211_key_needs_tailroom(key)) - increment_tailroom_need_count(sdata); - pubsta = sta ? &sta->sta : NULL; /* unless requested otherwise, NULL out the STA for client group keys */ if (sdata->vif.type == NL80211_IFTYPE_STATION && @@ -265,7 +268,8 @@ static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) !ieee80211_hw_check(&key->local->hw, PER_STA_AP_GTK)) pubsta = NULL; - key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; + ieee80211_key_set_uploaded(key, false); + ret = drv_set_key(key->local, DISABLE_KEY, sdata, pubsta, &key->conf); if (ret) @@ -576,6 +580,10 @@ static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, ieee80211_pairwise_rekey(old, new); } + /* enabling the new key in hardware may reduce it again */ + if (new && ieee80211_key_needs_tailroom(new)) + increment_tailroom_need_count(sdata); + if (old) { if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { ieee80211_key_disable_hw_accel(old); @@ -588,14 +596,15 @@ static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, ret = ieee80211_key_enable_hw_accel(new); } else if (new->conf.link_id < 0 || !sdata->vif.active_links || BIT(new->conf.link_id) & sdata->vif.active_links) { - new->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; - if (!ieee80211_key_needs_tailroom(new)) - decrease_tailroom_need_count(sdata, 1); + ieee80211_key_set_uploaded(new, true); } } - if (ret) + if (ret) { + if (new && ieee80211_key_needs_tailroom(new)) + decrease_tailroom_need_count(sdata); return ret; + } if (new) { spin_lock_bh(&sdata->local->key_lock); @@ -662,6 +671,9 @@ static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, spin_lock_bh(&sdata->local->key_lock); list_del(&old->list); spin_unlock_bh(&sdata->local->key_lock); + + if (ieee80211_key_needs_tailroom(old)) + decrease_tailroom_need_count(sdata); } return 0; @@ -839,36 +851,20 @@ static void ieee80211_key_free_common(struct ieee80211_key *key) kfree_sensitive(key); } -static void __ieee80211_key_cleanup(struct ieee80211_key *key, - bool delay_tailroom) +static void __ieee80211_key_cleanup(struct ieee80211_key *key) { - if (key->local) { - struct ieee80211_sub_if_data *sdata = key->sdata; - + if (key->local) ieee80211_debugfs_key_remove(key); - - if (delay_tailroom) { - /* see ieee80211_delayed_tailroom_dec */ - sdata->crypto_tx_tailroom_pending_dec++; - wiphy_delayed_work_queue(sdata->local->hw.wiphy, - &sdata->dec_tailroom_needed_wk, - HZ / 2); - } else { - decrease_tailroom_need_count(sdata, 1); - } - } } -static void __ieee80211_key_destroy(struct ieee80211_key *key, - bool delay_tailroom) +static void __ieee80211_key_destroy(struct ieee80211_key *key) { - __ieee80211_key_cleanup(key, delay_tailroom); + __ieee80211_key_cleanup(key); ieee80211_key_free_common(key); } -static void ieee80211_key_destroy(struct ieee80211_key *key, - bool delay_tailroom) +static void ieee80211_key_destroy(struct ieee80211_key *key) { if (!key) return; @@ -879,7 +875,7 @@ static void ieee80211_key_destroy(struct ieee80211_key *key, */ synchronize_net(); - __ieee80211_key_destroy(key, delay_tailroom); + __ieee80211_key_destroy(key); } void ieee80211_key_free_unused(struct ieee80211_key *key) @@ -928,12 +924,6 @@ int ieee80211_key_link(struct ieee80211_sub_if_data *sdata, struct ieee80211_key_slot *slot, struct ieee80211_key *key) { - /* - * We want to delay tailroom updates only for station - in that - * case it helps roaming speed, but in other cases it hurts and - * can cause warnings to appear. - */ - bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION; bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE; static atomic_t key_color = ATOMIC_INIT(0); struct ieee80211_key *old_key = NULL; @@ -1005,18 +995,15 @@ int ieee80211_key_link(struct ieee80211_sub_if_data *sdata, goto out; } - increment_tailroom_need_count(sdata); - ret = ieee80211_key_replace(sdata, slot, old_key, key); if (ret) { - decrease_tailroom_need_count(sdata, 1); key->local = NULL; key->sdata = NULL; goto out; } ieee80211_debugfs_key_add(key); - ieee80211_key_destroy(old_key, delay_tailroom); + ieee80211_key_destroy(old_key); return 0; out: @@ -1025,8 +1012,7 @@ int ieee80211_key_link(struct ieee80211_sub_if_data *sdata, } void ieee80211_key_free(struct ieee80211_sub_if_data *sdata, - struct ieee80211_key_slot *slot, - bool delay_tailroom) + struct ieee80211_key_slot *slot) { struct ieee80211_key *key; @@ -1037,7 +1023,7 @@ void ieee80211_key_free(struct ieee80211_sub_if_data *sdata, /* replace key with nothing if it was ever used */ if (key->sdata) ieee80211_key_replace(key->sdata, slot, key, NULL); - ieee80211_key_destroy(key, delay_tailroom); + ieee80211_key_destroy(key); } void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata) @@ -1048,18 +1034,17 @@ void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata) lockdep_assert_wiphy(sdata->local->hw.wiphy); sdata->crypto_tx_tailroom_needed_cnt = 0; - sdata->crypto_tx_tailroom_pending_dec = 0; if (sdata->vif.type == NL80211_IFTYPE_AP) { - list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) { + list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) vlan->crypto_tx_tailroom_needed_cnt = 0; - vlan->crypto_tx_tailroom_pending_dec = 0; - } } if (ieee80211_sdata_running(sdata)) { list_for_each_entry(key, &sdata->key_list, list) { - if (!(key->flags & KEY_FLAG_TAINTED)) + /* the device was reset, it's no longer uploaded */ + key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; + if (ieee80211_key_needs_tailroom(key)) increment_tailroom_need_count(sdata); ieee80211_key_enable_hw_accel(key); } @@ -1169,10 +1154,6 @@ EXPORT_SYMBOL(ieee80211_iter_keys_atomic); static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata, struct list_head *keys) { - decrease_tailroom_need_count(sdata, - sdata->crypto_tx_tailroom_pending_dec); - sdata->crypto_tx_tailroom_pending_dec = 0; - ieee80211_debugfs_key_remove_mgmt_default(sdata); ieee80211_debugfs_key_remove_beacon_default(sdata); @@ -1239,7 +1220,7 @@ void ieee80211_free_key_list(struct ieee80211_local *local, lockdep_assert_wiphy(local->hw.wiphy); list_for_each_entry_safe(key, tmp, keys, list) - __ieee80211_key_destroy(key, false); + __ieee80211_key_destroy(key); } void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, @@ -1251,9 +1232,6 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, struct ieee80211_key *key, *tmp; LIST_HEAD(keys); - wiphy_delayed_work_cancel(local->hw.wiphy, - &sdata->dec_tailroom_needed_wk); - lockdep_assert_wiphy(local->hw.wiphy); ieee80211_remove_link_keys(&sdata->deflink, &keys); @@ -1271,7 +1249,7 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, if (!list_empty(&keys) || force_synchronize) synchronize_net(); list_for_each_entry_safe(key, tmp, &keys, list) - __ieee80211_key_destroy(key, false); + __ieee80211_key_destroy(key); if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { if (sdata->bss) { @@ -1283,14 +1261,12 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, master->crypto_tx_tailroom_needed_cnt); } } else { - WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt || - sdata->crypto_tx_tailroom_pending_dec); + WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt); } if (sdata->vif.type == NL80211_IFTYPE_AP) { list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) - WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt || - vlan->crypto_tx_tailroom_pending_dec); + WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt); } } @@ -1316,8 +1292,7 @@ void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, if (WARN(ret, "failed to remove STA link key (%d)\n", ret)) continue; list_add(&key->list, &link_sta->key_destroy_list); - __ieee80211_key_cleanup(key, key->sdata->vif.type == - NL80211_IFTYPE_STATION); + __ieee80211_key_cleanup(key); } for (int i = 0; i < ARRAY_SIZE(link_sta->rx_cigtk); i++) { @@ -1332,8 +1307,7 @@ void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, if (WARN(ret, "failed to remove STA link key (%d)\n", ret)) continue; list_add(&key->list, &link_sta->key_destroy_list); - __ieee80211_key_cleanup(key, key->sdata->vif.type == - NL80211_IFTYPE_STATION); + __ieee80211_key_cleanup(key); } } @@ -1364,40 +1338,10 @@ void ieee80211_free_sta_keys(struct ieee80211_local *local, if (!key) continue; ieee80211_key_replace(key->sdata, &slot, key, NULL); - __ieee80211_key_destroy(key, key->sdata->vif.type == - NL80211_IFTYPE_STATION); + __ieee80211_key_destroy(key); } } -void ieee80211_delayed_tailroom_dec(struct wiphy *wiphy, - struct wiphy_work *wk) -{ - struct ieee80211_sub_if_data *sdata; - - sdata = container_of(wk, struct ieee80211_sub_if_data, - dec_tailroom_needed_wk.work); - - /* - * The reason for the delayed tailroom needed decrementing is to - * make roaming faster: during roaming, all keys are first deleted - * and then new keys are installed. The first new key causes the - * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes - * the cost of synchronize_net() (which can be slow). Avoid this - * by deferring the crypto_tx_tailroom_needed_cnt decrementing on - * key removal for a while, so if we roam the value is larger than - * zero and no 0->1 transition happens. - * - * The cost is that if the AP switching was from an AP with keys - * to one without, we still allocate tailroom while it would no - * longer be needed. However, in the typical (fast) roaming case - * within an ESS this usually won't happen. - */ - - decrease_tailroom_need_count(sdata, - sdata->crypto_tx_tailroom_pending_dec); - sdata->crypto_tx_tailroom_pending_dec = 0; -} - void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid, const u8 *replay_ctr, gfp_t gfp) { diff --git a/net/mac80211/key.h b/net/mac80211/key.h index 2fc985d41e8f..b7caec277fbe 100644 --- a/net/mac80211/key.h +++ b/net/mac80211/key.h @@ -191,8 +191,7 @@ int ieee80211_key_link(struct ieee80211_sub_if_data *sdata, struct ieee80211_key *key); int ieee80211_set_tx_key(struct ieee80211_key *key); void ieee80211_key_free(struct ieee80211_sub_if_data *sdata, - struct ieee80211_key_slot *slot, - bool delay_tailroom); + struct ieee80211_key_slot *slot); void ieee80211_key_free_unused(struct ieee80211_key *key); void ieee80211_set_default_key(struct ieee80211_link_data *link, int idx, bool uni, bool multi); @@ -215,7 +214,5 @@ void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata); int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata, unsigned long del_links_mask, unsigned long add_links_mask); -void ieee80211_delayed_tailroom_dec(struct wiphy *wiphy, - struct wiphy_work *wk); #endif /* IEEE80211_KEY_H */ diff --git a/net/mac80211/main.c b/net/mac80211/main.c index dd00f126f037..c2cad0f92b43 100644 --- a/net/mac80211/main.c +++ b/net/mac80211/main.c @@ -512,8 +512,6 @@ static void ieee80211_restart_work(struct work_struct *work) WLAN_REASON_UNSPECIFIED, false); } - wiphy_delayed_work_flush(local->hw.wiphy, - &sdata->dec_tailroom_needed_wk); } ieee80211_scan_cancel(local); diff --git a/net/mac80211/pm.c b/net/mac80211/pm.c index f63676c44853..6a27f8f35c8e 100644 --- a/net/mac80211/pm.c +++ b/net/mac80211/pm.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* * Portions - * Copyright (C) 2020-2021, 2023-2024 Intel Corporation + * Copyright (C) 2020-2021, 2023-2024, 2026 Intel Corporation */ #include #include @@ -160,8 +160,6 @@ int __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan, break; } - wiphy_delayed_work_flush(local->hw.wiphy, - &sdata->dec_tailroom_needed_wk); drv_remove_interface(local, sdata); } -- 2.56.0 From: Johannes Berg Keys that are only used for RX as well as management-only keys don't need to have tailroom added to data frames (we already add tailroom to all management frames), so don't count them. This avoids allocating tailroom for every frame just because a management key would need it. Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/key.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/net/mac80211/key.c b/net/mac80211/key.c index 8d04fba60e17..436a165d0b91 100644 --- a/net/mac80211/key.c +++ b/net/mac80211/key.c @@ -93,6 +93,19 @@ static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata) static bool ieee80211_key_needs_tailroom(struct ieee80211_key *key) { + /* non-pairwise keys from a station are only for RX */ + if (key->sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) + return false; + + /* management frames always have tailroom reserved */ + switch (key->conf.cipher) { + case WLAN_CIPHER_SUITE_AES_CMAC: + case WLAN_CIPHER_SUITE_BIP_CMAC_256: + case WLAN_CIPHER_SUITE_BIP_GMAC_128: + case WLAN_CIPHER_SUITE_BIP_GMAC_256: + return false; + } + if (key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | IEEE80211_KEY_FLAG_PUT_MIC_SPACE | IEEE80211_KEY_FLAG_RESERVE_TAILROOM)) -- 2.56.0 From: Johannes Berg When a key is removed, we free it after a synchronize_net() (which may be amortized with other calls thereof), so if it was still being used under RCU, that code can no longer do so. This can delay some operations significantly. However, it's not really required that we do this, it'll be concurrent with TX so we can just pretend the key is still there as far as the concurrent TX is concerned and free it asynchronously after a grace period. This currently requires an RCU work item due to crypto TFMs, but that can go away too once we convert to the library. This then implies an rcu_barrier() before flushing the WQ to ensure everything that must run on there actually will. We also no longer need the STA's key_destroy_list, since that just handled the collection and combined freeing of the keys after synchronize_net(). Assisted-by: LLM Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/cfg.c | 5 +---- net/mac80211/key.c | 45 ++++++++++++----------------------------- net/mac80211/key.h | 3 ++- net/mac80211/main.c | 2 ++ net/mac80211/sta_info.c | 4 ---- net/mac80211/sta_info.h | 3 --- 6 files changed, 18 insertions(+), 44 deletions(-) diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c index d92b8cf8e9f2..6e466f8e1653 100644 --- a/net/mac80211/cfg.c +++ b/net/mac80211/cfg.c @@ -2356,10 +2356,7 @@ static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev, ieee80211_link_info_change_notify(sdata, link, changes); ieee80211_remove_link_keys(link, &keys); - if (!list_empty(&keys)) { - synchronize_net(); - ieee80211_free_key_list(local, &keys); - } + ieee80211_free_key_list(local, &keys); if (sdata->wdev.links[link_id].cac_started) { chandef = link_conf->chanreq.oper; diff --git a/net/mac80211/key.c b/net/mac80211/key.c index 436a165d0b91..b66168edab25 100644 --- a/net/mac80211/key.c +++ b/net/mac80211/key.c @@ -864,31 +864,24 @@ static void ieee80211_key_free_common(struct ieee80211_key *key) kfree_sensitive(key); } -static void __ieee80211_key_cleanup(struct ieee80211_key *key) +static void ieee80211_key_free_work(struct work_struct *wk) { - if (key->local) - ieee80211_debugfs_key_remove(key); -} - -static void __ieee80211_key_destroy(struct ieee80211_key *key) -{ - __ieee80211_key_cleanup(key); + struct ieee80211_key *key; + key = container_of(to_rcu_work(wk), struct ieee80211_key, free_work); ieee80211_key_free_common(key); } +/* the key must no longer be reachable, it's freed after an RCU grace period */ static void ieee80211_key_destroy(struct ieee80211_key *key) { if (!key) return; - /* - * Synchronize so the TX path can no longer be using this - * key before we free/remove it. - */ - synchronize_net(); + ieee80211_debugfs_key_remove(key); - __ieee80211_key_destroy(key); + INIT_RCU_WORK(&key->free_work, ieee80211_key_free_work); + queue_rcu_work(key->local->workqueue, &key->free_work); } void ieee80211_key_free_unused(struct ieee80211_key *key) @@ -1233,7 +1226,7 @@ void ieee80211_free_key_list(struct ieee80211_local *local, lockdep_assert_wiphy(local->hw.wiphy); list_for_each_entry_safe(key, tmp, keys, list) - __ieee80211_key_destroy(key); + ieee80211_key_destroy(key); } void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, @@ -1259,10 +1252,10 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, WARN_ON(!list_empty(&sdata->key_list)); - if (!list_empty(&keys) || force_synchronize) + if (force_synchronize) synchronize_net(); list_for_each_entry_safe(key, tmp, &keys, list) - __ieee80211_key_destroy(key); + ieee80211_key_destroy(key); if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { if (sdata->bss) { @@ -1304,8 +1297,7 @@ void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, ret = ieee80211_key_replace(key->sdata, &slot, key, NULL); if (WARN(ret, "failed to remove STA link key (%d)\n", ret)) continue; - list_add(&key->list, &link_sta->key_destroy_list); - __ieee80211_key_cleanup(key); + ieee80211_key_destroy(key); } for (int i = 0; i < ARRAY_SIZE(link_sta->rx_cigtk); i++) { @@ -1319,18 +1311,7 @@ void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, ret = ieee80211_key_replace(key->sdata, &slot, key, NULL); if (WARN(ret, "failed to remove STA link key (%d)\n", ret)) continue; - list_add(&key->list, &link_sta->key_destroy_list); - __ieee80211_key_cleanup(key); - } -} - -void ieee80211_free_link_sta_keys(struct link_sta_info *link_sta) -{ - struct ieee80211_key *key, *tmp; - - list_for_each_entry_safe(key, tmp, &link_sta->key_destroy_list, list) { - list_del(&key->list); - ieee80211_key_free_common(key); + ieee80211_key_destroy(key); } } @@ -1351,7 +1332,7 @@ void ieee80211_free_sta_keys(struct ieee80211_local *local, if (!key) continue; ieee80211_key_replace(key->sdata, &slot, key, NULL); - __ieee80211_key_destroy(key); + ieee80211_key_destroy(key); } } diff --git a/net/mac80211/key.h b/net/mac80211/key.h index b7caec277fbe..b7afd6c90eb0 100644 --- a/net/mac80211/key.h +++ b/net/mac80211/key.h @@ -131,6 +131,8 @@ struct ieee80211_key { unsigned int color; + struct rcu_work free_work; + /* * key config, must be last because it contains key * material as variable length member @@ -207,7 +209,6 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, bool force_synchronize); void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, struct link_sta_info *link_sta); -void ieee80211_free_link_sta_keys(struct link_sta_info *link_sta); void ieee80211_free_sta_keys(struct ieee80211_local *local, struct sta_info *sta); void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata); diff --git a/net/mac80211/main.c b/net/mac80211/main.c index c2cad0f92b43..6e31076bcc10 100644 --- a/net/mac80211/main.c +++ b/net/mac80211/main.c @@ -1748,6 +1748,8 @@ void ieee80211_unregister_hw(struct ieee80211_hw *hw) skb_queue_purge(&local->skb_queue_unreliable); wiphy_unregister(local->hw.wiphy); + /* keys are freed via queue_rcu_work() */ + rcu_barrier(); destroy_workqueue(local->workqueue); ieee80211_led_exit(local); kfree(local->int_scan_req); diff --git a/net/mac80211/sta_info.c b/net/mac80211/sta_info.c index 790997592d8c..23166fc39729 100644 --- a/net/mac80211/sta_info.c +++ b/net/mac80211/sta_info.c @@ -353,7 +353,6 @@ struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, static void sta_info_free_link(struct link_sta_info *link_sta) { free_percpu(link_sta->pcpu_rx_stats); - ieee80211_free_link_sta_keys(link_sta); } static void sta_link_free_rcu(struct rcu_head *head) @@ -561,8 +560,6 @@ static int sta_info_alloc_link(struct ieee80211_local *local, struct ieee80211_hw *hw = &local->hw; int i; - INIT_LIST_HEAD(&link_info->key_destroy_list); - if (ieee80211_hw_check(hw, USES_RSS)) { link_info->pcpu_rx_stats = alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp); @@ -1586,7 +1583,6 @@ static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc) continue; ieee80211_unlink_link_sta_keys(local, link_sta); - ieee80211_free_link_sta_keys(link_sta); } ieee80211_free_sta_keys(local, sta); diff --git a/net/mac80211/sta_info.h b/net/mac80211/sta_info.h index 27f31fd7258e..8c6a9fee6b29 100644 --- a/net/mac80211/sta_info.h +++ b/net/mac80211/sta_info.h @@ -522,7 +522,6 @@ struct ieee80211_fragment_cache { * @uhr_dbe_enabled: for STAs as clients to an AP interface indicates * DBE is enabled by the STA * @debugfs_dir: debug filesystem directory dentry - * @key_destroy_list: used during removal, keys to destroy * @pub: public (driver visible) link STA data */ struct link_sta_info { @@ -582,8 +581,6 @@ struct link_sta_info { struct dentry *debugfs_dir; #endif - struct list_head key_destroy_list; - struct ieee80211_link_sta *pub; }; -- 2.56.0 From: Johannes Berg The only reason for the force_synchronize here was for some callers to amortize their own synchronize_rcu() with the one required for keys, but keys no longer require one. Clean up the argument. It seems like this should require some callers to call it themselves, but the original user was do_stop(), which now has its own (again) anyway to ensure no frames go to the SKB queue before purging it. See commit 7907c7d33c37 ("mac80211: free all AP/VLAN keys at once") for the move into key code. The other callers (AP_VLAN, NAN, mesh) didn't really care at all, so we can just drop the argument. Assisted-by: LLM Reviewed-by: Miriam Rachel Korenblit Signed-off-by: Johannes Berg --- net/mac80211/iface.c | 14 ++++---------- net/mac80211/key.c | 5 +---- net/mac80211/key.h | 3 +-- net/mac80211/mesh.c | 2 +- 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c index 581a5d57e560..8bfd6bc9c9fe 100644 --- a/net/mac80211/iface.c +++ b/net/mac80211/iface.c @@ -614,8 +614,7 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_do case NL80211_IFTYPE_AP_VLAN: list_del(&sdata->u.vlan.list); RCU_INIT_POINTER(sdata->vif.bss_conf.chanctx_conf, NULL); - /* see comment in the default case below */ - ieee80211_free_keys(sdata, true); + ieee80211_free_keys(sdata); /* increased by AP value on ifup, so reset on ifdown */ sdata->crypto_tx_tailroom_needed_cnt = 0; /* no need to tell driver */ @@ -662,7 +661,7 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_do * Free the remaining keys that might be associated with the * NAN interface, e.g., IGTK and BIGTK used for Tx. */ - ieee80211_free_keys(sdata, true); + ieee80211_free_keys(sdata); break; case NL80211_IFTYPE_NAN_DATA: RCU_INIT_POINTER(sdata->u.nan_data.nmi, NULL); @@ -674,13 +673,8 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_do * Free the remaining keys, if there are any * (which can happen in AP mode if userspace sets * keys before the interface is operating) - * - * Force the key freeing to always synchronize_net() - * to wait for the RX path in case it is using this - * interface enqueuing frames at this very time on - * another CPU. */ - ieee80211_free_keys(sdata, true); + ieee80211_free_keys(sdata); skb_queue_purge(&sdata->skb_queue); skb_queue_purge(&sdata->status_queue); } @@ -906,7 +900,7 @@ static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata) wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work); /* free extra data */ - ieee80211_free_keys(sdata, false); + ieee80211_free_keys(sdata); ieee80211_debugfs_remove_netdev(sdata); diff --git a/net/mac80211/key.c b/net/mac80211/key.c index b66168edab25..d1e3fe37d2e3 100644 --- a/net/mac80211/key.c +++ b/net/mac80211/key.c @@ -1229,8 +1229,7 @@ void ieee80211_free_key_list(struct ieee80211_local *local, ieee80211_key_destroy(key); } -void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, - bool force_synchronize) +void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) { struct ieee80211_local *local = sdata->local; struct ieee80211_sub_if_data *vlan; @@ -1252,8 +1251,6 @@ void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, WARN_ON(!list_empty(&sdata->key_list)); - if (force_synchronize) - synchronize_net(); list_for_each_entry_safe(key, tmp, &keys, list) ieee80211_key_destroy(key); diff --git a/net/mac80211/key.h b/net/mac80211/key.h index b7afd6c90eb0..9f13d175623a 100644 --- a/net/mac80211/key.h +++ b/net/mac80211/key.h @@ -205,8 +205,7 @@ void ieee80211_remove_link_keys(struct ieee80211_link_data *link, struct list_head *keys); void ieee80211_free_key_list(struct ieee80211_local *local, struct list_head *keys); -void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, - bool force_synchronize); +void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata); void ieee80211_unlink_link_sta_keys(struct ieee80211_local *local, struct link_sta_info *link_sta); void ieee80211_free_sta_keys(struct ieee80211_local *local, diff --git a/net/mac80211/mesh.c b/net/mac80211/mesh.c index 8f8814125375..5f523e14cf84 100644 --- a/net/mac80211/mesh.c +++ b/net/mac80211/mesh.c @@ -1226,7 +1226,7 @@ void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata) /* flush STAs and mpaths on this iface */ sta_info_flush(sdata, -1); - ieee80211_free_keys(sdata, true); + ieee80211_free_keys(sdata); mesh_path_flush_by_iface(sdata); /* stop the beacon */ -- 2.56.0