wifi: ath12k: avoid dynamic alloc when parsing wmi tb
PATCH v4 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 v3:
- simplified ath12k_core_init() with a single statement
- move perpcu.h include directly to wmi.c
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 | 13 ++ drivers/net/wireless/ath/ath12k/wmi.c | 201 ++++++++----------------- drivers/net/wireless/ath/ath12k/wmi.h | 3 + 3 files changed, 78 insertions(+), 139 deletions(-)
drivers/net/wireless/ath/ath12k/core.c +13 -0
@@ -2321,5 +2321,18 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; } +static int ath12k_init (void ) +{ +return ath12k_wmi_alloc (); +} + +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/wmi.c +62 -139
807 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 13 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.
Quoted 4 lines
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]>
Quoted 52 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 v3:
- simplifiedath12k_core_init()with a single statement
- moveperpcu.hinclude directly towmi.cchanges 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 | 13 ++ drivers/net/wireless/ath/ath12k/wmi.c | 201 ++++++++----------------- drivers/net/wireless/ath/ath12k/wmi.h | 3 + 3 files changed, 78 insertions(+), 139 deletions(-)drivers/net/wireless/ath/ath12k/core.c +0
-0
drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2326
Quoted 6 lines
drivers/net/wireless/ath/ath12k/core.c +3
-0@@ -2321,5 +2321,18 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,return NULL ; } +static int ath12k_init (void ) +{ +return ath12k_wmi_alloc ();
Since CONFIG_ATH12K is tristate, a built-in boot can continue past a failed ath12k_init() and still run ath12k_wifi7_init().
Please ensure that later initialization path is guarded against allocation failure.
Or may be have this allocated on first device probe and free it on last device deinit ?
drivers/net/wireless/ath/ath12k/core.c struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, · line 2338
Quoted 12 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" );
--
Ramesh
Quoted 4 lines · Rameshkumar Sundaram
Since
CONFIG_ATH12Kis tristate, a built-in boot can continue past a failedath12k_init()and still runath12k_wifi7_init().
I genuinely thought the kernel prevented this. I was wrong.
Quoted 3 lines
Please ensure that later initialization path is guarded against allocation failure.
I can add a flag like so to be able to check from ath12k_wifi7_init() if the init finished ok. Something in the lines of
drivers/net/wireless/ath/ath12k/core.c +11 -1
@@ -34,6 +34,9 @@ module_param_named(ftm_mode, ath12k_ftm_mode, bool, 0444);MODULE_PARM_DESC (ftm_mode,"Boots up in factory test mode" );EXPORT_SYMBOL (ath12k_ftm_mode); +bool ath12k_init_ok = false; +EXPORT_SYMBOL (ath12k_init_ok); +/* protected with ath12k_hw_group_mutex */ static struct list_head ath12k_hw_group_list =LIST_HEAD_INIT (ath12k_hw_group_list);
@@ -2323,7 +2326,14 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,static int ath12k_init (void ) { -return ath12k_wmi_alloc (); +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return -ENOMEM ; + + ath12k_init_ok = true; +return 0 ; }static void ath12k_exit (void )
drivers/net/wireless/ath/ath12k/core.h +2 -0
@@ -101,6 +101,8 @@ enum ath12k_crypt_mode { ATH12K_CRYPT_MODE_SW, }; +extern bool ath12k_init_ok; +static inline enum wme_ac ath12k_tid_to_ac (u32 tid) { return (((tid == 0) || (tid == 3)) ? WME_AC_BE :
drivers/net/wireless/ath/ath12k/wifi7/core.c +3 -0
@@ -38,6 +38,9 @@ void ath12k_wifi7_arch_deinit(struct ath12k_base *ab)static int ath12k_wifi7_init (void ) { +if (!ath12k_init_ok) +return -ENOTSUPP ; + ahb_err =ath12k_wifi7_ahb_init ();if (ahb_err) pr_warn("Failed to initialize ath12k Wi-Fi 7 AHB device: %d\n",
I don't like it much but it is easy enough.
But I don't know if there is a more idiomatic way of doing things
Quoted 2 lines
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per ath12k_base
What do you guys think ?
Quoted 72 lines · Nicolas Escande
Quoted 4 lines · Rameshkumar Sundaram
Since
CONFIG_ATH12Kis tristate, a built-in boot can continue past a failedath12k_init()and still runath12k_wifi7_init().I genuinely thought the kernel prevented this. I was wrong.
Quoted 3 lines
Please ensure that later initialization path is guarded against allocation failure.
I can add a flag like so to be able to check from
ath12k_wifi7_init()if the init finished ok. Something in the lines ofdrivers/net/wireless/ath/ath12k/core.c +11
-1@@ -34,6 +34,9 @@ module_param_named(ftm_mode, ath12k_ftm_mode, bool, 0444);MODULE_PARM_DESC (ftm_mode,"Boots up in factory test mode" );EXPORT_SYMBOL (ath12k_ftm_mode); +bool ath12k_init_ok = false; +EXPORT_SYMBOL (ath12k_init_ok); +/* protected with ath12k_hw_group_mutex */ static struct list_head ath12k_hw_group_list =LIST_HEAD_INIT (ath12k_hw_group_list);@@ -2323,7 +2326,14 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,static int ath12k_init (void ) { -return ath12k_wmi_alloc (); +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return -ENOMEM ; + + ath12k_init_ok = true; +return 0 ; }static void ath12k_exit (void )drivers/net/wireless/ath/ath12k/core.h +2
-0@@ -101,6 +101,8 @@ enum ath12k_crypt_mode { ATH12K_CRYPT_MODE_SW, }; +extern bool ath12k_init_ok; +static inline enum wme_ac ath12k_tid_to_ac (u32 tid) { return (((tid == 0) || (tid == 3)) ? WME_AC_BE :drivers/net/wireless/ath/ath12k/wifi7/core.c +3
-0@@ -38,6 +38,9 @@ void ath12k_wifi7_arch_deinit(struct ath12k_base *ab)static int ath12k_wifi7_init (void ) { +if (!ath12k_init_ok) +return -ENOTSUPP ; + ahb_err =ath12k_wifi7_ahb_init ();if (ahb_err) pr_warn("Failed to initialize ath12k Wi-Fi 7 AHB device: %d\n",
Quoted 2 lines
I don't like it much but it is easy enough.
But I don't know if there is a more idiomatic way of doing things
I'd prefer to expose a function rather than a global variable. In other words keep the flag static, and expose a function that returns the value of the flag, i.e.:
bool ath12k_core_initialized(void)
{
return ath12k_init_ok;
}EXPORT_SYMBOL(ath12k_core_initialized);
Quoted 9 lines
Quoted 3 lines
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per
ath12k_baseWhat do you guys think ?
Going back to that may be the better solution. It isn't nice that this current solution may allocate memory when the driver isn't actually used. But I'll let others on the team weigh in as well.
/jeff
Quoted 93 lines · Jeff Johnson
Quoted 72 lines · Nicolas Escande
Quoted 4 lines · Rameshkumar Sundaram
Since
CONFIG_ATH12Kis tristate, a built-in boot can continue past a failedath12k_init()and still runath12k_wifi7_init().I genuinely thought the kernel prevented this. I was wrong.
Quoted 3 lines
Please ensure that later initialization path is guarded against allocation failure.
I can add a flag like so to be able to check from
ath12k_wifi7_init()if the init finished ok. Something in the lines ofdrivers/net/wireless/ath/ath12k/core.c +11
-1@@ -34,6 +34,9 @@ module_param_named(ftm_mode, ath12k_ftm_mode, bool, 0444);MODULE_PARM_DESC (ftm_mode,"Boots up in factory test mode" );EXPORT_SYMBOL (ath12k_ftm_mode); +bool ath12k_init_ok = false; +EXPORT_SYMBOL (ath12k_init_ok); +/* protected with ath12k_hw_group_mutex */ static struct list_head ath12k_hw_group_list =LIST_HEAD_INIT (ath12k_hw_group_list);@@ -2323,7 +2326,14 @@ struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,static int ath12k_init (void ) { -return ath12k_wmi_alloc (); +int ret; + + ret =ath12k_wmi_alloc (); +if (ret) +return -ENOMEM ; + + ath12k_init_ok = true; +return 0 ; }static void ath12k_exit (void )drivers/net/wireless/ath/ath12k/core.h +2
-0@@ -101,6 +101,8 @@ enum ath12k_crypt_mode { ATH12K_CRYPT_MODE_SW, }; +extern bool ath12k_init_ok; +static inline enum wme_ac ath12k_tid_to_ac (u32 tid) { return (((tid == 0) || (tid == 3)) ? WME_AC_BE :drivers/net/wireless/ath/ath12k/wifi7/core.c +3
-0@@ -38,6 +38,9 @@ void ath12k_wifi7_arch_deinit(struct ath12k_base *ab)static int ath12k_wifi7_init (void ) { +if (!ath12k_init_ok) +return -ENOTSUPP ; + ahb_err =ath12k_wifi7_ahb_init ();if (ahb_err) pr_warn("Failed to initialize ath12k Wi-Fi 7 AHB device: %d\n",Quoted 2 lines
I don't like it much but it is easy enough.
But I don't know if there is a more idiomatic way of doing thingsI'd prefer to expose a function rather than a global variable. In other words keep the flag static, and expose a function that returns the value of the flag, i.e.:
bool
ath12k_core_initialized(void){ return ath12k_init_ok; }
EXPORT_SYMBOL(ath12k_core_initialized);Quoted 7 lines
Quoted 3 lines
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per
ath12k_base
Quoted 7 lines
Quoted 2 lines
What do you guys think ?
Going back to that may be the better solution. It isn't nice that this current solution may allocate memory when the driver isn't actually used. But I'll let others on the team weigh in as well.
Yeah, allocating once per ath12k_base is definitely the simpler ownership model.
I was only wondering whether sharing it across devices might be worth a look, since this is per-CPU scratch space and the table itself is fairly large.
--
Ramesh
Quoted 17 lines · Rameshkumar Sundaram
Quoted 8 lines · Jeff Johnson
Quoted 7 lines · Nicolas Escande
Quoted 2 lines · Rameshkumar Sundaram
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per
ath12k_baseQuoted 7 lines
Quoted 2 lines
What do you guys think ?
Going back to that may be the better solution. It isn't nice that this current solution may allocate memory when the driver isn't actually used. But I'll let others on the team weigh in as well.
Quoted 5 lines
Yeah, allocating once per
ath12k_baseis definitely the simpler ownership model.
I was only wondering whether sharing it across devices might be worth a look, since this is per-CPU scratch space and the table itself is fairly large.
The other alternative is to still have a single global allocation, but also keep a reference count that starts at 0. when each ar starts it calls a single api to alloc the memory and when it stops it calls another api to dealloc the memory
when the first ar starts and calls the alloc api, the refcount will be 0 so it will allocate the memory and increment the refcount to 1. when any subsequent ars start and call the alloc api, it will just increment the ref count. on deinit each ar will call the dealloc api. this api will just decrement the refcount until it reaches 0 at which time the memory is freed.
Quoted 29 lines · Jeff Johnson
Quoted 17 lines · Rameshkumar Sundaram
Quoted 8 lines · Jeff Johnson
Quoted 7 lines · Nicolas Escande
Quoted 2 lines · Rameshkumar Sundaram
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per
ath12k_baseQuoted 7 lines
Quoted 2 lines
What do you guys think ?
Going back to that may be the better solution. It isn't nice that this current solution may allocate memory when the driver isn't actually used. But I'll let others on the team weigh in as well.
Quoted 5 lines
Yeah, allocating once per
ath12k_baseis definitely the simpler ownership model.
I was only wondering whether sharing it across devices might be worth a look, since this is per-CPU scratch space and the table itself is fairly large.The other alternative is to still have a single global allocation, but also keep a reference count that starts at 0. when each ar starts it calls a single api to alloc the memory and when it stops it calls another api to dealloc the memory
Quoted 5 lines
when the first ar starts and calls the alloc api, the refcount will be 0 so it will allocate the memory and increment the refcount to 1. when any subsequent ars start and call the alloc api, it will just increment the ref count. on deinit each ar will call the dealloc api. this api will just decrement the refcount until it reaches 0 at which time the memory is freed.
We can do that, but we'll need a lock to protect this shared ressource:
- The clean way would mean adding yet another lock to protect this, but it feels like there are already enough locks in ath12k.
- The other way would be to piggy back another existing one. ath12k_hw_group_mutex would be a good candidate to be honest
What do you prefer ?
Quoted 36 lines · Nicolas Escande
Quoted 29 lines · Jeff Johnson
Quoted 17 lines · Rameshkumar Sundaram
Quoted 8 lines · Jeff Johnson
Quoted 7 lines · Nicolas Escande
Quoted 2 lines · Rameshkumar Sundaram
Or may be have this allocated on first device probe and free it on last device deinit ?
That seems even more involved. It would be easier to go back to the previous version and simply, alloc it once per
ath12k_baseQuoted 7 lines
Quoted 2 lines
What do you guys think ?
Going back to that may be the better solution. It isn't nice that this current solution may allocate memory when the driver isn't actually used. But I'll let others on the team weigh in as well.
Quoted 5 lines
Yeah, allocating once per
ath12k_baseis definitely the simpler ownership model.
I was only wondering whether sharing it across devices might be worth a look, since this is per-CPU scratch space and the table itself is fairly large.The other alternative is to still have a single global allocation, but also keep a reference count that starts at 0. when each ar starts it calls a single api to alloc the memory and when it stops it calls another api to dealloc the memory
Quoted 5 lines
when the first ar starts and calls the alloc api, the refcount will be 0 so it will allocate the memory and increment the refcount to 1. when any subsequent ars start and call the alloc api, it will just increment the ref count. on deinit each ar will call the dealloc api. this api will just decrement the refcount until it reaches 0 at which time the memory is freed.
Quoted 8 lines
We can do that, but we'll need a lock to protect this shared ressource:
- The clean way would mean adding yet another lock to protect this, but it feels like there are already enough locks in ath12k.
- The other way would be to piggy back another existing one.ath12k_hw_group_mutexwould be a good candidate to be honestWhat do you prefer ?
i prefer a separate lock for this use case.
leveraging one thing for a completely different use case leads to confusion.
/jeff