Skip to contentSkip to conversation

net: aquantia: minor bug fixes after static analysis

PATCH In review Igor Russkikh 2 participants 8 messages Feb 27, 2019 – Mar 2, 2019

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

v2 cover net: aquantia: minor bug fixes after static analysis

This patchset fixes minor errors and warnings found by smatch and kasan.

Extra patch is to replace AQ_HW_WAIT_FOR with readx_poll_timeout to improve readability.

V2:
use readx_poll
resubmitted to net-next since the changeset became quite big.

Igor Russkikh (1):
net: aquantia: fixed instack structure overflow

Nikita Danilov (5):
net: aquantia: fixed memcpy size
net: aquantia: added newline at end of file
net: aquantia: fixed buffer overflow
net: aquantia: replace AQ_HW_WAIT_FOR with readx_poll_timeout_atomic
net: aquantia: use better wrappers for state registers

 .../ethernet/aquantia/atlantic/aq_ethtool.c   |   2 +-
 .../ethernet/aquantia/atlantic/aq_hw_utils.h  |  14 +--
 .../net/ethernet/aquantia/atlantic/aq_nic.c   |   2 +-
 .../ethernet/aquantia/atlantic/aq_pci_func.c  |   2 +
 .../aquantia/atlantic/hw_atl/hw_atl_a0.c      |  25 ++--
 .../aquantia/atlantic/hw_atl/hw_atl_b0.c      |  16 ++-
 .../aquantia/atlantic/hw_atl/hw_atl_llh.c     |  21 ++++
 .../aquantia/atlantic/hw_atl/hw_atl_llh.h     |  12 ++
 .../atlantic/hw_atl/hw_atl_llh_internal.h     |   2 +
 .../aquantia/atlantic/hw_atl/hw_atl_utils.c   | 116 ++++++++++++------
 .../atlantic/hw_atl/hw_atl_utils_fw2x.c       |  68 +++++++---
 11 files changed, 197 insertions(+), 83 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/6 net: aquantia: fixed memcpy size

From: Nikita Danilov <[email protected]>

Not careful array dereference caused analysis tools to think there could be memory overflow.

There was actually no corruption because the array is two dimensional.

drivers/net/ethernet/aquantia/atlantic/aq_ethtool.c: 140
aq_ethtool_get_strings() error:

    memcpy() '*aq_ethtool_stat_names' too small (32 vs 704)
---
 drivers/net/ethernet/aquantia/atlantic/aq_ethtool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/aquantia/atlantic/aq_ethtool.c +1 -1
index 38e87eed76b9..a718d7a1f76c 100644
@@ -138,7 +138,7 @@ static void aq_ethtool_get_strings(struct net_device *ndev,
 	u8 *p = data;
 
 	if (stringset == ETH_SS_STATS) {
-		memcpy(p, *aq_ethtool_stat_names,
+		memcpy(p, aq_ethtool_stat_names,
 		       sizeof(aq_ethtool_stat_names));
 		p = p + sizeof(aq_ethtool_stat_names);
 		for (i = 0; i < cfg->vecs; i++) {
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/6 net: aquantia: added newline at end of file

In reply to Igor Russkikh

From: Nikita Danilov <[email protected]>

drivers/net/ethernet/aquantia/atlantic/aq_nic.c: 991:1:
warning: no newline at end of file

---
 drivers/net/ethernet/aquantia/atlantic/aq_nic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/aquantia/atlantic/aq_nic.c +1 -1
index 0147c037ca96..ff83667410bd 100644
@@ -986,4 +986,4 @@ void aq_nic_shutdown(struct aq_nic_s *self)
 
 err_exit:
 	rtnl_unlock();
-}
\ No newline at end of file
+}
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 3/6 net: aquantia: fixed buffer overflow

In reply to Igor Russkikh

From: Nikita Danilov <[email protected]>

The overflow is detected by smatch:

drivers/net/ethernet/aquantia/atlantic/aq_pci_func.c: 175
aq_pci_func_free_irqs() error: buffer overflow 'self->aq_vec' 8 <= 31

In reality msix_entry_mask always restricts number of iterations. Adding extra condition to make logic clear and smatch happy.

---
 drivers/net/ethernet/aquantia/atlantic/aq_pci_func.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/aquantia/atlantic/aq_pci_func.c +2 -0
index c8b44cdb91c1..0217ff4669a4 100644
@@ -170,6 +170,8 @@ void aq_pci_func_free_irqs(struct aq_nic_s *self)
 	for (i = 32U; i--;) {
 		if (!((1U << i) & self->msix_entry_mask))
 			continue;
+		if (i >= AQ_CFG_VECS_MAX)
+			continue;
 
 		if (pdev->msix_enabled)
 			irq_set_affinity_hint(pci_irq_vector(pdev, i), NULL);
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 4/6 net: aquantia: fixed instack structure overflow

In reply to Igor Russkikh

This is a real stack undercorruption found by kasan build.

The issue did no harm normally because it only overflowed 2 bytes after bitary array which on most architectures were mapped into err local.

Fixes bab6de8fd180 ("net: ethernet: aquantia: Atlantic A0 and B0 specific functions.")
---
 drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_a0.c | 4 ++--
 drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_b0.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_a0.c +2 -2
index 2469ed4d86b9..30fdcb9b11fd 100644
@@ -207,8 +207,8 @@ static int hw_atl_a0_hw_rss_set(struct aq_hw_s *self,
 	u32 i = 0U;
 	u32 num_rss_queues = max(1U, self->aq_nic_cfg->num_rss_queues);
 	int err = 0;
-	u16 bitary[(HW_ATL_A0_RSS_REDIRECTION_MAX *
-					HW_ATL_A0_RSS_REDIRECTION_BITS / 16U)];
+	u16 bitary[1 + (HW_ATL_A0_RSS_REDIRECTION_MAX *
+		   HW_ATL_A0_RSS_REDIRECTION_BITS / 16U)];
 
 	memset(bitary, 0, sizeof(bitary));
 
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_b0.c +2 -2
index b58ca7cb8e9d..c4cdc51350b2 100644
@@ -199,8 +199,8 @@ static int hw_atl_b0_hw_rss_set(struct aq_hw_s *self,
 	u32 i = 0U;
 	u32 num_rss_queues = max(1U, self->aq_nic_cfg->num_rss_queues);
 	int err = 0;
-	u16 bitary[(HW_ATL_B0_RSS_REDIRECTION_MAX *
-					HW_ATL_B0_RSS_REDIRECTION_BITS / 16U)];
+	u16 bitary[1 + (HW_ATL_B0_RSS_REDIRECTION_MAX *
+		   HW_ATL_B0_RSS_REDIRECTION_BITS / 16U)];
 
 	memset(bitary, 0, sizeof(bitary));
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 5/6 net: aquantia: replace AQ_HW_WAIT_FOR with readx_poll_timeout_atomic

In reply to Igor Russkikh

From: Nikita Danilov <[email protected]>

David noticed the original define was hiding 'err' variable reference. Thats confusing and counterintuitive.

Andrew noted the whole macro could be replaced with standard readx_poll kernel macro. This makes code more readable.

---
 .../ethernet/aquantia/atlantic/aq_hw_utils.h  |  14 +--
 .../aquantia/atlantic/hw_atl/hw_atl_a0.c      |  21 +++-
 .../aquantia/atlantic/hw_atl/hw_atl_b0.c      |  12 +-
 .../aquantia/atlantic/hw_atl/hw_atl_llh.c     |  21 ++++
 .../aquantia/atlantic/hw_atl/hw_atl_llh.h     |  12 ++
 .../atlantic/hw_atl/hw_atl_llh_internal.h     |   2 +
 .../aquantia/atlantic/hw_atl/hw_atl_utils.c   | 110 +++++++++++++-----
 .../atlantic/hw_atl/hw_atl_utils_fw2x.c       |  64 +++++++---
 8 files changed, 184 insertions(+), 72 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_hw_utils.h +2 -12
index dc88a1221f1d..bc711238ca0c 100644
@@ -14,6 +14,8 @@
 #ifndef AQ_HW_UTILS_H
 #define AQ_HW_UTILS_H
 
+#include <linux/iopoll.h>
+
 #include "aq_common.h"
 
 #ifndef HIDWORD
@@ -23,18 +25,6 @@
 
 #define AQ_HW_SLEEP(_US_) mdelay(_US_)
 
-#define AQ_HW_WAIT_FOR(_B_, _US_, _N_) \
-do { \
-	unsigned int AQ_HW_WAIT_FOR_i; \
-	for (AQ_HW_WAIT_FOR_i = _N_; (!(_B_)) && (AQ_HW_WAIT_FOR_i);\
-	--AQ_HW_WAIT_FOR_i) {\
-		udelay(_US_); \
-	} \
-	if (!AQ_HW_WAIT_FOR_i) {\
-		err = -ETIME; \
-	} \
-} while (0)
-
 #define aq_pr_err(...) pr_err(AQ_CFG_DRV_NAME ": " __VA_ARGS__)
 #define aq_pr_trace(...) pr_info(AQ_CFG_DRV_NAME ": " __VA_ARGS__)
 
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_a0.c +15 -6
index 30fdcb9b11fd..f6f8338153a2 100644
@@ -85,6 +85,7 @@ const struct aq_hw_caps_s hw_atl_a0_caps_aqc109 = {
 static int hw_atl_a0_hw_reset(struct aq_hw_s *self)
 {
 	int err = 0;
+	u32 val;
 
 	hw_atl_glb_glb_reg_res_dis_set(self, 1U);
 	hw_atl_pci_pci_reg_res_dis_set(self, 0U);
@@ -95,7 +96,9 @@ static int hw_atl_a0_hw_reset(struct aq_hw_s *self)
 	hw_atl_glb_soft_res_set(self, 1);
 
 	/* check 10 times by 1ms */
-	AQ_HW_WAIT_FOR(hw_atl_glb_soft_res_get(self) == 0, 1000U, 10U);
+	err = readx_poll_timeout_atomic(hw_atl_glb_soft_res_get,
+					self, val, val == 0,
+					1000U, 10000U);
 	if (err < 0)
 		goto err_exit;
 
@@ -103,7 +106,9 @@ static int hw_atl_a0_hw_reset(struct aq_hw_s *self)
 	hw_atl_itr_res_irq_set(self, 1U);
 
 	/* check 10 times by 1ms */
-	AQ_HW_WAIT_FOR(hw_atl_itr_res_irq_get(self) == 0, 1000U, 10U);
+	err = readx_poll_timeout_atomic(hw_atl_itr_res_irq_get,
+					self, val, val == 0,
+					1000U, 10000U);
 	if (err < 0)
 		goto err_exit;
 
@@ -181,6 +186,7 @@ static int hw_atl_a0_hw_rss_hash_set(struct aq_hw_s *self,
 	int err = 0;
 	unsigned int i = 0U;
 	unsigned int addr = 0U;
+	u32 val;
 
 	for (i = 10, addr = 0U; i--; ++addr) {
 		u32 key_data = cfg->is_rss ?
@@ -188,8 +194,9 @@ static int hw_atl_a0_hw_rss_hash_set(struct aq_hw_s *self,
 		hw_atl_rpf_rss_key_wr_data_set(self, key_data);
 		hw_atl_rpf_rss_key_addr_set(self, addr);
 		hw_atl_rpf_rss_key_wr_en_set(self, 1U);
-		AQ_HW_WAIT_FOR(hw_atl_rpf_rss_key_wr_en_get(self) == 0,
-			       1000U, 10U);
+		err = readx_poll_timeout_atomic(hw_atl_rpf_rss_key_wr_en_get,
+						self, val, val == 0,
+						1000U, 10000U);
 		if (err < 0)
 			goto err_exit;
 	}
@@ -209,6 +216,7 @@ static int hw_atl_a0_hw_rss_set(struct aq_hw_s *self,
 	int err = 0;
 	u16 bitary[1 + (HW_ATL_A0_RSS_REDIRECTION_MAX *
 		   HW_ATL_A0_RSS_REDIRECTION_BITS / 16U)];
+	u32 val;
 
 	memset(bitary, 0, sizeof(bitary));
 
@@ -222,8 +230,9 @@ static int hw_atl_a0_hw_rss_set(struct aq_hw_s *self,
 		hw_atl_rpf_rss_redir_tbl_wr_data_set(self, bitary[i]);
 		hw_atl_rpf_rss_redir_tbl_addr_set(self, i);
 		hw_atl_rpf_rss_redir_wr_en_set(self, 1U);
-		AQ_HW_WAIT_FOR(hw_atl_rpf_rss_redir_wr_en_get(self) == 0,
-			       1000U, 10U);
+		err = readx_poll_timeout_atomic(hw_atl_rpf_rss_redir_wr_en_get,
+						self, val, val == 0,
+						1000U, 10000U);
 		if (err < 0)
 			goto err_exit;
 	}
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_b0.c +8 -4
index c4cdc51350b2..7eb5ea948d61 100644
@@ -173,6 +173,7 @@ static int hw_atl_b0_hw_rss_hash_set(struct aq_hw_s *self,
 	int err = 0;
 	unsigned int i = 0U;
 	unsigned int addr = 0U;
+	u32 val;
 
 	for (i = 10, addr = 0U; i--; ++addr) {
 		u32 key_data = cfg->is_rss ?
@@ -180,8 +181,9 @@ static int hw_atl_b0_hw_rss_hash_set(struct aq_hw_s *self,
 		hw_atl_rpf_rss_key_wr_data_set(self, key_data);
 		hw_atl_rpf_rss_key_addr_set(self, addr);
 		hw_atl_rpf_rss_key_wr_en_set(self, 1U);
-		AQ_HW_WAIT_FOR(hw_atl_rpf_rss_key_wr_en_get(self) == 0,
-			       1000U, 10U);
+		err = readx_poll_timeout_atomic(hw_atl_rpf_rss_key_wr_en_get,
+						self, val, val == 0,
+						1000U, 10000U);
 		if (err < 0)
 			goto err_exit;
 	}
@@ -201,6 +203,7 @@ static int hw_atl_b0_hw_rss_set(struct aq_hw_s *self,
 	int err = 0;
 	u16 bitary[1 + (HW_ATL_B0_RSS_REDIRECTION_MAX *
 		   HW_ATL_B0_RSS_REDIRECTION_BITS / 16U)];
+	u32 val;
 
 	memset(bitary, 0, sizeof(bitary));
 
@@ -214,8 +217,9 @@ static int hw_atl_b0_hw_rss_set(struct aq_hw_s *self,
 		hw_atl_rpf_rss_redir_tbl_wr_data_set(self, bitary[i]);
 		hw_atl_rpf_rss_redir_tbl_addr_set(self, i);
 		hw_atl_rpf_rss_redir_wr_en_set(self, 1U);
-		AQ_HW_WAIT_FOR(hw_atl_rpf_rss_redir_wr_en_get(self) == 0,
-			       1000U, 10U);
+		err = readx_poll_timeout_atomic(hw_atl_rpf_rss_redir_wr_en_get,
+						self, val, val == 0,
+						1000U, 10000U);
 		if (err < 0)
 			goto err_exit;
 	}
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh.c +21 -0
index 939f77e2e117..f72194c77f5f 100644
@@ -1585,3 +1585,24 @@ void hw_atl_rpfl3l4_ipv6_dest_addr_set(struct aq_hw_s *aq_hw, u8 location,
 				HW_ATL_RPF_L3_DSTA_ADR(location + i),
 				ipv6_dest[i]);
 }
+
+u32 hw_atl_sem_ram_get(struct aq_hw_s *self)
+{
+	return hw_atl_reg_glb_cpu_sem_get(self, HW_ATL_FW_SM_RAM);
+}
+
+u32 hw_atl_scrpad_get(struct aq_hw_s *aq_hw, u32 scratch_scp)
+{
+	return aq_hw_read_reg(aq_hw,
+			      HW_ATL_GLB_CPU_SCRATCH_SCP_ADR(scratch_scp));
+}
+
+u32 hw_atl_scrpad12_get(struct aq_hw_s *self)
+{
+	return  hw_atl_scrpad_get(self, 0xB);
+}
+
+u32 hw_atl_scrpad25_get(struct aq_hw_s *self)
+{
+	return hw_atl_scrpad_get(self, 0x18);
+}
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh.h +12 -0
index 03c570d115fe..40e6c1e44b5b 100644
@@ -752,4 +752,16 @@ void hw_atl_rpfl3l4_ipv6_src_addr_set(struct aq_hw_s *aq_hw, u8 location,
 void hw_atl_rpfl3l4_ipv6_dest_addr_set(struct aq_hw_s *aq_hw, u8 location,
 				       u32 *ipv6_dest);
 
+/* get global microprocessor ram semaphore */
+u32 hw_atl_sem_ram_get(struct aq_hw_s *self);
+
+/* get global microprocessor scratch pad register */
+u32 hw_atl_scrpad_get(struct aq_hw_s *aq_hw, u32 scratch_scp);
+
+/* get global microprocessor scratch pad 12 register */
+u32 hw_atl_scrpad12_get(struct aq_hw_s *self);
+
+/* get global microprocessor scratch pad 25 register */
+u32 hw_atl_scrpad25_get(struct aq_hw_s *self);
+
 #endif /* HW_ATL_LLH_H */
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_llh_internal.h +2 -0
index 8470d92db812..50b4dee5562d 100644
@@ -2519,4 +2519,6 @@
 /* Default value of bitfield l3_da0[1F:0] */
 #define HW_ATL_RPF_L3_DSTA_DEFAULT 0x0
 
+#define HW_ATL_FW_SM_RAM        0x2U
+
 #endif /* HW_ATL_LLH_INTERNAL_H */
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils.c +78 -32

194 lines not on this page · raw message

drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils_fw2x.c +46 -18

128 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 6/6 net: aquantia: use better wrappers for state registers

In reply to Igor Russkikh

From: Nikita Danilov <[email protected]>

Replace some direct registers reads with better
online functions.

---
 .../net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils.c    | 6 +++---
 .../ethernet/aquantia/atlantic/hw_atl/hw_atl_utils_fw2x.c   | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils.c +3 -3
index e5df40b00afd..eb4b99d56081 100644
@@ -298,7 +298,7 @@ int hw_atl_utils_fw_downld_dwords(struct aq_hw_s *self, u32 a,
 		bool is_locked;
 
 		hw_atl_reg_glb_cpu_sem_set(self, 1U, HW_ATL_FW_SM_RAM);
-		is_locked = hw_atl_reg_glb_cpu_sem_get(self, HW_ATL_FW_SM_RAM);
+		is_locked = hw_atl_sem_ram_get(self);
 		if (!is_locked) {
 			err = -ETIME;
 			goto err_exit;
@@ -337,7 +337,7 @@ static int hw_atl_utils_fw_upload_dwords(struct aq_hw_s *self, u32 a, u32 *p,
 	int err = 0;
 	bool is_locked;
 
-	is_locked = hw_atl_reg_glb_cpu_sem_get(self, HW_ATL_FW_SM_RAM);
+	is_locked = hw_atl_sem_ram_get(self);
 	if (!is_locked) {
 		err = -ETIME;
 		goto err_exit;
@@ -602,7 +602,7 @@ static int hw_atl_utils_mpi_set_state(struct aq_hw_s *self,
 
 int hw_atl_utils_mpi_get_link_status(struct aq_hw_s *self)
 {
-	u32 cp0x036C = aq_hw_read_reg(self, HW_ATL_MPI_STATE_ADR);
+	u32 cp0x036C = hw_atl_utils_mpi_get_state(self);
 	u32 link_speed_mask = cp0x036C >> HW_ATL_MPI_SPEED_SHIFT;
 	struct aq_hw_link_status_s *link_status = &self->aq_link_status;
 
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_utils_fw2x.c +2 -2
index c628290d7546..fe6c5658e016 100644
@@ -438,7 +438,7 @@ static int aq_fw2x_get_eee_rate(struct aq_hw_s *self, u32 *rate,
 
 	*supported_rates = fw2x_to_eee_mask(caps_hi);
 
-	mpi_state = aq_hw_read_reg(self, HW_ATL_FW2X_MPI_STATE2_ADDR);
+	mpi_state = aq_fw2x_state2_get(self);
 	*rate = fw2x_to_eee_mask(mpi_state);
 
 	return err;
@@ -468,7 +468,7 @@ static int aq_fw2x_set_flow_control(struct aq_hw_s *self)
 
 static u32 aq_fw2x_get_flow_control(struct aq_hw_s *self, u32 *fcmode)
 {
-	u32 mpi_state = aq_hw_read_reg(self, HW_ATL_FW2X_MPI_STATE2_ADDR);
+	u32 mpi_state = aq_fw2x_state2_get(self);
 
 	if (mpi_state & HW_ATL_FW2X_CAP_PAUSE)
 		if (mpi_state & HW_ATL_FW2X_CAP_ASYM_PAUSE)
Signature · 1 line
2.17.1
maintainer
#7 lore raw
In reply to Igor Russkikh

From: Igor Russkikh <[email protected]>
Date: Wed, 27 Feb 2019 12:10:01 +0000

Quoted 8 lines

This patchset fixes minor errors and warnings found by smatch and kasan.

Extra patch is to replace AQ_HW_WAIT_FOR with readx_poll_timeout to improve readability.

V2:
use readx_poll
resubmitted to net-next since the changeset became quite big.

Series applied, thanks Igor.