Simplify 'cfg80211_mlme_register_mgmt()' to allocate an instance of
'struct cfg80211_mgmt_registration' only if the latter is really
needed (i.e. when the list of registrations should be updated)
and prefer 'kmalloc()' over 'kzalloc()' since all of the members
are explicitly initialized.

Fixes: 9dba48a6ece7 ("cfg80211: support multicast RX registration")
Signed-off-by: Dmitry Antipov <[email protected]>
---
 net/wireless/mlme.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/net/wireless/mlme.c b/net/wireless/mlme.c
index 4790136758d5..c7d913c76966 100644
--- a/net/wireless/mlme.c
+++ b/net/wireless/mlme.c
@@ -639,7 +639,7 @@ int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, 
u32 snd_portid,
                                struct netlink_ext_ack *extack)
 {
        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
-       struct cfg80211_mgmt_registration *reg, *nreg;
+       struct cfg80211_mgmt_registration *reg;
        int err = 0;
        u16 mgmt_type;
        bool update_multicast = false;
@@ -680,10 +680,6 @@ int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, 
u32 snd_portid,
                return -EINVAL;
        }
 
-       nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
-       if (!nreg)
-               return -ENOMEM;
-
        spin_lock_bh(&rdev->mgmt_registrations_lock);
 
        list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
@@ -707,9 +703,14 @@ int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, 
u32 snd_portid,
        if (err)
                goto out;
 
-       if (update_multicast) {
-               kfree(nreg);
-       } else {
+       if (!update_multicast) {
+               struct cfg80211_mgmt_registration *nreg =
+                       kmalloc(sizeof(*reg) + match_len, GFP_KERNEL);
+
+               if (!nreg) {
+                       err = -ENOMEM;
+                       goto out;
+               }
                nreg->match_len = match_len;
                memcpy(nreg->match, match_data, match_len);
                nreg->nlportid = snd_portid;
@@ -726,7 +727,6 @@ int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, 
u32 snd_portid,
        return 0;
 
  out:
-       kfree(nreg);
        spin_unlock_bh(&rdev->mgmt_registrations_lock);
 
        return err;
-- 
2.47.1


Reply via email to