All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zong-Zhe Yang <kevin_yang@realtek.com>
To: Fedor Pchelkin <pchelkin@ispras.ru>,
	Ping-Ke Shih <pkshih@realtek.com>,
	Bitterblue Smith <rtl8821cerfe2@gmail.com>
Cc: Bernie Huang <phhuang@realtek.com>,
	"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"lvc-project@linuxtesting.org" <lvc-project@linuxtesting.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: RE: [PATCH rtw v3 2/5] wifi: rtw89: fix tx_wait initialization race
Date: Fri, 29 Aug 2025 09:40:57 +0000	[thread overview]
Message-ID: <99e8d23ce3314a76a2a50db1e7d386f3@realtek.com> (raw)
In-Reply-To: <20250828211245.178843-3-pchelkin@ispras.ru>

Fedor Pchelkin <pchelkin@ispras.ru> wrote:
> Now that nullfunc skbs are recycled in a separate work item in the driver, the following race
> during initialization and processing of those skbs might lead to noticeable bugs:
> 
>       Waiting thread                          Completing thread
> 
> rtw89_core_send_nullfunc()
>   rtw89_core_tx_write_link()
>     ...
>     rtw89_pci_txwd_submit()
>       skb_data->wait = NULL
>       /* add skb to the queue */
>       skb_queue_tail(&txwd->queue, skb)
>                                             rtw89_pci_napi_poll()
>                                             ...
>                                               rtw89_pci_release_txwd_skb()
>                                                 /* get skb from the queue */
>                                                 skb_unlink(skb, &txwd->queue)
>                                                 rtw89_pci_tx_status()
>                                                   rtw89_core_tx_wait_complete()
>                                                   /* use incorrect skb_data->wait
> */
>   rtw89_core_tx_kick_off_and_wait()
>   /* assign skb_data->wait but too late */
> 
> The value of skb_data->wait indicates whether skb is passed on to the core ieee80211 stack
> or released by the driver itself.  So assure that by the time skb is added to txwd queue and
> becomes visible to the completing side, it has already allocated tx_wait-related data (in case
> it's needed).
> 
> Found by Linux Verification Center (linuxtesting.org).
> 
> Fixes: 1ae5ca615285 ("wifi: rtw89: add function to wait for completion of TX skbs")
> Cc: stable@vger.kernel.org
> Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
> ---
>  drivers/net/wireless/realtek/rtw89/core.c | 30 ++++++++++++++---------
> drivers/net/wireless/realtek/rtw89/pci.c  |  2 --
>  2 files changed, 18 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtw89/core.c
> b/drivers/net/wireless/realtek/rtw89/core.c
> index 5c964283cd67..57f20559dfde 100644
> --- a/drivers/net/wireless/realtek/rtw89/core.c
> +++ b/drivers/net/wireless/realtek/rtw89/core.c
> @@ -1094,20 +1094,12 @@ int rtw89_core_tx_kick_off_and_wait(struct rtw89_dev *rtwdev,
> struct sk_buff *sk
>                                     int qsel, unsigned int timeout)  {
>         struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
> -       struct rtw89_tx_wait_info *wait;
> +       struct rtw89_tx_wait_info *wait = skb_data->wait;

wiphy_dereference(rtwdev->hw->wiphy, skb_data->wait)

>         unsigned long time_left;
>         int ret = 0;
> 
>         lockdep_assert_wiphy(rtwdev->hw->wiphy);
> 
> -       wait = kzalloc(sizeof(*wait), GFP_KERNEL);
> -       if (!wait) {
> -               rtw89_core_tx_kick_off(rtwdev, qsel);
> -               return 0;
> -       }
> -
> -       init_completion(&wait->completion);
> -       rcu_assign_pointer(skb_data->wait, wait);
>         wait->skb = skb;

Also, move "wait->skb = skb" to where wait is initialized.

> 
>         rtw89_core_tx_kick_off(rtwdev, qsel); @@ -1172,10 +1164,12 @@ int
> rtw89_h2c_tx(struct rtw89_dev *rtwdev,  static int rtw89_core_tx_write_link(struct
> rtw89_dev *rtwdev,
>                                     struct rtw89_vif_link *rtwvif_link,
>                                     struct rtw89_sta_link *rtwsta_link,
> -                                   struct sk_buff *skb, int *qsel, bool sw_mld)
> +                                   struct sk_buff *skb, int *qsel, bool sw_mld,
> +                                   struct rtw89_tx_wait_info *wait)
>  {
>         struct ieee80211_sta *sta = rtwsta_link_to_sta_safe(rtwsta_link);
>         struct ieee80211_vif *vif = rtwvif_link_to_vif(rtwvif_link);
> +       struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
>         struct rtw89_vif *rtwvif = rtwvif_link->rtwvif;
>         struct rtw89_core_tx_request tx_req = {};
>         int ret;
> @@ -1192,6 +1186,8 @@ static int rtw89_core_tx_write_link(struct rtw89_dev *rtwdev,
>         rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
>         rtw89_core_tx_wake(rtwdev, &tx_req);
> 
> +       rcu_assign_pointer(skb_data->wait, wait);
> +
>         ret = rtw89_hci_tx_write(rtwdev, &tx_req);
>         if (ret) {
>                 rtw89_err(rtwdev, "failed to transmit skb to HCI\n"); @@ -1228,7 +1224,8
> @@ int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
>                 }
>         }
> 
> -       return rtw89_core_tx_write_link(rtwdev, rtwvif_link, rtwsta_link, skb, qsel, false);
> +       return rtw89_core_tx_write_link(rtwdev, rtwvif_link, rtwsta_link, skb, qsel, false,
> +                                       NULL);
>  }
> 
>  static __le32 rtw89_build_txwd_body0(struct rtw89_tx_desc_info *desc_info) @@ -3426,6
> +3423,7 @@ int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev, struct rtw89_vif_link
> *rt
>         struct ieee80211_vif *vif = rtwvif_link_to_vif(rtwvif_link);
>         int link_id = ieee80211_vif_is_mld(vif) ? rtwvif_link->link_id : -1;
>         struct rtw89_sta_link *rtwsta_link;
> +       struct rtw89_tx_wait_info *wait;
>         struct ieee80211_sta *sta;
>         struct ieee80211_hdr *hdr;
>         struct rtw89_sta *rtwsta;
> @@ -3435,6 +3433,12 @@ int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev, struct
> rtw89_vif_link *rt
>         if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
>                 return 0;
> 
> +       wait = kzalloc(sizeof(*wait), GFP_KERNEL);
> +       if (!wait)
> +               return -ENOMEM;
> +
> +       init_completion(&wait->completion);
> +
>         rcu_read_lock();
>         sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
>         if (!sta) {
> @@ -3459,7 +3463,8 @@ int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev, struct
> rtw89_vif_link *rt
>                 goto out;
>         }
> 

Fill wait->skb as mentioned above.

> -       ret = rtw89_core_tx_write_link(rtwdev, rtwvif_link, rtwsta_link, skb, &qsel, true);
> +       ret = rtw89_core_tx_write_link(rtwdev, rtwvif_link, rtwsta_link, skb, &qsel, true,
> +                                      wait);
>         if (ret) {
>                 rtw89_warn(rtwdev, "nullfunc transmit failed: %d\n", ret);
>                 dev_kfree_skb_any(skb);
> @@ -3472,6 +3477,7 @@ int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev, struct
> rtw89_vif_link *rt
>                                                timeout);
>  out:
>         rcu_read_unlock();
> +       kfree(wait);
> 
>         return ret;
>  }
> diff --git a/drivers/net/wireless/realtek/rtw89/pci.c b/drivers/net/wireless/realtek/rtw89/pci.c
> index 4e3034b44f56..cb9682f306a6 100644
> --- a/drivers/net/wireless/realtek/rtw89/pci.c
> +++ b/drivers/net/wireless/realtek/rtw89/pci.c
> @@ -1372,7 +1372,6 @@ static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
>         struct pci_dev *pdev = rtwpci->pdev;
>         struct sk_buff *skb = tx_req->skb;
>         struct rtw89_pci_tx_data *tx_data = RTW89_PCI_TX_SKB_CB(skb);
> -       struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
>         bool en_wd_info = desc_info->en_wd_info;
>         u32 txwd_len;
>         u32 txwp_len;
> @@ -1388,7 +1387,6 @@ static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
>         }
> 
>         tx_data->dma = dma;
> -       rcu_assign_pointer(skb_data->wait, NULL);
> 
>         txwp_len = sizeof(*txwp_info);
>         txwd_len = chip->txwd_body_size;
> --
> 2.51.0
> 


  reply	other threads:[~2025-08-29  9:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28 21:11 [PATCH rtw v3 0/5] wifi: fixes for rtw89 Fedor Pchelkin
2025-08-28 21:11 ` [PATCH rtw v3 1/5] wifi: rtw89: fix use-after-free in rtw89_core_tx_kick_off_and_wait() Fedor Pchelkin
2025-08-29  7:42   ` Zong-Zhe Yang
2025-08-28 21:11 ` [PATCH rtw v3 2/5] wifi: rtw89: fix tx_wait initialization race Fedor Pchelkin
2025-08-29  9:40   ` Zong-Zhe Yang [this message]
2025-08-28 21:11 ` [PATCH rtw v3 3/5] wifi: rtw89: perform tx_wait completions for USB part Fedor Pchelkin
2025-08-29 19:57   ` Bitterblue Smith
2025-09-01 17:45     ` Fedor Pchelkin
2025-09-01 18:46       ` Bitterblue Smith
2025-09-17  9:25         ` Fedor Pchelkin
2025-08-28 21:12 ` [PATCH rtw v3 4/5] wifi: rtw89: fix leak in rtw89_core_send_nullfunc() Fedor Pchelkin
2025-08-28 21:12 ` [PATCH rtw v3 5/5] wifi: rtw89: avoid circular locking dependency in ser_state_run() Fedor Pchelkin

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=99e8d23ce3314a76a2a50db1e7d386f3@realtek.com \
    --to=kevin_yang@realtek.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=pchelkin@ispras.ru \
    --cc=phhuang@realtek.com \
    --cc=pkshih@realtek.com \
    --cc=rtl8821cerfe2@gmail.com \
    --cc=stable@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.