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