Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2134352rwl; Thu, 6 Apr 2023 06:22:51 -0700 (PDT) X-Google-Smtp-Source: AKy350YTzpH4NFo4yp9ZwavZiqu4cp6Uwkw1w/+xpNcUThyEK2tSn2OMgMvRdz+zlPUSKyvSTP8R X-Received: by 2002:a05:6a20:b88:b0:d6:26a3:98d with SMTP id i8-20020a056a200b8800b000d626a3098dmr2490238pzh.46.1680787371353; Thu, 06 Apr 2023 06:22:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680787371; cv=none; d=google.com; s=arc-20160816; b=OfHbTGPDY97EtFNOamhs2jGLQpl76H/nuKQ3yvDTv9b2Py61t6zh22Qw1W6D9bTUKZ l/Vtf0aS7IFLbZLnHYPQoowW7AbD0JgMmIkSHJ3wTVo/b3VVbAUOgVDqvRQOiwZYwwMU dXppu2um5ACnU8adzWfES9KBzGDVdLaCT64kNGQnbe/NFo0b/P69QouPK3ZmNig/THOO likoWXmCM2GxWviUSwHVYuwuWBvPOK5xrVWiQF/WnNpYNQ/blgqXCO28OafZEVdxfBKR a6qEz4bcaAHlk8wBUZgcO16VnmCB5lxuRWlJ7phz3yKNSkiszelnXSj2UaAqT/aDP7EV nfRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oqZbUgv+5sJFBzHrCS1hTYdTwSuWj9s9ZIzsmU8yC2w=; b=oEvEPN/XRghjiERkONj8byHxqqOiWSTkiNwckQpMtoqYVGe0gYKsVLk+PtWaCazH3P 6hjVxq91Q57Cqr/p82K5GAekmqKxwgVW/l2YbPZWZXZTVpxxVeLo9vS9vtSHxAOXmGKO bpsPMaBg1oRAsjMJ2xrmjjx2xzymtsuZanZ9ZcpYnTsgBFTXbGZNKfVZc57djvxo5yAl z2YJquahCbz7rrLZvf4vkwfzf8i/Ac1u6C3Mb/9ljRJAgFB26W7I/+awwkyAfK+WEi2f dXgMwCTb8Nnb3q5FchheVO+bh49q0B70aREQBEm1p3UmBihgPuqrWg3MWO7wTqazCVvV MndQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=maOYQjQq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k28-20020aa79d1c000000b005f681d0192csi1454704pfp.27.2023.04.06.06.22.38; Thu, 06 Apr 2023 06:22:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=maOYQjQq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238571AbjDFNV1 (ORCPT + 99 others); Thu, 6 Apr 2023 09:21:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238213AbjDFNU7 (ORCPT ); Thu, 6 Apr 2023 09:20:59 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C290A24A; Thu, 6 Apr 2023 06:20:29 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-50263dfe37dso6341573a12.0; Thu, 06 Apr 2023 06:20:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680787227; 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=oqZbUgv+5sJFBzHrCS1hTYdTwSuWj9s9ZIzsmU8yC2w=; b=maOYQjQq1ZPe9acCFlODIHwUftIv67eW0Nk33eytV7fht+mIS6IOChb1Fpyyf+ladC /jzbQ1Uh7XIyb3FhHZN1Y3USpm4DFrMrP2/EMOyMOTG4f7J+6aB517MwlQvVUaQctn5N Wg2z/ofeOItPpQtCPxHv3ZBywLDnNr8qYlBl8IQE9Dmu2rtzjCmsct/cUcKRMNNIiPwY EcdjaphACQqnpXYQ1llqNRibnTn9t+cVpBPAPNd9Y2/oDXDqe+IeAQHHVntwt6tkoE5G IBdJ/xlK0gqFMwYJb7sQfdvS8ddABQ6+/Q7ng7HWI+ACFWT/YezGm0VDNDKgRgdq7KlJ b71Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680787227; 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=oqZbUgv+5sJFBzHrCS1hTYdTwSuWj9s9ZIzsmU8yC2w=; b=VO01mcZ+KeCbMJ7VjSdCAWCGyb0ppVnkcPuTtmESna9RNLosurPiz9cSwr3ujMHNiZ gygpbuaTZSIU4jtOMjbGsMdyu50gZjBNeRe9gsUaGc9woFFtZn4WKPNnQUrKoeWVTkmM nMjCdMQ8NhaQYrWwNMhzbd/uQjAf0oHOMJmxKhQ0myk9FrkO+iucUkkiEX90zuMd0kjR 47IMjAPg6R4Q9Jd62aGlxkKeycQOCKxQtPxlXszfDKij2ME4arIGdwsTnAJuGU+mU7x+ pYHYtLpr/wJ4bG/dg2tgDHi4VwqXetUQfNVlkvqGcE244Ogznz92NElDMnkQXkvaoyVn 5tpw== X-Gm-Message-State: AAQBX9cGUqo2efz0f2BRGVYmvfvHgFURddHR92JZubupCuYLlwKe3j4R 63utAEz9TJ81W+MWfDgSufhXa0BLccE= X-Received: by 2002:aa7:c0d1:0:b0:501:cf67:97fc with SMTP id j17-20020aa7c0d1000000b00501cf6797fcmr5477693edp.10.1680787227544; Thu, 06 Apr 2023 06:20:27 -0700 (PDT) Received: from 127.com ([2620:10d:c092:600::2:a638]) by smtp.gmail.com with ESMTPSA id m20-20020a509994000000b0050470aa444fsm312732edb.51.2023.04.06.06.20.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 06:20:27 -0700 (PDT) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: Jens Axboe , asml.silence@gmail.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 8/8] io_uring: optimise io_req_local_work_add Date: Thu, 6 Apr 2023 14:20:14 +0100 Message-Id: X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Chains of memory accesses are never good for performance. The req->task->io_uring->in_cancel in io_req_local_work_add() is there so that when a task is exiting via io_uring_try_cancel_requests() and starts waiting for completions it gets woken up by every new task_work item queued. Do a little trick by announcing waiting in io_uring_try_cancel_requests() making io_req_local_work_add() to wake us up. We also need to check for deferred tw items after prepare_to_wait(TASK_INTERRUPTIBLE); Signed-off-by: Pavel Begunkov --- io_uring/io_uring.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 0ea50c46f27f..9bbf58297a0e 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -1335,10 +1335,6 @@ static void io_req_local_work_add(struct io_kiocb *req, unsigned flags) &req->io_task_work.node)); if (!first) { - if (unlikely(atomic_read(&req->task->io_uring->in_cancel))) { - io_move_task_work_from_local(ctx); - return; - } if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); if (ctx->has_evfd) @@ -3205,6 +3201,12 @@ static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, enum io_wq_cancel cret; bool ret = false; + /* set it so io_req_local_work_add() would wake us up */ + if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { + atomic_set(&ctx->cq_wait_nr, 1); + smp_mb(); + } + /* failed during ring init, it couldn't have issued any requests */ if (!ctx->rings) return false; @@ -3259,6 +3261,8 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) { struct io_uring_task *tctx = current->io_uring; struct io_ring_ctx *ctx; + struct io_tctx_node *node; + unsigned long index; s64 inflight; DEFINE_WAIT(wait); @@ -3280,9 +3284,6 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) break; if (!sqd) { - struct io_tctx_node *node; - unsigned long index; - xa_for_each(&tctx->xa, index, node) { /* sqpoll task will cancel all its requests */ if (node->ctx->sq_data) @@ -3305,7 +3306,13 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); io_run_task_work(); io_uring_drop_tctx_refs(current); - + xa_for_each(&tctx->xa, index, node) { + if (!llist_empty(&node->ctx->work_llist)) { + WARN_ON_ONCE(node->ctx->submitter_task && + node->ctx->submitter_task != current); + goto end_wait; + } + } /* * If we've seen completions, retry without waiting. This * avoids a race where a completion comes in before we did @@ -3313,6 +3320,7 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) */ if (inflight == tctx_inflight(tctx, !cancel_all)) schedule(); +end_wait: finish_wait(&tctx->wait, &wait); } while (1); -- 2.40.0