Skip to content

Commit 60877a3

Browse files
edumazetdavem330
authored andcommitted
net: allow large number of tx queues
netif_alloc_netdev_queues() uses kcalloc() to allocate memory for the "struct netdev_queue *_tx" array. For large number of tx queues, kcalloc() might fail, so this patch does a fallback to vzalloc(). As vmalloc() adds overhead on a critical network path, add __GFP_REPEAT to kzalloc() flags to do this fallback only when really needed. Signed-off-by: Eric Dumazet <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b0b02c7 commit 60877a3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

net/core/dev.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
#include <linux/cpu_rmap.h>
131131
#include <linux/static_key.h>
132132
#include <linux/hashtable.h>
133+
#include <linux/vmalloc.h>
133134

134135
#include "net-sysfs.h"
135136

@@ -5253,17 +5254,28 @@ static void netdev_init_one_queue(struct net_device *dev,
52535254
#endif
52545255
}
52555256

5257+
static void netif_free_tx_queues(struct net_device *dev)
5258+
{
5259+
if (is_vmalloc_addr(dev->_tx))
5260+
vfree(dev->_tx);
5261+
else
5262+
kfree(dev->_tx);
5263+
}
5264+
52565265
static int netif_alloc_netdev_queues(struct net_device *dev)
52575266
{
52585267
unsigned int count = dev->num_tx_queues;
52595268
struct netdev_queue *tx;
5269+
size_t sz = count * sizeof(*tx);
52605270

5261-
BUG_ON(count < 1);
5262-
5263-
tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
5264-
if (!tx)
5265-
return -ENOMEM;
5271+
BUG_ON(count < 1 || count > 0xffff);
52665272

5273+
tx = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
5274+
if (!tx) {
5275+
tx = vzalloc(sz);
5276+
if (!tx)
5277+
return -ENOMEM;
5278+
}
52675279
dev->_tx = tx;
52685280

52695281
netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
@@ -5811,7 +5823,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
58115823

58125824
free_pcpu:
58135825
free_percpu(dev->pcpu_refcnt);
5814-
kfree(dev->_tx);
5826+
netif_free_tx_queues(dev);
58155827
#ifdef CONFIG_RPS
58165828
kfree(dev->_rx);
58175829
#endif
@@ -5836,7 +5848,7 @@ void free_netdev(struct net_device *dev)
58365848

58375849
release_net(dev_net(dev));
58385850

5839-
kfree(dev->_tx);
5851+
netif_free_tx_queues(dev);
58405852
#ifdef CONFIG_RPS
58415853
kfree(dev->_rx);
58425854
#endif

0 commit comments

Comments
 (0)