From b6c27cf5fe5d9a9872982889a1a9cb471bad20ed Mon Sep 17 00:00:00 2001 From: Johannes Berg Date: Sun, 11 Oct 2026 11:31:31 +0200 Subject: [PATCH] wifi: cfg80211: check wiphy name in wiphy_register() We currently check names already in wiphy_new_nm(), but that could technically race when two are registered at the same time. Currently that only happens with hwsim, which probably can't race, but it's confusing. Do the check later, with cfg80211_mutex held, so it'll always be precise. Assisted-by: LLM Link: https://patch.msgid.link/20261011113308.20e9fccb29da.I3a32d7fcfba5fd7b4ee378aa182d87e46f98c6e6@changeid Signed-off-by: Johannes Berg --- net/wireless/core.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/net/wireless/core.c b/net/wireless/core.c index f6ac022ea4bd..d2931b9d7e58 100644 --- a/net/wireless/core.c +++ b/net/wireless/core.c @@ -660,33 +660,13 @@ struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv, /* atomic_inc_return makes it start at 1, make it start at 0 */ rdev->wiphy_idx--; - /* give it a proper name */ - if (requested_name && requested_name[0]) { + /* remember the name, used/checked during registration */ + if (!requested_name || !requested_name[0] || + dev_set_name(&rdev->wiphy.dev, "%s", requested_name)) { int rv; - mutex_lock(&cfg80211_mutex); - rv = cfg80211_dev_check_name(rdev, requested_name); - - if (rv < 0) { - mutex_unlock(&cfg80211_mutex); - goto use_default_name; - } - - rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name); - mutex_unlock(&cfg80211_mutex); - if (rv) - goto use_default_name; - } else { - int rv; - -use_default_name: - /* NOTE: This is *probably* safe w/out holding rtnl because of - * the restrictions on phy names. Probably this call could - * fail if some other part of the kernel (re)named a device - * phyX. But, might should add some locking and check return - * value, and use a different name if this one exists? - */ - rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx); + rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", + rdev->wiphy_idx); if (rv < 0) { kfree(rdev); return NULL; @@ -1240,7 +1220,14 @@ int wiphy_register(struct wiphy *wiphy) rtnl_lock(); mutex_lock(&cfg80211_mutex); wiphy_lock(&rdev->wiphy); - res = device_add(&rdev->wiphy.dev); + + /* use default name if the requested one isn't good */ + res = cfg80211_dev_check_name(rdev, dev_name(&rdev->wiphy.dev)); + if (res) + res = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", + rdev->wiphy_idx); + if (!res) + res = device_add(&rdev->wiphy.dev); if (res) { wiphy_unlock(&rdev->wiphy); mutex_unlock(&cfg80211_mutex);