hv_netvsc: Allocate rx indirection table size dynamically
PATCH v2 1/1 hv_netvsc: Allocate rx indirection table size dynamically
Allocate the size of rx indirection table dynamically in netvsc from the value of size provided by OID_GEN_RECEIVE_SCALE_CAPABILITIES query instead of using a constant value of ITAB_NUM.
---
Changes in v2:
* Added a missing free for rx_table to fix a leak
* Corrected alignment around rx table size query
* Fixed incorrect error handling for rx_table pointer.
--- drivers/net/hyperv/hyperv_net.h | 5 ++++- drivers/net/hyperv/netvsc_drv.c | 18 ++++++++++++++---- drivers/net/hyperv/rndis_filter.c | 22 ++++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-)
drivers/net/hyperv/hyperv_net.h +4 -1
@@ -74,6 +74,7 @@ struct ndis_recv_scale_cap { /* NDIS_RECEIVE_SCALE_CAPABILITIES */#define NDIS_RSS_HASH_SECRET_KEY_MAX_SIZE_REVISION_2 40#define ITAB_NUM 128 +#define ITAB_NUM_MAX 256struct ndis_recv_scale_param {/* NDIS_RECEIVE_SCALE_PARAMETERS */ struct ndis_obj_header hdr;
@@ -1034,7 +1035,9 @@ struct net_device_context {u32 tx_table[VRSS_SEND_TAB_SIZE ]; -u16 rx_table[ITAB_NUM ]; +u16 *rx_table; + +int rx_table_sz;/* Ethtool settings */ u8 duplex;
drivers/net/hyperv/netvsc_drv.c +14 -4
@@ -1040,6 +1040,13 @@ static int netvsc_detach(struct net_device *ndev,rndis_filter_device_remove (hdev, nvdev); +/* +* Free the rx indirection table and reset the table size to 0. +* With the netvsc_attach call it will get allocated again. +*/ + ndev_ctx->rx_table_sz =0 ; +kfree (ndev_ctx->rx_table); +return 0 ; }
@@ -1747,7 +1754,9 @@ static u32 netvsc_get_rxfh_key_size(struct net_device *dev)static u32 netvsc_rss_indir_size (struct net_device *dev) { -return ITAB_NUM ; +struct net_device_context *ndc =netdev_priv (dev); + +return ndc->rx_table_sz; } static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
@@ -1766,7 +1775,7 @@ static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, rndis_dev = ndev->extension;if (indir) { -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) indir[i] = ndc->rx_table[i]; }
@@ -1792,11 +1801,11 @@ static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, rndis_dev = ndev->extension;if (indir) { -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++)if (indir[i] >= ndev->num_chn)return -EINVAL ; -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) ndc->rx_table[i] = indir[i]; }
@@ -2638,6 +2647,7 @@ static void netvsc_remove(struct hv_device *dev)hv_set_drvdata (dev,NULL ); +kfree (ndev_ctx->rx_table);free_percpu (ndev_ctx->vf_stats);free_netdev (net); }
drivers/net/hyperv/rndis_filter.c +18 -4
@@ -927,7 +927,7 @@ static int rndis_set_rss_param_msg(struct rndis_device *rdev,struct rndis_set_request *set;struct rndis_set_complete *set_complete;u32 extlen =sizeof (struct ndis_recv_scale_param ) + -4 *ITAB_NUM +NETVSC_HASH_KEYLEN ; +4 * ndc->rx_table_sz +NETVSC_HASH_KEYLEN ;struct ndis_recv_scale_param *rssp;u32 *itab;u8 *keyp;
@@ -953,7 +953,7 @@ static int rndis_set_rss_param_msg(struct rndis_device *rdev, rssp->hashinfo =NDIS_HASH_FUNC_TOEPLITZ |NDIS_HASH_IPV4 |NDIS_HASH_TCP_IPV4 |NDIS_HASH_IPV6 |NDIS_HASH_TCP_IPV6 ; - rssp->indirect_tabsize =4 *ITAB_NUM ; + rssp->indirect_tabsize =4 * ndc->rx_table_sz; rssp->indirect_taboffset =sizeof (struct ndis_recv_scale_param ); rssp->hashkey_size =NETVSC_HASH_KEYLEN ; rssp->hashkey_offset = rssp->indirect_taboffset +
@@ -961,7 +961,7 @@ static int rndis_set_rss_param_msg(struct rndis_device *rdev,/* Set indirection table entries */ itab = (u32 *)(rssp +1 ); -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) itab[i] = ndc->rx_table[i];/* Set hask key values */
@@ -1548,6 +1548,20 @@ struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,if (ret || rsscap.num_recv_que <2 ) goto out; +if (rsscap.num_indirect_tabent && + rsscap.num_indirect_tabent <=ITAB_NUM_MAX ) + ndc->rx_table_sz = rsscap.num_indirect_tabent; +else + ndc->rx_table_sz =ITAB_NUM ; + + ndc->rx_table =kzalloc (sizeof (u16) * ndc->rx_table_sz, +GFP_KERNEL ); +if (!ndc->rx_table) { +netdev_err (net,"Error in allocating rx indirection table of size %d\n" , + ndc->rx_table_sz); + goto out; + } +/* This guarantees that num_possible_rss_qs <= num_online_cpus */ num_possible_rss_qs =min_t (u32,num_online_cpus (), rsscap.num_recv_que);
@@ -1558,7 +1572,7 @@ struct netvsc_device *rndis_filter_device_add(struct hv_device *dev, net_device->num_chn =min (net_device->max_chn, device_info->num_chn);if (!netif_is_rxfh_configured (net)) { -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) ndc->rx_table[i] =ethtool_rxfh_indir_default ( i, net_device->num_chn); }
Signature · 1 line
2.34.1
drivers/net/hyperv/hyperv_net.h struct net_device_context { · line 1040
Quoted 8 lines · On Wed, 24 May 2023 02:57:10 -0700 Shradha Gupta…
drivers/net/hyperv/hyperv_net.h +3
-1@@ -1034,7 +1035,9 @@ struct net_device_context {u32 tx_table[VRSS_SEND_TAB_SIZE ]; -u16 rx_table[ITAB_NUM ]; +u16 *rx_table; + +int rx_table_sz;
Size should never be negative, use u32 or u16 here?
Haiyang Zhang
maintainerQuoted 64 lines
-----Original Message-----
From: Shradha Gupta <[email protected]>
Sent: Wednesday, May 24, 2023 5:57 AM
To: [email protected]; [email protected]; [email protected]
Cc: Shradha Gupta <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Wei Liu <[email protected]>; Dexuan Cui <[email protected]>; Long Li <[email protected]>; Michael Kelley (LINUX) <[email protected]>; David S. Miller <[email protected]>; Steen Hegelund <[email protected]>; Simon Horman <[email protected]>
Subject: [PATCH v2]hv_netvsc: Allocate rx indirection table size dynamicallyAllocate the size of rx indirection table dynamically in netvsc from the value of size provided by
OID_GEN_RECEIVE_SCALE_CAPABILITIESquery instead of using a constant value ofITAB_NUM.Signed-off-by Shradha Gupta <[email protected]>---
Changes in v2:
* Added a missing free forrx_tableto fix a leak
* Corrected alignment around rx table size query
* Fixed incorrect error handling forrx_tablepointer.--- drivers/net/hyperv/hyperv_net.h | 5 ++++- drivers/net/hyperv/netvsc_drv.c | 18 ++++++++++++++---- drivers/net/hyperv/rndis_filter.c | 22 ++++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-)drivers/net/hyperv/hyperv_net.h +0
-0@@ -74,6 +74,7 @@ struct ndis_recv_scale_cap { /*
NDIS_RECEIVE_SCALE_CAPABILITIES*/
#defineNDIS_RSS_HASH_SECRET_KEY_MAX_SIZE_REVISION_240#define
ITAB_NUM128drivers/net/hyperv/hyperv_net.h +4
-1+#define ITAB_NUM_MAX 256struct ndis_recv_scale_param {/* NDIS_RECEIVE_SCALE_PARAMETERS */ struct ndis_obj_header hdr;@@ -1034,7 +1035,9 @@ struct net_device_context {u32 tx_table[VRSS_SEND_TAB_SIZE ]; -u16 rx_table[ITAB_NUM ]; +u16 *rx_table; + +int rx_table_sz;/* Ethtool settings */ u8 duplex;drivers/net/hyperv/netvsc_drv.c +0
-0
drivers/net/hyperv/netvsc_drv.c static int netvsc_detach(struct net_device *ndev, · line 1049
Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +7
-0@@ -1040,6 +1040,13 @@ static int netvsc_detach(struct net_device *ndev,rndis_filter_device_remove (hdev, nvdev); +/* +* Free the rx indirection table and reset the table size to 0. +* With the netvsc_attach call it will get allocated again. +*/ + ndev_ctx->rx_table_sz =0 ; +kfree (ndev_ctx->rx_table); +
Please move the table free to rndis_filter_device_remove() which is the counter part of rndis_filter_device_add(). So it's automatically called by netvsc_detach/remove() and other error cases.
Also, set ndev_ctx->rx_table = NULL after free to prevent potential double free, or accessing freed memory, etc.
Quoted 29 lines
drivers/net/hyperv/netvsc_drv.c +3
-1return 0 ; }@@ -1747,7 +1754,9 @@ static u32 netvsc_get_rxfh_key_size(struct net_device *dev)static u32 netvsc_rss_indir_size (struct net_device *dev) { -return ITAB_NUM ; +struct net_device_context *ndc =netdev_priv (dev); + +return ndc->rx_table_sz; }static int netvsc_get_rxfh(struct
net_device*dev, u32 *indir, u8 *key,drivers/net/hyperv/netvsc_drv.c +1
-1@@ -1766,7 +1775,7 @@ static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, rndis_dev = ndev->extension;if (indir) { -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) indir[i] = ndc->rx_table[i]; }@@ -1792,11 +1801,11 @@ static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, rndis_dev = ndev->extension;
drivers/net/hyperv/netvsc_drv.c static int netvsc_set_rxfh(struct net_device · line 1811
Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +2
-2if (indir) { -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++)if (indir[i] >= ndev->num_chn)return -EINVAL ; -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) ndc->rx_table[i] = indir[i]; }
Please use the ethtool to change & show the table contents. So these functions are tested:
ethtool -x eth0
ethtool -X eth0 ...
Also, run perf test to ensure no perf regression.
drivers/net/hyperv/netvsc_drv.c static void netvsc_remove(struct hv_device *dev) · line 2650
Quoted 6 lines
drivers/net/hyperv/netvsc_drv.c +1
-0@@ -2638,6 +2647,7 @@ static void netvsc_remove(struct hv_device *dev)hv_set_drvdata (dev,NULL ); +kfree (ndev_ctx->rx_table);
Move the table free to rndis_filter_device_remove() as said earlier.
Quoted 47 lines
drivers/net/hyperv/netvsc_drv.c +0
-0free_percpu (ndev_ctx->vf_stats);free_netdev (net); }drivers/net/hyperv/rndis_filter.c +1
-1@@ -927,7 +927,7 @@ static int rndis_set_rss_param_msg(structrndis_device *rdev, struct rndis_set_request *set;struct rndis_set_complete *set_complete;u32 extlen =sizeof (struct ndis_recv_scale_param ) + -4 *ITAB_NUM +NETVSC_HASH_KEYLEN ; +4 * ndc->rx_table_sz +NETVSC_HASH_KEYLEN ;struct ndis_recv_scale_param *rssp;u32 *itab;drivers/net/hyperv/rndis_filter.c +1
-1u8 *keyp;@@ -953,7 +953,7 @@ static int rndis_set_rss_param_msg(struct rndis_device *rdev, rssp->hashinfo =NDIS_HASH_FUNC_TOEPLITZ |NDIS_HASH_IPV4 |NDIS_HASH_TCP_IPV4 |NDIS_HASH_IPV6 |NDIS_HASH_TCP_IPV6 ; - rssp->indirect_tabsize =4 *ITAB_NUM ; + rssp->indirect_tabsize =4 * ndc->rx_table_sz; rssp->indirect_taboffset =sizeof (struct ndis_recv_scale_param ); rssp->hashkey_size =NETVSC_HASH_KEYLEN ;drivers/net/hyperv/rndis_filter.c +1
-1rssp->hashkey_offset = rssp->indirect_taboffset +@@ -961,7 +961,7 @@ static int rndis_set_rss_param_msg(structrndis_device *rdev,/* Set indirection table entries */ itab = (u32 *)(rssp +1 ); -for (i =0 ; i <ITAB_NUM ; i++) +for (i =0 ; i < ndc->rx_table_sz; i++) itab[i] = ndc->rx_table[i];drivers/net/hyperv/rndis_filter.c +4
-0/* Set hask key values */ @@ -1548,6 +1548,20 @@ struct netvsc_device *rndis_filter_device_add(struct hv_device *dev, if (ret || rsscap.num_recv_que < 2) goto out; + if (rsscap.num_indirect_tabent && + rsscap.num_indirect_tabent <=ITAB_NUM_MAX ) + ndc->rx_table_sz = rsscap.num_indirect_tabent; + else
drivers/net/hyperv/rndis_filter.c struct netvsc_device · line 1565
Quoted 10 lines
drivers/net/hyperv/rndis_filter.c +9
-0+ ndc->rx_table_sz =ITAB_NUM ; + + ndc->rx_table =kzalloc (sizeof (u16) * ndc->rx_table_sz, +GFP_KERNEL ); +if (!ndc->rx_table) { + netdev_err(net, "Error in allocating rx indirection table of size %d\n", + ndc->rx_table_sz); + goto out; + }
When no enough memory for the table, it should:
goto err_dev_remv;
So the device_add fails with an error.
Otherwise, it may continue to run with just one channel. The perf will be low, but the error is not easily visible. That's probably why you didn't find the "if (ndc->rx_table) " condition error in the patch v1.
Thanks,
- Haiyang
Quoted 5 lines · Shradha Gupta
Allocate the size of rx indirection table dynamically in netvsc from the value of size provided by
OID_GEN_RECEIVE_SCALE_CAPABILITIESquery instead of using a constant value ofITAB_NUM.Signed-off-by Shradha Gupta <[email protected]>
Hi Shradha,
thanks for your patch.
Some nits from my side.
And some friendly advice: please consider using checkpatch
...
drivers/net/hyperv/netvsc_drv.c static int netvsc_detach(struct net_device *ndev, · line 1046
Quoted 12 lines
drivers/net/hyperv/netvsc_drv.c +4
-0@@ -1040,6 +1040,13 @@ static int netvsc_detach(struct net_device *ndev,rndis_filter_device_remove (hdev, nvdev); +/* +* Free the rx indirection table and reset the table size to 0. +* With the netvsc_attach call it will get allocated again. +*/
nit: In Networking code multi-line comments look like this:
/* Free the rx indirection table and reset the table size to 0. * With the netvsc_attach call it will get allocated again. */
Quoted 6 lines
drivers/net/hyperv/netvsc_drv.c +3
-0+ ndev_ctx->rx_table_sz =0 ; +kfree (ndev_ctx->rx_table); +return 0 ; }
...
drivers/net/hyperv/netvsc_drv.c struct netvsc_device *rndis_filter_device_add(struct hv_device *dev, · line 1558
Quoted 12 lines
drivers/net/hyperv/netvsc_drv.c +8
-0@@ -1548,6 +1548,20 @@ struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,if (ret || rsscap.num_recv_que <2 ) goto out; +if (rsscap.num_indirect_tabent && + rsscap.num_indirect_tabent <=ITAB_NUM_MAX ) + ndc->rx_table_sz = rsscap.num_indirect_tabent; +else + ndc->rx_table_sz =ITAB_NUM ; + + ndc->rx_table =kzalloc (sizeof (u16) * ndc->rx_table_sz, +GFP_KERNEL );
nit: kcalloc seems appropriate here.
drivers/net/hyperv/netvsc_drv.c struct netvsc_device *rndis_filter_device_add(struct hv_device *dev, · line 1561
Quoted 3 lines
drivers/net/hyperv/netvsc_drv.c +3
-0+if (!ndc->rx_table) { +netdev_err (net,"Error in allocating rx indirection table of size %d\n" , + ndc->rx_table_sz);
nit: No need to log memory allocation errors, the mm core does that.
Also, the alignment of the line above should match of the opening '(' of the preceding line.
f(a,
b);
Quoted 5 lines
drivers/net/hyperv/netvsc_drv.c +3
-0+ goto out; + } +/* This guarantees that num_possible_rss_qs <= num_online_cpus */ num_possible_rss_qs = min_t(u32, num_online_cpus(),