Skip to content

Commit 792060d

Browse files
committed
io_uring/net: improve the usercopy for sendmsg/recvmsg
We're spending a considerable amount of the sendmsg/recvmsg time just copying in the message header. And for provided buffers, the known single entry iovec. Be a bit smarter about it and enable/disable user access around our copying. In a test case that does both sendmsg and recvmsg, the runtime before this change (averaged over multiple runs, very stable times however): Kernel Time Diff ==================================== -git 4720 usec -git+commit 4311 usec -8.7% and looking at a profile diff, we see the following: 0.25% +9.33% [kernel.kallsyms] [k] _copy_from_user 4.47% -3.32% [kernel.kallsyms] [k] __io_msg_copy_hdr.constprop.0 where we drop more than 9% of _copy_from_user() time, and consequently add time to __io_msg_copy_hdr() where the copies are now attributed to, but with a net win of 6%. In comparison, the same test case with send/recv runs in 3745 usec, which is (expectedly) still quite a bit faster. But at least sendmsg/recvmsg is now only ~13% slower, where it was ~21% slower before. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c559780 commit 792060d

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

io_uring/net.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,42 @@ static int io_msg_copy_hdr(struct io_kiocb *req, struct io_async_msghdr *iomsg,
255255
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
256256
int ret;
257257

258-
if (copy_from_user(msg, sr->umsg, sizeof(*sr->umsg)))
258+
if (!user_access_begin(sr->umsg, sizeof(*sr->umsg)))
259259
return -EFAULT;
260260

261+
ret = -EFAULT;
262+
unsafe_get_user(msg->msg_name, &sr->umsg->msg_name, ua_end);
263+
unsafe_get_user(msg->msg_namelen, &sr->umsg->msg_namelen, ua_end);
264+
unsafe_get_user(msg->msg_iov, &sr->umsg->msg_iov, ua_end);
265+
unsafe_get_user(msg->msg_iovlen, &sr->umsg->msg_iovlen, ua_end);
266+
unsafe_get_user(msg->msg_control, &sr->umsg->msg_control, ua_end);
267+
unsafe_get_user(msg->msg_controllen, &sr->umsg->msg_controllen, ua_end);
268+
msg->msg_flags = 0;
269+
261270
if (req->flags & REQ_F_BUFFER_SELECT) {
262271
if (msg->msg_iovlen == 0) {
263272
sr->len = iomsg->fast_iov[0].iov_len = 0;
264273
iomsg->fast_iov[0].iov_base = NULL;
265274
iomsg->free_iov = NULL;
266275
} else if (msg->msg_iovlen > 1) {
267-
return -EINVAL;
276+
ret = -EINVAL;
277+
goto ua_end;
268278
} else {
269-
if (copy_from_user(iomsg->fast_iov, msg->msg_iov,
270-
sizeof(*msg->msg_iov)))
271-
return -EFAULT;
279+
/* we only need the length for provided buffers */
280+
if (!access_ok(&msg->msg_iov[0].iov_len, sizeof(__kernel_size_t)))
281+
goto ua_end;
282+
unsafe_get_user(iomsg->fast_iov[0].iov_len,
283+
&msg->msg_iov[0].iov_len, ua_end);
272284
sr->len = iomsg->fast_iov[0].iov_len;
273285
iomsg->free_iov = NULL;
274286
}
275-
276-
return 0;
287+
ret = 0;
288+
ua_end:
289+
user_access_end();
290+
return ret;
277291
}
278292

293+
user_access_end();
279294
iomsg->free_iov = iomsg->fast_iov;
280295
ret = __import_iovec(ddir, msg->msg_iov, msg->msg_iovlen, UIO_FASTIOV,
281296
&iomsg->free_iov, &iomsg->msg.msg_iter, false);

0 commit comments

Comments
 (0)