net: move skb ref helpers to new header
[linux-2.6-block.git] / net / core / skbuff.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Routines having to do with the 'struct sk_buff' memory handlers.
4 *
113aa838 5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 *
1da177e4
LT
8 * Fixes:
9 * Alan Cox : Fixed the worst of the load
10 * balancer bugs.
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
23 *
24 * NOTE:
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
1da177e4
LT
29 */
30
31/*
32 * The functions in this file will not compile correctly with gcc 2.4.x
33 */
34
e005d193
JP
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
1da177e4
LT
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
1da177e4
LT
40#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/slab.h>
de960aa9
FW
45#include <linux/tcp.h>
46#include <linux/udp.h>
90017acc 47#include <linux/sctp.h>
1da177e4
LT
48#include <linux/netdevice.h>
49#ifdef CONFIG_NET_CLS_ACT
50#include <net/pkt_sched.h>
51#endif
52#include <linux/string.h>
53#include <linux/skbuff.h>
f6d827b1 54#include <linux/skbuff_ref.h>
9c55e01c 55#include <linux/splice.h>
1da177e4
LT
56#include <linux/cache.h>
57#include <linux/rtnetlink.h>
58#include <linux/init.h>
716ea3a7 59#include <linux/scatterlist.h>
ac45f602 60#include <linux/errqueue.h>
268bb0ce 61#include <linux/prefetch.h>
071c0fc6 62#include <linux/bitfield.h>
0d5501c1 63#include <linux/if_vlan.h>
2a2ea508 64#include <linux/mpls.h>
183f47fc 65#include <linux/kcov.h>
6d0d4199 66#include <linux/iov_iter.h>
1da177e4
LT
67
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
d457a0e3 72#include <net/gso.h>
aa70d2d1 73#include <net/hotdata.h>
ed1f50c3 74#include <net/ip6_checksum.h>
1da177e4 75#include <net/xfrm.h>
8822e270 76#include <net/mpls.h>
3ee17bc7 77#include <net/mptcp.h>
78476d31 78#include <net/mctp.h>
75eaf63e 79#include <net/page_pool/helpers.h>
071c0fc6 80#include <net/dropreason.h>
1da177e4 81
7c0f6ba6 82#include <linux/uaccess.h>
ad8d75ff 83#include <trace/events/skb.h>
51c56b00 84#include <linux/highmem.h>
b245be1f
WB
85#include <linux/capability.h>
86#include <linux/user_namespace.h>
2544af03 87#include <linux/indirect_call_wrapper.h>
2195e2a0 88#include <linux/textsearch.h>
a1f8e7f7 89
39564c3f 90#include "dev.h"
7f678def 91#include "sock_destructor.h"
7b7ed885 92
df5042f4
FW
93#ifdef CONFIG_SKB_EXTENSIONS
94static struct kmem_cache *skbuff_ext_cache __ro_after_init;
95#endif
bf9f1baa 96
bf9f1baa
ED
97#define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER)
98
99/* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two.
100 * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique
101 * size, and we can differentiate heads from skb_small_head_cache
102 * vs system slabs by looking at their size (skb_end_offset()).
103 */
104#define SKB_SMALL_HEAD_CACHE_SIZE \
105 (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \
106 (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \
107 SKB_SMALL_HEAD_SIZE)
108
109#define SKB_SMALL_HEAD_HEADROOM \
110 SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)
bf9f1baa 111
5f74f82e
HWR
112int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
113EXPORT_SYMBOL(sysctl_max_skb_frags);
1da177e4 114
21d2e673
MA
115/* kcm_write_msgs() relies on casting paged frags to bio_vec to use
116 * iov_iter_bvec(). These static asserts ensure the cast is valid is long as the
117 * netmem is a page.
118 */
119static_assert(offsetof(struct bio_vec, bv_page) ==
120 offsetof(skb_frag_t, netmem));
121static_assert(sizeof_field(struct bio_vec, bv_page) ==
122 sizeof_field(skb_frag_t, netmem));
123
124static_assert(offsetof(struct bio_vec, bv_len) == offsetof(skb_frag_t, len));
125static_assert(sizeof_field(struct bio_vec, bv_len) ==
126 sizeof_field(skb_frag_t, len));
127
128static_assert(offsetof(struct bio_vec, bv_offset) ==
129 offsetof(skb_frag_t, offset));
130static_assert(sizeof_field(struct bio_vec, bv_offset) ==
131 sizeof_field(skb_frag_t, offset));
132
9cb252c4
MD
133#undef FN
134#define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
071c0fc6 135static const char * const drop_reasons[] = {
0e84afe8 136 [SKB_CONSUMED] = "CONSUMED",
9cb252c4
MD
137 DEFINE_DROP_REASON(FN, FN)
138};
071c0fc6
JB
139
140static const struct drop_reason_list drop_reasons_core = {
141 .reasons = drop_reasons,
142 .n_reasons = ARRAY_SIZE(drop_reasons),
143};
144
145const struct drop_reason_list __rcu *
146drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = {
147 [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core),
148};
149EXPORT_SYMBOL(drop_reasons_by_subsys);
150
151/**
152 * drop_reasons_register_subsys - register another drop reason subsystem
153 * @subsys: the subsystem to register, must not be the core
154 * @list: the list of drop reasons within the subsystem, must point to
155 * a statically initialized list
156 */
157void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,
158 const struct drop_reason_list *list)
159{
160 if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
161 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
162 "invalid subsystem %d\n", subsys))
163 return;
164
165 /* must point to statically allocated memory, so INIT is OK */
166 RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list);
167}
168EXPORT_SYMBOL_GPL(drop_reasons_register_subsys);
169
170/**
171 * drop_reasons_unregister_subsys - unregister a drop reason subsystem
172 * @subsys: the subsystem to remove, must not be the core
173 *
174 * Note: This will synchronize_rcu() to ensure no users when it returns.
175 */
176void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)
177{
178 if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
179 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
180 "invalid subsystem %d\n", subsys))
181 return;
182
183 RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL);
184
185 synchronize_rcu();
186}
187EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys);
ec43908d 188
1da177e4 189/**
f05de73b
JS
190 * skb_panic - private function for out-of-line support
191 * @skb: buffer
192 * @sz: size
193 * @addr: address
99d5851e 194 * @msg: skb_over_panic or skb_under_panic
1da177e4 195 *
f05de73b
JS
196 * Out-of-line support for skb_put() and skb_push().
197 * Called via the wrapper skb_over_panic() or skb_under_panic().
198 * Keep out of line to prevent kernel bloat.
199 * __builtin_return_address is not used because it is not always reliable.
1da177e4 200 */
f05de73b 201static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99d5851e 202 const char msg[])
1da177e4 203{
41a46913 204 pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
99d5851e 205 msg, addr, skb->len, sz, skb->head, skb->data,
e005d193
JP
206 (unsigned long)skb->tail, (unsigned long)skb->end,
207 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
208 BUG();
209}
210
f05de73b 211static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1da177e4 212{
f05de73b 213 skb_panic(skb, sz, addr, __func__);
1da177e4
LT
214}
215
f05de73b
JS
216static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
217{
218 skb_panic(skb, sz, addr, __func__);
219}
c93bdd0e 220
50fad4b5 221#define NAPI_SKB_CACHE_SIZE 64
f450d539
AL
222#define NAPI_SKB_CACHE_BULK 16
223#define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2)
50fad4b5 224
dbae2b06
PA
225#if PAGE_SIZE == SZ_4K
226
227#define NAPI_HAS_SMALL_PAGE_FRAG 1
228#define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc)
229
230/* specialized page frag allocator using a single order 0 page
231 * and slicing it into 1K sized fragment. Constrained to systems
232 * with a very limited amount of 1K fragments fitting a single
233 * page - to avoid excessive truesize underestimation
234 */
235
236struct page_frag_1k {
237 void *va;
238 u16 offset;
239 bool pfmemalloc;
240};
241
242static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
243{
244 struct page *page;
245 int offset;
246
247 offset = nc->offset - SZ_1K;
248 if (likely(offset >= 0))
249 goto use_frag;
250
251 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
252 if (!page)
253 return NULL;
254
255 nc->va = page_address(page);
256 nc->pfmemalloc = page_is_pfmemalloc(page);
257 offset = PAGE_SIZE - SZ_1K;
258 page_ref_add(page, offset / SZ_1K);
259
260use_frag:
261 nc->offset = offset;
262 return nc->va + offset;
263}
264#else
265
266/* the small page is actually unused in this build; add dummy helpers
267 * to please the compiler and avoid later preprocessor's conditionals
268 */
269#define NAPI_HAS_SMALL_PAGE_FRAG 0
270#define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false
271
272struct page_frag_1k {
273};
274
275static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
276{
277 return NULL;
278}
279
280#endif
281
50fad4b5
AL
282struct napi_alloc_cache {
283 struct page_frag_cache page;
dbae2b06 284 struct page_frag_1k page_small;
50fad4b5
AL
285 unsigned int skb_count;
286 void *skb_cache[NAPI_SKB_CACHE_SIZE];
287};
288
289static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
290static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
291
dbae2b06
PA
292/* Double check that napi_get_frags() allocates skbs with
293 * skb->head being backed by slab, not a page fragment.
294 * This is to make sure bug fixed in 3226b158e67c
295 * ("net: avoid 32 x truesize under-estimation for tiny skbs")
296 * does not accidentally come back.
297 */
298void napi_get_frags_check(struct napi_struct *napi)
299{
300 struct sk_buff *skb;
301
302 local_bh_disable();
303 skb = napi_get_frags(napi);
304 WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
305 napi_free_frags(napi);
306 local_bh_enable();
307}
308
32e3573f 309void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
50fad4b5
AL
310{
311 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
312
50fad4b5
AL
313 fragsz = SKB_DATA_ALIGN(fragsz);
314
411c5f36
YL
315 return __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
316 align_mask);
50fad4b5
AL
317}
318EXPORT_SYMBOL(__napi_alloc_frag_align);
319
320void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
321{
50fad4b5
AL
322 void *data;
323
324 fragsz = SKB_DATA_ALIGN(fragsz);
afa79d08 325 if (in_hardirq() || irqs_disabled()) {
32e3573f
YD
326 struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
327
411c5f36
YL
328 data = __page_frag_alloc_align(nc, fragsz, GFP_ATOMIC,
329 align_mask);
50fad4b5 330 } else {
32e3573f
YD
331 struct napi_alloc_cache *nc;
332
50fad4b5 333 local_bh_disable();
32e3573f 334 nc = this_cpu_ptr(&napi_alloc_cache);
411c5f36
YL
335 data = __page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC,
336 align_mask);
50fad4b5
AL
337 local_bh_enable();
338 }
339 return data;
340}
341EXPORT_SYMBOL(__netdev_alloc_frag_align);
342
f450d539
AL
343static struct sk_buff *napi_skb_cache_get(void)
344{
345 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
346 struct sk_buff *skb;
347
49ae83fc 348 if (unlikely(!nc->skb_count)) {
aa70d2d1 349 nc->skb_count = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
f450d539
AL
350 GFP_ATOMIC,
351 NAPI_SKB_CACHE_BULK,
352 nc->skb_cache);
49ae83fc
SPL
353 if (unlikely(!nc->skb_count))
354 return NULL;
355 }
f450d539
AL
356
357 skb = nc->skb_cache[--nc->skb_count];
aa70d2d1 358 kasan_mempool_unpoison_object(skb, kmem_cache_size(net_hotdata.skbuff_cache));
f450d539
AL
359
360 return skb;
361}
362
ce098da1
KC
363static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
364 unsigned int size)
ba0509b6
JDB
365{
366 struct skb_shared_info *shinfo;
ba0509b6
JDB
367
368 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
369
370 /* Assumes caller memset cleared SKB */
371 skb->truesize = SKB_TRUESIZE(size);
372 refcount_set(&skb->users, 1);
373 skb->head = data;
374 skb->data = data;
375 skb_reset_tail_pointer(skb);
763087da 376 skb_set_end_offset(skb, size);
ba0509b6
JDB
377 skb->mac_header = (typeof(skb->mac_header))~0U;
378 skb->transport_header = (typeof(skb->transport_header))~0U;
68822bdf 379 skb->alloc_cpu = raw_smp_processor_id();
ba0509b6
JDB
380 /* make sure we initialize shinfo sequentially */
381 shinfo = skb_shinfo(skb);
382 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
383 atomic_set(&shinfo->dataref, 1);
384
6370cc3b 385 skb_set_kcov_handle(skb, kcov_common_handle());
ba0509b6
JDB
386}
387
ce098da1
KC
388static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
389 unsigned int *size)
390{
391 void *resized;
392
393 /* Must find the allocation size (and grow it to match). */
394 *size = ksize(data);
395 /* krealloc() will immediately return "data" when
396 * "ksize(data)" is requested: it is the existing upper
397 * bounds. As a result, GFP_ATOMIC will be ignored. Note
398 * that this "new" pointer needs to be passed back to the
399 * caller for use so the __alloc_size hinting will be
400 * tracked correctly.
401 */
402 resized = krealloc(data, *size, GFP_ATOMIC);
403 WARN_ON_ONCE(resized != data);
404 return resized;
405}
406
407/* build_skb() variant which can operate on slab buffers.
408 * Note that this should be used sparingly as slab buffers
409 * cannot be combined efficiently by GRO!
410 */
411struct sk_buff *slab_build_skb(void *data)
412{
413 struct sk_buff *skb;
414 unsigned int size;
415
aa70d2d1 416 skb = kmem_cache_alloc(net_hotdata.skbuff_cache, GFP_ATOMIC);
ce098da1
KC
417 if (unlikely(!skb))
418 return NULL;
419
420 memset(skb, 0, offsetof(struct sk_buff, tail));
421 data = __slab_build_skb(skb, data, &size);
422 __finalize_skb_around(skb, data, size);
423
424 return skb;
425}
426EXPORT_SYMBOL(slab_build_skb);
427
428/* Caller must provide SKB that is memset cleared */
429static void __build_skb_around(struct sk_buff *skb, void *data,
430 unsigned int frag_size)
431{
432 unsigned int size = frag_size;
433
434 /* frag_size == 0 is considered deprecated now. Callers
435 * using slab buffer should use slab_build_skb() instead.
436 */
437 if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
438 data = __slab_build_skb(skb, data, &size);
439
440 __finalize_skb_around(skb, data, size);
441}
442
b2b5ce9d 443/**
2ea2f62c 444 * __build_skb - build a network buffer
b2b5ce9d 445 * @data: data buffer provided by caller
ce098da1 446 * @frag_size: size of data (must not be 0)
b2b5ce9d
ED
447 *
448 * Allocate a new &sk_buff. Caller provides space holding head and
ce098da1
KC
449 * skb_shared_info. @data must have been allocated from the page
450 * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
451 * allocation is deprecated, and callers should use slab_build_skb()
452 * instead.)
b2b5ce9d
ED
453 * The return is the new skb buffer.
454 * On a failure the return is %NULL, and @data is not freed.
455 * Notes :
456 * Before IO, driver allocates only data buffer where NIC put incoming frame
457 * Driver should add room at head (NET_SKB_PAD) and
458 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
459 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
460 * before giving packet to stack.
461 * RX rings only contains data buffers, not full skbs.
462 */
2ea2f62c 463struct sk_buff *__build_skb(void *data, unsigned int frag_size)
b2b5ce9d 464{
b2b5ce9d 465 struct sk_buff *skb;
b2b5ce9d 466
aa70d2d1 467 skb = kmem_cache_alloc(net_hotdata.skbuff_cache, GFP_ATOMIC);
ba0509b6 468 if (unlikely(!skb))
b2b5ce9d
ED
469 return NULL;
470
b2b5ce9d 471 memset(skb, 0, offsetof(struct sk_buff, tail));
483126b3 472 __build_skb_around(skb, data, frag_size);
b2b5ce9d 473
483126b3 474 return skb;
b2b5ce9d 475}
2ea2f62c
ED
476
477/* build_skb() is wrapper over __build_skb(), that specifically
478 * takes care of skb->head and skb->pfmemalloc
2ea2f62c
ED
479 */
480struct sk_buff *build_skb(void *data, unsigned int frag_size)
481{
482 struct sk_buff *skb = __build_skb(data, frag_size);
483
3c640126 484 if (likely(skb && frag_size)) {
2ea2f62c 485 skb->head_frag = 1;
566b6701 486 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
2ea2f62c
ED
487 }
488 return skb;
489}
b2b5ce9d
ED
490EXPORT_SYMBOL(build_skb);
491
ba0509b6
JDB
492/**
493 * build_skb_around - build a network buffer around provided skb
494 * @skb: sk_buff provide by caller, must be memset cleared
495 * @data: data buffer provided by caller
12c1604a 496 * @frag_size: size of data
ba0509b6
JDB
497 */
498struct sk_buff *build_skb_around(struct sk_buff *skb,
499 void *data, unsigned int frag_size)
500{
501 if (unlikely(!skb))
502 return NULL;
503
483126b3 504 __build_skb_around(skb, data, frag_size);
ba0509b6 505
483126b3 506 if (frag_size) {
ba0509b6 507 skb->head_frag = 1;
566b6701 508 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
ba0509b6
JDB
509 }
510 return skb;
511}
512EXPORT_SYMBOL(build_skb_around);
513
f450d539
AL
514/**
515 * __napi_build_skb - build a network buffer
516 * @data: data buffer provided by caller
12c1604a 517 * @frag_size: size of data
f450d539
AL
518 *
519 * Version of __build_skb() that uses NAPI percpu caches to obtain
520 * skbuff_head instead of inplace allocation.
521 *
522 * Returns a new &sk_buff on success, %NULL on allocation failure.
523 */
524static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
525{
526 struct sk_buff *skb;
527
528 skb = napi_skb_cache_get();
529 if (unlikely(!skb))
530 return NULL;
531
532 memset(skb, 0, offsetof(struct sk_buff, tail));
533 __build_skb_around(skb, data, frag_size);
534
535 return skb;
536}
537
538/**
539 * napi_build_skb - build a network buffer
540 * @data: data buffer provided by caller
12c1604a 541 * @frag_size: size of data
f450d539
AL
542 *
543 * Version of __napi_build_skb() that takes care of skb->head_frag
544 * and skb->pfmemalloc when the data is a page or page fragment.
545 *
546 * Returns a new &sk_buff on success, %NULL on allocation failure.
547 */
548struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
549{
550 struct sk_buff *skb = __napi_build_skb(data, frag_size);
551
552 if (likely(skb) && frag_size) {
553 skb->head_frag = 1;
554 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
555 }
556
557 return skb;
558}
559EXPORT_SYMBOL(napi_build_skb);
560
5381b23d
AL
561/*
562 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
563 * the caller if emergency pfmemalloc reserves are being used. If it is and
564 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
565 * may be used. Otherwise, the packet data may be discarded until enough
566 * memory is free
567 */
5c0e820c 568static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
ef28095f 569 bool *pfmemalloc)
5381b23d 570{
5381b23d 571 bool ret_pfmemalloc = false;
915d975b 572 size_t obj_size;
5c0e820c 573 void *obj;
5381b23d 574
5c0e820c 575 obj_size = SKB_HEAD_ALIGN(*size);
bf9f1baa
ED
576 if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
577 !(flags & KMALLOC_NOT_NORMAL_BITS)) {
aa70d2d1 578 obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache,
bf9f1baa
ED
579 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
580 node);
880ce5f2
ED
581 *size = SKB_SMALL_HEAD_CACHE_SIZE;
582 if (obj || !(gfp_pfmemalloc_allowed(flags)))
bf9f1baa 583 goto out;
880ce5f2
ED
584 /* Try again but now we are using pfmemalloc reserves */
585 ret_pfmemalloc = true;
aa70d2d1 586 obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache, flags, node);
880ce5f2 587 goto out;
bf9f1baa 588 }
915d975b
ED
589
590 obj_size = kmalloc_size_roundup(obj_size);
591 /* The following cast might truncate high-order bits of obj_size, this
592 * is harmless because kmalloc(obj_size >= 2^32) will fail anyway.
593 */
594 *size = (unsigned int)obj_size;
595
5381b23d
AL
596 /*
597 * Try a regular allocation, when that fails and we're not entitled
598 * to the reserves, fail.
599 */
5c0e820c 600 obj = kmalloc_node_track_caller(obj_size,
5381b23d
AL
601 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
602 node);
603 if (obj || !(gfp_pfmemalloc_allowed(flags)))
604 goto out;
605
606 /* Try again but now we are using pfmemalloc reserves */
607 ret_pfmemalloc = true;
5c0e820c 608 obj = kmalloc_node_track_caller(obj_size, flags, node);
5381b23d
AL
609
610out:
611 if (pfmemalloc)
612 *pfmemalloc = ret_pfmemalloc;
613
614 return obj;
615}
616
617/* Allocate a new skbuff. We do this ourselves so we can fill in a few
618 * 'private' fields and also do memory statistics to find all the
619 * [BEEP] leaks.
620 *
621 */
622
623/**
624 * __alloc_skb - allocate a network buffer
625 * @size: size to allocate
626 * @gfp_mask: allocation mask
627 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
628 * instead of head cache and allocate a cloned (child) skb.
629 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
630 * allocations in case the data is required for writeback
631 * @node: numa node to allocate memory on
632 *
633 * Allocate a new &sk_buff. The returned buffer has no headroom and a
634 * tail room of at least size bytes. The object has a reference count
635 * of one. The return is the buffer. On a failure the return is %NULL.
636 *
637 * Buffers may only be allocated from interrupts using a @gfp_mask of
638 * %GFP_ATOMIC.
639 */
640struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
641 int flags, int node)
642{
643 struct kmem_cache *cache;
5381b23d 644 struct sk_buff *skb;
5381b23d 645 bool pfmemalloc;
a5df6333 646 u8 *data;
5381b23d
AL
647
648 cache = (flags & SKB_ALLOC_FCLONE)
aa70d2d1 649 ? net_hotdata.skbuff_fclone_cache : net_hotdata.skbuff_cache;
5381b23d
AL
650
651 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
652 gfp_mask |= __GFP_MEMALLOC;
653
654 /* Get the HEAD */
d13612b5
AL
655 if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
656 likely(node == NUMA_NO_NODE || node == numa_mem_id()))
657 skb = napi_skb_cache_get();
658 else
659 skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
df1ae022
AL
660 if (unlikely(!skb))
661 return NULL;
5381b23d
AL
662 prefetchw(skb);
663
664 /* We do our best to align skb_shared_info on a separate cache
665 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
666 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
667 * Both skb->head and skb_shared_info are cache line aligned.
668 */
5c0e820c 669 data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
df1ae022 670 if (unlikely(!data))
5381b23d 671 goto nodata;
12d6c1d3 672 /* kmalloc_size_roundup() might give us more room than requested.
5381b23d
AL
673 * Put skb_shared_info exactly at the end of allocated zone,
674 * to allow max possible filling before reallocation.
675 */
65998d2b 676 prefetchw(data + SKB_WITH_OVERHEAD(size));
5381b23d
AL
677
678 /*
679 * Only clear those fields we need to clear, not those that we will
680 * actually initialise below. Hence, don't put any more fields after
681 * the tail pointer in struct sk_buff!
682 */
683 memset(skb, 0, offsetof(struct sk_buff, tail));
65998d2b 684 __build_skb_around(skb, data, size);
5381b23d 685 skb->pfmemalloc = pfmemalloc;
5381b23d
AL
686
687 if (flags & SKB_ALLOC_FCLONE) {
688 struct sk_buff_fclones *fclones;
689
690 fclones = container_of(skb, struct sk_buff_fclones, skb1);
691
692 skb->fclone = SKB_FCLONE_ORIG;
693 refcount_set(&fclones->fclone_ref, 1);
5381b23d
AL
694 }
695
5381b23d 696 return skb;
df1ae022 697
5381b23d
AL
698nodata:
699 kmem_cache_free(cache, skb);
df1ae022 700 return NULL;
5381b23d
AL
701}
702EXPORT_SYMBOL(__alloc_skb);
703
fd11a83d
AD
704/**
705 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
706 * @dev: network device to receive on
d7499160 707 * @len: length to allocate
fd11a83d
AD
708 * @gfp_mask: get_free_pages mask, passed to alloc_skb
709 *
710 * Allocate a new &sk_buff and assign it a usage count of one. The
711 * buffer has NET_SKB_PAD headroom built in. Users should allocate
712 * the headroom they think they need without accounting for the
713 * built in space. The built in space is used for optimisations.
714 *
715 * %NULL is returned if there is no free memory.
716 */
9451980a
AD
717struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
718 gfp_t gfp_mask)
fd11a83d 719{
b63ae8ca 720 struct page_frag_cache *nc;
fd11a83d 721 struct sk_buff *skb;
9451980a
AD
722 bool pfmemalloc;
723 void *data;
724
725 len += NET_SKB_PAD;
fd11a83d 726
66c55602
AL
727 /* If requested length is either too small or too big,
728 * we use kmalloc() for skb->head allocation.
729 */
730 if (len <= SKB_WITH_OVERHEAD(1024) ||
731 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
d0164adc 732 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
a080e7bd
AD
733 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
734 if (!skb)
735 goto skb_fail;
736 goto skb_success;
737 }
fd11a83d 738
115f1a5c 739 len = SKB_HEAD_ALIGN(len);
9451980a
AD
740
741 if (sk_memalloc_socks())
742 gfp_mask |= __GFP_MEMALLOC;
743
afa79d08 744 if (in_hardirq() || irqs_disabled()) {
92dcabd7
SAS
745 nc = this_cpu_ptr(&netdev_alloc_cache);
746 data = page_frag_alloc(nc, len, gfp_mask);
747 pfmemalloc = nc->pfmemalloc;
748 } else {
749 local_bh_disable();
750 nc = this_cpu_ptr(&napi_alloc_cache.page);
751 data = page_frag_alloc(nc, len, gfp_mask);
752 pfmemalloc = nc->pfmemalloc;
753 local_bh_enable();
754 }
9451980a
AD
755
756 if (unlikely(!data))
757 return NULL;
758
759 skb = __build_skb(data, len);
760 if (unlikely(!skb)) {
181edb2b 761 skb_free_frag(data);
9451980a 762 return NULL;
7b2e497a 763 }
fd11a83d 764
9451980a
AD
765 if (pfmemalloc)
766 skb->pfmemalloc = 1;
767 skb->head_frag = 1;
768
a080e7bd 769skb_success:
9451980a
AD
770 skb_reserve(skb, NET_SKB_PAD);
771 skb->dev = dev;
772
a080e7bd 773skb_fail:
8af27456
CH
774 return skb;
775}
b4ac530f 776EXPORT_SYMBOL(__netdev_alloc_skb);
1da177e4 777
fd11a83d 778/**
6e9b0190 779 * napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
fd11a83d 780 * @napi: napi instance this buffer was allocated for
d7499160 781 * @len: length to allocate
fd11a83d
AD
782 *
783 * Allocate a new sk_buff for use in NAPI receive. This buffer will
784 * attempt to allocate the head from a special reserved region used
785 * only for NAPI Rx allocation. By doing this we can save several
786 * CPU cycles by avoiding having to disable and re-enable IRQs.
787 *
788 * %NULL is returned if there is no free memory.
789 */
6e9b0190 790struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
fd11a83d 791{
6e9b0190 792 gfp_t gfp_mask = GFP_ATOMIC | __GFP_NOWARN;
3226b158 793 struct napi_alloc_cache *nc;
fd11a83d 794 struct sk_buff *skb;
dbae2b06 795 bool pfmemalloc;
9451980a
AD
796 void *data;
797
ee2640df 798 DEBUG_NET_WARN_ON_ONCE(!in_softirq());
9451980a 799 len += NET_SKB_PAD + NET_IP_ALIGN;
fd11a83d 800
3226b158
ED
801 /* If requested length is either too small or too big,
802 * we use kmalloc() for skb->head allocation.
dbae2b06
PA
803 * When the small frag allocator is available, prefer it over kmalloc
804 * for small fragments
3226b158 805 */
dbae2b06 806 if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) ||
3226b158 807 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
d0164adc 808 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
cfb8ec65
AL
809 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
810 NUMA_NO_NODE);
a080e7bd
AD
811 if (!skb)
812 goto skb_fail;
813 goto skb_success;
814 }
9451980a 815
3226b158 816 nc = this_cpu_ptr(&napi_alloc_cache);
9451980a
AD
817
818 if (sk_memalloc_socks())
819 gfp_mask |= __GFP_MEMALLOC;
fd11a83d 820
dbae2b06
PA
821 if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
822 /* we are artificially inflating the allocation size, but
823 * that is not as bad as it may look like, as:
824 * - 'len' less than GRO_MAX_HEAD makes little sense
825 * - On most systems, larger 'len' values lead to fragment
826 * size above 512 bytes
827 * - kmalloc would use the kmalloc-1k slab for such values
828 * - Builds with smaller GRO_MAX_HEAD will very likely do
829 * little networking, as that implies no WiFi and no
830 * tunnels support, and 32 bits arches.
831 */
832 len = SZ_1K;
833
834 data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
835 pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
836 } else {
115f1a5c 837 len = SKB_HEAD_ALIGN(len);
dbae2b06
PA
838
839 data = page_frag_alloc(&nc->page, len, gfp_mask);
840 pfmemalloc = nc->page.pfmemalloc;
841 }
842
9451980a
AD
843 if (unlikely(!data))
844 return NULL;
845
cfb8ec65 846 skb = __napi_build_skb(data, len);
9451980a 847 if (unlikely(!skb)) {
181edb2b 848 skb_free_frag(data);
9451980a 849 return NULL;
fd11a83d
AD
850 }
851
dbae2b06 852 if (pfmemalloc)
9451980a
AD
853 skb->pfmemalloc = 1;
854 skb->head_frag = 1;
855
a080e7bd 856skb_success:
9451980a
AD
857 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
858 skb->dev = napi->dev;
859
a080e7bd 860skb_fail:
fd11a83d
AD
861 return skb;
862}
6e9b0190 863EXPORT_SYMBOL(napi_alloc_skb);
fd11a83d 864
21d2e673
MA
865void skb_add_rx_frag_netmem(struct sk_buff *skb, int i, netmem_ref netmem,
866 int off, int size, unsigned int truesize)
654bed16 867{
c123e0d3
ED
868 DEBUG_NET_WARN_ON_ONCE(size > truesize);
869
21d2e673 870 skb_fill_netmem_desc(skb, i, netmem, off, size);
654bed16
PZ
871 skb->len += size;
872 skb->data_len += size;
50269e19 873 skb->truesize += truesize;
654bed16 874}
21d2e673 875EXPORT_SYMBOL(skb_add_rx_frag_netmem);
654bed16 876
f8e617e1
JW
877void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
878 unsigned int truesize)
879{
880 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
881
c123e0d3
ED
882 DEBUG_NET_WARN_ON_ONCE(size > truesize);
883
f8e617e1
JW
884 skb_frag_size_add(frag, size);
885 skb->len += size;
886 skb->data_len += size;
887 skb->truesize += truesize;
888}
889EXPORT_SYMBOL(skb_coalesce_rx_frag);
890
27b437c8 891static void skb_drop_list(struct sk_buff **listp)
1da177e4 892{
bd8a7036 893 kfree_skb_list(*listp);
27b437c8 894 *listp = NULL;
1da177e4
LT
895}
896
27b437c8
HX
897static inline void skb_drop_fraglist(struct sk_buff *skb)
898{
899 skb_drop_list(&skb_shinfo(skb)->frag_list);
900}
901
1da177e4
LT
902static void skb_clone_fraglist(struct sk_buff *skb)
903{
904 struct sk_buff *list;
905
fbb398a8 906 skb_walk_frags(skb, list)
1da177e4
LT
907 skb_get(list);
908}
909
8cfa2dee
LC
910static bool is_pp_page(struct page *page)
911{
912 return (page->pp_magic & ~0x3UL) == PP_SIGNATURE;
913}
914
27accb3c
LB
915int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
916 unsigned int headroom)
e6d5dbdd
LB
917{
918#if IS_ENABLED(CONFIG_PAGE_POOL)
919 u32 size, truesize, len, max_head_size, off;
920 struct sk_buff *skb = *pskb, *nskb;
921 int err, i, head_off;
922 void *data;
923
924 /* XDP does not support fraglist so we need to linearize
925 * the skb.
926 */
927 if (skb_has_frag_list(skb))
928 return -EOPNOTSUPP;
929
930 max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE - headroom);
931 if (skb->len > max_head_size + MAX_SKB_FRAGS * PAGE_SIZE)
932 return -ENOMEM;
933
934 size = min_t(u32, skb->len, max_head_size);
935 truesize = SKB_HEAD_ALIGN(size) + headroom;
936 data = page_pool_dev_alloc_va(pool, &truesize);
937 if (!data)
938 return -ENOMEM;
939
940 nskb = napi_build_skb(data, truesize);
941 if (!nskb) {
942 page_pool_free_va(pool, data, true);
943 return -ENOMEM;
944 }
945
946 skb_reserve(nskb, headroom);
947 skb_copy_header(nskb, skb);
948 skb_mark_for_recycle(nskb);
949
950 err = skb_copy_bits(skb, 0, nskb->data, size);
951 if (err) {
952 consume_skb(nskb);
953 return err;
954 }
955 skb_put(nskb, size);
956
957 head_off = skb_headroom(nskb) - skb_headroom(skb);
958 skb_headers_offset_update(nskb, head_off);
959
960 off = size;
961 len = skb->len - off;
962 for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
963 struct page *page;
964 u32 page_off;
965
966 size = min_t(u32, len, PAGE_SIZE);
967 truesize = size;
968
969 page = page_pool_dev_alloc(pool, &page_off, &truesize);
c6a28acb 970 if (!page) {
e6d5dbdd
LB
971 consume_skb(nskb);
972 return -ENOMEM;
973 }
974
975 skb_add_rx_frag(nskb, i, page, page_off, size, truesize);
976 err = skb_copy_bits(skb, off, page_address(page) + page_off,
977 size);
978 if (err) {
979 consume_skb(nskb);
980 return err;
981 }
982
983 len -= size;
984 off += size;
985 }
986
987 consume_skb(skb);
988 *pskb = nskb;
989
990 return 0;
991#else
992 return -EOPNOTSUPP;
993#endif
994}
27accb3c 995EXPORT_SYMBOL(skb_pp_cow_data);
e6d5dbdd
LB
996
997int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb,
998 struct bpf_prog *prog)
999{
1000 if (!prog->aux->xdp_has_frags)
1001 return -EINVAL;
1002
1003 return skb_pp_cow_data(pool, pskb, XDP_PACKET_HEADROOM);
1004}
1005EXPORT_SYMBOL(skb_cow_data_for_xdp);
1006
75eaf63e 1007#if IS_ENABLED(CONFIG_PAGE_POOL)
4a96a4e8 1008bool napi_pp_put_page(struct page *page)
75eaf63e 1009{
75eaf63e
AL
1010 page = compound_head(page);
1011
1012 /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
1013 * in order to preserve any existing bits, such as bit 0 for the
1014 * head page of compound page and bit 1 for pfmemalloc page, so
1015 * mask those bits for freeing side when doing below checking,
1016 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
1017 * to avoid recycling the pfmemalloc page.
1018 */
8cfa2dee 1019 if (unlikely(!is_pp_page(page)))
75eaf63e
AL
1020 return false;
1021
4a96a4e8 1022 page_pool_put_full_page(page->pp, page, false);
75eaf63e
AL
1023
1024 return true;
1025}
1026EXPORT_SYMBOL(napi_pp_put_page);
1027#endif
1028
4a96a4e8 1029static bool skb_pp_recycle(struct sk_buff *skb, void *data)
4727bab4
YL
1030{
1031 if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
1032 return false;
4a96a4e8 1033 return napi_pp_put_page(virt_to_page(data));
4727bab4
YL
1034}
1035
f7dc3248
LC
1036/**
1037 * skb_pp_frag_ref() - Increase fragment references of a page pool aware skb
1038 * @skb: page pool aware skb
1039 *
1040 * Increase the fragment reference count (pp_ref_count) of a skb. This is
1041 * intended to gain fragment references only for page pool aware skbs,
1042 * i.e. when skb->pp_recycle is true, and not for fragments in a
1043 * non-pp-recycling skb. It has a fallback to increase references on normal
1044 * pages, as page pool aware skbs may also have normal page fragments.
1045 */
1046static int skb_pp_frag_ref(struct sk_buff *skb)
1047{
1048 struct skb_shared_info *shinfo;
1049 struct page *head_page;
1050 int i;
1051
1052 if (!skb->pp_recycle)
1053 return -EINVAL;
1054
1055 shinfo = skb_shinfo(skb);
1056
1057 for (i = 0; i < shinfo->nr_frags; i++) {
1058 head_page = compound_head(skb_frag_page(&shinfo->frags[i]));
1059 if (likely(is_pp_page(head_page)))
1060 page_pool_ref_page(head_page);
1061 else
1062 page_ref_inc(head_page);
1063 }
1064 return 0;
1065}
1066
bf9f1baa
ED
1067static void skb_kfree_head(void *head, unsigned int end_offset)
1068{
bf9f1baa 1069 if (end_offset == SKB_SMALL_HEAD_HEADROOM)
aa70d2d1 1070 kmem_cache_free(net_hotdata.skb_small_head_cache, head);
bf9f1baa 1071 else
bf9f1baa
ED
1072 kfree(head);
1073}
1074
4a96a4e8 1075static void skb_free_head(struct sk_buff *skb)
d3836f21 1076{
181edb2b
AD
1077 unsigned char *head = skb->head;
1078
6a5bcd84 1079 if (skb->head_frag) {
4a96a4e8 1080 if (skb_pp_recycle(skb, head))
6a5bcd84 1081 return;
181edb2b 1082 skb_free_frag(head);
6a5bcd84 1083 } else {
bf9f1baa 1084 skb_kfree_head(head, skb_end_offset(skb));
6a5bcd84 1085 }
d3836f21
ED
1086}
1087
4a96a4e8 1088static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason)
1da177e4 1089{
ff04a771
ED
1090 struct skb_shared_info *shinfo = skb_shinfo(skb);
1091 int i;
1da177e4 1092
1cface55 1093 if (!skb_data_unref(skb, shinfo))
2cc3aeb5 1094 goto exit;
a6686f2f 1095
753f1ca4
PB
1096 if (skb_zcopy(skb)) {
1097 bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
1098
1099 skb_zcopy_clear(skb, true);
1100 if (skip_unref)
1101 goto free_head;
1102 }
70c43167 1103
ff04a771 1104 for (i = 0; i < shinfo->nr_frags; i++)
f58f3c95 1105 __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
a6686f2f 1106
753f1ca4 1107free_head:
ff04a771 1108 if (shinfo->frag_list)
511a3eda 1109 kfree_skb_list_reason(shinfo->frag_list, reason);
ff04a771 1110
4a96a4e8 1111 skb_free_head(skb);
2cc3aeb5
IA
1112exit:
1113 /* When we clone an SKB we copy the reycling bit. The pp_recycle
1114 * bit is only set on the head though, so in order to avoid races
1115 * while trying to recycle fragments on __skb_frag_unref() we need
1116 * to make one SKB responsible for triggering the recycle path.
1117 * So disable the recycling bit if an SKB is cloned and we have
58e61e41 1118 * additional references to the fragmented part of the SKB.
2cc3aeb5
IA
1119 * Eventually the last SKB will have the recycling bit set and it's
1120 * dataref set to 0, which will trigger the recycling
1121 */
1122 skb->pp_recycle = 0;
1da177e4
LT
1123}
1124
1125/*
1126 * Free an skbuff by memory without cleaning the state.
1127 */
2d4baff8 1128static void kfree_skbmem(struct sk_buff *skb)
1da177e4 1129{
d0bf4a9e 1130 struct sk_buff_fclones *fclones;
d179cd12 1131
d179cd12
DM
1132 switch (skb->fclone) {
1133 case SKB_FCLONE_UNAVAILABLE:
aa70d2d1 1134 kmem_cache_free(net_hotdata.skbuff_cache, skb);
6ffe75eb 1135 return;
d179cd12
DM
1136
1137 case SKB_FCLONE_ORIG:
d0bf4a9e 1138 fclones = container_of(skb, struct sk_buff_fclones, skb1);
d179cd12 1139
6ffe75eb
ED
1140 /* We usually free the clone (TX completion) before original skb
1141 * This test would have no chance to be true for the clone,
1142 * while here, branch prediction will be good.
d179cd12 1143 */
2638595a 1144 if (refcount_read(&fclones->fclone_ref) == 1)
6ffe75eb
ED
1145 goto fastpath;
1146 break;
e7820e39 1147
6ffe75eb
ED
1148 default: /* SKB_FCLONE_CLONE */
1149 fclones = container_of(skb, struct sk_buff_fclones, skb2);
d179cd12 1150 break;
3ff50b79 1151 }
2638595a 1152 if (!refcount_dec_and_test(&fclones->fclone_ref))
6ffe75eb
ED
1153 return;
1154fastpath:
aa70d2d1 1155 kmem_cache_free(net_hotdata.skbuff_fclone_cache, fclones);
1da177e4
LT
1156}
1157
0a463c78 1158void skb_release_head_state(struct sk_buff *skb)
1da177e4 1159{
adf30907 1160 skb_dst_drop(skb);
9c2b3328 1161 if (skb->destructor) {
7890e2f0 1162 DEBUG_NET_WARN_ON_ONCE(in_hardirq());
1da177e4
LT
1163 skb->destructor(skb);
1164 }
a3bf7ae9 1165#if IS_ENABLED(CONFIG_NF_CONNTRACK)
cb9c6836 1166 nf_conntrack_put(skb_nfct(skb));
1da177e4 1167#endif
df5042f4 1168 skb_ext_put(skb);
04a4bb55
LB
1169}
1170
1171/* Free everything but the sk_buff shell. */
4a96a4e8 1172static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason)
04a4bb55
LB
1173{
1174 skb_release_head_state(skb);
a28b1b90 1175 if (likely(skb->head))
4a96a4e8 1176 skb_release_data(skb, reason);
2d4baff8
HX
1177}
1178
1179/**
1180 * __kfree_skb - private function
1181 * @skb: buffer
1182 *
1183 * Free an sk_buff. Release anything attached to the buffer.
1184 * Clean the state. This is an internal helper function. Users should
1185 * always call kfree_skb
1186 */
1da177e4 1187
2d4baff8
HX
1188void __kfree_skb(struct sk_buff *skb)
1189{
4a96a4e8 1190 skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1da177e4
LT
1191 kfree_skbmem(skb);
1192}
b4ac530f 1193EXPORT_SYMBOL(__kfree_skb);
1da177e4 1194
a4650da2
JDB
1195static __always_inline
1196bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1197{
1198 if (unlikely(!skb_unref(skb)))
1199 return false;
1200
071c0fc6
JB
1201 DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET ||
1202 u32_get_bits(reason,
1203 SKB_DROP_REASON_SUBSYS_MASK) >=
1204 SKB_DROP_REASON_SUBSYS_NUM);
a4650da2
JDB
1205
1206 if (reason == SKB_CONSUMED)
dd1b5278 1207 trace_consume_skb(skb, __builtin_return_address(0));
a4650da2
JDB
1208 else
1209 trace_kfree_skb(skb, __builtin_return_address(0), reason);
1210 return true;
1211}
1212