Skip to content

Commit

Permalink
regulatory: use RCU to protect global and wiphy regdomains
Browse files Browse the repository at this point in the history
To simplify the locking and not require cfg80211_mutex
(which nl80211 uses to access the global regdomain) and
also to make it possible for drivers to access their
wiphy->regd safely, use RCU to protect these pointers.

Acked-by: Luis R. Rodriguez <mcgrof@do-not-panic.com>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
  • Loading branch information
Johannes Berg committed Jan 3, 2013
1 parent 379b82f commit 458f4f9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 66 deletions.
2 changes: 1 addition & 1 deletion include/net/cfg80211.h
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ struct wiphy {

/* fields below are read-only, assigned by cfg80211 */

const struct ieee80211_regdomain *regd;
const struct ieee80211_regdomain __rcu *regd;

/* the item in /sys/class/ieee80211/ points to this,
* you need use set_wiphy_dev() (see below) */
Expand Down
2 changes: 2 additions & 0 deletions include/net/regulatory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <linux/rcupdate.h>

/**
* enum environment_cap - Environment parsed from country IE
Expand Down Expand Up @@ -101,6 +102,7 @@ struct ieee80211_reg_rule {
};

struct ieee80211_regdomain {
struct rcu_head rcu_head;
u32 n_reg_rules;
char alpha2[2];
u8 dfs_region;
Expand Down
35 changes: 18 additions & 17 deletions net/wireless/nl80211.c
Original file line number Diff line number Diff line change
Expand Up @@ -3787,12 +3787,8 @@ static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
* window between nl80211_init() and regulatory_init(), if that is
* even possible.
*/
mutex_lock(&cfg80211_mutex);
if (unlikely(!cfg80211_regdomain)) {
mutex_unlock(&cfg80211_mutex);
if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
return -EINPROGRESS;
}
mutex_unlock(&cfg80211_mutex);

if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
return -EINVAL;
Expand Down Expand Up @@ -4152,6 +4148,7 @@ static int nl80211_update_mesh_config(struct sk_buff *skb,

static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
{
const struct ieee80211_regdomain *regdom;
struct sk_buff *msg;
void *hdr = NULL;
struct nlattr *nl_reg_rules;
Expand All @@ -4174,35 +4171,36 @@ static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
if (!hdr)
goto put_failure;

if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
cfg80211_regdomain->alpha2) ||
(cfg80211_regdomain->dfs_region &&
nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
cfg80211_regdomain->dfs_region)))
goto nla_put_failure;

if (reg_last_request_cell_base() &&
nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
NL80211_USER_REG_HINT_CELL_BASE))
goto nla_put_failure;

rcu_read_lock();
regdom = rcu_dereference(cfg80211_regdomain);

if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
(regdom->dfs_region &&
nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
goto nla_put_failure_rcu;

nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
if (!nl_reg_rules)
goto nla_put_failure;
goto nla_put_failure_rcu;

for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
for (i = 0; i < regdom->n_reg_rules; i++) {
struct nlattr *nl_reg_rule;
const struct ieee80211_reg_rule *reg_rule;
const struct ieee80211_freq_range *freq_range;
const struct ieee80211_power_rule *power_rule;

reg_rule = &cfg80211_regdomain->reg_rules[i];
reg_rule = &regdom->reg_rules[i];
freq_range = &reg_rule->freq_range;
power_rule = &reg_rule->power_rule;

nl_reg_rule = nla_nest_start(msg, i);
if (!nl_reg_rule)
goto nla_put_failure;
goto nla_put_failure_rcu;

if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
reg_rule->flags) ||
Expand All @@ -4216,17 +4214,20 @@ static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
power_rule->max_antenna_gain) ||
nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
power_rule->max_eirp))
goto nla_put_failure;
goto nla_put_failure_rcu;

nla_nest_end(msg, nl_reg_rule);
}
rcu_read_unlock();

nla_nest_end(msg, nl_reg_rules);

genlmsg_end(msg, hdr);
err = genlmsg_reply(msg, info);
goto out;

nla_put_failure_rcu:
rcu_read_unlock();
nla_put_failure:
genlmsg_cancel(msg, hdr);
put_failure:
Expand Down
Loading

0 comments on commit 458f4f9

Please sign in to comment.