ACPI / SBS: Add 5 us delay to fix SBS hangs on MacBook
[linux-2.6-block.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
ED
282/**
283 * build_skb - build a network buffer
284 * @data: data buffer provided by caller
d3836f21 285 * @frag_size: size of fragment, or 0 if head was kmalloced
b2b5ce9d
ED
286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
deceb4c0
FF
288 * skb_shared_info. @data must have been allocated by kmalloc() only if
289 * @frag_size is 0, otherwise data should come from the page allocator.
b2b5ce9d
ED
290 * The return is the new skb buffer.
291 * On a failure the return is %NULL, and @data is not freed.
292 * Notes :
293 * Before IO, driver allocates only data buffer where NIC put incoming frame
294 * Driver should add room at head (NET_SKB_PAD) and
295 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
296 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
297 * before giving packet to stack.
298 * RX rings only contains data buffers, not full skbs.
299 */
d3836f21 300struct sk_buff *build_skb(void *data, unsigned int frag_size)
b2b5ce9d
ED
301{
302 struct skb_shared_info *shinfo;
303 struct sk_buff *skb;
d3836f21 304 unsigned int size = frag_size ? : ksize(data);
b2b5ce9d
ED
305
306 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
307 if (!skb)
308 return NULL;
309
d3836f21 310 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
b2b5ce9d
ED
311
312 memset(skb, 0, offsetof(struct sk_buff, tail));
313 skb->truesize = SKB_TRUESIZE(size);
d3836f21 314 skb->head_frag = frag_size != 0;
b2b5ce9d
ED
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}
331EXPORT_SYMBOL(build_skb);
332
a1c7fff7 333struct netdev_alloc_cache {
69b08f62
ED
334 struct page_frag frag;
335 /* we maintain a pagecount bias, so that we dont dirty cache line
336 * containing page->_count every time we allocate a fragment.
337 */
338 unsigned int pagecnt_bias;
a1c7fff7
ED
339};
340static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
ffde7328 341static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
a1c7fff7 342
ffde7328
AD
343static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
344 gfp_t gfp_mask)
6f532612 345{
ffde7328
AD
346 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
347 struct page *page = NULL;
348 gfp_t gfp = gfp_mask;
349
350 if (order) {
351 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
352 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
353 nc->frag.size = PAGE_SIZE << (page ? order : 0);
354 }
6f532612 355
ffde7328
AD
356 if (unlikely(!page))
357 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
358
359 nc->frag.page = page;
360
361 return page;
362}
363
364static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
365 unsigned int fragsz, gfp_t gfp_mask)
366{
367 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
368 struct page *page = nc->frag.page;
369 unsigned int size;
370 int offset;
371
372 if (unlikely(!page)) {
6f532612 373refill:
ffde7328
AD
374 page = __page_frag_refill(nc, gfp_mask);
375 if (!page)
376 return NULL;
377
378 /* if size can vary use frag.size else just use PAGE_SIZE */
379 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
380
4c450583
ED
381 /* Even if we own the page, we do not use atomic_set().
382 * This would break get_page_unless_zero() users.
383 */
ffde7328
AD
384 atomic_add(size - 1, &page->_count);
385
386 /* reset page count bias and offset to start of new frag */
387 nc->pagecnt_bias = size;
388 nc->frag.offset = size;
6f532612 389 }
540eb7bf 390
ffde7328
AD
391 offset = nc->frag.offset - fragsz;
392 if (unlikely(offset < 0)) {
393 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
394 goto refill;
395
396 /* if size can vary use frag.size else just use PAGE_SIZE */
397 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
398
399 /* OK, page count is 0, we can safely set it */
400 atomic_set(&page->_count, size);
401
402 /* reset page count bias and offset to start of new frag */
403 nc->pagecnt_bias = size;
404 offset = size - fragsz;
6f532612 405 }
540eb7bf 406
540eb7bf 407 nc->pagecnt_bias--;
ffde7328
AD
408 nc->frag.offset = offset;
409
410 return page_address(page) + offset;
411}
412
413static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
414{
415 unsigned long flags;
416 void *data;
417
418 local_irq_save(flags);
419 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
6f532612
ED
420 local_irq_restore(flags);
421 return data;
422}
c93bdd0e
MG
423
424/**
425 * netdev_alloc_frag - allocate a page fragment
426 * @fragsz: fragment size
427 *
428 * Allocates a frag from a page for receive buffer.
429 * Uses GFP_ATOMIC allocations.
430 */
431void *netdev_alloc_frag(unsigned int fragsz)
432{
433 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
434}
6f532612
ED
435EXPORT_SYMBOL(netdev_alloc_frag);
436
ffde7328
AD
437static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
438{
439 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
440}
441
442void *napi_alloc_frag(unsigned int fragsz)
443{
444 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
445}
446EXPORT_SYMBOL(napi_alloc_frag);
447
8af27456 448/**
fd11a83d 449 * __alloc_rx_skb - allocate an skbuff for rx
8af27456
CH
450 * @length: length to allocate
451 * @gfp_mask: get_free_pages mask, passed to alloc_skb
fd11a83d
AD
452 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
453 * allocations in case we have to fallback to __alloc_skb()
454 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
455 * from napi_cache instead of netdev_cache.
8af27456
CH
456 *
457 * Allocate a new &sk_buff and assign it a usage count of one. The
458 * buffer has unspecified headroom built in. Users should allocate
459 * the headroom they think they need without accounting for the
460 * built in space. The built in space is used for optimisations.
461 *
462 * %NULL is returned if there is no free memory.
463 */
fd11a83d
AD
464static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
465 int flags)
8af27456 466{
6f532612 467 struct sk_buff *skb = NULL;
fd11a83d 468 unsigned int fragsz = SKB_DATA_ALIGN(length) +
a1c7fff7
ED
469 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
470
310e158c 471 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
c93bdd0e
MG
472 void *data;
473
474 if (sk_memalloc_socks())
475 gfp_mask |= __GFP_MEMALLOC;
476
fd11a83d
AD
477 data = (flags & SKB_ALLOC_NAPI) ?
478 __napi_alloc_frag(fragsz, gfp_mask) :
479 __netdev_alloc_frag(fragsz, gfp_mask);
a1c7fff7 480
6f532612
ED
481 if (likely(data)) {
482 skb = build_skb(data, fragsz);
483 if (unlikely(!skb))
484 put_page(virt_to_head_page(data));
a1c7fff7 485 }
a1c7fff7 486 } else {
fd11a83d 487 skb = __alloc_skb(length, gfp_mask,
c93bdd0e 488 SKB_ALLOC_RX, NUMA_NO_NODE);
a1c7fff7 489 }
fd11a83d
AD
490 return skb;
491}
492
493/**
494 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
495 * @dev: network device to receive on
496 * @length: length to allocate
497 * @gfp_mask: get_free_pages mask, passed to alloc_skb
498 *
499 * Allocate a new &sk_buff and assign it a usage count of one. The
500 * buffer has NET_SKB_PAD headroom built in. Users should allocate
501 * the headroom they think they need without accounting for the
502 * built in space. The built in space is used for optimisations.
503 *
504 * %NULL is returned if there is no free memory.
505 */
506struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
507 unsigned int length, gfp_t gfp_mask)
508{
509 struct sk_buff *skb;
510
511 length += NET_SKB_PAD;
512 skb = __alloc_rx_skb(length, gfp_mask, 0);
513
7b2e497a 514 if (likely(skb)) {
8af27456 515 skb_reserve(skb, NET_SKB_PAD);
7b2e497a
CH
516 skb->dev = dev;
517 }
fd11a83d 518
8af27456
CH
519 return skb;
520}
b4ac530f 521EXPORT_SYMBOL(__netdev_alloc_skb);
1da177e4 522
fd11a83d
AD
523/**
524 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
525 * @napi: napi instance this buffer was allocated for
526 * @length: length to allocate
527 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
528 *
529 * Allocate a new sk_buff for use in NAPI receive. This buffer will
530 * attempt to allocate the head from a special reserved region used
531 * only for NAPI Rx allocation. By doing this we can save several
532 * CPU cycles by avoiding having to disable and re-enable IRQs.
533 *
534 * %NULL is returned if there is no free memory.
535 */
536struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
537 unsigned int length, gfp_t gfp_mask)
538{
539 struct sk_buff *skb;
540
541 length += NET_SKB_PAD + NET_IP_ALIGN;
542 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
543
544 if (likely(skb)) {
545 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
546 skb->dev = napi->dev;
547 }
548
549 return skb;
550}
551EXPORT_SYMBOL(__napi_alloc_skb);
552
654bed16 553void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
50269e19 554 int size, unsigned int truesize)
654bed16
PZ
555{
556 skb_fill_page_desc(skb, i, page, off, size);
557 skb->len += size;
558 skb->data_len += size;
50269e19 559 skb->truesize += truesize;
654bed16
PZ
560}
561EXPORT_SYMBOL(skb_add_rx_frag);
562
f8e617e1
JW
563void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
564 unsigned int truesize)
565{
566 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
567
568 skb_frag_size_add(frag, size);
569 skb->len += size;
570 skb->data_len += size;
571 skb->truesize += truesize;
572}
573EXPORT_SYMBOL(skb_coalesce_rx_frag);
574
27b437c8 575static void skb_drop_list(struct sk_buff **listp)
1da177e4 576{
bd8a7036 577 kfree_skb_list(*listp);
27b437c8 578 *listp = NULL;
1da177e4
LT
579}
580
27b437c8
HX
581static inline void skb_drop_fraglist(struct sk_buff *skb)
582{
583 skb_drop_list(&skb_shinfo(skb)->frag_list);
584}
585
1da177e4
LT
586static void skb_clone_fraglist(struct sk_buff *skb)
587{
588 struct sk_buff *list;
589
fbb398a8 590 skb_walk_frags(skb, list)
1da177e4
LT
591 skb_get(list);
592}
593
d3836f21
ED
594static void skb_free_head(struct sk_buff *skb)
595{
596 if (skb->head_frag)
597 put_page(virt_to_head_page(skb->head));
598 else
599 kfree(skb->head);
600}
601
5bba1712 602static void skb_release_data(struct sk_buff *skb)
1da177e4 603{
ff04a771
ED
604 struct skb_shared_info *shinfo = skb_shinfo(skb);
605 int i;
1da177e4 606
ff04a771
ED
607 if (skb->cloned &&
608 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
609 &shinfo->dataref))
610 return;
a6686f2f 611
ff04a771
ED
612 for (i = 0; i < shinfo->nr_frags; i++)
613 __skb_frag_unref(&shinfo->frags[i]);
a6686f2f 614
ff04a771
ED
615 /*
616 * If skb buf is from userspace, we need to notify the caller
617 * the lower device DMA has done;
618 */
619 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
620 struct ubuf_info *uarg;
1da177e4 621
ff04a771
ED
622 uarg = shinfo->destructor_arg;
623 if (uarg->callback)
624 uarg->callback(uarg, true);
1da177e4 625 }
ff04a771
ED
626
627 if (shinfo->frag_list)
628 kfree_skb_list(shinfo->frag_list);
629
630 skb_free_head(skb);
1da177e4
LT
631}
632
633/*
634 * Free an skbuff by memory without cleaning the state.
635 */
2d4baff8 636static void kfree_skbmem(struct sk_buff *skb)
1da177e4 637{
d0bf4a9e 638 struct sk_buff_fclones *fclones;
d179cd12 639
d179cd12
DM
640 switch (skb->fclone) {
641 case SKB_FCLONE_UNAVAILABLE:
642 kmem_cache_free(skbuff_head_cache, skb);
6ffe75eb 643 return;
d179cd12
DM
644
645 case SKB_FCLONE_ORIG:
d0bf4a9e 646 fclones = container_of(skb, struct sk_buff_fclones, skb1);
d179cd12 647
6ffe75eb
ED
648 /* We usually free the clone (TX completion) before original skb
649 * This test would have no chance to be true for the clone,
650 * while here, branch prediction will be good.
d179cd12 651 */
6ffe75eb
ED
652 if (atomic_read(&fclones->fclone_ref) == 1)
653 goto fastpath;
654 break;
e7820e39 655
6ffe75eb
ED
656 default: /* SKB_FCLONE_CLONE */
657 fclones = container_of(skb, struct sk_buff_fclones, skb2);
d179cd12 658 break;
3ff50b79 659 }
6ffe75eb
ED
660 if (!atomic_dec_and_test(&fclones->fclone_ref))
661 return;
662fastpath:
663 kmem_cache_free(skbuff_fclone_cache, fclones);
1da177e4
LT
664}
665
04a4bb55 666static void skb_release_head_state(struct sk_buff *skb)
1da177e4 667{
adf30907 668 skb_dst_drop(skb);
1da177e4
LT
669#ifdef CONFIG_XFRM
670 secpath_put(skb->sp);
671#endif
9c2b3328
SH
672 if (skb->destructor) {
673 WARN_ON(in_irq());
1da177e4
LT
674 skb->destructor(skb);
675 }
a3bf7ae9 676#if IS_ENABLED(CONFIG_NF_CONNTRACK)
5f79e0f9 677 nf_conntrack_put(skb->nfct);
2fc72c7b 678#endif
1109a90c 679#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1da177e4
LT
680 nf_bridge_put(skb->nf_bridge);
681#endif
04a4bb55
LB
682}
683
684/* Free everything but the sk_buff shell. */
685static void skb_release_all(struct sk_buff *skb)
686{
687 skb_release_head_state(skb);
5e71d9d7 688 if (likely(skb->head))
0ebd0ac5 689 skb_release_data(skb);
2d4baff8
HX
690}
691
692/**
693 * __kfree_skb - private function
694 * @skb: buffer
695 *
696 * Free an sk_buff. Release anything attached to the buffer.
697 * Clean the state. This is an internal helper function. Users should
698 * always call kfree_skb
699 */
1da177e4 700
2d4baff8
HX
701void __kfree_skb(struct sk_buff *skb)
702{
703 skb_release_all(skb);
1da177e4
LT
704 kfree_skbmem(skb);
705}
b4ac530f 706EXPORT_SYMBOL(__kfree_skb);
1da177e4 707