Skip to content

Commit bdacf3e

Browse files
Sebastian Andrzej Siewiorkuba-moo
authored andcommitted
net: Use nested-BH locking for napi_alloc_cache.
napi_alloc_cache is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking. Add a local_lock_t to the data structure and use local_lock_nested_bh() for locking. This change adds only lockdep coverage and does not alter the functional behaviour for !PREEMPT_RT. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 43d7ca2 commit bdacf3e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

net/core/skbuff.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,17 @@ static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
277277
#endif
278278

279279
struct napi_alloc_cache {
280+
local_lock_t bh_lock;
280281
struct page_frag_cache page;
281282
struct page_frag_1k page_small;
282283
unsigned int skb_count;
283284
void *skb_cache[NAPI_SKB_CACHE_SIZE];
284285
};
285286

286287
static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
287-
static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
288+
static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache) = {
289+
.bh_lock = INIT_LOCAL_LOCK(bh_lock),
290+
};
288291

289292
/* Double check that napi_get_frags() allocates skbs with
290293
* skb->head being backed by slab, not a page fragment.
@@ -306,11 +309,16 @@ void napi_get_frags_check(struct napi_struct *napi)
306309
void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
307310
{
308311
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
312+
void *data;
309313

310314
fragsz = SKB_DATA_ALIGN(fragsz);
311315

312-
return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
316+
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
317+
data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
313318
align_mask);
319+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
320+
return data;
321+
314322
}
315323
EXPORT_SYMBOL(__napi_alloc_frag_align);
316324

@@ -338,16 +346,20 @@ static struct sk_buff *napi_skb_cache_get(void)
338346
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
339347
struct sk_buff *skb;
340348

349+
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
341350
if (unlikely(!nc->skb_count)) {
342351
nc->skb_count = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
343352
GFP_ATOMIC,
344353
NAPI_SKB_CACHE_BULK,
345354
nc->skb_cache);
346-
if (unlikely(!nc->skb_count))
355+
if (unlikely(!nc->skb_count)) {
356+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
347357
return NULL;
358+
}
348359
}
349360

350361
skb = nc->skb_cache[--nc->skb_count];
362+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
351363
kasan_mempool_unpoison_object(skb, kmem_cache_size(net_hotdata.skbuff_cache));
352364

353365
return skb;
@@ -740,9 +752,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
740752
pfmemalloc = nc->pfmemalloc;
741753
} else {
742754
local_bh_disable();
755+
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
756+
743757
nc = this_cpu_ptr(&napi_alloc_cache.page);
744758
data = page_frag_alloc(nc, len, gfp_mask);
745759
pfmemalloc = nc->pfmemalloc;
760+
761+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
746762
local_bh_enable();
747763
}
748764

@@ -806,11 +822,11 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
806822
goto skb_success;
807823
}
808824

809-
nc = this_cpu_ptr(&napi_alloc_cache);
810-
811825
if (sk_memalloc_socks())
812826
gfp_mask |= __GFP_MEMALLOC;
813827

828+
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
829+
nc = this_cpu_ptr(&napi_alloc_cache);
814830
if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
815831
/* we are artificially inflating the allocation size, but
816832
* that is not as bad as it may look like, as:
@@ -832,6 +848,7 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
832848
data = page_frag_alloc(&nc->page, len, gfp_mask);
833849
pfmemalloc = nc->page.pfmemalloc;
834850
}
851+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
835852

836853
if (unlikely(!data))
837854
return NULL;
@@ -1431,6 +1448,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)
14311448
if (!kasan_mempool_poison_object(skb))
14321449
return;
14331450

1451+
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
14341452
nc->skb_cache[nc->skb_count++] = skb;
14351453

14361454
if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
@@ -1442,6 +1460,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)
14421460
nc->skb_cache + NAPI_SKB_CACHE_HALF);
14431461
nc->skb_count = NAPI_SKB_CACHE_HALF;
14441462
}
1463+
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
14451464
}
14461465

14471466
void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason)

0 commit comments

Comments
 (0)