Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp2965651ybj; Mon, 23 Sep 2019 12:18:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqyha7XcsrYJ4b4UXkbUqxhZpZYul12rhrS/uWSnVHnS306GAVJ5EASCVyjJxYKRcfGYH1Il X-Received: by 2002:a17:906:d050:: with SMTP id bo16mr1307910ejb.146.1569266335695; Mon, 23 Sep 2019 12:18:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569266335; cv=none; d=google.com; s=arc-20160816; b=B4x9cEVIFshKVczGuq4oJ76yhW1XnMNGlI3isN4YzORLQHC+mxqDHoQm6oGQNY4Ouf rYN0oKt7e6hdnENkYO1Oqr6Tzasf+8oLtKpT6saC1RW2XtQO9Muekopfenq5E/HjtFLC NVfCb5uFNREqfOiGj2mP5FIAK4tWVRY7AJN4pGhtXaDtxPJFcINxgU5Ad6ANwXMYzcZu 0kUrbQia48vKoaAlgJDHzeeHpbdwm24cOXyyNPgMiYaYBCpR6ib8hahBnnpnl5FwAPOL Af4KaN99xbsJyIQDjFpMoKSNPekMZ+MF0Y3OZ+ao416ZUAh47qtOo7ahbcBQhBM5ilu9 QgGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CD5dbN6dWmUlIx4cKSdgxz+b1tGDJ8hFL6z+Q546CZk=; b=Ku0vyCzQGP3gklwul1EUDtiwS1ZYrELsN8rgBAAsL8UAwoKwzjjq7MUSiIgiYmOhx2 YZ6+UDFTDSy7sHxx21mSHdNB0lFAD3ANAdaKwF9bVkR+hwojeOqZNT1tbYB2+Wuy8TAN 3VkEQMH5kXIddywvbWlzOiSP99L6cpOKTEyn0UWKeuxTxcr8R9COsRcJQRD2gcGlBhp0 HYqJvO3TNERdbTtZqTLWbmtY3SbF9vYAqX4mIDRbSTwTDdexM7K3MhVKcWU3fp0e6bh7 9Xhr2FWpoe6cHXMWBnjUPSaKmoPyUb021yPSkF1ghJBfoSsD7JQxSUkhAZR2kwSpfdf5 3PIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=E8AgvgEx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x62si7521486ede.352.2019.09.23.12.18.31; Mon, 23 Sep 2019 12:18:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=E8AgvgEx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727848AbfIVIJW (ORCPT + 99 others); Sun, 22 Sep 2019 04:09:22 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:35126 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727741AbfIVIJW (ORCPT ); Sun, 22 Sep 2019 04:09:22 -0400 Received: by mail-wm1-f68.google.com with SMTP id y21so6055824wmi.0; Sun, 22 Sep 2019 01:09:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CD5dbN6dWmUlIx4cKSdgxz+b1tGDJ8hFL6z+Q546CZk=; b=E8AgvgExUI3gKjb+333Cd12keB8bc0WQFm2GpsOIlfUr7WYthQc8alEPksUTgL+n49 keuqFaF5/5HPCsxXo+UtFlIijmQ4dK9LJ4RdtGKf4JG7JN/z95IvlHNOq+npitQx+wvr YKq+oPNLD+tMkML8nh/tNDZQhq1tVD5YCaRz/LLbqdGxvG/20yagbTWn/3CxPzx1dzYX KgvCUw0vn5Ia0QTkpHtz2tyuBz3Z0VQ0L3N0L9O95gbwGFYnQYYhCiQAvs2kW4reVqMQ gez8w4veI56zulgySmi4zvpu7dvFmeU3wsr2kL5UzBDjacTuXGtPmh89xH9npx+NqyFB 8q3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CD5dbN6dWmUlIx4cKSdgxz+b1tGDJ8hFL6z+Q546CZk=; b=Jc5e+5/3QwM5pWiWF4kI8e1yAynXiAZxEohvGJEKKe+Ocm4QiqDRJo6Y6kzJCEyt5Y lXp41tw9QrE/9kLGlrknGSQ0KTLyToWR/P3N8yY4E+EgKBj7XE10sohcGpS1gTvy8zzE eqU5uozMY9lFt6ahJZdZrVXHz+6jqabJFTNLGXwrRyMvQAnXGY9n+uO2/EphSZyPvkZO Jq9SDj6OgQ46whsUWQWLpugKqUF1Z54eUq+tofvQkG9dG6yEq8jdtk9/97fWT6fgdYes 4P/dvEOhBjxdnZ17J6nApO4XOartqBUhKkoK7vDWSJNShYXS4YZwpkMX/C4o14EQ5LLl RmtA== X-Gm-Message-State: APjAAAUD3mGUBEfMa2X+bXJzTWne3JwwLcGQ/52k9on7tp5gT6WRKm7b nZVnPTWrmR2/Ikv2vPZbczk= X-Received: by 2002:a7b:c74a:: with SMTP id w10mr9564923wmk.30.1569139757683; Sun, 22 Sep 2019 01:09:17 -0700 (PDT) Received: from localhost.localdomain ([109.126.147.119]) by smtp.gmail.com with ESMTPSA id x5sm7726983wrt.75.2019.09.22.01.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Sep 2019 01:09:17 -0700 (PDT) From: "Pavel Begunkov (Silence)" To: Jens Axboe , Ingo Molnar , Peter Zijlstra , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Pavel Begunkov Subject: [PATCH v2 2/2] io_uring: Optimise cq waiting with wait_threshold Date: Sun, 22 Sep 2019 11:08:51 +0300 Message-Id: <321aa8db2bbefb8f4b41d7b2608f629fbd5d3d55.1569139018.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Begunkov While waiting for completion events in io_cqring_wait(), the process will be waken up inside wait_threshold_interruptible() on any request completion, check num of events in completion queue and potentially go to sleep again. Apparently, there could be a lot of such spurious wakeups with lots of overhead. It especially manifests itself, when min_events is large, and completions are arriving one by one or in small batches (that usually is true). E.g. if device completes requests one by one and io_uring_enter is waiting for 100 events, then there will be ~99 spurious wakeups. Use new wait_threshold_*() instead, which won't wake it up until necessary number of events is collected. Performance test: The first thread generates requests (QD=512) one by one, so they will be completed in the similar pattern. The second thread waiting for 128 events to complete. Tested with null_blk with 5us delay and 3.8GHz Intel CPU. throughput before: 270 KIOPS throughput after: 370 KIOPS ~40% throughput boost, exaggerated, but makes a point. v2: wake always in io_timeout_fn() with WQ_THRESHOLD_WAKE_ALWAYS Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5c3f2bb81637..05f4391c7bbe 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -70,6 +70,7 @@ #include #include #include +#include #include @@ -414,6 +415,13 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) return ctx; } +static unsigned int io_cqring_events(struct io_rings *rings) +{ + /* See comment at the top of this file */ + smp_rmb(); + return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); +} + static inline bool io_sequence_defer(struct io_ring_ctx *ctx, struct io_kiocb *req) { @@ -559,16 +567,27 @@ static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, } } -static void io_cqring_ev_posted(struct io_ring_ctx *ctx) +static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, + unsigned int nr_events) { if (waitqueue_active(&ctx->wait)) - wake_up(&ctx->wait); + wake_up_threshold(&ctx->wait, nr_events); if (waitqueue_active(&ctx->sqo_wait)) wake_up(&ctx->sqo_wait); if (ctx->cq_ev_fd) eventfd_signal(ctx->cq_ev_fd, 1); } +static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) +{ + __io_cqring_ev_posted(ctx, io_cqring_events(ctx->rings)); +} + +static inline void io_cqring_timeout_posted(struct io_ring_ctx *ctx) +{ + __io_cqring_ev_posted(ctx, WQ_THRESHOLD_WAKE_ALWAYS); +} + static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data, long res) { @@ -587,7 +606,7 @@ static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs) percpu_ref_put_many(&ctx->refs, refs); if (waitqueue_active(&ctx->wait)) - wake_up(&ctx->wait); + wake_up_threshold(&ctx->wait, io_cqring_events(ctx->rings)); } static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, @@ -722,12 +741,6 @@ static void io_put_req(struct io_kiocb *req) io_free_req(req); } -static unsigned io_cqring_events(struct io_rings *rings) -{ - /* See comment at the top of this file */ - smp_rmb(); - return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); -} /* * Find and free completed poll iocbs @@ -1824,7 +1837,7 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) io_commit_cqring(ctx); spin_unlock_irqrestore(&ctx->completion_lock, flags); - io_cqring_ev_posted(ctx); + io_cqring_timeout_posted(ctx); io_put_req(req); return HRTIMER_NORESTART; @@ -2723,7 +2736,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, * we started waiting. For timeouts, we always want to return to * userspace. */ - ret = wait_event_interruptible(ctx->wait, + ret = wait_threshold_interruptible(ctx->wait, min_events, io_cqring_events(rings) >= min_events || atomic_read(&ctx->cq_timeouts) != nr_timeouts); restore_saved_sigmask_unless(ret == -ERESTARTSYS); -- 2.23.0