Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: Don't disable interrupts in __netdev_alloc_skb()

__netdev_alloc_skb() 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_skb() check if interrupts are disabled. If they are, use
`netdev_alloc_cache'. Otherwise disable BH and use `napi_alloc_cache.page'.
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>

authored by

Sebastian Andrzej Siewior and committed by
David S. Miller
92dcabd7 7ba7aeab

+11 -8
+11 -8
net/core/skbuff.c
··· 423 423 gfp_t gfp_mask) 424 424 { 425 425 struct page_frag_cache *nc; 426 - unsigned long flags; 427 426 struct sk_buff *skb; 428 427 bool pfmemalloc; 429 428 void *data; ··· 443 444 if (sk_memalloc_socks()) 444 445 gfp_mask |= __GFP_MEMALLOC; 445 446 446 - local_irq_save(flags); 447 - 448 - nc = this_cpu_ptr(&netdev_alloc_cache); 449 - data = page_frag_alloc(nc, len, gfp_mask); 450 - pfmemalloc = nc->pfmemalloc; 451 - 452 - local_irq_restore(flags); 447 + if (in_irq() || irqs_disabled()) { 448 + nc = this_cpu_ptr(&netdev_alloc_cache); 449 + data = page_frag_alloc(nc, len, gfp_mask); 450 + pfmemalloc = nc->pfmemalloc; 451 + } else { 452 + local_bh_disable(); 453 + nc = this_cpu_ptr(&napi_alloc_cache.page); 454 + data = page_frag_alloc(nc, len, gfp_mask); 455 + pfmemalloc = nc->pfmemalloc; 456 + local_bh_enable(); 457 + } 453 458 454 459 if (unlikely(!data)) 455 460 return NULL;