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