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 int old; 353 354 old = atomic_read(uref); 355 do { 356 if (unlikely(old == 0)) 357 return false; 358 } while (!atomic_try_cmpxchg(uref, &old, old - 1)); 359 360 return true; 361 } 362 363 static void io_zcrx_get_niov_uref(struct net_iov *niov) 364 { 365 atomic_inc(io_get_user_counter(niov)); 366 } 367 368 static void io_fill_zcrx_offsets(struct io_uring_zcrx_offsets *offsets) 369 { 370 offsets->head = offsetof(struct io_uring, head); 371 offsets->tail = offsetof(struct io_uring, tail); 372 offsets->rqes = ALIGN(sizeof(struct io_uring), L1_CACHE_BYTES); 373 } 374 375 static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx, 376 struct io_zcrx_ifq *ifq, 377 struct io_uring_zcrx_ifq_reg *reg, 378 struct io_uring_region_desc *rd, 379 u32 id) 380 { 381 u64 mmap_offset; 382 size_t off, size; 383 void *ptr; 384 int ret; 385 386 io_fill_zcrx_offsets(®->offsets); 387 off = reg->offsets.rqes; 388 size = off + sizeof(struct io_uring_zcrx_rqe) * reg->rq_entries; 389 if (size > rd->size) 390 return -EINVAL; 391 392 mmap_offset = IORING_MAP_OFF_ZCRX_REGION; 393 mmap_offset += id << IORING_OFF_PBUF_SHIFT; 394 395 ret = io_create_region(ctx, &ifq->region, rd, mmap_offset); 396 if (ret < 0) 397 return ret; 398 399 ptr = io_region_get_ptr(&ifq->region); 400 ifq->rq_ring = (struct io_uring *)ptr; 401 ifq->rqes = (struct io_uring_zcrx_rqe *)(ptr + off); 402 403 return 0; 404 } 405 406 static void io_free_rbuf_ring(struct io_zcrx_ifq *ifq) 407 { 408 io_free_region(ifq->user, &ifq->region); 409 ifq->rq_ring = NULL; 410 ifq->rqes = NULL; 411 } 412 413 static void io_zcrx_free_area(struct io_zcrx_ifq *ifq, 414 struct io_zcrx_area *area) 415 { 416 io_zcrx_unmap_area(ifq, area); 417 io_release_area_mem(&area->mem); 418 419 if (area->mem.account_pages) 420 io_unaccount_mem(ifq->user, ifq->mm_account, 421 area->mem.account_pages); 422 423 kvfree(area->freelist); 424 kvfree(area->nia.niovs); 425 kvfree(area->user_refs); 426 kfree(area); 427 } 428 429 static int io_zcrx_append_area(struct io_zcrx_ifq *ifq, 430 struct io_zcrx_area *area) 431 { 432 if (ifq->area) 433 return -EINVAL; 434 ifq->area = area; 435 return 0; 436 } 437 438 static int io_zcrx_create_area(struct io_zcrx_ifq *ifq, 439 struct io_uring_zcrx_area_reg *area_reg, 440 struct io_uring_zcrx_ifq_reg *reg) 441 { 442 int buf_size_shift = PAGE_SHIFT; 443 struct io_zcrx_area *area; 444 unsigned nr_iovs; 445 int i, ret; 446 447 if (reg->rx_buf_len) { 448 if (!is_power_of_2(reg->rx_buf_len) || 449 reg->rx_buf_len < PAGE_SIZE) 450 return -EINVAL; 451 buf_size_shift = ilog2(reg->rx_buf_len); 452 } 453 454 ret = -ENOMEM; 455 area = kzalloc_obj(*area); 456 if (!area) 457 goto err; 458 area->ifq = ifq; 459 460 ret = io_import_area(ifq, &area->mem, area_reg); 461 if (ret) 462 goto err; 463 464 if (buf_size_shift > io_area_max_shift(&area->mem)) { 465 ret = -ERANGE; 466 goto err; 467 } 468 469 ifq->niov_shift = buf_size_shift; 470 nr_iovs = area->mem.size >> ifq->niov_shift; 471 area->nia.num_niovs = nr_iovs; 472 473 ret = -ENOMEM; 474 area->nia.niovs = kvmalloc_objs(area->nia.niovs[0], nr_iovs, 475 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 476 if (!area->nia.niovs) 477 goto err; 478 479 area->freelist = kvmalloc_array(nr_iovs, sizeof(area->freelist[0]), 480 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 481 if (!area->freelist) 482 goto err; 483 484 area->user_refs = kvmalloc_objs(area->user_refs[0], nr_iovs, 485 GFP_KERNEL_ACCOUNT | __GFP_ZERO); 486 if (!area->user_refs) 487 goto err; 488 489 for (i = 0; i < nr_iovs; i++) { 490 struct net_iov *niov = &area->nia.niovs[i]; 491 492 niov->owner = &area->nia; 493 area->freelist[i] = i; 494 atomic_set(&area->user_refs[i], 0); 495 niov->type = NET_IOV_IOURING; 496 } 497 498 area->free_count = nr_iovs; 499 /* we're only supporting one area per ifq for now */ 500 area->area_id = 0; 501 area_reg->rq_area_token = (u64)area->area_id << IORING_ZCRX_AREA_SHIFT; 502 spin_lock_init(&area->freelist_lock); 503 504 ret = io_zcrx_append_area(ifq, area); 505 if (!ret) 506 return 0; 507 err: 508 if (area) 509 io_zcrx_free_area(ifq, area); 510 return ret; 511 } 512 513 static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx) 514 { 515 struct io_zcrx_ifq *ifq; 516 517 ifq = kzalloc_obj(*ifq); 518 if (!ifq) 519 return NULL; 520 521 ifq->if_rxq = -1; 522 spin_lock_init(&ifq->rq_lock); 523 mutex_init(&ifq->pp_lock); 524 refcount_set(&ifq->refs, 1); 525 refcount_set(&ifq->user_refs, 1); 526 return ifq; 527 } 528 529 static void io_zcrx_drop_netdev(struct io_zcrx_ifq *ifq) 530 { 531 guard(mutex)(&ifq->pp_lock); 532 533 if (!ifq->netdev) 534 return; 535 netdev_put(ifq->netdev, &ifq->netdev_tracker); 536 ifq->netdev = NULL; 537 } 538 539 static void io_close_queue(struct io_zcrx_ifq *ifq) 540 { 541 struct net_device *netdev; 542 netdevice_tracker netdev_tracker; 543 struct pp_memory_provider_params p = { 544 .mp_ops = &io_uring_pp_zc_ops, 545 .mp_priv = ifq, 546 }; 547 548 scoped_guard(mutex, &ifq->pp_lock) { 549 netdev = ifq->netdev; 550 netdev_tracker = ifq->netdev_tracker; 551 ifq->netdev = NULL; 552 } 553 554 if (netdev) { 555 if (ifq->if_rxq != -1) { 556 netdev_lock(netdev); 557 netif_mp_close_rxq(netdev, ifq->if_rxq, &p); 558 netdev_unlock(netdev); 559 } 560 netdev_put(netdev, &netdev_tracker); 561 } 562 ifq->if_rxq = -1; 563 } 564 565 static void io_zcrx_ifq_free(struct io_zcrx_ifq *ifq) 566 { 567 io_close_queue(ifq); 568 569 if (ifq->area) 570 io_zcrx_free_area(ifq, ifq->area); 571 free_uid(ifq->user); 572 if (ifq->mm_account) 573 mmdrop(ifq->mm_account); 574 if (ifq->dev) 575 put_device(ifq->dev); 576 577 io_free_rbuf_ring(ifq); 578 mutex_destroy(&ifq->pp_lock); 579 kfree(ifq); 580 } 581 582 static void io_put_zcrx_ifq(struct io_zcrx_ifq *ifq) 583 { 584 if (refcount_dec_and_test(&ifq->refs)) 585 io_zcrx_ifq_free(ifq); 586 } 587 588 static void io_zcrx_return_niov_freelist(struct net_iov *niov) 589 { 590 struct io_zcrx_area *area = io_zcrx_iov_to_area(niov); 591 592 spin_lock_bh(&area->freelist_lock); 593 area->freelist[area->free_count++] = net_iov_idx(niov); 594 spin_unlock_bh(&area->freelist_lock); 595 } 596 597 static void io_zcrx_return_niov(struct net_iov *niov) 598 { 599 netmem_ref netmem = net_iov_to_netmem(niov); 600 601 if (!niov->desc.pp) { 602 /* copy fallback allocated niovs */ 603 io_zcrx_return_niov_freelist(niov); 604 return; 605 } 606 page_pool_put_unrefed_netmem(niov->desc.pp, netmem, -1, false); 607 } 608 609 static void io_zcrx_scrub(struct io_zcrx_ifq *ifq) 610 { 611 struct io_zcrx_area *area = ifq->area; 612 int i; 613 614 if (!area) 615 return; 616 617 /* Reclaim back all buffers given to the user space. */ 618 for (i = 0; i < area->nia.num_niovs; i++) { 619 struct net_iov *niov = &area->nia.niovs[i]; 620 int nr; 621 622 if (!atomic_read(io_get_user_counter(niov))) 623 continue; 624 nr = atomic_xchg(io_get_user_counter(niov), 0); 625 if (nr && !page_pool_unref_netmem(net_iov_to_netmem(niov), nr)) 626 io_zcrx_return_niov(niov); 627 } 628 } 629 630 static void zcrx_unregister(struct io_zcrx_ifq *ifq) 631 { 632 if (refcount_dec_and_test(&ifq->user_refs)) { 633 io_close_queue(ifq); 634 io_zcrx_scrub(ifq); 635 } 636 io_put_zcrx_ifq(ifq); 637 } 638 639 struct io_mapped_region *io_zcrx_get_region(struct io_ring_ctx *ctx, 640 unsigned int id) 641 { 642 struct io_zcrx_ifq *ifq = xa_load(&ctx->zcrx_ctxs, id); 643 644 lockdep_assert_held(&ctx->mmap_lock); 645 646 return ifq ? &ifq->region : NULL; 647 } 648 649 static int zcrx_box_release(struct inode *inode, struct file *file) 650 { 651 struct io_zcrx_ifq *ifq = file->private_data; 652 653 if (WARN_ON_ONCE(!ifq)) 654 return -EFAULT; 655 zcrx_unregister(ifq); 656 return 0; 657 } 658 659 static const struct file_operations zcrx_box_fops = { 660 .owner = THIS_MODULE, 661 .release = zcrx_box_release, 662 }; 663 664 static int zcrx_export(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq, 665 struct zcrx_ctrl *ctrl, void __user *arg) 666 { 667 struct zcrx_ctrl_export *ce = &ctrl->zc_export; 668 struct file *file; 669 int fd = -1; 670 671 if (!mem_is_zero(ce, sizeof(*ce))) 672 return -EINVAL; 673 fd = get_unused_fd_flags(O_CLOEXEC); 674 if (fd < 0) 675 return fd; 676 677 ce->zcrx_fd = fd; 678 if (copy_to_user(arg, ctrl, sizeof(*ctrl))) { 679 put_unused_fd(fd); 680 return -EFAULT; 681 } 682 683 refcount_inc(&ifq->refs); 684 refcount_inc(&ifq->user_refs); 685 686 file = anon_inode_create_getfile("[zcrx]", &zcrx_box_fops, 687 ifq, O_CLOEXEC, NULL); 688 if (IS_ERR(file)) { 689 put_unused_fd(fd); 690 zcrx_unregister(ifq); 691 return PTR_ERR(file); 692 } 693 694 fd_install(fd, file); 695 return 0; 696 } 697 698 static int import_zcrx(struct io_ring_ctx *ctx, 699 struct io_uring_zcrx_ifq_reg __user *arg, 700 struct io_uring_zcrx_ifq_reg *reg) 701 { 702 struct io_zcrx_ifq *ifq; 703 struct file *file; 704 int fd, ret; 705 u32 id; 706 707 if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) 708 return -EINVAL; 709 if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))) 710 return -EINVAL; 711 if (reg->if_rxq || reg->rq_entries || reg->area_ptr || reg->region_ptr) 712 return -EINVAL; 713 if (reg->flags & ~ZCRX_REG_IMPORT) 714 return -EINVAL; 715 716 fd = reg->if_idx; 717 CLASS(fd, f)(fd); 718 if (fd_empty(f)) 719 return -EBADF; 720 721 file = fd_file(f); 722 if (file->f_op != &zcrx_box_fops || !file->private_data) 723 return -EBADF; 724 725 ifq = file->private_data; 726 refcount_inc(&ifq->refs); 727 refcount_inc(&ifq->user_refs); 728 729 scoped_guard(mutex, &ctx->mmap_lock) { 730 ret = xa_alloc(&ctx->zcrx_ctxs, &id, NULL, xa_limit_31b, GFP_KERNEL); 731 if (ret) 732 goto err; 733 } 734 735 reg->zcrx_id = id; 736 io_fill_zcrx_offsets(®->offsets); 737 if (copy_to_user(arg, reg, sizeof(*reg))) { 738 ret = -EFAULT; 739 goto err_xa_erase; 740 } 741 742 scoped_guard(mutex, &ctx->mmap_lock) { 743 ret = -ENOMEM; 744 if (xa_store(&ctx->zcrx_ctxs, id, ifq, GFP_KERNEL)) 745 goto err_xa_erase; 746 } 747 748 return 0; 749 err_xa_erase: 750 scoped_guard(mutex, &ctx->mmap_lock) 751 xa_erase(&ctx->zcrx_ctxs, id); 752 err: 753 zcrx_unregister(ifq); 754 return ret; 755 } 756 757 int io_register_zcrx_ifq(struct io_ring_ctx *ctx, 758 struct io_uring_zcrx_ifq_reg __user *arg) 759 { 760 struct pp_memory_provider_params mp_param = {}; 761 struct io_uring_zcrx_area_reg area; 762 struct io_uring_zcrx_ifq_reg reg; 763 struct io_uring_region_desc rd; 764 struct io_zcrx_ifq *ifq; 765 int ret; 766 u32 id; 767 768 /* 769 * 1. Interface queue allocation. 770 * 2. It can observe data destined for sockets of other tasks. 771 */ 772 if (!capable(CAP_NET_ADMIN)) 773 return -EPERM; 774 775 /* mandatory io_uring features for zc rx */ 776 if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) 777 return -EINVAL; 778 if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))) 779 return -EINVAL; 780 if (copy_from_user(®, arg, sizeof(reg))) 781 return -EFAULT; 782 if (!mem_is_zero(®.__resv, sizeof(reg.__resv)) || reg.zcrx_id) 783 return -EINVAL; 784 if (reg.flags & ZCRX_REG_IMPORT) 785 return import_zcrx(ctx, arg, ®); 786 if (copy_from_user(&rd, u64_to_user_ptr(reg.region_ptr), sizeof(rd))) 787 return -EFAULT; 788 if (reg.if_rxq == -1 || !reg.rq_entries || reg.flags) 789 return -EINVAL; 790 if (reg.rq_entries > IO_RQ_MAX_ENTRIES) { 791 if (!(ctx->flags & IORING_SETUP_CLAMP)) 792 return -EINVAL; 793 reg.rq_entries = IO_RQ_MAX_ENTRIES; 794 } 795 reg.rq_entries = roundup_pow_of_two(reg.rq_entries); 796 797 if (copy_from_user(&area, u64_to_user_ptr(reg.area_ptr), sizeof(area))) 798 return -EFAULT; 799 800 ifq = io_zcrx_ifq_alloc(ctx); 801 if (!ifq) 802 return -ENOMEM; 803 804 if (ctx->user) { 805 get_uid(ctx->user); 806 ifq->user = ctx->user; 807 } 808 if (ctx->mm_account) { 809 mmgrab(ctx->mm_account); 810 ifq->mm_account = ctx->mm_account; 811 } 812 ifq->rq_entries = reg.rq_entries; 813 814 scoped_guard(mutex, &ctx->mmap_lock) { 815 /* preallocate id */ 816 ret = xa_alloc(&ctx->zcrx_ctxs, &id, NULL, xa_limit_31b, GFP_KERNEL); 817 if (ret) 818 goto ifq_free; 819 } 820 821 ret = io_allocate_rbuf_ring(ctx, ifq, ®, &rd, id); 822 if (ret) 823 goto err; 824 825 ifq->netdev = netdev_get_by_index_lock(current->nsproxy->net_ns, reg.if_idx); 826 if (!ifq->netdev) { 827 ret = -ENODEV; 828 goto err; 829 } 830 netdev_hold(ifq->netdev, &ifq->netdev_tracker, GFP_KERNEL); 831 832 ifq->dev = netdev_queue_get_dma_dev(ifq->netdev, reg.if_rxq); 833 if (!ifq->dev) { 834 ret = -EOPNOTSUPP; 835 goto netdev_put_unlock; 836 } 837 get_device(ifq->dev); 838 839 ret = io_zcrx_create_area(ifq, &area, ®); 840 if (ret) 841 goto netdev_put_unlock; 842 843 if (reg.rx_buf_len) 844 mp_param.rx_page_size = 1U << ifq->niov_shift; 845 mp_param.mp_ops = &io_uring_pp_zc_ops; 846 mp_param.mp_priv = ifq; 847 ret = netif_mp_open_rxq(ifq->netdev, reg.if_rxq, &mp_param, NULL); 848 if (ret) 849 goto netdev_put_unlock; 850 netdev_unlock(ifq->netdev); 851 ifq->if_rxq = reg.if_rxq; 852 853 reg.zcrx_id = id; 854 855 scoped_guard(mutex, &ctx->mmap_lock) { 856 /* publish ifq */ 857 ret = -ENOMEM; 858 if (xa_store(&ctx->zcrx_ctxs, id, ifq, GFP_KERNEL)) 859 goto err; 860 } 861 862 reg.rx_buf_len = 1U << ifq->niov_shift; 863 864 if (copy_to_user(arg, ®, sizeof(reg)) || 865 copy_to_user(u64_to_user_ptr(reg.region_ptr), &rd, sizeof(rd)) || 866 copy_to_user(u64_to_user_ptr(reg.area_ptr), &area, sizeof(area))) { 867 ret = -EFAULT; 868 goto err; 869 } 870 return 0; 871 netdev_put_unlock: 872 netdev_unlock(ifq->netdev); 873 err: 874 scoped_guard(mutex, &ctx->mmap_lock) 875 xa_erase(&ctx->zcrx_ctxs, id); 876 ifq_free: 877 zcrx_unregister(ifq); 878 return ret; 879 } 880 881 static struct net_iov *__io_zcrx_get_free_niov(struct io_zcrx_area *area) 882 { 883 unsigned niov_idx; 884 885 lockdep_assert_held(&area->freelist_lock); 886 887 niov_idx = area->freelist[--area->free_count]; 888 return &area->nia.niovs[niov_idx]; 889 } 890 891 void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx) 892 { 893 struct io_zcrx_ifq *ifq; 894 895 lockdep_assert_held(&ctx->uring_lock); 896 897 while (1) { 898 scoped_guard(mutex, &ctx->mmap_lock) { 899 unsigned long id = 0; 900 901 ifq = xa_find(&ctx->zcrx_ctxs, &id, ULONG_MAX, XA_PRESENT); 902 if (ifq) 903 xa_erase(&ctx->zcrx_ctxs, id); 904 } 905 if (!ifq) 906 break; 907 zcrx_unregister(ifq); 908 } 909 910 xa_destroy(&ctx->zcrx_ctxs); 911 } 912 913 static inline u32 io_zcrx_rqring_entries(struct io_zcrx_ifq *ifq) 914 { 915 u32 entries; 916 917 entries = smp_load_acquire(&ifq->rq_ring->tail) - ifq->cached_rq_head; 918 return min(entries, ifq->rq_entries); 919 } 920 921 static struct io_uring_zcrx_rqe *io_zcrx_get_rqe(struct io_zcrx_ifq *ifq, 922 unsigned mask) 923 { 924 unsigned int idx = ifq->cached_rq_head++ & mask; 925 926 return &ifq->rqes[idx]; 927 } 928 929 static inline bool io_parse_rqe(struct io_uring_zcrx_rqe *rqe, 930 struct io_zcrx_ifq *ifq, 931 struct net_iov **ret_niov) 932 { 933 __u64 off = READ_ONCE(rqe->off); 934 unsigned niov_idx, area_idx; 935 struct io_zcrx_area *area; 936 937 area_idx = off >> IORING_ZCRX_AREA_SHIFT; 938 niov_idx = (off & ~IORING_ZCRX_AREA_MASK) >> ifq->niov_shift; 939 940 if (unlikely(rqe->__pad || area_idx)) 941 return false; 942 area = ifq->area; 943 944 if (unlikely(niov_idx >= area->nia.num_niovs)) 945 return false; 946 niov_idx = array_index_nospec(niov_idx, area->nia.num_niovs); 947 948 *ret_niov = &area->nia.niovs[niov_idx]; 949 return true; 950 } 951 952 static void io_zcrx_ring_refill(struct page_pool *pp, 953 struct io_zcrx_ifq *ifq) 954 { 955 unsigned int mask = ifq->rq_entries - 1; 956 unsigned int entries; 957 958 guard(spinlock_bh)(&ifq->rq_lock); 959 960 entries = io_zcrx_rqring_entries(ifq); 961 entries = min_t(unsigned, entries, PP_ALLOC_CACHE_REFILL); 962 if (unlikely(!entries)) 963 return; 964 965 do { 966 struct io_uring_zcrx_rqe *rqe = io_zcrx_get_rqe(ifq, mask); 967 struct net_iov *niov; 968 netmem_ref netmem; 969 970 if (!io_parse_rqe(rqe, ifq, &niov)) 971 continue; 972 if (!io_zcrx_put_niov_uref(niov)) 973 continue; 974 975 netmem = net_iov_to_netmem(niov); 976 if (!page_pool_unref_and_test(netmem)) 977 continue; 978 979 if (unlikely(niov->desc.pp != pp)) { 980 io_zcrx_return_niov(niov); 981 continue; 982 } 983 984 io_zcrx_sync_for_device(pp, niov); 985 net_mp_netmem_place_in_cache(pp, netmem); 986 } while (--entries); 987 988 smp_store_release(&ifq->rq_ring->head, ifq->cached_rq_head); 989 } 990 991 static void io_zcrx_refill_slow(struct page_pool *pp, struct io_zcrx_ifq *ifq) 992 { 993 struct io_zcrx_area *area = ifq->area; 994 995 spin_lock_bh(&area->freelist_lock); 996 while (area->free_count && pp->alloc.count < PP_ALLOC_CACHE_REFILL) { 997 struct net_iov *niov = __io_zcrx_get_free_niov(area); 998 netmem_ref netmem = net_iov_to_netmem(niov); 999 1000 net_mp_niov_set_page_pool(pp, niov); 1001 io_zcrx_sync_for_device(pp, niov); 1002 net_mp_netmem_place_in_cache(pp, netmem); 1003 } 1004 spin_unlock_bh(&area->freelist_lock); 1005 } 1006 1007 static netmem_ref io_pp_zc_alloc_netmems(struct page_pool *pp, gfp_t gfp) 1008 { 1009 struct io_zcrx_ifq *ifq = io_pp_to_ifq(pp); 1010 1011 /* pp should already be ensuring that */ 1012 if (unlikely(pp->alloc.count)) 1013 goto out_return; 1014 1015 io_zcrx_ring_refill(pp, ifq); 1016 if (likely(pp->alloc.count)) 1017 goto out_return; 1018 1019 io_zcrx_refill_slow(pp, ifq); 1020 if (!pp->alloc.count) 1021 return 0; 1022 out_return: 1023 return pp->alloc.cache[--pp->alloc.count]; 1024 } 1025 1026 static bool io_pp_zc_release_netmem(struct page_pool *pp, netmem_ref netmem) 1027 { 1028 struct net_iov *niov; 1029 1030 if (WARN_ON_ONCE(!netmem_is_net_iov(netmem))) 1031 return false; 1032 1033 niov = netmem_to_net_iov(netmem); 1034 net_mp_niov_clear_page_pool(niov); 1035 io_zcrx_return_niov_freelist(niov); 1036 return false; 1037 } 1038 1039 static int io_pp_zc_init(struct page_pool *pp) 1040 { 1041 struct io_zcrx_ifq *ifq = io_pp_to_ifq(pp); 1042 int ret; 1043 1044 if (WARN_ON_ONCE(!ifq)) 1045 return -EINVAL; 1046 if (WARN_ON_ONCE(ifq->dev != pp->p.dev)) 1047 return -EINVAL; 1048 if (WARN_ON_ONCE(!pp->dma_map)) 1049 return -EOPNOTSUPP; 1050 if (pp->p.order + PAGE_SHIFT != ifq->niov_shift) 1051 return -EINVAL; 1052 if (pp->p.dma_dir != DMA_FROM_DEVICE) 1053 return -EOPNOTSUPP; 1054 1055 ret = io_zcrx_map_area(ifq, ifq->area); 1056 if (ret) 1057 return ret; 1058 1059 refcount_inc(&ifq->refs); 1060 return 0; 1061 } 1062 1063 static void io_pp_zc_destroy(struct page_pool *pp) 1064 { 1065 io_put_zcrx_ifq(io_pp_to_ifq(pp)); 1066 } 1067 1068 static int io_pp_nl_fill(void *mp_priv, struct sk_buff *rsp, 1069 struct netdev_rx_queue *rxq) 1070 { 1071 struct nlattr *nest; 1072 int type; 1073 1074 type = rxq ? NETDEV_A_QUEUE_IO_URING : NETDEV_A_PAGE_POOL_IO_URING; 1075 nest = nla_nest_start(rsp, type); 1076 if (!nest) 1077 return -EMSGSIZE; 1078 nla_nest_end(rsp, nest); 1079 1080 return 0; 1081 } 1082 1083 static void io_pp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq) 1084 { 1085 struct pp_memory_provider_params *p = &rxq->mp_params; 1086 struct io_zcrx_ifq *ifq = mp_priv; 1087 1088 io_zcrx_drop_netdev(ifq); 1089 if (ifq->area) 1090 io_zcrx_unmap_area(ifq, ifq->area); 1091 1092 p->mp_ops = NULL; 1093 p->mp_priv = NULL; 1094 } 1095 1096 static const struct memory_provider_ops io_uring_pp_zc_ops = { 1097 .alloc_netmems = io_pp_zc_alloc_netmems, 1098 .release_netmem = io_pp_zc_release_netmem, 1099 .init = io_pp_zc_init, 1100 .destroy = io_pp_zc_destroy, 1101 .nl_fill = io_pp_nl_fill, 1102 .uninstall = io_pp_uninstall, 1103 }; 1104 1105 static unsigned zcrx_parse_rq(netmem_ref *netmem_array, unsigned nr, 1106 struct io_zcrx_ifq *zcrx) 1107 { 1108 unsigned int mask = zcrx->rq_entries - 1; 1109 unsigned int i; 1110 1111 nr = min(nr, io_zcrx_rqring_entries(zcrx)); 1112 for (i = 0; i < nr; i++) { 1113 struct io_uring_zcrx_rqe *rqe = io_zcrx_get_rqe(zcrx, mask); 1114 struct net_iov *niov; 1115 1116 if (!io_parse_rqe(rqe, zcrx, &niov)) 1117 break; 1118 netmem_array[i] = net_iov_to_netmem(niov); 1119 } 1120 1121 smp_store_release(&zcrx->rq_ring->head, zcrx->cached_rq_head); 1122 return i; 1123 } 1124 1125 #define ZCRX_FLUSH_BATCH 32 1126 1127 static void zcrx_return_buffers(netmem_ref *netmems, unsigned nr) 1128 { 1129 unsigned i; 1130 1131 for (i = 0; i < nr; i++) { 1132 netmem_ref netmem = netmems[i]; 1133 struct net_iov *niov = netmem_to_net_iov(netmem); 1134 1135 if (!io_zcrx_put_niov_uref(niov)) 1136 continue; 1137 if (!page_pool_unref_and_test(netmem)) 1138 continue; 1139 io_zcrx_return_niov(niov); 1140 } 1141 } 1142 1143 static int zcrx_flush_rq(struct io_ring_ctx *ctx, struct io_zcrx_ifq *zcrx, 1144 struct zcrx_ctrl *ctrl) 1145 { 1146 struct zcrx_ctrl_flush_rq *frq = &ctrl->zc_flush; 1147 netmem_ref netmems[ZCRX_FLUSH_BATCH]; 1148 unsigned total = 0; 1149 unsigned nr; 1150 1151 if (!mem_is_zero(&frq->__resv, sizeof(frq->__resv))) 1152 return -EINVAL; 1153 1154 do { 1155 scoped_guard(spinlock_bh, &zcrx->rq_lock) { 1156 nr = zcrx_parse_rq(netmems, ZCRX_FLUSH_BATCH, zcrx); 1157 zcrx_return_buffers(netmems, nr); 1158 } 1159 1160 total += nr; 1161 1162 if (fatal_signal_pending(current)) 1163 break; 1164 cond_resched(); 1165 } while (nr == ZCRX_FLUSH_BATCH && total < zcrx->rq_entries); 1166 1167 return 0; 1168 } 1169 1170 int io_zcrx_ctrl(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) 1171 { 1172 struct zcrx_ctrl ctrl; 1173 struct io_zcrx_ifq *zcrx; 1174 1175 if (nr_args) 1176 return -EINVAL; 1177 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1178 return -EFAULT; 1179 if (!mem_is_zero(&ctrl.__resv, sizeof(ctrl.__resv))) 1180 return -EFAULT; 1181 1182 zcrx = xa_load(&ctx->zcrx_ctxs, ctrl.zcrx_id); 1183 if (!zcrx) 1184 return -ENXIO; 1185 1186 switch (ctrl.op) { 1187 case ZCRX_CTRL_FLUSH_RQ: 1188 return zcrx_flush_rq(ctx, zcrx, &ctrl); 1189 case ZCRX_CTRL_EXPORT: 1190 return zcrx_export(ctx, zcrx, &ctrl, arg); 1191 } 1192 1193 return -EOPNOTSUPP; 1194 } 1195 1196 static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov, 1197 struct io_zcrx_ifq *ifq, int off, int len) 1198 { 1199 struct io_ring_ctx *ctx = req->ctx; 1200 struct io_uring_zcrx_cqe *rcqe; 1201 struct io_zcrx_area *area; 1202 struct io_uring_cqe *cqe; 1203 u64 offset; 1204 1205 if (!io_defer_get_uncommited_cqe(ctx, &cqe)) 1206 return false; 1207 1208 cqe->user_data = req->cqe.user_data; 1209 cqe->res = len; 1210 cqe->flags = IORING_CQE_F_MORE; 1211 if (ctx->flags & IORING_SETUP_CQE_MIXED) 1212 cqe->flags |= IORING_CQE_F_32; 1213 1214 area = io_zcrx_iov_to_area(niov); 1215 offset = off + (net_iov_idx(niov) << ifq->niov_shift); 1216 rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1); 1217 rcqe->off = offset + ((u64)area->area_id << IORING_ZCRX_AREA_SHIFT); 1218 rcqe->__pad = 0; 1219 return true; 1220 } 1221 1222 static struct net_iov *io_alloc_fallback_niov(struct io_zcrx_ifq *ifq) 1223 { 1224 struct io_zcrx_area *area = ifq->area; 1225 struct net_iov *niov = NULL; 1226 1227 if (area->mem.is_dmabuf) 1228 return NULL; 1229 1230 spin_lock_bh(&area->freelist_lock); 1231 if (area->free_count) 1232 niov = __io_zcrx_get_free_niov(area); 1233 spin_unlock_bh(&area->freelist_lock); 1234 1235 if (niov) 1236 page_pool_fragment_netmem(net_iov_to_netmem(niov), 1); 1237 return niov; 1238 } 1239 1240 struct io_copy_cache { 1241 struct page *page; 1242 unsigned long offset; 1243 size_t size; 1244 }; 1245 1246 static ssize_t io_copy_page(struct io_copy_cache *cc, struct page *src_page, 1247 unsigned int src_offset, size_t len) 1248 { 1249 size_t copied = 0; 1250 1251 len = min(len, cc->size); 1252 1253 while (len) { 1254 void *src_addr, *dst_addr; 1255 struct page *dst_page = cc->page; 1256 unsigned dst_offset = cc->offset; 1257 size_t n = len; 1258 1259 if (folio_test_partial_kmap(page_folio(dst_page)) || 1260 folio_test_partial_kmap(page_folio(src_page))) { 1261 dst_page += dst_offset / PAGE_SIZE; 1262 dst_offset = offset_in_page(dst_offset); 1263 src_page += src_offset / PAGE_SIZE; 1264 src_offset = offset_in_page(src_offset); 1265 n = min(PAGE_SIZE - src_offset, PAGE_SIZE - dst_offset); 1266 n = min(n, len); 1267 } 1268 1269 dst_addr = kmap_local_page(dst_page) + dst_offset; 1270 src_addr = kmap_local_page(src_page) + src_offset; 1271 1272 memcpy(dst_addr, src_addr, n); 1273 1274 kunmap_local(src_addr); 1275 kunmap_local(dst_addr); 1276 1277 cc->size -= n; 1278 cc->offset += n; 1279 src_offset += n; 1280 len -= n; 1281 copied += n; 1282 } 1283 return copied; 1284 } 1285 1286 static ssize_t io_zcrx_copy_chunk(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1287 struct page *src_page, unsigned int src_offset, 1288 size_t len) 1289 { 1290 size_t copied = 0; 1291 int ret = 0; 1292 1293 while (len) { 1294 struct io_copy_cache cc; 1295 struct net_iov *niov; 1296 size_t n; 1297 1298 niov = io_alloc_fallback_niov(ifq); 1299 if (!niov) { 1300 ret = -ENOMEM; 1301 break; 1302 } 1303 1304 cc.page = io_zcrx_iov_page(niov); 1305 cc.offset = 0; 1306 cc.size = PAGE_SIZE; 1307 1308 n = io_copy_page(&cc, src_page, src_offset, len); 1309 1310 if (!io_zcrx_queue_cqe(req, niov, ifq, 0, n)) { 1311 io_zcrx_return_niov(niov); 1312 ret = -ENOSPC; 1313 break; 1314 } 1315 1316 io_zcrx_get_niov_uref(niov); 1317 src_offset += n; 1318 len -= n; 1319 copied += n; 1320 } 1321 1322 return copied ? copied : ret; 1323 } 1324 1325 static int io_zcrx_copy_frag(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1326 const skb_frag_t *frag, int off, int len) 1327 { 1328 struct page *page = skb_frag_page(frag); 1329 1330 return io_zcrx_copy_chunk(req, ifq, page, off + skb_frag_off(frag), len); 1331 } 1332 1333 static int io_zcrx_recv_frag(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1334 const skb_frag_t *frag, int off, int len) 1335 { 1336 struct net_iov *niov; 1337 struct page_pool *pp; 1338 1339 if (unlikely(!skb_frag_is_net_iov(frag))) 1340 return io_zcrx_copy_frag(req, ifq, frag, off, len); 1341 1342 niov = netmem_to_net_iov(frag->netmem); 1343 pp = niov->desc.pp; 1344 1345 if (!pp || pp->mp_ops != &io_uring_pp_zc_ops || io_pp_to_ifq(pp) != ifq) 1346 return -EFAULT; 1347 1348 if (!io_zcrx_queue_cqe(req, niov, ifq, off + skb_frag_off(frag), len)) 1349 return -ENOSPC; 1350 1351 /* 1352 * Prevent it from being recycled while user is accessing it. 1353 * It has to be done before grabbing a user reference. 1354 */ 1355 page_pool_ref_netmem(net_iov_to_netmem(niov)); 1356 io_zcrx_get_niov_uref(niov); 1357 return len; 1358 } 1359 1360 static int 1361 io_zcrx_recv_skb(read_descriptor_t *desc, struct sk_buff *skb, 1362 unsigned int offset, size_t len) 1363 { 1364 struct io_zcrx_args *args = desc->arg.data; 1365 struct io_zcrx_ifq *ifq = args->ifq; 1366 struct io_kiocb *req = args->req; 1367 struct sk_buff *frag_iter; 1368 unsigned start, start_off = offset; 1369 int i, copy, end, off; 1370 int ret = 0; 1371 1372 len = min_t(size_t, len, desc->count); 1373 /* 1374 * __tcp_read_sock() always calls io_zcrx_recv_skb one last time, even 1375 * if desc->count is already 0. This is caused by the if (offset + 1 != 1376 * skb->len) check. Return early in this case to break out of 1377 * __tcp_read_sock(). 1378 */ 1379 if (!len) 1380 return 0; 1381 if (unlikely(args->nr_skbs++ > IO_SKBS_PER_CALL_LIMIT)) 1382 return -EAGAIN; 1383 1384 if (unlikely(offset < skb_headlen(skb))) { 1385 ssize_t copied; 1386 size_t to_copy; 1387 1388 to_copy = min_t(size_t, skb_headlen(skb) - offset, len); 1389 copied = io_zcrx_copy_chunk(req, ifq, virt_to_page(skb->data), 1390 offset_in_page(skb->data) + offset, 1391 to_copy); 1392 if (copied < 0) { 1393 ret = copied; 1394 goto out; 1395 } 1396 offset += copied; 1397 len -= copied; 1398 if (!len) 1399 goto out; 1400 if (offset != skb_headlen(skb)) 1401 goto out; 1402 } 1403 1404 start = skb_headlen(skb); 1405 1406 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1407 const skb_frag_t *frag; 1408 1409 if (WARN_ON(start > offset + len)) 1410 return -EFAULT; 1411 1412 frag = &skb_shinfo(skb)->frags[i]; 1413 end = start + skb_frag_size(frag); 1414 1415 if (offset < end) { 1416 copy = end - offset; 1417 if (copy > len) 1418 copy = len; 1419 1420 off = offset - start; 1421 ret = io_zcrx_recv_frag(req, ifq, frag, off, copy); 1422 if (ret < 0) 1423 goto out; 1424 1425 offset += ret; 1426 len -= ret; 1427 if (len == 0 || ret != copy) 1428 goto out; 1429 } 1430 start = end; 1431 } 1432 1433 skb_walk_frags(skb, frag_iter) { 1434 if (WARN_ON(start > offset + len)) 1435 return -EFAULT; 1436 1437 end = start + frag_iter->len; 1438 if (offset < end) { 1439 size_t count; 1440 1441 copy = end - offset; 1442 if (copy > len) 1443 copy = len; 1444 1445 off = offset - start; 1446 count = desc->count; 1447 ret = io_zcrx_recv_skb(desc, frag_iter, off, copy); 1448 desc->count = count; 1449 if (ret < 0) 1450 goto out; 1451 1452 offset += ret; 1453 len -= ret; 1454 if (len == 0 || ret != copy) 1455 goto out; 1456 } 1457 start = end; 1458 } 1459 1460 out: 1461 if (offset == start_off) 1462 return ret; 1463 desc->count -= (offset - start_off); 1464 return offset - start_off; 1465 } 1466 1467 static int io_zcrx_tcp_recvmsg(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1468 struct sock *sk, int flags, 1469 unsigned issue_flags, unsigned int *outlen) 1470 { 1471 unsigned int len = *outlen; 1472 struct io_zcrx_args args = { 1473 .req = req, 1474 .ifq = ifq, 1475 .sock = sk->sk_socket, 1476 }; 1477 read_descriptor_t rd_desc = { 1478 .count = len ? len : UINT_MAX, 1479 .arg.data = &args, 1480 }; 1481 int ret; 1482 1483 lock_sock(sk); 1484 ret = tcp_read_sock(sk, &rd_desc, io_zcrx_recv_skb); 1485 if (len && ret > 0) 1486 *outlen = len - ret; 1487 if (ret <= 0) { 1488 if (ret < 0 || sock_flag(sk, SOCK_DONE)) 1489 goto out; 1490 if (sk->sk_err) 1491 ret = sock_error(sk); 1492 else if (sk->sk_shutdown & RCV_SHUTDOWN) 1493 goto out; 1494 else if (sk->sk_state == TCP_CLOSE) 1495 ret = -ENOTCONN; 1496 else 1497 ret = -EAGAIN; 1498 } else if (unlikely(args.nr_skbs > IO_SKBS_PER_CALL_LIMIT) && 1499 (issue_flags & IO_URING_F_MULTISHOT)) { 1500 ret = IOU_REQUEUE; 1501 } else if (sock_flag(sk, SOCK_DONE)) { 1502 /* Make it to retry until it finally gets 0. */ 1503 if (issue_flags & IO_URING_F_MULTISHOT) 1504 ret = IOU_REQUEUE; 1505 else 1506 ret = -EAGAIN; 1507 } 1508 out: 1509 release_sock(sk); 1510 return ret; 1511 } 1512 1513 int io_zcrx_recv(struct io_kiocb *req, struct io_zcrx_ifq *ifq, 1514 struct socket *sock, unsigned int flags, 1515 unsigned issue_flags, unsigned int *len) 1516 { 1517 struct sock *sk = sock->sk; 1518 const struct proto *prot = READ_ONCE(sk->sk_prot); 1519 1520 if (prot->recvmsg != tcp_recvmsg) 1521 return -EPROTONOSUPPORT; 1522 1523 sock_rps_record_flow(sk); 1524 return io_zcrx_tcp_recvmsg(req, ifq, sk, flags, issue_flags, len); 1525 } 1526