diff mbox series

wifi: rtw89: Correct immediate cfg_len calculation for scan_offload_be

Message ID 20250108102512.3477461-1-buaajxlj@163.com (mailing list archive)
State Changes Requested
Delegated to: Ping-Ke Shih
Headers show
Series wifi: rtw89: Correct immediate cfg_len calculation for scan_offload_be | expand

Commit Message

Liang Jie Jan. 8, 2025, 10:25 a.m. UTC
From: Liang Jie <liangjie@lixiang.com>

This patch ensures the correct calculation of `cfg_len` prior to the
allocation of the skb in the `rtw89_fw_h2c_scan_offload_be` function,
particularly when the `SCAN_OFFLOAD_BE_V0` firmware feature is enabled.
It addresses an issue where an incorrect skb size might be allocated
due to a delayed setting of `cfg_len`, potentially leading to memory
inefficiencies.

By moving the conditional check and assignment of `cfg_len` before skb
allocation, the patch guarantees that `len`, which depends on `cfg_len`,
is accurately computed, ensuring proper skb size and preventing any
unnecessary memory reservation for firmware operations not supporting
beyond the `w8` member of the command data structure.

This correction helps to optimize memory usage and maintain consistent
behavior across different firmware versions.

Fixes: 6ca6b918f280 ("wifi: rtw89: 8922a: Add new fields for scan offload H2C command")

Signed-off-by: Liang Jie <liangjie@lixiang.com>
---
 drivers/net/wireless/realtek/rtw89/fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Ping-Ke Shih Jan. 9, 2025, 12:35 a.m. UTC | #1
Liang Jie <buaajxlj@163.com> wrote: 
> From: Liang Jie <liangjie@lixiang.com>
> 
> This patch ensures the correct calculation of `cfg_len` prior to the

No "This patch". Just in imperative tense -- "Ensure ..."

> allocation of the skb in the `rtw89_fw_h2c_scan_offload_be` function,
> particularly when the `SCAN_OFFLOAD_BE_V0` firmware feature is enabled.
> It addresses an issue where an incorrect skb size might be allocated
> due to a delayed setting of `cfg_len`, potentially leading to memory
> inefficiencies.
> 
> By moving the conditional check and assignment of `cfg_len` before skb
> allocation, the patch guarantees that `len`, which depends on `cfg_len`,
> is accurately computed, ensuring proper skb size and preventing any
> unnecessary memory reservation for firmware operations not supporting
> beyond the `w8` member of the command data structure.
> 
> This correction helps to optimize memory usage and maintain consistent
> behavior across different firmware versions.
> 
> Fixes: 6ca6b918f280 ("wifi: rtw89: 8922a: Add new fields for scan offload H2C command")
> 

No this blank line. 

> Signed-off-by: Liang Jie <liangjie@lixiang.com>
> ---
>  drivers/net/wireless/realtek/rtw89/fw.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
> index 2191c037d72e..5eb4beb9e485 100644
> --- a/drivers/net/wireless/realtek/rtw89/fw.c
> +++ b/drivers/net/wireless/realtek/rtw89/fw.c
> @@ -5169,7 +5169,7 @@ int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
>         u8 macc_role_size = sizeof(*macc_role) * option->num_macc_role;
>         u8 opch_size = sizeof(*opch) * option->num_opch;
>         u8 probe_id[NUM_NL80211_BANDS];
> -       u8 cfg_len = sizeof(*h2c);
> +       u8 cfg_len;

In fact, SCAN_OFFLOAD_BE_V0 means `old` format used by `old' firmware version,
which isn't a main flow. I prefer to keep here unchanged, and modify cfg_len
along with SCAN_OFFLOAD_BE_V0 for v0 firmware. 

As existing patterns, this will be

	u8 cfg_len = sizeof(*h2c);
	u8 ver = U8_MAX;

	if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
		ver = 0;

	...

	if (ver == 0)
		cfg_len = offsetofend(typeof(*h2c), w8);

	...

	if (ver == 0)
		goto flex_member;

	...


>         unsigned int cond;
>         void *ptr;
>         int ret;
> @@ -5178,6 +5178,11 @@ int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
> 
>         rtw89_scan_get_6g_disabled_chan(rtwdev, option);
> 
> +       if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
> +               cfg_len = offsetofend(typeof(*h2c), w8);
> +       else
> +               cfg_len = sizeof(*h2c);
> +
>         len = cfg_len + macc_role_size + opch_size;
>         skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, len);
>         if (!skb) {
> @@ -5246,10 +5251,8 @@ int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
>                                            RTW89_H2C_SCANOFLD_BE_W8_PROBE_RATE_6GHZ);
>         }
> 
> -       if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw)) {
> -               cfg_len = offsetofend(typeof(*h2c), w8);
> +       if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
>                 goto flex_member;
> -       }
> 
>         h2c->w9 = le32_encode_bits(sizeof(*h2c) / sizeof(h2c->w0),
>                                    RTW89_H2C_SCANOFLD_BE_W9_SIZE_CFG) |
> --
> 2.25.1
Liang Jie Jan. 9, 2025, 1:23 a.m. UTC | #2
On Thu, 9 Jan 2025 00:35:38 +0000, Ping-Ke Shih <pkshih@realtek.com> wrote:
> Liang Jie <buaajxlj@163.com> wrote: 
> > From: Liang Jie <liangjie@lixiang.com>
> > 
> > This patch ensures the correct calculation of `cfg_len` prior to the
> 
> No "This patch". Just in imperative tense -- "Ensure ..."
> 
> > allocation of the skb in the `rtw89_fw_h2c_scan_offload_be` function,
> >
> >   ...
> >
> > behavior across different firmware versions.
> > 
> > Fixes: 6ca6b918f280 ("wifi: rtw89: 8922a: Add new fields for scan offload H2C command")
> > 
> 
> No this blank line. 
> 
> > Signed-off-by: Liang Jie <liangjie@lixiang.com>
> > ---
> >
> >   ...
> >
> >         u8 probe_id[NUM_NL80211_BANDS];
> > -       u8 cfg_len = sizeof(*h2c);
> > +       u8 cfg_len;
> 
> In fact, SCAN_OFFLOAD_BE_V0 means `old` format used by `old' firmware version,
> which isn't a main flow. I prefer to keep here unchanged, and modify cfg_len
> along with SCAN_OFFLOAD_BE_V0 for v0 firmware. 
> 
> As existing patterns, this will be
> 
> 	u8 cfg_len = sizeof(*h2c);
> 	u8 ver = U8_MAX;
> 
> 	if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
> 		ver = 0;
> 
> 	...
> 
> 	if (ver == 0)
> 		cfg_len = offsetofend(typeof(*h2c), w8);
> 
> 	...
> 
> 	if (ver == 0)
> 		goto flex_member;
> 
> 	...
> 
> 
> >         unsigned int cond;
> >         void *ptr;
> >         int ret;
> >
> >   ...
> >
> > 2.25.1

Thank you very much for your suggestions. I will incorporate them and submit
[PATCH v2] accordingly.

Best regards,
Liang Jie
diff mbox series

Patch

diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
index 2191c037d72e..5eb4beb9e485 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -5169,7 +5169,7 @@  int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
 	u8 macc_role_size = sizeof(*macc_role) * option->num_macc_role;
 	u8 opch_size = sizeof(*opch) * option->num_opch;
 	u8 probe_id[NUM_NL80211_BANDS];
-	u8 cfg_len = sizeof(*h2c);
+	u8 cfg_len;
 	unsigned int cond;
 	void *ptr;
 	int ret;
@@ -5178,6 +5178,11 @@  int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
 
 	rtw89_scan_get_6g_disabled_chan(rtwdev, option);
 
+	if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
+		cfg_len = offsetofend(typeof(*h2c), w8);
+	else
+		cfg_len = sizeof(*h2c);
+
 	len = cfg_len + macc_role_size + opch_size;
 	skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, len);
 	if (!skb) {
@@ -5246,10 +5251,8 @@  int rtw89_fw_h2c_scan_offload_be(struct rtw89_dev *rtwdev,
 					   RTW89_H2C_SCANOFLD_BE_W8_PROBE_RATE_6GHZ);
 	}
 
-	if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw)) {
-		cfg_len = offsetofend(typeof(*h2c), w8);
+	if (RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD_BE_V0, &rtwdev->fw))
 		goto flex_member;
-	}
 
 	h2c->w9 = le32_encode_bits(sizeof(*h2c) / sizeof(h2c->w0),
 				   RTW89_H2C_SCANOFLD_BE_W9_SIZE_CFG) |