Skip to contentSkip to conversation

net: mana: Allow variable size indirection table

PATCH Superseded Shradha Gupta 3 participants 9 messages May 31, 2024 – Jun 11, 2024

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 net: mana: Allow variable size indirection table

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

---
Changes in v3:
* Fixed the memory leak(save_table) in mana_set_rxfh()

Changes in v2:
* Rebased to latest net-next tree
* Rearranged cleanup code in mana_probe_port to avoid extra operations

---
 drivers/infiniband/hw/mana/qp.c               | 10 +--
 drivers/net/ethernet/microsoft/mana/mana_en.c | 68 ++++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 27 +++++---
 include/net/mana/gdma.h                       |  4 +-
 include/net/mana/mana.h                       |  9 +--
 5 files changed, 89 insertions(+), 29 deletions(-)
drivers/infiniband/hw/mana/qp.c +5 -5
index ba13c5abf8ef..2d411a16a127 100644
@@ -21,7 +21,7 @@ static int mana_ib_cfg_vport_steering(struct mana_ib_dev *dev,
 
 	gc = mdev_to_gc(dev);
 
-	req_buf_size = struct_size(req, indir_tab, MANA_INDIRECT_TABLE_SIZE);
+	req_buf_size = struct_size(req, indir_tab, MANA_INDIRECT_TABLE_DEF_SIZE);
 	req = kzalloc(req_buf_size, GFP_KERNEL);
 	if (!req)
 		return -ENOMEM;
@@ -41,18 +41,18 @@ static int mana_ib_cfg_vport_steering(struct mana_ib_dev *dev,
 	if (log_ind_tbl_size)
 		req->rss_enable = true;
 
-	req->num_indir_entries = MANA_INDIRECT_TABLE_SIZE;
+	req->num_indir_entries = MANA_INDIRECT_TABLE_DEF_SIZE;
 	req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2,
 					 indir_tab);
 	req->update_indir_tab = true;
 	req->cqe_coalescing_enable = 1;
 
 	/* The ind table passed to the hardware must have
-	 * MANA_INDIRECT_TABLE_SIZE entries. Adjust the verb
+	 * MANA_INDIRECT_TABLE_DEF_SIZE entries. Adjust the verb
 	 * ind_table to MANA_INDIRECT_TABLE_SIZE if required
 	 */
 	ibdev_dbg(&dev->ib_dev, "ind table size %u\n", 1 << log_ind_tbl_size);
-	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
+	for (i = 0; i < MANA_INDIRECT_TABLE_DEF_SIZE; i++) {
 		req->indir_tab[i] = ind_table[i % (1 << log_ind_tbl_size)];
 		ibdev_dbg(&dev->ib_dev, "index %u handle 0x%llx\n", i,
 			  req->indir_tab[i]);
@@ -137,7 +137,7 @@ static int mana_ib_create_qp_rss(struct ib_qp *ibqp, struct ib_pd *pd,
 	}
 
 	ind_tbl_size = 1 << ind_tbl->log_ind_tbl_size;
-	if (ind_tbl_size > MANA_INDIRECT_TABLE_SIZE) {
+	if (ind_tbl_size > MANA_INDIRECT_TABLE_DEF_SIZE) {
 		ibdev_dbg(&mdev->ib_dev,
 			  "Indirect table size %d exceeding limit\n",
 			  ind_tbl_size);
drivers/net/ethernet/microsoft/mana/mana_en.c +57 -11

166 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +19 -8
index ab2413d71f6c..146d5db1792f 100644
@@ -245,7 +245,9 @@ static u32 mana_get_rxfh_key_size(struct net_device *ndev)
 
 static u32 mana_rss_indir_size(struct net_device *ndev)
 {
-	return MANA_INDIRECT_TABLE_SIZE;
+	struct mana_port_context *apc = netdev_priv(ndev);
+
+	return apc->indir_table_sz;
 }
 
 static int mana_get_rxfh(struct net_device *ndev,
@@ -257,7 +259,7 @@ static int mana_get_rxfh(struct net_device *ndev,
 	rxfh->hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
 
 	if (rxfh->indir) {
-		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+		for (i = 0; i < apc->indir_table_sz; i++)
 			rxfh->indir[i] = apc->indir_table[i];
 	}
 
@@ -273,8 +275,8 @@ static int mana_set_rxfh(struct net_device *ndev,
 {
 	struct mana_port_context *apc = netdev_priv(ndev);
 	bool update_hash = false, update_table = false;
-	u32 save_table[MANA_INDIRECT_TABLE_SIZE];
 	u8 save_key[MANA_HASH_KEY_SIZE];
+	u32 *save_table;
 	int i, err;
 
 	if (!apc->port_is_up)
@@ -284,13 +286,19 @@ static int mana_set_rxfh(struct net_device *ndev,
 	    rxfh->hfunc != ETH_RSS_HASH_TOP)
 		return -EOPNOTSUPP;
 
+	save_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
+	if (!save_table)
+		return -ENOMEM;
+
 	if (rxfh->indir) {
-		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
-			if (rxfh->indir[i] >= apc->num_queues)
-				return -EINVAL;
+		for (i = 0; i < apc->indir_table_sz; i++)
+			if (rxfh->indir[i] >= apc->num_queues) {
+				err = -EINVAL;
+				goto cleanup;
+			}
 
 		update_table = true;
-		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
+		for (i = 0; i < apc->indir_table_sz; i++) {
 			save_table[i] = apc->indir_table[i];
 			apc->indir_table[i] = rxfh->indir[i];
 		}
@@ -306,7 +314,7 @@ static int mana_set_rxfh(struct net_device *ndev,
 
 	if (err) { /* recover to original values */
 		if (update_table) {
-			for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+			for (i = 0; i < apc->indir_table_sz; i++)
 				apc->indir_table[i] = save_table[i];
 		}
 
@@ -316,6 +324,9 @@ static int mana_set_rxfh(struct net_device *ndev,
 		mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
 	}
 
+cleanup:
+	kfree(save_table);
+
 	return err;
 }
 
include/net/mana/gdma.h +3 -1
index 27684135bb4d..c547756c4284 100644
@@ -543,11 +543,13 @@ enum {
  */
 #define GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX BIT(2)
 #define GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG BIT(3)
+#define GDMA_DRV_CAP_FLAG_1_VARIABLE_INDIRECTION_TABLE_SUPPORT BIT(5)
 
 #define GDMA_DRV_CAP_FLAGS1 \
 	(GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT | \
 	 GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX | \
-	 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG)
+	 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG | \
+	 GDMA_DRV_CAP_FLAG_1_VARIABLE_INDIRECTION_TABLE_SUPPORT)
 
 #define GDMA_DRV_CAP_FLAGS2 0
 
include/net/mana/mana.h +5 -4
index 561f6719fb4e..59823901b74f 100644
@@ -30,8 +30,8 @@ enum TRI_STATE {
 };
 
 /* Number of entries for hardware indirection table must be in power of 2 */
-#define MANA_INDIRECT_TABLE_SIZE 64
-#define MANA_INDIRECT_TABLE_MASK (MANA_INDIRECT_TABLE_SIZE - 1)
+#define MANA_INDIRECT_TABLE_MAX_SIZE 512
+#define MANA_INDIRECT_TABLE_DEF_SIZE 64
 
 /* The Toeplitz hash key's length in bytes: should be multiple of 8 */
 #define MANA_HASH_KEY_SIZE 40
@@ -410,10 +410,11 @@ struct mana_port_context {
 	struct mana_tx_qp *tx_qp;
 
 	/* Indirection Table for RX & TX. The values are queue indexes */
-	u32 indir_table[MANA_INDIRECT_TABLE_SIZE];
+	u32 *indir_table;
+	u32 indir_table_sz;
 
 	/* Indirection table containing RxObject Handles */
-	mana_handle_t rxobj_table[MANA_INDIRECT_TABLE_SIZE];
+	mana_handle_t *rxobj_table;
 
 	/*  Hash key used by the NIC */
 	u8 hashkey[MANA_HASH_KEY_SIZE];
Signature · 1 line
2.34.1
#1 lore raw
Quoted 22 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

---
Changes in v3:
* Fixed the memory leak(save_table) in mana_set_rxfh()

Changes in v2:
* Rebased to latest net-next tree
* Rearranged cleanup code in mana_probe_port to avoid extra operations

---
 drivers/infiniband/hw/mana/qp.c               | 10 +--
 drivers/net/ethernet/microsoft/mana/mana_en.c | 68 ++++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 27 +++++---
 include/net/mana/gdma.h                       |  4 +-
 include/net/mana/mana.h                       |  9 +--
 5 files changed, 89 insertions(+), 29 deletions(-)

<...>

Quoted 11 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;
@@ -2897,6 +2936,7 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 {

<...>

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

Why do you need to NULLify here? Will apc is going to be accessible after call to mana_remove? or port probe failure?

Thanks

#2 lore raw
Quoted 43 lines · Leon Romanovsky
Quoted 22 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

---
Changes in v3:
* Fixed the memory leak(save_table) in mana_set_rxfh()

Changes in v2:
* Rebased to latest net-next tree
* Rearranged cleanup code in mana_probe_port to avoid extra operations

---
 drivers/infiniband/hw/mana/qp.c               | 10 +--
 drivers/net/ethernet/microsoft/mana/mana_en.c | 68 ++++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 27 +++++---
 include/net/mana/gdma.h                       |  4 +-
 include/net/mana/mana.h                       |  9 +--
 5 files changed, 89 insertions(+), 29 deletions(-)

<...>

Quoted 11 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;
@@ -2897,6 +2936,7 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 {

<...>

Quoted 3 lines
(quoted diff) +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 9 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

Why do you need to NULLify here? Will apc is going to be accessible after call to mana_remove? or port probe failure?

Right, they won't be accessed. This is just for the sake of completeness and to prevent double free in case there are code bug in other place.

Regards,
Shradha.

Quoted 2 lines

Thanks

#3 lore raw
Quoted 53 lines · Shradha Gupta
Quoted 43 lines · Leon Romanovsky
Quoted 22 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

---
Changes in v3:
* Fixed the memory leak(save_table) in mana_set_rxfh()

Changes in v2:
* Rebased to latest net-next tree
* Rearranged cleanup code in mana_probe_port to avoid extra operations

---
 drivers/infiniband/hw/mana/qp.c               | 10 +--
 drivers/net/ethernet/microsoft/mana/mana_en.c | 68 ++++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 27 +++++---
 include/net/mana/gdma.h                       |  4 +-
 include/net/mana/mana.h                       |  9 +--
 5 files changed, 89 insertions(+), 29 deletions(-)

<...>

Quoted 11 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;
@@ -2897,6 +2936,7 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 {

<...>

Quoted 3 lines
(quoted diff) +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 9 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

Why do you need to NULLify here? Will apc is going to be accessible after call to mana_remove? or port probe failure?

Quoted 2 lines

Right, they won't be accessed. This is just for the sake of completeness and to prevent double free in case there are code bug in other place.

This coding patter is called defensive programming, which is discouraged in the kernel. You are not preventing double free, but hiding bugs which were possible to be found by various static analysis tools.

Please don't do it.

Thanks

Quoted 5 lines

Regards,
Shradha.

Quoted 2 lines

Thanks

#4 lore raw
Quoted 57 lines · Leon Romanovsky
Quoted 53 lines · Shradha Gupta
Quoted 43 lines · Leon Romanovsky
Quoted 22 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

---
Changes in v3:
* Fixed the memory leak(save_table) in mana_set_rxfh()

Changes in v2:
* Rebased to latest net-next tree
* Rearranged cleanup code in mana_probe_port to avoid extra operations

---
 drivers/infiniband/hw/mana/qp.c               | 10 +--
 drivers/net/ethernet/microsoft/mana/mana_en.c | 68 ++++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 27 +++++---
 include/net/mana/gdma.h                       |  4 +-
 include/net/mana/mana.h                       |  9 +--
 5 files changed, 89 insertions(+), 29 deletions(-)

<...>

Quoted 11 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;
@@ -2897,6 +2936,7 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 {

<...>

Quoted 3 lines
(quoted diff) +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 9 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

Why do you need to NULLify here? Will apc is going to be accessible after call to mana_remove? or port probe failure?

Quoted 2 lines

Right, they won't be accessed. This is just for the sake of completeness and to prevent double free in case there are code bug in other place.

Quoted 7 lines

This coding patter is called defensive programming, which is discouraged in the kernel. You are not preventing double free, but hiding bugs which were possible to be found by various static analysis tools.

Please don't do it.

Thanks

Understood, it makes sense. Let me fix this in the next version.

Regards,
Shradha

Quoted 6 lines
Quoted 6 lines

Regards,
Shradha.

Quoted 2 lines

Thanks

reviewer
#5 lore raw
In reply to Shradha Gupta
Quoted 8 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

...

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
@@ -2344,11 +2352,33 @@ static int mana_create_vport(struct mana_port_context *apc,
 	return mana_create_txq(apc, net);
 }
 
+static int mana_rss_table_alloc(struct mana_port_context *apc)
+{
+	if (!apc->indir_table_sz) {
+		netdev_err(apc->ndev,
+			   "Indirection table size not set for vPort %d\n",
+			   apc->port_idx);

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_create_vport(struct mana_port_context *apc, · line 2370

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+		return -EINVAL;
+	}
+
+	apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
+	if (!apc->indir_table)
+		return -ENOMEM;
+
+	apc->rxobj_table = kcalloc(apc->indir_table_sz, sizeof(mana_handle_t), GFP_KERNEL);
+	if (!apc->rxobj_table) {
+		kfree(apc->indir_table);

Hi, Shradha

Perhaps I am on the wrong track here, but I have some concerns about clean-up paths.

Firstly. I think that apc->indir_table should be to NULL here for consistency with other clean-up paths. Or alternatively, fields of apc should not set to NULL elsewhere after being freed.

In looking into this I noticed that mana_probe() does not call mana_remove() or return an error in the cases where mana_probe_port() or mana_attach() fail unless add_adev also fails. If so, is that intentional?

In any case, I would suggest as a follow-up, arranging things so that when an error occurs in a function, anything that was allocated is unwound before returning an error.

I think this would make allocation/deallocation easier to reason with. And I suspect it would avoid both the need for fields of structures to be zeroed after being freed, and the need to call mana_remove() from mana_probe().

Quoted 15 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -1
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 static void mana_rss_table_init(struct mana_port_context *apc)
 {
 	int i;
 
-	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+	for (i = 0; i < apc->indir_table_sz; i++)
 		apc->indir_table[i] =
 			ethtool_rxfh_indir_default(i, apc->num_queues);
 }

...

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
@@ -2739,11 +2772,17 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
 	err = register_netdev(ndev);
 	if (err) {
 		netdev_err(ndev, "Unable to register netdev.\n");
-		goto reset_apc;
+		goto free_indir;
 	}
 
 	return 0;

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_probe_port(struct mana_context *ac, int port_idx, · line 2788

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
 
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;

nit: Not strictly related to this patch, but the reset_apc code should
probably be a call to mana_cleanup_port_context() as it is the dual of mana_init_port_context() which is called earlier in mana_probe_port()

...

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

The code to free and zero indir_table_sz and indir_table appears twice in this patch. Perhaps a helper to do this, which would be the dual of mana_rss_table_alloc is in order.

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
 		rtnl_unlock();
 

...

#6 lore raw
Quoted 39 lines · Simon Horman
Quoted 8 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

...

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
@@ -2344,11 +2352,33 @@ static int mana_create_vport(struct mana_port_context *apc,
 	return mana_create_txq(apc, net);
 }
 
+static int mana_rss_table_alloc(struct mana_port_context *apc)
+{
+	if (!apc->indir_table_sz) {
+		netdev_err(apc->ndev,
+			   "Indirection table size not set for vPort %d\n",
+			   apc->port_idx);

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_create_vport(struct mana_port_context *apc, · line 2370

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+		return -EINVAL;
+	}
+
+	apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
+	if (!apc->indir_table)
+		return -ENOMEM;
+
+	apc->rxobj_table = kcalloc(apc->indir_table_sz, sizeof(mana_handle_t), GFP_KERNEL);
+	if (!apc->rxobj_table) {
+		kfree(apc->indir_table);

Hi, Shradha

Quoted 6 lines

Perhaps I am on the wrong track here, but I have some concerns about clean-up paths.

Firstly. I think that apc->indir_table should be to NULL here for consistency with other clean-up paths. Or alternatively, fields of apc should not set to NULL elsewhere after being freed.

Hi Simon,

Thanks for the comments. This makes sense, I am planning of consistently removing the NULLify from other places too as per Leon's comments.

Quoted 4 lines

In looking into this I noticed that mana_probe() does not call mana_remove() or return an error in the cases where mana_probe_port() or mana_attach() fail unless add_adev also fails. If so, is that intentional?

Right, so most calls like mana_probe_port(), mana_attach() cleanup after themselves in the code if there is any error. So, not having to call mana_remove() in these cases in mana_probe() is intentional. But I do agree that an error is returned in mana_probe() only if add_adev also fails. I'll fix that too in the next version

Quoted 9 lines

In any case, I would suggest as a follow-up, arranging things so that when an error occurs in a function, anything that was allocated is unwound before returning an error.

I think this would make allocation/deallocation easier to reason with. And I suspect it would avoid both the need for fields of structures to be zeroed after being freed, and the need to call mana_remove() from mana_probe().

Agreed

Quoted 29 lines
Quoted 16 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -1
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 static void mana_rss_table_init(struct mana_port_context *apc)
 {
 	int i;
 
-	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+	for (i = 0; i < apc->indir_table_sz; i++)
 		apc->indir_table[i] =
 			ethtool_rxfh_indir_default(i, apc->num_queues);
 }

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
@@ -2739,11 +2772,17 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
 	err = register_netdev(ndev);
 	if (err) {
 		netdev_err(ndev, "Unable to register netdev.\n");
-		goto reset_apc;
+		goto free_indir;
 	}
 
 	return 0;
 
Quoted 13 lines

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_probe_port(struct mana_context *ac, int port_idx, · line 2788

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;

nit: Not strictly related to this patch, but the reset_apc code should
probably be a call to mana_cleanup_port_context() as it is the dual of mana_init_port_context() which is called earlier in mana_probe_port()

Sure, let me do that too.

Quoted 6 lines

...

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 10 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

The code to free and zero indir_table_sz and indir_table appears twice in this patch. Perhaps a helper to do this, which would be the dual of mana_rss_table_alloc is in order.

Makes sense, will change this too.

Thanks,
Shradha.

Quoted 6 lines
Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
 		rtnl_unlock();
 

...

reviewer
#7 lore raw
Quoted 43 lines · Shradha Gupta
Quoted 42 lines · Simon Horman
Quoted 8 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

...

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
@@ -2344,11 +2352,33 @@ static int mana_create_vport(struct mana_port_context *apc,
 	return mana_create_txq(apc, net);
 }
 
+static int mana_rss_table_alloc(struct mana_port_context *apc)
+{
+	if (!apc->indir_table_sz) {
+		netdev_err(apc->ndev,
+			   "Indirection table size not set for vPort %d\n",
+			   apc->port_idx);

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_create_vport(struct mana_port_context *apc, · line 2370

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+		return -EINVAL;
+	}
+
+	apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
+	if (!apc->indir_table)
+		return -ENOMEM;
+
+	apc->rxobj_table = kcalloc(apc->indir_table_sz, sizeof(mana_handle_t), GFP_KERNEL);
+	if (!apc->rxobj_table) {
+		kfree(apc->indir_table);

Hi, Shradha

Perhaps I am on the wrong track here, but I have some concerns about clean-up paths.

Quoted 8 lines
Quoted 3 lines

Firstly. I think that apc->indir_table should be to NULL here for consistency with other clean-up paths. Or alternatively, fields of apc should not set to NULL elsewhere after being freed.

Hi Simon,

Thanks for the comments. This makes sense, I am planning of consistently removing the NULLify from other places too as per Leon's comments.

Great!

Quoted 10 lines
Quoted 4 lines

In looking into this I noticed that mana_probe() does not call mana_remove() or return an error in the cases where mana_probe_port() or mana_attach() fail unless add_adev also fails. If so, is that intentional?

Right, so most calls like mana_probe_port(), mana_attach() cleanup after themselves in the code if there is any error. So, not having to call mana_remove() in these cases in mana_probe() is intentional. But I do agree that an error is returned in mana_probe() only if add_adev also fails. I'll fix that too in the next version

I'm not entirely sure, but perhaps that is a candidate for a separate patch.

Quoted 54 lines
Quoted 9 lines

In any case, I would suggest as a follow-up, arranging things so that when an error occurs in a function, anything that was allocated is unwound before returning an error.

I think this would make allocation/deallocation easier to reason with. And I suspect it would avoid both the need for fields of structures to be zeroed after being freed, and the need to call mana_remove() from mana_probe().

Agreed

Quoted 29 lines
Quoted 16 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -1
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 static void mana_rss_table_init(struct mana_port_context *apc)
 {
 	int i;
 
-	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+	for (i = 0; i < apc->indir_table_sz; i++)
 		apc->indir_table[i] =
 			ethtool_rxfh_indir_default(i, apc->num_queues);
 }

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
@@ -2739,11 +2772,17 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
 	err = register_netdev(ndev);
 	if (err) {
 		netdev_err(ndev, "Unable to register netdev.\n");
-		goto reset_apc;
+		goto free_indir;
 	}
 
 	return 0;
 
Quoted 13 lines

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_probe_port(struct mana_context *ac, int port_idx, · line 2788

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;

nit: Not strictly related to this patch, but the reset_apc code should
probably be a call to mana_cleanup_port_context() as it is the dual of mana_init_port_context() which is called earlier in mana_probe_port()

Quoted 1 line

Sure, let me do that too.

FWIIW, I think it would be appropriate to put that change in a separate patch.

Quoted 16 lines
Quoted 6 lines

...

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 10 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

The code to free and zero indir_table_sz and indir_table appears twice in this patch. Perhaps a helper to do this, which would be the dual of mana_rss_table_alloc is in order.

Quoted 1 line

Makes sense, will change this too.

Thanks.

#8 lore raw
Quoted 122 lines · Simon Horman
Quoted 43 lines · Shradha Gupta
Quoted 42 lines · Simon Horman
Quoted 8 lines · Shradha Gupta

Allow variable size indirection table allocation in MANA instead of using a constant value MANA_INDIRECT_TABLE_SIZE. The size is now derived from the MANA_QUERY_VPORT_CONFIG and the indirection table is allocated dynamically.

...

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

...

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
@@ -2344,11 +2352,33 @@ static int mana_create_vport(struct mana_port_context *apc,
 	return mana_create_txq(apc, net);
 }
 
+static int mana_rss_table_alloc(struct mana_port_context *apc)
+{
+	if (!apc->indir_table_sz) {
+		netdev_err(apc->ndev,
+			   "Indirection table size not set for vPort %d\n",
+			   apc->port_idx);

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_create_vport(struct mana_port_context *apc, · line 2370

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+		return -EINVAL;
+	}
+
+	apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
+	if (!apc->indir_table)
+		return -ENOMEM;
+
+	apc->rxobj_table = kcalloc(apc->indir_table_sz, sizeof(mana_handle_t), GFP_KERNEL);
+	if (!apc->rxobj_table) {
+		kfree(apc->indir_table);

Hi, Shradha

Perhaps I am on the wrong track here, but I have some concerns about clean-up paths.

Quoted 8 lines
Quoted 3 lines

Firstly. I think that apc->indir_table should be to NULL here for consistency with other clean-up paths. Or alternatively, fields of apc should not set to NULL elsewhere after being freed.

Hi Simon,

Thanks for the comments. This makes sense, I am planning of consistently removing the NULLify from other places too as per Leon's comments.

Great!

Quoted 10 lines
Quoted 4 lines

In looking into this I noticed that mana_probe() does not call mana_remove() or return an error in the cases where mana_probe_port() or mana_attach() fail unless add_adev also fails. If so, is that intentional?

Right, so most calls like mana_probe_port(), mana_attach() cleanup after themselves in the code if there is any error. So, not having to call mana_remove() in these cases in mana_probe() is intentional. But I do agree that an error is returned in mana_probe() only if add_adev also fails. I'll fix that too in the next version

I'm not entirely sure, but perhaps that is a candidate for a separate patch.

Quoted 54 lines
Quoted 9 lines

In any case, I would suggest as a follow-up, arranging things so that when an error occurs in a function, anything that was allocated is unwound before returning an error.

I think this would make allocation/deallocation easier to reason with. And I suspect it would avoid both the need for fields of structures to be zeroed after being freed, and the need to call mana_remove() from mana_probe().

Agreed

Quoted 42 lines
Quoted 16 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -1
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 static void mana_rss_table_init(struct mana_port_context *apc)
 {
 	int i;
 
-	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
+	for (i = 0; i < apc->indir_table_sz; i++)
 		apc->indir_table[i] =
 			ethtool_rxfh_indir_default(i, apc->num_queues);
 }

...

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
@@ -2739,11 +2772,17 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
 	err = register_netdev(ndev);
 	if (err) {
 		netdev_err(ndev, "Unable to register netdev.\n");
-		goto reset_apc;
+		goto free_indir;
 	}
 
 	return 0;

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_probe_port(struct mana_context *ac, int port_idx, · line 2788

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
 
+free_indir:
+	apc->indir_table_sz = 0;
+	kfree(apc->indir_table);
+	apc->indir_table = NULL;
+	kfree(apc->rxobj_table);
+	apc->rxobj_table = NULL;
 reset_apc:
 	kfree(apc->rxqs);
 	apc->rxqs = NULL;

nit: Not strictly related to this patch, but the reset_apc code should
probably be a call to mana_cleanup_port_context() as it is the dual of mana_init_port_context() which is called earlier in mana_probe_port()

Quoted 3 lines
Quoted 1 line

Sure, let me do that too.

FWIIW, I think it would be appropriate to put that change in a separate patch.

Fixing this and other similar changes in a different patch. Thanks

Quoted 20 lines
Quoted 17 lines
Quoted 7 lines

...

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
@@ -2931,6 +2972,11 @@ void mana_remove(struct gdma_dev *gd, bool suspending)
 		}
 
Quoted 10 lines

drivers/net/ethernet/microsoft/mana/mana_en.c void mana_remove(struct gdma_dev *gd, bool suspending) · line 2979

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +5 -0
 		unregister_netdevice(ndev);
+		apc->indir_table_sz = 0;
+		kfree(apc->indir_table);
+		apc->indir_table = NULL;
+		kfree(apc->rxobj_table);
+		apc->rxobj_table = NULL;

The code to free and zero indir_table_sz and indir_table appears twice in this patch. Perhaps a helper to do this, which would be the dual of mana_rss_table_alloc is in order.

Quoted 1 line

Makes sense, will change this too.

Thanks.