Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: rtl8723bs: Use % 4096 instead of & 0xfff

The sequence number is constrained to a range of [0, 4095], which
is a total of 4096 values. The bitmask operation using `& 0xfff` is
used to perform this wrap-around. While this is functionally correct,
it obscures the intended semantic of a 4096-based wrap.

Using a modulo operation `% 4096u` makes the wrap-around logic
explicit and easier to understand. It clearly signals that the
sequence number cycles through a range of 4096 values.
It also makes the code robust against potential changes of the 4096
upper limit, especially when it becomes a non power-of-2 value while
the AND(&) works solely for power-of-2 values.

The use of `% 4096u` also guarantees that the modulo operation is
performed with unsigned arithmetic, preventing potential issues with
the signed types.

Found by Coccinelle.

Suggested-by: Andy Shevchenko <andy@kernel.org>
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://lore.kernel.org/r/e8d515539ba560961003eae15d301d03e6cdd17d.1744966511.git.abrahamadekunle50@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Abraham Samuel Adekunle and committed by
Greg Kroah-Hartman
a5df13cd 28925280

+7 -7
+1 -1
drivers/staging/rtl8723bs/core/rtw_mlme_ext.c
··· 3511 3511 /* if ((psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.mac_address)) != NULL) */ 3512 3512 psta = rtw_get_stainfo(pstapriv, raddr); 3513 3513 if (psta) { 3514 - start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1; 3514 + start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07] % 4096u) + 1; 3515 3515 3516 3516 psta->BA_starting_seqctrl[status & 0x07] = start_seq; 3517 3517
+3 -3
drivers/staging/rtl8723bs/core/rtw_recv.c
··· 1641 1641 struct dvobj_priv *psdpriv = padapter->dvobj; 1642 1642 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; 1643 1643 u8 wsize = preorder_ctrl->wsize_b; 1644 - u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/* 4096; */ 1644 + u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) % 4096u; 1645 1645 1646 1646 /* Rx Reorder initialize condition. */ 1647 1647 if (preorder_ctrl->indicate_seq == 0xFFFF) ··· 1657 1657 /* 2. Incoming SeqNum is larger than the WinEnd => Window shift N */ 1658 1658 /* */ 1659 1659 if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) { 1660 - preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF; 1660 + preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096u; 1661 1661 1662 1662 } else if (SN_LESS(wend, seq_num)) { 1663 1663 /* boundary situation, when seq_num cross 0xFFF */ ··· 1772 1772 list_del_init(&(prframe->u.hdr.list)); 1773 1773 1774 1774 if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num)) 1775 - preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF; 1775 + preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096u; 1776 1776 1777 1777 /* Set this as a lock to make sure that only one thread is indicating packet. */ 1778 1778 /* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
+3 -3
drivers/staging/rtl8723bs/core/rtw_xmit.c
··· 943 943 944 944 if (psta) { 945 945 psta->sta_xmitpriv.txseq_tid[pattrib->priority]++; 946 - psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF; 946 + psta->sta_xmitpriv.txseq_tid[pattrib->priority] %= 4096u; 947 947 pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority]; 948 948 949 949 SetSeqNum(hdr, pattrib->seqnum); ··· 964 964 pattrib->ampdu_en = false;/* AGG BK */ 965 965 } else if (SN_EQUAL(pattrib->seqnum, tx_seq)) { 966 966 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = 967 - (tx_seq + 1) & 0xfff; 967 + (tx_seq + 1) % 4096u; 968 968 969 969 pattrib->ampdu_en = true;/* AGG EN */ 970 970 } else { 971 971 psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = 972 - (pattrib->seqnum + 1) & 0xfff; 972 + (pattrib->seqnum + 1) % 4096u; 973 973 974 974 pattrib->ampdu_en = true;/* AGG EN */ 975 975 }