|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <linux/kernel.h> |
| 3 | +#include <linux/errno.h> |
| 4 | +#include <linux/mm.h> |
| 5 | +#include <linux/io_uring.h> |
| 6 | + |
| 7 | +#include <uapi/linux/io_uring.h> |
| 8 | + |
| 9 | +#include "io_uring.h" |
| 10 | +#include "kbuf.h" |
| 11 | +#include "memmap.h" |
| 12 | +#include "zcrx.h" |
| 13 | + |
| 14 | +#define IO_RQ_MAX_ENTRIES 32768 |
| 15 | + |
| 16 | +static int io_allocate_rbuf_ring(struct io_zcrx_ifq *ifq, |
| 17 | + struct io_uring_zcrx_ifq_reg *reg, |
| 18 | + struct io_uring_region_desc *rd) |
| 19 | +{ |
| 20 | + size_t off, size; |
| 21 | + void *ptr; |
| 22 | + int ret; |
| 23 | + |
| 24 | + off = sizeof(struct io_uring); |
| 25 | + size = off + sizeof(struct io_uring_zcrx_rqe) * reg->rq_entries; |
| 26 | + if (size > rd->size) |
| 27 | + return -EINVAL; |
| 28 | + |
| 29 | + ret = io_create_region_mmap_safe(ifq->ctx, &ifq->ctx->zcrx_region, rd, |
| 30 | + IORING_MAP_OFF_ZCRX_REGION); |
| 31 | + if (ret < 0) |
| 32 | + return ret; |
| 33 | + |
| 34 | + ptr = io_region_get_ptr(&ifq->ctx->zcrx_region); |
| 35 | + ifq->rq_ring = (struct io_uring *)ptr; |
| 36 | + ifq->rqes = (struct io_uring_zcrx_rqe *)(ptr + off); |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +static void io_free_rbuf_ring(struct io_zcrx_ifq *ifq) |
| 41 | +{ |
| 42 | + io_free_region(ifq->ctx, &ifq->ctx->zcrx_region); |
| 43 | + ifq->rq_ring = NULL; |
| 44 | + ifq->rqes = NULL; |
| 45 | +} |
| 46 | + |
| 47 | +static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx) |
| 48 | +{ |
| 49 | + struct io_zcrx_ifq *ifq; |
| 50 | + |
| 51 | + ifq = kzalloc(sizeof(*ifq), GFP_KERNEL); |
| 52 | + if (!ifq) |
| 53 | + return NULL; |
| 54 | + |
| 55 | + ifq->if_rxq = -1; |
| 56 | + ifq->ctx = ctx; |
| 57 | + return ifq; |
| 58 | +} |
| 59 | + |
| 60 | +static void io_zcrx_ifq_free(struct io_zcrx_ifq *ifq) |
| 61 | +{ |
| 62 | + io_free_rbuf_ring(ifq); |
| 63 | + kfree(ifq); |
| 64 | +} |
| 65 | + |
| 66 | +int io_register_zcrx_ifq(struct io_ring_ctx *ctx, |
| 67 | + struct io_uring_zcrx_ifq_reg __user *arg) |
| 68 | +{ |
| 69 | + struct io_uring_zcrx_ifq_reg reg; |
| 70 | + struct io_uring_region_desc rd; |
| 71 | + struct io_zcrx_ifq *ifq; |
| 72 | + int ret; |
| 73 | + |
| 74 | + /* |
| 75 | + * 1. Interface queue allocation. |
| 76 | + * 2. It can observe data destined for sockets of other tasks. |
| 77 | + */ |
| 78 | + if (!capable(CAP_NET_ADMIN)) |
| 79 | + return -EPERM; |
| 80 | + |
| 81 | + /* mandatory io_uring features for zc rx */ |
| 82 | + if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN && |
| 83 | + ctx->flags & IORING_SETUP_CQE32)) |
| 84 | + return -EINVAL; |
| 85 | + if (ctx->ifq) |
| 86 | + return -EBUSY; |
| 87 | + if (copy_from_user(®, arg, sizeof(reg))) |
| 88 | + return -EFAULT; |
| 89 | + if (copy_from_user(&rd, u64_to_user_ptr(reg.region_ptr), sizeof(rd))) |
| 90 | + return -EFAULT; |
| 91 | + if (memchr_inv(®.__resv, 0, sizeof(reg.__resv))) |
| 92 | + return -EINVAL; |
| 93 | + if (reg.if_rxq == -1 || !reg.rq_entries || reg.flags) |
| 94 | + return -EINVAL; |
| 95 | + if (reg.rq_entries > IO_RQ_MAX_ENTRIES) { |
| 96 | + if (!(ctx->flags & IORING_SETUP_CLAMP)) |
| 97 | + return -EINVAL; |
| 98 | + reg.rq_entries = IO_RQ_MAX_ENTRIES; |
| 99 | + } |
| 100 | + reg.rq_entries = roundup_pow_of_two(reg.rq_entries); |
| 101 | + |
| 102 | + if (!reg.area_ptr) |
| 103 | + return -EFAULT; |
| 104 | + |
| 105 | + ifq = io_zcrx_ifq_alloc(ctx); |
| 106 | + if (!ifq) |
| 107 | + return -ENOMEM; |
| 108 | + |
| 109 | + ret = io_allocate_rbuf_ring(ifq, ®, &rd); |
| 110 | + if (ret) |
| 111 | + goto err; |
| 112 | + |
| 113 | + ifq->rq_entries = reg.rq_entries; |
| 114 | + ifq->if_rxq = reg.if_rxq; |
| 115 | + |
| 116 | + reg.offsets.rqes = sizeof(struct io_uring); |
| 117 | + reg.offsets.head = offsetof(struct io_uring, head); |
| 118 | + reg.offsets.tail = offsetof(struct io_uring, tail); |
| 119 | + |
| 120 | + if (copy_to_user(arg, ®, sizeof(reg)) || |
| 121 | + copy_to_user(u64_to_user_ptr(reg.region_ptr), &rd, sizeof(rd))) { |
| 122 | + ret = -EFAULT; |
| 123 | + goto err; |
| 124 | + } |
| 125 | + |
| 126 | + ctx->ifq = ifq; |
| 127 | + return 0; |
| 128 | +err: |
| 129 | + io_zcrx_ifq_free(ifq); |
| 130 | + return ret; |
| 131 | +} |
| 132 | + |
| 133 | +void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx) |
| 134 | +{ |
| 135 | + struct io_zcrx_ifq *ifq = ctx->ifq; |
| 136 | + |
| 137 | + lockdep_assert_held(&ctx->uring_lock); |
| 138 | + |
| 139 | + if (!ifq) |
| 140 | + return; |
| 141 | + |
| 142 | + ctx->ifq = NULL; |
| 143 | + io_zcrx_ifq_free(ifq); |
| 144 | +} |
| 145 | + |
| 146 | +void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx) |
| 147 | +{ |
| 148 | + lockdep_assert_held(&ctx->uring_lock); |
| 149 | +} |
0 commit comments