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