Linux wireless drivers development
 help / color / mirror / Atom feed
From: Sarika Sharma <sarika.sharma@oss.qualcomm.com>
To: johannes@sipsolutions.net
Cc: linux-wireless@vger.kernel.org,
	Sarika Sharma <sarika.sharma@oss.qualcomm.com>
Subject: [PATCH wireless-next v2 1/2] wifi: cfg80211/mac80211: change memory allocation for link_sinfo structure
Date: Thu, 30 Apr 2026 11:08:09 +0530	[thread overview]
Message-ID: <20260430053810.2088793-2-sarika.sharma@oss.qualcomm.com> (raw)
In-Reply-To: <20260430053810.2088793-1-sarika.sharma@oss.qualcomm.com>

Currently, during the NL80211_CMD_GET_STATION call, cfg80211 allocates
memory for link_sinfo objects for all possible links, regardless
of whether they are valid for the station. However, mac80211 only
fills in link_sinfo for valid links, leading to unnecessary memory
consumption.

To optimize memory usage, introduce an API in cfg80211 to dynamically
allocate link_sinfo and the corresponding link tidstats objects.
Memory is allocated only for valid links during link_sinfo population
in mac80211.

Also, refactor cfg80211_sinfo_release_content() so that link_sinfo is
freed separately, keeping allocation and free paths symmetric.

Signed-off-by: Sarika Sharma <sarika.sharma@oss.qualcomm.com>
---
 include/net/cfg80211.h  | 28 ++++++++++++++++++++++++----
 net/mac80211/ethtool.c  |  4 ++++
 net/mac80211/sta_info.c | 14 +++++++++-----
 net/wireless/nl80211.c  | 29 ++++++-----------------------
 net/wireless/util.c     | 21 +++++++++++++++++++++
 5 files changed, 64 insertions(+), 32 deletions(-)

diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h
index 9d3639ff9c28..7e6fab00d07d 100644
--- a/include/net/cfg80211.h
+++ b/include/net/cfg80211.h
@@ -9146,6 +9146,28 @@ int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp);
 int cfg80211_link_sinfo_alloc_tid_stats(struct link_station_info *link_sinfo,
 					gfp_t gfp);
 
+/**
+ * cfg80211_alloc_link_sinfo_stats - allocate link_station_info
+ * @tidstats: indicate if per-tid stats are required
+ * @gfp: allocation flags
+ *
+ * Return: pointer on success, ERR_PTR() on  failure.
+ */
+struct link_station_info *
+cfg80211_alloc_link_sinfo_stats(bool tidstats, gfp_t gfp);
+
+/**
+ * cfg80211_free_link_sinfo - free the content and memory allocated for
+ *	link_sinfo
+ * @link_sinfo: the link_station information
+ */
+static inline void
+cfg80211_free_link_sinfo(struct link_station_info *link_sinfo)
+{
+	kfree(link_sinfo->pertid);
+	kfree(link_sinfo);
+}
+
 /**
  * cfg80211_sinfo_release_content - release contents of station info
  * @sinfo: the station information
@@ -9159,10 +9181,8 @@ static inline void cfg80211_sinfo_release_content(struct station_info *sinfo)
 	kfree(sinfo->pertid);
 
 	for (int link_id = 0; link_id < ARRAY_SIZE(sinfo->links); link_id++) {
-		if (sinfo->links[link_id]) {
-			kfree(sinfo->links[link_id]->pertid);
-			kfree(sinfo->links[link_id]);
-		}
+		if (sinfo->links[link_id])
+			cfg80211_free_link_sinfo(sinfo->links[link_id]);
 	}
 }
 
diff --git a/net/mac80211/ethtool.c b/net/mac80211/ethtool.c
index 3d365626faa4..780229e6bc6d 100644
--- a/net/mac80211/ethtool.c
+++ b/net/mac80211/ethtool.c
@@ -136,6 +136,8 @@ static void ieee80211_get_stats(struct net_device *dev,
 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))
 			data[i] = (u8)sinfo.signal_avg;
 		i++;
+		if (sinfo.valid_links)
+			cfg80211_sinfo_release_content(&sinfo);
 	} else {
 		list_for_each_entry(sta, &local->sta_list, list) {
 			/* Make sure this station belongs to the proper dev */
@@ -147,6 +149,8 @@ static void ieee80211_get_stats(struct net_device *dev,
 			i = 0;
 			ADD_STA_STATS(&sta->deflink);
 			data[i++] = sdata->tx_handlers_drop;
+			if (sinfo.valid_links)
+				cfg80211_sinfo_release_content(&sinfo);
 		}
 	}
 
diff --git a/net/mac80211/sta_info.c b/net/mac80211/sta_info.c
index 4c31ef8817ce..e6ed9375105c 100644
--- a/net/mac80211/sta_info.c
+++ b/net/mac80211/sta_info.c
@@ -2963,8 +2963,7 @@ static void sta_set_link_sinfo(struct sta_info *sta,
 				BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
 	}
 
-	if (tidstats && !cfg80211_link_sinfo_alloc_tid_stats(link_sinfo,
-							     GFP_KERNEL)) {
+	if (tidstats) {
 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
 			sta_set_tidstats(sta, &link_sinfo->pertid[i], i,
 					 link_id);
@@ -3252,6 +3251,7 @@ void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
 	}
 
 	if (sta->sta.valid_links) {
+		struct link_station_info *link_sinfo;
 		struct ieee80211_link_data *link;
 		struct link_sta_info *link_sta;
 		int link_id;
@@ -3267,12 +3267,16 @@ void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
 			link = wiphy_dereference(sdata->local->hw.wiphy,
 						 sdata->link[link_id]);
 
-			if (!link_sta || !sinfo->links[link_id] || !link) {
+			link_sinfo =
+				cfg80211_alloc_link_sinfo_stats(tidstats,
+								GFP_KERNEL);
+			if (!link_sta || !link || IS_ERR(link_sinfo)) {
 				sinfo->valid_links &= ~BIT(link_id);
 				continue;
 			}
-			sta_set_link_sinfo(sta, sinfo->links[link_id],
-					   link, tidstats);
+
+			sta_set_link_sinfo(sta, link_sinfo, link, tidstats);
+			sinfo->links[link_id] = link_sinfo;
 		}
 	}
 }
diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
index f334cdef8958..108583fb2cd2 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -8199,7 +8199,7 @@ static int nl80211_dump_station(struct sk_buff *skb,
 	u8 mac_addr[ETH_ALEN];
 	int sta_idx = cb->args[2];
 	bool sinfo_alloc = false;
-	int err, i;
+	int err;
 
 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
 	if (err)
@@ -8220,20 +8220,13 @@ static int nl80211_dump_station(struct sk_buff *skb,
 	while (1) {
 		memset(&sinfo, 0, sizeof(sinfo));
 
-		for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
-			sinfo.links[i] =
-				kzalloc_obj(*sinfo.links[0]);
-			if (!sinfo.links[i]) {
-				err = -ENOMEM;
-				goto out_err;
-			}
-			sinfo_alloc = true;
-		}
-
 		err = rdev_dump_station(rdev, wdev, sta_idx,
 					mac_addr, &sinfo);
 		if (err == -ENOENT)
 			break;
+
+		sinfo_alloc = true;
+
 		if (err)
 			goto out_err;
 
@@ -8273,7 +8266,7 @@ static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
 	struct station_info sinfo;
 	struct sk_buff *msg;
 	u8 *mac_addr = NULL;
-	int err, i;
+	int err;
 
 	memset(&sinfo, 0, sizeof(sinfo));
 
@@ -8288,19 +8281,9 @@ static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
 	if (!rdev->ops->get_station)
 		return -EOPNOTSUPP;
 
-	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
-		sinfo.links[i] = kzalloc_obj(*sinfo.links[0]);
-		if (!sinfo.links[i]) {
-			cfg80211_sinfo_release_content(&sinfo);
-			return -ENOMEM;
-		}
-	}
-
 	err = rdev_get_station(rdev, wdev, mac_addr, &sinfo);
-	if (err) {
-		cfg80211_sinfo_release_content(&sinfo);
+	if (err)
 		return err;
-	}
 
 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 	if (!msg) {
diff --git a/net/wireless/util.c b/net/wireless/util.c
index cff5a1bd95cc..53e7dfd988ac 100644
--- a/net/wireless/util.c
+++ b/net/wireless/util.c
@@ -2738,6 +2738,27 @@ int cfg80211_link_sinfo_alloc_tid_stats(struct link_station_info *link_sinfo,
 }
 EXPORT_SYMBOL(cfg80211_link_sinfo_alloc_tid_stats);
 
+struct link_station_info *
+cfg80211_alloc_link_sinfo_stats(bool tidstats, gfp_t gfp)
+{
+	struct link_station_info *link_sinfo;
+	int ret;
+
+	link_sinfo = kzalloc_obj(*link_sinfo, gfp);
+	if (!link_sinfo)
+		return ERR_PTR(-ENOMEM);
+
+	if (tidstats) {
+		ret = cfg80211_link_sinfo_alloc_tid_stats(link_sinfo, gfp);
+		if (ret) {
+			kfree(link_sinfo);
+			return ERR_PTR(ret);
+		}
+	}
+	return link_sinfo;
+}
+EXPORT_SYMBOL(cfg80211_alloc_link_sinfo_stats);
+
 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
 {
 	sinfo->pertid = kzalloc_objs(*(sinfo->pertid), IEEE80211_NUM_TIDS + 1,

base-commit: 1f5ffc672165ff851063a5fd044b727ab2517ae3
-- 
2.34.1


  reply	other threads:[~2026-04-30  5:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30  5:38 [PATCH wireless-next v2 0/2] wifi: cfg80211/mac80211: optimize station info handling Sarika Sharma
2026-04-30  5:38 ` Sarika Sharma [this message]
2026-04-30  5:38 ` [PATCH wireless-next v2 2/2] wifi: cfg80211/mac80211: set only non-MLO-applicable fields for non-MLO stations Sarika Sharma
2026-05-05 10:21 ` [PATCH wireless-next v2 0/2] wifi: cfg80211/mac80211: optimize station info handling Johannes Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260430053810.2088793-2-sarika.sharma@oss.qualcomm.com \
    --to=sarika.sharma@oss.qualcomm.com \
    --cc=johannes@sipsolutions.net \
    --cc=linux-wireless@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox