Skip to contentSkip to conversation

Avoid per WMI message tb allocation

RFC PATCH In review Nicolas Escande 4 participants 8 messages Feb 26 – Mar 3 Qualcomm ATH12K wireless driver

lore
Share & tools
lore
#0 lore raw

cover Avoid per WMI message tb allocation

So for a bit of context I have devices that have memory allocation faillures when parsing WMI messages such as bellow:

	[73277.177015] swapper/0: page allocation failure: order:2, mode:0x40920(GFP_ATOMIC|__GFP_COMP|__GFP_ZERO), nodemask=(null)
	[73277.177035] CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted XXX #109
	[73277.177040] Hardware name: XXX
	[73277.177043] Call trace:
	[73277.177045]  show_stack+0x14/0x20 (C)
	[73277.177054]  dump_stack_lvl+0x58/0x74
	[73277.177058]  dump_stack+0x14/0x1c
	[73277.177061]  warn_alloc+0xd4/0x150
	[73277.177065]  __alloc_pages_noprof+0x688/0xa10
	[73277.177068]  ___kmalloc_large_node+0x64/0xf8
	[73277.177072]  __kmalloc_large_noprof+0x10/0x20
	[73277.177075]  ath12k_wmi_tlv_parse_alloc.constprop.0+0x24/0x90 [ath12k]
	[73277.177095]  ath12k_wmi_op_rx+0x658/0x2624 [ath12k]
	[73277.177111]  ath12k_htc_rx_completion_handler+0x464/0x660 [ath12k]
	[73277.177125]  ath12k_ce_per_engine_service+0x2dc/0x3ec [ath12k]
	[73277.177140]  ath12k_pci_ce_workqueue+0x30/0x50 [ath12k]
	[73277.177155]  process_one_work+0x154/0x2e0
	[73277.177160]  bh_worker+0x1dc/0x228
	[73277.177164]  workqueue_softirq_action+0x74/0x80
	[73277.177168]  tasklet_action+0x10/0x40
	[73277.177172]  handle_softirqs+0xfc/0x240
	[73277.177175]  __do_softirq+0x10/0x18
	[73277.177178]  ____do_softirq+0xc/0x20
	...

So it seems that parsing a a WMI TLV requires an array of WMI_TAG_MAX (void *). This array is then populated with pointers of parsed structs depending on the WMI type. This alloc happens on a per WMI message, in softirq, using GFP_ATOMIC which puts pressure on the kernel allocator and can fail.

I'm not very familliar but from my understanding of the system_bh_wq, we can have at most one work item per CPU running this code path. So the following patch aims changes the code to have a per cpu array instead to parse the WMI msg and thus avoiding dynamic allocation.

What do you think ? Is this the right way forward ?

Nicolas Escande (1):
wifi: ath12k: avoid dynamic alloc when parsing wmi tb

 drivers/net/wireless/ath/ath12k/core.c |   7 +
 drivers/net/wireless/ath/ath12k/core.h |   2 +
 drivers/net/wireless/ath/ath12k/wmi.c  | 170 ++++++-------------------
 3 files changed, 49 insertions(+), 130 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

RFC PATCH 1/1 wifi: ath12k: avoid dynamic alloc when parsing wmi tb

On each WMI message received from the hardware, we alloc a temporary array of WMI_TAG_MAX entries of type void *. This array is then populated with pointers of parsed structs depending on the WMI type, and then freed. This alloc can fail when memory pressure in the system is high enough.

Given the fact that it is scheduled in softirq with the system_bh_wq, we should not be able to parse more than one WMI message per CPU at any time

So instead lets move to a per cpu allocated array, stored in the struct ath12k_base, that is reused accros calls. The ath12k_wmi_tlv_parse_alloc() is also renamed into / merged with ath12k_wmi_tlv_parse() as it no longer allocs memory but returns the existing per-cpu one.

---
 drivers/net/wireless/ath/ath12k/core.c |   7 +
 drivers/net/wireless/ath/ath12k/core.h |   2 +
 drivers/net/wireless/ath/ath12k/wmi.c  | 170 ++++++-------------------
 3 files changed, 49 insertions(+), 130 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +7 -0
index 9d6c50a94e64..961c9df69aa1 100644
@@ -2237,6 +2237,7 @@ void ath12k_core_free(struct ath12k_base *ab)
 	timer_delete_sync(&ab->rx_replenish_retry);
 	destroy_workqueue(ab->workqueue_aux);
 	destroy_workqueue(ab->workqueue);
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 }
 
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 	if (!ab)
 		return NULL;
 
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;
+
 	init_completion(&ab->driver_recovery);
 
 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
@@ -2296,6 +2302,7 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 err_free_wq:
 	destroy_workqueue(ab->workqueue);
 err_sc_free:
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 	return NULL;
 }
drivers/net/wireless/ath/ath12k/core.h +2 -0
index 990934ec92fc..378573a100e8 100644
@@ -19,6 +19,7 @@
 #include <linux/average.h>
 #include <linux/of.h>
 #include <linux/rhashtable.h>
+#include <linux/percpu.h>
 #include "qmi.h"
 #include "htc.h"
 #include "wmi.h"
@@ -934,6 +935,7 @@ struct ath12k_base {
 	struct device *dev;
 	struct ath12k_qmi qmi;
 	struct ath12k_wmi_base wmi_ab;
+	void __percpu *wmi_tb;
 	struct completion fw_ready;
 	u8 device_id;
 	int num_radios;
drivers/net/wireless/ath/ath12k/wmi.c +40 -130

733 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

Hello,

Quoted 33 lines · Nicolas Escande

On each WMI message received from the hardware, we alloc a temporary array of WMI_TAG_MAX entries of type void *. This array is then populated with pointers of parsed structs depending on the WMI type, and then freed. This alloc can fail when memory pressure in the system is high enough.

Given the fact that it is scheduled in softirq with the system_bh_wq, we should not be able to parse more than one WMI message per CPU at any time

So instead lets move to a per cpu allocated array, stored in the struct ath12k_base, that is reused accros calls. The ath12k_wmi_tlv_parse_alloc() is also renamed into / merged with ath12k_wmi_tlv_parse() as it no longer allocs memory but returns the existing per-cpu one.

---
 drivers/net/wireless/ath/ath12k/core.c |   7 +
 drivers/net/wireless/ath/ath12k/core.h |   2 +
 drivers/net/wireless/ath/ath12k/wmi.c  | 170 ++++++-------------------
 3 files changed, 49 insertions(+), 130 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +1 -0
index 9d6c50a94e64..961c9df69aa1 100644
@@ -2237,6 +2237,7 @@ void ath12k_core_free(struct ath12k_base *ab)
 	timer_delete_sync(&ab->rx_replenish_retry);
 	destroy_workqueue(ab->workqueue_aux);
 	destroy_workqueue(ab->workqueue);
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 }
 

drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2256

Quoted 7 lines
drivers/net/wireless/ath/ath12k/core.c +4 -0
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 	if (!ab)
 		return NULL;
 
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;

If !ab->wmi_tb, you're going to free_percpu(ab->wmi_tb); it works but it's not super pretty.

Quoted 12 lines
drivers/net/wireless/ath/ath12k/core.c +2 -0
+
 	init_completion(&ab->driver_recovery);
 
 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
@@ -2296,6 +2302,7 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 err_free_wq:
 	destroy_workqueue(ab->workqueue);
 err_sc_free:
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 	return NULL;
 }

[...]

drivers/net/wireless/ath/ath12k/core.c ath12k_pull_pdev_temp_ev(struct ath12k_base *ab, struct sk_buff *skb, · line 6756

Quoted 11 lines
drivers/net/wireless/ath/ath12k/core.c +0 -2
@@ -6795,11 +6749,9 @@ ath12k_pull_pdev_temp_ev(struct ath12k_base *ab, struct sk_buff *skb,
 	ev = tb[WMI_TAG_PDEV_TEMPERATURE_EVENT];
 	if (!ev) {
 		ath12k_warn(ab, "failed to fetch pdev temp ev");
-		kfree(tb);
 		return -EPROTO;
 	}
 
-	kfree(tb);
 	return 0;
 }

You're missing a change on ath12k_reg_11d_new_cc_event (was added by 591de41d7008585f2e7c35dbcf5922fcb4d79e39)

[...]

Best regards,
Pablo MG

#3 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

On Thu Feb 26, 2026 at 8:48 PM CET, Pablo MARTIN-GOMEZ wrote:
[...]

Quoted 9 lines

drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2256

Quoted 8 lines
drivers/net/wireless/ath/ath12k/core.c +4 -0
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 	if (!ab)
 		return NULL;
 
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;

If !ab->wmi_tb, you're going to free_percpu(ab->wmi_tb); it works but it's not super pretty.

Yes it's by design, we do not need to null check before free() and it avoids adding a new label that would cause more code chrun

Quoted 21 lines
Quoted 12 lines
drivers/net/wireless/ath/ath12k/core.c +2 -0
+
 	init_completion(&ab->driver_recovery);
 
 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
@@ -2296,6 +2302,7 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 err_free_wq:
 	destroy_workqueue(ab->workqueue);
 err_sc_free:
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 	return NULL;
 }

[...]

Quoted 8 lines
drivers/net/wireless/ath/ath12k/wmi.c +0 -1
@@ -6795,11 +6749,9 @@ ath12k_pull_pdev_temp_ev(struct ath12k_base *ab, struct sk_buff *skb,
 	ev = tb[WMI_TAG_PDEV_TEMPERATURE_EVENT];
 	if (!ev) {
 		ath12k_warn(ab, "failed to fetch pdev temp ev");
-		kfree(tb);
 		return -EPROTO;
 	}
 
Quoted 4 lines

drivers/net/wireless/ath/ath12k/wmi.c static int ath12k_pull_vdev_start_resp_tlv(struct ath12k_base *ab, struct sk_buf · line 5723

Quoted 3 lines
drivers/net/wireless/ath/ath12k/wmi.c +0 -1
-	kfree(tb);
 	return 0;
 }

You're missing a change on ath12k_reg_11d_new_cc_event (was added by 591de41d7008585f2e7c35dbcf5922fcb4d79e39)

Indeed, this was ported from an older kernel, I'll fix before non RFC submission if there is interrest from QCA around this feature.

Quoted 4 lines

[...]

Best regards,
Pablo MG

#4 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

In reply to Nicolas Escande
Quoted 65 lines · Nicolas Escande

On each WMI message received from the hardware, we alloc a temporary array of WMI_TAG_MAX entries of type void *. This array is then populated with pointers of parsed structs depending on the WMI type, and then freed. This alloc can fail when memory pressure in the system is high enough.

Given the fact that it is scheduled in softirq with the system_bh_wq, we should not be able to parse more than one WMI message per CPU at any time

So instead lets move to a per cpu allocated array, stored in the struct ath12k_base, that is reused accros calls. The ath12k_wmi_tlv_parse_alloc() is also renamed into / merged with ath12k_wmi_tlv_parse() as it no longer allocs memory but returns the existing per-cpu one.

---
 drivers/net/wireless/ath/ath12k/core.c |   7 +
 drivers/net/wireless/ath/ath12k/core.h |   2 +
 drivers/net/wireless/ath/ath12k/wmi.c  | 170 ++++++-------------------
 3 files changed, 49 insertions(+), 130 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +7 -0
index 9d6c50a94e64..961c9df69aa1 100644
@@ -2237,6 +2237,7 @@ void ath12k_core_free(struct ath12k_base *ab)
 	timer_delete_sync(&ab->rx_replenish_retry);
 	destroy_workqueue(ab->workqueue_aux);
 	destroy_workqueue(ab->workqueue);
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 }
 
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 	if (!ab)
 		return NULL;
 
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;
+
 	init_completion(&ab->driver_recovery);
 
 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
@@ -2296,6 +2302,7 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
 err_free_wq:
 	destroy_workqueue(ab->workqueue);
 err_sc_free:
+	free_percpu(ab->wmi_tb);
 	kfree(ab);
 	return NULL;
 }
drivers/net/wireless/ath/ath12k/core.h +1 -0
index 990934ec92fc..378573a100e8 100644
@@ -19,6 +19,7 @@
 #include <linux/average.h>
 #include <linux/of.h>
 #include <linux/rhashtable.h>
+#include <linux/percpu.h>
 #include "qmi.h"
 #include "htc.h"
 #include "wmi.h"

drivers/net/wireless/ath/ath12k/core.h struct ath12k_base { · line 938

Quoted 4 lines
drivers/net/wireless/ath/ath12k/core.h +1 -0
@@ -934,6 +935,7 @@ struct ath12k_base {
 	struct device *dev;
 	struct ath12k_qmi qmi;
 	struct ath12k_wmi_base wmi_ab;
+	void __percpu *wmi_tb;

instead of allocating it per device, how about making it global and allocating once when loading driver. This way we may save some memory in case where more than one devices get probed?

#5 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

On Fri Feb 27, 2026 at 4:18 AM CET, Baochen Qiang wrote:
[...]

Quoted 13 lines
Quoted 13 lines
drivers/net/wireless/ath/ath12k/core.h +1 -0
index 990934ec92fc..378573a100e8 100644
@@ -19,6 +19,7 @@
 #include <linux/average.h>
 #include <linux/of.h>
 #include <linux/rhashtable.h>
+#include <linux/percpu.h>
 #include "qmi.h"
 #include "htc.h"
 #include "wmi.h"
@@ -934,6 +935,7 @@ struct ath12k_base {
Quoted 8 lines

drivers/net/wireless/ath/ath12k/core.h struct ath12k_base { · line 938

Quoted 4 lines
drivers/net/wireless/ath/ath12k/core.h +1 -0
 	struct device *dev;
 	struct ath12k_qmi qmi;
 	struct ath12k_wmi_base wmi_ab;
+	void __percpu *wmi_tb;

instead of allocating it per device, how about making it global and allocating once when loading driver. This way we may save some memory in case where more than one devices get probed?

Yes I can do that.

I'll wait a few days to see if there's more feedback from you guys and I post something more polished then. And I'll probably post something for ATH11K, it seems to have the same pattern.

Thx for the review

#6 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

In reply to Nicolas Escande
Quoted 33 lines · Nicolas Escande

On each WMI message received from the hardware, we alloc a temporary array of WMI_TAG_MAX entries of type void *. This array is then populated with pointers of parsed structs depending on the WMI type, and then freed. This alloc can fail when memory pressure in the system is high enough.

Given the fact that it is scheduled in softirq with the system_bh_wq, we should not be able to parse more than one WMI message per CPU at any time

So instead lets move to a per cpu allocated array, stored in the struct ath12k_base, that is reused accros calls. The ath12k_wmi_tlv_parse_alloc() is also renamed into / merged with ath12k_wmi_tlv_parse() as it no longer allocs memory but returns the existing per-cpu one.

---
  drivers/net/wireless/ath/ath12k/core.c |   7 +
  drivers/net/wireless/ath/ath12k/core.h |   2 +
  drivers/net/wireless/ath/ath12k/wmi.c  | 170 ++++++-------------------
  3 files changed, 49 insertions(+), 130 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +1 -0
index 9d6c50a94e64..961c9df69aa1 100644
@@ -2237,6 +2237,7 @@ void ath12k_core_free(struct ath12k_base *ab)
  	timer_delete_sync(&ab->rx_replenish_retry);
  	destroy_workqueue(ab->workqueue_aux);
  	destroy_workqueue(ab->workqueue);
+	free_percpu(ab->wmi_tb);
  	kfree(ab);
  }
 

drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2257

Quoted 8 lines
drivers/net/wireless/ath/ath12k/core.c +5 -0
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
  	if (!ab)
  		return NULL;
 
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;
+

we could move this allocation after workqueue_aux alloc and avoid free_percpu(ab->wmi_tb); in the label

Quoted 49 lines
drivers/net/wireless/ath/ath12k/core.c +1 -0
  	init_completion(&ab->driver_recovery);
 
  	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
@@ -2296,6 +2302,7 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
  err_free_wq:
  	destroy_workqueue(ab->workqueue);
  err_sc_free:
+	free_percpu(ab->wmi_tb);
  	kfree(ab);
  	return NULL;
  }
drivers/net/wireless/ath/ath12k/core.h +2 -0
index 990934ec92fc..378573a100e8 100644
@@ -19,6 +19,7 @@
  #include <linux/average.h>
  #include <linux/of.h>
  #include <linux/rhashtable.h>
+#include <linux/percpu.h>
  #include "qmi.h"
  #include "htc.h"
  #include "wmi.h"
@@ -934,6 +935,7 @@ struct ath12k_base {
  	struct device *dev;
  	struct ath12k_qmi qmi;
  	struct ath12k_wmi_base wmi_ab;
+	void __percpu *wmi_tb;
  	struct completion fw_ready;
  	u8 device_id;
  	int num_radios;
drivers/net/wireless/ath/ath12k/wmi.c +0 -9
index 404f031a3c87..9ccc10c07c21 100644
@@ -289,29 +289,19 @@ static int ath12k_wmi_tlv_iter_parse(struct ath12k_base *ab, u16 tag, u16 len,
  	return 0;
  }
 
-static int ath12k_wmi_tlv_parse(struct ath12k_base *ar, const void **tb,
-				const void *ptr, size_t len)
-{
-	return ath12k_wmi_tlv_iter(ar, ptr, len, ath12k_wmi_tlv_iter_parse,
-				   (void *)tb);
-}
-
  static const void **
-ath12k_wmi_tlv_parse_alloc(struct ath12k_base *ab,
-			   struct sk_buff *skb, gfp_t gfp)

drivers/net/wireless/ath/ath12k/wmi.c static int ath12k_wmi_tlv_iter_parse(struct ath12k_base *ab, u16 tag, u16 len, · line 299

Quoted 10 lines
drivers/net/wireless/ath/ath12k/wmi.c +3 -3
+ath12k_wmi_tlv_parse(struct ath12k_base *ab, struct sk_buff *skb)
  {
  	const void **tb;
  	int ret;
 
-	tb = kcalloc(WMI_TAG_MAX, sizeof(*tb), gfp);
-	if (!tb)
-		return ERR_PTR(-ENOMEM);
+	tb = this_cpu_ptr(ab->wmi_tb);
+	memset(tb, 0, WMI_TAG_MAX * sizeof(void *));

prefer sizeof(*tb) over sizeof(void *)

Quoted 161 lines
drivers/net/wireless/ath/ath12k/wmi.c +7 -22
-	ret = ath12k_wmi_tlv_parse(ab, tb, skb->data, skb->len);
-	if (ret) {
-		kfree(tb);
+	ret = ath12k_wmi_tlv_iter(ab, skb->data, skb->len,
+				  ath12k_wmi_tlv_iter_parse, (void *)tb);
+	if (ret)
  		return ERR_PTR(ret);
-	}
 
  	return tb;
  }
@@ -5714,7 +5704,7 @@ static int ath12k_pull_vdev_start_resp_tlv(struct ath12k_base *ab, struct sk_buf
  	const struct wmi_vdev_start_resp_event *ev;
  	int ret;
 
-	tb = ath12k_wmi_tlv_parse_alloc(ab, skb, GFP_ATOMIC);
+	tb = ath12k_wmi_tlv_parse(ab, skb);
  	if (IS_ERR(tb)) {
  		ret = PTR_ERR(tb);
  		ath12k_warn(ab, "failed to parse tlv: %d\n", ret);
@@ -5724,13 +5714,11 @@ static int ath12k_pull_vdev_start_resp_tlv(struct ath12k_base *ab, struct sk_buf
  	ev = tb[WMI_TAG_VDEV_START_RESPONSE_EVENT];
  	if (!ev) {
  		ath12k_warn(ab, "failed to fetch vdev start resp ev");
-		kfree(tb);
  		return -EPROTO;
  	}
 
  	*vdev_rsp = *ev;
 
-	kfree(tb);
  	return 0;
  }
 
@@ -5809,7 +5797,7 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
 
  	ath12k_dbg(ab, ATH12K_DBG_WMI, "processing regulatory ext channel list\n");
 
-	tb = ath12k_wmi_tlv_parse_alloc(ab, skb, GFP_ATOMIC);
+	tb = ath12k_wmi_tlv_parse(ab, skb);
  	if (IS_ERR(tb)) {
  		ret = PTR_ERR(tb);
  		ath12k_warn(ab, "failed to parse tlv: %d\n", ret);
@@ -5819,7 +5807,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  	ev = tb[WMI_TAG_REG_CHAN_LIST_CC_EXT_EVENT];
  	if (!ev) {
  		ath12k_warn(ab, "failed to fetch reg chan list ext update ev\n");
-		kfree(tb);
  		return -EPROTO;
  	}
 
@@ -5849,7 +5836,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  	if (num_2g_reg_rules > MAX_REG_RULES || num_5g_reg_rules > MAX_REG_RULES) {
  		ath12k_warn(ab, "Num reg rules for 2G/5G exceeds max limit (num_2g_reg_rules: %d num_5g_reg_rules: %d max_rules: %d)\n",
  			    num_2g_reg_rules, num_5g_reg_rules, MAX_REG_RULES);
-		kfree(tb);
  		return -EINVAL;
  	}
 
@@ -5859,7 +5845,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  		if (num_6g_reg_rules_ap[i] > MAX_6GHZ_REG_RULES) {
  			ath12k_warn(ab, "Num 6G reg rules for AP mode(%d) exceeds max limit (num_6g_reg_rules_ap: %d, max_rules: %d)\n",
  				    i, num_6g_reg_rules_ap[i], MAX_6GHZ_REG_RULES);
-			kfree(tb);
  			return -EINVAL;
  		}
 
@@ -5884,14 +5869,12 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  		    num_6g_reg_rules_cl[WMI_REG_VLP_AP][i] >  MAX_6GHZ_REG_RULES) {
  			ath12k_warn(ab, "Num 6g client reg rules exceeds max limit, for client(type: %d)\n",
  				    i);
-			kfree(tb);
  			return -EINVAL;
  		}
  	}
 
  	if (!total_reg_rules) {
  		ath12k_warn(ab, "No reg rules available\n");
-		kfree(tb);
  		return -EINVAL;
  	}
 
@@ -5993,7 +5976,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  						      ext_wmi_reg_rule);
 
  		if (!reg_info->reg_rules_2g_ptr) {
-			kfree(tb);
  			ath12k_warn(ab, "Unable to Allocate memory for 2g rules\n");
  			return -ENOMEM;
  		}
@@ -6027,7 +6009,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  						      ext_wmi_reg_rule);
 
  		if (!reg_info->reg_rules_5g_ptr) {
-			kfree(tb);
  			ath12k_warn(ab, "Unable to Allocate memory for 5g rules\n");
  			return -ENOMEM;
  		}
@@ -6046,7 +6027,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  						      ext_wmi_reg_rule);
 
  		if (!reg_info->reg_rules_6g_ap_ptr[i]) {
-			kfree(tb);
  			ath12k_warn(ab, "Unable to Allocate memory for 6g ap rules\n");
  			return -ENOMEM;
  		}
@@ -6061,7 +6041,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
  							      ext_wmi_reg_rule);
 
  			if (!reg_info->reg_rules_6g_client_ptr[j][i]) {
-				kfree(tb);
  				ath12k_warn(ab, "Unable to Allocate memory for 6g client rules\n");
  				return -ENOMEM;
  			}
@@ -6096,7 +6075,6 @@ static int ath12k_pull_reg_chan_list_ext_update_ev(struct ath12k_base *ab,
 
  	ath12k_dbg(ab, ATH12K_DBG_WMI, "processed regulatory ext channel list\n");
 
-	kfree(tb);
  	return 0;
  }
 
@@ -6107,7 +6085,7 @@ static int ath12k_pull_peer_del_resp_ev(struct ath12k_base *ab, struct sk_buff *
  	const struct wmi_peer_delete_resp_event *ev;
  	int ret;
 
-	tb = ath12k_wmi_tlv_parse_alloc(ab, skb, GFP_ATOMIC);
+	tb = ath12k_wmi_tlv_parse(ab, skb);
  	if (IS_ERR(tb)) {
  		ret = PTR_ERR(tb);
  		ath12k_warn(ab, "failed to parse tlv: %d\n", ret);
@@ -6117,7 +6095,6 @@ static int ath12k_pull_peer_del_resp_ev(struct ath12k_base *ab, struct sk_buff *
  	ev = tb[WMI_TAG_PEER_DELETE_RESP_EVENT];
  	if (!ev) {
  		ath12k_warn(ab, "failed to fetch peer delete resp ev");
-		kfree(tb);
  		return -EPROTO;
  	}
 
@@ -6127,7 +6104,6 @@ static int ath12k_pull_peer_del_resp_ev(struct ath12k_base *ab, struct sk_buff *
  	ether_addr_copy(peer_del_resp->peer_macaddr.addr,
  			ev->peer_macaddr.addr);
 
-	kfree(tb);
  	return 0;
  }
 
@@ -6139,7 +6115,7 @@ static int ath12k_pull_vdev_del_resp_ev(struct ath12k_base *ab,
  	const struct wmi_vdev_delete_resp_event *ev;
  	int ret;
 
-	tb = ath12k_wmi_tlv_parse_alloc(ab, skb, GFP_ATOMIC);
+	tb = ath12k_wmi_tlv_parse(ab, skb);
  	if (IS_ERR(tb)) {
  		ret = PTR_ERR(tb);
  		ath12k_warn(ab, "failed to parse tlv: %d\n", ret);
@@ -6149,13 +6125,11 @@ static int ath12k_pull_vdev_del_resp_ev(struct ath12k_base *ab,
  	ev = tb[WMI_TAG_VDEV_DELETE_RESP_EVENT];
  	if (!ev) {
  		ath12k_warn(ab, "failed to fetch vdev delete resp ev");

drivers/net/wireless/ath/ath12k/wmi.c static int ath12k_pull_vdev_del_resp_ev(struct ath12k_base *ab, · line 6135

Quoted 10 lines
drivers/net/wireless/ath/ath12k/wmi.c +0 -2
-		kfree(tb);
  		return -EPROTO;
  	}
 
  	*vdev_id = le32_to_cpu(ev->vdev_id);
 
-	kfree(tb);
  	return 0;
  }
 

and one more case to be handled, ath12k_wmi_obss_color_collision_event()
const void **tb __free(kfree) = ath12k_wmi_tlv_parse_alloc(ab, skb,
GFP_ATOMIC);

Apart from this, the approach looks like a reasonable fix for the issue.

Another possible direction could be to define WMI event‑specific structures (or possibly a union of structs) and then leverage ath12k_wmi_tlv_parse() to extract only the required tags in a typed manner.

That said, such an approach would require a fair amount of refactoring, so sticking with the per‑CPU scratch buffer for now seems pragmatic. Moving towards a typed‑parse model incrementally could be something we consider over time.

--
Ramesh

#7 lore raw

Re: [RFC PATCH 1/1] wifi: ath12k: avoid dynamic alloc when parsing wmi tb

On Tue Mar 3, 2026 at 10:53 AM CET, Rameshkumar Sundaram wrote:
[...]

Quoted 5 lines
Quoted 5 lines
drivers/net/wireless/ath/ath12k/core.c +0 -0
@@ -2249,6 +2250,11 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
  	if (!ab)
  		return NULL;
 
Quoted 10 lines

drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2257

Quoted 5 lines
drivers/net/wireless/ath/ath12k/core.c +5 -0
+	ab->wmi_tb = __alloc_percpu(WMI_TAG_MAX * sizeof(void *),
+				    __alignof__(void *));
+	if (!ab->wmi_tb)
+		goto err_sc_free;
+

we could move this allocation after workqueue_aux alloc and avoid free_percpu(ab->wmi_tb); in the label

I'll look into it.

Quoted 3 lines
Quoted 3 lines
drivers/net/wireless/ath/ath12k/core.c +0 -0
  	init_completion(&ab->driver_recovery);
 
  	ab->workqueue = create_singlethread_workqueue("ath12k_wq");

[...]

Quoted 7 lines

drivers/net/wireless/ath/ath12k/wmi.c static int ath12k_wmi_tlv_iter_parse(struct ath12k_base *ab, u16 tag, u16 len, · line 299

Quoted 5 lines
drivers/net/wireless/ath/ath12k/wmi.c +2 -3
-	tb = kcalloc(WMI_TAG_MAX, sizeof(*tb), gfp);
-	if (!tb)
-		return ERR_PTR(-ENOMEM);
+	tb = this_cpu_ptr(ab->wmi_tb);
+	memset(tb, 0, WMI_TAG_MAX * sizeof(void *));

prefer sizeof(*tb) over sizeof(void *)

sure

Quoted 13 lines
Quoted 13 lines
drivers/net/wireless/ath/ath12k/wmi.c +3 -4
-	ret = ath12k_wmi_tlv_parse(ab, tb, skb->data, skb->len);
-	if (ret) {
-		kfree(tb);
+	ret = ath12k_wmi_tlv_iter(ab, skb->data, skb->len,
+				  ath12k_wmi_tlv_iter_parse, (void *)tb);
+	if (ret)
  		return ERR_PTR(ret);
-	}
 
  	return tb;
  }

[...]

Quoted 6 lines

and one more case to be handled, ath12k_wmi_obss_color_collision_event()
const void **tb __free(kfree) = ath12k_wmi_tlv_parse_alloc(ab, skb,
GFP_ATOMIC);

Sure as I said, this was whipped up from an older kernel to see if there was interrest. I'll fix all usages for the proper submission

Quoted 3 lines

Apart from this, the approach looks like a reasonable fix for the issue.

Good

Quoted 4 lines

Another possible direction could be to define WMI event‑specific structures (or possibly a union of structs) and then leverage ath12k_wmi_tlv_parse() to extract only the required tags in a typed manner.

I looked briefly into this as it seems it was in fact done that way in ath10k. But as in some rare occasion there are more than one single struct that gets parsed from one wmi message, I guess thats the reason it was changed to this pattern in ath11k.

Quoted 5 lines

That said, such an approach would require a fair amount of refactoring, so sticking with the per‑CPU scratch buffer for now seems pragmatic. Moving towards a typed‑parse model incrementally could be something we consider over time.

Indeed you would gain back type safety but this would mean a fair amount of code change.

Quoted 3 lines

--
Ramesh