1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <net/xsk_buff_pool.h>
4 #include <net/xdp_sock.h>
5 #include <net/xdp_sock_drv.h>
6
7 #include "xsk_queue.h"
8 #include "xdp_umem.h"
9 #include "xsk.h"
10
xp_add_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
12 {
13 unsigned long flags;
14
15 if (!xs->tx)
16 return;
17
18 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
19 list_add_rcu(&xs->tx_list, &pool->xsk_tx_list);
20 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
21 }
22
xp_del_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
24 {
25 unsigned long flags;
26
27 if (!xs->tx)
28 return;
29
30 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
31 list_del_rcu(&xs->tx_list);
32 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
33 }
34
xp_destroy(struct xsk_buff_pool * pool)35 void xp_destroy(struct xsk_buff_pool *pool)
36 {
37 if (!pool)
38 return;
39
40 kvfree(pool->heads);
41 kvfree(pool);
42 }
43
xp_create_and_assign_umem(struct xdp_sock * xs,struct xdp_umem * umem)44 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
45 struct xdp_umem *umem)
46 {
47 struct xsk_buff_pool *pool;
48 struct xdp_buff_xsk *xskb;
49 u32 i;
50
51 pool = kvzalloc(struct_size(pool, free_heads, umem->chunks),
52 GFP_KERNEL);
53 if (!pool)
54 goto out;
55
56 pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
57 if (!pool->heads)
58 goto out;
59
60 pool->chunk_mask = ~((u64)umem->chunk_size - 1);
61 pool->addrs_cnt = umem->size;
62 pool->heads_cnt = umem->chunks;
63 pool->free_heads_cnt = umem->chunks;
64 pool->headroom = umem->headroom;
65 pool->chunk_size = umem->chunk_size;
66 pool->unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
67 pool->frame_len = umem->chunk_size - umem->headroom -
68 XDP_PACKET_HEADROOM;
69 pool->umem = umem;
70 pool->addrs = umem->addrs;
71 INIT_LIST_HEAD(&pool->free_list);
72 INIT_LIST_HEAD(&pool->xsk_tx_list);
73 spin_lock_init(&pool->xsk_tx_list_lock);
74 refcount_set(&pool->users, 1);
75
76 pool->fq = xs->fq_tmp;
77 pool->cq = xs->cq_tmp;
78 xs->fq_tmp = NULL;
79 xs->cq_tmp = NULL;
80
81 for (i = 0; i < pool->free_heads_cnt; i++) {
82 xskb = &pool->heads[i];
83 xskb->pool = pool;
84 xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
85 pool->free_heads[i] = xskb;
86 }
87
88 return pool;
89
90 out:
91 xp_destroy(pool);
92 return NULL;
93 }
94
xp_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)95 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
96 {
97 u32 i;
98
99 for (i = 0; i < pool->heads_cnt; i++)
100 pool->heads[i].xdp.rxq = rxq;
101 }
102 EXPORT_SYMBOL(xp_set_rxq_info);
103
xp_disable_drv_zc(struct xsk_buff_pool * pool)104 static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
105 {
106 struct netdev_bpf bpf;
107 int err;
108
109 ASSERT_RTNL();
110
111 if (pool->umem->zc) {
112 bpf.command = XDP_SETUP_XSK_POOL;
113 bpf.xsk.pool = NULL;
114 bpf.xsk.queue_id = pool->queue_id;
115
116 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
117
118 if (err)
119 WARN(1, "Failed to disable zero-copy!\n");
120 }
121 }
122
__xp_assign_dev(struct xsk_buff_pool * pool,struct net_device * netdev,u16 queue_id,u16 flags)123 static int __xp_assign_dev(struct xsk_buff_pool *pool,
124 struct net_device *netdev, u16 queue_id, u16 flags)
125 {
126 bool force_zc, force_copy;
127 struct netdev_bpf bpf;
128 int err = 0;
129
130 ASSERT_RTNL();
131
132 force_zc = flags & XDP_ZEROCOPY;
133 force_copy = flags & XDP_COPY;
134
135 if (force_zc && force_copy)
136 return -EINVAL;
137
138 if (xsk_get_pool_from_qid(netdev, queue_id))
139 return -EBUSY;
140
141 pool->netdev = netdev;
142 pool->queue_id = queue_id;
143 err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
144 if (err)
145 return err;
146
147 if (flags & XDP_USE_NEED_WAKEUP) {
148 pool->uses_need_wakeup = true;
149 /* Tx needs to be explicitly woken up the first time.
150 * Also for supporting drivers that do not implement this
151 * feature. They will always have to call sendto().
152 */
153 pool->cached_need_wakeup = XDP_WAKEUP_TX;
154 }
155
156 dev_hold(netdev);
157
158 if (force_copy)
159 /* For copy-mode, we are done. */
160 return 0;
161
162 if (!netdev->netdev_ops->ndo_bpf ||
163 !netdev->netdev_ops->ndo_xsk_wakeup) {
164 err = -EOPNOTSUPP;
165 goto err_unreg_pool;
166 }
167
168 bpf.command = XDP_SETUP_XSK_POOL;
169 bpf.xsk.pool = pool;
170 bpf.xsk.queue_id = queue_id;
171
172 err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
173 if (err)
174 goto err_unreg_pool;
175
176 if (!pool->dma_pages) {
177 WARN(1, "Driver did not DMA map zero-copy buffers");
178 err = -EINVAL;
179 goto err_unreg_xsk;
180 }
181 pool->umem->zc = true;
182 return 0;
183
184 err_unreg_xsk:
185 xp_disable_drv_zc(pool);
186 err_unreg_pool:
187 if (!force_zc)
188 err = 0; /* fallback to copy mode */
189 if (err) {
190 xsk_clear_pool_at_qid(netdev, queue_id);
191 dev_put(netdev);
192 }
193 return err;
194 }
195
xp_assign_dev(struct xsk_buff_pool * pool,struct net_device * dev,u16 queue_id,u16 flags)196 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
197 u16 queue_id, u16 flags)
198 {
199 return __xp_assign_dev(pool, dev, queue_id, flags);
200 }
201
xp_assign_dev_shared(struct xsk_buff_pool * pool,struct xdp_umem * umem,struct net_device * dev,u16 queue_id)202 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
203 struct net_device *dev, u16 queue_id)
204 {
205 u16 flags;
206
207 /* One fill and completion ring required for each queue id. */
208 if (!pool->fq || !pool->cq)
209 return -EINVAL;
210
211 flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
212 if (pool->uses_need_wakeup)
213 flags |= XDP_USE_NEED_WAKEUP;
214
215 return __xp_assign_dev(pool, dev, queue_id, flags);
216 }
217
xp_clear_dev(struct xsk_buff_pool * pool)218 void xp_clear_dev(struct xsk_buff_pool *pool)
219 {
220 if (!pool->netdev)
221 return;
222
223 xp_disable_drv_zc(pool);
224 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
225 dev_put(pool->netdev);
226 pool->netdev = NULL;
227 }
228
xp_release_deferred(struct work_struct * work)229 static void xp_release_deferred(struct work_struct *work)
230 {
231 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
232 work);
233
234 rtnl_lock();
235 xp_clear_dev(pool);
236 rtnl_unlock();
237
238 if (pool->fq) {
239 xskq_destroy(pool->fq);
240 pool->fq = NULL;
241 }
242
243 if (pool->cq) {
244 xskq_destroy(pool->cq);
245 pool->cq = NULL;
246 }
247
248 xdp_put_umem(pool->umem, false);
249 xp_destroy(pool);
250 }
251
xp_get_pool(struct xsk_buff_pool * pool)252 void xp_get_pool(struct xsk_buff_pool *pool)
253 {
254 refcount_inc(&pool->users);
255 }
256
xp_put_pool(struct xsk_buff_pool * pool)257 bool xp_put_pool(struct xsk_buff_pool *pool)
258 {
259 if (!pool)
260 return false;
261
262 if (refcount_dec_and_test(&pool->users)) {
263 INIT_WORK(&pool->work, xp_release_deferred);
264 schedule_work(&pool->work);
265 return true;
266 }
267
268 return false;
269 }
270
xp_find_dma_map(struct xsk_buff_pool * pool)271 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
272 {
273 struct xsk_dma_map *dma_map;
274
275 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
276 if (dma_map->netdev == pool->netdev)
277 return dma_map;
278 }
279
280 return NULL;
281 }
282
xp_create_dma_map(struct device * dev,struct net_device * netdev,u32 nr_pages,struct xdp_umem * umem)283 static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
284 u32 nr_pages, struct xdp_umem *umem)
285 {
286 struct xsk_dma_map *dma_map;
287
288 dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
289 if (!dma_map)
290 return NULL;
291
292 dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
293 if (!dma_map->dma_pages) {
294 kfree(dma_map);
295 return NULL;
296 }
297
298 dma_map->netdev = netdev;
299 dma_map->dev = dev;
300 dma_map->dma_need_sync = false;
301 dma_map->dma_pages_cnt = nr_pages;
302 refcount_set(&dma_map->users, 1);
303 list_add(&dma_map->list, &umem->xsk_dma_list);
304 return dma_map;
305 }
306
xp_destroy_dma_map(struct xsk_dma_map * dma_map)307 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
308 {
309 list_del(&dma_map->list);
310 kvfree(dma_map->dma_pages);
311 kfree(dma_map);
312 }
313
__xp_dma_unmap(struct xsk_dma_map * dma_map,unsigned long attrs)314 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
315 {
316 dma_addr_t *dma;
317 u32 i;
318
319 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
320 dma = &dma_map->dma_pages[i];
321 if (*dma) {
322 dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
323 DMA_BIDIRECTIONAL, attrs);
324 *dma = 0;
325 }
326 }
327
328 xp_destroy_dma_map(dma_map);
329 }
330
xp_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)331 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
332 {
333 struct xsk_dma_map *dma_map;
334
335 if (pool->dma_pages_cnt == 0)
336 return;
337
338 dma_map = xp_find_dma_map(pool);
339 if (!dma_map) {
340 WARN(1, "Could not find dma_map for device");
341 return;
342 }
343
344 if (!refcount_dec_and_test(&dma_map->users))
345 return;
346
347 __xp_dma_unmap(dma_map, attrs);
348 kvfree(pool->dma_pages);
349 pool->dma_pages_cnt = 0;
350 pool->dev = NULL;
351 }
352 EXPORT_SYMBOL(xp_dma_unmap);
353
xp_check_dma_contiguity(struct xsk_dma_map * dma_map)354 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
355 {
356 u32 i;
357
358 for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
359 if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
360 dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
361 else
362 dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
363 }
364 }
365
xp_init_dma_info(struct xsk_buff_pool * pool,struct xsk_dma_map * dma_map)366 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
367 {
368 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
369 if (!pool->dma_pages)
370 return -ENOMEM;
371
372 pool->dev = dma_map->dev;
373 pool->dma_pages_cnt = dma_map->dma_pages_cnt;
374 pool->dma_need_sync = dma_map->dma_need_sync;
375 memcpy(pool->dma_pages, dma_map->dma_pages,
376 pool->dma_pages_cnt * sizeof(*pool->dma_pages));
377
378 return 0;
379 }
380
xp_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs,struct page ** pages,u32 nr_pages)381 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
382 unsigned long attrs, struct page **pages, u32 nr_pages)
383 {
384 struct xsk_dma_map *dma_map;
385 dma_addr_t dma;
386 int err;
387 u32 i;
388
389 dma_map = xp_find_dma_map(pool);
390 if (dma_map) {
391 err = xp_init_dma_info(pool, dma_map);
392 if (err)
393 return err;
394
395 refcount_inc(&dma_map->users);
396 return 0;
397 }
398
399 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
400 if (!dma_map)
401 return -ENOMEM;
402
403 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
404 dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
405 DMA_BIDIRECTIONAL, attrs);
406 if (dma_mapping_error(dev, dma)) {
407 __xp_dma_unmap(dma_map, attrs);
408 return -ENOMEM;
409 }
410 if (dma_need_sync(dev, dma))
411 dma_map->dma_need_sync = true;
412 dma_map->dma_pages[i] = dma;
413 }
414
415 if (pool->unaligned)
416 xp_check_dma_contiguity(dma_map);
417
418 err = xp_init_dma_info(pool, dma_map);
419 if (err) {
420 __xp_dma_unmap(dma_map, attrs);
421 return err;
422 }
423
424 return 0;
425 }
426 EXPORT_SYMBOL(xp_dma_map);
427
xp_addr_crosses_non_contig_pg(struct xsk_buff_pool * pool,u64 addr)428 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
429 u64 addr)
430 {
431 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
432 }
433
xp_check_unaligned(struct xsk_buff_pool * pool,u64 * addr)434 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
435 {
436 *addr = xp_unaligned_extract_addr(*addr);
437 if (*addr >= pool->addrs_cnt ||
438 *addr + pool->chunk_size > pool->addrs_cnt ||
439 xp_addr_crosses_non_contig_pg(pool, *addr))
440 return false;
441 return true;
442 }
443
xp_check_aligned(struct xsk_buff_pool * pool,u64 * addr)444 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
445 {
446 *addr = xp_aligned_extract_addr(pool, *addr);
447 return *addr < pool->addrs_cnt;
448 }
449
__xp_alloc(struct xsk_buff_pool * pool)450 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
451 {
452 struct xdp_buff_xsk *xskb;
453 u64 addr;
454 bool ok;
455
456 if (pool->free_heads_cnt == 0)
457 return NULL;
458
459 xskb = pool->free_heads[--pool->free_heads_cnt];
460
461 for (;;) {
462 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
463 pool->fq->queue_empty_descs++;
464 xp_release(xskb);
465 return NULL;
466 }
467
468 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
469 xp_check_aligned(pool, &addr);
470 if (!ok) {
471 pool->fq->invalid_descs++;
472 xskq_cons_release(pool->fq);
473 continue;
474 }
475 break;
476 }
477 xskq_cons_release(pool->fq);
478
479 xskb->orig_addr = addr;
480 xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
481 if (pool->dma_pages_cnt) {
482 xskb->frame_dma = (pool->dma_pages[addr >> PAGE_SHIFT] &
483 ~XSK_NEXT_PG_CONTIG_MASK) +
484 (addr & ~PAGE_MASK);
485 xskb->dma = xskb->frame_dma + pool->headroom +
486 XDP_PACKET_HEADROOM;
487 }
488 return xskb;
489 }
490
xp_alloc(struct xsk_buff_pool * pool)491 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
492 {
493 struct xdp_buff_xsk *xskb;
494
495 if (!pool->free_list_cnt) {
496 xskb = __xp_alloc(pool);
497 if (!xskb)
498 return NULL;
499 } else {
500 pool->free_list_cnt--;
501 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
502 free_list_node);
503 list_del(&xskb->free_list_node);
504 }
505
506 xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
507 xskb->xdp.data_meta = xskb->xdp.data;
508
509 if (pool->dma_need_sync) {
510 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
511 pool->frame_len,
512 DMA_BIDIRECTIONAL);
513 }
514 return &xskb->xdp;
515 }
516 EXPORT_SYMBOL(xp_alloc);
517
xp_can_alloc(struct xsk_buff_pool * pool,u32 count)518 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
519 {
520 if (pool->free_list_cnt >= count)
521 return true;
522 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
523 }
524 EXPORT_SYMBOL(xp_can_alloc);
525
xp_free(struct xdp_buff_xsk * xskb)526 void xp_free(struct xdp_buff_xsk *xskb)
527 {
528 xskb->pool->free_list_cnt++;
529 list_add(&xskb->free_list_node, &xskb->pool->free_list);
530 }
531 EXPORT_SYMBOL(xp_free);
532
xp_raw_get_data(struct xsk_buff_pool * pool,u64 addr)533 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
534 {
535 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
536 return pool->addrs + addr;
537 }
538 EXPORT_SYMBOL(xp_raw_get_data);
539
xp_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)540 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
541 {
542 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
543 return (pool->dma_pages[addr >> PAGE_SHIFT] &
544 ~XSK_NEXT_PG_CONTIG_MASK) +
545 (addr & ~PAGE_MASK);
546 }
547 EXPORT_SYMBOL(xp_raw_get_dma);
548
xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk * xskb)549 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
550 {
551 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
552 xskb->pool->frame_len, DMA_BIDIRECTIONAL);
553 }
554 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
555
xp_dma_sync_for_device_slow(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)556 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
557 size_t size)
558 {
559 dma_sync_single_range_for_device(pool->dev, dma, 0,
560 size, DMA_BIDIRECTIONAL);
561 }
562 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);
563