Skip to contentSkip to conversation

staging: rtl8192e: Use min_t/max_t macros for variable comparison

PATCH In review Deepak R Varma 4 participants 7 messages Nov 2, 2022 – Nov 4, 2022 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: rtl8192e: Use min_t/max_t macros for variable comparison

Simplify code by using min_t and max_t helper macros in place of lengthy if/else block oriented logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

Use the *_t variants of min/max macros to avoid compiler warnings about data typecast.
Also, use u32 as type for min_t macro to avoid any truncation of data associated with enum constant HT_AGG_SIZE_32K.

---
 drivers/staging/rtl8192e/rtl819x_HTProc.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/rtl8192e/rtl819x_HTProc.c +4 -9
index 62aa8e893c34..ccb86660ab48 100644
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);
 	if (pHTInfo->iot_action & HT_IOT_ACT_TX_USE_AMSDU_8K) {
 		pHTInfo->bCurrentAMPDUEnable = false;
 		pHTInfo->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;

--
2.34.1

Tested-by on v1 1/1
#1 lore raw
Quoted 41 lines · Deepak R Varma

Simplify code by using min_t and max_t helper macros in place of lengthy if/else block oriented logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

Use the *_t variants of min/max macros to avoid compiler warnings about data typecast.
Also, use u32 as type for min_t macro to avoid any truncation of data associated with enum constant HT_AGG_SIZE_32K.

---
  drivers/staging/rtl8192e/rtl819x_HTProc.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/rtl8192e/rtl819x_HTProc.c +4 -10
index 62aa8e893c34..ccb86660ab48 100644
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
  			else
  				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
  		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);
  		}
  	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);
  	if (pHTInfo->iot_action & HT_IOT_ACT_TX_USE_AMSDU_8K) {
  		pHTInfo->bCurrentAMPDUEnable = false;
  		pHTInfo->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;
--
Quoted 5 lines

2.34.1

#2 lore raw
In reply to Deepak R Varma

From: Deepak R Varma

Quoted 21 lines

Sent: 02 November 2022 22:37

Simplify code by using min_t and max_t helper macros in place of lengthy if/else block oriented logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

Use the *_t variants of min/max macros to avoid compiler warnings about data typecast.
Also, use u32 as type for min_t macro to avoid any truncation of data associated with enum constant HT_AGG_SIZE_32K.

---
 drivers/staging/rtl8192e/rtl819x_HTProc.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/rtl8192e/rtl819x_HTProc.c +0 -0
index 62aa8e893c34..ccb86660ab48 100644

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 591

Quoted 10 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -5
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);

For min() to fail there must be a signed v unsigned mismatch. Maybe that ought to be fixed.

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 595

Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -4
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);

Using u8 with max_t() really doesn't make any sense. The value will get promoted to signed int prior to the comparison.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#3 lore raw
Quoted 8 lines · David Laight
Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +0 -2
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
Quoted 9 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 591

Quoted 5 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -3
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);

For min() to fail there must be a signed v unsigned mismatch. Maybe that ought to be fixed.

u32 is the right choice here.

I'm having a hard time understanding your email. You might be saying we could declare HT_AGG_SIZE_32K as a u32 so then we could use min() instead of min_t()? HT_AGG_SIZE_32K is an enum.

pPeerHTCap->MaxRxAMPDUFactor is a bitfield.

	u8 MaxRxAMPDUFactor:2;

We will never be able to use min().

Quoted 10 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 595

Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -4
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);

Using u8 with max_t() really doesn't make any sense.

Using u8 looks wrong because you would worry that one of the types is larger than U8_MAX. But it's actually fine. The types are u8 vs another bitfield. I would probably have gone with u32 here as well.

Quoted 2 lines

The value will get promoted to signed int prior to the comparison.

That's sort of true-ish but I don't understand what you are saying? #confused

regards,
dan carpenter

#4 lore raw
Quoted 25 lines · Dan Carpenter
Quoted 8 lines · David Laight
Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +0 -2
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
Quoted 9 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 591

Quoted 5 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -3
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);

For min() to fail there must be a signed v unsigned mismatch. Maybe that ought to be fixed.

u32 is the right choice here.

I'm having a hard time understanding your email. You might be saying we could declare HT_AGG_SIZE_32K as a u32 so then we could use min() instead of min_t()? HT_AGG_SIZE_32K is an enum.

Quoted 5 lines

pPeerHTCap->MaxRxAMPDUFactor is a bitfield.

	u8 MaxRxAMPDUFactor:2;

We will never be able to use min().

I think we could do min((u32)a, (u32)b), but it is just unwrapped min_t if I understand David's comment.

Quoted 12 lines
Quoted 11 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 595

Quoted 9 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -4
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);

Using u8 with max_t() really doesn't make any sense.

Quoted 3 lines

Using u8 looks wrong because you would worry that one of the types is larger than U8_MAX. But it's actually fine. The types are u8 vs another bitfield. I would probably have gone with u32 here as well.

I will take your advise and upgrade the type to u32 as a revision.

Quoted 6 lines
Quoted 3 lines

The value will get promoted to signed int prior to the comparison.

That's sort of true-ish but I don't understand what you are saying? #confused

Yes, I too did not understand David's comment. I tried to dig dipper into max_t but it gets very complex. Can you please elaborate how you determined the promotion to signed int?

Quoted 5 lines

regards,
dan carpenter

#5 lore raw
Quoted 26 lines · Deepak R Varma
Quoted 25 lines · Dan Carpenter
Quoted 8 lines · David Laight
Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +0 -2
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
Quoted 9 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 591

Quoted 5 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -3
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);

For min() to fail there must be a signed v unsigned mismatch. Maybe that ought to be fixed.

u32 is the right choice here.

I'm having a hard time understanding your email. You might be saying we could declare HT_AGG_SIZE_32K as a u32 so then we could use min() instead of min_t()? HT_AGG_SIZE_32K is an enum.

Quoted 9 lines
Quoted 5 lines

pPeerHTCap->MaxRxAMPDUFactor is a bitfield.

	u8 MaxRxAMPDUFactor:2;

We will never be able to use min().

I think we could do min((u32)a, (u32)b), but it is just unwrapped min_t if I understand David's comment.

No. Do not do that. I think it's a checkpatch warning. What you have is fine.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 595

Quoted 9 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -4
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);

Using u8 with max_t() really doesn't make any sense.

Quoted 4 lines
Quoted 3 lines

Using u8 looks wrong because you would worry that one of the types is larger than U8_MAX. But it's actually fine. The types are u8 vs another bitfield. I would probably have gone with u32 here as well.

I will take your advise and upgrade the type to u32 as a revision.

Sounds good. It's not something I would have asked you to redo the patch over, but it would have been my personal preference.

regards,
dan carpenter

#6 lore raw
Quoted 52 lines · Dan Carpenter
Quoted 26 lines · Deepak R Varma
Quoted 25 lines · Dan Carpenter
Quoted 8 lines · David Laight
Quoted 8 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +0 -2
@@ -587,17 +587,12 @@ void HTOnAssocRsp(struct rtllib_device *ieee)
 			else
 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
 		} else {
-			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
-				pHTInfo->CurrentAMPDUFactor =
Quoted 9 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 591

Quoted 5 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -3
-						 pPeerHTCap->MaxRxAMPDUFactor;
-			else
-				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
+			pHTInfo->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
+							    HT_AGG_SIZE_32K);

For min() to fail there must be a signed v unsigned mismatch. Maybe that ought to be fixed.

u32 is the right choice here.

I'm having a hard time understanding your email. You might be saying we could declare HT_AGG_SIZE_32K as a u32 so then we could use min() instead of min_t()? HT_AGG_SIZE_32K is an enum.

Quoted 9 lines
Quoted 5 lines

pPeerHTCap->MaxRxAMPDUFactor is a bitfield.

	u8 MaxRxAMPDUFactor:2;

We will never be able to use min().

I think we could do min((u32)a, (u32)b), but it is just unwrapped min_t if I understand David's comment.

No. Do not do that. I think it's a checkpatch warning. What you have is fine.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

drivers/staging/rtl8192e/rtl819x_HTProc.c void HTOnAssocRsp(struct rtllib_device *ieee) · line 595

Quoted 9 lines
drivers/staging/rtl8192e/rtl819x_HTProc.c +2 -4
 		}
 	}
-	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
-		pHTInfo->current_mpdu_density = pHTInfo->MPDU_Density;
-	else
-		pHTInfo->current_mpdu_density = pPeerHTCap->MPDUDensity;
+	pHTInfo->current_mpdu_density = max_t(u8, pHTInfo->MPDU_Density,
+					      pPeerHTCap->MPDUDensity);

Using u8 with max_t() really doesn't make any sense.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Using u8 looks wrong because you would worry that one of the types is larger than U8_MAX. But it's actually fine. The types are u8 vs another bitfield. I would probably have gone with u32 here as well.

I will take your advise and upgrade the type to u32 as a revision.

Sounds good. It's not something I would have asked you to redo the patch over, but it would have been my personal preference.

That is no problem. I am waiting on David to elaborate on his feedback and accordingly plan a consolidated revision.

Quoted 5 lines

regards,
dan carpenter