Skip to contentSkip to conversation

staging: rtl8723bs: Improve readability and clarity of sequence number wrapping

PATCH New Abraham Samuel Adekunle 1 participant 3 messages Apr 18, 2025 Staging

lore
Share & tools
lore
#0 lore raw

v11 cover staging: rtl8723bs: Improve readability and clarity of sequence number wrapping

The patchset adds spaces around binary operators, breaks long lines to enhance readability and provides clarity on sequence number wrapping by using a modulo operation % 4096u, in place of the bitwise AND(&) operation & 0xfff.
The patches are required to be applied in sequence.

Changes in v10:
- Sent v11 to mailing list after v10 was deleted due to versioning mistake.

Changes in v9:
* PATCH 1:
- Added more converted cases found in the driver to the patch.
- The Coccinelle semantic patch used to find additional cases in the driver is:

@@
expression e;

@@
* e & 0xfff

- The bloat-o-meter tool was used to ensure the sizes of the binary files before and after the change were the same.

- $ make drivers/staging/rtl8723bs/core/rtw_xmit.o

  $ ./scripts/bloat-o-meter rtw_xmit_before.o rtw_xmit_after.o

This showed identical 'Before' and 'After' size values with a 'chg' of +0.00%. Changes in v8:
* PATCH 1:
- Added Reviewed-by tag to commit message.
* PATCH 2:
- Added Reviewed-by tag to commit message.
Changes in v7:
* PATCH 1:
- Added blank line to enhance readability
* PATCH 2:
- Changed power of 2 to power-of-2 in commit message.
Changes in v6:
- Modified cover letter BLURB to reference newly added line breaks.
* PATCH 1:
- Added line breaks to long lines to improve readability.
- Changed commit message to include the information about the broken lines added to the patch.
- Changed subject line title to also include the newly added line breaks.
* PATCH 2:
- Changed instances of & 0xfff to % 4096u which were now in the broken lines after the broken lines have been done in PATCH 1.
Changes in v5:
- Converted the patch with the subject "Use % 4096 instead of & 0xfff" patch to a patchset.
- Added a patch to add spaces around binary operator.
Changes in v4:
- Corrected patch to use '%' instead of '&'.
- To ensure this change does not affect the functional
behaviour, I compared the generated object files before and
after the change using the cmp which compares the two
object files byte by byte as shown below:

$ make drivers/staging/rtl8723bs/core/rtw_xmit.o $ cmp rtw_xmit_before.o rtw_xmit_after.o

No differences were found in the output, confirming that the change does not alter the compiled output.
Changes in v3:
- Added more description to the commit message.
- Removed blank line in the tag block.
- Added more patch recipients.
Changes in v2:
- Changed the commit message t a more descriptive message which makes it clear why the patch does the change.
- changed the subject title to include 4096u to show that an unsigned module is used.
Changes in v1:
- Added more patch recipients.

Abraham Samuel Adekunle (2):
staging: rtl8723bs: Add spaces and line breaks to improve readability
staging: rtl8723bs: Use % 4096 instead of & 0xfff

 drivers/staging/rtl8723bs/core/rtw_mlme_ext.c | 2 +-
 drivers/staging/rtl8723bs/core/rtw_recv.c     | 6 +++---
 drivers/staging/rtl8723bs/core/rtw_xmit.c     | 9 ++++++---
 3 files changed, 10 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v11 1/2 staging: rtl8723bs: Add spaces and line breaks to improve readability

The code contains no spaces around binary operators with long lines which reduces readability thereby not adhering to Linux kernel coding style.

Add white spaces around the binary operators and use line breaks to increase readability and ensure adherence to Linux kernel coding styles.

---
 drivers/staging/rtl8723bs/core/rtw_xmit.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/staging/rtl8723bs/core/rtw_xmit.c +5 -2
index 297c93d65315..5def2467f42b 100644
@@ -963,11 +963,14 @@ s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattr
 					if (SN_LESS(pattrib->seqnum, tx_seq)) {
 						pattrib->ampdu_en = false;/* AGG BK */
 					} else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
-						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (tx_seq+1)&0xfff;
+						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] =
+							(tx_seq + 1) & 0xfff;
 
 						pattrib->ampdu_en = true;/* AGG EN */
 					} else {
-						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] = (pattrib->seqnum+1)&0xfff;
+						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] =
+							(pattrib->seqnum + 1) & 0xfff;
+
 						pattrib->ampdu_en = true;/* AGG EN */
 					}
 				}
Signature · 1 line
2.34.1
#2 lore raw

PATCH v11 2/2 staging: rtl8723bs: Use % 4096 instead of & 0xfff

In reply to Abraham Samuel Adekunle

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.

---
 drivers/staging/rtl8723bs/core/rtw_mlme_ext.c | 2 +-
 drivers/staging/rtl8723bs/core/rtw_recv.c     | 6 +++---
 drivers/staging/rtl8723bs/core/rtw_xmit.c     | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
drivers/staging/rtl8723bs/core/rtw_mlme_ext.c +1 -1
index 952ce6dd5af9..f712ea0cabee 100644
@@ -3511,7 +3511,7 @@ void issue_action_BA(struct adapter *padapter, unsigned char *raddr, unsigned ch
 			/* if ((psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.mac_address)) != NULL) */
 			psta = rtw_get_stainfo(pstapriv, raddr);
 			if (psta) {
-				start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1;
+				start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07] % 4096u) + 1;
 
 				psta->BA_starting_seqctrl[status & 0x07] = start_seq;
 
drivers/staging/rtl8723bs/core/rtw_recv.c +3 -3
index a389ba5ecc6f..25411cc5c738 100644
@@ -1641,7 +1641,7 @@ static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_n
 	struct dvobj_priv *psdpriv = padapter->dvobj;
 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
 	u8 wsize = preorder_ctrl->wsize_b;
-	u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) & 0xFFF;/*  4096; */
+	u16 wend = (preorder_ctrl->indicate_seq + wsize - 1) % 4096u;
 
 	/*  Rx Reorder initialize condition. */
 	if (preorder_ctrl->indicate_seq == 0xFFFF)
@@ -1657,7 +1657,7 @@ static int check_indicate_seq(struct recv_reorder_ctrl *preorder_ctrl, u16 seq_n
 	/*  2. Incoming SeqNum is larger than the WinEnd => Window shift N */
 	/*  */
 	if (SN_EQUAL(seq_num, preorder_ctrl->indicate_seq)) {
-		preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
+		preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096u;
 
 	} else if (SN_LESS(wend, seq_num)) {
 		/*  boundary situation, when seq_num cross 0xFFF */
@@ -1772,7 +1772,7 @@ static int recv_indicatepkts_in_order(struct adapter *padapter, struct recv_reor
 			list_del_init(&(prframe->u.hdr.list));
 
 			if (SN_EQUAL(preorder_ctrl->indicate_seq, pattrib->seq_num))
-				preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) & 0xFFF;
+				preorder_ctrl->indicate_seq = (preorder_ctrl->indicate_seq + 1) % 4096u;
 
 			/* Set this as a lock to make sure that only one thread is indicating packet. */
 			/* pTS->RxIndicateState = RXTS_INDICATE_PROCESSING; */
drivers/staging/rtl8723bs/core/rtw_xmit.c +3 -3
index 5def2467f42b..76f25ab6a779 100644
@@ -943,7 +943,7 @@ s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattr
 
 			if (psta) {
 				psta->sta_xmitpriv.txseq_tid[pattrib->priority]++;
-				psta->sta_xmitpriv.txseq_tid[pattrib->priority] &= 0xFFF;
+				psta->sta_xmitpriv.txseq_tid[pattrib->priority] %= 4096u;
 				pattrib->seqnum = psta->sta_xmitpriv.txseq_tid[pattrib->priority];
 
 				SetSeqNum(hdr, pattrib->seqnum);
@@ -964,12 +964,12 @@ s32 rtw_make_wlanhdr(struct adapter *padapter, u8 *hdr, struct pkt_attrib *pattr
 						pattrib->ampdu_en = false;/* AGG BK */
 					} else if (SN_EQUAL(pattrib->seqnum, tx_seq)) {
 						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] =
-							(tx_seq + 1) & 0xfff;
+							(tx_seq + 1) % 4096u;
 
 						pattrib->ampdu_en = true;/* AGG EN */
 					} else {
 						psta->BA_starting_seqctrl[pattrib->priority & 0x0f] =
-							(pattrib->seqnum + 1) & 0xfff;
+							(pattrib->seqnum + 1) % 4096u;
 
 						pattrib->ampdu_en = true;/* AGG EN */
 					}
Signature · 1 line
2.34.1