Skip to content

Commit 0be8bf6

Browse files
edumazetpopcornmix
authored andcommitted
net: use bigger pages in __netdev_alloc_frag
We currently use percpu order-0 pages in __netdev_alloc_frag to deliver fragments used by __netdev_alloc_skb() Depending on NIC driver and arch being 32 or 64 bit, it allows a page to be split in several fragments (between 1 and 8), assuming PAGE_SIZE=4096 Switching to bigger pages (32768 bytes for PAGE_SIZE=4096 case) allows : - Better filling of space (the ending hole overhead is less an issue) - Less calls to page allocator or accesses to page->_count - Could allow struct skb_shared_info futures changes without major performance impact. This patch implements a transparent fallback to smaller pages in case of memory pressure. It also uses a standard "struct page_frag" instead of a custom one. Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Alexander Duyck <alexander.h.duyck@intel.com> Cc: Benjamin LaHaise <bcrl@kvack.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9d2f3db commit 0be8bf6

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

‎net/core/skbuff.c‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,43 +340,57 @@ struct sk_buff *build_skb(void *data, unsigned int frag_size)
340340
EXPORT_SYMBOL(build_skb);
341341

342342
struct netdev_alloc_cache {
343-
struct page *page;
344-
unsigned int offset;
345-
unsigned int pagecnt_bias;
343+
struct page_frag frag;
344+
/* we maintain a pagecount bias, so that we dont dirty cache line
345+
* containing page->_count every time we allocate a fragment.
346+
*/
347+
unsigned int pagecnt_bias;
346348
};
347349
static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
348350

349-
#define NETDEV_PAGECNT_BIAS (PAGE_SIZE / SMP_CACHE_BYTES)
351+
#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
352+
#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
353+
#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
350354

351355
static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
352356
{
353357
struct netdev_alloc_cache *nc;
354358
void *data = NULL;
359+
int order;
355360
unsigned long flags;
356361

357362
local_irq_save(flags);
358363
nc = &__get_cpu_var(netdev_alloc_cache);
359-
if (unlikely(!nc->page)) {
364+
if (unlikely(!nc->frag.page)) {
360365
refill:
361-
nc->page = alloc_page(gfp_mask);
362-
if (unlikely(!nc->page))
363-
goto end;
366+
for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
367+
gfp_t gfp = gfp_mask;
368+
369+
if (order)
370+
gfp |= __GFP_COMP | __GFP_NOWARN;
371+
nc->frag.page = alloc_pages(gfp, order);
372+
if (likely(nc->frag.page))
373+
break;
374+
if (--order < 0)
375+
goto end;
376+
}
377+
nc->frag.size = PAGE_SIZE << order;
364378
recycle:
365-
atomic_set(&nc->page->_count, NETDEV_PAGECNT_BIAS);
366-
nc->pagecnt_bias = NETDEV_PAGECNT_BIAS;
367-
nc->offset = 0;
379+
atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
380+
nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
381+
nc->frag.offset = 0;
368382
}
369383

370-
if (nc->offset + fragsz > PAGE_SIZE) {
384+
if (nc->frag.offset + fragsz > nc->frag.size) {
371385
/* avoid unnecessary locked operations if possible */
372-
if ((atomic_read(&nc->page->_count) == nc->pagecnt_bias) ||
373-
atomic_sub_and_test(nc->pagecnt_bias, &nc->page->_count))
386+
if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
387+
atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
374388
goto recycle;
375389
goto refill;
376390
}
377391

378-
data = page_address(nc->page) + nc->offset;
379-
nc->offset += fragsz;
392+
data = page_address(nc->frag.page) + nc->frag.offset;
393+
nc->frag.offset += fragsz;
380394
nc->pagecnt_bias--;
381395
end:
382396
local_irq_restore(flags);

0 commit comments

Comments
 (0)