@@ -277,14 +277,17 @@ static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
277
277
#endif
278
278
279
279
struct napi_alloc_cache {
280
+ local_lock_t bh_lock ;
280
281
struct page_frag_cache page ;
281
282
struct page_frag_1k page_small ;
282
283
unsigned int skb_count ;
283
284
void * skb_cache [NAPI_SKB_CACHE_SIZE ];
284
285
};
285
286
286
287
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
+ };
288
291
289
292
/* Double check that napi_get_frags() allocates skbs with
290
293
* skb->head being backed by slab, not a page fragment.
@@ -306,11 +309,16 @@ void napi_get_frags_check(struct napi_struct *napi)
306
309
void * __napi_alloc_frag_align (unsigned int fragsz , unsigned int align_mask )
307
310
{
308
311
struct napi_alloc_cache * nc = this_cpu_ptr (& napi_alloc_cache );
312
+ void * data ;
309
313
310
314
fragsz = SKB_DATA_ALIGN (fragsz );
311
315
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 ,
313
318
align_mask );
319
+ local_unlock_nested_bh (& napi_alloc_cache .bh_lock );
320
+ return data ;
321
+
314
322
}
315
323
EXPORT_SYMBOL (__napi_alloc_frag_align );
316
324
@@ -338,16 +346,20 @@ static struct sk_buff *napi_skb_cache_get(void)
338
346
struct napi_alloc_cache * nc = this_cpu_ptr (& napi_alloc_cache );
339
347
struct sk_buff * skb ;
340
348
349
+ local_lock_nested_bh (& napi_alloc_cache .bh_lock );
341
350
if (unlikely (!nc -> skb_count )) {
342
351
nc -> skb_count = kmem_cache_alloc_bulk (net_hotdata .skbuff_cache ,
343
352
GFP_ATOMIC ,
344
353
NAPI_SKB_CACHE_BULK ,
345
354
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 );
347
357
return NULL ;
358
+ }
348
359
}
349
360
350
361
skb = nc -> skb_cache [-- nc -> skb_count ];
362
+ local_unlock_nested_bh (& napi_alloc_cache .bh_lock );
351
363
kasan_mempool_unpoison_object (skb , kmem_cache_size (net_hotdata .skbuff_cache ));
352
364
353
365
return skb ;
@@ -740,9 +752,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
740
752
pfmemalloc = nc -> pfmemalloc ;
741
753
} else {
742
754
local_bh_disable ();
755
+ local_lock_nested_bh (& napi_alloc_cache .bh_lock );
756
+
743
757
nc = this_cpu_ptr (& napi_alloc_cache .page );
744
758
data = page_frag_alloc (nc , len , gfp_mask );
745
759
pfmemalloc = nc -> pfmemalloc ;
760
+
761
+ local_unlock_nested_bh (& napi_alloc_cache .bh_lock );
746
762
local_bh_enable ();
747
763
}
748
764
@@ -806,11 +822,11 @@ struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
806
822
goto skb_success ;
807
823
}
808
824
809
- nc = this_cpu_ptr (& napi_alloc_cache );
810
-
811
825
if (sk_memalloc_socks ())
812
826
gfp_mask |= __GFP_MEMALLOC ;
813
827
828
+ local_lock_nested_bh (& napi_alloc_cache .bh_lock );
829
+ nc = this_cpu_ptr (& napi_alloc_cache );
814
830
if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD (1024 )) {
815
831
/* we are artificially inflating the allocation size, but
816
832
* 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)
832
848
data = page_frag_alloc (& nc -> page , len , gfp_mask );
833
849
pfmemalloc = nc -> page .pfmemalloc ;
834
850
}
851
+ local_unlock_nested_bh (& napi_alloc_cache .bh_lock );
835
852
836
853
if (unlikely (!data ))
837
854
return NULL ;
@@ -1431,6 +1448,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)
1431
1448
if (!kasan_mempool_poison_object (skb ))
1432
1449
return ;
1433
1450
1451
+ local_lock_nested_bh (& napi_alloc_cache .bh_lock );
1434
1452
nc -> skb_cache [nc -> skb_count ++ ] = skb ;
1435
1453
1436
1454
if (unlikely (nc -> skb_count == NAPI_SKB_CACHE_SIZE )) {
@@ -1442,6 +1460,7 @@ static void napi_skb_cache_put(struct sk_buff *skb)
1442
1460
nc -> skb_cache + NAPI_SKB_CACHE_HALF );
1443
1461
nc -> skb_count = NAPI_SKB_CACHE_HALF ;
1444
1462
}
1463
+ local_unlock_nested_bh (& napi_alloc_cache .bh_lock );
1445
1464
}
1446
1465
1447
1466
void __napi_kfree_skb (struct sk_buff * skb , enum skb_drop_reason reason )
0 commit comments