regulatory: fix uevent
[firefly-linux-kernel-4.4.55.git] / net / wireless / reg.c
index 9b64b201cdf1d18695eccc894faff4e8f0ee937b..2193f62bb8aef75d8795b6e9f2e4f583f85465a6 100644 (file)
@@ -82,7 +82,8 @@ static struct regulatory_request core_request_world = {
 };
 
 /* Receipt of information from last regulatory request */
-static struct regulatory_request *last_request = &core_request_world;
+static struct regulatory_request __rcu *last_request =
+       (void __rcu *)&core_request_world;
 
 /* To trigger userspace events */
 static struct platform_device *reg_pdev;
@@ -102,7 +103,7 @@ const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
  * Protects static reg.c components:
  *     - cfg80211_regdomain (if not used with RCU)
  *     - cfg80211_world_regdom
- *     - last_request
+ *     - last_request (if not used with RCU)
  *     - reg_num_devs_support_basehint
  */
 static DEFINE_MUTEX(reg_mutex);
@@ -137,6 +138,12 @@ static void rcu_free_regdom(const struct ieee80211_regdomain *r)
        kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
 }
 
+static struct regulatory_request *get_last_request(void)
+{
+       return rcu_dereference_check(last_request,
+                                    lockdep_is_held(&reg_mutex));
+}
+
 /* Used to queue up regulatory hints */
 static LIST_HEAD(reg_requests_list);
 static spinlock_t reg_requests_lock;
@@ -206,6 +213,7 @@ static void reset_regdomains(bool full_reset,
                             const struct ieee80211_regdomain *new_regdom)
 {
        const struct ieee80211_regdomain *r;
+       struct regulatory_request *lr;
 
        assert_reg_lock();
 
@@ -228,9 +236,10 @@ static void reset_regdomains(bool full_reset,
        if (!full_reset)
                return;
 
-       if (last_request != &core_request_world)
-               kfree(last_request);
-       last_request = &core_request_world;
+       lr = get_last_request();
+       if (lr != &core_request_world && lr)
+               kfree_rcu(lr, rcu_head);
+       rcu_assign_pointer(last_request, &core_request_world);
 }
 
 /*
@@ -239,9 +248,11 @@ static void reset_regdomains(bool full_reset,
  */
 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
 {
-       WARN_ON(!last_request);
+       struct regulatory_request *lr;
 
-       assert_reg_lock();
+       lr = get_last_request();
+
+       WARN_ON(!lr);
 
        reset_regdomains(false, rd);
 
@@ -448,15 +459,12 @@ static int call_crda(const char *alpha2)
 
 static bool reg_is_valid_request(const char *alpha2)
 {
-       assert_reg_lock();
+       struct regulatory_request *lr = get_last_request();
 
-       if (!last_request)
+       if (!lr || lr->processed)
                return false;
 
-       if (last_request->processed)
-               return false;
-
-       return alpha2_equal(last_request->alpha2, alpha2);
+       return alpha2_equal(lr->alpha2, alpha2);
 }
 
 /* Sanity check on a regulatory rule */
@@ -702,16 +710,16 @@ static u32 map_regdom_flags(u32 rd_flags)
        return channel_flags;
 }
 
-static int freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
-                             const struct ieee80211_reg_rule **reg_rule,
-                             const struct ieee80211_regdomain *regd)
+static const struct ieee80211_reg_rule *
+freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
+                  const struct ieee80211_regdomain *regd)
 {
        int i;
        bool band_rule_found = false;
        bool bw_fits = false;
 
        if (!regd)
-               return -EINVAL;
+               return ERR_PTR(-EINVAL);
 
        for (i = 0; i < regd->n_reg_rules; i++) {
                const struct ieee80211_reg_rule *rr;
@@ -730,37 +738,34 @@ static int freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
 
                bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
 
-               if (band_rule_found && bw_fits) {
-                       *reg_rule = rr;
-                       return 0;
-               }
+               if (band_rule_found && bw_fits)
+                       return rr;
        }
 
        if (!band_rule_found)
-               return -ERANGE;
+               return ERR_PTR(-ERANGE);
 
-       return -EINVAL;
+       return ERR_PTR(-EINVAL);
 }
 
-int freq_reg_info(struct wiphy *wiphy, u32 center_freq,
-                 const struct ieee80211_reg_rule **reg_rule)
+const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
+                                              u32 center_freq)
 {
        const struct ieee80211_regdomain *regd;
-
-       assert_reg_lock();
+       struct regulatory_request *lr = get_last_request();
 
        /*
         * Follow the driver's regulatory domain, if present, unless a country
         * IE has been processed or a user wants to help complaince further
         */
-       if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
+       if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
+           lr->initiator != NL80211_REGDOM_SET_BY_USER &&
            wiphy->regd)
                regd = get_wiphy_regdom(wiphy);
        else
                regd = get_cfg80211_regdom();
 
-       return freq_reg_info_regd(wiphy, center_freq, reg_rule, regd);
+       return freq_reg_info_regd(wiphy, center_freq, regd);
 }
 EXPORT_SYMBOL(freq_reg_info);
 
@@ -822,19 +827,19 @@ static void handle_channel(struct wiphy *wiphy,
                           enum nl80211_reg_initiator initiator,
                           struct ieee80211_channel *chan)
 {
-       int r;
        u32 flags, bw_flags = 0;
        const struct ieee80211_reg_rule *reg_rule = NULL;
        const struct ieee80211_power_rule *power_rule = NULL;
        const struct ieee80211_freq_range *freq_range = NULL;
        struct wiphy *request_wiphy = NULL;
+       struct regulatory_request *lr = get_last_request();
 
-       request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
+       request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
 
        flags = chan->orig_flags;
 
-       r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq), &reg_rule);
-       if (r) {
+       reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
+       if (IS_ERR(reg_rule)) {
                /*
                 * We will disable all channels that do not match our
                 * received regulatory rule unless the hint is coming
@@ -846,7 +851,7 @@ static void handle_channel(struct wiphy *wiphy,
                 * while 5 GHz is still supported.
                 */
                if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-                   r == -ERANGE)
+                   PTR_ERR(reg_rule) == -ERANGE)
                        return;
 
                REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
@@ -862,7 +867,7 @@ static void handle_channel(struct wiphy *wiphy,
        if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
                bw_flags = IEEE80211_CHAN_NO_HT40;
 
-       if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
+       if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
            request_wiphy && request_wiphy == wiphy &&
            request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
                /*
@@ -927,7 +932,7 @@ bool reg_last_request_cell_base(void)
        bool val;
 
        mutex_lock(&reg_mutex);
-       val = reg_request_cell_base(last_request);
+       val = reg_request_cell_base(get_last_request());
        mutex_unlock(&reg_mutex);
 
        return val;
@@ -938,10 +943,12 @@ bool reg_last_request_cell_base(void)
 static enum reg_request_treatment
 reg_ignore_cell_hint(struct regulatory_request *pending_request)
 {
+       struct regulatory_request *lr = get_last_request();
+
        if (!reg_num_devs_support_basehint)
                return REG_REQ_IGNORE;
 
-       if (reg_request_cell_base(last_request) &&
+       if (reg_request_cell_base(lr) &&
            !regdom_changes(pending_request->alpha2))
                return REG_REQ_ALREADY_SET;
 
@@ -969,7 +976,9 @@ static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
 static bool ignore_reg_update(struct wiphy *wiphy,
                              enum nl80211_reg_initiator initiator)
 {
-       if (!last_request) {
+       struct regulatory_request *lr = get_last_request();
+
+       if (!lr) {
                REG_DBG_PRINT("Ignoring regulatory request %s since last_request is not set\n",
                              reg_initiator_name(initiator));
                return true;
@@ -988,18 +997,34 @@ static bool ignore_reg_update(struct wiphy *wiphy,
         */
        if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
            initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           !is_world_regdom(last_request->alpha2)) {
+           !is_world_regdom(lr->alpha2)) {
                REG_DBG_PRINT("Ignoring regulatory request %s since the driver requires its own regulatory domain to be set first\n",
                              reg_initiator_name(initiator));
                return true;
        }
 
-       if (reg_request_cell_base(last_request))
+       if (reg_request_cell_base(lr))
                return reg_dev_ignore_cell_hint(wiphy);
 
        return false;
 }
 
+static bool reg_is_world_roaming(struct wiphy *wiphy)
+{
+       const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
+       const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
+       struct regulatory_request *lr = get_last_request();
+
+       if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
+               return true;
+
+       if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
+           wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
+               return true;
+
+       return false;
+}
+
 static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
                              struct reg_beacon *reg_beacon)
 {
@@ -1019,6 +1044,9 @@ static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
 
        chan->beacon_found = true;
 
+       if (!reg_is_world_roaming(wiphy))
+               return;
+
        if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
                return;
 
@@ -1076,22 +1104,6 @@ static void wiphy_update_beacon_reg(struct wiphy *wiphy)
        }
 }
 
-static bool reg_is_world_roaming(struct wiphy *wiphy)
-{
-       const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
-       const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
-
-       if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
-               return true;
-
-       if (last_request &&
-           last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
-               return true;
-
-       return false;
-}
-
 /* Reap the advantages of previously found beacons */
 static void reg_process_beacons(struct wiphy *wiphy)
 {
@@ -1101,8 +1113,6 @@ static void reg_process_beacons(struct wiphy *wiphy)
         */
        if (!last_request)
                return;
-       if (!reg_is_world_roaming(wiphy))
-               return;
        wiphy_update_beacon_reg(wiphy);
 }
 
@@ -1184,13 +1194,12 @@ static void wiphy_update_regulatory(struct wiphy *wiphy,
                                    enum nl80211_reg_initiator initiator)
 {
        enum ieee80211_band band;
-
-       assert_reg_lock();
+       struct regulatory_request *lr = get_last_request();
 
        if (ignore_reg_update(wiphy, initiator))
                return;
 
-       last_request->dfs_region = get_cfg80211_regdom()->dfs_region;
+       lr->dfs_region = get_cfg80211_regdom()->dfs_region;
 
        for (band = 0; band < IEEE80211_NUM_BANDS; band++)
                handle_band(wiphy, initiator, wiphy->bands[band]);
@@ -1199,7 +1208,7 @@ static void wiphy_update_regulatory(struct wiphy *wiphy,
        reg_process_ht_flags(wiphy);
 
        if (wiphy->reg_notifier)
-               wiphy->reg_notifier(wiphy, last_request);
+               wiphy->reg_notifier(wiphy, lr);
 }
 
 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
@@ -1220,7 +1229,7 @@ static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
                if (initiator == NL80211_REGDOM_SET_BY_CORE &&
                    wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
                    wiphy->reg_notifier)
-                       wiphy->reg_notifier(wiphy, last_request);
+                       wiphy->reg_notifier(wiphy, get_last_request());
        }
 }
 
@@ -1228,16 +1237,15 @@ static void handle_channel_custom(struct wiphy *wiphy,
                                  struct ieee80211_channel *chan,
                                  const struct ieee80211_regdomain *regd)
 {
-       int r;
        u32 bw_flags = 0;
        const struct ieee80211_reg_rule *reg_rule = NULL;
        const struct ieee80211_power_rule *power_rule = NULL;
        const struct ieee80211_freq_range *freq_range = NULL;
 
-       r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
-                              &reg_rule, regd);
+       reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
+                                     regd);
 
-       if (r) {
+       if (IS_ERR(reg_rule)) {
                REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
                              chan->center_freq);
                chan->flags = IEEE80211_CHAN_DISABLED;
@@ -1300,28 +1308,28 @@ get_reg_request_treatment(struct wiphy *wiphy,
                          struct regulatory_request *pending_request)
 {
        struct wiphy *last_wiphy = NULL;
+       struct regulatory_request *lr = get_last_request();
 
        /* All initial requests are respected */
-       if (!last_request)
+       if (!lr)
                return REG_REQ_OK;
 
        switch (pending_request->initiator) {
        case NL80211_REGDOM_SET_BY_CORE:
                return REG_REQ_OK;
        case NL80211_REGDOM_SET_BY_COUNTRY_IE:
-               if (reg_request_cell_base(last_request)) {
+               if (reg_request_cell_base(lr)) {
                        /* Trust a Cell base station over the AP's country IE */
                        if (regdom_changes(pending_request->alpha2))
                                return REG_REQ_IGNORE;
                        return REG_REQ_ALREADY_SET;
                }
 
-               last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
+               last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
 
                if (unlikely(!is_an_alpha2(pending_request->alpha2)))
                        return -EINVAL;
-               if (last_request->initiator ==
-                   NL80211_REGDOM_SET_BY_COUNTRY_IE) {
+               if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
                        if (last_wiphy != wiphy) {
                                /*
                                 * Two cards with two APs claiming different
@@ -1343,7 +1351,7 @@ get_reg_request_treatment(struct wiphy *wiphy,
                }
                return 0;
        case NL80211_REGDOM_SET_BY_DRIVER:
-               if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
+               if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
                        if (regdom_changes(pending_request->alpha2))
                                return REG_REQ_OK;
                        return REG_REQ_ALREADY_SET;
@@ -1354,7 +1362,7 @@ get_reg_request_treatment(struct wiphy *wiphy,
                 * back in or if you add a new device for which the previously
                 * loaded card also agrees on the regulatory domain.
                 */
-               if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
+               if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
                    !regdom_changes(pending_request->alpha2))
                        return REG_REQ_ALREADY_SET;
 
@@ -1363,26 +1371,26 @@ get_reg_request_treatment(struct wiphy *wiphy,
                if (reg_request_cell_base(pending_request))
                        return reg_ignore_cell_hint(pending_request);
 
-               if (reg_request_cell_base(last_request))
+               if (reg_request_cell_base(lr))
                        return REG_REQ_IGNORE;
 
-               if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
+               if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
                        return REG_REQ_INTERSECT;
                /*
                 * If the user knows better the user should set the regdom
                 * to their country before the IE is picked up
                 */
-               if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
-                   last_request->intersect)
+               if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
+                   lr->intersect)
                        return REG_REQ_IGNORE;
                /*
                 * Process user requests only after previous user/driver/core
                 * requests have been processed
                 */
-               if ((last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
-                    last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
-                    last_request->initiator == NL80211_REGDOM_SET_BY_USER) &&
-                   regdom_changes(last_request->alpha2))
+               if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
+                    lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
+                    lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
+                   regdom_changes(lr->alpha2))
                        return REG_REQ_IGNORE;
 
                if (!regdom_changes(pending_request->alpha2))
@@ -1397,15 +1405,16 @@ get_reg_request_treatment(struct wiphy *wiphy,
 static void reg_set_request_processed(void)
 {
        bool need_more_processing = false;
+       struct regulatory_request *lr = get_last_request();
 
-       last_request->processed = true;
+       lr->processed = true;
 
        spin_lock(&reg_requests_lock);
        if (!list_empty(&reg_requests_list))
                need_more_processing = true;
        spin_unlock(&reg_requests_lock);
 
-       if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
+       if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
                cancel_delayed_work(&reg_timeout);
 
        if (need_more_processing)
@@ -1432,6 +1441,7 @@ __regulatory_hint(struct wiphy *wiphy,
        const struct ieee80211_regdomain *regd;
        bool intersect = false;
        enum reg_request_treatment treatment;
+       struct regulatory_request *lr;
 
        treatment = get_reg_request_treatment(wiphy, pending_request);
 
@@ -1472,18 +1482,20 @@ __regulatory_hint(struct wiphy *wiphy,
        }
 
 new_request:
-       if (last_request != &core_request_world)
-               kfree(last_request);
+       lr = get_last_request();
+       if (lr != &core_request_world && lr)
+               kfree_rcu(lr, rcu_head);
 
-       last_request = pending_request;
-       last_request->intersect = intersect;
-       last_request->processed = false;
+       pending_request->intersect = intersect;
+       pending_request->processed = false;
+       rcu_assign_pointer(last_request, pending_request);
+       lr = pending_request;
 
        pending_request = NULL;
 
-       if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
-               user_alpha2[0] = last_request->alpha2[0];
-               user_alpha2[1] = last_request->alpha2[1];
+       if (lr->initiator == NL80211_REGDOM_SET_BY_USER) {
+               user_alpha2[0] = lr->alpha2[0];
+               user_alpha2[1] = lr->alpha2[1];
        }
 
        /* When r == REG_REQ_INTERSECT we do need to call CRDA */
@@ -1494,13 +1506,13 @@ new_request:
                 * inform userspace we have processed the request
                 */
                if (treatment == REG_REQ_ALREADY_SET) {
-                       nl80211_send_reg_change_event(last_request);
+                       nl80211_send_reg_change_event(lr);
                        reg_set_request_processed();
                }
                return treatment;
        }
 
-       if (call_crda(last_request->alpha2))
+       if (call_crda(lr->alpha2))
                return REG_REQ_IGNORE;
        return REG_REQ_OK;
 }
@@ -1543,13 +1555,14 @@ static void reg_process_hint(struct regulatory_request *reg_request,
  */
 static void reg_process_pending_hints(void)
 {
-       struct regulatory_request *reg_request;
+       struct regulatory_request *reg_request, *lr;
 
        mutex_lock(&cfg80211_mutex);
        mutex_lock(&reg_mutex);
+       lr = get_last_request();
 
        /* When last_request->processed becomes true this will be rescheduled */
-       if (last_request && !last_request->processed) {
+       if (lr && !lr->processed) {
                REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
                goto out;
        }
@@ -1581,11 +1594,8 @@ static void reg_process_pending_beacon_hints(void)
        struct cfg80211_registered_device *rdev;
        struct reg_beacon *pending_beacon, *tmp;
 
-       /*
-        * No need to hold the reg_mutex here as we just touch wiphys
-        * and do not read or access regulatory variables.
-        */
        mutex_lock(&cfg80211_mutex);
+       mutex_lock(&reg_mutex);
 
        /* This goes through the _pending_ beacon list */
        spin_lock_bh(&reg_pending_beacons_lock);
@@ -1603,6 +1613,7 @@ static void reg_process_pending_beacon_hints(void)
        }
 
        spin_unlock_bh(&reg_pending_beacons_lock);
+       mutex_unlock(&reg_mutex);
        mutex_unlock(&cfg80211_mutex);
 }
 
@@ -1702,11 +1713,12 @@ void regulatory_hint_11d(struct wiphy *wiphy, enum ieee80211_band band,
 {
        char alpha2[2];
        enum environment_cap env = ENVIRON_ANY;
-       struct regulatory_request *request;
+       struct regulatory_request *request, *lr;
 
        mutex_lock(&reg_mutex);
+       lr = get_last_request();
 
-       if (unlikely(!last_request))
+       if (unlikely(!lr))
                goto out;
 
        /* IE len must be evenly divisible by 2 */
@@ -1729,8 +1741,8 @@ void regulatory_hint_11d(struct wiphy *wiphy, enum ieee80211_band band,
         * We leave conflict resolution to the workqueue, where can hold
         * cfg80211_mutex.
         */
-       if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           last_request->wiphy_idx != WIPHY_IDX_INVALID)
+       if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
+           lr->wiphy_idx != WIPHY_IDX_INVALID)
                goto out;
 
        request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
@@ -1912,11 +1924,23 @@ static bool freq_is_chan_12_13_14(u16 freq)
        return false;
 }
 
+static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
+{
+       struct reg_beacon *pending_beacon;
+
+       list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
+               if (beacon_chan->center_freq ==
+                   pending_beacon->chan.center_freq)
+                       return true;
+       return false;
+}
+
 int regulatory_hint_found_beacon(struct wiphy *wiphy,
                                 struct ieee80211_channel *beacon_chan,
                                 gfp_t gfp)
 {
        struct reg_beacon *reg_beacon;
+       bool processing;
 
        if (beacon_chan->beacon_found ||
            beacon_chan->flags & IEEE80211_CHAN_RADAR ||
@@ -1924,6 +1948,13 @@ int regulatory_hint_found_beacon(struct wiphy *wiphy,
             !freq_is_chan_12_13_14(beacon_chan->center_freq)))
                return 0;
 
+       spin_lock_bh(&reg_pending_beacons_lock);
+       processing = pending_reg_beacon(beacon_chan);
+       spin_unlock_bh(&reg_pending_beacons_lock);
+
+       if (processing)
+               return 0;
+
        reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
        if (!reg_beacon)
                return -ENOMEM;
@@ -2021,13 +2052,12 @@ static void print_dfs_region(u8 dfs_region)
 
 static void print_regdomain(const struct ieee80211_regdomain *rd)
 {
+       struct regulatory_request *lr = get_last_request();
 
        if (is_intersected_alpha2(rd->alpha2)) {
-               if (last_request->initiator ==
-                   NL80211_REGDOM_SET_BY_COUNTRY_IE) {
+               if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
                        struct cfg80211_registered_device *rdev;
-                       rdev = cfg80211_rdev_by_wiphy_idx(
-                               last_request->wiphy_idx);
+                       rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
                        if (rdev) {
                                pr_info("Current regulatory domain updated by AP to: %c%c\n",
                                        rdev->country_ie_alpha2[0],
@@ -2042,7 +2072,7 @@ static void print_regdomain(const struct ieee80211_regdomain *rd)
                if (is_unknown_alpha2(rd->alpha2))
                        pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
                else {
-                       if (reg_request_cell_base(last_request))
+                       if (reg_request_cell_base(lr))
                                pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
                                        rd->alpha2[0], rd->alpha2[1]);
                        else
@@ -2067,11 +2097,10 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
        const struct ieee80211_regdomain *regd;
        const struct ieee80211_regdomain *intersected_rd = NULL;
        struct wiphy *request_wiphy;
+       struct regulatory_request *lr = get_last_request();
 
        /* Some basic sanity checks first */
 
-       assert_reg_lock();
-
        if (!reg_is_valid_request(rd->alpha2))
                return -EINVAL;
 
@@ -2089,7 +2118,7 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
         * rd is non static (it means CRDA was present and was used last)
         * and the pending request came in from a country IE
         */
-       if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
+       if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
                /*
                 * If someone else asked us to change the rd lets only bother
                 * checking if the alpha2 changes if CRDA was already called
@@ -2111,16 +2140,16 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
                return -EINVAL;
        }
 
-       request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
+       request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
        if (!request_wiphy &&
-           (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
-            last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
+           (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
+            lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
                schedule_delayed_work(&reg_timeout, 0);
                return -ENODEV;
        }
 
-       if (!last_request->intersect) {
-               if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
+       if (!lr->intersect) {
+               if (lr->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
                        reset_regdomains(false, rd);
                        return 0;
                }
@@ -2148,7 +2177,7 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
 
        /* Intersection requires a bit more work */
 
-       if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
+       if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
                intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
                if (!intersected_rd)
                        return -EINVAL;
@@ -2158,7 +2187,7 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
                 * However if a driver requested this specific regulatory
                 * domain we keep it for its private use
                 */
-               if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
+               if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER)
                        rcu_assign_pointer(request_wiphy->regd, rd);
                else
                        kfree(rd);
@@ -2181,9 +2210,11 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
  */
 int set_regdom(const struct ieee80211_regdomain *rd)
 {
+       struct regulatory_request *lr;
        int r;
 
        mutex_lock(&reg_mutex);
+       lr = get_last_request();
 
        /* Note that this doesn't update the wiphys, this is done below */
        r = __set_regdom(rd);
@@ -2196,18 +2227,17 @@ int set_regdom(const struct ieee80211_regdomain *rd)
        }
 
        /* This would make this whole thing pointless */
-       if (WARN_ON(!last_request->intersect &&
-                   rd != get_cfg80211_regdom())) {
+       if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom())) {
                r = -EINVAL;
                goto out;
        }
 
        /* update all wiphys now with the new established regulatory domain */
-       update_all_wiphy_regulatory(last_request->initiator);
+       update_all_wiphy_regulatory(lr->initiator);
 
        print_regdomain(get_cfg80211_regdom());
 
-       nl80211_send_reg_change_event(last_request);
+       nl80211_send_reg_change_event(lr);
 
        reg_set_request_processed();
 
@@ -2220,13 +2250,21 @@ int set_regdom(const struct ieee80211_regdomain *rd)
 #ifdef CONFIG_HOTPLUG
 int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 {
-       if (last_request && !last_request->processed) {
-               if (add_uevent_var(env, "COUNTRY=%c%c",
-                                  last_request->alpha2[0],
-                                  last_request->alpha2[1]))
-                       return -ENOMEM;
+       struct regulatory_request *lr;
+       u8 alpha2[2];
+       bool add = false;
+
+       rcu_read_lock();
+       lr = get_last_request();
+       if (lr && !lr->processed) {
+               memcpy(alpha2, lr->alpha2, 2);
+               add = true;
        }
+       rcu_read_unlock();
 
+       if (add)
+               return add_uevent_var(env, "COUNTRY=%c%c",
+                                     alpha2[0], alpha2[1]);
        return 0;
 }
 #else
@@ -2252,8 +2290,10 @@ void wiphy_regulatory_register(struct wiphy *wiphy)
 void wiphy_regulatory_deregister(struct wiphy *wiphy)
 {
        struct wiphy *request_wiphy = NULL;
+       struct regulatory_request *lr;
 
        mutex_lock(&reg_mutex);
+       lr = get_last_request();
 
        if (!reg_dev_ignore_cell_hint(wiphy))
                reg_num_devs_support_basehint--;
@@ -2261,14 +2301,14 @@ void wiphy_regulatory_deregister(struct wiphy *wiphy)
        rcu_free_regdom(get_wiphy_regdom(wiphy));
        rcu_assign_pointer(wiphy->regd, NULL);
 
-       if (last_request)
-               request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
+       if (lr)
+               request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
 
        if (!request_wiphy || request_wiphy != wiphy)
                goto out;
 
-       last_request->wiphy_idx = WIPHY_IDX_INVALID;
-       last_request->country_ie_env = ENVIRON_ANY;
+       lr->wiphy_idx = WIPHY_IDX_INVALID;
+       lr->country_ie_env = ENVIRON_ANY;
 out:
        mutex_unlock(&reg_mutex);
 }