Skip to contentSkip to conversation

net: hns3: There are some cleanup for the HNS3 ethernet driver

PATCH Superseded Hao Lan 2 participants 17 messages May 15, 2023 – May 16, 2023

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hao Lan

author
#0 lore raw

cover net: hns3: There are some cleanup for the HNS3 ethernet driver

There are some cleanup for the HNS3 ethernet driver.

Hao Chen (2):
net: hns3: fix hns3 driver header file not self-contained issue
net: hns3: fix strncpy() not using dest-buf length as length issue

Jian Shen (1):
net: hns3: refine the tcam key convert handle

Peiyang Wang (1):
net: hns3: clear hns unused parameter alarm

 .../net/ethernet/hisilicon/hns3/hclge_mbx.h   |  4 +-
 drivers/net/ethernet/hisilicon/hns3/hnae3.h   |  3 +-
 .../hns3/hns3_common/hclge_comm_rss.c         |  3 +-
 .../hns3/hns3_common/hclge_comm_rss.h         |  3 +-
 .../hns3/hns3_common/hclge_comm_tqp_stats.h   |  2 +
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 36 ++++++++++----
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   |  2 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |  5 +-
 .../ethernet/hisilicon/hns3/hns3_ethtool.c    |  7 ++-
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++--
 .../hisilicon/hns3/hns3pf/hclge_main.c        | 47 ++++++++-----------
 .../hisilicon/hns3/hns3pf/hclge_main.h        | 11 ++---
 .../hisilicon/hns3/hns3pf/hclge_ptp.h         |  5 +-
 .../hisilicon/hns3/hns3vf/hclgevf_main.c      | 14 ++----
 14 files changed, 97 insertions(+), 74 deletions(-)
Signature · 1 line
2.30.0
author
#1 lore raw

PATCH 1/4 net: hns3: refine the tcam key convert handle

From: Jian Shen <[email protected]>

The expression '(k ^ ~v)' is exaclty '(k & v)', and '(k & v) & k' is exaclty 'k & v'. So simplify the expression for tcam key convert.
(k ^ ~v) & k ==  (k & v) & k ==  k & k & v == k & v

It also add necessary brackets for them.

---
 .../net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h   | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +3 -8
index 81aa6b0facf5..6a43d1515585 100644
@@ -835,15 +835,10 @@ struct hclge_vf_vlan_cfg {
  * Then for input key(k) and mask(v), we can calculate the value by
  * the formulae:
  *	x = (~k) & v
- *	y = (k ^ ~v) & k
+ *	y = k & v
  */
-#define calc_x(x, k, v) (x = ~(k) & (v))
-#define calc_y(y, k, v) \
-	do { \
-		const typeof(k) _k_ = (k); \
-		const typeof(v) _v_ = (v); \
-		(y) = (_k_ ^ ~_v_) & (_k_); \
-	} while (0)
+#define calc_x(x, k, v) ((x) = ~(k) & (v))
+#define calc_y(y, k, v) ((y) = (k) & (v))
 
 #define HCLGE_MAC_STATS_FIELD_OFF(f) (offsetof(struct hclge_mac_stats, f))
 #define HCLGE_STATS_READ(p, offset) (*(u64 *)((u8 *)(p) + (offset)))
Signature · 1 line
2.30.0
author
#4 lore raw

PATCH 2/4 net: hns3: fix hns3 driver header file not self-contained issue

In reply to Hao Lan

From: Hao Chen <[email protected]>

Hns3 driver header file uses the structure of other files, but does not include corresponding file, which causes a check warning that the header file is not self-contained by clang-tidy checker.

For example,
Header file 'hclge_mbx.h' is not self contained.
It should include following headers: (1) 'hclgevf_main.h' due to symbols 'struct hclgevf_dev'. The main source file is hns3_enet.c

Therefore, the required header file is included in the header file, and the structure declaration is added to the header file to avoid cyclic dependency of the header file.

---
 drivers/net/ethernet/hisilicon/hns3/hclge_mbx.h              | 4 +++-
 .../hisilicon/hns3/hns3_common/hclge_comm_tqp_stats.h        | 2 ++
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.h              | 3 +++
 drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_ptp.h       | 5 ++++-
 4 files changed, 12 insertions(+), 2 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hclge_mbx.h +3 -1
index abcd7877f7d2..487216aeae50 100644
@@ -7,6 +7,8 @@
 #include <linux/mutex.h>
 #include <linux/types.h>
 
+struct hclgevf_dev;
+
 enum HCLGE_MBX_OPCODE {
 	HCLGE_MBX_RESET = 0x01,		/* (VF -> PF) assert reset */
 	HCLGE_MBX_ASSERTING_RESET,	/* (PF -> VF) PF is asserting reset */
@@ -233,7 +235,7 @@ struct hclgevf_mbx_arq_ring {
 	__le16 msg_q[HCLGE_MBX_MAX_ARQ_MSG_NUM][HCLGE_MBX_MAX_ARQ_MSG_SIZE];
 };
 
-struct hclge_dev;
+struct hclge_vport;
 
 #define HCLGE_MBX_OPCODE_MAX 256
 struct hclge_mbx_ops_param {
drivers/net/ethernet/hisilicon/hns3/hns3_common/hclge_comm_tqp_stats.h +2 -0
index a46350162ee8..7aff1a544cf4 100644
@@ -7,6 +7,8 @@
 #include <linux/etherdevice.h>
 #include "hnae3.h"
 
+struct hclge_comm_hw;
+
 /* each tqp has TX & RX two queues */
 #define HCLGE_COMM_QUEUE_PAIR_SIZE 2
 
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +3 -0
index 88af34bbee34..1b360aa52e5d 100644
@@ -13,6 +13,9 @@
 
 struct iphdr;
 struct ipv6hdr;
+struct gre_base_hdr;
+struct tcphdr;
+struct udphdr;
 
 enum hns3_nic_state {
 	HNS3_NIC_STATE_TESTING,
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_ptp.h +4 -1
index bbee74cd8404..bceb61c791a1 100644
@@ -8,8 +8,11 @@
 #include <linux/net_tstamp.h>
 #include <linux/types.h>
 
-struct hclge_dev;
 struct ifreq;
+struct ethtool_ts_info;
+
+struct hnae3_handle;
+struct hclge_dev;
 
 #define HCLGE_PTP_REG_OFFSET	0x29000
 
Signature · 1 line
2.30.0
author
#8 lore raw

PATCH 3/4 net: hns3: fix strncpy() not using dest-buf length as length issue

In reply to Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +24 -7
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));
+		}
+		pos += item_len;
+		len -= item_len;
 	}
-
 	*pos++ = '\n';
 	*pos++ = '\0';
 }
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c +24 -5
index a0b46e7d863e..1354fd0461f7 100644
@@ -88,16 +88,35 @@ static void hclge_dbg_fill_content(char *content, u16 len,
 				   const struct hclge_dbg_item *items,
 				   const char **result, u16 size)
 {
+#define HCLGE_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HCLGE_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
+	len -= HCLGE_DBG_LINE_END_LEN;
+
 	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
-		pos += strlen(items[i].name) + items[i].interval;
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));
+		}
+		pos += item_len;
+		len -= item_len;
 	}
 	*pos++ = '\n';
 	*pos++ = '\0';
Signature · 1 line
2.30.0
author
#14 lore raw

PATCH 4/4 net: hns3: clear hns unused parameter alarm

In reply to Hao Lan

From: Peiyang Wang <[email protected]>

Several functions in the hns3 driver have unused parameters. The compiler will warn about them when building with -Wunused-parameter option of hns3.

---
 drivers/net/ethernet/hisilicon/hns3/hnae3.h   |  3 +-
 .../hns3/hns3_common/hclge_comm_rss.c         |  3 +-
 .../hns3/hns3_common/hclge_comm_rss.h         |  3 +-
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    |  5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   |  2 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |  2 +-
 .../ethernet/hisilicon/hns3/hns3_ethtool.c    |  7 ++-
 .../hisilicon/hns3/hns3pf/hclge_main.c        | 47 ++++++++-----------
 .../hisilicon/hns3/hns3vf/hclgevf_main.c      | 14 ++----
 9 files changed, 34 insertions(+), 52 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hnae3.h +1 -2
index 9c9c72dc57e0..b99d75260d59 100644
@@ -647,8 +647,7 @@ struct hnae3_ae_ops {
 	int (*rm_mc_addr)(struct hnae3_handle *handle,
 			  const unsigned char *addr);
 	void (*set_tso_stats)(struct hnae3_handle *handle, int enable);
-	void (*update_stats)(struct hnae3_handle *handle,
-			     struct net_device_stats *net_stats);
+	void (*update_stats)(struct hnae3_handle *handle);
 	void (*get_stats)(struct hnae3_handle *handle, u64 *data);
 	void (*get_mac_stats)(struct hnae3_handle *handle,
 			      struct hns3_mac_stats *mac_stats);
drivers/net/ethernet/hisilicon/hns3/hns3_common/hclge_comm_rss.c +1 -2
index ae2736549526..b4ae2160aff4 100644
@@ -305,8 +305,7 @@ int hclge_comm_set_rss_indir_table(struct hnae3_ae_dev *ae_dev,
 	return 0;
 }
 
-int hclge_comm_set_rss_input_tuple(struct hnae3_handle *nic,
-				   struct hclge_comm_hw *hw, bool is_pf,
+int hclge_comm_set_rss_input_tuple(struct hclge_comm_hw *hw,
 				   struct hclge_comm_rss_cfg *rss_cfg)
 {
 	struct hclge_comm_rss_input_tuple_cmd *req;
drivers/net/ethernet/hisilicon/hns3/hns3_common/hclge_comm_rss.h +1 -2
index 92af3d2980d3..cdafa63fe38b 100644
@@ -112,8 +112,7 @@ int hclge_comm_init_rss_tuple_cmd(struct hclge_comm_rss_cfg *rss_cfg,
 				  struct hnae3_ae_dev *ae_dev,
 				  struct hclge_comm_rss_input_tuple_cmd *req);
 u64 hclge_comm_convert_rss_tuple(u8 tuple_sets);
-int hclge_comm_set_rss_input_tuple(struct hnae3_handle *nic,
-				   struct hclge_comm_hw *hw, bool is_pf,
+int hclge_comm_set_rss_input_tuple(struct hclge_comm_hw *hw,
 				   struct hclge_comm_rss_cfg *rss_cfg);
 int hclge_comm_set_rss_indir_table(struct hnae3_ae_dev *ae_dev,
 				   struct hclge_comm_hw *hw, const u16 *indir);
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +2 -3
index cf415cb37685..8679dd91a8a2 100644
@@ -958,8 +958,7 @@ static const struct hns3_dbg_item tx_bd_info_items[] = {
 	{ "MSS_HW_CSUM", 0 },
 };
 
-static void hns3_dump_tx_bd_info(struct hns3_nic_priv *priv,
-				 struct hns3_desc *desc, char **result, int idx)
+static void hns3_dump_tx_bd_info(struct hns3_desc *desc, char **result, int idx)
 {
 	unsigned int j = 0;
 
@@ -1008,7 +1007,7 @@ static int hns3_dbg_tx_bd_info(struct hns3_dbg_data *d, char *buf, int len)
 	for (i = 0; i < ring->desc_num; i++) {
 		desc = &ring->desc[i];
 
-		hns3_dump_tx_bd_info(priv, desc, result, i);
+		hns3_dump_tx_bd_info(desc, result, i);
 		hns3_dbg_fill_content(content, sizeof(content),
 				      tx_bd_info_items, (const char **)result,
 				      ARRAY_SIZE(tx_bd_info_items));
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +1 -1
index b676496ec6d7..9f6890059666 100644
@@ -2538,7 +2538,7 @@ static void hns3_nic_get_stats64(struct net_device *netdev,
 	if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
 		return;
 
-	handle->ae_algo->ops->update_stats(handle, &netdev->stats);
+	handle->ae_algo->ops->update_stats(handle);
 
 	memset(&ring_total_stats, 0, sizeof(ring_total_stats));
 	for (idx = 0; idx < queue_num; idx++) {
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +1 -1
index 1b360aa52e5d..8c0016b359b7 100644
@@ -693,7 +693,7 @@ static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring)
 
 /* iterator for handling rings in ring group */
 #define hns3_for_each_ring(pos, head) \
-	for (pos = (head).ring; (pos); pos = (pos)->next)
+	for ((pos) = (head).ring; (pos); (pos) = (pos)->next)
 
 #define hns3_get_handle(ndev) \
 	(((struct hns3_nic_priv *)netdev_priv(ndev))->ae_handle)
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c +3 -4
index 51d1278b18f6..407d30ee55d2 100644
@@ -228,7 +228,7 @@ static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
 }
 
 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
-				  u32 end_ringid, u32 budget)
+				  u32 end_ringid)
 {
 	u32 i;
 
@@ -295,8 +295,7 @@ static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
 
 out:
 	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
-			      HNS3_NIC_LB_TEST_RING_ID,
-			      HNS3_NIC_LB_TEST_PKT_NUM);
+			      HNS3_NIC_LB_TEST_RING_ID);
 
 	kfree_skb(skb);
 	return ret_val;
@@ -618,7 +617,7 @@ static void hns3_get_stats(struct net_device *netdev,
 		return;
 	}
 
-	h->ae_algo->ops->update_stats(h, &netdev->stats);
+	h->ae_algo->ops->update_stats(h);
 
 	/* get per-queue stats */
 	p = hns3_get_stats_tqps(h, p);
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c +19 -28

129 lines not on this page · raw message

drivers/net/ethernet/hisilicon/hns3/hns3vf/hclgevf_main.c +5 -9
index f24046250341..a84a15b7f645 100644
@@ -121,8 +121,7 @@ static struct hclgevf_dev *hclgevf_ae_get_hdev(struct hnae3_handle *handle)
 		return container_of(handle, struct hclgevf_dev, nic);
 }
 
-static void hclgevf_update_stats(struct hnae3_handle *handle,
-				 struct net_device_stats *net_stats)
+static void hclgevf_update_stats(struct hnae3_handle *handle)
 {
 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
 	int status;
@@ -1642,8 +1641,7 @@ static void hclgevf_reset(struct hclgevf_dev *hdev)
 	hclgevf_reset_err_handle(hdev);
 }
 
-static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev,
-						     unsigned long *addr)
+static enum hnae3_reset_type hclgevf_get_reset_level(unsigned long *addr)
 {
 	enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
 
@@ -1682,8 +1680,7 @@ static void hclgevf_reset_event(struct pci_dev *pdev,
 
 	if (hdev->default_reset_request)
 		hdev->reset_level =
-			hclgevf_get_reset_level(hdev,
-						&hdev->default_reset_request);
+			hclgevf_get_reset_level(&hdev->default_reset_request);
 	else
 		hdev->reset_level = HNAE3_VF_FUNC_RESET;
 
@@ -1825,7 +1822,7 @@ static void hclgevf_reset_service_task(struct hclgevf_dev *hdev)
 
 		hdev->last_reset_time = jiffies;
 		hdev->reset_type =
-			hclgevf_get_reset_level(hdev, &hdev->reset_pending);
+			hclgevf_get_reset_level(&hdev->reset_pending);
 		if (hdev->reset_type != HNAE3_NONE_RESET)
 			hclgevf_reset(hdev);
 	} else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED,
@@ -2157,8 +2154,7 @@ static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev)
 		if (ret)
 			return ret;
 
-		ret = hclge_comm_set_rss_input_tuple(&hdev->nic, &hdev->hw.hw,
-						     false, rss_cfg);
+		ret = hclge_comm_set_rss_input_tuple(&hdev->hw.hw, rss_cfg);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.30.0
#2 lore raw

Re: [PATCH net-next 1/4] net: hns3: refine the tcam key convert handle

In reply to Hao Lan
Quoted 3 lines · Hao Lan

From: Jian Shen <[email protected]>

The expression '(k ^ ~v)' is exaclty '(k & v)', and

This part doesn't seem correct to me.

Suppose both k and v are 0.
For simplicity, consider only one bit.

Then: (k ^ ~v) == (0 ^ ~0) == (0 ^ 1) = 1
But (k & v) == (0 & 0) == 0

Quoted 2 lines

'(k & v) & k' is exaclty 'k & v'. So simplify the
expression for tcam key convert.

This I follow.

Quoted 1 line

(k ^ ~v) & k ==  (k & v) & k ==  k & k & v == k & v

Looking at the truth table (in non table form), this seems correct.

k == 0, v == 0:

(k ^ ~v) & k == (0 ^ ~0) & 0 == (0 ^ 1) & 0 == 1 & 0 == 0 k & v == 0 & 0 == 0
Good!

k == 0, v == 1:

(k ^ ~v) & k == (0 ^ ~1) & 0 == (0 ^ 0) & 0 == 1 & 0 == 0 k & v == 0 & 1 == 0
Good!

k == 1, v == 0:

(k ^ ~v) & k == (1 ^ ~0) & 1 == (1 ^ 1) & 1 == 0 & 1 == 0 k & v == 1 & 0 == 0
Good!

k == 1, v == 1:

(k ^ ~v) & k == (1 ^ ~1) & 1 == (1 ^ 0) & 1 == 1 & 1 == 1 k & v == 1 & 1 == 1
Good!

Quoted 19 lines

It also add necessary brackets for them.

---
 .../net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h   | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +1 -1
index 81aa6b0facf5..6a43d1515585 100644
@@ -835,15 +835,10 @@ struct hclge_vf_vlan_cfg {
  * Then for input key(k) and mask(v), we can calculate the value by
  * the formulae:
  *	x = (~k) & v
- *	y = (k ^ ~v) & k
+ *	y = k & v

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h struct hclge_vf_vlan_cfg { · line 841

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +2 -7
  */
-#define calc_x(x, k, v) (x = ~(k) & (v))
-#define calc_y(y, k, v) \
-	do { \
-		const typeof(k) _k_ = (k); \
-		const typeof(v) _v_ = (v); \
-		(y) = (_k_ ^ ~_v_) & (_k_); \
-	} while (0)
+#define calc_x(x, k, v) ((x) = ~(k) & (v))
+#define calc_y(y, k, v) ((y) = (k) & (v))

This also looks good to me.

Quoted 3 lines
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +0 -0
 #define HCLGE_MAC_STATS_FIELD_OFF(f) (offsetof(struct hclge_mac_stats, f))
 #define HCLGE_STATS_READ(p, offset) (*(u64 *)((u8 *)(p) + (offset)))
#9 lore raw

Re: [PATCH net-next 3/4] net: hns3: fix strncpy() not using dest-buf length as length issue

In reply to Hao Lan
Quoted 65 lines · Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +11 -6
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c static void hns3_dbg_fill_content(char *content, u16 len, · line 466

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +10 -0
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));

Hi,

The above memcpy() calls share the same property as the warning that is being addressed: the length copied depends on the source not the destination.

With the reworked code this seems safe. Which is good. But I wonder if, given all the checking done, it makes sense to simply call strcpy() here. Using strlen() as a length argument seems odd to me.

Quoted 57 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +3 -1
+		}
+		pos += item_len;
+		len -= item_len;
 	}
-
 	*pos++ = '\n';
 	*pos++ = '\0';
 }
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c +24 -5
index a0b46e7d863e..1354fd0461f7 100644
@@ -88,16 +88,35 @@ static void hclge_dbg_fill_content(char *content, u16 len,
 				   const struct hclge_dbg_item *items,
 				   const char **result, u16 size)
 {
+#define HCLGE_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HCLGE_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
+	len -= HCLGE_DBG_LINE_END_LEN;
+
 	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
-		pos += strlen(items[i].name) + items[i].interval;
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));
+		}
+		pos += item_len;
+		len -= item_len;
 	}
 	*pos++ = '\n';
 	*pos++ = '\0';
Signature · 1 line
2.30.0
#15 lore raw

Re: [PATCH net-next 4/4] net: hns3: clear hns unused parameter alarm

In reply to Hao Lan
Quoted 8 lines · Hao Lan

From: Peiyang Wang <[email protected]>

Several functions in the hns3 driver have unused parameters. The compiler will warn about them when building with -Wunused-parameter option of hns3.

...

drivers/net/ethernet/hisilicon/hns3/hns3_enet.h static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring) · line 696

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +1 -1
index 1b360aa52e5d..8c0016b359b7 100644
@@ -693,7 +693,7 @@ static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring)
 
 /* iterator for handling rings in ring group */
 #define hns3_for_each_ring(pos, head) \
-	for (pos = (head).ring; (pos); pos = (pos)->next)
+	for ((pos) = (head).ring; (pos); (pos) = (pos)->next)

Hi,

A minor nit from my side.

This hunk does not seem related to the topic of this patch.

Quoted 3 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +0 -0
 #define hns3_get_handle(ndev) \
 	(((struct hns3_nic_priv *)netdev_priv(ndev))->ae_handle)

...

#5 lore raw

Re: [PATCH net-next 2/4] net: hns3: fix hns3 driver header file not self-contained issue

In reply to Hao Lan
Quoted 11 lines · Hao Lan

From: Hao Chen <[email protected]>

Hns3 driver header file uses the structure of other files, but does not include corresponding file, which causes a check warning that the header file is not self-contained by clang-tidy checker.

For example,
Header file 'hclge_mbx.h' is not self contained.
It should include following headers: (1) 'hclgevf_main.h' due to symbols 'struct hclgevf_dev'. The main source file is hns3_enet.c

Quoted 6 lines

Therefore, the required header file is included in the header file, and the structure declaration is added to the header file to avoid cyclic dependency of the header file.

Hi,

out of curiosity I'm wondering if you could provide some more information on how you generated the warnings that you are addressing here.

#3 lore raw

Re: [PATCH net-next 1/4] net: hns3: refine the tcam key convert handle

In reply to Simon Horman
Quoted 4 lines · Simon Horman
Quoted 3 lines · Hao Lan

From: Jian Shen <[email protected]>

The expression '(k ^ ~v)' is exaclty '(k & v)', and

Sorry, one more thing: s/exaclty/exactly/
Also one more time below.

Quoted 78 lines

This part doesn't seem correct to me.

Suppose both k and v are 0.
For simplicity, consider only one bit.

Then: (k ^ ~v) == (0 ^ ~0) == (0 ^ 1) = 1
But (k & v) == (0 & 0) == 0

Quoted 2 lines

'(k & v) & k' is exaclty 'k & v'. So simplify the
expression for tcam key convert.

This I follow.

Quoted 1 line

(k ^ ~v) & k ==  (k & v) & k ==  k & k & v == k & v

Looking at the truth table (in non table form), this seems correct.

k == 0, v == 0:

(k ^ ~v) & k == (0 ^ ~0) & 0 == (0 ^ 1) & 0 == 1 & 0 == 0 k & v == 0 & 0 == 0
Good!

k == 0, v == 1:

(k ^ ~v) & k == (0 ^ ~1) & 0 == (0 ^ 0) & 0 == 1 & 0 == 0 k & v == 0 & 1 == 0
Good!

k == 1, v == 0:

(k ^ ~v) & k == (1 ^ ~0) & 1 == (1 ^ 1) & 1 == 0 & 1 == 0 k & v == 1 & 0 == 0
Good!

k == 1, v == 1:

(k ^ ~v) & k == (1 ^ ~1) & 1 == (1 ^ 0) & 1 == 1 & 1 == 1 k & v == 1 & 1 == 1
Good!

Quoted 19 lines

It also add necessary brackets for them.

---
 .../net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h   | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +1 -1
index 81aa6b0facf5..6a43d1515585 100644
@@ -835,15 +835,10 @@ struct hclge_vf_vlan_cfg {
  * Then for input key(k) and mask(v), we can calculate the value by
  * the formulae:
  *	x = (~k) & v
- *	y = (k ^ ~v) & k
+ *	y = k & v

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h struct hclge_vf_vlan_cfg { · line 841

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +2 -7
  */
-#define calc_x(x, k, v) (x = ~(k) & (v))
-#define calc_y(y, k, v) \
-	do { \
-		const typeof(k) _k_ = (k); \
-		const typeof(v) _v_ = (v); \
-		(y) = (_k_ ^ ~_v_) & (_k_); \
-	} while (0)
+#define calc_x(x, k, v) ((x) = ~(k) & (v))
+#define calc_y(y, k, v) ((y) = (k) & (v))

This also looks good to me.

Quoted 3 lines
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h +0 -0
 #define HCLGE_MAC_STATS_FIELD_OFF(f) (offsetof(struct hclge_mac_stats, f))
 #define HCLGE_STATS_READ(p, offset) (*(u64 *)((u8 *)(p) + (offset)))
author
#16 lore raw

Re: [PATCH net-next 4/4] net: hns3: clear hns unused parameter alarm

In reply to Simon Horman
Quoted 25 lines · Simon Horman
Quoted 8 lines · Hao Lan

From: Peiyang Wang <[email protected]>

Several functions in the hns3 driver have unused parameters. The compiler will warn about them when building with -Wunused-parameter option of hns3.

...

drivers/net/ethernet/hisilicon/hns3/hns3_enet.h static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring) · line 696

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +1 -1
index 1b360aa52e5d..8c0016b359b7 100644
@@ -693,7 +693,7 @@ static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring)
 
 /* iterator for handling rings in ring group */
 #define hns3_for_each_ring(pos, head) \
-	for (pos = (head).ring; (pos); pos = (pos)->next)
+	for ((pos) = (head).ring; (pos); (pos) = (pos)->next)

Hi,

Quoted 4 lines

A minor nit from my side.

This hunk does not seem related to the topic of this patch.

Hi,
Thank you for the review. It's really an oversight. We'll delete this hunk from this patch.

Quoted 7 lines

drivers/net/ethernet/hisilicon/hns3/hns3_enet.h static inline unsigned int hns3_page_order(struct hns3_enet_ring *ring) · line 699

Quoted 3 lines
drivers/net/ethernet/hisilicon/hns3/hns3_enet.h +0 -0
 #define hns3_get_handle(ndev) \
 	(((struct hns3_nic_priv *)netdev_priv(ndev))->ae_handle)

...
.

author
#10 lore raw

Re: [PATCH net-next 3/4] net: hns3: fix strncpy() not using dest-buf length as length issue

In reply to Simon Horman
Quoted 79 lines · Simon Horman
Quoted 65 lines · Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +11 -6
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c static void hns3_dbg_fill_content(char *content, u16 len, · line 466

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +10 -0
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));

Hi,

Quoted 8 lines

The above memcpy() calls share the same property as the warning that is being addressed: the length copied depends on the source not the destination.

With the reworked code this seems safe. Which is good. But I wonder if, given all the checking done, it makes sense to simply call strcpy() here. Using strlen() as a length argument seems odd to me.

Hi,
Thanks for your review.
1. We think the memcpy is correct, our length copied depends on the source, or do I not understand you?
void *memcpy(void *dest, const void *src, size_t count)

2. We don't know any other way to replace strlen. Do you have a better way for us?

Thank you

Quoted 59 lines
Quoted 57 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +3 -1
+		}
+		pos += item_len;
+		len -= item_len;
 	}
-
 	*pos++ = '\n';
 	*pos++ = '\0';
 }
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c +24 -5
index a0b46e7d863e..1354fd0461f7 100644
@@ -88,16 +88,35 @@ static void hclge_dbg_fill_content(char *content, u16 len,
 				   const struct hclge_dbg_item *items,
 				   const char **result, u16 size)
 {
+#define HCLGE_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HCLGE_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
+	len -= HCLGE_DBG_LINE_END_LEN;
+
 	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
-		pos += strlen(items[i].name) + items[i].interval;
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));
+		}
+		pos += item_len;
+		len -= item_len;
 	}
 	*pos++ = '\n';
 	*pos++ = '\0';
Signature · 1 line
2.30.0

.

author
#6 lore raw

Re: [PATCH net-next 2/4] net: hns3: fix hns3 driver header file not self-contained issue

In reply to Simon Horman
Quoted 6 lines · Simon Horman

Hi,

out of curiosity I'm wondering if you could provide some more information on how you generated the warnings that you are addressing here.
.

Hi,
Thanks for your review.
We will attach the warning details in the next patch.

#11 lore raw

Re: [PATCH net-next 3/4] net: hns3: fix strncpy() not using dest-buf length as length issue

In reply to Hao Lan
Quoted 90 lines · Hao Lan
Quoted 79 lines · Simon Horman
Quoted 65 lines · Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +11 -6
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c static void hns3_dbg_fill_content(char *content, u16 len, · line 466

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +10 -0
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));

Hi,

Quoted 8 lines

The above memcpy() calls share the same property as the warning that is being addressed: the length copied depends on the source not the destination.

With the reworked code this seems safe. Which is good. But I wonder if, given all the checking done, it makes sense to simply call strcpy() here. Using strlen() as a length argument seems odd to me.

Quoted 8 lines

Hi,
Thanks for your review.
1. We think the memcpy is correct, our length copied depends on the source, or do I not understand you?
void *memcpy(void *dest, const void *src, size_t count)

2. We don't know any other way to replace strlen. Do you have a better way for us?

My point is that strcpy() could be used here. Because the source is a string, and the length of the copy is the length of the source (string).

f.e., aren't the following functionally equivalent?

	memcpy(pos, result[i], strlen(result[i]));

	strcpy(pos, result[i])

In my view using strcpy here seems a bit simpler and therefore nicer. But if you don't think so, that is fine.

...

#7 lore raw

Re: [PATCH net-next 2/4] net: hns3: fix hns3 driver header file not self-contained issue

In reply to Hao Lan
Quoted 5 lines · Hao Lan
Quoted 2 lines · Simon Horman

Hi,

Quoted 7 lines
Quoted 4 lines

out of curiosity I'm wondering if you could provide some more information on how you generated the warnings that you are addressing here.
.

Hi,
Thanks for your review.
We will attach the warning details in the next patch.

Great, thanks!

author
#12 lore raw

Re: [PATCH net-next 3/4] net: hns3: fix strncpy() not using dest-buf length as length issue

In reply to Simon Horman
Quoted 104 lines · Simon Horman
Quoted 90 lines · Hao Lan
Quoted 79 lines · Simon Horman
Quoted 65 lines · Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +11 -6
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c static void hns3_dbg_fill_content(char *content, u16 len, · line 466

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +10 -0
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));

Hi,

Quoted 8 lines

The above memcpy() calls share the same property as the warning that is being addressed: the length copied depends on the source not the destination.

With the reworked code this seems safe. Which is good. But I wonder if, given all the checking done, it makes sense to simply call strcpy() here. Using strlen() as a length argument seems odd to me.

Quoted 8 lines

Hi,
Thanks for your review.
1. We think the memcpy is correct, our length copied depends on the source, or do I not understand you?
void *memcpy(void *dest, const void *src, size_t count)

2. We don't know any other way to replace strlen. Do you have a better way for us?

My point is that strcpy() could be used here. Because the source is a string, and the length of the copy is the length of the source (string).

Quoted 9 lines

f.e., aren't the following functionally equivalent?

	memcpy(pos, result[i], strlen(result[i]));

	strcpy(pos, result[i])

In my view using strcpy here seems a bit simpler and therefore nicer. But if you don't think so, that is fine.

Hi,
Thanks for your review.
Here is a warning report about using strcpy. This patch is used to fix the warning.

All warnings (new ones prefixed by >>):

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_filter_config.constprop' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2080:2:
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_filter_config.constprop' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2102:3:

Quoted 1 line
Quoted 1 line

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_mac_list' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1872:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_mac_list' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1887:4:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_pg' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:735:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_pg' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:775:3:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_qset' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1027:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_qset' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1059:3:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_vlan_offload_config' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2123:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_offload_config' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2154:3:

Quoted 1 line
Quoted 1 line

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 3 lines

...
.

#13 lore raw

Re: [PATCH net-next 3/4] net: hns3: fix strncpy() not using dest-buf length as length issue

Quoted 113 lines · Hao Lan
Quoted 110 lines · Simon Horman
Quoted 90 lines · Hao Lan
Quoted 79 lines · Simon Horman
Quoted 65 lines · Hao Lan

From: Hao Chen <[email protected]>

Now, strncpy() in hns3_dbg_fill_content() use src-length as copy-length, it may result in dest-buf overflow.

This patch is to fix intel compile warning for csky-linux-gcc (GCC) 12.1.0 compiler.

The warning reports as below:

hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

strncpy(pos, items[i].name, strlen(items[i].name));

hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

strncpy(pos, result[i], strlen(result[i]));

strncpy() use src-length as copy-length, it may result in dest-buf overflow.

So,this patch add some values check to avoid this issue.

---
 .../ethernet/hisilicon/hns3/hns3_debugfs.c    | 31 ++++++++++++++-----
 .../hisilicon/hns3/hns3pf/hclge_debugfs.c     | 29 ++++++++++++++---
 2 files changed, 48 insertions(+), 12 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +11 -6
index 4c3e90a1c4d0..cf415cb37685 100644
@@ -438,19 +438,36 @@ static void hns3_dbg_fill_content(char *content, u16 len,
 				  const struct hns3_dbg_item *items,
 				  const char **result, u16 size)
 {
+#define HNS3_DBG_LINE_END_LEN	2
 	char *pos = content;
+	u16 item_len;
 	u16 i;
 
+	if (!len) {
+		return;
+	} else if (len <= HNS3_DBG_LINE_END_LEN) {
+		*pos++ = '\0';
+		return;
+	}
+
 	memset(content, ' ', len);
-	for (i = 0; i < size; i++) {
-		if (result)
-			strncpy(pos, result[i], strlen(result[i]));
-		else
-			strncpy(pos, items[i].name, strlen(items[i].name));
+	len -= HNS3_DBG_LINE_END_LEN;
 
-		pos += strlen(items[i].name) + items[i].interval;
+	for (i = 0; i < size; i++) {

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c static void hns3_dbg_fill_content(char *content, u16 len, · line 466

Quoted 10 lines
drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c +10 -0
+		item_len = strlen(items[i].name) + items[i].interval;
+		if (len < item_len)
+			break;
+
+		if (result) {
+			if (item_len < strlen(result[i]))
+				break;
+			memcpy(pos, result[i], strlen(result[i]));
+		} else {
+			memcpy(pos, items[i].name, strlen(items[i].name));

Hi,

Quoted 8 lines

The above memcpy() calls share the same property as the warning that is being addressed: the length copied depends on the source not the destination.

With the reworked code this seems safe. Which is good. But I wonder if, given all the checking done, it makes sense to simply call strcpy() here. Using strlen() as a length argument seems odd to me.

Quoted 8 lines

Hi,
Thanks for your review.
1. We think the memcpy is correct, our length copied depends on the source, or do I not understand you?
void *memcpy(void *dest, const void *src, size_t count)

2. We don't know any other way to replace strlen. Do you have a better way for us?

My point is that strcpy() could be used here. Because the source is a string, and the length of the copy is the length of the source (string).

f.e., aren't the following functionally equivalent?

	memcpy(pos, result[i], strlen(result[i]));

	strcpy(pos, result[i])
Quoted 7 lines
Quoted 3 lines

In my view using strcpy here seems a bit simpler and therefore nicer. But if you don't think so, that is fine.

Hi,
Thanks for your review.
Here is a warning report about using strcpy. This patch is used to fix the warning.

Thanks.

Just to clarify, I was suggesting strcpy() rather than strncpy(). But I didn't try my suggestion.

In any case, all these ideas, including memcpy() have the underlying issue that is being flagged below: that the length of the copy is based on the source not the destination.

As I said earlier, I agree that your changes make this safe, regardless of what function is used to actually copy the data. And my suggestion is cosmetic rather than functional.

Quoted 56 lines

All warnings (new ones prefixed by >>):

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_filter_config.constprop' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2080:2:
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]

      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_filter_config.constprop' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2102:3:

Quoted 1 line
Quoted 1 line

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_mac_list' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1872:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_mac_list' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1887:4:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_pg' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:735:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_pg' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:775:3:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_qset' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1027:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_tm_qset' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:1059:3:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'hclge_dbg_fill_content',
       inlined from 'hclge_dbg_dump_vlan_offload_config' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2123:2:
   drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:92:25: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
      92 |                         strncpy(pos, items[i].name, strlen(items[i].name));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In function 'hclge_dbg_fill_content',
inlined from 'hclge_dbg_dump_vlan_offload_config' at drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:2154:3:

Quoted 1 line
Quoted 1 line

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

      90 |                         strncpy(pos, result[i], strlen(result[i]));
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 3 lines

...
.