wifi: ath12k: avoid dynamic alloc when parsing wmi tb
PATCH v3 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, that is reused across
calls: ath12K_wmi_tb that lives in wmi.c of the ath12K module. To alloc & free we added two new module_init/exit functions for the module and two new wmi functions to alloc/free this memory.
ath12k_wmi_tlv_parse_alloc() and ath12k_wmi_tlv_parse() are merged together as it no longer allocs mem but returns the existing per-cpu one.
---
changes from v2:
- removed now superfluous return in ath12k_wmi_event_teardown_complete()
- moved ath12k_wmi_tb declaration to wmi.c & added two functions to alloc / free it
- removed useless error message on memory allocation failure
changes from v1:
- rebased on ath-next 27401c9b1432
- changed wording according to Jeff's comment
- moved alloc/cleanup to new module_init/exit functions in the ath12k module as per Baochen's comment
changes from RFC:
- rebased on ath-next 8e0ab5b9adb7
- converted missing call sites ath12k_wmi_obss_color_collision_event() & ath12k_wmi_pdev_temperature_event()
- changed alloc order & cleanup path in ath12k_core_alloc() as it seems it confused people
- used sizeof(*tb) in ath12k_wmi_tlv_parse()
--- drivers/net/wireless/ath/ath12k/core.c | 19 +++ drivers/net/wireless/ath/ath12k/core.h | 1 + drivers/net/wireless/ath/ath12k/wmi.c | 200 ++++++++----------------- drivers/net/wireless/ath/ath12k/wmi.h | 3 + 4 files changed, 84 insertions(+), 139 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +19 -0
@@ -2321,5 +2321,24 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; } +static int ath12k_init (void ) +{ +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return ret; + +return 0 ; +} + +static void ath12k_exit (void ) +{ +ath12k_wmi_free (); +} + +module_init (ath12k_init); +module_exit (ath12k_exit); +MODULE_DESCRIPTION ("Driver support for Qualcomm Technologies WLAN devices" );MODULE_LICENSE ("Dual BSD/GPL" );
drivers/net/wireless/ath/ath12k/core.h +1 -0
@@ -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/wmi.c +61 -139
800 lines not on this page · raw message
drivers/net/wireless/ath/ath12k/wmi.h +3 -0
@@ -6572,4 +6572,7 @@ int ath12k_wmi_send_vdev_set_tpc_power(struct ath12k *ar, struct ath12k_reg_tpc_power_info *param);int ath12k_wmi_send_mlo_link_set_active_cmd (struct ath12k_base *ab,struct wmi_mlo_link_set_active_arg *param); +int ath12k_wmi_alloc (void ); +void ath12k_wmi_free (void ); +#endif
Signature · 1 line
2.53.0
Quoted 51 lines · Nicolas Escande
On each WMI message received from the hardware, we alloc a temporary array of
WMI_TAG_MAXentries 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, that is reused across
calls:ath12K_wmi_tbthat lives inwmi.cof the ath12K module. To alloc & free we added two new module_init/exit functions for the module and two new wmi functions to alloc/free this memory.
ath12k_wmi_tlv_parse_alloc()andath12k_wmi_tlv_parse()are merged together as it no longer allocs mem but returns the existing per-cpu one.Signed-off-by Nicolas Escande <[email protected]>---
changes from v2:
- removed now superfluous return inath12k_wmi_event_teardown_complete()
- movedath12k_wmi_tbdeclaration towmi.c& added two functions to alloc / free it
- removed useless error message on memory allocation failurechanges from v1:
- rebased on ath-next27401c9b1432
- changed wording according to Jeff's comment
- moved alloc/cleanup to new module_init/exit functions in the ath12k module as per Baochen's commentchanges from RFC:
- rebased on ath-next8e0ab5b9adb7
- converted missing call sitesath12k_wmi_obss_color_collision_event()&ath12k_wmi_pdev_temperature_event()
- changed alloc order & cleanup path inath12k_core_alloc()as it seems it confused people
- usedsizeof(*tb)inath12k_wmi_tlv_parse()--- drivers/net/wireless/ath/ath12k/core.c | 19 +++ drivers/net/wireless/ath/ath12k/core.h | 1 + drivers/net/wireless/ath/ath12k/wmi.c | 200 ++++++++----------------- drivers/net/wireless/ath/ath12k/wmi.h | 3 + 4 files changed, 84 insertions(+), 139 deletions(-)drivers/net/wireless/ath/ath12k/core.c +0
-0@@ -2321,5 +2321,24 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; }
drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2332
Quoted 10 lines
drivers/net/wireless/ath/ath12k/core.c +9
-0+static int ath12k_init (void ) +{ +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return ret; + +return 0 ;
nit: you can simply
return ath12k_wmi_alloc();
Quoted 17 lines
drivers/net/wireless/ath/ath12k/core.c +10
-0+} + +static void ath12k_exit (void ) +{ +ath12k_wmi_free (); +} + +module_init (ath12k_init); +module_exit (ath12k_exit); +MODULE_DESCRIPTION ("Driver support for Qualcomm Technologies WLAN devices" );MODULE_LICENSE ("Dual BSD/GPL" );drivers/net/wireless/ath/ath12k/core.h +0
-0
drivers/net/wireless/ath/ath12k/core.h line 22
Quoted 4 lines
drivers/net/wireless/ath/ath12k/core.h +1
-0@@ -19,6 +19,7 @@#include <linux/average.h> #include <linux/of.h> #include <linux/rhashtable.h> +#include <linux/percpu.h>
does it make more sense to add this to wmi.c instead?
On Mon Mar 16, 2026 at 11:42 AM CET, Baochen Qiang wrote:
[...]
Quoted 18 lines
Quoted 5 lines
drivers/net/wireless/ath/ath12k/core.c +0
-0@@ -2321,5 +2321,24 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; }drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2332
Quoted 10 lines
drivers/net/wireless/ath/ath12k/core.c +9
-0+static int ath12k_init (void ) +{ +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return ret; + +return 0 ;nit: you can simply
Quoted 1 line
return ath12k_wmi_alloc();
Yes we can, but I did this in case we need to add more stuff in that function at some later point. It would generate less code chrun. But I'm ok with a direct return statement if you think that's better. Just tell me and I'll send the v4.
Quoted 16 lines
Quoted 16 lines
drivers/net/wireless/ath/ath12k/core.c +10
-0+} + +static void ath12k_exit (void ) +{ +ath12k_wmi_free (); +} + +module_init (ath12k_init); +module_exit (ath12k_exit); +MODULE_DESCRIPTION ("Driver support for Qualcomm Technologies WLAN devices" );MODULE_LICENSE ("Dual BSD/GPL" );drivers/net/wireless/ath/ath12k/core.h +0
-1--- a/drivers/net/wireless/ath/ath12k/core.h
Quoted 8 lines
drivers/net/wireless/ath/ath12k/core.h line 22
Quoted 6 lines
+++
b/drivers/net/wireless/ath/ath12k/core.hdrivers/net/wireless/ath/ath12k/core.h +1
-0@@ -19,6 +19,7 @@#include <linux/average.h> #include <linux/of.h> #include <linux/rhashtable.h> +#include <linux/percpu.h> does it make more sense to add this to
wmi.cinstead?
Indeed it does. I'll fix it.
Quoted 20 lines · Nicolas Escande
On Mon Mar 16, 2026 at 11:42 AM CET, Baochen Qiang wrote:
[...]Quoted 18 lines
Quoted 5 lines
drivers/net/wireless/ath/ath12k/core.c +0
-0@@ -2321,5 +2321,24 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; }drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2332
Quoted 10 lines
drivers/net/wireless/ath/ath12k/core.c +9
-0+static int ath12k_init (void ) +{ +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return ret; + +return 0 ;nit: you can simply
Quoted 5 lines
Quoted 1 line
return ath12k_wmi_alloc();Yes we can, but I did this in case we need to add more stuff in that function at some later point. It would generate less code chrun. But I'm ok with a direct return statement if you think that's better. Just tell me and I'll send the v4.
I would prefer a direct return for now. After all We can always change to your style if necessary in the future.
Quoted 27 lines
Quoted 17 lines
Quoted 17 lines
drivers/net/wireless/ath/ath12k/core.c +10
-0+} + +static void ath12k_exit (void ) +{ +ath12k_wmi_free (); +} + +module_init (ath12k_init); +module_exit (ath12k_exit); +MODULE_DESCRIPTION ("Driver support for Qualcomm Technologies WLAN devices" );MODULE_LICENSE ("Dual BSD/GPL" );drivers/net/wireless/ath/ath12k/core.h +0
-1--- a/drivers/net/wireless/ath/ath12k/core.hQuoted 8 lines
drivers/net/wireless/ath/ath12k/core.h line 22
Quoted 6 lines
+++
b/drivers/net/wireless/ath/ath12k/core.hdrivers/net/wireless/ath/ath12k/core.h +1
-0@@ -19,6 +19,7 @@#include <linux/average.h> #include <linux/of.h> #include <linux/rhashtable.h> +#include <linux/percpu.h> does it make more sense to add this to
wmi.cinstead?Indeed it does. I'll fix it.