]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Routines having to do with the 'struct sk_buff' memory handlers. | |
4 | * | |
113aa838 | 5 | * Authors: Alan Cox <[email protected]> |
1da177e4 LT |
6 | * Florian La Roche <[email protected]> |
7 | * | |
1da177e4 LT |
8 | * Fixes: |
9 | * Alan Cox : Fixed the worst of the load | |
10 | * balancer bugs. | |
11 | * Dave Platt : Interrupt stacking fix. | |
12 | * Richard Kooijman : Timestamp fixes. | |
13 | * Alan Cox : Changed buffer format. | |
14 | * Alan Cox : destructor hook for AF_UNIX etc. | |
15 | * Linus Torvalds : Better skb_clone. | |
16 | * Alan Cox : Added skb_copy. | |
17 | * Alan Cox : Added all the changed routines Linus | |
18 | * only put in the headers | |
19 | * Ray VanTassle : Fixed --skb->lock in free | |
20 | * Alan Cox : skb_copy copy arp field | |
21 | * Andi Kleen : slabified it. | |
22 | * Robert Olsson : Removed skb_head_pool | |
23 | * | |
24 | * NOTE: | |
25 | * The __skb_ routines should be called with interrupts | |
26 | * disabled, or you better be *real* sure that the operation is atomic | |
27 | * with respect to whatever list is being frobbed (e.g. via lock_sock() | |
28 | * or via disabling bottom half handlers, etc). | |
1da177e4 LT |
29 | */ |
30 | ||
31 | /* | |
32 | * The functions in this file will not compile correctly with gcc 2.4.x | |
33 | */ | |
34 | ||
e005d193 JP |
35 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
36 | ||
1da177e4 LT |
37 | #include <linux/module.h> |
38 | #include <linux/types.h> | |
39 | #include <linux/kernel.h> | |
1da177e4 LT |
40 | #include <linux/mm.h> |
41 | #include <linux/interrupt.h> | |
42 | #include <linux/in.h> | |
43 | #include <linux/inet.h> | |
44 | #include <linux/slab.h> | |
de960aa9 FW |
45 | #include <linux/tcp.h> |
46 | #include <linux/udp.h> | |
90017acc | 47 | #include <linux/sctp.h> |
1da177e4 LT |
48 | #include <linux/netdevice.h> |
49 | #ifdef CONFIG_NET_CLS_ACT | |
50 | #include <net/pkt_sched.h> | |
51 | #endif | |
52 | #include <linux/string.h> | |
53 | #include <linux/skbuff.h> | |
9c55e01c | 54 | #include <linux/splice.h> |
1da177e4 LT |
55 | #include <linux/cache.h> |
56 | #include <linux/rtnetlink.h> | |
57 | #include <linux/init.h> | |
716ea3a7 | 58 | #include <linux/scatterlist.h> |
ac45f602 | 59 | #include <linux/errqueue.h> |
268bb0ce | 60 | #include <linux/prefetch.h> |
0d5501c1 | 61 | #include <linux/if_vlan.h> |
2a2ea508 | 62 | #include <linux/mpls.h> |
1da177e4 LT |
63 | |
64 | #include <net/protocol.h> | |
65 | #include <net/dst.h> | |
66 | #include <net/sock.h> | |
67 | #include <net/checksum.h> | |
ed1f50c3 | 68 | #include <net/ip6_checksum.h> |
1da177e4 | 69 | #include <net/xfrm.h> |
8822e270 | 70 | #include <net/mpls.h> |
3ee17bc7 | 71 | #include <net/mptcp.h> |
1da177e4 | 72 | |
7c0f6ba6 | 73 | #include <linux/uaccess.h> |
ad8d75ff | 74 | #include <trace/events/skb.h> |
51c56b00 | 75 | #include <linux/highmem.h> |
b245be1f WB |
76 | #include <linux/capability.h> |
77 | #include <linux/user_namespace.h> | |
2544af03 | 78 | #include <linux/indirect_call_wrapper.h> |
a1f8e7f7 | 79 | |
7b7ed885 BVA |
80 | #include "datagram.h" |
81 | ||
08009a76 AD |
82 | struct kmem_cache *skbuff_head_cache __ro_after_init; |
83 | static struct kmem_cache *skbuff_fclone_cache __ro_after_init; | |
df5042f4 FW |
84 | #ifdef CONFIG_SKB_EXTENSIONS |
85 | static struct kmem_cache *skbuff_ext_cache __ro_after_init; | |
86 | #endif | |
5f74f82e HWR |
87 | int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; |
88 | EXPORT_SYMBOL(sysctl_max_skb_frags); | |
1da177e4 | 89 | |
1da177e4 | 90 | /** |
f05de73b JS |
91 | * skb_panic - private function for out-of-line support |
92 | * @skb: buffer | |
93 | * @sz: size | |
94 | * @addr: address | |
99d5851e | 95 | * @msg: skb_over_panic or skb_under_panic |
1da177e4 | 96 | * |
f05de73b JS |
97 | * Out-of-line support for skb_put() and skb_push(). |
98 | * Called via the wrapper skb_over_panic() or skb_under_panic(). | |
99 | * Keep out of line to prevent kernel bloat. | |
100 | * __builtin_return_address is not used because it is not always reliable. | |
1da177e4 | 101 | */ |
f05de73b | 102 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
99d5851e | 103 | const char msg[]) |
1da177e4 | 104 | { |
41a46913 | 105 | pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", |
99d5851e | 106 | msg, addr, skb->len, sz, skb->head, skb->data, |
e005d193 JP |
107 | (unsigned long)skb->tail, (unsigned long)skb->end, |
108 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
109 | BUG(); |
110 | } | |
111 | ||
f05de73b | 112 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
1da177e4 | 113 | { |
f05de73b | 114 | skb_panic(skb, sz, addr, __func__); |
1da177e4 LT |
115 | } |
116 | ||
f05de73b JS |
117 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
118 | { | |
119 | skb_panic(skb, sz, addr, __func__); | |
120 | } | |
c93bdd0e | 121 | |
ba0509b6 JDB |
122 | /* Caller must provide SKB that is memset cleared */ |
123 | static struct sk_buff *__build_skb_around(struct sk_buff *skb, | |
124 | void *data, unsigned int frag_size) | |
125 | { | |
126 | struct skb_shared_info *shinfo; | |
127 | unsigned int size = frag_size ? : ksize(data); | |
128 | ||
129 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
130 | ||
131 | /* Assumes caller memset cleared SKB */ | |
132 | skb->truesize = SKB_TRUESIZE(size); | |
133 | refcount_set(&skb->users, 1); | |
134 | skb->head = data; | |
135 | skb->data = data; | |
136 | skb_reset_tail_pointer(skb); | |
137 | skb->end = skb->tail + size; | |
138 | skb->mac_header = (typeof(skb->mac_header))~0U; | |
139 | skb->transport_header = (typeof(skb->transport_header))~0U; | |
140 | ||
141 | /* make sure we initialize shinfo sequentially */ | |
142 | shinfo = skb_shinfo(skb); | |
143 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); | |
144 | atomic_set(&shinfo->dataref, 1); | |
145 | ||
6370cc3b AN |
146 | skb_set_kcov_handle(skb, kcov_common_handle()); |
147 | ||
ba0509b6 JDB |
148 | return skb; |
149 | } | |
150 | ||
b2b5ce9d | 151 | /** |
2ea2f62c | 152 | * __build_skb - build a network buffer |
b2b5ce9d | 153 | * @data: data buffer provided by caller |
2ea2f62c | 154 | * @frag_size: size of data, or 0 if head was kmalloced |
b2b5ce9d ED |
155 | * |
156 | * Allocate a new &sk_buff. Caller provides space holding head and | |
deceb4c0 | 157 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
2ea2f62c ED |
158 | * @frag_size is 0, otherwise data should come from the page allocator |
159 | * or vmalloc() | |
b2b5ce9d ED |
160 | * The return is the new skb buffer. |
161 | * On a failure the return is %NULL, and @data is not freed. | |
162 | * Notes : | |
163 | * Before IO, driver allocates only data buffer where NIC put incoming frame | |
164 | * Driver should add room at head (NET_SKB_PAD) and | |
165 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) | |
166 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it | |
167 | * before giving packet to stack. | |
168 | * RX rings only contains data buffers, not full skbs. | |
169 | */ | |
2ea2f62c | 170 | struct sk_buff *__build_skb(void *data, unsigned int frag_size) |
b2b5ce9d | 171 | { |
b2b5ce9d | 172 | struct sk_buff *skb; |
b2b5ce9d ED |
173 | |
174 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); | |
ba0509b6 | 175 | if (unlikely(!skb)) |
b2b5ce9d ED |
176 | return NULL; |
177 | ||
b2b5ce9d | 178 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
b2b5ce9d | 179 | |
ba0509b6 | 180 | return __build_skb_around(skb, data, frag_size); |
b2b5ce9d | 181 | } |
2ea2f62c ED |
182 | |
183 | /* build_skb() is wrapper over __build_skb(), that specifically | |
184 | * takes care of skb->head and skb->pfmemalloc | |
185 | * This means that if @frag_size is not zero, then @data must be backed | |
186 | * by a page fragment, not kmalloc() or vmalloc() | |
187 | */ | |
188 | struct sk_buff *build_skb(void *data, unsigned int frag_size) | |
189 | { | |
190 | struct sk_buff *skb = __build_skb(data, frag_size); | |
191 | ||
192 | if (skb && frag_size) { | |
193 | skb->head_frag = 1; | |
2f064f34 | 194 | if (page_is_pfmemalloc(virt_to_head_page(data))) |
2ea2f62c ED |
195 | skb->pfmemalloc = 1; |
196 | } | |
197 | return skb; | |
198 | } | |
b2b5ce9d ED |
199 | EXPORT_SYMBOL(build_skb); |
200 | ||
ba0509b6 JDB |
201 | /** |
202 | * build_skb_around - build a network buffer around provided skb | |
203 | * @skb: sk_buff provide by caller, must be memset cleared | |
204 | * @data: data buffer provided by caller | |
205 | * @frag_size: size of data, or 0 if head was kmalloced | |
206 | */ | |
207 | struct sk_buff *build_skb_around(struct sk_buff *skb, | |
208 | void *data, unsigned int frag_size) | |
209 | { | |
210 | if (unlikely(!skb)) | |
211 | return NULL; | |
212 | ||
213 | skb = __build_skb_around(skb, data, frag_size); | |
214 | ||
215 | if (skb && frag_size) { | |
216 | skb->head_frag = 1; | |
217 | if (page_is_pfmemalloc(virt_to_head_page(data))) | |
218 | skb->pfmemalloc = 1; | |
219 | } | |
220 | return skb; | |
221 | } | |
222 | EXPORT_SYMBOL(build_skb_around); | |
223 | ||
795bb1c0 JDB |
224 | #define NAPI_SKB_CACHE_SIZE 64 |
225 | ||
226 | struct napi_alloc_cache { | |
227 | struct page_frag_cache page; | |
e0d7924a | 228 | unsigned int skb_count; |
795bb1c0 JDB |
229 | void *skb_cache[NAPI_SKB_CACHE_SIZE]; |
230 | }; | |
231 | ||
b63ae8ca | 232 | static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); |
795bb1c0 | 233 | static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); |
ffde7328 | 234 | |
3f6e687d KH |
235 | static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask, |
236 | unsigned int align_mask) | |
ffde7328 | 237 | { |
7ba7aeab | 238 | struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); |
ffde7328 | 239 | |
3f6e687d | 240 | return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask); |
7ba7aeab SAS |
241 | } |
242 | ||
3f6e687d | 243 | void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
7ba7aeab SAS |
244 | { |
245 | fragsz = SKB_DATA_ALIGN(fragsz); | |
246 | ||
3f6e687d | 247 | return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); |
6f532612 | 248 | } |
3f6e687d | 249 | EXPORT_SYMBOL(__napi_alloc_frag_align); |
c93bdd0e | 250 | |
3f6e687d | 251 | void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) |
c93bdd0e | 252 | { |
7ba7aeab SAS |
253 | struct page_frag_cache *nc; |
254 | void *data; | |
ffde7328 | 255 | |
3bed3cc4 | 256 | fragsz = SKB_DATA_ALIGN(fragsz); |
7ba7aeab SAS |
257 | if (in_irq() || irqs_disabled()) { |
258 | nc = this_cpu_ptr(&netdev_alloc_cache); | |
3f6e687d | 259 | data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); |
7ba7aeab SAS |
260 | } else { |
261 | local_bh_disable(); | |
3f6e687d | 262 | data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); |
7ba7aeab SAS |
263 | local_bh_enable(); |
264 | } | |
265 | return data; | |
ffde7328 | 266 | } |
3f6e687d | 267 | EXPORT_SYMBOL(__netdev_alloc_frag_align); |
ffde7328 | 268 | |
5381b23d AL |
269 | /* |
270 | * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells | |
271 | * the caller if emergency pfmemalloc reserves are being used. If it is and | |
272 | * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves | |
273 | * may be used. Otherwise, the packet data may be discarded until enough | |
274 | * memory is free | |
275 | */ | |
ef28095f AL |
276 | static void *kmalloc_reserve(size_t size, gfp_t flags, int node, |
277 | bool *pfmemalloc) | |
5381b23d AL |
278 | { |
279 | void *obj; | |
280 | bool ret_pfmemalloc = false; | |
281 | ||
282 | /* | |
283 | * Try a regular allocation, when that fails and we're not entitled | |
284 | * to the reserves, fail. | |
285 | */ | |
286 | obj = kmalloc_node_track_caller(size, | |
287 | flags | __GFP_NOMEMALLOC | __GFP_NOWARN, | |
288 | node); | |
289 | if (obj || !(gfp_pfmemalloc_allowed(flags))) | |
290 | goto out; | |
291 | ||
292 | /* Try again but now we are using pfmemalloc reserves */ | |
293 | ret_pfmemalloc = true; | |
294 | obj = kmalloc_node_track_caller(size, flags, node); | |
295 | ||
296 | out: | |
297 | if (pfmemalloc) | |
298 | *pfmemalloc = ret_pfmemalloc; | |
299 | ||
300 | return obj; | |
301 | } | |
302 | ||
303 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few | |
304 | * 'private' fields and also do memory statistics to find all the | |
305 | * [BEEP] leaks. | |
306 | * | |
307 | */ | |
308 | ||
309 | /** | |
310 | * __alloc_skb - allocate a network buffer | |
311 | * @size: size to allocate | |
312 | * @gfp_mask: allocation mask | |
313 | * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache | |
314 | * instead of head cache and allocate a cloned (child) skb. | |
315 | * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for | |
316 | * allocations in case the data is required for writeback | |
317 | * @node: numa node to allocate memory on | |
318 | * | |
319 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
320 | * tail room of at least size bytes. The object has a reference count | |
321 | * of one. The return is the buffer. On a failure the return is %NULL. | |
322 | * | |
323 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
324 | * %GFP_ATOMIC. | |
325 | */ | |
326 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, | |
327 | int flags, int node) | |
328 | { | |
329 | struct kmem_cache *cache; | |
330 | struct skb_shared_info *shinfo; | |
331 | struct sk_buff *skb; | |
332 | u8 *data; | |
333 | bool pfmemalloc; | |
334 | ||
335 | cache = (flags & SKB_ALLOC_FCLONE) | |
336 | ? skbuff_fclone_cache : skbuff_head_cache; | |
337 | ||
338 | if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) | |
339 | gfp_mask |= __GFP_MEMALLOC; | |
340 | ||
341 | /* Get the HEAD */ | |
342 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); | |
343 | if (!skb) | |
344 | goto out; | |
345 | prefetchw(skb); | |
346 | ||
347 | /* We do our best to align skb_shared_info on a separate cache | |
348 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives | |
349 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. | |
350 | * Both skb->head and skb_shared_info are cache line aligned. | |
351 | */ | |
352 | size = SKB_DATA_ALIGN(size); | |
353 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
354 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); | |
355 | if (!data) | |
356 | goto nodata; | |
357 | /* kmalloc(size) might give us more room than requested. | |
358 | * Put skb_shared_info exactly at the end of allocated zone, | |
359 | * to allow max possible filling before reallocation. | |
360 | */ | |
361 | size = SKB_WITH_OVERHEAD(ksize(data)); | |
362 | prefetchw(data + size); | |
363 | ||
364 | /* | |
365 | * Only clear those fields we need to clear, not those that we will | |
366 | * actually initialise below. Hence, don't put any more fields after | |
367 | * the tail pointer in struct sk_buff! | |
368 | */ | |
369 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
370 | /* Account for allocated memory : skb + skb->head */ | |
371 | skb->truesize = SKB_TRUESIZE(size); | |
372 | skb->pfmemalloc = pfmemalloc; | |
373 | refcount_set(&skb->users, 1); | |
374 | skb->head = data; | |
375 | skb->data = data; | |
376 | skb_reset_tail_pointer(skb); | |
377 | skb->end = skb->tail + size; | |
378 | skb->mac_header = (typeof(skb->mac_header))~0U; | |
379 | skb->transport_header = (typeof(skb->transport_header))~0U; | |
380 | ||
381 | /* make sure we initialize shinfo sequentially */ | |
382 | shinfo = skb_shinfo(skb); | |
383 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); | |
384 | atomic_set(&shinfo->dataref, 1); | |
385 | ||
386 | if (flags & SKB_ALLOC_FCLONE) { | |
387 | struct sk_buff_fclones *fclones; | |
388 | ||
389 | fclones = container_of(skb, struct sk_buff_fclones, skb1); | |
390 | ||
391 | skb->fclone = SKB_FCLONE_ORIG; | |
392 | refcount_set(&fclones->fclone_ref, 1); | |
393 | ||
394 | fclones->skb2.fclone = SKB_FCLONE_CLONE; | |
395 | } | |
396 | ||
397 | skb_set_kcov_handle(skb, kcov_common_handle()); | |
398 | ||
399 | out: | |
400 | return skb; | |
401 | nodata: | |
402 | kmem_cache_free(cache, skb); | |
403 | skb = NULL; | |
404 | goto out; | |
405 | } | |
406 | EXPORT_SYMBOL(__alloc_skb); | |
407 | ||
fd11a83d AD |
408 | /** |
409 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device | |
410 | * @dev: network device to receive on | |
d7499160 | 411 | * @len: length to allocate |
fd11a83d AD |
412 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
413 | * | |
414 | * Allocate a new &sk_buff and assign it a usage count of one. The | |
415 | * buffer has NET_SKB_PAD headroom built in. Users should allocate | |
416 | * the headroom they think they need without accounting for the | |
417 | * built in space. The built in space is used for optimisations. | |
418 | * | |
419 | * %NULL is returned if there is no free memory. | |
420 | */ | |
9451980a AD |
421 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, |
422 | gfp_t gfp_mask) | |
fd11a83d | 423 | { |
b63ae8ca | 424 | struct page_frag_cache *nc; |
fd11a83d | 425 | struct sk_buff *skb; |
9451980a AD |
426 | bool pfmemalloc; |
427 | void *data; | |
428 | ||
429 | len += NET_SKB_PAD; | |
fd11a83d | 430 | |
66c55602 AL |
431 | /* If requested length is either too small or too big, |
432 | * we use kmalloc() for skb->head allocation. | |
433 | */ | |
434 | if (len <= SKB_WITH_OVERHEAD(1024) || | |
435 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || | |
d0164adc | 436 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
a080e7bd AD |
437 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
438 | if (!skb) | |
439 | goto skb_fail; | |
440 | goto skb_success; | |
441 | } | |
fd11a83d | 442 | |
9451980a AD |
443 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
444 | len = SKB_DATA_ALIGN(len); | |
445 | ||
446 | if (sk_memalloc_socks()) | |
447 | gfp_mask |= __GFP_MEMALLOC; | |
448 | ||
92dcabd7 SAS |
449 | if (in_irq() || irqs_disabled()) { |
450 | nc = this_cpu_ptr(&netdev_alloc_cache); | |
451 | data = page_frag_alloc(nc, len, gfp_mask); | |
452 | pfmemalloc = nc->pfmemalloc; | |
453 | } else { | |
454 | local_bh_disable(); | |
455 | nc = this_cpu_ptr(&napi_alloc_cache.page); | |
456 | data = page_frag_alloc(nc, len, gfp_mask); | |
457 | pfmemalloc = nc->pfmemalloc; | |
458 | local_bh_enable(); | |
459 | } | |
9451980a AD |
460 | |
461 | if (unlikely(!data)) | |
462 | return NULL; | |
463 | ||
464 | skb = __build_skb(data, len); | |
465 | if (unlikely(!skb)) { | |
181edb2b | 466 | skb_free_frag(data); |
9451980a | 467 | return NULL; |
7b2e497a | 468 | } |
fd11a83d | 469 | |
9451980a AD |
470 | if (pfmemalloc) |
471 | skb->pfmemalloc = 1; | |
472 | skb->head_frag = 1; | |
473 | ||
a080e7bd | 474 | skb_success: |
9451980a AD |
475 | skb_reserve(skb, NET_SKB_PAD); |
476 | skb->dev = dev; | |
477 | ||
a080e7bd | 478 | skb_fail: |
8af27456 CH |
479 | return skb; |
480 | } | |
b4ac530f | 481 | EXPORT_SYMBOL(__netdev_alloc_skb); |
1da177e4 | 482 | |
fd11a83d AD |
483 | /** |
484 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance | |
485 | * @napi: napi instance this buffer was allocated for | |
d7499160 | 486 | * @len: length to allocate |
fd11a83d AD |
487 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
488 | * | |
489 | * Allocate a new sk_buff for use in NAPI receive. This buffer will | |
490 | * attempt to allocate the head from a special reserved region used | |
491 | * only for NAPI Rx allocation. By doing this we can save several | |
492 | * CPU cycles by avoiding having to disable and re-enable IRQs. | |
493 | * | |
494 | * %NULL is returned if there is no free memory. | |
495 | */ | |
9451980a AD |
496 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, |
497 | gfp_t gfp_mask) | |
fd11a83d | 498 | { |
3226b158 | 499 | struct napi_alloc_cache *nc; |
fd11a83d | 500 | struct sk_buff *skb; |
9451980a AD |
501 | void *data; |
502 | ||
503 | len += NET_SKB_PAD + NET_IP_ALIGN; | |
fd11a83d | 504 | |
3226b158 ED |
505 | /* If requested length is either too small or too big, |
506 | * we use kmalloc() for skb->head allocation. | |
507 | */ | |
508 | if (len <= SKB_WITH_OVERHEAD(1024) || | |
509 | len > SKB_WITH_OVERHEAD(PAGE_SIZE) || | |
d0164adc | 510 | (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { |
a080e7bd AD |
511 | skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); |
512 | if (!skb) | |
513 | goto skb_fail; | |
514 | goto skb_success; | |
515 | } | |
9451980a | 516 | |
3226b158 | 517 | nc = this_cpu_ptr(&napi_alloc_cache); |
9451980a AD |
518 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
519 | len = SKB_DATA_ALIGN(len); | |
520 | ||
521 | if (sk_memalloc_socks()) | |
522 | gfp_mask |= __GFP_MEMALLOC; | |
fd11a83d | 523 | |
8c2dd3e4 | 524 | data = page_frag_alloc(&nc->page, len, gfp_mask); |
9451980a AD |
525 | if (unlikely(!data)) |
526 | return NULL; | |
527 | ||
528 | skb = __build_skb(data, len); | |
529 | if (unlikely(!skb)) { | |
181edb2b | 530 | skb_free_frag(data); |
9451980a | 531 | return NULL; |
fd11a83d AD |
532 | } |
533 | ||
795bb1c0 | 534 | if (nc->page.pfmemalloc) |
9451980a AD |
535 | skb->pfmemalloc = 1; |
536 | skb->head_frag = 1; | |
537 | ||
a080e7bd | 538 | skb_success: |
9451980a AD |
539 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
540 | skb->dev = napi->dev; | |
541 | ||
a080e7bd | 542 | skb_fail: |
fd11a83d AD |
543 | return skb; |
544 | } | |
545 | EXPORT_SYMBOL(__napi_alloc_skb); | |
546 | ||
654bed16 | 547 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
50269e19 | 548 | int size, unsigned int truesize) |
654bed16 PZ |
549 | { |
550 | skb_fill_page_desc(skb, i, page, off, size); | |
551 | skb->len += size; | |
552 | skb->data_len += size; | |
50269e19 | 553 | skb->truesize += truesize; |
654bed16 PZ |
554 | } |
555 | EXPORT_SYMBOL(skb_add_rx_frag); | |
556 | ||
f8e617e1 JW |
557 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
558 | unsigned int truesize) | |
559 | { | |
560 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
561 | ||
562 | skb_frag_size_add(frag, size); | |
563 | skb->len += size; | |
564 | skb->data_len += size; | |
565 | skb->truesize += truesize; | |
566 | } | |
567 | EXPORT_SYMBOL(skb_coalesce_rx_frag); | |
568 | ||
27b437c8 | 569 | static void skb_drop_list(struct sk_buff **listp) |
1da177e4 | 570 | { |
bd8a7036 | 571 | kfree_skb_list(*listp); |
27b437c8 | 572 | *listp = NULL; |
1da177e4 LT |
573 | } |
574 | ||
27b437c8 HX |
575 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
576 | { | |
577 | skb_drop_list(&skb_shinfo(skb)->frag_list); | |
578 | } | |
579 | ||
1da177e4 LT |
580 | static void skb_clone_fraglist(struct sk_buff *skb) |
581 | { | |
582 | struct sk_buff *list; | |
583 | ||
fbb398a8 | 584 | skb_walk_frags(skb, list) |
1da177e4 LT |
585 | skb_get(list); |
586 | } | |
587 | ||
d3836f21 ED |
588 | static void skb_free_head(struct sk_buff *skb) |
589 | { | |
181edb2b AD |
590 | unsigned char *head = skb->head; |
591 | ||
d3836f21 | 592 | if (skb->head_frag) |
181edb2b | 593 | skb_free_frag(head); |
d3836f21 | 594 | else |
181edb2b | 595 | kfree(head); |
d3836f21 ED |
596 | } |
597 | ||
5bba1712 | 598 | static void skb_release_data(struct sk_buff *skb) |
1da177e4 | 599 | { |
ff04a771 ED |
600 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
601 | int i; | |
1da177e4 | 602 | |
ff04a771 ED |
603 | if (skb->cloned && |
604 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
605 | &shinfo->dataref)) | |
606 | return; | |
a6686f2f | 607 | |
70c43167 JL |
608 | skb_zcopy_clear(skb, true); |
609 | ||
ff04a771 ED |
610 | for (i = 0; i < shinfo->nr_frags; i++) |
611 | __skb_frag_unref(&shinfo->frags[i]); | |
a6686f2f | 612 | |
ff04a771 ED |
613 | if (shinfo->frag_list) |
614 | kfree_skb_list(shinfo->frag_list); | |
615 | ||
616 | skb_free_head(skb); | |
1da177e4 LT |
617 | } |
618 | ||
619 | /* | |
620 | * Free an skbuff by memory without cleaning the state. | |
621 | */ | |
2d4baff8 | 622 | static void kfree_skbmem(struct sk_buff *skb) |
1da177e4 | 623 | { |
d0bf4a9e | 624 | struct sk_buff_fclones *fclones; |
d179cd12 | 625 | |
d179cd12 DM |
626 | switch (skb->fclone) { |
627 | case SKB_FCLONE_UNAVAILABLE: | |
628 | kmem_cache_free(skbuff_head_cache, skb); | |
6ffe75eb | 629 | return; |
d179cd12 DM |
630 | |
631 | case SKB_FCLONE_ORIG: | |
d0bf4a9e | 632 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
d179cd12 | 633 | |
6ffe75eb ED |
634 | /* We usually free the clone (TX completion) before original skb |
635 | * This test would have no chance to be true for the clone, | |
636 | * while here, branch prediction will be good. | |
d179cd12 | 637 | */ |
2638595a | 638 | if (refcount_read(&fclones->fclone_ref) == 1) |
6ffe75eb ED |
639 | goto fastpath; |
640 | break; | |
e7820e39 | 641 | |
6ffe75eb ED |
642 | default: /* SKB_FCLONE_CLONE */ |
643 | fclones = container_of(skb, struct sk_buff_fclones, skb2); | |
d179cd12 | 644 | break; |
3ff50b79 | 645 | } |
2638595a | 646 | if (!refcount_dec_and_test(&fclones->fclone_ref)) |
6ffe75eb ED |
647 | return; |
648 | fastpath: | |
649 | kmem_cache_free(skbuff_fclone_cache, fclones); | |
1da177e4 LT |
650 | } |
651 | ||
0a463c78 | 652 | void skb_release_head_state(struct sk_buff *skb) |
1da177e4 | 653 | { |
adf30907 | 654 | skb_dst_drop(skb); |
9c2b3328 SH |
655 | if (skb->destructor) { |
656 | WARN_ON(in_irq()); | |
1da177e4 LT |
657 | skb->destructor(skb); |
658 | } | |
a3bf7ae9 | 659 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
cb9c6836 | 660 | nf_conntrack_put(skb_nfct(skb)); |
1da177e4 | 661 | #endif |
df5042f4 | 662 | skb_ext_put(skb); |
04a4bb55 LB |
663 | } |
664 | ||
665 | /* Free everything but the sk_buff shell. */ | |
666 | static void skb_release_all(struct sk_buff *skb) | |
667 | { | |
668 | skb_release_head_state(skb); | |
a28b1b90 FW |
669 | if (likely(skb->head)) |
670 | skb_release_data(skb); | |
2d4baff8 HX |
671 | } |
672 | ||
673 | /** | |
674 | * __kfree_skb - private function | |
675 | * @skb: buffer | |
676 | * | |
677 | * Free an sk_buff. Release anything attached to the buffer. | |
678 | * Clean the state. This is an internal helper function. Users should | |
679 | * always call kfree_skb | |
680 | */ | |
1da177e4 | 681 | |
2d4baff8 HX |
682 | void __kfree_skb(struct sk_buff *skb) |
683 | { | |
684 | skb_release_all(skb); | |
1da177e4 LT |
685 | kfree_skbmem(skb); |
686 | } | |
b4ac530f | 687 | EXPORT_SYMBOL(__kfree_skb); |
1da177e4 | 688 | |