Skip to contentSkip to conversation

igc: optimize igc_ptp_systim_to_hwtstamp()

PATCH In review [email protected] 2 participants 2 messages Aug 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 igc: optimize igc_ptp_systim_to_hwtstamp()

From: Tom Rix <[email protected]>

Static analysis reports this representative problem igc_ptp.c:676:3: warning: The left operand of '+' is a garbage value

                ktime_add_ns(shhwtstamps.hwtstamp, adjust);
                ^            ~~~~~~~~~~~~~~~~~~~~

The issue is flagged because the setting of shhwtstamps is in igc_ptp_systim_to_hwtstamp() it is set only in one path through this switch.

	switch (adapter->hw.mac.type) {
	case igc_i225:
		memset(hwtstamps, 0, sizeof(*hwtstamps));
		/* Upper 32 bits contain s, lower 32 bits contain ns. */
		hwtstamps->hwtstamp = ktime_set(systim >> 32,
						systim & 0xFFFFFFFF);
		break;
	default:
		break;
	}

Changing the memset the a caller initialization is a small optimization and will resolve uninitialized use issue.

A switch statement with one case is overkill, convert to an if statement.

This function is small and only called once, change to inline for an expected small runtime and size improvement.

---
 drivers/net/ethernet/intel/igc/igc_ptp.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/net/ethernet/intel/igc/igc_ptp.c +6 -12
index 0f021909b430a0..1443a2da246e22 100644
@@ -417,20 +417,14 @@ static int igc_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
  * We need to convert the system time value stored in the RX/TXSTMP registers
  * into a hwtstamp which can be used by the upper level timestamping functions.
  **/
-static void igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
-				       struct skb_shared_hwtstamps *hwtstamps,
-				       u64 systim)
+static inline void igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
+					      struct skb_shared_hwtstamps *hwtstamps,
+					      u64 systim)
 {
-	switch (adapter->hw.mac.type) {
-	case igc_i225:
-		memset(hwtstamps, 0, sizeof(*hwtstamps));
-		/* Upper 32 bits contain s, lower 32 bits contain ns. */
+	/* Upper 32 bits contain s, lower 32 bits contain ns. */
+	if (adapter->hw.mac.type == igc_i225)
 		hwtstamps->hwtstamp = ktime_set(systim >> 32,
 						systim & 0xFFFFFFFF);
-		break;
-	default:
-		break;
-	}
 }
 
 /**
@@ -645,7 +639,7 @@ void igc_ptp_tx_hang(struct igc_adapter *adapter)
 static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter)
 {
 	struct sk_buff *skb = adapter->ptp_tx_skb;
-	struct skb_shared_hwtstamps shhwtstamps;
+	struct skb_shared_hwtstamps shhwtstamps = { 0 };
 	struct igc_hw *hw = &adapter->hw;
 	int adjust = 0;
 	u64 regval;
Signature · 1 line
2.26.3
#1 lore raw
Quoted 55 lines · trix at redhat.com

From: Tom Rix <[email protected]>

Static analysis reports this representative problem igc_ptp.c:676:3: warning: The left operand of '+' is a garbage value

                ktime_add_ns(shhwtstamps.hwtstamp, adjust);
                ^            ~~~~~~~~~~~~~~~~~~~~

The issue is flagged because the setting of shhwtstamps is in igc_ptp_systim_to_hwtstamp() it is set only in one path through this switch.

	switch (adapter->hw.mac.type) {
	case igc_i225:
		memset(hwtstamps, 0, sizeof(*hwtstamps));
		/* Upper 32 bits contain s, lower 32 bits contain ns.
*/
		hwtstamps->hwtstamp = ktime_set(systim >> 32,
						systim & 0xFFFFFFFF);
		break;
	default:
		break;
	}

Changing the memset the a caller initialization is a small optimization
and will resolve uninitialized use issue.

A switch statement with one case is overkill, convert to an if statement.

This function is small and only called once, change to inline for an expected small runtime and size improvement.

---
 drivers/net/ethernet/intel/igc/igc_ptp.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/net/ethernet/intel/igc/igc_ptp.c +0 -0
index 0f021909b430a0..1443a2da246e22 100644
@@ -417,20 +417,14 @@ static int igc_ptp_verify_pin(struct
 ptp_clock_info *ptp, unsigned int pin,
  * We need to convert the system time value stored in the RX/TXSTMP

registers
* into a hwtstamp which can be used by the upper level timestamping
functions.
**/

drivers/net/ethernet/intel/igc/igc_ptp.c +0 -2
-static void igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
-				       struct skb_shared_hwtstamps

*hwtstamps,

drivers/net/ethernet/intel/igc/igc_ptp.c +1 -1
-				       u64 systim)
+static inline void igc_ptp_systim_to_hwtstamp(struct igc_adapter
Quoted 1 line

*adapter,

Please don't use inline in C files, let the compiler decide.

Quoted 21 lines
drivers/net/ethernet/intel/igc/igc_ptp.c +1 -0
+					      struct

skb_shared_hwtstamps *hwtstamps,

drivers/net/ethernet/intel/igc/igc_ptp.c +1 -4
+					      u64 systim)
 {
-	switch (adapter->hw.mac.type) {
-	case igc_i225:
-		memset(hwtstamps, 0, sizeof(*hwtstamps));
-		/* Upper 32 bits contain s, lower 32 bits contain ns.

*/

drivers/net/ethernet/intel/igc/igc_ptp.c +2 -4
+	/* Upper 32 bits contain s, lower 32 bits contain ns. */
+	if (adapter->hw.mac.type == igc_i225)
 		hwtstamps->hwtstamp = ktime_set(systim >> 32,
 						systim & 0xFFFFFFFF);
-		break;
-	default:
-		break;
-	}
 }
 
 /**

drivers/net/ethernet/intel/igc/igc_ptp.c void igc_ptp_tx_hang(struct igc_adapter *adapter) · line 642

Quoted 5 lines
drivers/net/ethernet/intel/igc/igc_ptp.c +1 -1
@@ -645,7 +639,7 @@ void igc_ptp_tx_hang(struct igc_adapter *adapter)
 static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter)
 {
 	struct sk_buff *skb = adapter->ptp_tx_skb;
-	struct skb_shared_hwtstamps shhwtstamps;
+	struct skb_shared_hwtstamps shhwtstamps = { 0 };

Need to re-order for RCT.

Thanks,
Tony

Quoted 3 lines
drivers/net/ethernet/intel/igc/igc_ptp.c +0 -0
 	struct igc_hw *hw = &adapter->hw;
 	int adjust = 0;
 	u64 regval;