From nobody Thu Dec 18 14:51:16 2025 Received: from mail-yw1-f228.google.com (mail-yw1-f228.google.com [209.85.128.228]) (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 9B914224891 for ; Wed, 16 Apr 2025 19:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.228 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744832781; cv=none; b=ANZ/rY0GAKzjoEGr1eR92kvjB2NsaoOHsc35QUm1dClQhWZlwFLkaoufYe3CEo3Rk6jzBdVcyHXnlFavR6OYEd/HZgfLV+nu2eXBpFYjwzl1/s6IthxkkMmH5c3b1qOiJHSv9ugE/t81YGO1jGqbAV+agmZVyFOXO3vfcnwokz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744832781; c=relaxed/simple; bh=tYWKo49qWwobGHivnVbrXRT7o/0NtMaP2FGGsVhHUk0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BpjdMSB5AbycO2IvXDK91qyxs400BqE7hUrbm1MXaaDnftie6/HHj237jUaHZPf7tF0AqHyiQBSqpnVyGZAdBM9q8KaQiu3jAffAWCVbKuIzyFXqt3i6aHvigNrznRSX7rxs1Lcp3WbWCxsdkYcZ1OjiKmpJQ1FP+LZeX2cwtXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=P9cxdZWM; arc=none smtp.client-ip=209.85.128.228 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="P9cxdZWM" Received: by mail-yw1-f228.google.com with SMTP id 00721157ae682-702599fa7c5so8314707b3.1 for ; Wed, 16 Apr 2025 12:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1744832777; x=1745437577; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OofhF9O27cVrb8WKj94QBUeimdnvDakh4zV4gN59Auk=; b=P9cxdZWMqrsCEMoQNaOHOiknddPAA1vvdcpLEjhLd4R83CD+6hgWg7dH8XJTTW13mY 2d/fnmrqKEnjH9bwhSKDQFfPhRq3lQ0sIHwNWw5nTDiNpjk9/IPttXKXJoh6CNaH8tDW F/eutFH7mAhqNETC+Wbs867u+qQXTINblJue93iVWnzsYjkAaHcODWwNYS6LzhSB0t1v QVRj0nM1brG95gwRkyLKM8egeDf/5+VPmf+QsQ8mJeO8voGhDL1r3gfEKIUfSVCM2udF 4m7ukMr/e48tUWIXxUelwf/H1jMSxRHOAPbdKT8UVlTdr51y1sYNOz0z0e3//iT2LnhD SlvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744832777; x=1745437577; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OofhF9O27cVrb8WKj94QBUeimdnvDakh4zV4gN59Auk=; b=L9r9jXlyZWUkJ1yMu1zYLyj2Z3tYn1pPuRohbohuPBN+2mKf5/837YdaZbyf9iAE6l 2F/fOX4YqbbKHOlOBdP0pTwobzNxdzI/XJyUewX6omqyxTi3bzh2UW8IqzQI7lx4+CMT lqbsSbsWpd5Une8bmiRPfFpMBysHQquMLsC2fRXiOoCVVE8ckEkVsHlK4YwJJY6Bn1VW 0SdIHBbsnkZJ23I9B+lDiSuv7qDc/bhKcwnPheiPT4a2nm72fBkhvJN6r7dDdRI4V0Q0 iMtAJWLl3uvX4ywQ11j0xJFO/HX+Ku/EUqa0nmUcVMfuhncuCRBOCmPsPCgtxU9NgmZA vrVg== X-Forwarded-Encrypted: i=1; AJvYcCUFZ6TZp3smqBL7bnZRrT91A9cAovf29+Lqki6IlWc2bEMlQJzSvyfc66X2q0UGfqH1RXE3JD+iDfBnXNI=@vger.kernel.org X-Gm-Message-State: AOJu0YxSqiRKeNRdadUmj75kqUzzWdYcRa6x0lBEu0modFWmCi7u9g67 u33kg3yNYeMWoiSrcIi6jLxyIL83ig09NZZxYLSWNf5NbqV8+PfYyOgOZ0gEYmxDe618jNB3aez zuJDaxEOaNT7Od3TbM7dm+JYe/ZnuvryI X-Gm-Gg: ASbGncs+o2T5wq0rq7F7FwWPZt7/H6/kUiXRr70zZKsiqn5w+A8HcUEtKIpzvDBnogg 3s4gx7oX/uJeKZbVta4eDR2gvm2fBsIbs8TpWqIv2/gUsq1d3ep2OI3LAIdaEgy9Ad8+4jntME+ LKSUWBNou4+7qlirQidmXM/soBM+zcyl472PUrCMZ4c3UWQMgTnd+abXOoh5F1fDokSl0Qiub/0 6FU11ImPFVsXZpS4xhzAEc4qygN9Ta7omgxIgxz00WUaeHkl/Q06IVQ5v4ZxiW+2CsmJPeuahD3 T/OcC5c+f3A//Q3UOwxxQlYcnAixpkqufNG8Scnxy0EqXg== X-Google-Smtp-Source: AGHT+IHK6kJ+1KjaTnTQVEhaL8MlXYaxdyW3jy6ShqO7QymT3Ojn4Obrefs2o7lpRfVUMFUZ8u7oSSQml4nx X-Received: by 2002:a05:690c:3393:b0:6f9:4c00:53ae with SMTP id 00721157ae682-706beb2df29mr1335057b3.8.1744832777325; Wed, 16 Apr 2025 12:46:17 -0700 (PDT) Received: from c7-smtp-2023.dev.purestorage.com ([208.88.159.128]) by smtp-relay.gmail.com with ESMTPS id 00721157ae682-7053e0f43b6sm7203417b3.11.2025.04.16.12.46.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Apr 2025 12:46:17 -0700 (PDT) X-Relaying-Domain: purestorage.com Received: from dev-ushankar.dev.purestorage.com (dev-ushankar.dev.purestorage.com [10.7.70.36]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 8D61C340424; Wed, 16 Apr 2025 13:46:16 -0600 (MDT) Received: by dev-ushankar.dev.purestorage.com (Postfix, from userid 1557716368) id 81E3FE402BD; Wed, 16 Apr 2025 13:46:16 -0600 (MDT) From: Uday Shankar Date: Wed, 16 Apr 2025 13:46:05 -0600 Subject: [PATCH v5 1/4] ublk: require unique task per io instead of unique task per hctx Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250416-ublk_task_per_io-v5-1-9261ad7bff20@purestorage.com> References: <20250416-ublk_task_per_io-v5-0-9261ad7bff20@purestorage.com> In-Reply-To: <20250416-ublk_task_per_io-v5-0-9261ad7bff20@purestorage.com> To: Ming Lei , Jens Axboe , Caleb Sander Mateos Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Uday Shankar X-Mailer: b4 0.14.2 Currently, ublk_drv associates to each hardware queue (hctx) a unique task (called the queue's ubq_daemon) which is allowed to issue COMMIT_AND_FETCH commands against the hctx. If any other task attempts to do so, the command fails immediately with EINVAL. When considered together with the block layer architecture, the result is that for each CPU C on the system, there is a unique ublk server thread which is allowed to handle I/O submitted on CPU C. This can lead to suboptimal performance under imbalanced load generation. For an extreme example, suppose all the load is generated on CPUs mapping to a single ublk server thread. Then that thread may be fully utilized and become the bottleneck in the system, while other ublk server threads are totally idle. This issue can also be addressed directly in the ublk server without kernel support by having threads dequeue I/Os and pass them around to ensure even load. But this solution requires inter-thread communication at least twice for each I/O (submission and completion), which is generally a bad pattern for performance. The problem gets even worse with zero copy, as more inter-thread communication would be required to have the buffer register/unregister calls to come from the correct thread. Therefore, address this issue in ublk_drv by requiring a unique task per I/O instead of per queue/hctx. Imbalanced load can then be balanced across all ublk server threads by having threads issue FETCH_REQs in a round-robin manner. As a small toy example, consider a system with a single ublk device having 2 queues, each of queue depth 4. A ublk server having 4 threads could issue its FETCH_REQs against this device as follows (where each entry is the qid,tag pair that the FETCH_REQ targets): poller thread: T0 T1 T2 T3 0,0 0,1 0,2 0,3 1,3 1,0 1,1 1,2 Since tags appear to be allocated in sequential chunks, this setup provides a rough approximation to distributing I/Os round-robin across all ublk server threads, while letting I/Os stay fully thread-local. Signed-off-by: Uday Shankar Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 76 ++++++++++++++++++++++----------------------= ---- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index cdb1543fa4a9817aa2ca2fca66720f589cf222be..4e4863d873e6593ad94c72cbf97= 1f792df5795ae 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -150,6 +150,7 @@ struct ublk_io { int res; =20 struct io_uring_cmd *cmd; + struct task_struct *task; }; =20 struct ublk_queue { @@ -157,11 +158,9 @@ struct ublk_queue { int q_depth; =20 unsigned long flags; - struct task_struct *ubq_daemon; struct ublksrv_io_desc *io_cmd_buf; =20 bool force_abort; - bool timeout; bool canceling; bool fail_io; /* copy of dev->state =3D=3D UBLK_S_DEV_FAIL_IO */ unsigned short nr_io_ready; /* how many ios setup */ @@ -1072,11 +1071,6 @@ static inline struct ublk_uring_cmd_pdu *ublk_get_ur= ing_cmd_pdu( return io_uring_cmd_to_pdu(ioucmd, struct ublk_uring_cmd_pdu); } =20 -static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq) -{ - return ubq->ubq_daemon->flags & PF_EXITING; -} - /* todo: handle partial completion */ static inline void __ublk_complete_rq(struct request *req) { @@ -1224,13 +1218,13 @@ static void ublk_dispatch_req(struct ublk_queue *ub= q, /* * Task is exiting if either: * - * (1) current !=3D ubq_daemon. + * (1) current !=3D io->task. * io_uring_cmd_complete_in_task() tries to run task_work - * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING. + * in a workqueue if cmd's task is PF_EXITING. * * (2) current->flags & PF_EXITING. */ - if (unlikely(current !=3D ubq->ubq_daemon || current->flags & PF_EXITING)= ) { + if (unlikely(current !=3D io->task || current->flags & PF_EXITING)) { __ublk_abort_rq(ubq, req); return; } @@ -1336,23 +1330,20 @@ static void ublk_queue_cmd_list(struct ublk_queue *= ubq, struct rq_list *l) static enum blk_eh_timer_return ublk_timeout(struct request *rq) { struct ublk_queue *ubq =3D rq->mq_hctx->driver_data; + struct ublk_io *io =3D &ubq->ios[rq->tag]; unsigned int nr_inflight =3D 0; int i; =20 if (ubq->flags & UBLK_F_UNPRIVILEGED_DEV) { - if (!ubq->timeout) { - send_sig(SIGKILL, ubq->ubq_daemon, 0); - ubq->timeout =3D true; - } - + send_sig(SIGKILL, io->task, 0); return BLK_EH_DONE; } =20 - if (!ubq_daemon_is_dying(ubq)) + if (!(io->task->flags & PF_EXITING)) return BLK_EH_RESET_TIMER; =20 for (i =3D 0; i < ubq->q_depth; i++) { - struct ublk_io *io =3D &ubq->ios[i]; + io =3D &ubq->ios[i]; =20 if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) nr_inflight++; @@ -1552,8 +1543,8 @@ static void ublk_commit_completion(struct ublk_device= *ub, } =20 /* - * Called from ubq_daemon context via cancel fn, meantime quiesce ublk - * blk-mq queue, so we are called exclusively with blk-mq and ubq_daemon + * Called from io task context via cancel fn, meantime quiesce ublk + * blk-mq queue, so we are called exclusively with blk-mq and io task * context, so everything is serialized. */ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ub= q) @@ -1669,13 +1660,13 @@ static void ublk_uring_cmd_cancel_fn(struct io_urin= g_cmd *cmd, return; =20 task =3D io_uring_cmd_get_task(cmd); - if (WARN_ON_ONCE(task && task !=3D ubq->ubq_daemon)) + io =3D &ubq->ios[pdu->tag]; + if (WARN_ON_ONCE(task && task !=3D io->task)) return; =20 ub =3D ubq->dev; need_schedule =3D ublk_abort_requests(ub, ubq); =20 - io =3D &ubq->ios[pdu->tag]; WARN_ON_ONCE(io->cmd !=3D cmd); ublk_cancel_cmd(ubq, io, issue_flags); =20 @@ -1836,8 +1827,6 @@ static void ublk_mark_io_ready(struct ublk_device *ub= , struct ublk_queue *ubq) mutex_lock(&ub->mutex); ubq->nr_io_ready++; if (ublk_queue_ready(ubq)) { - ubq->ubq_daemon =3D current; - get_task_struct(ubq->ubq_daemon); ub->nr_queues_ready++; =20 if (capable(CAP_SYS_ADMIN)) @@ -1934,6 +1923,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *c= md, const struct ublksrv_io_cmd *ub_cmd) { struct ublk_device *ub =3D cmd->file->private_data; + struct task_struct *task; struct ublk_queue *ubq; struct ublk_io *io; u32 cmd_op =3D cmd->cmd_op; @@ -1952,13 +1942,13 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd = *cmd, if (!ubq || ub_cmd->q_id !=3D ubq->q_id) goto out; =20 - if (ubq->ubq_daemon && ubq->ubq_daemon !=3D current) - goto out; - if (tag >=3D ubq->q_depth) goto out; =20 io =3D &ubq->ios[tag]; + task =3D READ_ONCE(io->task); + if (task && task !=3D current) + goto out; =20 /* there is pending io cmd, something must be wrong */ if (io->flags & UBLK_IO_FLAG_ACTIVE) { @@ -2011,6 +2001,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *c= md, } =20 ublk_fill_io_cmd(io, cmd, ub_cmd->addr); + WRITE_ONCE(io->task, get_task_struct(current)); ublk_mark_io_ready(ub, ubq); break; case UBLK_IO_COMMIT_AND_FETCH_REQ: @@ -2248,9 +2239,15 @@ static void ublk_deinit_queue(struct ublk_device *ub= , int q_id) { int size =3D ublk_queue_cmd_buf_size(ub, q_id); struct ublk_queue *ubq =3D ublk_get_queue(ub, q_id); + struct ublk_io *io; + int i; + + for (i =3D 0; i < ubq->q_depth; i++) { + io =3D &ubq->ios[i]; + if (io->task) + put_task_struct(io->task); + } =20 - if (ubq->ubq_daemon) - put_task_struct(ubq->ubq_daemon); if (ubq->io_cmd_buf) free_pages((unsigned long)ubq->io_cmd_buf, get_order(size)); } @@ -2936,15 +2933,8 @@ static void ublk_queue_reinit(struct ublk_device *ub= , struct ublk_queue *ubq) { int i; =20 - WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq))); - /* All old ioucmds have to be completed */ ubq->nr_io_ready =3D 0; - /* old daemon is PF_EXITING, put it now */ - put_task_struct(ubq->ubq_daemon); - /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */ - ubq->ubq_daemon =3D NULL; - ubq->timeout =3D false; ubq->canceling =3D false; =20 for (i =3D 0; i < ubq->q_depth; i++) { @@ -2954,6 +2944,10 @@ static void ublk_queue_reinit(struct ublk_device *ub= , struct ublk_queue *ubq) io->flags =3D 0; io->cmd =3D NULL; io->addr =3D 0; + + WARN_ON_ONCE(!(io->task && (io->task->flags & PF_EXITING))); + put_task_struct(io->task); + io->task =3D NULL; } } =20 @@ -2993,7 +2987,7 @@ static int ublk_ctrl_start_recovery(struct ublk_devic= e *ub, pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id); for (i =3D 0; i < ub->dev_info.nr_hw_queues; i++) ublk_queue_reinit(ub, ublk_get_queue(ub, i)); - /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */ + /* set to NULL, otherwise new tasks cannot mmap the io_cmd_buf */ ub->mm =3D NULL; ub->nr_queues_ready =3D 0; ub->nr_privileged_daemon =3D 0; @@ -3011,14 +3005,14 @@ static int ublk_ctrl_end_recovery(struct ublk_devic= e *ub, int ret =3D -EINVAL; int i; =20 - pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...= \n", - __func__, ub->dev_info.nr_hw_queues, header->dev_id); - /* wait until new ubq_daemon sending all FETCH_REQ */ + pr_devel("%s: Waiting for all FETCH_REQs, dev id %d...\n", __func__, + header->dev_id); + if (wait_for_completion_interruptible(&ub->completion)) return -EINTR; =20 - pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n", - __func__, ub->dev_info.nr_hw_queues, header->dev_id); + pr_devel("%s: All FETCH_REQs received, dev id %d\n", __func__, + header->dev_id); =20 mutex_lock(&ub->mutex); if (ublk_nosrv_should_stop_dev(ub)) --=20 2.34.1