All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: "Jose A. Perez de Azpillaga" <azpijr@gmail.com>
Cc: linux-staging@lists.linux.dev,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Michael Straube <straube.linux@gmail.com>,
	Hans de Goede <hansg@kernel.org>,
	Khushal Chitturi <khushalchitturi@gmail.com>,
	Ethan Tidmore <ethantidmore06@gmail.com>,
	Vivek BalachandharTN <vivek.balachandhar@gmail.com>,
	Luka Gejak <luka.gejak@linux.dev>,
	Artur Stupa <arthur.stupa@gmail.com>,
	Zhuoheng Li <lizhuoheng@kylinos.cn>,
	Nino Zhang <ninozhang001@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] staging: rtl8723bs: refactor rtw_joinbss_event_prehandle to reduce indentation
Date: Fri, 20 Mar 2026 12:40:47 +0300	[thread overview]
Message-ID: <ab0WH6lA4GY6lp8J@stanley.mountain> (raw)
In-Reply-To: <20260319231344.569418-3-azpijr@gmail.com>

On Fri, Mar 20, 2026 at 12:13:35AM +0100, Jose A. Perez de Azpillaga wrote:
> The rtw_joinbss_event_prehandle function has excessive indentation due
> to deeply nested if-statements.
> 
> Refactor the function using early returns and guard clauses for the
> failure paths. This flattens the code and significantly improves
> readability.
> 
> Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
> ---
>  drivers/staging/rtl8723bs/core/rtw_mlme.c | 134 +++++++++++-----------
>  1 file changed, 69 insertions(+), 65 deletions(-)
> 
> diff --git a/drivers/staging/rtl8723bs/core/rtw_mlme.c b/drivers/staging/rtl8723bs/core/rtw_mlme.c
> index 805427353aa6..2a192c1dd038 100644
> --- a/drivers/staging/rtl8723bs/core/rtw_mlme.c
> +++ b/drivers/staging/rtl8723bs/core/rtw_mlme.c
> @@ -1175,84 +1175,88 @@ void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf)
>  	pmlmepriv->link_detect_info.traffic_transition_count = 0;
>  	pmlmepriv->link_detect_info.low_power_transition_count = 0;
>  
> -	if (pnetwork->join_res > 0) {
> -		spin_lock_bh(&pmlmepriv->scanned_queue.lock);
> -		retry = 0;
> -		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
> -			/* s1. find ptarget_wlan */
> -			if (check_fwstate(pmlmepriv, _FW_LINKED)) {
> -				if (the_same_macaddr) {
> -					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
> -				} else {
> -					pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
> -					if (pcur_wlan)
> -						pcur_wlan->fixed = false;
> -
> -					pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.mac_address);
> -					if (pcur_sta)
> -						rtw_free_stainfo(adapter,  pcur_sta);
> -
> -					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.mac_address);
> -					if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> -						if (ptarget_wlan)
> -							ptarget_wlan->fixed = true;
> -					}
> -				}
> +	if (pnetwork->join_res == -4) {
> +		rtw_reset_securitypriv(adapter);
> +		_set_timer(&pmlmepriv->assoc_timer, 1);
>  
> -			} else {
> -				ptarget_wlan = _rtw_find_same_network(&pmlmepriv->scanned_queue, pnetwork);
> -				if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> -					if (ptarget_wlan)
> -						ptarget_wlan->fixed = true;
> -				}
> -			}
> +		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
> +			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
>  
> -			/* s2. update cur_network */
> -			if (ptarget_wlan) {
> -				rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
> -			} else {
> -				netdev_dbg(adapter->pnetdev,
> -					   "Can't find ptarget_wlan when joinbss_event callback\n");
> -				spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> -				goto ignore_joinbss_callback;
> -			}
> +		goto ignore_joinbss_callback;
> +	}
>  
> -			/* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
> -			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> -				ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
> -				if (!ptarget_sta) {
> -					spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> -					goto ignore_joinbss_callback;
> -				}
> -			}
> +	if (pnetwork->join_res <= 0) {

This preserves how the existing code works, but originally there was
a comment here which said that join_res could only be < 0.  Emotionally,
I would prefer if zero were treated as success.  Can join_res actually
be zero?

Update: I looked and join_res can be zero.  It comes from:

	res = pmlmeinfo->aid = (int)(le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 4))&0x3fff);

in the OnAssocRsp() function.  I guess preserve the <= 0, but also
preserve the comment.

> +		_set_timer(&pmlmepriv->assoc_timer, 1);
> +		_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
> +		goto ignore_joinbss_callback;
> +	}
> +
> +	spin_lock_bh(&pmlmepriv->scanned_queue.lock);
> +	retry = 0;
> +
> +	if (!check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
> +		spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> +		goto ignore_joinbss_callback;
> +	}
> +
> +	/* s1. find ptarget_wlan */
> +	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
> +		if (the_same_macaddr) {
> +			ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
> +		} else {
> +			pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
> +			if (pcur_wlan)
> +				pcur_wlan->fixed = false;
> +
> +			pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.mac_address);
> +			if (pcur_sta)
> +				rtw_free_stainfo(adapter, pcur_sta);
>  
> -			/* s4. indicate connect */
> +			ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.mac_address);
>  			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> -				pmlmepriv->cur_network_scanned = ptarget_wlan;
> -				rtw_indicate_connect(adapter);
> +				if (ptarget_wlan)
> +					ptarget_wlan->fixed = true;
>  			}
> +		}
> +	} else {
> +		ptarget_wlan = _rtw_find_same_network(&pmlmepriv->scanned_queue, pnetwork);
> +		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> +			if (ptarget_wlan)
> +				ptarget_wlan->fixed = true;
> +		}
> +	}
>  
> -			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> +	/* s2. update cur_network */
> +	if (!ptarget_wlan) {
> +		netdev_dbg(adapter->pnetdev, "Can't find ptarget_wlan when joinbss_event callback\n");

This line is too long.  In the original code there was a newline after
the comma so it would have been 84 characters.  Try to make as few
unrelated changes as possible.


> +		spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> +		goto ignore_joinbss_callback;
> +	}
>  
> -			spin_unlock_bh(&pmlmepriv->lock);
> -			/* s5. Cancel assoc_timer */
> -			timer_delete_sync(&pmlmepriv->assoc_timer);
> -			spin_lock_bh(&pmlmepriv->lock);
> -		} else {
> +	rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
> +
> +	/* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
> +	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> +		ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
> +		if (!ptarget_sta) {
>  			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> +			goto ignore_joinbss_callback;
>  		}
> -	} else if (pnetwork->join_res == -4) {
> -		rtw_reset_securitypriv(adapter);
> -		_set_timer(&pmlmepriv->assoc_timer, 1);
> -
> -		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
> -			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
> +	}
>  
> -	} else {/* if join_res < 0 (join fails), then try again */
> -		_set_timer(&pmlmepriv->assoc_timer, 1);
> -		_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
> +	/* s4. indicate connect */
> +	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
> +		pmlmepriv->cur_network_scanned = ptarget_wlan;
> +		rtw_indicate_connect(adapter);
>  	}
>  
> +	spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
> +
> +	/* s5. Cancel assoc_timer */

Put this comment after the spinlock (the way it was in the original
code).

> +	spin_unlock_bh(&pmlmepriv->lock);
> +	timer_delete_sync(&pmlmepriv->assoc_timer);
> +	return;

Originally, we took a lock and then fell through to the unlock after
the ignore_joinbss_callback label.  Which is obviously bogus so your
return is better.  I also think it might be even better yet to get
rid of the ignore_joinbss_callback label entirely.  A simple

	spin_unlock_bh(&pmlmepriv->lock);
	return;

Is only one more line of code, but it's much more readable.

regards,
dan carpenter

> +
>  ignore_joinbss_callback:
>  
>  	spin_unlock_bh(&pmlmepriv->lock);


      parent reply	other threads:[~2026-03-20  9:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-19 23:13 [PATCH v2 0/2] staging: rtl8723bs: clean up rtw_joinbss_event_prehandle Jose A. Perez de Azpillaga
2026-03-19 23:13 ` [PATCH v2 1/2] staging: rtl8723bs: remove dead REJOIN code Jose A. Perez de Azpillaga
2026-03-19 23:13 ` [PATCH v2 2/2] staging: rtl8723bs: refactor rtw_joinbss_event_prehandle to reduce indentation Jose A. Perez de Azpillaga
2026-03-20  8:05   ` Luka Gejak
2026-03-20  9:40   ` Dan Carpenter [this message]

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=ab0WH6lA4GY6lp8J@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=arthur.stupa@gmail.com \
    --cc=azpijr@gmail.com \
    --cc=ethantidmore06@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hansg@kernel.org \
    --cc=khushalchitturi@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=lizhuoheng@kylinos.cn \
    --cc=luka.gejak@linux.dev \
    --cc=ninozhang001@gmail.com \
    --cc=straube.linux@gmail.com \
    --cc=vivek.balachandhar@gmail.com \
    /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.