Skip to content

Commit 7ba7aea

Browse files
Sebastian Andrzej Siewiordavem330
authored andcommitted
net: Don't disable interrupts in napi_alloc_frag()
netdev_alloc_frag() can be used from any context and is used by NAPI and non-NAPI drivers. Non-NAPI drivers use it in interrupt context and NAPI drivers use it during initial allocation (->ndo_open() or ->ndo_change_mtu()). Some NAPI drivers share the same function for the initial allocation and the allocation in their NAPI callback. The interrupts are disabled in order to ensure locked access from every context to `netdev_alloc_cache'. Let netdev_alloc_frag() check if interrupts are disabled. If they are, use `netdev_alloc_cache' otherwise disable BH and invoke __napi_alloc_frag() for the allocation. The IRQ check is cheaper compared to disabling & enabling interrupts and memory allocation with disabled interrupts does not work on -RT. Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9e49fe4 commit 7ba7aea

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

‎net/core/skbuff.c‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,21 @@ struct napi_alloc_cache {
366366
static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
367367
static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
368368

369-
static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
369+
static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
370370
{
371-
struct page_frag_cache *nc;
372-
unsigned long flags;
373-
void *data;
371+
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
374372

375-
local_irq_save(flags);
376-
nc = this_cpu_ptr(&netdev_alloc_cache);
377-
data = page_frag_alloc(nc, fragsz, gfp_mask);
378-
local_irq_restore(flags);
379-
return data;
373+
return page_frag_alloc(&nc->page, fragsz, gfp_mask);
380374
}
381375

376+
void *napi_alloc_frag(unsigned int fragsz)
377+
{
378+
fragsz = SKB_DATA_ALIGN(fragsz);
379+
380+
return __napi_alloc_frag(fragsz, GFP_ATOMIC);
381+
}
382+
EXPORT_SYMBOL(napi_alloc_frag);
383+
382384
/**
383385
* netdev_alloc_frag - allocate a page fragment
384386
* @fragsz: fragment size
@@ -388,26 +390,21 @@ static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
388390
*/
389391
void *netdev_alloc_frag(unsigned int fragsz)
390392
{
391-
fragsz = SKB_DATA_ALIGN(fragsz);
392-
393-
return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
394-
}
395-
EXPORT_SYMBOL(netdev_alloc_frag);
396-
397-
static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
398-
{
399-
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
400-
401-
return page_frag_alloc(&nc->page, fragsz, gfp_mask);
402-
}
393+
struct page_frag_cache *nc;
394+
void *data;
403395

404-
void *napi_alloc_frag(unsigned int fragsz)
405-
{
406396
fragsz = SKB_DATA_ALIGN(fragsz);
407-
408-
return __napi_alloc_frag(fragsz, GFP_ATOMIC);
397+
if (in_irq() || irqs_disabled()) {
398+
nc = this_cpu_ptr(&netdev_alloc_cache);
399+
data = page_frag_alloc(nc, fragsz, GFP_ATOMIC);
400+
} else {
401+
local_bh_disable();
402+
data = __napi_alloc_frag(fragsz, GFP_ATOMIC);
403+
local_bh_enable();
404+
}
405+
return data;
409406
}
410-
EXPORT_SYMBOL(napi_alloc_frag);
407+
EXPORT_SYMBOL(netdev_alloc_frag);
411408

412409
/**
413410
* __netdev_alloc_skb - allocate an skbuff for rx on a specific device

0 commit comments

Comments
 (0)