Skip to content

Commit fd11a83

Browse files
Alexander Duyckdavem330
authored andcommitted
net: Pull out core bits of __netdev_alloc_skb and add __napi_alloc_skb
This change pulls the core functionality out of __netdev_alloc_skb and places them in a new function named __alloc_rx_skb. The reason for doing this is to make these bits accessible to a new function __napi_alloc_skb. In addition __alloc_rx_skb now has a new flags value that is used to determine which page frag pool to allocate from. If the SKB_ALLOC_NAPI flag is set then the NAPI pool is used. The advantage of this is that we do not have to use local_irq_save/restore when accessing the NAPI pool from NAPI context. In my test setup I saw at least 11ns of savings using the napi_alloc_skb function versus the netdev_alloc_skb function, most of this being due to the fact that we didn't have to call local_irq_save/restore. The main use case for napi_alloc_skb would be for things such as copybreak or page fragment based receive paths where an skb is allocated after the data has been received instead of before. Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ffde732 commit fd11a83

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

include/linux/skbuff.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ struct net_device;
151151
struct scatterlist;
152152
struct pipe_inode_info;
153153
struct iov_iter;
154+
struct napi_struct;
154155

155156
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
156157
struct nf_conntrack {
@@ -673,6 +674,7 @@ struct sk_buff {
673674

674675
#define SKB_ALLOC_FCLONE 0x01
675676
#define SKB_ALLOC_RX 0x02
677+
#define SKB_ALLOC_NAPI 0x04
676678

677679
/* Returns true if the skb was allocated from PFMEMALLOC reserves */
678680
static inline bool skb_pfmemalloc(const struct sk_buff *skb)
@@ -2165,6 +2167,13 @@ static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
21652167
}
21662168

21672169
void *napi_alloc_frag(unsigned int fragsz);
2170+
struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
2171+
unsigned int length, gfp_t gfp_mask);
2172+
static inline struct sk_buff *napi_alloc_skb(struct napi_struct *napi,
2173+
unsigned int length)
2174+
{
2175+
return __napi_alloc_skb(napi, length, GFP_ATOMIC);
2176+
}
21682177

21692178
/**
21702179
* __dev_alloc_pages - allocate page for network Rx

net/core/dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,7 @@ struct sk_buff *napi_get_frags(struct napi_struct *napi)
41724172
struct sk_buff *skb = napi->skb;
41734173

41744174
if (!skb) {
4175-
skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
4175+
skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
41764176
napi->skb = skb;
41774177
}
41784178
return skb;

net/core/skbuff.c

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,13 @@ void *napi_alloc_frag(unsigned int fragsz)
444444
EXPORT_SYMBOL(napi_alloc_frag);
445445

446446
/**
447-
* __netdev_alloc_skb - allocate an skbuff for rx on a specific device
448-
* @dev: network device to receive on
447+
* __alloc_rx_skb - allocate an skbuff for rx
449448
* @length: length to allocate
450449
* @gfp_mask: get_free_pages mask, passed to alloc_skb
450+
* @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
451+
* allocations in case we have to fallback to __alloc_skb()
452+
* If SKB_ALLOC_NAPI is set, page fragment will be allocated
453+
* from napi_cache instead of netdev_cache.
451454
*
452455
* Allocate a new &sk_buff and assign it a usage count of one. The
453456
* buffer has unspecified headroom built in. Users should allocate
@@ -456,11 +459,11 @@ EXPORT_SYMBOL(napi_alloc_frag);
456459
*
457460
* %NULL is returned if there is no free memory.
458461
*/
459-
struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
460-
unsigned int length, gfp_t gfp_mask)
462+
static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
463+
int flags)
461464
{
462465
struct sk_buff *skb = NULL;
463-
unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
466+
unsigned int fragsz = SKB_DATA_ALIGN(length) +
464467
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
465468

466469
if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
@@ -469,25 +472,82 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
469472
if (sk_memalloc_socks())
470473
gfp_mask |= __GFP_MEMALLOC;
471474

472-
data = __netdev_alloc_frag(fragsz, gfp_mask);
475+
data = (flags & SKB_ALLOC_NAPI) ?
476+
__napi_alloc_frag(fragsz, gfp_mask) :
477+
__netdev_alloc_frag(fragsz, gfp_mask);
473478

474479
if (likely(data)) {
475480
skb = build_skb(data, fragsz);
476481
if (unlikely(!skb))
477482
put_page(virt_to_head_page(data));
478483
}
479484
} else {
480-
skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
485+
skb = __alloc_skb(length, gfp_mask,
481486
SKB_ALLOC_RX, NUMA_NO_NODE);
482487
}
488+
return skb;
489+
}
490+
491+
/**
492+
* __netdev_alloc_skb - allocate an skbuff for rx on a specific device
493+
* @dev: network device to receive on
494+
* @length: length to allocate
495+
* @gfp_mask: get_free_pages mask, passed to alloc_skb
496+
*
497+
* Allocate a new &sk_buff and assign it a usage count of one. The
498+
* buffer has NET_SKB_PAD headroom built in. Users should allocate
499+
* the headroom they think they need without accounting for the
500+
* built in space. The built in space is used for optimisations.
501+
*
502+
* %NULL is returned if there is no free memory.
503+
*/
504+
struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
505+
unsigned int length, gfp_t gfp_mask)
506+
{
507+
struct sk_buff *skb;
508+
509+
length += NET_SKB_PAD;
510+
skb = __alloc_rx_skb(length, gfp_mask, 0);
511+
483512
if (likely(skb)) {
484513
skb_reserve(skb, NET_SKB_PAD);
485514
skb->dev = dev;
486515
}
516+
487517
return skb;
488518
}
489519
EXPORT_SYMBOL(__netdev_alloc_skb);
490520

521+
/**
522+
* __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
523+
* @napi: napi instance this buffer was allocated for
524+
* @length: length to allocate
525+
* @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
526+
*
527+
* Allocate a new sk_buff for use in NAPI receive. This buffer will
528+
* attempt to allocate the head from a special reserved region used
529+
* only for NAPI Rx allocation. By doing this we can save several
530+
* CPU cycles by avoiding having to disable and re-enable IRQs.
531+
*
532+
* %NULL is returned if there is no free memory.
533+
*/
534+
struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
535+
unsigned int length, gfp_t gfp_mask)
536+
{
537+
struct sk_buff *skb;
538+
539+
length += NET_SKB_PAD + NET_IP_ALIGN;
540+
skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
541+
542+
if (likely(skb)) {
543+
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
544+
skb->dev = napi->dev;
545+
}
546+
547+
return skb;
548+
}
549+
EXPORT_SYMBOL(__napi_alloc_skb);
550+
491551
void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
492552
int size, unsigned int truesize)
493553
{

0 commit comments

Comments
 (0)