1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/dma-map-ops.h> 5 #include <linux/mm.h> 6 #include <linux/nospec.h> 7 #include <linux/io_uring.h> 8 #include <linux/netdevice.h> 9 #include <linux/rtnetlink.h> 10 #include <linux/skbuff_ref.h> 11 #include <linux/anon_inodes.h> 12 13 #include <net/page_pool/helpers.h> 14 #include <net/page_pool/memory_provider.h> 15 #include <net/netlink.h> 16 #include <net/netdev_queues.h> 17 #include <net/netdev_rx_queue.h> 18 #include <net/tcp.h> 19 #include <net/rps.h> 20 21 #include <trace/events/page_pool.h> 22 23 #include <uapi/linux/io_uring.h> 24 25 #include "io_uring.h" 26 #include "kbuf.h" 27 #include "memmap.h" 28 #include "zcrx.h" 29 #include "rsrc.h" 30 31 #define IO_ZCRX_AREA_SUPPORTED_FLAGS (IORING_ZCRX_AREA_DMABUF) 32 33 #define IO_DMA_ATTR (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING) 34 35 static inline struct io_zcrx_ifq *io_pp_to_ifq(struct page_pool *pp) 36 { 37 return pp->mp_priv; 38 } 39 40 static inline struct io_zcrx_area *io_zcrx_iov_to_area(const struct net_iov *niov) 41 { 42 struct net_iov_area *owner = net_iov_owner(niov); 43 44 return container_of(owner, struct io_zcrx_area, nia); 45 } 46 47 static inline struct page *io_zcrx_iov_page(const struct net_iov *niov) 48 { 49 struct io_zcrx_area *area = io_zcrx_iov_to_area(niov); 50 unsigned niov_pages_shift; 51 52 lockdep_assert(!area->mem.is_dmabuf); 53 54 niov_pages_shift = area->ifq->niov_shift - PAGE_SHIFT; 55 return area->mem.pages[net_iov_idx(niov) << niov_pages_shift]; 56 } 57 58 static int io_area_max_shift(struct io_zcrx_mem *mem) 59 { 60 struct sg_table *sgt = mem->sgt; 61 struct scatterlist *sg; 62 unsigned shift = -1U; 63 unsigned i; 64 65 for_each_sgtable_dma_sg(sgt, sg, i) 66 shift = min(shift, __ffs(sg->length)); 67 return shift; 68 } 69 70 static int io_populate_area_dma(struct io_zcrx_ifq *ifq, 71 struct io_zcrx_area *area) 72 { 73 unsigned niov_size = 1U << ifq->niov_shift; 74 struct sg_table *sgt = area->mem.sgt; 75 struct scatterlist *sg; 76 unsigned i, niov_idx = 0; 77 78 for_each_sgtable_dma_sg(sgt, sg, i) { 79 dma_addr_t dma = sg_dma_address(sg); 80 unsigned long sg_len = sg_dma_len(sg); 81 82 if (WARN_ON_ONCE(sg_len % niov_size)) 83 return -EINVAL; 84 85 while (sg_len && niov_idx < area->nia.num_niovs) { 86 struct net_iov *niov = &area->nia.niovs[niov_idx]; 87 88 if (net_mp_niov_set_dma_addr(niov, dma)) 89 return -EFAULT; 90 sg_len -= niov_size; 91 dma += niov_size; 92 niov_idx++; 93 } 94 } 95 96 if (WARN_ON_ONCE(niov_idx != area->nia.num_niovs)) 97 return -EFAULT; 98 return 0; 99 } 100 101 static void io_release_dmabuf(struct io_zcrx_mem *mem) 102 { 103 if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) 104 return; 105 106 if (mem->sgt) 107 dma_buf_unmap_attachment_unlocked(mem->attach, mem->sgt, 108 DMA_FROM_DEVICE); 109 if (mem->attach) 110 dma_buf_detach(mem->dmabuf, mem->attach); 111 if (mem->dmabuf) 112 dma_buf_put(mem->dmabuf); 113 114 mem->sgt = NULL; 115 mem->attach = NULL; 116 mem->dmabuf = NULL; 117 } 118 119 static int io_import_dmabuf(struct io_zcrx_ifq *ifq, 120 struct io_zcrx_mem *mem, 121 struct io_uring_zcrx_area_reg *area_reg) 122 { 123 unsigned long off = (unsigned long)area_reg->addr; 124 unsigned long len = (unsigned long)area_reg->len; 125 unsigned long total_size = 0; 126 struct scatterlist *sg; 127 int dmabuf_fd = area_reg->dmabuf_fd; 128 int i, ret; 129 130 if (off) 131 return -EINVAL; 132 if (WARN_ON_ONCE(!ifq->dev)) 133 return -EFAULT; 134 if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) 135 return -EINVAL; 136 137 mem->is_dmabuf = true; 138 mem->dmabuf = dma_buf_get(dmabuf_fd); 139 if (IS_ERR(mem->dmabuf)) { 140 ret = PTR_ERR(mem->dmabuf); 141 mem->dmabuf = NULL; 142 goto err; 143 } 144 145 mem->attach = dma_buf_attach(mem->dmabuf, ifq->dev); 146 if (IS_ERR(mem->attach)) { 147 ret = PTR_ERR(mem->attach); 148 mem->attach = NULL; 149 goto err; 150 } 151 152 mem->sgt = dma_buf_map_attachment_unlocked(mem->attach, DMA_FROM_DEVICE); 153 if (IS_ERR(mem->sgt)) { 154 ret = PTR_ERR(mem->sgt); 155 mem->sgt = NULL; 156 goto err; 157 } 158 159 for_each_sgtable_dma_sg(mem->sgt, sg, i) 160 total_size += sg_dma_len(sg); 161 162 if (total_size != len) { 163 ret = -EINVAL; 164 goto err; 165 } 166 167 mem->size = len; 168 return 0; 169 err: 170 io_release_dmabuf(mem); 171 return ret; 172 } 173 174 static unsigned long io_count_account_pages(struct page **pages, unsigned nr_pages) 175 { 176 struct folio *last_folio = NULL; 177 unsigned long res = 0; 178 int i; 179 180 for (i = 0; i < nr_pages; i++) { 181 struct folio *folio = page_folio(pages[i]); 182 183 if (folio == last_folio) 184 continue; 185 last_folio = folio; 186 res += folio_nr_pages(folio); 187 } 188 return res; 189 } 190 191 static int io_import_umem(struct io_zcrx_ifq *ifq, 192 struct io_zcrx_mem *mem, 193 struct io_uring_zcrx_area_reg *area_reg) 194 { 195 struct page **pages; 196 int nr_pages, ret; 197 198 if (area_reg->dmabuf_fd) 199 return -EINVAL; 200 if (!area_reg->addr) 201 return -EFAULT; 202 pages = io_pin_pages((unsigned long)area_reg->addr, area_reg->len, 203 &nr_pages); 204 if (IS_ERR(pages)) 205 return PTR_ERR(pages); 206 207 ret = sg_alloc_table_from_pages(&mem->page_sg_table, pages, nr_pages, 208 0, (unsigned long)nr_pages << PAGE_SHIFT, 209 GFP_KERNEL_ACCOUNT); 210 if (ret) { 211 unpin_user_pages(pages, nr_pages); 212 kvfree(pages); 213 return ret; 214 } 215 216 mem->account_pages = io_count_account_pages(pages, nr_pages); 217 ret = io_account_mem(ifq->user, ifq->mm_account, mem->account_pages); 218 if (ret < 0) 219 mem->account_pages = 0; 220 221 mem->sgt = &mem->page_sg_table; 222 mem->pages = pages; 223 mem->nr_folios = nr_pages; 224 mem->size = area_reg->len; 225 return ret; 226 } 227 228 static void io_release_area_mem(struct io_zcrx_mem *mem) 229 { 230 if (mem->is_dmabuf) { 231 io_release_dmabuf(mem); 232 return; 233 } 234 if (mem->pages) { 235 unpin_user_pages(mem->pages, mem->nr_folios); 236 sg_free_table(mem->sgt); 237 mem->sgt = NULL; 238 kvfree(mem->pages); 239 } 240 } 241 242 static int io_import_area(struct io_zcrx_ifq *ifq, 243 struct io_zcrx_mem *mem, 244 struct io_uring_zcrx_area_reg *area_reg) 245 { 246 int ret; 247 248 if (area_reg->flags & ~IO_ZCRX_AREA_SUPPORTED_FLAGS) 249 return -EINVAL; 250 if (area_reg->rq_area_token) 251 return -EINVAL; 252 if (area_reg->__resv2[0] || area_reg->__resv2[1]) 253 return -EINVAL; 254 255 ret = io_validate_user_buf_range(area_reg->addr, area_reg->len); 256 if (ret) 257 return ret; 258 if (area_reg->addr & ~PAGE_MASK || area_reg->len & ~PAGE_MASK) 259 return -EINVAL; 260 261 if (area_reg->flags & IORING_ZCRX_AREA_DMABUF) 262 return io_import_dmabuf(ifq, mem, area_reg); 263 return io_import_umem(ifq, mem, area_reg); 264 } 265 266 static void io_zcrx_unmap_area(struct io_zcrx_ifq *ifq, 267 struct io_zcrx_area *area) 268 { 269 int i; 270 271 guard(mutex)(&ifq->pp_lock); 272 if (!area->is_mapped) 273 return; 274 area->is_mapped = false; 275 276 for (i = 0; i < area->nia.num_niovs; i++) 277 net_mp_niov_set_dma_addr(&area->nia.niovs[i], 0); 278 279 if (area->mem.is_dmabuf) { 280 io_release_dmabuf(&area->mem); 281 } else { 282 dma_unmap_sgtable(ifq->dev, &area->mem.page_sg_table, 283 DMA_FROM_DEVICE, IO_DMA_ATTR); 284 } 285 } 286 287 static int io_zcrx_map_area(struct io_zcrx_ifq *ifq, struct io_zcrx_area *area) 288 { 289 int ret; 290 291 guard(mutex)(&ifq->pp_lock); 292 if (area->is_mapped) 293 return 0; 294 295 if (!area->mem.is_dmabuf) { 296 ret = dma_map_sgtable(ifq->dev, &area->mem.page_sg_table, 297 DMA_FROM_DEVICE, IO_DMA_ATTR); 298 if (ret < 0) 299 return ret; 300 } 301 302 ret = io_populate_area_dma(ifq, area); 303 if (ret && !area->mem.is_dmabuf) 304 dma_unmap_sgtable(ifq->dev, &area->mem.page_sg_table, 305 DMA_FROM_DEVICE, IO_DMA_ATTR); 306 if (ret == 0) 307 area->is_mapped = true; 308 return ret; 309 } 310 311 static void io_zcrx_sync_for_device(struct page_pool *pool, 312 struct net_iov *niov) 313 { 314 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) 315 dma_addr_t dma_addr; 316 317 unsigned niov_size; 318 319 if (!dma_dev_need_sync(pool->p.dev)) 320 return; 321 322 niov_size = 1U << io_pp_to_ifq(pool)->niov_shift; 323 dma_addr = page_pool_get_dma_addr_netmem(net_iov_to_netmem(niov)); 324 __dma_sync_single_for_device(pool->p.dev, dma_addr + pool->p.offset, 325 niov_size, pool->p.dma_dir); 326 #endif 327 } 328 329 #define IO_RQ_MAX_ENTRIES 32768 330 331 #define IO_SKBS_PER_CALL_LIMIT 20 332 333 struct io_zcrx_args { 334 struct io_kiocb *req; 335 struct io_zcrx_ifq *ifq; 336 struct socket *sock; 337 unsigned nr_skbs; 338 }; 339 340 static const struct memory_provider_ops io_uring_pp_zc_ops; 341 342 static inline atomic_t *io_get_user_counter(struct net_iov *niov) 343 { 344 struct io_zcrx_area *area = io_zcrx_iov_to_area(niov); 345 346 return &area->user_refs[net_iov_idx(niov)]; 347 } 348 349 static bool io_zcrx_put_niov_uref(struct net_iov *niov) 350 { 351 atomic_t *uref = io_get_user_counter(niov); 352 353 if (unlikely(!atomic_read(uref))) 354 return false; 355 atomic_dec(uref); 356 return true; 357 } 358 359 static void io_zcrx_get_niov_uref(struct net_iov *niov) 360 { 361 atomic_inc(io_get_user_counter(niov)); 362 } 363 364 static void io_fill_zcrx_offsets(struct io_uring_zcrx_offsets *offsets) 365 { 366 offsets->head = offsetof(struct io_uring, head); 367 offsets->tail = offsetof(struct io_uring, tail); 368 offsets->rqes = ALIGN(sizeof(struct io_uring), L1_CACHE_BYTES); 369 } 370 371 static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx, 372 struct io_zcrx_ifq *ifq, 373 struct io_uring_zcrx_ifq_reg *reg, 374 struct io_uring_region_desc *rd, 375 u32 id) 376 { 377 u64 mmap_offset; 378 size_t off, size; 379 void *ptr; 380 int ret; 381 382 io_fill_zcrx_offsets(®->offsets); 383 off = reg->offsets.rqes; 384 size = off + sizeof(struct io_uring_zcrx_rqe) * reg->rq_entries; 385 if (size > rd->size) 386 return -EINVAL; 387 388 mmap_offset = IORING_MAP_OFF_ZCRX_REGION; 389 mmap_offset += id << IORING_OFF_PBUF_SHIFT; 390 391 ret = io_create_region(ctx, &ifq->region, rd, mmap_offset); 392 if (ret < 0) 393 return ret; 394 395 ptr = io_region_get_ptr(&ifq->region); 396 ifq->rq_ring = (struct io_uring *)ptr; 397 ifq->rqes = (struct io_uring_zcrx_rqe *)(ptr + off); 398 399 return 0; 400 } 401 402 static void io_free_rbuf_ring(struct io_zcrx_ifq *ifq) 403 { 404 io_free_region(ifq->user, &ifq->region); 405 ifq->rq_ring = NULL; 406 ifq->rqes = NULL; 407 } 408 409 static void io_zcrx_free_area(struct io_zcrx_ifq *ifq, 410 struct io_zcrx_area *area) 411 { 412 io_zcrx_unmap_area(ifq, area); 413 io_release_area_mem(&area->mem); 414 415 if (area->mem.account_pages) 416 io_unaccount_mem(ifq->user, ifq->mm_account, 417 area->mem.account_pages); 418 419 kvfree(area->freelist); 420 kvfree(area->nia.niovs); 421 kvfree(area->user_refs); 422 kfree(area); 423 } 424 425 static int io_zcrx_append_area(struct io_zcrx_ifq *ifq, 426 struct io_zcrx_area *area) 427 { 428 if (ifq->area) 429 return -EINVAL; 430 ifq->area = area; 431 return 0; 432 } 433 434 static int io_zcrx_create_area(struct io_zcrx_ifq *ifq, 435 struct io_uring_zcrx_area_reg *area_reg, 436 struct io_uring_zcrx_ifq_reg *reg) 437 { 438 int buf_size_shift = PAGE_SHIFT; 439 struct io_zcrx_area *area; 440 unsigned nr_iovs; 441 int i, ret; 442 443 if (reg->rx_buf_len) { 444 if (!is_power_of_2(reg->rx_buf_len) || 445 reg->rx_buf_len < PAGE_SIZE) 446 return -EINVAL; 447 buf_size_shift = ilog2(reg->rx_buf_len); 448 } 449 450 ret = -ENOMEM; 451 area = kzalloc(sizeof(*area), GFP_KERNEL); 452 if (!area) 453 goto err; 454 area->ifq = ifq; 455 456 ret = io_import_area(ifq, &area->mem, area_reg); 457 if (ret) 458 goto err; 459 460 if (buf_size_shift > io_area_max_shift(&area->mem)) { 461 ret = -ERANGE; 462 goto err; 463 } 464 465 ifq->niov_shift = buf_size_shift; 466 nr_iovs = area->mem.size >> ifq->niov_shift; 467 area->nia.num_niovs = nr_iovs; 468 469 ret = -ENOMEM; 470 area->nia.niovs = kvmalloc_array(nr_iovs, sizeof(area->nia.niovs[0]), 471 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 472 if (!area->nia.niovs) 473 goto err; 474 475 area->freelist = kvmalloc_array(nr_iovs, sizeof(area->freelist[0]), 476 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 477 if (!area->freelist) 478 goto err; 479 480 area->user_refs = kvmalloc_array(nr_iovs, sizeof(area->user_refs[0]), 481 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 482 if (!area->user_refs) 483 goto err; 484 485 for (i = 0; i < nr_iovs; i++) { 486 struct net_iov *niov = &area->nia.niovs[i]; 487 488 niov->owner = &area->nia; 489 area->freelist[i] = i; 490 atomic_set(&area->user_refs[i], 0); 491 niov->type = NET_IOV_IOURING; 492 } 493 494 area->free_count = nr_iovs; 495 /* we're only supporting one area per ifq for now */ 496 area->area_id = 0; 497 area_reg->rq_area_token = (u64)area->area_id << IORING_ZCRX_AREA_SHIFT; 498 spin_lock_init(&area->freelist_lock); 499 500 ret = io_zcrx_append_area(ifq, area); 501 if (!ret) 502 return 0; 503 err: 504 if (area) 505 io_zcrx_free_area(ifq, area); 506 return ret; 507 } 508 509 static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx) 510 { 511 struct io_zcrx_ifq *ifq; 512 513 ifq = kzalloc(sizeof(*ifq), GFP_KERNEL); 514 if (!ifq) 515 return NULL; 516 517 ifq->if_rxq = -1; 518 spin_lock_init(&ifq->rq_lock); 519 mutex_init(&ifq->pp_lock); 520 refcount_set(&ifq->refs, 1); 521 refcount_set(&ifq->user_refs, 1); 522 return ifq; 523 } 524 525 static void io_zcrx_drop_netdev(struct io_zcrx_ifq *ifq) 526 { 527 guard(mutex)(&ifq->pp_lock); 528 529 if (!ifq->netdev) 530 return; 531 netdev_put(ifq->netdev, &ifq->netdev_tracker); 532 ifq->netdev = NULL; 533 } 534 535 static void io_close_queue(struct io_zcrx_ifq *ifq) 536 { 537 struct net_device *netdev; 538 netdevice_tracker netdev_tracker; 539 struct pp_memory_provider_params p = { 540 .mp_ops = &io_uring_pp_zc_ops, 541 .mp_priv = ifq, 542 }; 543 544 scoped_guard(mutex, &ifq->pp_lock) { 545 netdev = ifq->netdev; 546 netdev_tracker = ifq->netdev_tracker; 547 ifq->netdev = NULL; 548 } 549 550 if (netdev) { 551 if (ifq->if_rxq != -1) 552 net_mp_close_rxq(netdev, ifq->if_rxq, &p); 553 netdev_put(netdev, &netdev_tracker); 554 } 555 ifq->if_rxq = -1; 556 } 557 558 static void io_zcrx_ifq_free(struct io_zcrx_ifq *ifq) 559 { 560 io_close_queue(ifq); 561 562 if (ifq->area) 563 io_zcrx_free_area(ifq, ifq->area); 564 free_uid(ifq->user); 565 if (ifq->mm_account) 566 mmdrop(ifq->mm_account); 567 if (ifq->dev) 568 put_device(ifq->dev); 569 570 io_free_rbuf_ring(ifq); 571 mutex_destroy(&ifq->pp_lock); 572 kfree(ifq); 573 } 574 575 static void io_put_zcrx_ifq(struct io_zcrx_ifq *ifq) 576 { 577 if (refcount_dec_and_test(&ifq->refs)) 578 io_zcrx_ifq_free(ifq); 579 } 580 581 static void io_zcrx_return_niov_freelist(struct net_iov *niov) 582 { 583 struct io_zcrx_area *area = io_zcrx_iov_to_area(niov); 584 585 spin_lock_bh(&area->freelist_lock); 586 area->freelist[area->free_count++] = net_iov_idx(niov); 587 spin_unlock_bh(&area->freelist_lock); 588 } 589 590 static void io_zcrx_return_niov(struct net_iov *niov) 591 { 592 netmem_ref netmem = net_iov_to_netmem(niov); 593 594 if (!niov->desc.pp) { 595 /* copy fallback allocated niovs */ 596 io_zcrx_return_niov_freelist(niov); 597 return; 598 } 599 page_pool_put_unrefed_netmem(niov->desc.pp, netmem, -1, false); 600 } 601 602 static void io_zcrx_scrub(struct io_zcrx_ifq *ifq) 603 { 604 struct io_zcrx_area *area = ifq->area; 605 int i; 606 607 if (!area) 608 return; 609 610 /* Reclaim back all buffers given to the user space. */ 611 for (i = 0; i < area->nia.num_niovs; i++) { 612 struct net_iov *niov = &area->nia.niovs[i]; 613 int nr; 614 615 if (!atomic_read(io_get_user_counter(niov))) 616 continue; 617 nr = atomic_xchg(io_get_user_counter(niov), 0); 618 if (nr && !page_pool_unref_netmem(net_iov_to_netmem(niov), nr)) 619 io_zcrx_return_niov(niov); 620 } 621 } 622 623 static void zcrx_unregister(struct io_zcrx_ifq *ifq) 624 { 625 if (refcount_dec_and_test(&ifq->user_refs)) { 626 io_close_queue(ifq); 627 io_zcrx_scrub(ifq); 628 } 629 io_put_zcrx_ifq(ifq); 630 } 631 632 struct io_mapped_region *io_zcrx_get_region(struct io_ring_ctx *ctx, 633 unsigned int id) 634 { 635 struct io_zcrx_ifq *ifq = xa_load(&ctx->zcrx_ctxs, id); 636 637 lockdep_assert_held(&ctx->mmap_lock); 638 639 return ifq ? &ifq->region : NULL; 640 } 641 642 static int zcrx_box_release(struct inode *inode, struct file *file) 643 { 644 struct io_zcrx_ifq *ifq = file->private_data; 645 646 if (WARN_ON_ONCE(!ifq)) 647 return -EFAULT; 648 zcrx_unregister(ifq); 649 return 0; 650 } 651 652 static const struct file_operations zcrx_box_fops = { 653 .owner = THIS_MODULE, 654 .release = zcrx_box_release, 655 }; 656 657 static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq, 658 struct zcrx_ctrl *ctrl, void __user *arg) 659 { 660 struct zcrx_ctrl_export *ce = &ctrl->zc_export; 661 struct file *file; 662 int fd = -1; 663 664 if (!mem_is_zero(ce, sizeof(*ce))) 665 return -EINVAL; 666 fd = get_unused_fd_flags(O_CLOEXEC); 667 if (fd < 0) 668 return fd; 669 670 ce->zcrx_fd = fd; 671 if (copy_to_user(arg, ctrl, sizeof(*ctrl))) { 672 put_unused_fd(fd); 673 return -EFAULT; 674 } 675 676 refcount_inc(&ifq->refs); 677 refcount_inc(&ifq->user_refs); 678 679 file = anon_inode_create_getfile("[zcrx]", &zcrx_box_fops, 680 ifq, O_CLOEXEC, NULL); 681 if (IS_ERR(file)) { 682 put_unused_fd(fd); 683 zcrx_unregister(ifq); 684 return PTR_ERR(file); 685 } 686 687 fd_install(fd, file); 688 return 0; 689 } 690 691 static int import_zcrx(struct io_ring_ctx *ctx, 692 struct io_uring_zcrx_ifq_reg __user *arg, 693 struct io_uring_zcrx_ifq_reg *reg) 694 { 695 struct io_zcrx_ifq *ifq; 696 struct file *file; 697 int fd, ret; 698 u32 id; 699 700 if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) 701 return -EINVAL; 702 if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))) 703 return -EINVAL; 704 if (reg->if_rxq || reg->rq_entries || reg->area_ptr || reg->region_ptr) 705 return -EINVAL; 706 if (reg->flags & ~ZCRX_REG_IMPORT) 707 return -EINVAL; 708 709 fd = reg->if_idx; 710 CLASS(fd, f)(fd); 711 if (fd_empty(f)) 712 return -EBADF; 713 714 file = fd_file(f); 715 if (file->f_op != &zcrx_box_fops || !file->private_data) 716 return -EBADF; 717 718 ifq = file->private_data; 719 refcount_inc(&ifq->refs); 720 refcount_inc(&ifq->user_refs); 721 722 scoped_guard(mutex, &ctx->mmap_lock) { 723 ret = xa_alloc(&ctx->zcrx_ctxs, &id, NULL, xa_limit_31b, GFP_KERNEL); 724 if (ret) 725 goto err; 726 } 727 728 reg->zcrx_id = id; 729 io_fill_zcrx_offsets(®->offsets); 730 if (copy_to_user(arg, reg, sizeof(*reg))) { 731 ret = -EFAULT; 732 goto err_xa_erase; 733 } 734 735 scoped_guard(mutex, &ctx->mmap_lock) { 736 ret = -ENOMEM; 737 if (xa_store(&ctx->zcrx_ctxs, id, ifq, GFP_KERNEL)) 738 goto err_xa_erase; 739 } 740 741 return 0; 742 err_xa_erase: 743 scoped_guard(mutex, &ctx->mmap_lock) 744 xa_erase(&ctx->zcrx_ctxs, id); 745 err: 746 zcrx_unregister(ifq); 747 return ret; 748 } 749 750 int io_register_zcrx_ifq(struct io_ring_ctx *ctx, 751 struct io_uring_zcrx_ifq_reg __user *arg) 752 { 753 struct pp_memory_provider_params mp_param = {}; 754 struct io_uring_zcrx_area_reg area; 755 struct io_uring_zcrx_ifq_reg reg; 756 struct io_uring_region_desc rd; 757 struct io_zcrx_ifq *ifq; 758 int ret; 759 u32 id; 760 761 /* 762 * 1. Interface queue allocation. 763 * 2. It can observe data destined for sockets of other tasks. 764 */ 765 if (!capable(CAP_NET_ADMIN)) 766 return -EPERM; 767 768 /* mandatory io_uring features for zc rx */ 769 if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) 770 return -EINVAL; 771 if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))) 772 return -EINVAL; 773 if (copy_from_user(®, arg, sizeof(reg))) 774 return -EFAULT; 775 if (!mem_is_zero(®.__resv, sizeof(reg.__resv)) || reg.zcrx_id) 776 return -EINVAL; 777 if (reg.flags & ZCRX_REG_IMPORT) 778 return import_zcrx(ctx, arg, ®); 779 if (copy_from_user(&rd, u64_to_user_ptr(reg.region_ptr), sizeof(rd))) 780 return -EFAULT; 781 if (reg.if_rxq == -1 || !reg.rq_entries || reg.flags) 782 return -EINVAL; 783 if (reg.rq_entries > IO_RQ_MAX_ENTRIES) { 784 if (!(ctx->flags & IORING_SETUP_CLAMP)) 785 return -EINVAL; 786 reg.rq_entries = IO_RQ_MAX_ENTRIES; 787 } 788 reg.rq_entries = roundup_pow_of_two(reg.rq_entries); 789 790 if (copy_from_user(&area, u64_to_user_ptr(reg.area_ptr), sizeof(area))) 791 return -EFAULT; 792 793 ifq = io_zcrx_ifq_alloc(ctx); 794 if (!ifq) 795 return -ENOMEM; 796 797 if (ctx->user) { 798 get_uid(ctx->user); 799 ifq->user = ctx->user; 800 } 801 if (ctx->mm_account) { 802 mmgrab(ctx->mm_account); 803 ifq->mm_account = ctx->mm_account; 804 } 805 ifq->rq_entries = reg.rq_entries; 806 807 scoped_guard(mutex, &ctx->mmap_lock) { 808 /* preallocate id */ 809 ret = xa_alloc(&ctx->zcrx_ctxs, &id, NULL, xa_limit_31b, GFP_KERNEL); 810 if (ret) 811 goto ifq_free; 812 } 813 814 ret = io_allocate_rbuf_ring(ctx, ifq, ®, &rd, id); 815 if (ret) 816 goto err; 817 818 ifq->netdev = netdev_get_by_index_lock(current->nsproxy->net_ns, reg.if_idx); 819 if (!ifq->netdev) { 820 ret = -ENODEV; 821 goto err; 822 } 823 netdev_hold(ifq->netdev, &ifq->netdev_tracker, GFP_KERNEL); 824 825 ifq->dev = netdev_queue_get_dma_dev(ifq->netdev, reg.if_rxq); 826 if (!ifq->dev) { 827 ret = -EOPNOTSUPP; 828 goto netdev_put_unlock; 829 } 830 get_device(ifq->dev); 831 832 ret = io_zcrx_create_area(ifq, &area, ®); 833 if (ret) 834 goto netdev_put_unlock; 835 836 mp_param.rx_page_size = 1U << ifq->niov_shift; 837 mp_param.mp_ops = &io_uring_pp_zc_ops; 838 mp_param.mp_priv = ifq; 839 ret = __net_mp_open_rxq(ifq->netdev, reg.if_rxq, &mp_param, NULL); 840 if (ret) 841 goto netdev_put_unlock; 842 netdev_unlock(ifq->netdev); 843 ifq->if_rxq = reg.if_rxq; 844 845 reg.zcrx_id = id; 846 847 scoped_guard(mutex, &ctx->mmap_lock) { 848 /* publish ifq */ 849 ret = -ENOMEM; 850 if (xa_store(&ctx->zcrx_ctxs, id, ifq, GFP_KERNEL)) 851 goto err; 852 } 853 854 reg.rx_buf_len = 1U << ifq->niov_shift; 855 856 if (copy_to_user(arg, ®, sizeof(reg)) || 857 copy_to_user(u64_to_user_ptr(reg.region_ptr), &rd, sizeof(rd)) || 858 copy_to_user(u64_to_user_ptr(reg.area_ptr), &area, sizeof(area))) { 859 ret = -EFAULT; 860 goto err; 861 } 862 return 0; 863 netdev_put_unlock: 864 netdev_unlock(ifq->netdev); 865 err: 866 scoped_guard(mutex, &ctx->mmap_lock) 867 xa_erase(&ctx->zcrx_ctxs, id); 868 ifq_free: 869 zcrx_unregister(ifq); 870 return ret; 871 } 872 873 static struct net_iov *__io_zcrx_get_free_niov(struct io_zcrx_area *area) 874 { 875 unsigned niov_idx; 876 877 lockdep_assert_held(&area->freelist_lock); 878 879 niov_idx = area->freelist[--area->free_count]; 880 return &area->nia.niovs[niov_idx]; 881 } 882 883 void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx) 884 { 885 struct io_zcrx_ifq *ifq; 886 887 lockdep_assert_held(&ctx->uring_lock); 888 889 while (1) { 890 scoped_guard(mutex, &ctx->mmap_lock) { 891 unsigned long id = 0; 892 893 ifq = xa_find(&ctx->zcrx_ctxs, &id, ULONG_MAX, XA_PRESENT); 894 if (ifq) 895 xa_erase(&ctx->zcrx_ctxs, id); 896 } 897 if (!ifq) 898 break; 899 zcrx_unregister(ifq); 900 } 901 902 xa_destroy(&ctx->zcrx_ctxs); 903 } 904 905 static inline u32 io_zcrx_rqring_entries(struct io_zcrx_ifq *ifq) 906 { 907 u32 entries; 908 909 entries = smp_load_acquire(&ifq->rq_ring->tail) - ifq->cached_rq_head; 910 return min(entries, ifq->rq_entries); 911 } 912 913 static struct io_uring_zcrx_rqe *io_zcrx_get_rqe(struct io_zcrx_ifq *ifq, 914 unsigned mask) 915 { 916 unsigned int idx = ifq->cached_rq_head++ & mask; 917 918 return &ifq->rqes[idx]; 919 } 920 921 static inline bool io_parse_rqe(struct io_uring_zcrx_rqe *rqe, 922 struct io_zcrx_ifq *ifq, 923 struct net_iov **ret_niov) 924 { 925 unsigned niov_idx, area_idx; 926 struct io_zcrx_area *area; 927 928 area_idx = rqe->off >> IORING_ZCRX_AREA_SHIFT; 929 niov_idx = (rqe->off & ~IORING_ZCRX_AREA_MASK) >> ifq->niov_shift; 930 931 if (unlikely(rqe->__pad || area_idx)) 932 return false; 933 area = ifq->area; 934 935 if (unlikely(niov_idx >= area->nia.num_niovs)) 936 return false; 937 niov_idx = array_index_nospec(niov_idx, area->nia.num_niovs); 938 939 *ret_niov = &area->nia.niovs[niov_idx]; 940 return true; 941 } 942 943 static void io_zcrx_ring_refill(struct page_pool *pp, 944 struct io_zcrx_ifq *ifq) 945 { 946 unsigned int mask = ifq->rq_entries - 1; 947 unsigned int entries; 948 949 guard(spinlock_bh)(&ifq->rq_lock); 950 951 entries = io_zcrx_rqring_entries(ifq); 952 entries = min_t(unsigned, entries, PP_ALLOC_CACHE_REFILL); 953 if (unlikely(!entries)) 954 return; 955 956 do { 957 struct io_uring_zcrx_rqe *rqe = io_zcrx_get_rqe(ifq, mask); 958 struct net_iov *niov; 959 netmem_ref netmem; 960 961 if (!io_parse_rqe(rqe, ifq, &niov)) 962 continue; 963 if (!io_zcrx_put_niov_uref(niov)) 964 continue; 965 966 netmem = net_iov_to_netmem(niov); 967 if (!page_pool_unref_and_test(netmem)) 968 continue; 969 970 if (unlikely(niov->desc.pp != pp)) { 971 io_zcrx_return_niov(niov); 972 continue; 973 } 974 975 io_zcrx_sync_for_device(pp, niov); 976 net_mp_netmem_place_in_cache(pp, netmem); 977 } while (--entries); 978 979 smp_store_release(&ifq->rq_ring->head, ifq->cached_rq_head); 980 } 981 982 static void io_zcrx_refill_slow(struct page_pool *pp, struct io_zcrx_ifq *ifq) 983 { 984 struct io_zcrx_area *area = ifq->area; 985 986 spin_lock_bh(&area->freelist_lock); 987 while (area->free_count && pp->alloc.count < PP_ALLOC_CACHE_REFILL) { 988 struct net_iov *niov = __io_zcrx_get_free_niov(area); 989 netmem_ref netmem = net_iov_to_netmem(niov); 990 991 net_mp_niov_set_page_pool(pp, niov); 992 io_zcrx_sync_for_device(pp, niov); 993 net_mp_netmem_place_in_cache(pp, netmem); 994 } 995 spin_unlock_bh(&area->freelist_lock); 996 } 997 998 static netmem_ref io_pp_zc_alloc_netmems(struct page_pool *pp, gfp_t gfp) 999 { 1000 struct io_zcrx_ifq *ifq = io_pp_to_ifq(pp); 1001 1002 /* pp should already be ensuring that */ 1003 if (unlikely(pp->alloc.count)) 1004 goto out_return; 1005 1006 io_zcrx_ring_refill(pp, ifq); 1007 if (likely(pp->alloc.count)) 1008 goto out_return; 1009 1010 io_zcrx_refill_slow(pp, ifq); 1011 if (!pp->alloc.count) 1012 return 0; 1013 out_return: 1014 return pp->alloc.cache[--pp->alloc.count]; 1015 } 1016 1017 static bool io_pp_zc_release_netmem(struct page_pool *pp, netmem_ref netmem) 1018 { 1019 struct net_iov *niov; 1020 1021 if (WARN_ON_ONCE(!netmem_is_net_iov(netmem))) 1022 return false; 1023 1024 niov = netmem_to_net_iov(netmem); 1025 net_mp_niov_clear_page_pool(niov); 1026 io_zcrx_return_niov_freelist(niov); 1027 return false; 1028 } 1029 1030 static int io_pp_zc_init(struct page_pool *pp) 1031 { 1032 struct io_zcrx_ifq *ifq = io_pp_to_ifq(pp); 1033 int ret; 1034 1035 if (WARN_ON_ONCE(!ifq)) 1036 return -EINVAL; 1037 if (WARN_ON_ONCE(ifq->dev != pp->p.dev)) 1038 return -EINVAL; 1039 if (WARN_ON_ONCE(!pp->dma_map)) 1040 return -EOPNOTSUPP; 1041 if (pp->p.order + PAGE_SHIFT != ifq->niov_shift) 1042 return -EINVAL; 1043 if (pp->p.dma_dir != DMA_FROM_DEVICE) 1044 return -EOPNOTSUPP; 1045 1046 ret = io_zcrx_map_area(ifq, ifq->area); 1047 if (ret) 1048 return ret; 1049 1050 refcount_inc(&ifq->refs); 1051 return 0; 1052 } 1053 1054 static void io_pp_zc_destroy(struct page_pool *pp) 1055 { 1056 io_put_zcrx_ifq(io_pp_to_ifq(pp)); 1057 } 1058 1059 static int io_pp_nl_fill(void *mp_priv, struct sk_buff *rsp, 1060 struct netdev_rx_queue *rxq) 1061 { 1062 struct nlattr *nest; 1063 int type; 1064 1065 type = rxq ? NETDEV_A_QUEUE_IO_URING : NETDEV_A_PAGE_POOL_IO_URING; 1066 nest = nla_nest_start(rsp, type); 1067 if (!nest) 1068 return -EMSGSIZE; 1069 nla_nest_end(rsp, nest); 1070 1071 return 0; 1072 } 1073 1074 static void io_pp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq) 1075 { 1076 struct pp_memory_provider_params *p = &rxq->mp_params; 1077 struct io_zcrx_ifq *ifq = mp_priv; 1078 1079 io_zcrx_drop_netdev(ifq); 1080 if (ifq->area) 1081 io_zcrx_unmap_area(ifq, ifq->area); 1082 1083 p->mp_ops = NULL; 1084 p->mp_priv = NULL; 1085 } 1086 1087 static const struct memory_provider_ops io_uring_pp_zc_ops = { 1088 .alloc_netmems = io_pp_zc_alloc_netmems, 1089 .release_netmem = io_pp_zc_release_netmem, 1090 .init = io_pp_zc_init, 1091 .destroy = io_pp_zc_destroy, 1092 .nl_fill = io_pp_nl_fill, 1093 .uninstall = io_pp_uninstall, 1094 }; 1095 1096 static unsigned zcrx_parse_rq(netmem_ref *netmem_array, unsigned nr, 1097 struct io_zcrx_ifq *zcrx) 1098 { 1099 unsigned int mask = zcrx->rq_entries - 1; 1100 unsigned int i; 1101 1102 nr = min(nr, io_zcrx_rqring_entries(zcrx)); 1103 for (i = 0; i < nr; i++) { 1104 struct io_uring_zcrx_rqe *rqe = io_zcrx_get_rqe(zcrx, mask); 1105 struct net_iov *niov; 1106 1107 if (!io_parse_rqe(rqe, zcrx, &niov)) 1108 break; 1109 netmem_array[i] = net_iov_to_netmem(niov); 1110 } 1111 1112 smp_store_release(&zcrx->rq_ring->head, zcrx->cached_rq_head); 1113 return i; 1114 } 1115 1116 #define ZCRX_FLUSH_BATCH 32 1117 1118 static void zcrx_return_buffers(netmem_ref *netmems, unsigned nr) 1119 { 1120 unsigned i; 1121 1122 for (i = 0; i < nr; i++) { 1123 netmem_ref netmem = netmems[i]; 1124 struct net_iov *niov = netmem_to_net_iov(netmem); 1125 1126 if (!io_zcrx_put_niov_uref(niov)) 1127 continue; 1128 if (!page_pool_unref_and_test(netmem)) 1129 continue; 1130 io_zcrx_return_niov(niov); 1131 } 1132 } 1133 1134 static int zcrx_flush_rq(struct io_ring_ctx *ctx, struct io_zcrx_ifq *zcrx, 1135 struct zcrx_ctrl *ctrl) 1136 { 1137 struct zcrx_ctrl_flush_rq *frq = &ctrl->zc_flush; 1138 netmem_ref netmems[ZCRX_FLUSH_BATCH]; 1139 unsigned total = 0; 1140 unsigned nr; 1141 1142 if (!mem_is_zero(&frq->__resv, sizeof(frq->__resv))) 1143 return -EINVAL; 1144 1145 do { 1146 scoped_guard(spinlock_bh, &zcrx->rq_lock) { 1147 nr = zcrx_parse_rq(netmems, ZCRX_FLUSH_BATCH, zcrx); 1148 zcrx_return_buffers(netmems, nr); 1149 } 1150 1151 total += nr; 1152 1153 if (fatal_signal_pending(current)) 1154 break; 1155 cond_resched(); 1156 } while (nr == ZCRX_FLUSH_BATCH && total < zcrx->rq_entries); 1157 1158 return 0; 1159 } 1160 1161 int io_zcrx_ctrl(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) 1162 { 1163 struct zcrx_ctrl ctrl; 1164 struct io_zcrx_ifq *zcrx; 1165 1166 if (nr_args) 1167 return -EINVAL; 1168 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1169 return -EFAULT; 1170 if (!mem_is_zero(&ctrl.__resv, sizeof(ctrl.__resv))) 1171 return -EFAULT; 1172 1173 zcrx = xa_load(&ctx->zcrx_ctxs, ctrl.zcrx_id); 1174 if (!zcrx) 1175 return -ENXIO; 1176 1177 switch (ctrl.op) { 1178 case ZCRX_CTRL_FLUSH_RQ: 1179 return zcrx_flush_rq(ctx, zcrx, &ctrl); 1180 case ZCRX_CTRL_EXPORT: 1181 return zcrx_export(ctx, zcrx, &ctrl, arg); 1182 } 1183 1184 return -EOPNOTSUPP; 1185 } 1186 1187 static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov, 1188 struct io_zcrx_ifq *ifq, int off, int len) 1189 { 1190 struct io_ring_ctx *ctx = req->ctx; 1191 struct io_uring_zcrx_cqe *rcqe; 1192 struct io_zcrx_area *area; 1193 struct io_uring_cqe *cqe; 1194 u64 offset; 1195 1196 if (!io_defer_get_uncommited_cqe(ctx, &cqe)) 1197 return false; 1198 1199 cqe->user_data = req->cqe.user_data; 1200 cqe->res = len; 1201 cqe->flags = IORING_CQE_F_MORE; 1202 if (ctx->flags & IORING_SETUP_CQE_MIXED) 1203 cqe->flags |= IORING_CQE_F_32; 1204 1205 area = io_zcrx_iov_to_area(niov); 1206 offset = off + (net_iov_idx(niov) << ifq->niov_shift); 1207 rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1); 1208 rcqe->off = offset + ((u64)area->area_id << IORING_ZCRX_AREA_SHIFT); 1209 rcqe->__pad = 0; 1210 return true; 1211 } 1212 1213 static struct net_iov *io_alloc_fallback_niov(struct io_zcrx_ifq *ifq) 1214 { 1215 struct io_zcrx_area *area = ifq->area; 1216 struct net_iov *niov = NULL; 1217 1218 if (area->mem.is_dmabuf) 1219 return NULL; 1220 1221 spin_lock_bh(&area->freelist_lock); 1222 if (area->free_count) 1223 niov = __io_zcrx_get_free_niov(area); 1224 spin_unlock_bh(&area->freelist_lock); 1225 1226 if (niov) 1227 page_pool_fragment_netmem(net_iov_to_netmem(niov), 1); 1228 return niov; 1229 } 1230 1231 struct io_copy_cache { 1232 struct page *page; 1233 unsigned long offset; 1234 size_t size; 1235 }; 1236 1237 static ssize_t io_copy_page(struct io_copy_cache *cc, struct page *src_page, 1238 unsigned int src_offset, size_t len) 1239 { 1240 size_t copied = 0; 1241 1242 len = min(len, cc->size); 1243 1244 while (len) { 1245 void *src_addr, *dst_addr; 1246 struct page *dst_page = cc->page; 1247 unsigned dst_offset = cc->offset; 1248 size_t n = len; 1249 1250 if (folio_test_partial_kmap(page_folio(dst_page)) || 1251 folio_test_partial_kmap(page_folio(src_page))) { 1252 dst_page += dst_offset / PAGE_SIZE; 1253 dst_offset = offset_in_page(dst_offset); 1254 src_page += src_offset / PAGE_SIZE; 1255 src_offset = offset_in_page(src_offset); 1256 n = min(PAGE_SIZE - src_offset, PAGE_SIZE - dst_offset); 1257 n = min(n, len); 1258 } 1259 1260 dst_addr = kmap_local_page(dst_page) + dst_offset; 1261 src_addr = kmap_local_page(src_page) + src_offset; 1262 1263 memcpy(dst_addr, src_addr, n); 1264 1265 kunmap_local(src_addr); 1266 kunmap_local(dst_addr); 1267 1268 cc->size -= n; 1269 cc->offset += n; 1270 src_offset += n; 1271 len -= n; 1272 copied += n; 1273 } 1274 return copied; 1275 } 1276 1277 static ssize_t io_zcrx_copy_chunk(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1278 struct page *src_page, unsigned int src_offset, 1279 size_t len) 1280 { 1281 size_t copied = 0; 1282 int ret = 0; 1283 1284 while (len) { 1285 struct io_copy_cache cc; 1286 struct net_iov *niov; 1287 size_t n; 1288 1289 niov = io_alloc_fallback_niov(ifq); 1290 if (!niov) { 1291 ret = -ENOMEM; 1292 break; 1293 } 1294 1295 cc.page = io_zcrx_iov_page(niov); 1296 cc.offset = 0; 1297 cc.size = PAGE_SIZE; 1298 1299 n = io_copy_page(&cc, src_page, src_offset, len); 1300 1301 if (!io_zcrx_queue_cqe(req, niov, ifq, 0, n)) { 1302 io_zcrx_return_niov(niov); 1303 ret = -ENOSPC; 1304 break; 1305 } 1306 1307 io_zcrx_get_niov_uref(niov); 1308 src_offset += n; 1309 len -= n; 1310 copied += n; 1311 } 1312 1313 return copied ? copied : ret; 1314 } 1315 1316 static int io_zcrx_copy_frag(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1317 const skb_frag_t *frag, int off, int len) 1318 { 1319 struct page *page = skb_frag_page(frag); 1320 1321 return io_zcrx_copy_chunk(req, ifq, page, off + skb_frag_off(frag), len); 1322 } 1323 1324 static int io_zcrx_recv_frag(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1325 const skb_frag_t *frag, int off, int len) 1326 { 1327 struct net_iov *niov; 1328 struct page_pool *pp; 1329 1330 if (unlikely(!skb_frag_is_net_iov(frag))) 1331 return io_zcrx_copy_frag(req, ifq, frag, off, len); 1332 1333 niov = netmem_to_net_iov(frag->netmem); 1334 pp = niov->desc.pp; 1335 1336 if (!pp || pp->mp_ops != &io_uring_pp_zc_ops || io_pp_to_ifq(pp) != ifq) 1337 return -EFAULT; 1338 1339 if (!io_zcrx_queue_cqe(req, niov, ifq, off + skb_frag_off(frag), len)) 1340 return -ENOSPC; 1341 1342 /* 1343 * Prevent it from being recycled while user is accessing it. 1344 * It has to be done before grabbing a user reference. 1345 */ 1346 page_pool_ref_netmem(net_iov_to_netmem(niov)); 1347 io_zcrx_get_niov_uref(niov); 1348 return len; 1349 } 1350 1351 static int 1352 io_zcrx_recv_skb(read_descriptor_t *desc, struct sk_buff *skb, 1353 unsigned int offset, size_t len) 1354 { 1355 struct io_zcrx_args *args = desc->arg.data; 1356 struct io_zcrx_ifq *ifq = args->ifq; 1357 struct io_kiocb *req = args->req; 1358 struct sk_buff *frag_iter; 1359 unsigned start, start_off = offset; 1360 int i, copy, end, off; 1361 int ret = 0; 1362 1363 len = min_t(size_t, len, desc->count); 1364 /* 1365 * __tcp_read_sock() always calls io_zcrx_recv_skb one last time, even 1366 * if desc->count is already 0. This is caused by the if (offset + 1 != 1367 * skb->len) check. Return early in this case to break out of 1368 * __tcp_read_sock(). 1369 */ 1370 if (!len) 1371 return 0; 1372 if (unlikely(args->nr_skbs++ > IO_SKBS_PER_CALL_LIMIT)) 1373 return -EAGAIN; 1374 1375 if (unlikely(offset < skb_headlen(skb))) { 1376 ssize_t copied; 1377 size_t to_copy; 1378 1379 to_copy = min_t(size_t, skb_headlen(skb) - offset, len); 1380 copied = io_zcrx_copy_chunk(req, ifq, virt_to_page(skb->data), 1381 offset_in_page(skb->data) + offset, 1382 to_copy); 1383 if (copied < 0) { 1384 ret = copied; 1385 goto out; 1386 } 1387 offset += copied; 1388 len -= copied; 1389 if (!len) 1390 goto out; 1391 if (offset != skb_headlen(skb)) 1392 goto out; 1393 } 1394 1395 start = skb_headlen(skb); 1396 1397 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1398 const skb_frag_t *frag; 1399 1400 if (WARN_ON(start > offset + len)) 1401 return -EFAULT; 1402 1403 frag = &skb_shinfo(skb)->frags[i]; 1404 end = start + skb_frag_size(frag); 1405 1406 if (offset < end) { 1407 copy = end - offset; 1408 if (copy > len) 1409 copy = len; 1410 1411 off = offset - start; 1412 ret = io_zcrx_recv_frag(req, ifq, frag, off, copy); 1413 if (ret < 0) 1414 goto out; 1415 1416 offset += ret; 1417 len -= ret; 1418 if (len == 0 || ret != copy) 1419 goto out; 1420 } 1421 start = end; 1422 } 1423 1424 skb_walk_frags(skb, frag_iter) { 1425 if (WARN_ON(start > offset + len)) 1426 return -EFAULT; 1427 1428 end = start + frag_iter->len; 1429 if (offset < end) { 1430 size_t count; 1431 1432 copy = end - offset; 1433 if (copy > len) 1434 copy = len; 1435 1436 off = offset - start; 1437 count = desc->count; 1438 ret = io_zcrx_recv_skb(desc, frag_iter, off, copy); 1439 desc->count = count; 1440 if (ret < 0) 1441 goto out; 1442 1443 offset += ret; 1444 len -= ret; 1445 if (len == 0 || ret != copy) 1446 goto out; 1447 } 1448 start = end; 1449 } 1450 1451 out: 1452 if (offset == start_off) 1453 return ret; 1454 desc->count -= (offset - start_off); 1455 return offset - start_off; 1456 } 1457 1458 static int io_zcrx_tcp_recvmsg(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1459 struct sock *sk, int flags, 1460 unsigned issue_flags, unsigned int *outlen) 1461 { 1462 unsigned int len = *outlen; 1463 struct io_zcrx_args args = { 1464 .req = req, 1465 .ifq = ifq, 1466 .sock = sk->sk_socket, 1467 }; 1468 read_descriptor_t rd_desc = { 1469 .count = len ? len : UINT_MAX, 1470 .arg.data = &args, 1471 }; 1472 int ret; 1473 1474 lock_sock(sk); 1475 ret = tcp_read_sock(sk, &rd_desc, io_zcrx_recv_skb); 1476 if (len && ret > 0) 1477 *outlen = len - ret; 1478 if (ret <= 0) { 1479 if (ret < 0 || sock_flag(sk, SOCK_DONE)) 1480 goto out; 1481 if (sk->sk_err) 1482 ret = sock_error(sk); 1483 else if (sk->sk_shutdown & RCV_SHUTDOWN) 1484 goto out; 1485 else if (sk->sk_state == TCP_CLOSE) 1486 ret = -ENOTCONN; 1487 else 1488 ret = -EAGAIN; 1489 } else if (unlikely(args.nr_skbs > IO_SKBS_PER_CALL_LIMIT) && 1490 (issue_flags & IO_URING_F_MULTISHOT)) { 1491 ret = IOU_REQUEUE; 1492 } else if (sock_flag(sk, SOCK_DONE)) { 1493 /* Make it to retry until it finally gets 0. */ 1494 if (issue_flags & IO_URING_F_MULTISHOT) 1495 ret = IOU_REQUEUE; 1496 else 1497 ret = -EAGAIN; 1498 } 1499 out: 1500 release_sock(sk); 1501 return ret; 1502 } 1503 1504 int io_zcrx_recv(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1505 struct socket *sock, unsigned int flags, 1506 unsigned issue_flags, unsigned int *len) 1507 { 1508 struct sock *sk = sock->sk; 1509 const struct proto *prot = READ_ONCE(sk->sk_prot); 1510 1511 if (prot->recvmsg != tcp_recvmsg) 1512 return -EPROTONOSUPPORT; 1513 1514 sock_rps_record_flow(sk); 1515 return io_zcrx_tcp_recvmsg(req, ifq, sk, flags, issue_flags, len); 1516 } 1517