]>
Commit | Line | Data |
---|---|---|
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> | |
50 | #include <linux/netdevice.h> | |
51 | #ifdef CONFIG_NET_CLS_ACT | |
52 | #include <net/pkt_sched.h> | |
53 | #endif | |
54 | #include <linux/string.h> | |
55 | #include <linux/skbuff.h> | |
9c55e01c | 56 | #include <linux/splice.h> |
1da177e4 LT |
57 | #include <linux/cache.h> |
58 | #include <linux/rtnetlink.h> | |
59 | #include <linux/init.h> | |
716ea3a7 | 60 | #include <linux/scatterlist.h> |
ac45f602 | 61 | #include <linux/errqueue.h> |
268bb0ce | 62 | #include <linux/prefetch.h> |
1da177e4 LT |
63 | |
64 | #include <net/protocol.h> | |
65 | #include <net/dst.h> | |
66 | #include <net/sock.h> | |
67 | #include <net/checksum.h> | |
68 | #include <net/xfrm.h> | |
69 | ||
70 | #include <asm/uaccess.h> | |
ad8d75ff | 71 | #include <trace/events/skb.h> |
51c56b00 | 72 | #include <linux/highmem.h> |
a1f8e7f7 | 73 | |
d7e8883c | 74 | struct kmem_cache *skbuff_head_cache __read_mostly; |
e18b890b | 75 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
1da177e4 | 76 | |
9c55e01c JA |
77 | static void sock_pipe_buf_release(struct pipe_inode_info *pipe, |
78 | struct pipe_buffer *buf) | |
79 | { | |
8b9d3728 | 80 | put_page(buf->page); |
9c55e01c JA |
81 | } |
82 | ||
83 | static void sock_pipe_buf_get(struct pipe_inode_info *pipe, | |
84 | struct pipe_buffer *buf) | |
85 | { | |
8b9d3728 | 86 | get_page(buf->page); |
9c55e01c JA |
87 | } |
88 | ||
89 | static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, | |
90 | struct pipe_buffer *buf) | |
91 | { | |
92 | return 1; | |
93 | } | |
94 | ||
95 | ||
96 | /* Pipe buffer operations for a socket. */ | |
28dfef8f | 97 | static const struct pipe_buf_operations sock_pipe_buf_ops = { |
9c55e01c JA |
98 | .can_merge = 0, |
99 | .map = generic_pipe_buf_map, | |
100 | .unmap = generic_pipe_buf_unmap, | |
101 | .confirm = generic_pipe_buf_confirm, | |
102 | .release = sock_pipe_buf_release, | |
103 | .steal = sock_pipe_buf_steal, | |
104 | .get = sock_pipe_buf_get, | |
105 | }; | |
106 | ||
1da177e4 | 107 | /** |
f05de73b JS |
108 | * skb_panic - private function for out-of-line support |
109 | * @skb: buffer | |
110 | * @sz: size | |
111 | * @addr: address | |
99d5851e | 112 | * @msg: skb_over_panic or skb_under_panic |
1da177e4 | 113 | * |
f05de73b JS |
114 | * Out-of-line support for skb_put() and skb_push(). |
115 | * Called via the wrapper skb_over_panic() or skb_under_panic(). | |
116 | * Keep out of line to prevent kernel bloat. | |
117 | * __builtin_return_address is not used because it is not always reliable. | |
1da177e4 | 118 | */ |
f05de73b | 119 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
99d5851e | 120 | const char msg[]) |
1da177e4 | 121 | { |
e005d193 | 122 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
99d5851e | 123 | msg, addr, skb->len, sz, skb->head, skb->data, |
e005d193 JP |
124 | (unsigned long)skb->tail, (unsigned long)skb->end, |
125 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
126 | BUG(); |
127 | } | |
128 | ||
f05de73b | 129 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
1da177e4 | 130 | { |
f05de73b | 131 | skb_panic(skb, sz, addr, __func__); |
1da177e4 LT |
132 | } |
133 | ||
f05de73b JS |
134 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
135 | { | |
136 | skb_panic(skb, sz, addr, __func__); | |
137 | } | |
c93bdd0e MG |
138 | |
139 | /* | |
140 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells | |
141 | * the caller if emergency pfmemalloc reserves are being used. If it is and | |
142 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves | |
143 | * may be used. Otherwise, the packet data may be discarded until enough | |
144 | * memory is free | |
145 | */ | |
146 | #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ | |
147 | __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) | |
61c5e88a | 148 | |
149 | static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, | |
150 | unsigned long ip, bool *pfmemalloc) | |
c93bdd0e MG |
151 | { |
152 | void *obj; | |
153 | bool ret_pfmemalloc = false; | |
154 | ||
155 | /* | |
156 | * Try a regular allocation, when that fails and we're not entitled | |
157 | * to the reserves, fail. | |
158 | */ | |
159 | obj = kmalloc_node_track_caller(size, | |
160 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, | |
161 | node); | |
162 | if (obj || !(gfp_pfmemalloc_allowed(flags))) | |
163 | goto out; | |
164 | ||
165 | /* Try again but now we are using pfmemalloc reserves */ | |
166 | ret_pfmemalloc = true; | |
167 | obj = kmalloc_node_track_caller(size, flags, node); | |
168 | ||
169 | out: | |
170 | if (pfmemalloc) | |
171 | *pfmemalloc = ret_pfmemalloc; | |
172 | ||
173 | return obj; | |
174 | } | |
175 | ||
1da177e4 LT |
176 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
177 | * 'private' fields and also do memory statistics to find all the | |
178 | * [BEEP] leaks. | |
179 | * | |
180 | */ | |
181 | ||
0ebd0ac5 PM |
182 | struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) |
183 | { | |
184 | struct sk_buff *skb; | |
185 | ||
186 | /* Get the HEAD */ | |
187 | skb = kmem_cache_alloc_node(skbuff_head_cache, | |
188 | gfp_mask & ~__GFP_DMA, node); | |
189 | if (!skb) | |
190 | goto out; | |
191 | ||
192 | /* | |
193 | * Only clear those fields we need to clear, not those that we will | |
194 | * actually initialise below. Hence, don't put any more fields after | |
195 | * the tail pointer in struct sk_buff! | |
196 | */ | |
197 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
198 | skb->data = NULL; | |
199 | skb->truesize = sizeof(struct sk_buff); | |
200 | atomic_set(&skb->users, 1); | |
201 | ||
202 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | |
203 | skb->mac_header = ~0U; | |
204 | #endif | |
205 | out: | |
206 | return skb; | |
207 | } | |
208 | ||
1da177e4 | 209 | /** |
d179cd12 | 210 | * __alloc_skb - allocate a network buffer |
1da177e4 LT |
211 | * @size: size to allocate |
212 | * @gfp_mask: allocation mask | |
c93bdd0e MG |
213 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
214 | * instead of head cache and allocate a cloned (child) skb. | |
215 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for | |
216 | * allocations in case the data is required for writeback | |
b30973f8 | 217 | * @node: numa node to allocate memory on |
1da177e4 LT |
218 | * |
219 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
94b6042c BH |
220 | * tail room of at least size bytes. The object has a reference count |
221 | * of one. The return is the buffer. On a failure the return is %NULL. | |
1da177e4 LT |
222 | * |
223 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
224 | * %GFP_ATOMIC. | |
225 | */ | |
dd0fc66f | 226 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
c93bdd0e | 227 | int flags, int node) |
1da177e4 | 228 | { |
e18b890b | 229 | struct kmem_cache *cache; |
4947d3ef | 230 | struct skb_shared_info *shinfo; |
1da177e4 LT |
231 | struct sk_buff *skb; |
232 | u8 *data; | |
c93bdd0e | 233 | bool pfmemalloc; |
1da177e4 | 234 | |
c93bdd0e MG |
235 | cache = (flags & SKB_ALLOC_FCLONE) |
236 | ? skbuff_fclone_cache : skbuff_head_cache; | |
237 | ||
238 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) | |
239 | gfp_mask |= __GFP_MEMALLOC; | |
8798b3fb | 240 | |
1da177e4 | 241 | /* Get the HEAD */ |
b30973f8 | 242 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
1da177e4 LT |
243 | if (!skb) |
244 | goto out; | |
ec7d2f2c | 245 | prefetchw(skb); |
1da177e4 | 246 | |
87fb4b7b ED |
247 | /* We do our best to align skb_shared_info on a separate cache |
248 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives | |
249 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. | |
250 | * Both skb->head and skb_shared_info are cache line aligned. | |
251 | */ | |
bc417e30 | 252 | size = SKB_DATA_ALIGN(size); |
87fb4b7b | 253 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
c93bdd0e | 254 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
1da177e4 LT |
255 | if (!data) |
256 | goto nodata; | |
87fb4b7b ED |
257 | /* kmalloc(size) might give us more room than requested. |
258 | * Put skb_shared_info exactly at the end of allocated zone, | |
259 | * to allow max possible filling before reallocation. | |
260 | */ | |
261 | size = SKB_WITH_OVERHEAD(ksize(data)); | |
ec7d2f2c | 262 | prefetchw(data + size); |
1da177e4 | 263 | |
ca0605a7 | 264 | /* |
c8005785 JB |
265 | * Only clear those fields we need to clear, not those that we will |
266 | * actually initialise below. Hence, don't put any more fields after | |
267 | * the tail pointer in struct sk_buff! | |
ca0605a7 ACM |
268 | */ |
269 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
87fb4b7b ED |
270 | /* Account for allocated memory : skb + skb->head */ |
271 | skb->truesize = SKB_TRUESIZE(size); | |
c93bdd0e | 272 | skb->pfmemalloc = pfmemalloc; |
1da177e4 LT |
273 | atomic_set(&skb->users, 1); |
274 | skb->head = data; | |
275 | skb->data = data; | |
27a884dc | 276 | skb_reset_tail_pointer(skb); |
4305b541 | 277 | skb->end = skb->tail + size; |
19633e12 SH |
278 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
279 | skb->mac_header = ~0U; | |
fda55eca | 280 | skb->transport_header = ~0U; |
19633e12 SH |
281 | #endif |
282 | ||
4947d3ef BL |
283 | /* make sure we initialize shinfo sequentially */ |
284 | shinfo = skb_shinfo(skb); | |
ec7d2f2c | 285 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
4947d3ef | 286 | atomic_set(&shinfo->dataref, 1); |
c2aa3665 | 287 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
4947d3ef | 288 | |
c93bdd0e | 289 | if (flags & SKB_ALLOC_FCLONE) { |
d179cd12 DM |
290 | struct sk_buff *child = skb + 1; |
291 | atomic_t *fclone_ref = (atomic_t *) (child + 1); | |
1da177e4 | 292 | |
fe55f6d5 VN |
293 | kmemcheck_annotate_bitfield(child, flags1); |
294 | kmemcheck_annotate_bitfield(child, flags2); | |
d179cd12 DM |
295 | skb->fclone = SKB_FCLONE_ORIG; |
296 | atomic_set(fclone_ref, 1); | |
297 | ||
298 | child->fclone = SKB_FCLONE_UNAVAILABLE; | |
c93bdd0e | 299 | child->pfmemalloc = pfmemalloc; |
d179cd12 | 300 | } |
1da177e4 LT |
301 | out: |
302 | return skb; | |
303 | nodata: | |
8798b3fb | 304 | kmem_cache_free(cache, skb); |
1da177e4 LT |
305 | skb = NULL; |
306 | goto out; | |
1da177e4 | 307 | } |
b4ac530f | 308 | EXPORT_SYMBOL(__alloc_skb); |
1da177e4 | 309 | |
b2b5ce9d ED |
310 | /** |
311 | * build_skb - build a network buffer | |
312 | * @data: data buffer provided by caller | |
d3836f21 | 313 | * @frag_size: size of fragment, or 0 if head was kmalloced |
b2b5ce9d ED |
314 | * |
315 | * Allocate a new &sk_buff. Caller provides space holding head and | |
316 | * skb_shared_info. @data must have been allocated by kmalloc() | |
317 | * The return is the new skb buffer. | |
318 | * On a failure the return is %NULL, and @data is not freed. | |
319 | * Notes : | |
320 | * Before IO, driver allocates only data buffer where NIC put incoming frame | |
321 | * Driver should add room at head (NET_SKB_PAD) and | |
322 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) | |
323 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it | |
324 | * before giving packet to stack. | |
325 | * RX rings only contains data buffers, not full skbs. | |
326 | */ | |
d3836f21 | 327 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
b2b5ce9d ED |
328 | { |
329 | struct skb_shared_info *shinfo; | |
330 | struct sk_buff *skb; | |
d3836f21 | 331 | unsigned int size = frag_size ? : ksize(data); |
b2b5ce9d ED |
332 | |
333 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); | |
334 | if (!skb) | |
335 | return NULL; | |
336 | ||
d3836f21 | 337 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
b2b5ce9d ED |
338 | |
339 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
340 | skb->truesize = SKB_TRUESIZE(size); | |
d3836f21 | 341 | skb->head_frag = frag_size != 0; |
b2b5ce9d ED |
342 | atomic_set(&skb->users, 1); |
343 | skb->head = data; | |
344 | skb->data = data; | |
345 | skb_reset_tail_pointer(skb); | |
346 | skb->end = skb->tail + size; | |
347 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | |
348 | skb->mac_header = ~0U; | |
fda55eca | 349 | skb->transport_header = ~0U; |
b2b5ce9d ED |
350 | #endif |
351 | ||
352 | /* make sure we initialize shinfo sequentially */ | |
353 | shinfo = skb_shinfo(skb); | |
354 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); | |
355 | atomic_set(&shinfo->dataref, 1); | |
356 | kmemcheck_annotate_variable(shinfo->destructor_arg); | |
357 | ||
358 | return skb; | |
359 | } | |
360 | EXPORT_SYMBOL(build_skb); | |
361 | ||
a1c7fff7 | 362 | struct netdev_alloc_cache { |
69b08f62 ED |
363 | struct page_frag frag; |
364 | /* we maintain a pagecount bias, so that we dont dirty cache line | |
365 | * containing page->_count every time we allocate a fragment. | |
366 | */ | |
367 | unsigned int pagecnt_bias; | |
a1c7fff7 ED |
368 | }; |
369 | static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); | |
370 | ||
c93bdd0e | 371 | static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
6f532612 ED |
372 | { |
373 | struct netdev_alloc_cache *nc; | |
374 | void *data = NULL; | |
69b08f62 | 375 | int order; |
6f532612 ED |
376 | unsigned long flags; |
377 | ||
378 | local_irq_save(flags); | |
379 | nc = &__get_cpu_var(netdev_alloc_cache); | |
69b08f62 | 380 | if (unlikely(!nc->frag.page)) { |
6f532612 | 381 | refill: |
69b08f62 ED |
382 | for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) { |
383 | gfp_t gfp = gfp_mask; | |
384 | ||
385 | if (order) | |
386 | gfp |= __GFP_COMP | __GFP_NOWARN; | |
387 | nc->frag.page = alloc_pages(gfp, order); | |
388 | if (likely(nc->frag.page)) | |
389 | break; | |
390 | if (--order < 0) | |
391 | goto end; | |
392 | } | |
393 | nc->frag.size = PAGE_SIZE << order; | |
540eb7bf | 394 | recycle: |
69b08f62 ED |
395 | atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS); |
396 | nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; | |
397 | nc->frag.offset = 0; | |
6f532612 | 398 | } |
540eb7bf | 399 | |
69b08f62 | 400 | if (nc->frag.offset + fragsz > nc->frag.size) { |
540eb7bf | 401 | /* avoid unnecessary locked operations if possible */ |
69b08f62 ED |
402 | if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) || |
403 | atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count)) | |
540eb7bf AD |
404 | goto recycle; |
405 | goto refill; | |
6f532612 | 406 | } |
540eb7bf | 407 | |
69b08f62 ED |
408 | data = page_address(nc->frag.page) + nc->frag.offset; |
409 | nc->frag.offset += fragsz; | |
540eb7bf AD |
410 | nc->pagecnt_bias--; |
411 | end: | |
6f532612 ED |
412 | local_irq_restore(flags); |
413 | return data; | |
414 | } | |
c93bdd0e MG |
415 | |
416 | /** | |
417 | * netdev_alloc_frag - allocate a page fragment | |
418 | * @fragsz: fragment size | |
419 | * | |
420 | * Allocates a frag from a page for receive buffer. | |
421 | * Uses GFP_ATOMIC allocations. | |
422 | */ | |
423 | void *netdev_alloc_frag(unsigned int fragsz) | |
424 | { | |
425 | return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); | |
426 | } | |
6f532612 ED |
427 | EXPORT_SYMBOL(netdev_alloc_frag); |
428 | ||
8af27456 CH |
429 | /** |
430 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device | |
431 | * @dev: network device to receive on | |
432 | * @length: length to allocate | |
433 | * @gfp_mask: get_free_pages mask, passed to alloc_skb | |
434 | * | |
435 | * Allocate a new &sk_buff and assign it a usage count of one. The | |
436 | * buffer has unspecified headroom built in. Users should allocate | |
437 | * the headroom they think they need without accounting for the | |
438 | * built in space. The built in space is used for optimisations. | |
439 | * | |
440 | * %NULL is returned if there is no free memory. | |
441 | */ | |
442 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, | |
6f532612 | 443 | unsigned int length, gfp_t gfp_mask) |
8af27456 | 444 | { |
6f532612 | 445 | struct sk_buff *skb = NULL; |
a1c7fff7 ED |
446 | unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + |
447 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
448 | ||
310e158c | 449 | if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { |
c93bdd0e MG |
450 | void *data; |
451 | ||
452 | if (sk_memalloc_socks()) | |
453 | gfp_mask |= __GFP_MEMALLOC; | |
454 | ||
455 | data = __netdev_alloc_frag(fragsz, gfp_mask); | |
a1c7fff7 | 456 | |
6f532612 ED |
457 | if (likely(data)) { |
458 | skb = build_skb(data, fragsz); | |
459 | if (unlikely(!skb)) | |
460 | put_page(virt_to_head_page(data)); | |
a1c7fff7 | 461 | } |
a1c7fff7 | 462 | } else { |
c93bdd0e MG |
463 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, |
464 | SKB_ALLOC_RX, NUMA_NO_NODE); | |
a1c7fff7 | 465 | } |
7b2e497a | 466 | if (likely(skb)) { |
8af27456 | 467 | skb_reserve(skb, NET_SKB_PAD); |
7b2e497a CH |
468 | skb->dev = dev; |
469 | } | |
8af27456 CH |
470 | return skb; |
471 | } | |
b4ac530f | 472 | EXPORT_SYMBOL(__netdev_alloc_skb); |
1da177e4 | 473 | |
654bed16 | 474 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
50269e19 | 475 | int size, unsigned int truesize) |
654bed16 PZ |
476 | { |
477 | skb_fill_page_desc(skb, i, page, off, size); | |
478 | skb->len += size; | |
479 | skb->data_len += size; | |
50269e19 | 480 | skb->truesize += truesize; |
654bed16 PZ |
481 | } |
482 | EXPORT_SYMBOL(skb_add_rx_frag); | |
483 | ||
27b437c8 | 484 | static void skb_drop_list(struct sk_buff **listp) |
1da177e4 | 485 | { |
27b437c8 | 486 | struct sk_buff *list = *listp; |
1da177e4 | 487 | |
27b437c8 | 488 | *listp = NULL; |
1da177e4 LT |
489 | |
490 | do { | |
491 | struct sk_buff *this = list; | |
492 | list = list->next; | |
493 | kfree_skb(this); | |
494 | } while (list); | |
495 | } | |
496 | ||
27b437c8 HX |
497 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
498 | { | |
499 | skb_drop_list(&skb_shinfo(skb)->frag_list); | |
500 | } | |
501 | ||
1da177e4 LT |
502 | static void skb_clone_fraglist(struct sk_buff *skb) |
503 | { | |
504 | struct sk_buff *list; | |
505 | ||
fbb398a8 | 506 | skb_walk_frags(skb, list) |
1da177e4 LT |
507 | skb_get(list); |
508 | } | |
509 | ||
d3836f21 ED |
510 | static void skb_free_head(struct sk_buff *skb) |
511 | { | |
512 | if (skb->head_frag) | |
513 | put_page(virt_to_head_page(skb->head)); | |
514 | else | |
515 | kfree(skb->head); | |
516 | } | |
517 | ||
5bba1712 | 518 | static void skb_release_data(struct sk_buff *skb) |
1da177e4 LT |
519 | { |
520 | if (!skb->cloned || | |
521 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
522 | &skb_shinfo(skb)->dataref)) { | |
523 | if (skb_shinfo(skb)->nr_frags) { | |
524 | int i; | |
525 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
ea2ab693 | 526 | skb_frag_unref(skb, i); |
1da177e4 LT |
527 | } |
528 | ||
a6686f2f SM |
529 | /* |
530 | * If skb buf is from userspace, we need to notify the caller | |
531 | * the lower device DMA has done; | |
532 | */ | |
533 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { | |
534 | struct ubuf_info *uarg; | |
535 | ||
536 | uarg = skb_shinfo(skb)->destructor_arg; | |
537 | if (uarg->callback) | |
e19d6763 | 538 | uarg->callback(uarg, true); |
a6686f2f SM |
539 | } |
540 | ||
21dc3301 | 541 | if (skb_has_frag_list(skb)) |
1da177e4 LT |
542 | skb_drop_fraglist(skb); |
543 | ||
d3836f21 | 544 | skb_free_head(skb); |
1da177e4 LT |
545 | } |
546 | } | |
547 | ||
548 | /* | |
549 | * Free an skbuff by memory without cleaning the state. | |
550 | */ | |
2d4baff8 | 551 | static void kfree_skbmem(struct sk_buff *skb) |
1da177e4 | 552 | { |
d179cd12 DM |
553 | struct sk_buff *other; |
554 | atomic_t *fclone_ref; | |
555 | ||
d179cd12 DM |
556 | switch (skb->fclone) { |
557 | case SKB_FCLONE_UNAVAILABLE: | |
558 | kmem_cache_free(skbuff_head_cache, skb); | |
559 | break; | |
560 | ||
561 | case SKB_FCLONE_ORIG: | |
562 | fclone_ref = (atomic_t *) (skb + 2); | |
563 | if (atomic_dec_and_test(fclone_ref)) | |
564 | kmem_cache_free(skbuff_fclone_cache, skb); | |
565 | break; | |
566 | ||
567 | case SKB_FCLONE_CLONE: | |
568 | fclone_ref = (atomic_t *) (skb + 1); | |
569 | other = skb - 1; | |
570 | ||
571 | /* The clone portion is available for | |
572 | * fast-cloning again. | |
573 | */ | |
574 | skb->fclone = SKB_FCLONE_UNAVAILABLE; | |
575 | ||
576 | if (atomic_dec_and_test(fclone_ref)) | |
577 | kmem_cache_free(skbuff_fclone_cache, other); | |
578 | break; | |
3ff50b79 | 579 | } |
1da177e4 LT |
580 | } |
581 | ||
04a4bb55 | 582 | static void skb_release_head_state(struct sk_buff *skb) |
1da177e4 | 583 | { |
adf30907 | 584 | skb_dst_drop(skb); |
1da177e4 LT |
585 | #ifdef CONFIG_XFRM |
586 | secpath_put(skb->sp); | |
587 | #endif | |
9c2b3328 SH |
588 | if (skb->destructor) { |
589 | WARN_ON(in_irq()); | |
1da177e4 LT |
590 | skb->destructor(skb); |
591 | } | |
a3bf7ae9 | 592 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
5f79e0f9 | 593 | nf_conntrack_put(skb->nfct); |
2fc72c7b KK |
594 | #endif |
595 | #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED | |
9fb9cbb1 YK |
596 | nf_conntrack_put_reasm(skb->nfct_reasm); |
597 | #endif | |
1da177e4 LT |
598 | #ifdef CONFIG_BRIDGE_NETFILTER |
599 | nf_bridge_put(skb->nf_bridge); | |
600 | #endif | |
1da177e4 LT |
601 | /* XXX: IS this still necessary? - JHS */ |
602 | #ifdef CONFIG_NET_SCHED | |
603 | skb->tc_index = 0; | |
604 | #ifdef CONFIG_NET_CLS_ACT | |
605 | skb->tc_verd = 0; | |
1da177e4 LT |
606 | #endif |
607 | #endif | |
04a4bb55 LB |
608 | } |
609 | ||
610 | /* Free everything but the sk_buff shell. */ | |
611 | static void skb_release_all(struct sk_buff *skb) | |
612 | { | |
613 | skb_release_head_state(skb); | |
0ebd0ac5 PM |
614 | if (likely(skb->data)) |
615 | skb_release_data(skb); | |
2d4baff8 HX |
616 | } |
617 | ||
618 | /** | |
619 | * __kfree_skb - private function | |
620 | * @skb: buffer | |
621 | * | |
622 | * Free an sk_buff. Release anything attached to the buffer. | |
623 | * Clean the state. This is an internal helper function. Users should | |
624 | * always call kfree_skb | |
625 | */ | |
1da177e4 | 626 | |
2d4baff8 HX |
627 | void __kfree_skb(struct sk_buff *skb) |
628 | { | |
629 | skb_release_all(skb); | |
1da177e4 LT |
630 | kfree_skbmem(skb); |
631 | } | |
b4ac530f | 632 | EXPORT_SYMBOL(__kfree_skb); |
1da177e4 | 633 | |