]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/skbuff.c
mm/net: Rename and move page fragment handling from net/ to mm/
[thirdparty/linux.git] / net / core / skbuff.c
CommitLineData
1da177e4
LT
1/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
113aa838 4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
1da177e4
LT
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
e005d193
JP
39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
1da177e4
LT
41#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
fe55f6d5 44#include <linux/kmemcheck.h>
1da177e4
LT
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
de960aa9
FW
50#include <linux/tcp.h>
51#include <linux/udp.h>
1da177e4
LT
52#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
9c55e01c 58#include <linux/splice.h>
1da177e4
LT
59#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
716ea3a7 62#include <linux/scatterlist.h>
ac45f602 63#include <linux/errqueue.h>
268bb0ce 64#include <linux/prefetch.h>
0d5501c1 65#include <linux/if_vlan.h>
1da177e4
LT
66
67#include <net/protocol.h>
68#include <net/dst.h>
69#include <net/sock.h>
70#include <net/checksum.h>
ed1f50c3 71#include <net/ip6_checksum.h>
1da177e4
LT
72#include <net/xfrm.h>
73
74#include <asm/uaccess.h>
ad8d75ff 75#include <trace/events/skb.h>
51c56b00 76#include <linux/highmem.h>
b245be1f
WB
77#include <linux/capability.h>
78#include <linux/user_namespace.h>
a1f8e7f7 79
d7e8883c 80struct kmem_cache *skbuff_head_cache __read_mostly;
e18b890b 81static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1da177e4 82
1da177e4 83/**
f05de73b
JS
84 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
99d5851e 88 * @msg: skb_over_panic or skb_under_panic
1da177e4 89 *
f05de73b
JS
90 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
1da177e4 94 */
f05de73b 95static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99d5851e 96 const char msg[])
1da177e4 97{
e005d193 98 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99d5851e 99 msg, addr, skb->len, sz, skb->head, skb->data,
e005d193
JP
100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
102 BUG();
103}
104
f05de73b 105static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1da177e4 106{
f05de73b 107 skb_panic(skb, sz, addr, __func__);
1da177e4
LT
108}
109
f05de73b
JS
110static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111{
112 skb_panic(skb, sz, addr, __func__);
113}
c93bdd0e
MG
114
115/*
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
121 */
122#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
61c5e88a 124
125static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
c93bdd0e
MG
127{
128 void *obj;
129 bool ret_pfmemalloc = false;
130
131 /*
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
134 */
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
140
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
144
145out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
148
149 return obj;
150}
151
1da177e4
LT
152/* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
155 *
156 */
157
0ebd0ac5
PM
158struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
159{
160 struct sk_buff *skb;
161
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
167
168 /*
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
172 */
173 memset(skb, 0, offsetof(struct sk_buff, tail));
5e71d9d7 174 skb->head = NULL;
0ebd0ac5
PM
175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
177
35d04610 178 skb->mac_header = (typeof(skb->mac_header))~0U;
0ebd0ac5
PM
179out:
180 return skb;
181}
182
1da177e4 183/**
d179cd12 184 * __alloc_skb - allocate a network buffer
1da177e4
LT
185 * @size: size to allocate
186 * @gfp_mask: allocation mask
c93bdd0e
MG
187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
b30973f8 191 * @node: numa node to allocate memory on
1da177e4
LT
192 *
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
94b6042c
BH
194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
1da177e4
LT
196 *
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
199 */
dd0fc66f 200struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
c93bdd0e 201 int flags, int node)
1da177e4 202{
e18b890b 203 struct kmem_cache *cache;
4947d3ef 204 struct skb_shared_info *shinfo;
1da177e4
LT
205 struct sk_buff *skb;
206 u8 *data;
c93bdd0e 207 bool pfmemalloc;
1da177e4 208
c93bdd0e
MG
209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
211
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
8798b3fb 214
1da177e4 215 /* Get the HEAD */
b30973f8 216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
217 if (!skb)
218 goto out;
ec7d2f2c 219 prefetchw(skb);
1da177e4 220
87fb4b7b
ED
221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
225 */
bc417e30 226 size = SKB_DATA_ALIGN(size);
87fb4b7b 227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c93bdd0e 228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
1da177e4
LT
229 if (!data)
230 goto nodata;
87fb4b7b
ED
231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
234 */
235 size = SKB_WITH_OVERHEAD(ksize(data));
ec7d2f2c 236 prefetchw(data + size);
1da177e4 237
ca0605a7 238 /*
c8005785
JB
239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
ca0605a7
ACM
242 */
243 memset(skb, 0, offsetof(struct sk_buff, tail));
87fb4b7b
ED
244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
c93bdd0e 246 skb->pfmemalloc = pfmemalloc;
1da177e4
LT
247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
27a884dc 250 skb_reset_tail_pointer(skb);
4305b541 251 skb->end = skb->tail + size;
35d04610
CW
252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
19633e12 254
4947d3ef
BL
255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
ec7d2f2c 257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
4947d3ef 258 atomic_set(&shinfo->dataref, 1);
c2aa3665 259 kmemcheck_annotate_variable(shinfo->destructor_arg);
4947d3ef 260
c93bdd0e 261 if (flags & SKB_ALLOC_FCLONE) {
d0bf4a9e 262 struct sk_buff_fclones *fclones;
1da177e4 263
d0bf4a9e
ED
264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
265
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
d179cd12 267 skb->fclone = SKB_FCLONE_ORIG;
d0bf4a9e 268 atomic_set(&fclones->fclone_ref, 1);
d179cd12 269
6ffe75eb 270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
d0bf4a9e 271 fclones->skb2.pfmemalloc = pfmemalloc;
d179cd12 272 }
1da177e4
LT
273out:
274 return skb;
275nodata:
8798b3fb 276 kmem_cache_free(cache, skb);
1da177e4
LT
277 skb = NULL;
278 goto out;
1da177e4 279}
b4ac530f 280EXPORT_SYMBOL(__alloc_skb);
1da177e4 281
b2b5ce9d 282/**
2ea2f62c 283 * __build_skb - build a network buffer
b2b5ce9d 284 * @data: data buffer provided by caller
2ea2f62c 285 * @frag_size: size of data, or 0 if head was kmalloced
b2b5ce9d
ED
286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
deceb4c0 288 * skb_shared_info. @data must have been allocated by kmalloc() only if
2ea2f62c
ED
289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
b2b5ce9d
ED
291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
300 */
2ea2f62c 301struct sk_buff *__build_skb(void *data, unsigned int frag_size)
b2b5ce9d
ED
302{
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
d3836f21 305 unsigned int size = frag_size ? : ksize(data);
b2b5ce9d
ED
306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
310
d3836f21 311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
b2b5ce9d
ED
312
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
35d04610
CW
320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
b2b5ce9d
ED
322
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
328
329 return skb;
330}
2ea2f62c
ED
331
332/* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
336 */
337struct sk_buff *build_skb(void *data, unsigned int frag_size)
338{
339 struct sk_buff *skb = __build_skb(data, frag_size);
340
341 if (skb && frag_size) {
342 skb->head_frag = 1;
343 if (virt_to_head_page(data)->pfmemalloc)
344 skb->pfmemalloc = 1;
345 }
346 return skb;
347}
b2b5ce9d
ED
348EXPORT_SYMBOL(build_skb);
349
b63ae8ca
AD
350static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
351static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
ffde7328
AD
352
353static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
354{
b63ae8ca 355 struct page_frag_cache *nc;
ffde7328
AD
356 unsigned long flags;
357 void *data;
358
359 local_irq_save(flags);
9451980a
AD
360 nc = this_cpu_ptr(&netdev_alloc_cache);
361 data = __alloc_page_frag(nc, fragsz, gfp_mask);
6f532612
ED
362 local_irq_restore(flags);
363 return data;
364}
c93bdd0e
MG
365
366/**
367 * netdev_alloc_frag - allocate a page fragment
368 * @fragsz: fragment size
369 *
370 * Allocates a frag from a page for receive buffer.
371 * Uses GFP_ATOMIC allocations.
372 */
373void *netdev_alloc_frag(unsigned int fragsz)
374{
375 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
376}
6f532612
ED
377EXPORT_SYMBOL(netdev_alloc_frag);
378
ffde7328
AD
379static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
380{
b63ae8ca 381 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
9451980a
AD
382
383 return __alloc_page_frag(nc, fragsz, gfp_mask);
ffde7328
AD
384}
385
386void *napi_alloc_frag(unsigned int fragsz)
387{
388 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
389}
390EXPORT_SYMBOL(napi_alloc_frag);
391
fd11a83d
AD
392/**
393 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
394 * @dev: network device to receive on
395 * @length: length to allocate
396 * @gfp_mask: get_free_pages mask, passed to alloc_skb
397 *
398 * Allocate a new &sk_buff and assign it a usage count of one. The
399 * buffer has NET_SKB_PAD headroom built in. Users should allocate
400 * the headroom they think they need without accounting for the
401 * built in space. The built in space is used for optimisations.
402 *
403 * %NULL is returned if there is no free memory.
404 */
9451980a
AD
405struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
406 gfp_t gfp_mask)
fd11a83d 407{
b63ae8ca 408 struct page_frag_cache *nc;
9451980a 409 unsigned long flags;
fd11a83d 410 struct sk_buff *skb;
9451980a
AD
411 bool pfmemalloc;
412 void *data;
413
414 len += NET_SKB_PAD;
fd11a83d 415
9451980a
AD
416 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
417 (gfp_mask & (__GFP_WAIT | GFP_DMA)))
418 return __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
fd11a83d 419
9451980a
AD
420 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
421 len = SKB_DATA_ALIGN(len);
422
423 if (sk_memalloc_socks())
424 gfp_mask |= __GFP_MEMALLOC;
425
426 local_irq_save(flags);
427
428 nc = this_cpu_ptr(&netdev_alloc_cache);
429 data = __alloc_page_frag(nc, len, gfp_mask);
430 pfmemalloc = nc->pfmemalloc;
431
432 local_irq_restore(flags);
433
434 if (unlikely(!data))
435 return NULL;
436
437 skb = __build_skb(data, len);
438 if (unlikely(!skb)) {
439 put_page(virt_to_head_page(data));
440 return NULL;
7b2e497a 441 }
fd11a83d 442
9451980a
AD
443 /* use OR instead of assignment to avoid clearing of bits in mask */
444 if (pfmemalloc)
445 skb->pfmemalloc = 1;
446 skb->head_frag = 1;
447
448 skb_reserve(skb, NET_SKB_PAD);
449 skb->dev = dev;
450
8af27456
CH
451 return skb;
452}
b4ac530f 453EXPORT_SYMBOL(__netdev_alloc_skb);
1da177e4 454
fd11a83d
AD
455/**
456 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
457 * @napi: napi instance this buffer was allocated for
458 * @length: length to allocate
459 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
460 *
461 * Allocate a new sk_buff for use in NAPI receive. This buffer will
462 * attempt to allocate the head from a special reserved region used
463 * only for NAPI Rx allocation. By doing this we can save several
464 * CPU cycles by avoiding having to disable and re-enable IRQs.
465 *
466 * %NULL is returned if there is no free memory.
467 */
9451980a
AD
468struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
469 gfp_t gfp_mask)
fd11a83d 470{
b63ae8ca 471 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
fd11a83d 472 struct sk_buff *skb;
9451980a
AD
473 void *data;
474
475 len += NET_SKB_PAD + NET_IP_ALIGN;
fd11a83d 476
9451980a
AD
477 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
478 (gfp_mask & (__GFP_WAIT | GFP_DMA)))
479 return __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
480
481 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
482 len = SKB_DATA_ALIGN(len);
483
484 if (sk_memalloc_socks())
485 gfp_mask |= __GFP_MEMALLOC;
fd11a83d 486
9451980a
AD
487 data = __alloc_page_frag(nc, len, gfp_mask);
488 if (unlikely(!data))
489 return NULL;
490
491 skb = __build_skb(data, len);
492 if (unlikely(!skb)) {
493 put_page(virt_to_head_page(data));
494 return NULL;
fd11a83d
AD
495 }
496
9451980a
AD
497 /* use OR instead of assignment to avoid clearing of bits in mask */
498 if (nc->pfmemalloc)
499 skb->pfmemalloc = 1;
500 skb->head_frag = 1;
501
502 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
503 skb->dev = napi->dev;
504
fd11a83d
AD
505 return skb;
506}
507EXPORT_SYMBOL(__napi_alloc_skb);
508
654bed16 509void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
50269e19 510 int size, unsigned int truesize)
654bed16
PZ
511{
512 skb_fill_page_desc(skb, i, page, off, size);
513 skb->len += size;
514 skb->data_len += size;
50269e19 515 skb->truesize += truesize;
654bed16
PZ
516}
517EXPORT_SYMBOL(skb_add_rx_frag);
518
f8e617e1
JW
519void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
520 unsigned int truesize)
521{
522 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
523
524 skb_frag_size_add(frag, size);
525 skb->len += size;
526 skb->data_len += size;
527 skb->truesize += truesize;
528}
529EXPORT_SYMBOL(skb_coalesce_rx_frag);
530
27b437c8 531static void skb_drop_list(struct sk_buff **listp)
1da177e4 532{
bd8a7036 533 kfree_skb_list(*listp);
27b437c8 534 *listp = NULL;
1da177e4
LT
535}
536
27b437c8
HX
537static inline void skb_drop_fraglist(struct sk_buff *skb)
538{
539 skb_drop_list(&skb_shinfo(skb)->frag_list);
540}
541
1da177e4
LT
542static void skb_clone_fraglist(struct sk_buff *skb)
543{
544 struct sk_buff *list;
545
fbb398a8 546 skb_walk_frags(skb, list)
1da177e4
LT
547 skb_get(list);
548}
549
d3836f21
ED
550static void skb_free_head(struct sk_buff *skb)
551{
552 if (skb->head_frag)
553 put_page(virt_to_head_page(skb->head));
554 else
555 kfree(skb->head);
556}
557
5bba1712 558static void skb_release_data(struct sk_buff *skb)
1da177e4 559{
ff04a771
ED
560 struct skb_shared_info *shinfo = skb_shinfo(skb);
561 int i;
1da177e4 562
ff04a771
ED
563 if (skb->cloned &&
564 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
565 &shinfo->dataref))
566 return;
a6686f2f 567
ff04a771
ED
568 for (i = 0; i < shinfo->nr_frags; i++)
569 __skb_frag_unref(&shinfo->frags[i]);
a6686f2f 570
ff04a771
ED
571 /*
572 * If skb buf is from userspace, we need to notify the caller
573 * the lower device DMA has done;
574 */
575 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
576 struct ubuf_info *uarg;
1da177e4 577
ff04a771
ED
578 uarg = shinfo->destructor_arg;
579 if (uarg->callback)
580 uarg->callback(uarg, true);
1da177e4 581 }
ff04a771
ED
582
583 if (shinfo->frag_list)
584 kfree_skb_list(shinfo->frag_list);
585
586 skb_free_head(skb);
1da177e4
LT
587}
588
589/*
590 * Free an skbuff by memory without cleaning the state.
591 */
2d4baff8 592static void kfree_skbmem(struct sk_buff *skb)
1da177e4 593{
d0bf4a9e 594 struct sk_buff_fclones *fclones;
d179cd12 595
d179cd12
DM
596 switch (skb->fclone) {
597 case SKB_FCLONE_UNAVAILABLE:
598 kmem_cache_free(skbuff_head_cache, skb);
6ffe75eb 599 return;
d179cd12
DM
600
601 case SKB_FCLONE_ORIG:
d0bf4a9e 602 fclones = container_of(skb, struct sk_buff_fclones, skb1);
d179cd12 603
6ffe75eb
ED
604 /* We usually free the clone (TX completion) before original skb
605 * This test would have no chance to be true for the clone,
606 * while here, branch prediction will be good.
d179cd12 607 */
6ffe75eb
ED
608 if (atomic_read(&fclones->fclone_ref) == 1)
609 goto fastpath;
610 break;
e7820e39 611
6ffe75eb
ED
612 default: /* SKB_FCLONE_CLONE */
613 fclones = container_of(skb, struct sk_buff_fclones, skb2);
d179cd12 614 break;
3ff50b79 615 }
6ffe75eb
ED
616 if (!atomic_dec_and_test(&fclones->fclone_ref))
617 return;
618fastpath:
619 kmem_cache_free(skbuff_fclone_cache, fclones);
1da177e4
LT
620}
621
04a4bb55 622static void skb_release_head_state(struct sk_buff *skb)
1da177e4 623{
adf30907 624 skb_dst_drop(skb);
1da177e4
LT
625#ifdef CONFIG_XFRM
626 secpath_put(skb->sp);
627#endif
9c2b3328
SH
628 if (skb->destructor) {
629 WARN_ON(in_irq());
1da177e4
LT
630 skb->destructor(skb);
631 }
a3bf7ae9 632#if IS_ENABLED(CONFIG_NF_CONNTRACK)
5f79e0f9 633 nf_conntrack_put(skb->nfct);
2fc72c7b 634#endif
1109a90c 635#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1da177e4
LT
636 nf_bridge_put(skb->nf_bridge);
637#endif
04a4bb55
LB
638}
639
640/* Free everything but the sk_buff shell. */
641static void skb_release_all(struct sk_buff *skb)
642{
643 skb_release_head_state(skb);
5e71d9d7 644 if (likely(skb->head))
0ebd0ac5 645 skb_release_data(skb);
2d4baff8
HX
646}
647
648/**
649 * __kfree_skb - private function
650 * @skb: buffer
651 *
652 * Free an sk_buff. Release anything attached to the buffer.
653 * Clean the state. This is an internal helper function. Users should
654 * always call kfree_skb
655 */
1da177e4 656
2d4baff8
HX
657void __kfree_skb(struct sk_buff *skb)
658{
659 skb_release_all(skb);
1da177e4
LT
660 kfree_skbmem(skb);
661}
b4ac530f 662EXPORT_SYMBOL(__kfree_skb);
1da177e4 663