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