From patchwork Fri Jan 20 16:20:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13110154 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C5BEC27C7C for ; Fri, 20 Jan 2023 16:22:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229847AbjATQWT (ORCPT ); Fri, 20 Jan 2023 11:22:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbjATQWS (ORCPT ); Fri, 20 Jan 2023 11:22:18 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9824E56487 for ; Fri, 20 Jan 2023 08:22:13 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id mp20so15213644ejc.7 for ; Fri, 20 Jan 2023 08:22:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kNXtuGs/ChtdEeAKTWqgirpCrA/uohpZGq3JkDE5JVo=; b=QEhP2CwCpfdThhnz+396awlWOnS4yOuvezx3naUmfbIGQTK3V8Ta5779GTmUHXleZF VgqchxAaImFMo+UZM6QQ6EFkuZeZmvxXE2tlASc1JRbEFA4LGxEe/ZD+yBPdzDQOjtBj Yhb7euVGDg1ZvMA7VbT2BTqYe7ehlaqu5kwc4tDR0ra5uABQOT2uqC8WnJ+YJcYYtm3W WYG0YVTGy5KIfpoktcRWYM7eqeZvh0o0lzmcQpDJgEwcswGLBMZ5KSdbgwFS2yKt0CUo 2U8XVtEKvnYz97lFJIdLViFy/8wXw6LpUHhiDoyoQ4wZtcvOH/b1pYrYHw1W7i9oqOii eBQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kNXtuGs/ChtdEeAKTWqgirpCrA/uohpZGq3JkDE5JVo=; b=k3CX2bdiO2J62WbQ1fGZLGI3xHS/4D+y1L78IhuFI1TLr5IeO35VLwqMIgciemkeod iDORQSOLN+piUCqN0VKer5Rf43nRu2rpuohGeeKHvHZw64gnXPl/rwFb7bsEd72sqFAu OLJYl7igWZAt4pqj/K07jzePvnGpBVv2LMv/2tjdhGcpyxs8eIAW3NHt/Zrxpu6Gvaxi fRJ7HZsSxQEphkmpw3vO9LS3kOPYiHf+GMd60nfLJr33rmoDYqd1VleRDZHdKTjksHin Xi1N2rrqpHblf4xy0N1mdqQt5QqnKoBlwKHmJOhexXP276BwzmM1HUWaNtc22ObgUJqH UG/A== X-Gm-Message-State: AFqh2korKVn2cEJaqifLw3h1TZA8S85AdZnh9GZ25CPP8W6IXeqKKWJ5 P/xnRP8SJX9PKoPAVcsVS2NBuXsASOk= X-Google-Smtp-Source: AMrXdXvwmBRAk1DOu45sr8hEeh5EIEVkB6TByM9SV3CCs7gFTLFEuO+GofDipGn4xx5fvHtGcVD63Q== X-Received: by 2002:a17:907:a07b:b0:7c1:3f04:efa2 with SMTP id ia27-20020a170907a07b00b007c13f04efa2mr28555624ejc.29.1674231731878; Fri, 20 Jan 2023 08:22:11 -0800 (PST) Received: from 127.com ([2620:10d:c092:600::2:4670]) by smtp.gmail.com with ESMTPSA id s12-20020a1709060c0c00b0086621d9d9b0sm11406040ejf.81.2023.01.20.08.22.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 08:22:11 -0800 (PST) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com Subject: [PATCH for-6.2 1/3] io_uring/msg_ring: fix flagging remote execution Date: Fri, 20 Jan 2023 16:20:59 +0000 Message-Id: X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org There is a couple of problems with queueing a tw in io_msg_ring_data() for remote execution. First, once we queue it the target ring can go away and so setting IORING_SQ_TASKRUN there is not safe. Secondly, the userspace might not expect IORING_SQ_TASKRUN. Extract a helper and uniformly use TWA_SIGNAL without TWA_SIGNAL_NO_IPI tricks for now, just as it was done in the original patch. Fixes: 6d043ee1164ca ("io_uring: do msg_ring in target task via tw") Signed-off-by: Pavel Begunkov --- io_uring/msg_ring.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c index a333781565d3..bb868447dcdf 100644 --- a/io_uring/msg_ring.c +++ b/io_uring/msg_ring.c @@ -58,6 +58,25 @@ void io_msg_ring_cleanup(struct io_kiocb *req) msg->src_file = NULL; } +static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx) +{ + if (!target_ctx->task_complete) + return false; + return current != target_ctx->submitter_task; +} + +static int io_msg_exec_remote(struct io_kiocb *req, task_work_func_t func) +{ + struct io_ring_ctx *ctx = req->file->private_data; + struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); + + init_task_work(&msg->tw, func); + if (task_work_add(ctx->submitter_task, &msg->tw, TWA_SIGNAL)) + return -EOWNERDEAD; + + return IOU_ISSUE_SKIP_COMPLETE; +} + static void io_msg_tw_complete(struct callback_head *head) { struct io_msg *msg = container_of(head, struct io_msg, tw); @@ -96,15 +115,8 @@ static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags) if (msg->src_fd || msg->dst_fd || msg->flags) return -EINVAL; - if (target_ctx->task_complete && current != target_ctx->submitter_task) { - init_task_work(&msg->tw, io_msg_tw_complete); - if (task_work_add(target_ctx->submitter_task, &msg->tw, - TWA_SIGNAL_NO_IPI)) - return -EOWNERDEAD; - - atomic_or(IORING_SQ_TASKRUN, &target_ctx->rings->sq_flags); - return IOU_ISSUE_SKIP_COMPLETE; - } + if (io_msg_need_remote(target_ctx)) + return io_msg_exec_remote(req, io_msg_tw_complete); ret = -EOVERFLOW; if (target_ctx->flags & IORING_SETUP_IOPOLL) { @@ -202,14 +214,8 @@ static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags) req->flags |= REQ_F_NEED_CLEANUP; } - if (target_ctx->task_complete && current != target_ctx->submitter_task) { - init_task_work(&msg->tw, io_msg_tw_fd_complete); - if (task_work_add(target_ctx->submitter_task, &msg->tw, - TWA_SIGNAL)) - return -EOWNERDEAD; - - return IOU_ISSUE_SKIP_COMPLETE; - } + if (io_msg_need_remote(target_ctx)) + return io_msg_exec_remote(req, io_msg_tw_fd_complete); return io_msg_install_complete(req, issue_flags); } From patchwork Fri Jan 20 16:21:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13110155 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5DF2C25B4E for ; Fri, 20 Jan 2023 16:22:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229834AbjATQWU (ORCPT ); Fri, 20 Jan 2023 11:22:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229936AbjATQWT (ORCPT ); Fri, 20 Jan 2023 11:22:19 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F3F45648C for ; Fri, 20 Jan 2023 08:22:14 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id bk15so15202905ejb.9 for ; Fri, 20 Jan 2023 08:22:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lL/MFQt8b8dhap63osJLhrmA3bFPT55Fdb32RgCKHtM=; b=XpnNg3W57ntM/qtWoQLEGLuGEEe8oycuXUmfYOlzQkukfykPLHLF/S+J0b28/+jH7+ rbNbZU/pyY4VyoNxrSeyvQGt6nm11eCYKcR2km2Xj9Zl4VAWugn8zleeSZBsbgt5swxr Mqz8Y4ApG2XaZjbJjKOHrxrfnzInj+AB0St1asGi8LqV+E+sO1CQjODrZS8l9CB+VmP3 enAtzYtVbtCiQ/nPIPoJB+KJi6rm8DDczQXw57wk0LD1BIcIk8D3LcPRF9FuVgAv/los ZPugvTrTSj7sFxJyItp+mPf21l/V+Brst/xa0DydhuBWv/H9FiQ+94IWgKJUU241wBOO 3+IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lL/MFQt8b8dhap63osJLhrmA3bFPT55Fdb32RgCKHtM=; b=AA6hYSGzEyQUC4ojkfxGf3k9rasZov8SwFqnMli8H9cZWGEr9AoHr4+trdxx/C0mnB RBYk593G0Vwhw1OBOElUPU8l1uVzXgTjBCRbNupsMwlNeh1EIrDTdc2IBsAmZx/nMGMy kINCgq4umqBQpDDac2sP6ByGK54F6Ts3JqsQXqdWsskAB9qyG7553/03ebGqMahG2RaJ Y5AHXvou5B9Xaizf/d00P7fSXuf18Ko36iwlgFEc0PXnbYqlv8yQh13gtTvXsLhhmt0m XtKy2S+5m6GYNYJRJKA/xda6A24MKU6oeKsrXnpl0hZ7zy6w+EVCRge8QlApG7w7WYgW UiCQ== X-Gm-Message-State: AFqh2kpCFBK3UkSskLz699kGVtaZ0z6Kq8fVjqWS3AfOpOpp9d8mnMoy OP0iLspkrIRnHmKQQR9snJQW9lp5XpI= X-Google-Smtp-Source: AMrXdXv87X56bSi2Zj3DJw9LKtfjL8u2i44TFQjDuwQcfUA7A0uJSdETQZrNPFKe8tJH7Gyw2JhLKQ== X-Received: by 2002:a17:907:d406:b0:846:8c9a:68a0 with SMTP id vi6-20020a170907d40600b008468c9a68a0mr18339938ejc.30.1674231732818; Fri, 20 Jan 2023 08:22:12 -0800 (PST) Received: from 127.com ([2620:10d:c092:600::2:4670]) by smtp.gmail.com with ESMTPSA id s12-20020a1709060c0c00b0086621d9d9b0sm11406040ejf.81.2023.01.20.08.22.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 08:22:12 -0800 (PST) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com Subject: [PATCH for-6.2 2/3] io_uring/msg_ring: fix remote queue to disabled ring Date: Fri, 20 Jan 2023 16:21:00 +0000 Message-Id: <845f25277fd30f80ecff4a1352bb10739f300b28.1674231554.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org IORING_SETUP_R_DISABLED rings don't have the submitter task set, so it's not always safe to use ->submitter_task and we have to check if it has already been set. Fixes: 6d043ee1164ca ("io_uring: do msg_ring in target task via tw") Signed-off-by: Pavel Begunkov --- io_uring/io_uring.c | 4 ++-- io_uring/msg_ring.c | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 2ac1cd8d23ea..0a4efada9b3c 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3674,7 +3674,7 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, if (ctx->flags & IORING_SETUP_SINGLE_ISSUER && !(ctx->flags & IORING_SETUP_R_DISABLED)) - ctx->submitter_task = get_task_struct(current); + WRITE_ONCE(ctx->submitter_task, get_task_struct(current)); file = io_uring_get_file(ctx); if (IS_ERR(file)) { @@ -3868,7 +3868,7 @@ static int io_register_enable_rings(struct io_ring_ctx *ctx) return -EBADFD; if (ctx->flags & IORING_SETUP_SINGLE_ISSUER && !ctx->submitter_task) - ctx->submitter_task = get_task_struct(current); + WRITE_ONCE(ctx->submitter_task, get_task_struct(current)); if (ctx->restrictions.registered) ctx->restricted = 1; diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c index bb868447dcdf..c68cd3898035 100644 --- a/io_uring/msg_ring.c +++ b/io_uring/msg_ring.c @@ -69,6 +69,10 @@ static int io_msg_exec_remote(struct io_kiocb *req, task_work_func_t func) { struct io_ring_ctx *ctx = req->file->private_data; struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); + struct task_struct *task = READ_ONCE(ctx->submitter_task); + + if (unlikely(!task)) + return -EOWNERDEAD; init_task_work(&msg->tw, func); if (task_work_add(ctx->submitter_task, &msg->tw, TWA_SIGNAL)) From patchwork Fri Jan 20 16:21:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 13110156 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33743C38141 for ; Fri, 20 Jan 2023 16:22:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229475AbjATQWV (ORCPT ); Fri, 20 Jan 2023 11:22:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbjATQWT (ORCPT ); Fri, 20 Jan 2023 11:22:19 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20C75E3B0 for ; Fri, 20 Jan 2023 08:22:15 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id vw16so15189825ejc.12 for ; Fri, 20 Jan 2023 08:22:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VgfhYJXc6p1/AlgNYL1c0MKlj8csbE9UKF7fOgnyQxk=; b=lPB82oHib2j0TdeQIhpB15CFCYNCcmCe77NzlClq5GwU1DMtOGYg8C9FRAYJRGlqpv RCFDRXuf/TJdQuV8I3+O171ixMC/b25UwOZ75ZRi2mEtG/7yL4fdoiLWGpQ72u2oYQqW JTUNdMISRd/JRIya/iIVXsFLcilqmekVMytR+W7zEPPSlslPs+gDCEsLEsa7QOzVf5U1 VxA9hr847OX2CcA93Ysoj1ReEffiaD+A5bhaSH38iLk14vrLNdn2exrpicsUL0bQ6RQP urdcftECn5JJ7Et82mZqZ8P3xYnC3hdrqipxxsQHzQ3S2qAt0Vk1mUj3zE3X8ZLdf3kd /tuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VgfhYJXc6p1/AlgNYL1c0MKlj8csbE9UKF7fOgnyQxk=; b=ycJz0KaYZvAhfuVSdi5wwc8UTzgygtNyIpUc9rhbgwn4znGKBXmyUyTkawBXj8u7Pe PU/U2xIy2qBv5u16uWyKpWTOtuLy8O7xhZHx+Z2+4RM74JeEP3ymHInVIHSiT43CdShR MmJN8uEniQ9/XFCHKjKLg8VhO77iNX+GUNu9e3mgV2cJ+gUKwJVKT+bfxL/KewaPLbwm M09ZTCfM1q+WhRq/aoCnPBnMxGK8EQIOkx+k2z9XKBGc2gSEs9iQJLUC7rHGLkZpReFd ChJKDVHObllB/b9eLFitqAcSE8gP4QBaUz7Hgsi2ulkihWG/Dcy08GPFkO3td0Kc3Lie 7Pig== X-Gm-Message-State: AFqh2kpMYTQfTrZIKkiQ+0nG/A9WMh9YRpdWG++IwruxExz69ZnKXGn5 jLvvrrqQzeR4O+BYUtiYeN+3Ft6Mt28= X-Google-Smtp-Source: AMrXdXtTmLD82l4TOReZB+9E6HCHYq6rKwhE1YShP3ayB7jAO/jQoomBn4sm1tU9wYwhkCJyKlN/ng== X-Received: by 2002:a17:907:3e18:b0:84d:3403:f4f2 with SMTP id hp24-20020a1709073e1800b0084d3403f4f2mr21279103ejc.62.1674231733576; Fri, 20 Jan 2023 08:22:13 -0800 (PST) Received: from 127.com ([2620:10d:c092:600::2:4670]) by smtp.gmail.com with ESMTPSA id s12-20020a1709060c0c00b0086621d9d9b0sm11406040ejf.81.2023.01.20.08.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 08:22:13 -0800 (PST) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com Subject: [PATCH for-6.2 3/3] io_uring/msg_ring: optimise with correct tw notify method Date: Fri, 20 Jan 2023 16:21:01 +0000 Message-Id: <205404218a6f113709407d596bc5a97ecfabedf9.1674231554.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org We may want to use TWA_SIGNAL_NO_IPI instead of TWA_SIGNAL if the target ring is configured with IORING_SETUP_COOP_TASKRUN, change io_msg_exec_remote() to use the target ring's ->notify_method. The caveat is that we have to set IORING_SQ_TASKRUN if the rings asks for it. However, once task_work_add() succeeds the target ring might go away and so we grab a ctx reference to pin the ring until we set IORING_SQ_TASKRUN. Signed-off-by: Pavel Begunkov --- io_uring/msg_ring.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c index c68cd3898035..12dc9ed3d062 100644 --- a/io_uring/msg_ring.c +++ b/io_uring/msg_ring.c @@ -70,15 +70,22 @@ static int io_msg_exec_remote(struct io_kiocb *req, task_work_func_t func) struct io_ring_ctx *ctx = req->file->private_data; struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); struct task_struct *task = READ_ONCE(ctx->submitter_task); + int ret = IOU_ISSUE_SKIP_COMPLETE; if (unlikely(!task)) return -EOWNERDEAD; + percpu_ref_get(&ctx->refs); init_task_work(&msg->tw, func); - if (task_work_add(ctx->submitter_task, &msg->tw, TWA_SIGNAL)) - return -EOWNERDEAD; - - return IOU_ISSUE_SKIP_COMPLETE; + if (task_work_add(ctx->submitter_task, &msg->tw, ctx->notify_method)) { + ret = -EOWNERDEAD; + goto out; + } + if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) + atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); +out: + percpu_ref_put(&ctx->refs); + return ret; } static void io_msg_tw_complete(struct callback_head *head)