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> | |
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 | 80 | struct kmem_cache *skbuff_head_cache __read_mostly; |
e18b890b | 81 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
5f74f82e HWR |
82 | int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; |
83 | EXPORT_SYMBOL(sysctl_max_skb_frags); | |
1da177e4 | 84 | |
1da177e4 | 85 | /** |
f05de73b JS |
86 | * skb_panic - private function for out-of-line support |
87 | * @skb: buffer | |
88 | * @sz: size | |
89 | * @addr: address | |
99d5851e | 90 | * @msg: skb_over_panic or skb_under_panic |
1da177e4 | 91 | * |
f05de73b JS |
92 | * Out-of-line support for skb_put() and skb_push(). |
93 | * Called via the wrapper skb_over_panic() or skb_under_panic(). | |
94 | * Keep out of line to prevent kernel bloat. | |
95 | * __builtin_return_address is not used because it is not always reliable. | |
1da177e4 | 96 | */ |
f05de73b | 97 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
99d5851e | 98 | const char msg[]) |
1da177e4 | 99 | { |
e005d193 | 100 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
99d5851e | 101 | msg, addr, skb->len, sz, skb->head, skb->data, |
e005d193 JP |
102 | (unsigned long)skb->tail, (unsigned long)skb->end, |
103 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
104 | BUG(); |
105 | } | |
106 | ||
f05de73b | 107 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
1da177e4 | 108 | { |
f05de73b | 109 | skb_panic(skb, sz, addr, __func__); |
1da177e4 LT |
110 | } |
111 | ||
f05de73b JS |
112 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
113 | { | |
114 | skb_panic(skb, sz, addr, __func__); | |
115 | } | |
c93bdd0e MG |
116 | |
117 | /* | |
118 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells | |
119 | * the caller if emergency pfmemalloc reserves are being used. If it is and | |
120 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves | |
121 | * may be used. Otherwise, the packet data may be discarded until enough | |
122 | * memory is free | |
123 | */ | |
124 | #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ | |
125 | __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) | |
61c5e88a | 126 | |
127 | static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, | |
128 | unsigned long ip, bool *pfmemalloc) | |
c93bdd0e MG |
129 | { |
130 | void *obj; | |
131 | bool ret_pfmemalloc = false; | |
132 | ||
133 | /* | |
134 | * Try a regular allocation, when that fails and we're not entitled | |
135 | * to the reserves, fail. | |
136 | */ | |
137 | obj = kmalloc_node_track_caller(size, | |
138 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, | |
139 | node); | |
140 | if (obj || !(gfp_pfmemalloc_allowed(flags))) | |
141 | goto out; | |
142 | ||
143 | /* Try again but now we are using pfmemalloc reserves */ | |
144 | ret_pfmemalloc = true; | |
145 | obj = kmalloc_node_track_caller(size, flags, node); | |
146 | ||
147 | out: | |
148 | if (pfmemalloc) | |
149 | *pfmemalloc = ret_pfmemalloc; | |
150 | ||
151 | return obj; | |
152 | } | |
153 | ||
1da177e4 LT |
154 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
155 | * 'private' fields and also do memory statistics to find all the | |
156 | * [BEEP] leaks. | |
157 | * | |
158 | */ | |
159 | ||
0ebd0ac5 PM |
160 | struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) |
161 | { | |
162 | struct sk_buff *skb; | |
163 | ||
164 | /* Get the HEAD */ | |
165 | skb = kmem_cache_alloc_node(skbuff_head_cache, | |
166 | gfp_mask & ~__GFP_DMA, node); | |
167 | if (!skb) | |
168 | goto out; | |
169 | ||
170 | /* | |
171 | * Only clear those fields we need to clear, not those that we will | |
172 | * actually initialise below. Hence, don't put any more fields after | |
173 | * the tail pointer in struct sk_buff! | |
174 | */ | |
175 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
5e71d9d7 | 176 | skb->head = NULL; |
0ebd0ac5 PM |
177 | skb->truesize = sizeof(struct sk_buff); |
178 | atomic_set(&skb->users, 1); | |
179 | ||
35d04610 | 180 | skb->mac_header = (typeof(skb->mac_header))~0U; |
0ebd0ac5 PM |
181 | out: |
182 | return skb; | |
183 | } | |
184 | ||
1da177e4 | 185 | /** |
d179cd12 | 186 | * __alloc_skb - allocate a network buffer |
1da177e4 LT |
187 | * @size: size to allocate |
188 | * @gfp_mask: allocation mask | |
c93bdd0e MG |
189 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache |
190 | * instead of head cache and allocate a cloned (child) skb. | |
191 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for | |
192 | * allocations in case the data is required for writeback | |
b30973f8 | 193 | * @node: numa node to allocate memory on |
1da177e4 LT |
194 | * |
195 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
94b6042c BH |
196 | * tail room of at least size bytes. The object has a reference count |
197 | * of one. The return is the buffer. On a failure the return is %NULL. | |
1da177e4 LT |
198 | * |
199 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
200 | * %GFP_ATOMIC. | |
201 | */ | |
dd0fc66f | 202 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
c93bdd0e | 203 | int flags, int node) |
1da177e4 | 204 | { |
e18b890b | 205 | struct kmem_cache *cache; |
4947d3ef | 206 | struct skb_shared_info *shinfo; |
1da177e4 LT |
207 | struct sk_buff *skb; |
208 | u8 *data; | |
c93bdd0e | 209 | bool pfmemalloc; |
1da177e4 | 210 | |
c93bdd0e MG |
211 | cache = (flags & SKB_ALLOC_FCLONE) |
212 | ? skbuff_fclone_cache : skbuff_head_cache; | |
213 | ||
214 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) | |
215 | gfp_mask |= __GFP_MEMALLOC; | |
8798b3fb | 216 | |
1da177e4 | 217 | /* Get the HEAD */ |
b30973f8 | 218 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
1da177e4 LT |
219 | if (!skb) |
220 | goto out; | |
ec7d2f2c | 221 | prefetchw(skb); |
1da177e4 | 222 | |
87fb4b7b ED |
223 | /* We do our best to align skb_shared_info on a separate cache |
224 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives | |
225 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. | |
226 | * Both skb->head and skb_shared_info are cache line aligned. | |
227 | */ | |
bc417e30 | 228 | size = SKB_DATA_ALIGN(size); |
87fb4b7b | 229 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
c93bdd0e | 230 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
1da177e4 LT |
231 | if (!data) |
232 | goto nodata; | |
87fb4b7b ED |
233 | /* kmalloc(size) might give us more room than requested. |
234 | * Put skb_shared_info exactly at the end of allocated zone, | |
235 | * to allow max possible filling before reallocation. | |
236 | */ | |
237 | size = SKB_WITH_OVERHEAD(ksize(data)); | |
ec7d2f2c | 238 | prefetchw(data + size); |
1da177e4 | 239 | |
ca0605a7 | 240 | /* |
c8005785 JB |
241 | * Only clear those fields we need to clear, not those that we will |
242 | * actually initialise below. Hence, don't put any more fields after | |
243 | * the tail pointer in struct sk_buff! | |
ca0605a7 ACM |
244 | */ |
245 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
87fb4b7b ED |
246 | /* Account for allocated memory : skb + skb->head */ |
247 | skb->truesize = SKB_TRUESIZE(size); | |
c93bdd0e | 248 | skb->pfmemalloc = pfmemalloc; |
1da177e4 LT |
249 | atomic_set(&skb->users, 1); |
250 | skb->head = data; | |
251 | skb->data = data; | |
27a884dc | 252 | skb_reset_tail_pointer(skb); |
4305b541 | 253 | skb->end = skb->tail + size; |
35d04610 CW |
254 | skb->mac_header = (typeof(skb->mac_header))~0U; |
255 | skb->transport_header = (typeof(skb->transport_header))~0U; | |
19633e12 | 256 | |
4947d3ef BL |
257 | /* make sure we initialize shinfo sequentially */ |
258 | shinfo = skb_shinfo(skb); | |
ec7d2f2c | 259 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
4947d3ef | 260 | atomic_set(&shinfo->dataref, 1); |
c2aa3665 | 261 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
4947d3ef | 262 | |
c93bdd0e | 263 | if (flags & SKB_ALLOC_FCLONE) { |
d0bf4a9e | 264 | struct sk_buff_fclones *fclones; |
1da177e4 | 265 | |
d0bf4a9e ED |
266 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
267 | ||
268 | kmemcheck_annotate_bitfield(&fclones->skb2, flags1); | |
d179cd12 | 269 | skb->fclone = SKB_FCLONE_ORIG; |
d0bf4a9e | 270 | atomic_set(&fclones->fclone_ref, 1); |
d179cd12 | 271 | |
6ffe75eb | 272 | fclones->skb2.fclone = SKB_FCLONE_CLONE; |
d0bf4a9e | 273 | fclones->skb2.pfmemalloc = pfmemalloc; |
d179cd12 | 274 | } |
1da177e4 LT |
275 | out: |
276 | return skb; | |
277 | nodata: | |
8798b3fb | 278 | kmem_cache_free(cache, skb); |
1da177e4 LT |
279 | skb = NULL; |
280 | goto out; | |
1da177e4 | 281 | } |
b4ac530f | 282 | EXPORT_SYMBOL(__alloc_skb); |
1da177e4 | 283 | |
b2b5ce9d | 284 | /** |
2ea2f62c | 285 | * __build_skb - build a network buffer |
b2b5ce9d | 286 | * @data: data buffer provided by caller |
2ea2f62c | 287 | * @frag_size: size of data, or 0 if head was kmalloced |
b2b5ce9d ED |
288 | * |
289 | * Allocate a new &sk_buff. Caller provides space holding head and | |
deceb4c0 | 290 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
2ea2f62c ED |
291 | * @frag_size is 0, otherwise data should come from the page allocator |
292 | * or vmalloc() | |
b2b5ce9d ED |
293 | * The return is the new skb buffer. |
294 | * On a failure the return is %NULL, and @data is not freed. | |
295 | * Notes : | |
296 | * Before IO, driver allocates only data buffer where NIC put incoming frame | |
297 | * Driver should add room at head (NET_SKB_PAD) and | |
298 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) | |
299 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it | |
300 | * before giving packet to stack. | |
301 | * RX rings only contains data buffers, not full skbs. | |
302 | */ | |
2ea2f62c | 303 | struct sk_buff *__build_skb(void *data, unsigned int frag_size) |
b2b5ce9d ED |
304 | { |
305 | struct skb_shared_info *shinfo; | |
306 | struct sk_buff *skb; | |
d3836f21 | 307 | unsigned int size = frag_size ? : ksize(data); |
b2b5ce9d ED |
308 | |
309 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); | |
310 | if (!skb) | |
311 | return NULL; | |
312 | ||
d3836f21 | 313 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
b2b5ce9d ED |
314 | |
315 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
316 | skb->truesize = SKB_TRUESIZE(size); | |
317 | atomic_set(&skb->users, 1); | |
318 | skb->head = data; | |
319 | skb->data = data; | |
320 | skb_reset_tail_pointer(skb); | |
321 | skb->end = skb->tail + size; | |
35d04610 CW |
322 | skb->mac_header = (typeof(skb->mac_header))~0U; |
323 | skb->transport_header = (typeof(skb->transport_header))~0U; | |
b2b5ce9d ED |
324 | |
325 | /* make sure we initialize shinfo sequentially */ | |
326 | shinfo = skb_shinfo(skb); | |
327 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); | |
328 | atomic_set(&shinfo->dataref, 1); | |
329 | kmemcheck_annotate_variable(shinfo->destructor_arg); | |
330 | ||
331 | return skb; | |
332 | } | |
2ea2f62c ED |
333 | |
334 | /* build_skb() is wrapper over __build_skb(), that specifically | |
335 | * takes care of skb->head and skb->pfmemalloc | |
336 | * This means that if @frag_size is not zero, then @data must be backed | |
337 | * by a page fragment, not kmalloc() or vmalloc() | |
338 | */ | |
339 | struct sk_buff *build_skb(void *data, unsigned int frag_size) | |
340 | { | |
341 | struct sk_buff *skb = __build_skb(data, frag_size); | |
342 | ||
343 | if (skb && frag_size) { | |
344 | skb->head_frag = 1; | |
2f064f34 | 345 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
2ea2f62c ED |
346 | skb->pfmemalloc = 1; |
347 | } | |
348 | return skb; | |
349 | } | |
b2b5ce9d ED |
350 | EXPORT_SYMBOL(build_skb); |
351 | ||
795bb1c0 JDB |
352 | #define NAPI_SKB_CACHE_SIZE 64 |
353 | ||
354 | struct napi_alloc_cache { | |
355 | struct page_frag_cache page; | |
356 | size_t skb_count; | |
357 | void *skb_cache[NAPI_SKB_CACHE_SIZE]; | |
358 | }; | |
359 | ||
b63ae8ca | 360 | static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); |
795bb1c0 | 361 | static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); |
ffde7328 AD |
362 | |
363 | static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) | |
364 | { | |
b63ae8ca | 365 | struct page_frag_cache *nc; |
ffde7328 AD |
366 | unsigned long flags; |
367 | void *data; | |
368 | ||
369 | local_irq_save(flags); | |
9451980a AD |
370 | nc = this_cpu_ptr(&netdev_alloc_cache); |
371 | data = __alloc_page_frag(nc, fragsz, gfp_mask); | |
6f532612 ED |
372 | local_irq_restore(flags); |
373 | return data; | |
374 | } | |
c93bdd0e MG |
375 | |
376 | /** | |
377 | * netdev_alloc_frag - allocate a page fragment | |
378 | * @fragsz: fragment size | |
379 | * | |
380 | * Allocates a frag from a page for receive buffer. | |
381 | * Uses GFP_ATOMIC allocations. | |
382 | */ | |
383 | void *netdev_alloc_frag(unsigned int fragsz) | |
384 | { | |
385 | return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); | |
386 | } | |
6f532612 ED |
387 | EXPORT_SYMBOL(netdev_alloc_frag); |
388 | ||
ffde7328 AD |
389 | static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
390 | { | |
795bb1c0 | 391 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
9451980a | 392 | |
795bb1c0 | 393 | return __alloc_page_frag(&nc->page, fragsz, gfp_mask); |
ffde7328 AD |
394 | } |
395 | ||
396 | void *napi_alloc_frag(unsigned int fragsz) | |
397 | { | |
398 | return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); | |
399 | } | |
400 | EXPORT_SYMBOL(napi_alloc_frag); | |
401 | ||
fd11a83d AD |
402 | /** |
403 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device | |
404 | * @dev: network device to receive on | |
d7499160 | 405 | * @len: length to allocate |
fd11a83d AD |
406 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
407 | * | |
408 | * Allocate a new &sk_buff and assign it a usage count of one. The | |
409 | * buffer has NET_SKB_PAD headroom built in. Users should allocate | |
410 | * the headroom they think they need without accounting for the | |
411 | * built in space. The built in space is used for optimisations. | |
412 | * | |
413 | * %NULL is returned if there is no free memory. | |
414 | */ | |
9451980a AD |
415 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, |
416 | gfp_t gfp_mask) | |
fd11a83d | 417 | { |
b63ae8ca | 418 | struct page_frag_cache *nc; |
9451980a | 419 | unsigned long flags; |
fd11a83d | 420 | struct sk_buff *skb; |
9451980a AD |
421 | bool pfmemalloc; |
422 | void *data; | |
423 | ||
424 | len += NET_SKB_PAD; | |
fd11a83d | 425 | |
9451980a | 426 | if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || |
d0164adc | 427 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
a080e7bd AD |
428 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
429 | if (!skb) | |
430 | goto skb_fail; | |
431 | goto skb_success; | |
432 | } | |
fd11a83d | 433 | |
9451980a AD |
434 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
435 | len = SKB_DATA_ALIGN(len); | |
436 | ||
437 | if (sk_memalloc_socks()) | |
438 | gfp_mask |= __GFP_MEMALLOC; | |
439 | ||
440 | local_irq_save(flags); | |
441 | ||
442 | nc = this_cpu_ptr(&netdev_alloc_cache); | |
443 | data = __alloc_page_frag(nc, len, gfp_mask); | |
444 | pfmemalloc = nc->pfmemalloc; | |
445 | ||
446 | local_irq_restore(flags); | |
447 | ||
448 | if (unlikely(!data)) | |
449 | return NULL; | |
450 | ||
451 | skb = __build_skb(data, len); | |
452 | if (unlikely(!skb)) { | |
181edb2b | 453 | skb_free_frag(data); |
9451980a | 454 | return NULL; |
7b2e497a | 455 | } |
fd11a83d | 456 | |
9451980a AD |
457 | /* use OR instead of assignment to avoid clearing of bits in mask */ |
458 | if (pfmemalloc) | |
459 | skb->pfmemalloc = 1; | |
460 | skb->head_frag = 1; | |
461 | ||
a080e7bd | 462 | skb_success: |
9451980a AD |
463 | skb_reserve(skb, NET_SKB_PAD); |
464 | skb->dev = dev; | |
465 | ||
a080e7bd | 466 | skb_fail: |
8af27456 CH |
467 | return skb; |
468 | } | |
b4ac530f | 469 | EXPORT_SYMBOL(__netdev_alloc_skb); |
1da177e4 | 470 | |
fd11a83d AD |
471 | /** |
472 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance | |
473 | * @napi: napi instance this buffer was allocated for | |
d7499160 | 474 | * @len: length to allocate |
fd11a83d AD |
475 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
476 | * | |
477 | * Allocate a new sk_buff for use in NAPI receive. This buffer will | |
478 | * attempt to allocate the head from a special reserved region used | |
479 | * only for NAPI Rx allocation. By doing this we can save several | |
480 | * CPU cycles by avoiding having to disable and re-enable IRQs. | |
481 | * | |
482 | * %NULL is returned if there is no free memory. | |
483 | */ | |
9451980a AD |
484 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, |
485 | gfp_t gfp_mask) | |
fd11a83d | 486 | { |
795bb1c0 | 487 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
fd11a83d | 488 | struct sk_buff *skb; |
9451980a AD |
489 | void *data; |
490 | ||
491 | len += NET_SKB_PAD + NET_IP_ALIGN; | |
fd11a83d | 492 | |
9451980a | 493 | if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || |
d0164adc | 494 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
a080e7bd AD |
495 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
496 | if (!skb) | |
497 | goto skb_fail; | |
498 | goto skb_success; | |
499 | } | |
9451980a AD |
500 | |
501 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
502 | len = SKB_DATA_ALIGN(len); | |
503 | ||
504 | if (sk_memalloc_socks()) | |
505 | gfp_mask |= __GFP_MEMALLOC; | |
fd11a83d | 506 | |
795bb1c0 | 507 | data = __alloc_page_frag(&nc->page, len, gfp_mask); |
9451980a AD |
508 | if (unlikely(!data)) |
509 | return NULL; | |
510 | ||
511 | skb = __build_skb(data, len); | |
512 | if (unlikely(!skb)) { | |
181edb2b | 513 | skb_free_frag(data); |
9451980a | 514 | return NULL; |
fd11a83d AD |
515 | } |
516 | ||
9451980a | 517 | /* use OR instead of assignment to avoid clearing of bits in mask */ |
795bb1c0 | 518 | if (nc->page.pfmemalloc) |
9451980a AD |
519 | skb->pfmemalloc = 1; |
520 | skb->head_frag = 1; | |
521 | ||
a080e7bd | 522 | skb_success: |
9451980a AD |
523 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
524 | skb->dev = napi->dev; | |
525 | ||
a080e7bd | 526 | skb_fail: |
fd11a83d AD |
527 | return skb; |
528 | } | |
529 | EXPORT_SYMBOL(__napi_alloc_skb); | |
530 | ||
654bed16 | 531 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
50269e19 | 532 | int size, unsigned int truesize) |
654bed16 PZ |
533 | { |
534 | skb_fill_page_desc(skb, i, page, off, size); | |
535 | skb->len += size; | |
536 | skb->data_len += size; | |
50269e19 | 537 | skb->truesize += truesize; |
654bed16 PZ |
538 | } |
539 | EXPORT_SYMBOL(skb_add_rx_frag); | |
540 | ||
f8e617e1 JW |
541 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
542 | unsigned int truesize) | |
543 | { | |
544 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
545 | ||
546 | skb_frag_size_add(frag, size); | |
547 | skb->len += size; | |
548 | skb->data_len += size; | |
549 | skb->truesize += truesize; | |
550 | } | |
551 | EXPORT_SYMBOL(skb_coalesce_rx_frag); | |
552 | ||
27b437c8 | 553 | static void skb_drop_list(struct sk_buff **listp) |
1da177e4 | 554 | { |
bd8a7036 | 555 | kfree_skb_list(*listp); |
27b437c8 | 556 | *listp = NULL; |
1da177e4 LT |
557 | } |
558 | ||
27b437c8 HX |
559 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
560 | { | |
561 | skb_drop_list(&skb_shinfo(skb)->frag_list); | |
562 | } | |
563 | ||
1da177e4 LT |
564 | static void skb_clone_fraglist(struct sk_buff *skb) |
565 | { | |
566 | struct sk_buff *list; | |
567 | ||
fbb398a8 | 568 | skb_walk_frags(skb, list) |
1da177e4 LT |
569 | skb_get(list); |
570 | } | |
571 | ||
d3836f21 ED |
572 | static void skb_free_head(struct sk_buff *skb) |
573 | { | |
181edb2b AD |
574 | unsigned char *head = skb->head; |
575 | ||
d3836f21 | 576 | if (skb->head_frag) |
181edb2b | 577 | skb_free_frag(head); |
d3836f21 | 578 | else |
181edb2b | 579 | kfree(head); |
d3836f21 ED |
580 | } |
581 | ||
5bba1712 | 582 | static void skb_release_data(struct sk_buff *skb) |
1da177e4 | 583 | { |
ff04a771 ED |
584 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
585 | int i; | |
1da177e4 | 586 | |
ff04a771 ED |
587 | if (skb->cloned && |
588 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
589 | &shinfo->dataref)) | |
590 | return; | |
a6686f2f | 591 | |
ff04a771 ED |
592 | for (i = 0; i < shinfo->nr_frags; i++) |
593 | __skb_frag_unref(&shinfo->frags[i]); | |
a6686f2f | 594 | |
ff04a771 ED |
595 | /* |
596 | * If skb buf is from userspace, we need to notify the caller | |
597 | * the lower device DMA has done; | |
598 | */ | |
599 | if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { | |
600 | struct ubuf_info *uarg; | |
1da177e4 | 601 | |
ff04a771 ED |
602 | uarg = shinfo->destructor_arg; |
603 | if (uarg->callback) | |
604 | uarg->callback(uarg, true); | |
1da177e4 | 605 | } |
ff04a771 ED |
606 | |
607 | if (shinfo->frag_list) | |
608 | kfree_skb_list(shinfo->frag_list); | |
609 | ||
610 | skb_free_head(skb); | |
1da177e4 LT |
611 | } |
612 | ||
613 | /* | |
614 | * Free an skbuff by memory without cleaning the state. | |
615 | */ | |
2d4baff8 | 616 | static void kfree_skbmem(struct sk_buff *skb) |
1da177e4 | 617 | { |
d0bf4a9e | 618 | struct sk_buff_fclones *fclones; |
d179cd12 | 619 | |
d179cd12 DM |
620 | switch (skb->fclone) { |
621 | case SKB_FCLONE_UNAVAILABLE: | |
622 | kmem_cache_free(skbuff_head_cache, skb); | |
6ffe75eb | 623 | return; |
d179cd12 DM |
624 | |
625 | case SKB_FCLONE_ORIG: | |
d0bf4a9e | 626 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
d179cd12 | 627 | |
6ffe75eb ED |
628 | /* We usually free the clone (TX completion) before original skb |
629 | * This test would have no chance to be true for the clone, | |
630 | * while here, branch prediction will be good. | |
d179cd12 | 631 | */ |
6ffe75eb ED |
632 | if (atomic_read(&fclones->fclone_ref) == 1) |
633 | goto fastpath; | |
634 | break; | |
e7820e39 | 635 | |
6ffe75eb ED |
636 | default: /* SKB_FCLONE_CLONE */ |
637 | fclones = container_of(skb, struct sk_buff_fclones, skb2); | |
d179cd12 | 638 | break; |
3ff50b79 | 639 | } |
6ffe75eb ED |
640 | if (!atomic_dec_and_test(&fclones->fclone_ref)) |
641 | return; | |
642 | fastpath: | |
643 | kmem_cache_free(skbuff_fclone_cache, fclones); | |
1da177e4 LT |
644 | } |
645 | ||
04a4bb55 | 646 | static void skb_release_head_state(struct sk_buff *skb) |
1da177e4 | 647 | { |
adf30907 | 648 | skb_dst_drop(skb); |
1da177e4 LT |
649 | #ifdef CONFIG_XFRM |
650 | secpath_put(skb->sp); | |
651 | #endif | |
9c2b3328 SH |
652 | if (skb->destructor) { |
653 | WARN_ON(in_irq()); | |
1da177e4 LT |
654 | skb->destructor(skb); |
655 | } | |
a3bf7ae9 | 656 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
5f79e0f9 | 657 | nf_conntrack_put(skb->nfct); |
2fc72c7b | 658 | #endif |
1109a90c | 659 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1da177e4 LT |
660 | nf_bridge_put(skb->nf_bridge); |
661 | #endif | |
04a4bb55 LB |
662 | } |
663 | ||
664 | /* Free everything but the sk_buff shell. */ | |
665 | static void skb_release_all(struct sk_buff *skb) | |
666 | { | |
667 | skb_release_head_state(skb); | |
5e71d9d7 | 668 | if (likely(skb->head)) |
0ebd0ac5 | 669 | skb_release_data(skb); |
2d4baff8 HX |
670 | } |
671 | ||
672 | /** | |
673 | * __kfree_skb - private function | |
674 | * @skb: buffer | |
675 | * | |
676 | * Free an sk_buff. Release anything attached to the buffer. | |
677 | * Clean the state. This is an internal helper function. Users should | |
678 | * always call kfree_skb | |
679 | */ | |
1da177e4 | 680 | |
2d4baff8 HX |
681 | void __kfree_skb(struct sk_buff *skb) |
682 | { | |
683 | skb_release_all(skb); | |
1da177e4 LT |
684 | kfree_skbmem(skb); |
685 | } | |
b4ac530f | 686 | EXPORT_SYMBOL(__kfree_skb); |
1da177e4 | 687 |