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