From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) (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 4AD35367F3D for ; Wed, 4 Mar 2026 16:29:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772641760; cv=none; b=nL9vWzIjciKyqrBhcfNTt9aO+jnDi2SBYAy+4bEbcTkAJb8/LkkiAjs+fl4133vxyo1/asuFEeJxLhN68FO3hyk064mF7XwMA8ulFjnM8Cm1GLhv6inzN6kt9peYOP2TtSMwx5rO0WeSk55Q8SJL0LSmWYFcxI3d7A3D2N8Kjg0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772641760; c=relaxed/simple; bh=ViIB3ZUka+cm+G4iKklUTITvb7PG+0M2Kii8KWLAzVw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=pZGrFbrG2wd2GeaSTL0hd4Cv1rP58omDlbqCsQWN+NnduMQtDQs2N08/Oh8GUKORIMcT9BtlqCS5xqEja9u1xh1fGf4Op2D7W7wAXM7/8FnFBQBJ+6zpZFXLaCVirFrbWI024DARVfh3f9IIS3bacMrsLuUWl5NXJw1/CQZrCIY= 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=Aon4QGlN; arc=none smtp.client-ip=209.85.160.45 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="Aon4QGlN" Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-4138136f02eso1706339fac.2 for ; Wed, 04 Mar 2026 08:29:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1772641756; x=1773246556; 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=P/5lhk6C8jE/PS2fAtX4oJhx14kIa3UNBGxkwqnfz2E=; b=Aon4QGlNcdV7jt762eL0TpKisfaeXyVLFH82ns+6auDeoj1os9fzW/dM035AgrIW4E lskgt4U0BVTxXAvPj0UJrdFYnCRej8zw2i9AD1vmEn/3O4CipEka5a/2/hWNGnTYiTmD cbm+1VtUcXzfxueob2a3eI92GevhOhffHHfplwXbzZD4K8BuQ+xSftH0wizKkIbbWRYR nRyn1/aK2ir48umzQbp3uqPtjS8BiBR8faiQTqiHqqwFlhlm2Onem0jO7FEOX7Fc9DLE 1g2ENP2CTf/sm75VANWVCDqOCU+lyNOaninbfYWotIgAXDn4NhavwHnWHSv0E/4f6qxl nFPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772641756; x=1773246556; 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=P/5lhk6C8jE/PS2fAtX4oJhx14kIa3UNBGxkwqnfz2E=; b=uyUlKCid/V2Zq3xuv13CeuWieNIN+UTTbCLGQI8h6U6aeQ56nd0fQKjIHQT/0EvZSD cL7F2b8GSohvIf0oi0jhIs9vD98pD6QDbyyloWeN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abc with SMTP id 586e51a60fabf-416ab5fd865mr1373419fac.20.1772641756143; Wed, 04 Mar 2026 08:29:16 -0800 (PST) Received: from [192.168.1.150] ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4160cf9b24dsm18350522fac.7.2026.03.04.08.29.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Mar 2026 08:29:15 -0800 (PST) Message-ID: Date: Wed, 4 Mar 2026 09:29:14 -0700 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: Caleb Sander Mateos , Ming Lei 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/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. -- Jens Axboe