From patchwork Thu Jan 19 18:02:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13108464 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 F1CACC46467 for ; Thu, 19 Jan 2023 18:02:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229618AbjASSCm (ORCPT ); Thu, 19 Jan 2023 13:02:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229654AbjASSCl (ORCPT ); Thu, 19 Jan 2023 13:02:41 -0500 Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D0536E8C for ; Thu, 19 Jan 2023 10:02:34 -0800 (PST) Received: by mail-io1-xd34.google.com with SMTP id i70so1314521ioa.12 for ; Thu, 19 Jan 2023 10:02:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.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=Ff7H/3PeYDmHiRyUux/4lL6G6GX0g0GmP5JBfk3c0nE=; b=D1JDi4Bz0xr0JqrS/b8q5y5YGd/ekXo8pKyVp74cD2v5c6hPWA3ukFEAB9zCrpOzlw Ru9escdyn5K48Fu60svdG8oZtCzVyEwtaeUl0a1wnt7mXLYEvxgAG/bRcdclRhXeXnb+ BweV+EEkFLfDbieVxoCNN37tk76628gPcR9Tai3OCLXa7AprxldowaAkswtnfFI0iyVe f2Kq1ngNVnCmI0dcQHtslKGYzeW1lCjGUldlo3M20RrEpbM3UI7N2f6Lqz1ET49VjnAy o6YYdUtN6ClIF8mt9p4412XfFB+zEMB3TYrftGLrSYxFCqR2XbtmV3ONMXZWSBKKnBpO gYww== 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=Ff7H/3PeYDmHiRyUux/4lL6G6GX0g0GmP5JBfk3c0nE=; b=OZXwkiVaBfP/N1k+AuX1Tttx0RVlljyuqoK7wW/92sNQCJDTHRABhOc6tXjjul2L42 Q1r3ao8I5Sla95aQ7XAYLoIhkIQ/+ENrd/vF12Y8OdNp+tV78rcTsDP3uF3h47+RLp+e XBdGCJ2HgaBt0VXAY7YUvITRGY/m214hGLRw4dCNbS77/uVCSMPpCaXnMdBm3KW1LYsW mEs2GBXu5wLHNtEp/cZes87fVRyLZZEDCj9vfHyz8ddOyXt0kDdA0xRslfdnGuYu3+tm eJSqT3Yhu+HKSiaDt9jXy++p4IzbOdNTdGeSwcuJAnor47LiOseOCE57bctG+tKXhtRO snGQ== X-Gm-Message-State: AFqh2kpt5MwSf5L4K9pUGGO8wvR1j4FSQC/lxbGaMaKZ8rcC+BevhMl1 28aiFm6RuEIgWi49XWPd4s3yrSLXhufNzFsg X-Google-Smtp-Source: AMrXdXtsZL3q27H5uwNKpbGMxlFZ3g0TYIpoBRvli4GoLFPmfUmCDJHm/NSblZbBNXSkSK9HEveU1w== X-Received: by 2002:a5d:9e4d:0:b0:707:6808:45c0 with SMTP id i13-20020a5d9e4d000000b00707680845c0mr769304ioi.1.1674151348469; Thu, 19 Jan 2023 10:02:28 -0800 (PST) Received: from m1max.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id d3-20020a0566022be300b00704d1d8faecsm2354914ioy.48.2023.01.19.10.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 10:02:28 -0800 (PST) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe Subject: [PATCH 1/2] io_uring/msg_ring: move double lock/unlock helpers higher up Date: Thu, 19 Jan 2023 11:02:24 -0700 Message-Id: <20230119180225.466835-2-axboe@kernel.dk> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230119180225.466835-1-axboe@kernel.dk> References: <20230119180225.466835-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org In preparation for needing them somewhere else, move them and get rid of the unused 'issue_flags' for the unlock side. No functional changes in this patch. Signed-off-by: Jens Axboe --- io_uring/msg_ring.c | 47 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c index 2d3cd945a531..321f5eafef99 100644 --- a/io_uring/msg_ring.c +++ b/io_uring/msg_ring.c @@ -25,6 +25,28 @@ struct io_msg { u32 flags; }; +static void io_double_unlock_ctx(struct io_ring_ctx *octx) +{ + mutex_unlock(&octx->uring_lock); +} + +static int io_double_lock_ctx(struct io_ring_ctx *octx, + unsigned int issue_flags) +{ + /* + * To ensure proper ordering between the two ctxs, we can only + * attempt a trylock on the target. If that fails and we already have + * the source ctx lock, punt to io-wq. + */ + if (!(issue_flags & IO_URING_F_UNLOCKED)) { + if (!mutex_trylock(&octx->uring_lock)) + return -EAGAIN; + return 0; + } + mutex_lock(&octx->uring_lock); + return 0; +} + void io_msg_ring_cleanup(struct io_kiocb *req) { struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); @@ -77,29 +99,6 @@ static int io_msg_ring_data(struct io_kiocb *req) return -EOVERFLOW; } -static void io_double_unlock_ctx(struct io_ring_ctx *octx, - unsigned int issue_flags) -{ - mutex_unlock(&octx->uring_lock); -} - -static int io_double_lock_ctx(struct io_ring_ctx *octx, - unsigned int issue_flags) -{ - /* - * To ensure proper ordering between the two ctxs, we can only - * attempt a trylock on the target. If that fails and we already have - * the source ctx lock, punt to io-wq. - */ - if (!(issue_flags & IO_URING_F_UNLOCKED)) { - if (!mutex_trylock(&octx->uring_lock)) - return -EAGAIN; - return 0; - } - mutex_lock(&octx->uring_lock); - return 0; -} - static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags) { struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); @@ -148,7 +147,7 @@ static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flag if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) ret = -EOVERFLOW; out_unlock: - io_double_unlock_ctx(target_ctx, issue_flags); + io_double_unlock_ctx(target_ctx); return ret; } From patchwork Thu Jan 19 18:02:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13108463 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 80251C004D4 for ; Thu, 19 Jan 2023 18:02:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229887AbjASSCl (ORCPT ); Thu, 19 Jan 2023 13:02:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230024AbjASSCk (ORCPT ); Thu, 19 Jan 2023 13:02:40 -0500 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EC8A4EE3 for ; Thu, 19 Jan 2023 10:02:33 -0800 (PST) Received: by mail-io1-xd36.google.com with SMTP id p189so1358465iod.0 for ; Thu, 19 Jan 2023 10:02:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.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=zo7rwkH1ueFFKseiRYFfEN5MPxzoA/BBxczMpa9w0Y0=; b=hI5jF/b+VFhhgONYOJNZjrABvD9AEAyWZlYPVfBwLibCyH2aT43WQIWS+PA1/FQrT0 66nwCuSYXgMMDENlFGpTiz0nJXkduIS7PCcoB4J4eD+ZaNWNCvZ47bGziyriYRRFJl4Y nS/jEuJg6Gr+O5kqQEJWwg/06JaN8Wxo7E5QerOmYgJsSMxo0oSLcsKCm1EmiiLTXV0t L0LZ9npUUfCgzlSACd7MZVMiy7sp1nQTREBv5G8uGZKIVuVz5jX/EB6YcJldZuPx5hPv /sHnnh1OoXTdVhowYzD8z6U4FZiw5CP8UcmNH65/jVLPHDF4rNvWm8oF2Au0Z+eaB9es kwGw== 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=zo7rwkH1ueFFKseiRYFfEN5MPxzoA/BBxczMpa9w0Y0=; b=xKy9jvMKR219ELkMfzSv4eRra1qt6CayZY8CYgo7VU0egG+To0Avse1ATB7gD+O5pa 22c3BTiIsXZS6cOmPpqjQkzWPj5pbjxJ57ITDsxldiuTSlYbqF2oEcs5vUujD2sEPIjG NSrRWaIhdSipG5tHg1pYmd7JAOliERwEuIohxUAUxKcj+pGieaobS4mXhgIC7kKg8x0F qsK/VjgZyALgeNjDgribnCSST8/t1TuUbm5TGXPnGWxmDP9icUBbQOirXu83weJsskTQ nkUnxQDnRcI1f1wOJTm/5xJzB8H1XVzdCdjJWCTiuqqplhOkKObiTBE6YNECSnJH/1gX QIFQ== X-Gm-Message-State: AFqh2koxJ6Wh+TYlWMApR2po/t4rzIcfJtjaVxTh+dLJBlMSgE9k3+6X UoHIl2TBMAQz4jBuJXzcuZC+Xb4dBVYLtiIH X-Google-Smtp-Source: AMrXdXtzXy9XT++h5uDYKb6y0c2OpDXb6GLURjwNhYfp1qwV6Z9xDhSeYTqID6BZPVufVNF9YBQ65g== X-Received: by 2002:a6b:4f10:0:b0:6ed:95f:92e7 with SMTP id d16-20020a6b4f10000000b006ed095f92e7mr1878438iob.0.1674151352907; Thu, 19 Jan 2023 10:02:32 -0800 (PST) Received: from m1max.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id d3-20020a0566022be300b00704d1d8faecsm2354914ioy.48.2023.01.19.10.02.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 10:02:28 -0800 (PST) From: Jens Axboe To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, Jens Axboe , Xingyuan Mo Subject: [PATCH 2/2] io_uring/msg_ring: fix missing lock on overflow for IOPOLL Date: Thu, 19 Jan 2023 11:02:25 -0700 Message-Id: <20230119180225.466835-3-axboe@kernel.dk> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230119180225.466835-1-axboe@kernel.dk> References: <20230119180225.466835-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org If the target ring is configured with IOPOLL, then we always need to hold the target ring uring_lock before posting CQEs. We could just grab it unconditionally, but since we don't expect many target rings to be of this type, make grabbing the uring_lock conditional on the ring type. Link: https://lore.kernel.org/io-uring/Y8krlYa52%2F0YGqkg@ip-172-31-85-199.ec2.internal/ Reported-by: Xingyuan Mo Signed-off-by: Jens Axboe --- io_uring/msg_ring.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/io_uring/msg_ring.c b/io_uring/msg_ring.c index 321f5eafef99..a333781565d3 100644 --- a/io_uring/msg_ring.c +++ b/io_uring/msg_ring.c @@ -65,20 +65,33 @@ static void io_msg_tw_complete(struct callback_head *head) struct io_ring_ctx *target_ctx = req->file->private_data; int ret = 0; - if (current->flags & PF_EXITING) + if (current->flags & PF_EXITING) { ret = -EOWNERDEAD; - else if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) - ret = -EOVERFLOW; + } else { + /* + * If the target ring is using IOPOLL mode, then we need to be + * holding the uring_lock for posting completions. Other ring + * types rely on the regular completion locking, which is + * handled while posting. + */ + if (target_ctx->flags & IORING_SETUP_IOPOLL) + mutex_lock(&target_ctx->uring_lock); + if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) + ret = -EOVERFLOW; + if (target_ctx->flags & IORING_SETUP_IOPOLL) + mutex_unlock(&target_ctx->uring_lock); + } if (ret < 0) req_set_fail(req); io_req_queue_tw_complete(req, ret); } -static int io_msg_ring_data(struct io_kiocb *req) +static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags) { struct io_ring_ctx *target_ctx = req->file->private_data; struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); + int ret; if (msg->src_fd || msg->dst_fd || msg->flags) return -EINVAL; @@ -93,10 +106,18 @@ static int io_msg_ring_data(struct io_kiocb *req) return IOU_ISSUE_SKIP_COMPLETE; } - if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) - return 0; - - return -EOVERFLOW; + ret = -EOVERFLOW; + if (target_ctx->flags & IORING_SETUP_IOPOLL) { + if (unlikely(io_double_lock_ctx(target_ctx, issue_flags))) + return -EAGAIN; + if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) + ret = 0; + io_double_unlock_ctx(target_ctx); + } else { + if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) + ret = 0; + } + return ret; } static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags) @@ -223,7 +244,7 @@ int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) switch (msg->cmd) { case IORING_MSG_DATA: - ret = io_msg_ring_data(req); + ret = io_msg_ring_data(req, issue_flags); break; case IORING_MSG_SEND_FD: ret = io_msg_send_fd(req, issue_flags);