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