Commit 9ed94117 authored by Guo Zhengkui's avatar Guo Zhengkui Committed by David S. Miller
Browse files

hinic: use ARRAY_SIZE instead of ARRAY_LEN



ARRAY_SIZE defined in <linux/kernel.h> is safer than self-defined
macros to get size of an array such as ARRAY_LEN used here. Because
ARRAY_SIZE uses __must_be_array(arr) to ensure arr is really an array.

Reported-by: default avatarAlejandro Colomar <colomar.6.4.3@gmail.com>
Signed-off-by: default avatarGuo Zhengkui <guozhengkui@vivo.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 16b1c4e0
Loading
Loading
Loading
Loading
+15 −17
Original line number Diff line number Diff line
@@ -1205,8 +1205,6 @@ static u32 hinic_get_rxfh_indir_size(struct net_device *netdev)
	return HINIC_RSS_INDIR_SIZE;
}

#define ARRAY_LEN(arr) ((int)((int)sizeof(arr) / (int)sizeof(arr[0])))

#define HINIC_FUNC_STAT(_stat_item) {	\
	.name = #_stat_item, \
	.size = sizeof_field(struct hinic_vport_stats, _stat_item), \
@@ -1374,7 +1372,7 @@ static void get_drv_queue_stats(struct hinic_dev *nic_dev, u64 *data)
			break;

		hinic_txq_get_stats(&nic_dev->txqs[qid], &txq_stats);
		for (j = 0; j < ARRAY_LEN(hinic_tx_queue_stats); j++, i++) {
		for (j = 0; j < ARRAY_SIZE(hinic_tx_queue_stats); j++, i++) {
			p = (char *)&txq_stats +
				hinic_tx_queue_stats[j].offset;
			data[i] = (hinic_tx_queue_stats[j].size ==
@@ -1387,7 +1385,7 @@ static void get_drv_queue_stats(struct hinic_dev *nic_dev, u64 *data)
			break;

		hinic_rxq_get_stats(&nic_dev->rxqs[qid], &rxq_stats);
		for (j = 0; j < ARRAY_LEN(hinic_rx_queue_stats); j++, i++) {
		for (j = 0; j < ARRAY_SIZE(hinic_rx_queue_stats); j++, i++) {
			p = (char *)&rxq_stats +
				hinic_rx_queue_stats[j].offset;
			data[i] = (hinic_rx_queue_stats[j].size ==
@@ -1411,7 +1409,7 @@ static void hinic_get_ethtool_stats(struct net_device *netdev,
		netif_err(nic_dev, drv, netdev,
			  "Failed to get vport stats from firmware\n");

	for (j = 0; j < ARRAY_LEN(hinic_function_stats); j++, i++) {
	for (j = 0; j < ARRAY_SIZE(hinic_function_stats); j++, i++) {
		p = (char *)&vport_stats + hinic_function_stats[j].offset;
		data[i] = (hinic_function_stats[j].size ==
				sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
@@ -1420,8 +1418,8 @@ static void hinic_get_ethtool_stats(struct net_device *netdev,
	port_stats = kzalloc(sizeof(*port_stats), GFP_KERNEL);
	if (!port_stats) {
		memset(&data[i], 0,
		       ARRAY_LEN(hinic_port_stats) * sizeof(*data));
		i += ARRAY_LEN(hinic_port_stats);
		       ARRAY_SIZE(hinic_port_stats) * sizeof(*data));
		i += ARRAY_SIZE(hinic_port_stats);
		goto get_drv_stats;
	}

@@ -1430,7 +1428,7 @@ static void hinic_get_ethtool_stats(struct net_device *netdev,
		netif_err(nic_dev, drv, netdev,
			  "Failed to get port stats from firmware\n");

	for (j = 0; j < ARRAY_LEN(hinic_port_stats); j++, i++) {
	for (j = 0; j < ARRAY_SIZE(hinic_port_stats); j++, i++) {
		p = (char *)port_stats + hinic_port_stats[j].offset;
		data[i] = (hinic_port_stats[j].size ==
				sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
@@ -1449,14 +1447,14 @@ static int hinic_get_sset_count(struct net_device *netdev, int sset)

	switch (sset) {
	case ETH_SS_TEST:
		return ARRAY_LEN(hinic_test_strings);
		return ARRAY_SIZE(hinic_test_strings);
	case ETH_SS_STATS:
		q_num = nic_dev->num_qps;
		count = ARRAY_LEN(hinic_function_stats) +
			(ARRAY_LEN(hinic_tx_queue_stats) +
			ARRAY_LEN(hinic_rx_queue_stats)) * q_num;
		count = ARRAY_SIZE(hinic_function_stats) +
			(ARRAY_SIZE(hinic_tx_queue_stats) +
			ARRAY_SIZE(hinic_rx_queue_stats)) * q_num;

		count += ARRAY_LEN(hinic_port_stats);
		count += ARRAY_SIZE(hinic_port_stats);

		return count;
	default:
@@ -1476,27 +1474,27 @@ static void hinic_get_strings(struct net_device *netdev,
		memcpy(data, *hinic_test_strings, sizeof(hinic_test_strings));
		return;
	case ETH_SS_STATS:
		for (i = 0; i < ARRAY_LEN(hinic_function_stats); i++) {
		for (i = 0; i < ARRAY_SIZE(hinic_function_stats); i++) {
			memcpy(p, hinic_function_stats[i].name,
			       ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}

		for (i = 0; i < ARRAY_LEN(hinic_port_stats); i++) {
		for (i = 0; i < ARRAY_SIZE(hinic_port_stats); i++) {
			memcpy(p, hinic_port_stats[i].name,
			       ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}

		for (i = 0; i < nic_dev->num_qps; i++) {
			for (j = 0; j < ARRAY_LEN(hinic_tx_queue_stats); j++) {
			for (j = 0; j < ARRAY_SIZE(hinic_tx_queue_stats); j++) {
				sprintf(p, hinic_tx_queue_stats[j].name, i);
				p += ETH_GSTRING_LEN;
			}
		}

		for (i = 0; i < nic_dev->num_qps; i++) {
			for (j = 0; j < ARRAY_LEN(hinic_rx_queue_stats); j++) {
			for (j = 0; j < ARRAY_SIZE(hinic_rx_queue_stats); j++) {
				sprintf(p, hinic_rx_queue_stats[j].name, i);
				p += ETH_GSTRING_LEN;
			}