From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C05E8364E85 for ; Mon, 16 Mar 2026 22:11:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773699087; cv=none; b=NipKep1Y0tELjRQktLi17alEmHrpuEz3inIHmu2e4Fuj8UdiOZtjiJh++mj93u2Iu0mqSvRvOTPPdvk1J9Otn4OhJeNVDeHrQIDOG0XoSGUSUI/f/kwAbr7QI139wHYK8iwo5kM7It0RSBne6m6XZTWyTxvg9tEnZNQ5QgqSpg4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773699087; c=relaxed/simple; bh=nJOilR8qSyWZZzgS1PCd/nuXq0Qh5RcuYaeD4l0HmZ8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=RWNfgU/O3TkUoy2ogQTlG7clUfMJS2V1Va0IiRNtCFjfmPvLcm65ZbltK6d4jXkb2cqwEeE7BXXRH4LbM0lv5zuxK7C1fyVqBzY4x9ptni29BdBkmJS+gICId0pZgcLoQXiGzORIBFHgNHPX5AujPnVPktNThffYGud8cF1a3ZU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b=xO0ch5ma; arc=none smtp.client-ip=209.85.160.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel-dk.20230601.gappssmtp.com header.i=@kernel-dk.20230601.gappssmtp.com header.b="xO0ch5ma" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-417c34b0509so2511775fac.1 for ; Mon, 16 Mar 2026 15:11:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1773699085; x=1774303885; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rNqYWrRFGodaMOG+fMFfMLFfzEQ/Hrz19UG5a14j8/I=; b=xO0ch5maDRLXh01I2MkihfZcE/ihTiDkSALF2duxReStIph3vv28J+VwAfWWnj0L2G wWYTBPYRPQmX3nnm0Ym65X943U06dbA4ynV4lYYrn2LZIDL9UXgr6uimHgyxF3AzN2xj SAKEFGLFc1eS83VFs7yeO+5AmHQtBnFvpls4LlciEh7Kp+FrzeQenhd70+9l7FQNDzp8 33fgEqj3PB68qZJ4+HnL44dQ3KAdyJcjN8JiKrxTKJmWsH1WEp2nnEStDvIs9ROMkYID Se+9YJYhsI0VXpMfMxZeVH9psfutZnAJUoE5xh/Dza9qQSLkXHPT/LZ3gKz4sde68U7D s0TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773699085; x=1774303885; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rNqYWrRFGodaMOG+fMFfMLFfzEQ/Hrz19UG5a14j8/I=; b=AmjR30tzgTjuof15mqg518UgGW9CDIQ157yaIAi9tIFcRSY9YdonCJSB421caDRupf mS+2wZ+JLW5qyARJ5Klv8mr1GP2DLhXjvYAWZGUQdQ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:9076 with SMTP id 586e51a60fabf-417b93c709cmr8308716fac.29.1773699084609; Mon, 16 Mar 2026 15:11:24 -0700 (PDT) Received: from [192.168.1.150] ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4177e6e82cdsm18056067fac.18.2026.03.16.15.11.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Mar 2026 15:11:24 -0700 (PDT) Message-ID: <7dfb5a9b-9af2-4463-b9a1-0eb680fd6c47@kernel.dk> Date: Mon, 16 Mar 2026 16:11:23 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 3/5] io_uring: count CQEs in io_iopoll_check() To: Ming Lei , Caleb Sander Mateos Cc: Christoph Hellwig , Keith Busch , Sagi Grimberg , io-uring@vger.kernel.org, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, Anuj Gupta , Kanchan Joshi References: <20260302172914.2488599-1-csander@purestorage.com> <20260302172914.2488599-4-csander@purestorage.com> Content-Language: en-US From: Jens Axboe In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 3/6/26 7:35 PM, Ming Lei wrote: > On Fri, Mar 06, 2026 at 05:38:15PM -0800, Caleb Sander Mateos wrote: >> On Wed, Mar 4, 2026 at 8:29?AM Jens Axboe wrote: >>> >>> On 3/4/26 8:46 AM, Caleb Sander Mateos wrote: >>>> On Wed, Mar 4, 2026 at 2:33?AM Ming Lei wrote: >>>>> >>>>> On Mon, Mar 02, 2026 at 10:29:12AM -0700, Caleb Sander Mateos wrote: >>>>>> A subsequent commit will allow uring_cmds that don't use iopoll on >>>>>> IORING_SETUP_IOPOLL io_urings. As a result, CQEs can be posted without >>>>>> setting the iopoll_completed flag for a request in iopoll_list or going >>>>>> through task work. For example, a UBLK_U_IO_FETCH_IO_CMDS command could >>>>>> call io_uring_mshot_cmd_post_cqe() to directly post a CQE. The >>>>>> io_iopoll_check() loop currently only counts completions posted in >>>>>> io_do_iopoll() when determining whether the min_events threshold has >>>>>> been met. It also exits early if there are any existing CQEs before >>>>>> polling, or if any CQEs are posted while running task work. CQEs posted >>>>>> via io_uring_mshot_cmd_post_cqe() or other mechanisms won't be counted >>>>>> against min_events. >>>>>> >>>>>> Explicitly check the available CQEs in each io_iopoll_check() loop >>>>>> iteration to account for CQEs posted in any fashion. >>>>>> >>>>>> Signed-off-by: Caleb Sander Mateos >>>>>> --- >>>>>> io_uring/io_uring.c | 9 ++------- >>>>>> 1 file changed, 2 insertions(+), 7 deletions(-) >>>>>> >>>>>> diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c >>>>>> index 46f39831d27c..b4625695bb3a 100644 >>>>>> --- a/io_uring/io_uring.c >>>>>> +++ b/io_uring/io_uring.c >>>>>> @@ -1184,11 +1184,10 @@ __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) >>>>>> io_move_task_work_from_local(ctx); >>>>>> } >>>>>> >>>>>> static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events) >>>>>> { >>>>>> - unsigned int nr_events = 0; >>>>>> unsigned long check_cq; >>>>>> >>>>>> min_events = min(min_events, ctx->cq_entries); >>>>>> >>>>>> lockdep_assert_held(&ctx->uring_lock); >>>>>> @@ -1227,34 +1226,30 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events) >>>>>> * the poll to the issued list. Otherwise we can spin here >>>>>> * forever, while the workqueue is stuck trying to acquire the >>>>>> * very same mutex. >>>>>> */ >>>>>> if (list_empty(&ctx->iopoll_list) || io_task_work_pending(ctx)) { >>>>>> - u32 tail = ctx->cached_cq_tail; >>>>>> - >>>>>> (void) io_run_local_work_locked(ctx, min_events); >>>>>> >>>>>> if (task_work_pending(current) || list_empty(&ctx->iopoll_list)) { >>>>>> mutex_unlock(&ctx->uring_lock); >>>>>> io_run_task_work(); >>>>>> mutex_lock(&ctx->uring_lock); >>>>>> } >>>>>> /* some requests don't go through iopoll_list */ >>>>>> - if (tail != ctx->cached_cq_tail || list_empty(&ctx->iopoll_list)) >>>>>> + if (list_empty(&ctx->iopoll_list)) >>>>>> break; >>>>>> } >>>>>> ret = io_do_iopoll(ctx, !min_events); >>>>>> if (unlikely(ret < 0)) >>>>>> return ret; >>>>>> >>>>>> if (task_sigpending(current)) >>>>>> return -EINTR; >>>>>> if (need_resched()) >>>>>> break; >>>>>> - >>>>>> - nr_events += ret; >>>>>> - } while (nr_events < min_events); >>>>>> + } while (io_cqring_events(ctx) < min_events); >>>>> >>>>> Before entering the loop, if io_cqring_events() finds any queued CQE, >>>>> io_iopoll_check() returns immediately without polling. >>>>> >>>>> If the queued CQE is originated from non-iopoll uring_cmd, iopoll request >>>>> will not be polled, may this be one issue? >>>> >>>> I also noticed that logic and thought it seemed odd. I would think >>>> we'd always want to wait for min_events CQEs (and iopoll once even if >>>> min_events is 0). Looks like Jens added the early return in commit >>>> a3a0e43fd770 ("io_uring: don't enter poll loop if we have CQEs >>>> pending"), perhaps he can shed some light on it? >>> >>> I don't recall the bug in question, it's been a while... But it always >>> makes sense to return events that are ready, and skip polling. It should >>> only be done if there are no ready events to reap. >> >> Ming, are you okay with preserving that behavior in this patch then? I >> guess there's a potential fairness concern where REQ_F_IOPOLL requests >> may not be polled for some time if non-REQ_F_IOPOLL requests continue >> to frequently post CQEs. > > IMO, the fairness may not a big deal given userspace should keep polling > if the iopoll IO isn't done. I think so. > But forget to mention, if non-iopoll CQE is posted and ->cq_flush becomes > true, io_submit_flush_completions() may not get chance to run in case of > the early return. > > Maybe something like below is needed: > > @@ -1556,8 +1556,10 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events) > * If we do, we can potentially be spinning for commands that > * already triggered a CQE (eg in error). > */ > - if (io_cqring_events(ctx)) > + if (io_cqring_events(ctx)) { > + io_submit_flush_completions(ctx); > return 0; > + } We should probably experiment with this a bit, I don't think it's a showstopper for merging this. Caleb, I think we can stage this for 7.1 and see how it goes. -- Jens Axboe