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