Commit | Line | Data |
---|---|---|
2b43470a BT |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
97246d6d | 3 | #include <linux/netdevice.h> |
8ef890df | 4 | #include <net/netdev_lock.h> |
2b43470a BT |
5 | #include <net/xsk_buff_pool.h> |
6 | #include <net/xdp_sock.h> | |
1c1efc2a | 7 | #include <net/xdp_sock_drv.h> |
2b43470a BT |
8 | |
9 | #include "xsk_queue.h" | |
1c1efc2a MK |
10 | #include "xdp_umem.h" |
11 | #include "xsk.h" | |
2b43470a | 12 | |
a5aa8e52 MK |
13 | void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) |
14 | { | |
15 | unsigned long flags; | |
16 | ||
17 | if (!xs->tx) | |
18 | return; | |
19 | ||
20 | spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); | |
21 | list_add_rcu(&xs->tx_list, &pool->xsk_tx_list); | |
22 | spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); | |
23 | } | |
24 | ||
25 | void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) | |
26 | { | |
27 | unsigned long flags; | |
28 | ||
29 | if (!xs->tx) | |
30 | return; | |
31 | ||
32 | spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); | |
33 | list_del_rcu(&xs->tx_list); | |
34 | spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); | |
35 | } | |
36 | ||
2b43470a BT |
37 | void xp_destroy(struct xsk_buff_pool *pool) |
38 | { | |
39 | if (!pool) | |
40 | return; | |
41 | ||
d1bc532e | 42 | kvfree(pool->tx_descs); |
2b43470a BT |
43 | kvfree(pool->heads); |
44 | kvfree(pool); | |
45 | } | |
46 | ||
ba3beec2 MF |
47 | int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs) |
48 | { | |
49 | pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs), | |
50 | GFP_KERNEL); | |
51 | if (!pool->tx_descs) | |
52 | return -ENOMEM; | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
1c1efc2a MK |
57 | struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, |
58 | struct xdp_umem *umem) | |
2b43470a | 59 | { |
94033cd8 | 60 | bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
2b43470a BT |
61 | struct xsk_buff_pool *pool; |
62 | struct xdp_buff_xsk *xskb; | |
94033cd8 | 63 | u32 i, entries; |
2b43470a | 64 | |
94033cd8 MK |
65 | entries = unaligned ? umem->chunks : 0; |
66 | pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL); | |
2b43470a BT |
67 | if (!pool) |
68 | goto out; | |
69 | ||
1c1efc2a | 70 | pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); |
2b43470a BT |
71 | if (!pool->heads) |
72 | goto out; | |
73 | ||
ba3beec2 MF |
74 | if (xs->tx) |
75 | if (xp_alloc_tx_descs(pool, xs)) | |
d1bc532e | 76 | goto out; |
d1bc532e | 77 | |
1c1efc2a MK |
78 | pool->chunk_mask = ~((u64)umem->chunk_size - 1); |
79 | pool->addrs_cnt = umem->size; | |
80 | pool->heads_cnt = umem->chunks; | |
81 | pool->free_heads_cnt = umem->chunks; | |
82 | pool->headroom = umem->headroom; | |
83 | pool->chunk_size = umem->chunk_size; | |
94033cd8 MK |
84 | pool->chunk_shift = ffs(umem->chunk_size) - 1; |
85 | pool->unaligned = unaligned; | |
1c1efc2a MK |
86 | pool->frame_len = umem->chunk_size - umem->headroom - |
87 | XDP_PACKET_HEADROOM; | |
1742b3d5 | 88 | pool->umem = umem; |
7f7ffa4e | 89 | pool->addrs = umem->addrs; |
341ac980 | 90 | pool->tx_metadata_len = umem->tx_metadata_len; |
11614723 | 91 | pool->tx_sw_csum = umem->flags & XDP_UMEM_TX_SW_CSUM; |
a1356ac7 | 92 | spin_lock_init(&pool->rx_lock); |
2b43470a | 93 | INIT_LIST_HEAD(&pool->free_list); |
24ea5012 | 94 | INIT_LIST_HEAD(&pool->xskb_list); |
a5aa8e52 MK |
95 | INIT_LIST_HEAD(&pool->xsk_tx_list); |
96 | spin_lock_init(&pool->xsk_tx_list_lock); | |
f09ced40 | 97 | spin_lock_init(&pool->cq_lock); |
1c1efc2a | 98 | refcount_set(&pool->users, 1); |
2b43470a | 99 | |
7361f9c3 MK |
100 | pool->fq = xs->fq_tmp; |
101 | pool->cq = xs->cq_tmp; | |
7361f9c3 | 102 | |
2b43470a BT |
103 | for (i = 0; i < pool->free_heads_cnt; i++) { |
104 | xskb = &pool->heads[i]; | |
105 | xskb->pool = pool; | |
1c1efc2a | 106 | xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; |
30ec2c1b | 107 | INIT_LIST_HEAD(&xskb->list_node); |
94033cd8 MK |
108 | if (pool->unaligned) |
109 | pool->free_heads[i] = xskb; | |
110 | else | |
559847f5 | 111 | xp_init_xskb_addr(xskb, pool, (u64)i * pool->chunk_size); |
2b43470a BT |
112 | } |
113 | ||
7f7ffa4e | 114 | return pool; |
2b43470a BT |
115 | |
116 | out: | |
117 | xp_destroy(pool); | |
118 | return NULL; | |
119 | } | |
120 | ||
2b43470a BT |
121 | void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) |
122 | { | |
123 | u32 i; | |
124 | ||
125 | for (i = 0; i < pool->heads_cnt; i++) | |
126 | pool->heads[i].xdp.rxq = rxq; | |
127 | } | |
128 | EXPORT_SYMBOL(xp_set_rxq_info); | |
129 | ||
b4e352ff MF |
130 | void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc) |
131 | { | |
132 | u32 i; | |
133 | ||
134 | for (i = 0; i < pool->heads_cnt; i++) { | |
135 | struct xdp_buff_xsk *xskb = &pool->heads[i]; | |
136 | ||
137 | memcpy(xskb->cb + desc->off, desc->src, desc->bytes); | |
138 | } | |
139 | } | |
140 | EXPORT_SYMBOL(xp_fill_cb); | |
141 | ||
921b6869 MK |
142 | static void xp_disable_drv_zc(struct xsk_buff_pool *pool) |
143 | { | |
144 | struct netdev_bpf bpf; | |
145 | int err; | |
146 | ||
147 | ASSERT_RTNL(); | |
148 | ||
149 | if (pool->umem->zc) { | |
150 | bpf.command = XDP_SETUP_XSK_POOL; | |
151 | bpf.xsk.pool = NULL; | |
152 | bpf.xsk.queue_id = pool->queue_id; | |
153 | ||
154 | err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf); | |
155 | ||
156 | if (err) | |
157 | WARN(1, "Failed to disable zero-copy!\n"); | |
158 | } | |
159 | } | |
160 | ||
0ae0cb2b MM |
161 | #define NETDEV_XDP_ACT_ZC (NETDEV_XDP_ACT_BASIC | \ |
162 | NETDEV_XDP_ACT_REDIRECT | \ | |
163 | NETDEV_XDP_ACT_XSK_ZEROCOPY) | |
164 | ||
f0863eab BT |
165 | int xp_assign_dev(struct xsk_buff_pool *pool, |
166 | struct net_device *netdev, u16 queue_id, u16 flags) | |
1c1efc2a | 167 | { |
1c1efc2a MK |
168 | bool force_zc, force_copy; |
169 | struct netdev_bpf bpf; | |
170 | int err = 0; | |
171 | ||
172 | ASSERT_RTNL(); | |
173 | ||
174 | force_zc = flags & XDP_ZEROCOPY; | |
175 | force_copy = flags & XDP_COPY; | |
176 | ||
177 | if (force_zc && force_copy) | |
178 | return -EINVAL; | |
179 | ||
c2d3d6a4 | 180 | if (xsk_get_pool_from_qid(netdev, queue_id)) |
1c1efc2a MK |
181 | return -EBUSY; |
182 | ||
921b6869 MK |
183 | pool->netdev = netdev; |
184 | pool->queue_id = queue_id; | |
c2d3d6a4 | 185 | err = xsk_reg_pool_at_qid(netdev, pool, queue_id); |
1c1efc2a MK |
186 | if (err) |
187 | return err; | |
188 | ||
d609f3d2 TS |
189 | if (flags & XDP_USE_SG) |
190 | pool->umem->flags |= XDP_UMEM_SG_FLAG; | |
191 | ||
e3920818 | 192 | if (flags & XDP_USE_NEED_WAKEUP) |
c2d3d6a4 | 193 | pool->uses_need_wakeup = true; |
e3920818 BT |
194 | /* Tx needs to be explicitly woken up the first time. Also |
195 | * for supporting drivers that do not implement this | |
196 | * feature. They will always have to call sendto() or poll(). | |
197 | */ | |
198 | pool->cached_need_wakeup = XDP_WAKEUP_TX; | |
1c1efc2a | 199 | |
c2d3d6a4 MK |
200 | dev_hold(netdev); |
201 | ||
1c1efc2a MK |
202 | if (force_copy) |
203 | /* For copy-mode, we are done. */ | |
204 | return 0; | |
205 | ||
0ae0cb2b | 206 | if ((netdev->xdp_features & NETDEV_XDP_ACT_ZC) != NETDEV_XDP_ACT_ZC) { |
1c1efc2a MK |
207 | err = -EOPNOTSUPP; |
208 | goto err_unreg_pool; | |
209 | } | |
210 | ||
24ea5012 MF |
211 | if (netdev->xdp_zc_max_segs == 1 && (flags & XDP_USE_SG)) { |
212 | err = -EOPNOTSUPP; | |
213 | goto err_unreg_pool; | |
214 | } | |
215 | ||
170aafe3 MA |
216 | if (dev_get_min_mp_channel_count(netdev)) { |
217 | err = -EBUSY; | |
218 | goto err_unreg_pool; | |
219 | } | |
220 | ||
1c1efc2a MK |
221 | bpf.command = XDP_SETUP_XSK_POOL; |
222 | bpf.xsk.pool = pool; | |
223 | bpf.xsk.queue_id = queue_id; | |
224 | ||
97246d6d | 225 | netdev_ops_assert_locked(netdev); |
c2d3d6a4 | 226 | err = netdev->netdev_ops->ndo_bpf(netdev, &bpf); |
1c1efc2a MK |
227 | if (err) |
228 | goto err_unreg_pool; | |
229 | ||
921b6869 MK |
230 | if (!pool->dma_pages) { |
231 | WARN(1, "Driver did not DMA map zero-copy buffers"); | |
12c8a8ca | 232 | err = -EINVAL; |
921b6869 MK |
233 | goto err_unreg_xsk; |
234 | } | |
c2d3d6a4 | 235 | pool->umem->zc = true; |
6e126872 | 236 | pool->xdp_zc_max_segs = netdev->xdp_zc_max_segs; |
1c1efc2a MK |
237 | return 0; |
238 | ||
921b6869 MK |
239 | err_unreg_xsk: |
240 | xp_disable_drv_zc(pool); | |
1c1efc2a MK |
241 | err_unreg_pool: |
242 | if (!force_zc) | |
243 | err = 0; /* fallback to copy mode */ | |
17864891 | 244 | if (err) { |
c2d3d6a4 | 245 | xsk_clear_pool_at_qid(netdev, queue_id); |
17864891 MM |
246 | dev_put(netdev); |
247 | } | |
1c1efc2a MK |
248 | return err; |
249 | } | |
250 | ||
60240bc2 | 251 | int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs, |
b5aea28d MK |
252 | struct net_device *dev, u16 queue_id) |
253 | { | |
254 | u16 flags; | |
60240bc2 | 255 | struct xdp_umem *umem = umem_xs->umem; |
b5aea28d MK |
256 | |
257 | /* One fill and completion ring required for each queue id. */ | |
258 | if (!pool->fq || !pool->cq) | |
259 | return -EINVAL; | |
260 | ||
261 | flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; | |
60240bc2 | 262 | if (umem_xs->pool->uses_need_wakeup) |
b5aea28d MK |
263 | flags |= XDP_USE_NEED_WAKEUP; |
264 | ||
f0863eab | 265 | return xp_assign_dev(pool, dev, queue_id, flags); |
b5aea28d MK |
266 | } |
267 | ||
1c1efc2a MK |
268 | void xp_clear_dev(struct xsk_buff_pool *pool) |
269 | { | |
606048cb JK |
270 | struct net_device *netdev = pool->netdev; |
271 | ||
c2d3d6a4 | 272 | if (!pool->netdev) |
1c1efc2a MK |
273 | return; |
274 | ||
606048cb | 275 | netdev_lock_ops(netdev); |
921b6869 | 276 | xp_disable_drv_zc(pool); |
c2d3d6a4 | 277 | xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); |
c2d3d6a4 | 278 | pool->netdev = NULL; |
606048cb JK |
279 | netdev_unlock_ops(netdev); |
280 | dev_put(netdev); | |
1c1efc2a MK |
281 | } |
282 | ||
283 | static void xp_release_deferred(struct work_struct *work) | |
284 | { | |
285 | struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, | |
286 | work); | |
287 | ||
288 | rtnl_lock(); | |
289 | xp_clear_dev(pool); | |
290 | rtnl_unlock(); | |
291 | ||
7361f9c3 MK |
292 | if (pool->fq) { |
293 | xskq_destroy(pool->fq); | |
294 | pool->fq = NULL; | |
295 | } | |
296 | ||
297 | if (pool->cq) { | |
298 | xskq_destroy(pool->cq); | |
299 | pool->cq = NULL; | |
300 | } | |
301 | ||
537cf4e3 | 302 | xdp_put_umem(pool->umem, false); |
1c1efc2a MK |
303 | xp_destroy(pool); |
304 | } | |
305 | ||
306 | void xp_get_pool(struct xsk_buff_pool *pool) | |
307 | { | |
308 | refcount_inc(&pool->users); | |
309 | } | |
310 | ||
e5e1a4bc | 311 | bool xp_put_pool(struct xsk_buff_pool *pool) |
1c1efc2a MK |
312 | { |
313 | if (!pool) | |
e5e1a4bc | 314 | return false; |
1c1efc2a MK |
315 | |
316 | if (refcount_dec_and_test(&pool->users)) { | |
317 | INIT_WORK(&pool->work, xp_release_deferred); | |
318 | schedule_work(&pool->work); | |
e5e1a4bc | 319 | return true; |
1c1efc2a | 320 | } |
e5e1a4bc MK |
321 | |
322 | return false; | |
1c1efc2a MK |
323 | } |
324 | ||
921b6869 MK |
325 | static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) |
326 | { | |
327 | struct xsk_dma_map *dma_map; | |
328 | ||
329 | list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { | |
330 | if (dma_map->netdev == pool->netdev) | |
331 | return dma_map; | |
332 | } | |
333 | ||
334 | return NULL; | |
335 | } | |
336 | ||
337 | static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev, | |
338 | u32 nr_pages, struct xdp_umem *umem) | |
339 | { | |
340 | struct xsk_dma_map *dma_map; | |
341 | ||
342 | dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL); | |
343 | if (!dma_map) | |
344 | return NULL; | |
345 | ||
346 | dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL); | |
1d6fd78a | 347 | if (!dma_map->dma_pages) { |
921b6869 MK |
348 | kfree(dma_map); |
349 | return NULL; | |
350 | } | |
351 | ||
352 | dma_map->netdev = netdev; | |
353 | dma_map->dev = dev; | |
921b6869 | 354 | dma_map->dma_pages_cnt = nr_pages; |
bf74a370 | 355 | refcount_set(&dma_map->users, 1); |
921b6869 MK |
356 | list_add(&dma_map->list, &umem->xsk_dma_list); |
357 | return dma_map; | |
358 | } | |
359 | ||
360 | static void xp_destroy_dma_map(struct xsk_dma_map *dma_map) | |
361 | { | |
362 | list_del(&dma_map->list); | |
363 | kvfree(dma_map->dma_pages); | |
364 | kfree(dma_map); | |
365 | } | |
366 | ||
367 | static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs) | |
2b43470a BT |
368 | { |
369 | dma_addr_t *dma; | |
370 | u32 i; | |
371 | ||
921b6869 MK |
372 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
373 | dma = &dma_map->dma_pages[i]; | |
2b43470a | 374 | if (*dma) { |
512d1999 | 375 | *dma &= ~XSK_NEXT_PG_CONTIG_MASK; |
921b6869 | 376 | dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE, |
2b43470a BT |
377 | DMA_BIDIRECTIONAL, attrs); |
378 | *dma = 0; | |
379 | } | |
380 | } | |
381 | ||
921b6869 MK |
382 | xp_destroy_dma_map(dma_map); |
383 | } | |
384 | ||
385 | void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) | |
386 | { | |
387 | struct xsk_dma_map *dma_map; | |
388 | ||
6ec7be9a | 389 | if (!pool->dma_pages) |
921b6869 MK |
390 | return; |
391 | ||
392 | dma_map = xp_find_dma_map(pool); | |
393 | if (!dma_map) { | |
394 | WARN(1, "Could not find dma_map for device"); | |
395 | return; | |
396 | } | |
397 | ||
ac9a48a6 LZ |
398 | if (refcount_dec_and_test(&dma_map->users)) |
399 | __xp_dma_unmap(dma_map, attrs); | |
921b6869 | 400 | |
2b43470a | 401 | kvfree(pool->dma_pages); |
6ec7be9a | 402 | pool->dma_pages = NULL; |
2b43470a BT |
403 | pool->dma_pages_cnt = 0; |
404 | pool->dev = NULL; | |
405 | } | |
406 | EXPORT_SYMBOL(xp_dma_unmap); | |
407 | ||
921b6869 | 408 | static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map) |
2b43470a BT |
409 | { |
410 | u32 i; | |
411 | ||
921b6869 MK |
412 | for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) { |
413 | if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1]) | |
414 | dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK; | |
2b43470a | 415 | else |
921b6869 | 416 | dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK; |
2b43470a BT |
417 | } |
418 | } | |
419 | ||
921b6869 MK |
420 | static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) |
421 | { | |
58ca14ed MK |
422 | if (!pool->unaligned) { |
423 | u32 i; | |
424 | ||
425 | for (i = 0; i < pool->heads_cnt; i++) { | |
426 | struct xdp_buff_xsk *xskb = &pool->heads[i]; | |
bea14124 | 427 | u64 orig_addr; |
58ca14ed | 428 | |
bea14124 MF |
429 | orig_addr = xskb->xdp.data_hard_start - pool->addrs - pool->headroom; |
430 | xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, orig_addr); | |
58ca14ed MK |
431 | } |
432 | } | |
433 | ||
921b6869 MK |
434 | pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); |
435 | if (!pool->dma_pages) | |
436 | return -ENOMEM; | |
437 | ||
438 | pool->dev = dma_map->dev; | |
439 | pool->dma_pages_cnt = dma_map->dma_pages_cnt; | |
921b6869 MK |
440 | memcpy(pool->dma_pages, dma_map->dma_pages, |
441 | pool->dma_pages_cnt * sizeof(*pool->dma_pages)); | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
2b43470a BT |
446 | int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, |
447 | unsigned long attrs, struct page **pages, u32 nr_pages) | |
448 | { | |
921b6869 | 449 | struct xsk_dma_map *dma_map; |
2b43470a | 450 | dma_addr_t dma; |
921b6869 | 451 | int err; |
2b43470a BT |
452 | u32 i; |
453 | ||
921b6869 MK |
454 | dma_map = xp_find_dma_map(pool); |
455 | if (dma_map) { | |
456 | err = xp_init_dma_info(pool, dma_map); | |
457 | if (err) | |
458 | return err; | |
2b43470a | 459 | |
bf74a370 | 460 | refcount_inc(&dma_map->users); |
921b6869 MK |
461 | return 0; |
462 | } | |
2b43470a | 463 | |
921b6869 MK |
464 | dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); |
465 | if (!dma_map) | |
466 | return -ENOMEM; | |
467 | ||
468 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { | |
2b43470a BT |
469 | dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, |
470 | DMA_BIDIRECTIONAL, attrs); | |
471 | if (dma_mapping_error(dev, dma)) { | |
921b6869 | 472 | __xp_dma_unmap(dma_map, attrs); |
2b43470a BT |
473 | return -ENOMEM; |
474 | } | |
921b6869 | 475 | dma_map->dma_pages[i] = dma; |
2b43470a BT |
476 | } |
477 | ||
478 | if (pool->unaligned) | |
921b6869 MK |
479 | xp_check_dma_contiguity(dma_map); |
480 | ||
481 | err = xp_init_dma_info(pool, dma_map); | |
482 | if (err) { | |
483 | __xp_dma_unmap(dma_map, attrs); | |
484 | return err; | |
485 | } | |
486 | ||
2b43470a BT |
487 | return 0; |
488 | } | |
489 | EXPORT_SYMBOL(xp_dma_map); | |
490 | ||
2b43470a BT |
491 | static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, |
492 | u64 addr) | |
493 | { | |
494 | return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); | |
495 | } | |
496 | ||
2b43470a BT |
497 | static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) |
498 | { | |
499 | *addr = xp_unaligned_extract_addr(*addr); | |
500 | if (*addr >= pool->addrs_cnt || | |
501 | *addr + pool->chunk_size > pool->addrs_cnt || | |
502 | xp_addr_crosses_non_contig_pg(pool, *addr)) | |
503 | return false; | |
504 | return true; | |
505 | } | |
506 | ||
507 | static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) | |
508 | { | |
509 | *addr = xp_aligned_extract_addr(pool, *addr); | |
510 | return *addr < pool->addrs_cnt; | |
511 | } | |
512 | ||
1d10b2be MF |
513 | static struct xdp_buff_xsk *xp_get_xskb(struct xsk_buff_pool *pool, u64 addr) |
514 | { | |
515 | struct xdp_buff_xsk *xskb; | |
516 | ||
517 | if (pool->unaligned) { | |
518 | xskb = pool->free_heads[--pool->free_heads_cnt]; | |
519 | xp_init_xskb_addr(xskb, pool, addr); | |
520 | if (pool->dma_pages) | |
521 | xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); | |
522 | } else { | |
523 | xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; | |
524 | } | |
525 | ||
526 | return xskb; | |
527 | } | |
528 | ||
2b43470a BT |
529 | static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) |
530 | { | |
531 | struct xdp_buff_xsk *xskb; | |
532 | u64 addr; | |
533 | bool ok; | |
534 | ||
535 | if (pool->free_heads_cnt == 0) | |
536 | return NULL; | |
537 | ||
2b43470a BT |
538 | for (;;) { |
539 | if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { | |
8aa5a335 | 540 | pool->fq->queue_empty_descs++; |
2b43470a BT |
541 | return NULL; |
542 | } | |
543 | ||
544 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : | |
545 | xp_check_aligned(pool, &addr); | |
546 | if (!ok) { | |
547 | pool->fq->invalid_descs++; | |
548 | xskq_cons_release(pool->fq); | |
549 | continue; | |
550 | } | |
551 | break; | |
552 | } | |
2b43470a | 553 | |
1d10b2be | 554 | xskb = xp_get_xskb(pool, addr); |
94033cd8 MK |
555 | |
556 | xskq_cons_release(pool->fq); | |
2b43470a BT |
557 | return xskb; |
558 | } | |
559 | ||
560 | struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) | |
561 | { | |
562 | struct xdp_buff_xsk *xskb; | |
563 | ||
564 | if (!pool->free_list_cnt) { | |
565 | xskb = __xp_alloc(pool); | |
566 | if (!xskb) | |
567 | return NULL; | |
568 | } else { | |
569 | pool->free_list_cnt--; | |
570 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, | |
30ec2c1b MF |
571 | list_node); |
572 | list_del_init(&xskb->list_node); | |
2b43470a BT |
573 | } |
574 | ||
575 | xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; | |
576 | xskb->xdp.data_meta = xskb->xdp.data; | |
f7f6aa8e | 577 | xskb->xdp.flags = 0; |
2b43470a | 578 | |
163943ac AL |
579 | if (pool->dev) |
580 | xp_dma_sync_for_device(pool, xskb->dma, pool->frame_len); | |
581 | ||
2b43470a BT |
582 | return &xskb->xdp; |
583 | } | |
584 | EXPORT_SYMBOL(xp_alloc); | |
585 | ||
47e4075d MK |
586 | static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) |
587 | { | |
588 | u32 i, cached_cons, nb_entries; | |
589 | ||
590 | if (max > pool->free_heads_cnt) | |
591 | max = pool->free_heads_cnt; | |
592 | max = xskq_cons_nb_entries(pool->fq, max); | |
593 | ||
594 | cached_cons = pool->fq->cached_cons; | |
595 | nb_entries = max; | |
596 | i = max; | |
597 | while (i--) { | |
598 | struct xdp_buff_xsk *xskb; | |
599 | u64 addr; | |
600 | bool ok; | |
601 | ||
602 | __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr); | |
603 | ||
604 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : | |
605 | xp_check_aligned(pool, &addr); | |
606 | if (unlikely(!ok)) { | |
607 | pool->fq->invalid_descs++; | |
608 | nb_entries--; | |
609 | continue; | |
610 | } | |
611 | ||
1d10b2be | 612 | xskb = xp_get_xskb(pool, addr); |
94033cd8 | 613 | |
47e4075d | 614 | *xdp = &xskb->xdp; |
47e4075d MK |
615 | xdp++; |
616 | } | |
617 | ||
618 | xskq_cons_release_n(pool->fq, max); | |
619 | return nb_entries; | |
620 | } | |
621 | ||
622 | static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries) | |
623 | { | |
624 | struct xdp_buff_xsk *xskb; | |
625 | u32 i; | |
626 | ||
627 | nb_entries = min_t(u32, nb_entries, pool->free_list_cnt); | |
628 | ||
629 | i = nb_entries; | |
630 | while (i--) { | |
30ec2c1b MF |
631 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, list_node); |
632 | list_del_init(&xskb->list_node); | |
47e4075d MK |
633 | |
634 | *xdp = &xskb->xdp; | |
635 | xdp++; | |
636 | } | |
637 | pool->free_list_cnt -= nb_entries; | |
638 | ||
639 | return nb_entries; | |
640 | } | |
641 | ||
4144a105 MF |
642 | static u32 xp_alloc_slow(struct xsk_buff_pool *pool, struct xdp_buff **xdp, |
643 | u32 max) | |
47e4075d | 644 | { |
4144a105 | 645 | int i; |
47e4075d | 646 | |
4144a105 | 647 | for (i = 0; i < max; i++) { |
a95a4d9b MK |
648 | struct xdp_buff *buff; |
649 | ||
a95a4d9b | 650 | buff = xp_alloc(pool); |
4144a105 MF |
651 | if (unlikely(!buff)) |
652 | return i; | |
653 | *xdp = buff; | |
654 | xdp++; | |
47e4075d MK |
655 | } |
656 | ||
4144a105 MF |
657 | return max; |
658 | } | |
659 | ||
660 | u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) | |
661 | { | |
662 | u32 nb_entries1 = 0, nb_entries2; | |
663 | ||
664 | if (unlikely(pool->dev && dma_dev_need_sync(pool->dev))) | |
665 | return xp_alloc_slow(pool, xdp, max); | |
666 | ||
47e4075d MK |
667 | if (unlikely(pool->free_list_cnt)) { |
668 | nb_entries1 = xp_alloc_reused(pool, xdp, max); | |
669 | if (nb_entries1 == max) | |
670 | return nb_entries1; | |
671 | ||
672 | max -= nb_entries1; | |
673 | xdp += nb_entries1; | |
674 | } | |
675 | ||
676 | nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max); | |
677 | if (!nb_entries2) | |
678 | pool->fq->queue_empty_descs++; | |
679 | ||
680 | return nb_entries1 + nb_entries2; | |
681 | } | |
682 | EXPORT_SYMBOL(xp_alloc_batch); | |
683 | ||
2b43470a BT |
684 | bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) |
685 | { | |
6b083650 MF |
686 | u32 req_count, avail_count; |
687 | ||
2b43470a BT |
688 | if (pool->free_list_cnt >= count) |
689 | return true; | |
6b083650 MF |
690 | |
691 | req_count = count - pool->free_list_cnt; | |
692 | avail_count = xskq_cons_nb_entries(pool->fq, req_count); | |
693 | if (!avail_count) | |
694 | pool->fq->queue_empty_descs++; | |
695 | ||
696 | return avail_count >= req_count; | |
2b43470a BT |
697 | } |
698 | EXPORT_SYMBOL(xp_can_alloc); | |
699 | ||
700 | void xp_free(struct xdp_buff_xsk *xskb) | |
701 | { | |
30ec2c1b | 702 | if (!list_empty(&xskb->list_node)) |
199d983b MK |
703 | return; |
704 | ||
2b43470a | 705 | xskb->pool->free_list_cnt++; |
30ec2c1b | 706 | list_add(&xskb->list_node, &xskb->pool->free_list); |
2b43470a BT |
707 | } |
708 | EXPORT_SYMBOL(xp_free); | |
709 | ||
23d9324a AL |
710 | static u64 __xp_raw_get_addr(const struct xsk_buff_pool *pool, u64 addr) |
711 | { | |
712 | return pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; | |
713 | } | |
714 | ||
715 | static void *__xp_raw_get_data(const struct xsk_buff_pool *pool, u64 addr) | |
2b43470a | 716 | { |
2b43470a BT |
717 | return pool->addrs + addr; |
718 | } | |
23d9324a AL |
719 | |
720 | void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) | |
721 | { | |
722 | return __xp_raw_get_data(pool, __xp_raw_get_addr(pool, addr)); | |
723 | } | |
2b43470a BT |
724 | EXPORT_SYMBOL(xp_raw_get_data); |
725 | ||
23d9324a | 726 | static dma_addr_t __xp_raw_get_dma(const struct xsk_buff_pool *pool, u64 addr) |
2b43470a | 727 | { |
2b43470a BT |
728 | return (pool->dma_pages[addr >> PAGE_SHIFT] & |
729 | ~XSK_NEXT_PG_CONTIG_MASK) + | |
730 | (addr & ~PAGE_MASK); | |
731 | } | |
23d9324a AL |
732 | |
733 | dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) | |
734 | { | |
735 | return __xp_raw_get_dma(pool, __xp_raw_get_addr(pool, addr)); | |
736 | } | |
2b43470a | 737 | EXPORT_SYMBOL(xp_raw_get_dma); |
23d9324a AL |
738 | |
739 | /** | |
740 | * xp_raw_get_ctx - get &xdp_desc context | |
741 | * @pool: XSk buff pool desc address belongs to | |
742 | * @addr: desc address (from userspace) | |
743 | * | |
744 | * Helper for getting desc's DMA address and metadata pointer, if present. | |
745 | * Saves one call on hotpath, double calculation of the actual address, | |
746 | * and inline checks for metadata presence and sanity. | |
747 | * | |
748 | * Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata | |
749 | * pointer, if it is present and valid (initialized to %NULL otherwise). | |
750 | */ | |
751 | struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) | |
752 | { | |
753 | struct xdp_desc_ctx ret; | |
754 | ||
755 | addr = __xp_raw_get_addr(pool, addr); | |
756 | ||
757 | ret.dma = __xp_raw_get_dma(pool, addr); | |
758 | ret.meta = __xsk_buff_get_metadata(pool, __xp_raw_get_data(pool, addr)); | |
759 | ||
760 | return ret; | |
761 | } | |
762 | EXPORT_SYMBOL(xp_raw_get_ctx); |