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