From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9FB024728F for ; Fri, 21 Nov 2025 01:59:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690356; cv=none; b=Yt14nLOc/jGt3/DjKfYx+GFIdc6eFvzGNwMbiz7bFG2AuehVW6Zv1nbdVUppjoztktYT5kaxJKtFSWjngtC8cryttd35fe7/MbwndyVKMcftQCQ0QtBF0RcEXDWOMAypizNAwCKXrgpVKjaFbc9wkOoEyOhrIC2mzvTPfw7z37w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690356; c=relaxed/simple; bh=A+Ci1L9h0yO7Ol7an2BcEr6Bgg9KUfw1d0OxivMPq6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LXExfv9pWU7B5rsrZqEONsXPxH9PoSO4sxLcgM4E50Da1vfbTdqq9tfvu+mh8sVh0+bDCb1mwIDFyQ6BXBRLw29Wm4l76JI9c0z49a2gwK71EZOL2pSTsLUjLV47Bnqs+gsHg1qB2XBFarhiScE0cpbjaPm8WsEDLwGd4UEeCCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LdGpB8De; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LdGpB8De" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690353; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SKVrOLGglcyGcmX+exd2HinbN0ONxMUnNRMW45Q/QCE=; b=LdGpB8De0EZmBJVrTKt7PVVtEhAHrZHCuMyyAr02TOfFOGsNd5wNSldmpPSpv4fqP1ANlf 5SNJwMdbq1JCdYwyzleiKE07FzctOc/mzhNkp2s9hV/9GwL3q7rU/hHEoGOLG5MevvA5Rk sOzeDkLIA1hrT7DU7gHmIDNxlXOe2hg= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-315-fFOdtJ4-MXOTTUWc3lEypQ-1; Thu, 20 Nov 2025 20:59:10 -0500 X-MC-Unique: fFOdtJ4-MXOTTUWc3lEypQ-1 X-Mimecast-MFC-AGG-ID: fFOdtJ4-MXOTTUWc3lEypQ_1763690348 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A22961800473; Fri, 21 Nov 2025 01:59:08 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8516E30044DB; Fri, 21 Nov 2025 01:59:06 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness Date: Fri, 21 Nov 2025 09:58:23 +0800 Message-ID: <20251121015851.3672073-2-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation. The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility. This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes. Cc: Stefani Seibold Cc: Andrew Morton Cc: linux-kernel@vger.kernel.org Signed-off-by: Ming Lei --- include/linux/kfifo.h | 34 ++++++++++++++++++++++++++++++++-- lib/kfifo.c | 8 ++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h index fd743d4c4b4b..8b81ac74829c 100644 --- a/include/linux/kfifo.h +++ b/include/linux/kfifo.h @@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \ }) \ ) =20 +/** + * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA no= de + * @fifo: pointer to the fifo + * @size: the number of elements in the fifo, this must be a power of 2 + * @gfp_mask: get_free_pages mask, passed to kmalloc() + * @node: NUMA node to allocate memory on + * + * This macro dynamically allocates a new fifo buffer with NUMA node aware= ness. + * + * The number of elements will be rounded-up to a power of 2. + * The fifo will be release with kfifo_free(). + * Return 0 if no error, otherwise an error code. + */ +#define kfifo_alloc_node(fifo, size, gfp_mask, node) \ +__kfifo_int_must_check_helper( \ +({ \ + typeof((fifo) + 1) __tmp =3D (fifo); \ + struct __kfifo *__kfifo =3D &__tmp->kfifo; \ + __is_kfifo_ptr(__tmp) ? \ + __kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) := \ + -EINVAL; \ +}) \ +) + /** * kfifo_free - frees the fifo * @fifo: the fifo to be freed @@ -899,8 +923,14 @@ __kfifo_uint_must_check_helper( \ ) =20 =20 -extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, - size_t esize, gfp_t gfp_mask); +extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size, + size_t esize, gfp_t gfp_mask, int node); + +static inline int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, + size_t esize, gfp_t gfp_mask) +{ + return __kfifo_alloc_node(fifo, size, esize, gfp_mask, NUMA_NO_NODE); +} =20 extern void __kfifo_free(struct __kfifo *fifo); =20 diff --git a/lib/kfifo.c b/lib/kfifo.c index a8b2eed90599..525e66f8294c 100644 --- a/lib/kfifo.c +++ b/lib/kfifo.c @@ -22,8 +22,8 @@ static inline unsigned int kfifo_unused(struct __kfifo *f= ifo) return (fifo->mask + 1) - (fifo->in - fifo->out); } =20 -int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, - size_t esize, gfp_t gfp_mask) +int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size, + size_t esize, gfp_t gfp_mask, int node) { /* * round up to the next power of 2, since our 'let the indices @@ -41,7 +41,7 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, return -EINVAL; } =20 - fifo->data =3D kmalloc_array(esize, size, gfp_mask); + fifo->data =3D kmalloc_array_node(esize, size, gfp_mask, node); =20 if (!fifo->data) { fifo->mask =3D 0; @@ -51,7 +51,7 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, =20 return 0; } -EXPORT_SYMBOL(__kfifo_alloc); +EXPORT_SYMBOL(__kfifo_alloc_node); =20 void __kfifo_free(struct __kfifo *fifo) { --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27C8025A341 for ; Fri, 21 Nov 2025 01:59:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690358; cv=none; b=cnSjYQV1Yg+Of6vHvUxgg+1oS3lfXVKBrRgz/JaaJfSDHThTnPDSs9+1PMyjmM38Pnarozv/nys5vDq4PgilSMUvLdLhW7bcHgMWUwG/VQ4K+R2HgNFOUSPKY2WvDsxtz0xuT9RL1bBZOM92W0w7ewKNyaPgFMlCFGcuaOcszC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690358; c=relaxed/simple; bh=bTepRVlUnh5zmgP2mbLkSTFzKTwdQ5Nbz+v0A4Zh6Z4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G+liH28mVGIwInUKWJnKgxRWn8wm0lYhf/xP4bjKAm8VewqEwvQXZD5/ksZBsQcnHzcDzigZdXoUugAaxh8hnUKbao1pfmu2yOi27slias6yIloDMKCWn2Eg/clH9/5e8PmcHWMu4YDcV0vxer8ztrPz3KkgtWligYIMv2sjegc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZnOLPw8N; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZnOLPw8N" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690355; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ukTf8SMpn9XlUYt2gwe5inrS0HBBtFrJ8LHwKh+e4hU=; b=ZnOLPw8N5yquniXHIFVDzrBhSjFruUepP2ceFPakf2HZl3OqJRztDr9sRciIouDzlQ4xgv DI02mJiT0k5UBa/9d7i9aVmDevskHnhtDNfIzoqEG3jgUL0+rBJ6/Wy/QBUerziJwAMR6L 3+wZf+fLGcNy6wJK2f6ZoOgcAKuMREE= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-8xBPPQPsMlWvfj3nQvXqVw-1; Thu, 20 Nov 2025 20:59:14 -0500 X-MC-Unique: 8xBPPQPsMlWvfj3nQvXqVw-1 X-Mimecast-MFC-AGG-ID: 8xBPPQPsMlWvfj3nQvXqVw_1763690353 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E45311800250; Fri, 21 Nov 2025 01:59:12 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8D3E71800984; Fri, 21 Nov 2025 01:59:10 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 02/27] ublk: add parameter `struct io_uring_cmd *` to ublk_prep_auto_buf_reg() Date: Fri, 21 Nov 2025 09:58:24 +0800 Message-ID: <20251121015851.3672073-3-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Add parameter `struct io_uring_cmd *` to ublk_prep_auto_buf_reg() and prepare for reusing this helper for the coming UBLK_BATCH_IO feature, which can fetch & commit one batch of io commands via single uring_cmd. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 75f210523e52..2884e0687e31 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -1176,11 +1176,12 @@ ublk_auto_buf_reg_fallback(const struct ublk_queue = *ubq, struct ublk_io *io) } =20 static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request= *req, - struct ublk_io *io, unsigned int issue_flags) + struct ublk_io *io, struct io_uring_cmd *cmd, + unsigned int issue_flags) { int ret; =20 - ret =3D io_buffer_register_bvec(io->cmd, req, ublk_io_release, + ret =3D io_buffer_register_bvec(cmd, req, ublk_io_release, io->buf.index, issue_flags); if (ret) { if (io->buf.flags & UBLK_AUTO_BUF_REG_FALLBACK) { @@ -1192,18 +1193,19 @@ static bool ublk_auto_buf_reg(const struct ublk_que= ue *ubq, struct request *req, } =20 io->task_registered_buffers =3D 1; - io->buf_ctx_handle =3D io_uring_cmd_ctx_handle(io->cmd); + io->buf_ctx_handle =3D io_uring_cmd_ctx_handle(cmd); io->flags |=3D UBLK_IO_FLAG_AUTO_BUF_REG; return true; } =20 static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq, struct request *req, struct ublk_io *io, + struct io_uring_cmd *cmd, unsigned int issue_flags) { ublk_init_req_ref(ubq, io); if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) - return ublk_auto_buf_reg(ubq, req, io, issue_flags); + return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags); =20 return true; } @@ -1278,7 +1280,7 @@ static void ublk_dispatch_req(struct ublk_queue *ubq, if (!ublk_start_io(ubq, req, io)) return; =20 - if (ublk_prep_auto_buf_reg(ubq, req, io, issue_flags)) + if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags)) ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags); } =20 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67B7427B50F for ; Fri, 21 Nov 2025 01:59:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690368; cv=none; b=iKKuUJnvczhncgDh8EUkroFPFp++ZYRoRmWxCXSRVhlFDMBcnIGIAgWpUjpR/RwQuEC1BIz8Df4ITaIFyHxgua295bOgpC9zCsknzigbiwboLKU61Z6IVHBWKowG6ghD5gezAP0c8K7Z+lcUn0zKfAmrZHfNJx0v0ruqYBryL3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690368; c=relaxed/simple; bh=ID3VdaatVF2sSDoX38s0c3b1u8IK71WzWOR198pdGR4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jg07DFA/DbG3BkUBv/jgUYCUrnOM/SAI40T+KZkeHL+F7l0iz0igISsLqEyEYeZyD0MRurqdaGgDvr6S6F1T0P4Qp+dxdTacIwbeaUtIaCEWtJwH8ItvrU4tLDpNSurq2rv4G5pPL5D7qKtFm9V2V/VqRRR2VwNk0Lt+1Ptgz7I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CBmeKNzW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CBmeKNzW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iVoBVCrQUYZ9ilGjyioJ6sWCbx0PTCJ/7FPYB9TJGCc=; b=CBmeKNzWWz/TMjGjVwC+/AJMr9+24GFerBYqARjbJe5Pi1S5i8tfoaWy3hP4pgxn+LIohM n3tgRzHkmG0kFSr+txSvgluldzM6qe2Kl//JdZFLFaz5dww2h1iI1vsWV/W2WH9fS2nPAl XLs8JNwe5GkCytsPXU1u6EQHOE6cDVM= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-333-fP1sLXXWNf2FS94u3rdpjg-1; Thu, 20 Nov 2025 20:59:19 -0500 X-MC-Unique: fP1sLXXWNf2FS94u3rdpjg-1 X-Mimecast-MFC-AGG-ID: fP1sLXXWNf2FS94u3rdpjg_1763690357 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EF1B21956088; Fri, 21 Nov 2025 01:59:16 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E648830044DB; Fri, 21 Nov 2025 01:59:15 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 03/27] ublk: add `union ublk_io_buf` with improved naming Date: Fri, 21 Nov 2025 09:58:25 +0800 Message-ID: <20251121015851.3672073-4-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Add `union ublk_io_buf` for naming the anonymous union of struct ublk_io's addr and buf fields, meantime apply it to `struct ublk_io` for storing eith= er ublk auto buffer register data or ublk server io buffer address. The union uses clear field names: - `addr`: for regular ublk server io buffer addresses - `auto_reg`: for ublk auto buffer registration data This eliminates confusing access patterns and improves code readability. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 2884e0687e31..f1fa5ceacdf6 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -155,12 +155,13 @@ struct ublk_uring_cmd_pdu { */ #define UBLK_REFCOUNT_INIT (REFCOUNT_MAX / 2) =20 +union ublk_io_buf { + __u64 addr; + struct ublk_auto_buf_reg auto_reg; +}; + struct ublk_io { - /* userspace buffer address from io cmd */ - union { - __u64 addr; - struct ublk_auto_buf_reg buf; - }; + union ublk_io_buf buf; unsigned int flags; int res; =20 @@ -498,7 +499,7 @@ static blk_status_t ublk_setup_iod_zoned(struct ublk_qu= eue *ubq, iod->op_flags =3D ublk_op | ublk_req_build_flags(req); iod->nr_sectors =3D blk_rq_sectors(req); iod->start_sector =3D blk_rq_pos(req); - iod->addr =3D io->addr; + iod->addr =3D io->buf.addr; =20 return BLK_STS_OK; } @@ -981,7 +982,7 @@ static unsigned int ublk_map_io(const struct ublk_queue= *ubq, struct iov_iter iter; const int dir =3D ITER_DEST; =20 - import_ubuf(dir, u64_to_user_ptr(io->addr), rq_bytes, &iter); + import_ubuf(dir, u64_to_user_ptr(io->buf.addr), rq_bytes, &iter); return ublk_copy_user_pages(req, 0, &iter, dir); } return rq_bytes; @@ -1002,7 +1003,7 @@ static unsigned int ublk_unmap_io(bool need_map, =20 WARN_ON_ONCE(io->res > rq_bytes); =20 - import_ubuf(dir, u64_to_user_ptr(io->addr), io->res, &iter); + import_ubuf(dir, u64_to_user_ptr(io->buf.addr), io->res, &iter); return ublk_copy_user_pages(req, 0, &iter, dir); } return rq_bytes; @@ -1068,7 +1069,7 @@ static blk_status_t ublk_setup_iod(struct ublk_queue = *ubq, struct request *req) iod->op_flags =3D ublk_op | ublk_req_build_flags(req); iod->nr_sectors =3D blk_rq_sectors(req); iod->start_sector =3D blk_rq_pos(req); - iod->addr =3D io->addr; + iod->addr =3D io->buf.addr; =20 return BLK_STS_OK; } @@ -1182,9 +1183,9 @@ static bool ublk_auto_buf_reg(const struct ublk_queue= *ubq, struct request *req, int ret; =20 ret =3D io_buffer_register_bvec(cmd, req, ublk_io_release, - io->buf.index, issue_flags); + io->buf.auto_reg.index, issue_flags); if (ret) { - if (io->buf.flags & UBLK_AUTO_BUF_REG_FALLBACK) { + if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) { ublk_auto_buf_reg_fallback(ubq, io); return true; } @@ -1473,7 +1474,7 @@ static void ublk_queue_reinit(struct ublk_device *ub,= struct ublk_queue *ubq) */ io->flags &=3D UBLK_IO_FLAG_CANCELED; io->cmd =3D NULL; - io->addr =3D 0; + io->buf.addr =3D 0; =20 /* * old task is PF_EXITING, put it now @@ -2034,13 +2035,16 @@ static inline int ublk_check_cmd_op(u32 cmd_op) =20 static inline int ublk_set_auto_buf_reg(struct ublk_io *io, struct io_urin= g_cmd *cmd) { - io->buf =3D ublk_sqe_addr_to_auto_buf_reg(READ_ONCE(cmd->sqe->addr)); + struct ublk_auto_buf_reg buf; + + buf =3D ublk_sqe_addr_to_auto_buf_reg(READ_ONCE(cmd->sqe->addr)); =20 - if (io->buf.reserved0 || io->buf.reserved1) + if (buf.reserved0 || buf.reserved1) return -EINVAL; =20 - if (io->buf.flags & ~UBLK_AUTO_BUF_REG_F_MASK) + if (buf.flags & ~UBLK_AUTO_BUF_REG_F_MASK) return -EINVAL; + io->buf.auto_reg =3D buf; return 0; } =20 @@ -2062,7 +2066,7 @@ static int ublk_handle_auto_buf_reg(struct ublk_io *i= o, * this ublk request gets stuck. */ if (io->buf_ctx_handle =3D=3D io_uring_cmd_ctx_handle(cmd)) - *buf_idx =3D io->buf.index; + *buf_idx =3D io->buf.auto_reg.index; } =20 return ublk_set_auto_buf_reg(io, cmd); @@ -2090,7 +2094,7 @@ ublk_config_io_buf(const struct ublk_device *ub, stru= ct ublk_io *io, if (ublk_dev_support_auto_buf_reg(ub)) return ublk_handle_auto_buf_reg(io, cmd, buf_idx); =20 - io->addr =3D buf_addr; + io->buf.addr =3D buf_addr; return 0; } =20 @@ -2287,7 +2291,7 @@ static bool ublk_get_data(const struct ublk_queue *ub= q, struct ublk_io *io, */ io->flags &=3D ~UBLK_IO_FLAG_NEED_GET_DATA; /* update iod->addr because ublksrv may have passed a new io buffer */ - ublk_get_iod(ubq, req->tag)->addr =3D io->addr; + ublk_get_iod(ubq, req->tag)->addr =3D io->buf.addr; pr_devel("%s: update iod->addr: qid %d tag %d io_flags %x addr %llx\n", __func__, ubq->q_id, req->tag, io->flags, ublk_get_iod(ubq, req->tag)->addr); --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51CE0284880 for ; Fri, 21 Nov 2025 01:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690372; cv=none; b=GqHpTO6s2i3+ScFmwjD9AgYr5ajkxprvHChUTmfTuCkpxt5nKVekf4+DfUHOug+9L6XxZuqvl17tvo3WpE9d8iI1ToeQc7ket+5GsO3N31NaN37tZ+ctMwVlVt+s1Rjf2UEA4TyqqXMn6wfyj30Ix28JMJCI8KXReVU8Bx2/U7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690372; c=relaxed/simple; bh=2sEs+ptW36m+FbqKF4ySBIhs1E5oCnMNQ3hLz1W9yQo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IZC2neZ8vNujGfR6fL73O3V5CPAMU9E4w83ATvJLfwmYrJdmHg7nkdMsBC3/GGNt4FpVrJsqNC2TEg0O6rCOfy7dCmVOzADFLTcqiSxUbgIdndPHDPIh2/Zpr6a1QHNjukRbfwcKmqKQVG3HLRz6utAtYUWa8vWLPzcF2VTF15w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fVjUPYD2; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fVjUPYD2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yzFM7RscsZAkx5obOZIu/ddnHKngMYodVSIc+DQ/QEg=; b=fVjUPYD2xv3acB+5Bld8sNE1VuWRQYt21TQQ8CQWT1kJgCudHxj4XMTPdVUpw4JIujLaIr cIvoNk+lfcaWRur6WB7y5uWSmkBSsIvipSVuyb1UwEooAvU+3ZKjiwnP7x689kE1LGkkH1 xRzmCZQb3IAqDiRyIABpMHDo0RyBh6c= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-8CSPvGiLNzedJsKOTZQ5Xg-1; Thu, 20 Nov 2025 20:59:23 -0500 X-MC-Unique: 8CSPvGiLNzedJsKOTZQ5Xg-1 X-Mimecast-MFC-AGG-ID: 8CSPvGiLNzedJsKOTZQ5Xg_1763690362 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 50EDA195609F; Fri, 21 Nov 2025 01:59:21 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0D33D1956045; Fri, 21 Nov 2025 01:59:19 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 04/27] ublk: refactor auto buffer register in ublk_dispatch_req() Date: Fri, 21 Nov 2025 09:58:26 +0800 Message-ID: <20251121015851.3672073-5-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Refactor auto buffer register code and prepare for supporting batch IO feature, and the main motivation is to put 'ublk_io' operation code together, so that per-io lock can be applied for the code block. The key changes are: - Rename ublk_auto_buf_reg() as ublk_do_auto_buf_reg() - Introduce an enum `auto_buf_reg_res` to represent the result of the buffer registration attempt (FAIL, FALLBACK, OK). - Split the existing `ublk_do_auto_buf_reg` function into two: - `__ublk_do_auto_buf_reg`: Performs the actual buffer registration and returns the `auto_buf_reg_res` status. - `ublk_do_auto_buf_reg`: A wrapper that calls the internal function and handles the I/O preparation based on the result. - Introduce `ublk_prep_auto_buf_reg_io` to encapsulate the logic for preparing the I/O for completion after buffer registration. - Pass the `tag` directly to `ublk_auto_buf_reg_fallback` to avoid recalculating it. This refactoring makes the control flow clearer and isolates the different stages of the auto buffer registration process. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 64 +++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index f1fa5ceacdf6..b36cd55eceb0 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -1168,17 +1168,37 @@ static inline void __ublk_abort_rq(struct ublk_queu= e *ubq, } =20 static void -ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, struct ublk_io *i= o) +ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, unsigned tag) { - unsigned tag =3D io - ubq->ios; struct ublksrv_io_desc *iod =3D ublk_get_iod(ubq, tag); =20 iod->op_flags |=3D UBLK_IO_F_NEED_REG_BUF; } =20 -static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request= *req, - struct ublk_io *io, struct io_uring_cmd *cmd, - unsigned int issue_flags) +enum auto_buf_reg_res { + AUTO_BUF_REG_FAIL, + AUTO_BUF_REG_FALLBACK, + AUTO_BUF_REG_OK, +}; + +static void ublk_prep_auto_buf_reg_io(const struct ublk_queue *ubq, + struct request *req, struct ublk_io *io, + struct io_uring_cmd *cmd, + enum auto_buf_reg_res res) +{ + if (res =3D=3D AUTO_BUF_REG_OK) { + io->task_registered_buffers =3D 1; + io->buf_ctx_handle =3D io_uring_cmd_ctx_handle(cmd); + io->flags |=3D UBLK_IO_FLAG_AUTO_BUF_REG; + } + ublk_init_req_ref(ubq, io); + __ublk_prep_compl_io_cmd(io, req); +} + +static enum auto_buf_reg_res +__ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req, + struct ublk_io *io, struct io_uring_cmd *cmd, + unsigned int issue_flags) { int ret; =20 @@ -1186,29 +1206,27 @@ static bool ublk_auto_buf_reg(const struct ublk_que= ue *ubq, struct request *req, io->buf.auto_reg.index, issue_flags); if (ret) { if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) { - ublk_auto_buf_reg_fallback(ubq, io); - return true; + ublk_auto_buf_reg_fallback(ubq, req->tag); + return AUTO_BUF_REG_FALLBACK; } blk_mq_end_request(req, BLK_STS_IOERR); - return false; + return AUTO_BUF_REG_FAIL; } =20 - io->task_registered_buffers =3D 1; - io->buf_ctx_handle =3D io_uring_cmd_ctx_handle(cmd); - io->flags |=3D UBLK_IO_FLAG_AUTO_BUF_REG; - return true; + return AUTO_BUF_REG_OK; } =20 -static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq, - struct request *req, struct ublk_io *io, - struct io_uring_cmd *cmd, - unsigned int issue_flags) +static void ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct requ= est *req, + struct ublk_io *io, struct io_uring_cmd *cmd, + unsigned int issue_flags) { - ublk_init_req_ref(ubq, io); - if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) - return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags); + enum auto_buf_reg_res res =3D __ublk_do_auto_buf_reg(ubq, req, io, cmd, + issue_flags); =20 - return true; + if (res !=3D AUTO_BUF_REG_FAIL) { + ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res); + io_uring_cmd_done(cmd, UBLK_IO_RES_OK, issue_flags); + } } =20 static bool ublk_start_io(const struct ublk_queue *ubq, struct request *re= q, @@ -1281,8 +1299,12 @@ static void ublk_dispatch_req(struct ublk_queue *ubq, if (!ublk_start_io(ubq, req, io)) return; =20 - if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags)) + if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) { + ublk_do_auto_buf_reg(ubq, req, io, io->cmd, issue_flags); + } else { + ublk_init_req_ref(ubq, io); ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags); + } } =20 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd, --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADAF728489E for ; Fri, 21 Nov 2025 01:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690373; cv=none; b=u8W5HFTs8Ga7p/k4QOKgwu8ddJngQxMBeSDY9wnWLVCtWGYQqw51StDyFDU9lBxoV9yo9sPoaNsV7hI9UHupOgNNYPITNNDHmXjcsw91EuS2dYiFRo3on+qaFr/BbyOuhFM6U9mys6Bx4kqPafTHnePI5ptWsgF4wocbQtI9tGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690373; c=relaxed/simple; bh=Nr4pSoQwCzbbKfxs/8KKTOumo6FQ5gMWaQtD5tU/aJk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ES8dcSSoMxclNreZpQi4FQtUEH+NhK02BoilibB/2/p6QlIZJbCa0XytJuVcVI4qTNygWavZrRHBlemEdjqELaqtKolIgKAkox4+vrGptzxZweLqy9gjnaufB8JKEaw3SQs5U06Cm7BIXHrJUS9+sdybWXzDTtgQRSr+8o57gLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FkkXGgNH; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FkkXGgNH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gtLgc3ofh2FJNoVGQhSZzQnpnokBG3N60WsQ1kIOUHs=; b=FkkXGgNHUKaVmWjK2wAzDCIJJbyp1XjirEioyoFtIL8Am1aDGOZpvTxGTyPNE8Lpl6uORE qsOeYOv7U/qR7kOJmXyCPWcW9Gn6VOolIqiuM6ZJ4nxQHaBQ5WtDVp3tYRhSNXXaMnO7Fo g8xhNkS4yWGwjgC5P4t8ZECRiQroFYE= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-586-vBWxKaVsM_K2ZUL1wRs-OA-1; Thu, 20 Nov 2025 20:59:27 -0500 X-MC-Unique: vBWxKaVsM_K2ZUL1wRs-OA-1 X-Mimecast-MFC-AGG-ID: vBWxKaVsM_K2ZUL1wRs-OA_1763690365 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8455F19560AE; Fri, 21 Nov 2025 01:59:25 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0BEDD1940E82; Fri, 21 Nov 2025 01:59:23 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 05/27] ublk: pass const pointer to ublk_queue_is_zoned() Date: Fri, 21 Nov 2025 09:58:27 +0800 Message-ID: <20251121015851.3672073-6-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Pass const pointer to ublk_queue_is_zoned() because it is readonly. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index b36cd55eceb0..5e83c1b2a69e 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -265,7 +265,7 @@ static inline bool ublk_dev_is_zoned(const struct ublk_= device *ub) return ub->dev_info.flags & UBLK_F_ZONED; } =20 -static inline bool ublk_queue_is_zoned(struct ublk_queue *ubq) +static inline bool ublk_queue_is_zoned(const struct ublk_queue *ubq) { return ubq->flags & UBLK_F_ZONED; } --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7220288C2D for ; Fri, 21 Nov 2025 01:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690377; cv=none; b=TXNSHMIixSSTKsN1mCwcVSDLeQRKODBxDFCVco9VVhsRgK4i0BOwOeHSJ4xLKUvfQb0OeSjU9AEtwl90vKDpx2IskWFl6kRmx7ZMr81Y6c8sas6o3VM+JFDOPPlNV7aaBrAI8R73wyAIaCwF0hqB3fU7jDrLLUh5AR6A13LOwPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690377; c=relaxed/simple; bh=CfhiXF2toJi0vOqff2Us/WG6eVGDX1+2SysinflwNOY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WEvGDNiQ9ktoRHy3UEoCPGC9BDsjDr6BZ4m7gKY428KQvdDxhpqee+dbe9KSndlDkcXdmgL0GOlOwR3GWNvoTeLjOEVaEK/u1aE1DEqbN5heb8vWYoeSPf23Q/xz59gCCKq9eT4XnxzbFM9ZfYM8qG5QjAVq5xRJhYxuWcyTzxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Satn9mLu; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Satn9mLu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SdG/qIdtYpcV+p//szbolXF2Zwd6nFqi0cnvJj6VANg=; b=Satn9mLulzuaZIZSmQX9X9JGsFbtRkDz1UGewLWn14zmEAKGZ2C9zcXMiHQkzWylUmDD2M Z1IX79jyaPUrs9NI/1IspW3xv5lM7R7jDD4CrisqlhMpFms16m7lbRK96dHJqIVygcHuwo 7k9pk2Y6bYS2rhlJT2cerTWaxRwwams= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-612-RZAl_U91Mo6wYkaFH5Qcgw-1; Thu, 20 Nov 2025 20:59:30 -0500 X-MC-Unique: RZAl_U91Mo6wYkaFH5Qcgw-1 X-Mimecast-MFC-AGG-ID: RZAl_U91Mo6wYkaFH5Qcgw_1763690369 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 48A741954B17; Fri, 21 Nov 2025 01:59:29 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 411A5180049F; Fri, 21 Nov 2025 01:59:27 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 06/27] ublk: add helper of __ublk_fetch() Date: Fri, 21 Nov 2025 09:58:28 +0800 Message-ID: <20251121015851.3672073-7-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add helper __ublk_fetch() for refactoring ublk_fetch(). Meantime move ublk_config_io_buf() out of __ublk_fetch() to make the code structure cleaner. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 46 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 5e83c1b2a69e..dd9c35758a46 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -2234,39 +2234,41 @@ static int ublk_check_fetch_buf(const struct ublk_d= evice *ub, __u64 buf_addr) return 0; } =20 -static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub, - struct ublk_io *io, __u64 buf_addr) +static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub, + struct ublk_io *io) { - int ret =3D 0; - - /* - * When handling FETCH command for setting up ublk uring queue, - * ub->mutex is the innermost lock, and we won't block for handling - * FETCH, so it is fine even for IO_URING_F_NONBLOCK. - */ - mutex_lock(&ub->mutex); /* UBLK_IO_FETCH_REQ is only allowed before dev is setup */ - if (ublk_dev_ready(ub)) { - ret =3D -EBUSY; - goto out; - } + if (ublk_dev_ready(ub)) + return -EBUSY; =20 /* allow each command to be FETCHed at most once */ - if (io->flags & UBLK_IO_FLAG_ACTIVE) { - ret =3D -EINVAL; - goto out; - } + if (io->flags & UBLK_IO_FLAG_ACTIVE) + return -EINVAL; =20 WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV); =20 ublk_fill_io_cmd(io, cmd); - ret =3D ublk_config_io_buf(ub, io, cmd, buf_addr, NULL); - if (ret) - goto out; =20 WRITE_ONCE(io->task, get_task_struct(current)); ublk_mark_io_ready(ub); -out: + + return 0; +} + +static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub, + struct ublk_io *io, __u64 buf_addr) +{ + int ret; + + /* + * When handling FETCH command for setting up ublk uring queue, + * ub->mutex is the innermost lock, and we won't block for handling + * FETCH, so it is fine even for IO_URING_F_NONBLOCK. + */ + mutex_lock(&ub->mutex); + ret =3D __ublk_fetch(cmd, ub, io); + if (!ret) + ret =3D ublk_config_io_buf(ub, io, cmd, buf_addr, NULL); mutex_unlock(&ub->mutex); return ret; } --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BA7626D4C3 for ; Fri, 21 Nov 2025 01:59:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690384; cv=none; b=Yb5bCsDjR4OpVHVmeOezczlVUrk87/0KTiOZZdCoZdNVFnpN4vYHllL3qHRBYHBg4DaJQlCYf/fUTgFD5uSxR3Aidj6stn7M6aIWq9IupVAuTq3p4xczyt4mtT1ictA5VEL6THXrZCENT3XjS5DNSjgmWbcue01a5YftH8MCpYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690384; c=relaxed/simple; bh=9lz/zTnuBVl9xO+Syb30DgeeOR4F5ieQ9i3f2HRRm7Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EFtaghqyrckZlXM3VsO5A+HRcC7xFKSRySYe1YSMdN2iOHEqklCASwBkE+vxZF+d0ShRCJ9RZX9q99BwN1ciAgAUYEXBkbt1Ps37SyAfG8+iPq+WRWSDT/qGT1b5Rm/a4eYpIEeF5DuZSkdI3iknpuDi74kL91Pu5nAxySvWYpg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Rx5yBgC6; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Rx5yBgC6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PO6f5jz42DBlQAUhK3se1j7b6gW7jvpeEuu1PFTGky4=; b=Rx5yBgC68eL5XeinhaIR/WCUkoCGhqRXyws39EjuPcNoUaJJegE1EV3/FIYS3jujDP7NOI cE8P4nsUKku7afkgJfu8n2tZu5dP7RkEgYHGlJT+6UKlDF410QM8RHWHoBFDu/dZebade+ 2bMKcLGfgWyNKIQ/JmnFz8ksuYh+Gl4= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-161-5tEhFnH9Nba6nKW0iXWT6w-1; Thu, 20 Nov 2025 20:59:36 -0500 X-MC-Unique: 5tEhFnH9Nba6nKW0iXWT6w-1 X-Mimecast-MFC-AGG-ID: 5tEhFnH9Nba6nKW0iXWT6w_1763690373 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 44D1219560B5; Fri, 21 Nov 2025 01:59:33 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 28F6330044DB; Fri, 21 Nov 2025 01:59:31 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 07/27] ublk: define ublk_ch_batch_io_fops for the coming feature F_BATCH_IO Date: Fri, 21 Nov 2025 09:58:29 +0800 Message-ID: <20251121015851.3672073-8-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Introduces the basic structure for a batched I/O feature in the ublk driver. It adds placeholder functions and a new file operations structure, ublk_ch_batch_io_fops, which will be used for fetching and committing I/O commands in batches. Currently, the feature is disabled. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index dd9c35758a46..1fcca52591c3 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -254,6 +254,11 @@ static inline struct request *__ublk_check_and_get_req= (struct ublk_device *ub, u16 q_id, u16 tag, struct ublk_io *io, size_t offset); static inline unsigned int ublk_req_build_flags(struct request *req); =20 +static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub) +{ + return false; +} + static inline struct ublksrv_io_desc * ublk_get_iod(const struct ublk_queue *ubq, unsigned tag) { @@ -2512,6 +2517,12 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cm= d, unsigned int issue_flags) return ublk_ch_uring_cmd_local(cmd, issue_flags); } =20 +static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd, + unsigned int issue_flags) +{ + return -EOPNOTSUPP; +} + static inline bool ublk_check_ubuf_dir(const struct request *req, int ubuf_dir) { @@ -2618,6 +2629,16 @@ static const struct file_operations ublk_ch_fops =3D= { .mmap =3D ublk_ch_mmap, }; =20 +static const struct file_operations ublk_ch_batch_io_fops =3D { + .owner =3D THIS_MODULE, + .open =3D ublk_ch_open, + .release =3D ublk_ch_release, + .read_iter =3D ublk_ch_read_iter, + .write_iter =3D ublk_ch_write_iter, + .uring_cmd =3D ublk_ch_batch_io_uring_cmd, + .mmap =3D ublk_ch_mmap, +}; + static void ublk_deinit_queue(struct ublk_device *ub, int q_id) { struct ublk_queue *ubq =3D ub->queues[q_id]; @@ -2778,7 +2799,10 @@ static int ublk_add_chdev(struct ublk_device *ub) if (ret) goto fail; =20 - cdev_init(&ub->cdev, &ublk_ch_fops); + if (ublk_dev_support_batch_io(ub)) + cdev_init(&ub->cdev, &ublk_ch_batch_io_fops); + else + cdev_init(&ub->cdev, &ublk_ch_fops); ret =3D cdev_device_add(&ub->cdev, dev); if (ret) goto fail; --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C1E7299937 for ; Fri, 21 Nov 2025 01:59:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690387; cv=none; b=DMKXSqQBBx/BlNweYTCfpRbajCIqIJ7CeTM3IwLO2eswIJbvwz0tfLdVabN4E64Nyr3JE+1CEXSS6ipMRNA/8Ht7JYB8Y0tLqRjFYu1aios3kpiNDmeNrfXDpRvp3NgcayCU3nun0Jpv3qYCuoipEr11ttRrRJkooIh4e4RiUJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690387; c=relaxed/simple; bh=kJcy4CUmvgEo2byWDAoPUrwSACio5iRzha6q2St6BWk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Io/hdaRbAz2zhpxV8a5JTKV8iHQEjdQ/8CnhwwFaCLziVXM5s4+ykq/RbLEXE4vKvKBQmrp7eBIMTbcIcnP77usG/9Pac7RbghdZ2C7Vgxwlp06VW8Cs5N6Spck1llukZO8ygKRidiTKcNibwQaGPh/ZirmMV0uovL+IMAVgLFo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=N9i3QqDY; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N9i3QqDY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690384; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ODUrNlcPCRXeNA5FcaQBBiL+omHRqn5JUmPJnFh1T0E=; b=N9i3QqDYkR9Wl7eENBAKQMlfukEig5dE9A0VbDkhn3+5Y5pAiumDLGi2tFs/soN49QJPee Jom2AGgFUX8OorXwR7/EUMQBaia9ETtOBWFJu9Di3SFUxcJ16DPEVfJ8XxD/QQvujiaLP5 WHqYv/IprNNVPPLw65n/yuno7bnIel0= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-U5BkInFHMZaC0o3oUkwELw-1; Thu, 20 Nov 2025 20:59:38 -0500 X-MC-Unique: U5BkInFHMZaC0o3oUkwELw-1 X-Mimecast-MFC-AGG-ID: U5BkInFHMZaC0o3oUkwELw_1763690377 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 849C718001FE; Fri, 21 Nov 2025 01:59:37 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 14A0C30044FE; Fri, 21 Nov 2025 01:59:35 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 08/27] ublk: prepare for not tracking task context for command batch Date: Fri, 21 Nov 2025 09:58:30 +0800 Message-ID: <20251121015851.3672073-9-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" batch io is designed to be independent of task context, and we will not track task context for batch io feature. Reviewed-by: Caleb Sander Mateos Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 1fcca52591c3..c62b2f2057fe 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -2254,7 +2254,10 @@ static int __ublk_fetch(struct io_uring_cmd *cmd, st= ruct ublk_device *ub, =20 ublk_fill_io_cmd(io, cmd); =20 - WRITE_ONCE(io->task, get_task_struct(current)); + if (ublk_dev_support_batch_io(ub)) + WRITE_ONCE(io->task, NULL); + else + WRITE_ONCE(io->task, get_task_struct(current)); ublk_mark_io_ready(ub); =20 return 0; --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29354271468 for ; Fri, 21 Nov 2025 01:59:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690389; cv=none; b=RIk0F6Dm0BQWLkOefxTp/Cv0FlL+UBdjJAumBe0PcUYXgMLWXbFwllfkiJ04GNQD1t5Ni/XeEQYQoEclIoXsWdkvr8JOcVW+yardGeI0Ay1gaUoF5mNs6rzTZEHS5LzbyACPzRLokoi5i4Hn0Nr0OCpDJdurO1d5KcbSKDn51to= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690389; c=relaxed/simple; bh=C6+S1uh66SUGfjNvDo3bdbXbhKKD/a6/towSaHcs4Q4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=liU873vaJjCbGN8d1euEud+jjyj/74up9/UscUJeGXDufaF+vmu9Tog/txPnLSASbXmaMM3jQHoyawfCBKrBB+n6i2mSk2v+Nvjm38g1NeEeF+QHvzV9Ho62qbSiUcQmlhqJZKBympxqCDwq5jLW/oRuw89JwFkbKuCrXmRQppM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=h9TCruW5; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="h9TCruW5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690386; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q+I9PdJb9eELeqoa4CSjLGKXe9D0mUhTK6qjzTkzDrI=; b=h9TCruW5pMLw0SpwFX6Nf7Uo28C1cU7WZ4qy3LhcjncI4PvGLESI6LXrhiDBFxOFAB9hlA /o22n1PNSVNuGSCOCx3Q51yvMULLCFEwVHRHYI5Q7FhBd8AW+dEreBqU2dBDW5wnz2RhLI RAeRhgZ5ogdUZV4TYXYQDTc2D0+rAQY= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-L0KDSz_LNt-Gu1_d-pGBng-1; Thu, 20 Nov 2025 20:59:42 -0500 X-MC-Unique: L0KDSz_LNt-Gu1_d-pGBng-1 X-Mimecast-MFC-AGG-ID: L0KDSz_LNt-Gu1_d-pGBng_1763690381 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1532618002C4; Fri, 21 Nov 2025 01:59:41 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 13E491956045; Fri, 21 Nov 2025 01:59:39 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS Date: Fri, 21 Nov 2025 09:58:31 +0800 Message-ID: <20251121015851.3672073-10-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ. Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version. The new command header type is `struct ublk_batch_io`. This patch doesn't actually implement these commands yet, just validates the SQE fields. Signed-off-by: Ming Lei Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 85 ++++++++++++++++++++++++++++++++++- include/uapi/linux/ublk_cmd.h | 49 ++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index c62b2f2057fe..21890947ceec 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -85,6 +85,11 @@ UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED | \ UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT) =20 +#define UBLK_BATCH_F_ALL \ + (UBLK_BATCH_F_HAS_ZONE_LBA | \ + UBLK_BATCH_F_HAS_BUF_ADDR | \ + UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) + struct ublk_uring_cmd_pdu { /* * Store requests in same batch temporarily for queuing them to @@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu { u16 tag; }; =20 +struct ublk_batch_io_data { + struct ublk_device *ub; + struct io_uring_cmd *cmd; + struct ublk_batch_io header; +}; + /* * io command is active: sqe cmd is received, and its cqe isn't done * @@ -2520,10 +2531,82 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *c= md, unsigned int issue_flags) return ublk_ch_uring_cmd_local(cmd, issue_flags); } =20 +static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc) +{ + unsigned elem_bytes =3D sizeof(struct ublk_elem_header); + + if (uc->flags & ~UBLK_BATCH_F_ALL) + return -EINVAL; + + /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */ + if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) && + (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)) + return -EINVAL; + + elem_bytes +=3D (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA ? sizeof(u64) : 0)= + + (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR ? sizeof(u64) : 0); + if (uc->elem_bytes !=3D elem_bytes) + return -EINVAL; + return 0; +} + +static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data) +{ + + const struct ublk_batch_io *uc =3D &data->header; + + if (uc->nr_elem > data->ub->dev_info.queue_depth) + return -E2BIG; + + if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) && + !ublk_dev_is_zoned(data->ub)) + return -EINVAL; + + if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) && + !ublk_dev_need_map_io(data->ub)) + return -EINVAL; + + if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) && + !ublk_dev_support_auto_buf_reg(data->ub)) + return -EINVAL; + + return ublk_check_batch_cmd_flags(uc); +} + static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) { - return -EOPNOTSUPP; + const struct ublk_batch_io *uc =3D io_uring_sqe_cmd(cmd->sqe); + struct ublk_device *ub =3D cmd->file->private_data; + struct ublk_batch_io_data data =3D { + .ub =3D ub, + .cmd =3D cmd, + .header =3D (struct ublk_batch_io) { + .q_id =3D READ_ONCE(uc->q_id), + .flags =3D READ_ONCE(uc->flags), + .nr_elem =3D READ_ONCE(uc->nr_elem), + .elem_bytes =3D READ_ONCE(uc->elem_bytes), + }, + }; + u32 cmd_op =3D cmd->cmd_op; + int ret =3D -EINVAL; + + if (data.header.q_id >=3D ub->dev_info.nr_hw_queues) + goto out; + + switch (cmd_op) { + case UBLK_U_IO_PREP_IO_CMDS: + case UBLK_U_IO_COMMIT_IO_CMDS: + ret =3D ublk_check_batch_cmd(&data); + if (ret) + goto out; + ret =3D -EOPNOTSUPP; + break; + default: + ret =3D -EOPNOTSUPP; + } +out: + return ret; } =20 static inline bool ublk_check_ubuf_dir(const struct request *req, diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index ec77dabba45b..2ce5a496b622 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -102,6 +102,10 @@ _IOWR('u', 0x23, struct ublksrv_io_cmd) #define UBLK_U_IO_UNREGISTER_IO_BUF \ _IOWR('u', 0x24, struct ublksrv_io_cmd) +#define UBLK_U_IO_PREP_IO_CMDS \ + _IOWR('u', 0x25, struct ublk_batch_io) +#define UBLK_U_IO_COMMIT_IO_CMDS \ + _IOWR('u', 0x26, struct ublk_batch_io) =20 /* only ABORT means that no re-fetch */ #define UBLK_IO_RES_OK 0 @@ -525,6 +529,51 @@ struct ublksrv_io_cmd { }; }; =20 +struct ublk_elem_header { + __u16 tag; /* IO tag */ + + /* + * Buffer index for incoming io command, only valid iff + * UBLK_F_AUTO_BUF_REG is set + */ + __u16 buf_index; + __s32 result; /* I/O completion result (commit only) */ +}; + +/* + * uring_cmd buffer structure for batch commands + * + * buffer includes multiple elements, which number is specified by + * `nr_elem`. Each element buffer is organized in the following order: + * + * struct ublk_elem_buffer { + * // Mandatory fields (8 bytes) + * struct ublk_elem_header header; + * + * // Optional fields (8 bytes each, included based on flags) + * + * // Buffer address (if UBLK_BATCH_F_HAS_BUF_ADDR) for copying data + * // between ublk request and ublk server buffer + * __u64 buf_addr; + * + * // returned Zone append LBA (if UBLK_BATCH_F_HAS_ZONE_LBA) + * __u64 zone_lba; + * } + * + * Used for `UBLK_U_IO_PREP_IO_CMDS` and `UBLK_U_IO_COMMIT_IO_CMDS` + */ +struct ublk_batch_io { + __u16 q_id; +#define UBLK_BATCH_F_HAS_ZONE_LBA (1 << 0) +#define UBLK_BATCH_F_HAS_BUF_ADDR (1 << 1) +#define UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK (1 << 2) + __u16 flags; + __u16 nr_elem; + __u8 elem_bytes; + __u8 reserved; + __u64 reserved2; +}; + struct ublk_param_basic { #define UBLK_ATTR_READ_ONLY (1 << 0) #define UBLK_ATTR_ROTATIONAL (1 << 1) --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37BAF26D4C3 for ; Fri, 21 Nov 2025 01:59:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690390; cv=none; b=BLDGFxz1lKzwG5X60suJe6HUcmk//qtje40k1fVPAS/zMXps3jA31Aod+wrKsMepn+mcl/LBXLcsoV13PIGVnFpsjYZolbOWS24lYvEMJ8/NeS0bV+dmdL+iE6E5aLMRty2YxkLm1sNIf7IyPF6S2/4JLXO8zLtI2/nayi3Sw3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690390; c=relaxed/simple; bh=vJbcbVppBF99msYQHrhYPGXsAVIj9lSL88Qn9OcaJuE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Bsk1sBUAbBOaEiDcix+kI6KzELR4ve78Y2zGz7IfiDLmnpfxpzCmqJIrW+YGUOUucSDoL+8ZujiHIIYBMDpdQvkgW5Pm1d1pdtbx7L4L9rgk/3qSzKC9dptabPrWTn5Wz9f+1BplHIvUOvVp7CKQ/d8q9QPwwxZdtx81q9NS110= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=d4kVsqnU; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="d4kVsqnU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690388; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=klVmPWaEAkDVd48+5YP4HWMZITdL1v6A60yfa5YgneE=; b=d4kVsqnU4ZWzbRjHmOF6Z42B6DEZ1YL+9I9vrWPN6aSU3l01BoRrsO5U53DdXluXk5lhZB z+3CFzxqlHzRjLLfljinzb2x7MX9umfMfaMxQYDYfbqtwvDn4hTdFjbunQ9yhYfx+kmQep j6/2W4i07qlXozZ2qSbHengBjqyoGzc= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-262-yh949aweNU-V_yg0svoUqQ-1; Thu, 20 Nov 2025 20:59:46 -0500 X-MC-Unique: yh949aweNU-V_yg0svoUqQ-1 X-Mimecast-MFC-AGG-ID: yh949aweNU-V_yg0svoUqQ_1763690384 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C6A4418002C4; Fri, 21 Nov 2025 01:59:44 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C6B9930044E7; Fri, 21 Nov 2025 01:59:43 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 10/27] ublk: handle UBLK_U_IO_PREP_IO_CMDS Date: Fri, 21 Nov 2025 09:58:32 +0800 Message-ID: <20251121015851.3672073-11-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" This commit implements the handling of the UBLK_U_IO_PREP_IO_CMDS command, which allows userspace to prepare a batch of I/O requests. The core of this change is the `ublk_walk_cmd_buf` function, which iterates over the elements in the uring_cmd fixed buffer. For each element, it parses the I/O details, finds the corresponding `ublk_io` structure, and prepares = it for future dispatch. Add per-io lock for protecting concurrent delivery and committing. Signed-off-by: Ming Lei Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 193 +++++++++++++++++++++++++++++++++- include/uapi/linux/ublk_cmd.h | 5 + 2 files changed, 197 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 21890947ceec..66c77daae955 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -117,6 +117,7 @@ struct ublk_batch_io_data { struct ublk_device *ub; struct io_uring_cmd *cmd; struct ublk_batch_io header; + unsigned int issue_flags; }; =20 /* @@ -201,6 +202,7 @@ struct ublk_io { unsigned task_registered_buffers; =20 void *buf_ctx_handle; + spinlock_t lock; } ____cacheline_aligned_in_smp; =20 struct ublk_queue { @@ -270,6 +272,16 @@ static inline bool ublk_dev_support_batch_io(const str= uct ublk_device *ub) return false; } =20 +static inline void ublk_io_lock(struct ublk_io *io) +{ + spin_lock(&io->lock); +} + +static inline void ublk_io_unlock(struct ublk_io *io) +{ + spin_unlock(&io->lock); +} + static inline struct ublksrv_io_desc * ublk_get_iod(const struct ublk_queue *ubq, unsigned tag) { @@ -2531,6 +2543,171 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *c= md, unsigned int issue_flags) return ublk_ch_uring_cmd_local(cmd, issue_flags); } =20 +static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc, + const struct ublk_elem_header *elem) +{ + const void *buf =3D elem; + + if (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) + return *(__u64 *)(buf + sizeof(*elem)); + return 0; +} + +static struct ublk_auto_buf_reg +ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc, + const struct ublk_elem_header *elem) +{ + struct ublk_auto_buf_reg reg =3D { + .index =3D elem->buf_index, + .flags =3D (uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) ? + UBLK_AUTO_BUF_REG_FALLBACK : 0, + }; + + return reg; +} + +/* + * 48 can hold any type of buffer element(8, 16 and 24 bytes) because + * it is the least common multiple(LCM) of 8, 16 and 24 + */ +#define UBLK_CMD_BATCH_TMP_BUF_SZ (48 * 10) +struct ublk_batch_io_iter { + void __user *uaddr; + unsigned done, total; + unsigned char elem_bytes; + /* copy to this buffer from user space */ + unsigned char buf[UBLK_CMD_BATCH_TMP_BUF_SZ]; +}; + +static inline int +__ublk_walk_cmd_buf(struct ublk_queue *ubq, + struct ublk_batch_io_iter *iter, + const struct ublk_batch_io_data *data, + unsigned bytes, + int (*cb)(struct ublk_queue *q, + const struct ublk_batch_io_data *data, + const struct ublk_elem_header *elem)) +{ + unsigned int i; + int ret =3D 0; + + for (i =3D 0; i < bytes; i +=3D iter->elem_bytes) { + const struct ublk_elem_header *elem =3D + (const struct ublk_elem_header *)&iter->buf[i]; + + if (unlikely(elem->tag >=3D data->ub->dev_info.queue_depth)) { + ret =3D -EINVAL; + break; + } + + ret =3D cb(ubq, data, elem); + if (unlikely(ret)) + break; + } + + iter->done +=3D i; + return ret; +} + +static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter, + const struct ublk_batch_io_data *data, + int (*cb)(struct ublk_queue *q, + const struct ublk_batch_io_data *data, + const struct ublk_elem_header *elem)) +{ + struct ublk_queue *ubq =3D ublk_get_queue(data->ub, data->header.q_id); + int ret =3D 0; + + while (iter->done < iter->total) { + unsigned int len =3D min(sizeof(iter->buf), iter->total - iter->done); + + if (copy_from_user(iter->buf, iter->uaddr + iter->done, len)) { + pr_warn("ublk%d: read batch cmd buffer failed\n", + data->ub->dev_info.dev_id); + return -EFAULT; + } + + ret =3D __ublk_walk_cmd_buf(ubq, iter, data, len, cb); + if (ret) + return ret; + } + return 0; +} + +static int ublk_batch_unprep_io(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + const struct ublk_elem_header *elem) +{ + struct ublk_io *io =3D &ubq->ios[elem->tag]; + + data->ub->nr_io_ready--; + ublk_io_lock(io); + io->flags =3D 0; + ublk_io_unlock(io); + return 0; +} + +static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter, + const struct ublk_batch_io_data *data) +{ + int ret; + + /* Re-process only what we've already processed, starting from beginning = */ + iter->total =3D iter->done; + iter->done =3D 0; + + ret =3D ublk_walk_cmd_buf(iter, data, ublk_batch_unprep_io); + WARN_ON_ONCE(ret); +} + +static int ublk_batch_prep_io(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + const struct ublk_elem_header *elem) +{ + struct ublk_io *io =3D &ubq->ios[elem->tag]; + const struct ublk_batch_io *uc =3D &data->header; + union ublk_io_buf buf =3D { 0 }; + int ret; + + if (ublk_dev_support_auto_buf_reg(data->ub)) + buf.auto_reg =3D ublk_batch_auto_buf_reg(uc, elem); + else if (ublk_dev_need_map_io(data->ub)) { + buf.addr =3D ublk_batch_buf_addr(uc, elem); + + ret =3D ublk_check_fetch_buf(data->ub, buf.addr); + if (ret) + return ret; + } + + ublk_io_lock(io); + ret =3D __ublk_fetch(data->cmd, data->ub, io); + if (!ret) + io->buf =3D buf; + ublk_io_unlock(io); + + return ret; +} + +static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *dat= a) +{ + const struct ublk_batch_io *uc =3D &data->header; + struct io_uring_cmd *cmd =3D data->cmd; + struct ublk_batch_io_iter iter =3D { + .uaddr =3D u64_to_user_ptr(READ_ONCE(cmd->sqe->addr)), + .total =3D uc->nr_elem * uc->elem_bytes, + .elem_bytes =3D uc->elem_bytes, + }; + int ret; + + mutex_lock(&data->ub->mutex); + ret =3D ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io); + + if (ret && iter.done) + ublk_batch_revert_prep_cmd(&iter, data); + mutex_unlock(&data->ub->mutex); + return ret; +} + static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc) { unsigned elem_bytes =3D sizeof(struct ublk_elem_header); @@ -2587,6 +2764,7 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring= _cmd *cmd, .nr_elem =3D READ_ONCE(uc->nr_elem), .elem_bytes =3D READ_ONCE(uc->elem_bytes), }, + .issue_flags =3D issue_flags, }; u32 cmd_op =3D cmd->cmd_op; int ret =3D -EINVAL; @@ -2596,6 +2774,11 @@ static int ublk_ch_batch_io_uring_cmd(struct io_urin= g_cmd *cmd, =20 switch (cmd_op) { case UBLK_U_IO_PREP_IO_CMDS: + ret =3D ublk_check_batch_cmd(&data); + if (ret) + goto out; + ret =3D ublk_handle_batch_prep_cmd(&data); + break; case UBLK_U_IO_COMMIT_IO_CMDS: ret =3D ublk_check_batch_cmd(&data); if (ret) @@ -2770,7 +2953,7 @@ static int ublk_init_queue(struct ublk_device *ub, in= t q_id) struct ublk_queue *ubq; struct page *page; int numa_node; - int size; + int size, i; =20 /* Determine NUMA node based on queue's CPU affinity */ numa_node =3D ublk_get_queue_numa_node(ub, q_id); @@ -2795,6 +2978,9 @@ static int ublk_init_queue(struct ublk_device *ub, in= t q_id) } ubq->io_cmd_buf =3D page_address(page); =20 + for (i =3D 0; i < ubq->q_depth; i++) + spin_lock_init(&ubq->ios[i].lock); + ub->queues[q_id] =3D ubq; ubq->dev =3D ub; return 0; @@ -3021,6 +3207,11 @@ static int ublk_ctrl_start_dev(struct ublk_device *u= b, return -EINVAL; =20 mutex_lock(&ub->mutex); + /* device may become not ready in case of F_BATCH */ + if (!ublk_dev_ready(ub)) { + ret =3D -EINVAL; + goto out_unlock; + } if (ub->dev_info.state =3D=3D UBLK_S_DEV_LIVE || test_bit(UB_STATE_USED, &ub->state)) { ret =3D -EEXIST; diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index 2ce5a496b622..c96c299057c3 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -102,6 +102,11 @@ _IOWR('u', 0x23, struct ublksrv_io_cmd) #define UBLK_U_IO_UNREGISTER_IO_BUF \ _IOWR('u', 0x24, struct ublksrv_io_cmd) + +/* + * return 0 if the command is run successfully, otherwise failure code + * is returned + */ #define UBLK_U_IO_PREP_IO_CMDS \ _IOWR('u', 0x25, struct ublk_batch_io) #define UBLK_U_IO_COMMIT_IO_CMDS \ --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57DAE2BE641 for ; Fri, 21 Nov 2025 01:59:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690396; cv=none; b=sOFxiA79cqOjoT5ATnYwNcMTrt5S3VGRHd+i9jlYuRtYDElCJkhUoXbeD7of6Fa5nAGHebHvSgXF1LCY6tkWglaKlloDuQ/FWazc2k+XuJSjOxNoXZxItD2T9gQ4iCLp6saxGy4WNnd3YhqaXdHbBp1sR72RYbCOYDFn2IYtpwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690396; c=relaxed/simple; bh=5LMWIeqG5PIyYlZmJ7CPajCKcrm7T3hxhdznD9VobuQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GwAgpMfLXGQHfp0qyz8ZDeWGnKH6pqFhTyBNCumMbwKcCCTTom+ZBzqPQgwjNXf3rsT2QMpQUOFUfs+lUyBuVYqBf/PF1Ef+mXk6IboodM0K1IK7I4zKgUHAnl8oJrX67gwv2ErlRbVU+6xePnyjGx6GAkBjKgfNhstNw//bpiQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=h8Nex0p1; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="h8Nex0p1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690393; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QTJRR/bBQGWNs3uz+GpCrmo40Aq6u6MN2feOR37/kKg=; b=h8Nex0p1BX5M+zJDvzs7VxA43KtGnrK05b/gPaicuGUrg04svykmOISZMi6oKJ4wSL4cF5 MO5jveD0VLuYeCnfmvSJORdwrEb1AdKqTn6acnslL864cor+fWv1XvkIPWdHlIEZwG0o0w QqbZ7dFm54r9epGLvOGMSa3IEZF7mDg= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-354-e0rD3jMPO6WU0_54tOMYZQ-1; Thu, 20 Nov 2025 20:59:49 -0500 X-MC-Unique: e0rD3jMPO6WU0_54tOMYZQ-1 X-Mimecast-MFC-AGG-ID: e0rD3jMPO6WU0_54tOMYZQ_1763690388 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 86CE7180035F; Fri, 21 Nov 2025 01:59:48 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B17F01956045; Fri, 21 Nov 2025 01:59:47 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 11/27] ublk: handle UBLK_U_IO_COMMIT_IO_CMDS Date: Fri, 21 Nov 2025 09:58:33 +0800 Message-ID: <20251121015851.3672073-12-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Handle UBLK_U_IO_COMMIT_IO_CMDS by walking the uring_cmd fixed buffer: - read each element into one temp buffer in batch style - parse and apply each element for committing io result Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 117 ++++++++++++++++++++++++++++++++-- include/uapi/linux/ublk_cmd.h | 8 +++ 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 66c77daae955..ea992366af5b 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -2098,9 +2098,9 @@ static inline int ublk_set_auto_buf_reg(struct ublk_i= o *io, struct io_uring_cmd return 0; } =20 -static int ublk_handle_auto_buf_reg(struct ublk_io *io, - struct io_uring_cmd *cmd, - u16 *buf_idx) +static void __ublk_handle_auto_buf_reg(struct ublk_io *io, + struct io_uring_cmd *cmd, + u16 *buf_idx) { if (io->flags & UBLK_IO_FLAG_AUTO_BUF_REG) { io->flags &=3D ~UBLK_IO_FLAG_AUTO_BUF_REG; @@ -2118,7 +2118,13 @@ static int ublk_handle_auto_buf_reg(struct ublk_io *= io, if (io->buf_ctx_handle =3D=3D io_uring_cmd_ctx_handle(cmd)) *buf_idx =3D io->buf.auto_reg.index; } +} =20 +static int ublk_handle_auto_buf_reg(struct ublk_io *io, + struct io_uring_cmd *cmd, + u16 *buf_idx) +{ + __ublk_handle_auto_buf_reg(io, cmd, buf_idx); return ublk_set_auto_buf_reg(io, cmd); } =20 @@ -2553,6 +2559,17 @@ static inline __u64 ublk_batch_buf_addr(const struct= ublk_batch_io *uc, return 0; } =20 +static inline __u64 ublk_batch_zone_lba(const struct ublk_batch_io *uc, + const struct ublk_elem_header *elem) +{ + const void *buf =3D (const void *)elem; + + if (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) + return *(__u64 *)(buf + sizeof(*elem) + + 8 * !!(uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)); + return -1; +} + static struct ublk_auto_buf_reg ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc, const struct ublk_elem_header *elem) @@ -2708,6 +2725,98 @@ static int ublk_handle_batch_prep_cmd(const struct u= blk_batch_io_data *data) return ret; } =20 +static int ublk_batch_commit_io_check(const struct ublk_queue *ubq, + struct ublk_io *io, + union ublk_io_buf *buf) +{ + struct request *req =3D io->req; + + if (!req) + return -EINVAL; + + if (io->flags & UBLK_IO_FLAG_ACTIVE) + return -EBUSY; + + if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)) + return -EINVAL; + + if (ublk_need_map_io(ubq)) { + /* + * COMMIT_AND_FETCH_REQ has to provide IO buffer if + * NEED GET DATA is not enabled or it is Read IO. + */ + if (!buf->addr && (!ublk_need_get_data(ubq) || + req_op(req) =3D=3D REQ_OP_READ)) + return -EINVAL; + } + return 0; +} + +static int ublk_batch_commit_io(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + const struct ublk_elem_header *elem) +{ + struct ublk_io *io =3D &ubq->ios[elem->tag]; + const struct ublk_batch_io *uc =3D &data->header; + u16 buf_idx =3D UBLK_INVALID_BUF_IDX; + union ublk_io_buf buf =3D { 0 }; + struct request *req =3D NULL; + bool auto_reg =3D false; + bool compl =3D false; + int ret; + + if (ublk_dev_support_auto_buf_reg(data->ub)) { + buf.auto_reg =3D ublk_batch_auto_buf_reg(uc, elem); + auto_reg =3D true; + } else if (ublk_dev_need_map_io(data->ub)) + buf.addr =3D ublk_batch_buf_addr(uc, elem); + + ublk_io_lock(io); + ret =3D ublk_batch_commit_io_check(ubq, io, &buf); + if (!ret) { + io->res =3D elem->result; + io->buf =3D buf; + req =3D ublk_fill_io_cmd(io, data->cmd); + + if (auto_reg) + __ublk_handle_auto_buf_reg(io, data->cmd, &buf_idx); + compl =3D ublk_need_complete_req(data->ub, io); + } + ublk_io_unlock(io); + + if (unlikely(ret)) { + pr_warn("%s: dev %u queue %u io %u: commit failure %d\n", + __func__, data->ub->dev_info.dev_id, ubq->q_id, + elem->tag, ret); + return ret; + } + + /* can't touch 'ublk_io' any more */ + if (buf_idx !=3D UBLK_INVALID_BUF_IDX) + io_buffer_unregister_bvec(data->cmd, buf_idx, data->issue_flags); + if (req_op(req) =3D=3D REQ_OP_ZONE_APPEND) + req->__sector =3D ublk_batch_zone_lba(uc, elem); + if (compl) + __ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub)); + return 0; +} + +static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *d= ata) +{ + const struct ublk_batch_io *uc =3D &data->header; + struct io_uring_cmd *cmd =3D data->cmd; + struct ublk_batch_io_iter iter =3D { + .uaddr =3D u64_to_user_ptr(READ_ONCE(cmd->sqe->addr)), + .total =3D uc->nr_elem * uc->elem_bytes, + .elem_bytes =3D uc->elem_bytes, + }; + int ret; + + ret =3D ublk_walk_cmd_buf(&iter, data, ublk_batch_commit_io); + + return iter.done =3D=3D 0 ? ret : iter.done; +} + static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc) { unsigned elem_bytes =3D sizeof(struct ublk_elem_header); @@ -2783,7 +2892,7 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring= _cmd *cmd, ret =3D ublk_check_batch_cmd(&data); if (ret) goto out; - ret =3D -EOPNOTSUPP; + ret =3D ublk_handle_batch_commit_cmd(&data); break; default: ret =3D -EOPNOTSUPP; diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index c96c299057c3..295ec8f34173 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -109,6 +109,14 @@ */ #define UBLK_U_IO_PREP_IO_CMDS \ _IOWR('u', 0x25, struct ublk_batch_io) +/* + * If failure code is returned, nothing in the command buffer is handled. + * Otherwise, the returned value means how many bytes in command buffer + * are handled actually, then number of handled IOs can be calculated with + * `elem_bytes` for each IO. IOs in the remained bytes are not committed, + * userspace has to check return value for dealing with partial committing + * correctly. + */ #define UBLK_U_IO_COMMIT_IO_CMDS \ _IOWR('u', 0x26, struct ublk_batch_io) =20 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 206732BEC57 for ; Fri, 21 Nov 2025 01:59:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690399; cv=none; b=pJAU713F9wn5aMmvI67f1dXl2bD/1+xbWu0DZc38xlZ9Jr9rpH+LrEgAxMNY5ePFOv4DnHuwjQ7lVeiG9tZPWLlA+GfDb4A2J0/aPItz2EpOVfpy0u7Bq0jazWYx1ecaa11+Vs0GEUjO9tIjoETLSjro1MF4iCBRy96QDRd4+cs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690399; c=relaxed/simple; bh=7zvwDyyBODSvYRmv8jqSfpojwR/HIpTaNTnLi8CH9UI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oUlejOEkooIdAbddz4FK7oUuzd6AEICqt7RQ5n7Qls8Gr4xSRGboynTgzM/i2U9Zw4BgnoALFSHxCXMu1enTOt9G8CPGjmoXYb/yjDwSF797OUQvoNO/oDMvd/uyeLf6ASHH+YcrXMLu1DnhI0pQA7y7sEPyxa0tvtJRlQO793A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QBG+MmzL; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QBG+MmzL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690397; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ycppPCNc+qj3Td0PdcNTHKzS0HwLiZSsLA49DzWiJto=; b=QBG+MmzL1o5E2GaSzhZGD+rP22drv3V9UxxpZzFBW2DcJiEBHlw1gb2MVTwsiuRvEygcTR 7Xrw7LWAHx9gs0A5Fc+Hf1fozp615jbFFrDe3X/mnb8tMFuCiLj9TzBAS8O1D/eqPOOPY6 KMfI+KRsX7cicUhYu/ixBjDnv+lGgUU= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-446-F6rDYoGKPMu0-N46NJ1W4Q-1; Thu, 20 Nov 2025 20:59:53 -0500 X-MC-Unique: F6rDYoGKPMu0-N46NJ1W4Q-1 X-Mimecast-MFC-AGG-ID: F6rDYoGKPMu0-N46NJ1W4Q_1763690392 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5DD11180035F; Fri, 21 Nov 2025 01:59:52 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5CF1F180049F; Fri, 21 Nov 2025 01:59:50 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 12/27] ublk: add io events fifo structure Date: Fri, 21 Nov 2025 09:58:34 +0800 Message-ID: <20251121015851.3672073-13-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add ublk io events fifo structure and prepare for supporting command batch, which will use io_uring multishot uring_cmd for fetching one batch of io commands each time. One nice feature of kfifo is to allow multiple producer vs single consumer. We just need lock the producer side, meantime the single consumer can be lockless. The producer is actually from ublk_queue_rq() or ublk_queue_rqs(), so lock contention can be eased by setting proper blk-mq nr_queues. Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 65 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index ea992366af5b..6ff284243630 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -44,6 +44,7 @@ #include #include #include +#include #include =20 #define UBLK_MINORS (1U << MINORBITS) @@ -217,6 +218,22 @@ struct ublk_queue { bool fail_io; /* copy of dev->state =3D=3D UBLK_S_DEV_FAIL_IO */ spinlock_t cancel_lock; struct ublk_device *dev; + + /* + * Inflight ublk request tag is saved in this fifo + * + * There are multiple writer from ublk_queue_rq() or ublk_queue_rqs(), + * so lock is required for storing request tag to fifo + * + * Make sure just one reader for fetching request from task work + * function to ublk server, so no need to grab the lock in reader + * side. + */ + struct { + DECLARE_KFIFO_PTR(evts_fifo, unsigned short); + spinlock_t evts_lock; + }____cacheline_aligned_in_smp; + struct ublk_io ios[] __counted_by(q_depth); }; =20 @@ -282,6 +299,32 @@ static inline void ublk_io_unlock(struct ublk_io *io) spin_unlock(&io->lock); } =20 +/* Initialize the queue */ +static inline int ublk_io_evts_init(struct ublk_queue *q, unsigned int siz= e, + int numa_node) +{ + spin_lock_init(&q->evts_lock); + return kfifo_alloc_node(&q->evts_fifo, size, GFP_KERNEL, numa_node); +} + +/* Check if queue is empty */ +static inline bool ublk_io_evts_empty(const struct ublk_queue *q) +{ + return kfifo_is_empty(&q->evts_fifo); +} + +/* Check if queue is full */ +static inline bool ublk_io_evts_full(const struct ublk_queue *q) +{ + return kfifo_is_full(&q->evts_fifo); +} + +static inline void ublk_io_evts_deinit(struct ublk_queue *q) +{ + WARN_ON_ONCE(!kfifo_is_empty(&q->evts_fifo)); + kfifo_free(&q->evts_fifo); +} + static inline struct ublksrv_io_desc * ublk_get_iod(const struct ublk_queue *ubq, unsigned tag) { @@ -3038,6 +3081,9 @@ static void ublk_deinit_queue(struct ublk_device *ub,= int q_id) if (ubq->io_cmd_buf) free_pages((unsigned long)ubq->io_cmd_buf, get_order(size)); =20 + if (ublk_dev_support_batch_io(ub)) + ublk_io_evts_deinit(ubq); + kvfree(ubq); ub->queues[q_id] =3D NULL; } @@ -3062,7 +3108,7 @@ static int ublk_init_queue(struct ublk_device *ub, in= t q_id) struct ublk_queue *ubq; struct page *page; int numa_node; - int size, i; + int size, i, ret =3D -ENOMEM; =20 /* Determine NUMA node based on queue's CPU affinity */ numa_node =3D ublk_get_queue_numa_node(ub, q_id); @@ -3081,18 +3127,27 @@ static int ublk_init_queue(struct ublk_device *ub, = int q_id) =20 /* Allocate I/O command buffer on local NUMA node */ page =3D alloc_pages_node(numa_node, gfp_flags, get_order(size)); - if (!page) { - kvfree(ubq); - return -ENOMEM; - } + if (!page) + goto fail_nomem; ubq->io_cmd_buf =3D page_address(page); =20 for (i =3D 0; i < ubq->q_depth; i++) spin_lock_init(&ubq->ios[i].lock); =20 + if (ublk_dev_support_batch_io(ub)) { + ret =3D ublk_io_evts_init(ubq, ubq->q_depth, numa_node); + if (ret) + goto fail; + } ub->queues[q_id] =3D ubq; ubq->dev =3D ub; + return 0; +fail: + ublk_deinit_queue(ub, q_id); +fail_nomem: + kvfree(ubq); + return ret; } =20 static void ublk_deinit_queues(struct ublk_device *ub) --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD1EE27A12B for ; Fri, 21 Nov 2025 02:00:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690408; cv=none; b=CTMQzKVHjrNE6D2F6IUYNWc4jqJvyiDbQbqzV+gyOdhvira1qGUTCSigkTOFKEul4tnVtSf8B9e6Mf6socARAtdDDoc15Z9ldQWjZB2HrvXJ2f4AH16wuJhi35nWw2fJ7m4rp0krOzXP2W9TFUtlY0Gt1VTeKz3tcZMZ3bL5Lkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690408; c=relaxed/simple; bh=AbFJ1DV82wHy9v2F+sPPlfbaAqosms/ezZTSgb3MceA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IiPfEGPEx8+cf9bdNBSLdCgejxrpLL4vT4ikMX1hH3G7LwofUlFE0osWTKmfz078B+wiQ+1sohYsqO9dzz6ZbqZU0O31ZRagxg2+aG9hGbWP2HWcGsAk0hYqgEmAjFUbgNKXRai9MRHVRscOS+IpEfUtBB7SmW6z3Z9jz0pbugs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZVTAXrXF; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZVTAXrXF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690403; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MwI8giRM7oA7xbL4F/ys7eykoL20MQlNafaZl3D58sA=; b=ZVTAXrXFAdkNcSuHluspValKH3zw27sH/iEUXnW0s1XetbmYV7arTibA9z/AdPDbehhewK VT4VquPNYghfIRPBp7jLYD8Juf8V/li1ifaxb7sLlpv+46RBNgqNXmS/ssD5HFRrTx11Pi /DRU1lkeMbb9vHRWxRXX0pqT9KK3hjA= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-651-4UpknkH3N9aKno40MONbqw-1; Thu, 20 Nov 2025 20:59:57 -0500 X-MC-Unique: 4UpknkH3N9aKno40MONbqw-1 X-Mimecast-MFC-AGG-ID: 4UpknkH3N9aKno40MONbqw_1763690396 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 36F951800342; Fri, 21 Nov 2025 01:59:56 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5190E1800451; Fri, 21 Nov 2025 01:59:54 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 13/27] ublk: add batch I/O dispatch infrastructure Date: Fri, 21 Nov 2025 09:58:35 +0800 Message-ID: <20251121015851.3672073-14-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add infrastructure for delivering I/O commands to ublk server in batches, preparing for the upcoming UBLK_U_IO_FETCH_IO_CMDS feature. Key components: - struct ublk_batch_fcmd: Represents a batch fetch uring_cmd that will receive multiple I/O tags in a single operation, using io_uring's multishot command for efficient ublk IO delivery. - ublk_batch_dispatch(): Batch version of ublk_dispatch_req() that: * Pulls multiple request tags from the events FIFO (lock-free reader) * Prepares each I/O for delivery (including auto buffer registration) * Delivers tags to userspace via single uring_cmd notification * Handles partial failures by restoring undelivered tags to FIFO The batch approach significantly reduces notification overhead by aggregati= ng multiple I/O completions into single uring_cmd, while maintaining the same I/O processing semantics as individual operations. Error handling ensures system consistency: if buffer selection or CQE posting fails, undelivered tags are restored to the FIFO for retry, meantime IO state has to be restored. This runs in task work context, scheduled via io_uring_cmd_complete_in_task= () or called directly from ->uring_cmd(), enabling efficient batch processing without blocking the I/O submission path. Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 189 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 6ff284243630..cc9c92d97349 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -91,6 +91,12 @@ UBLK_BATCH_F_HAS_BUF_ADDR | \ UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) =20 +/* ublk batch fetch uring_cmd */ +struct ublk_batch_fcmd { + struct io_uring_cmd *cmd; + unsigned short buf_group; +}; + struct ublk_uring_cmd_pdu { /* * Store requests in same batch temporarily for queuing them to @@ -168,6 +174,9 @@ struct ublk_batch_io_data { */ #define UBLK_REFCOUNT_INIT (REFCOUNT_MAX / 2) =20 +/* used for UBLK_F_BATCH_IO only */ +#define UBLK_BATCH_IO_UNUSED_TAG ((unsigned short)-1) + union ublk_io_buf { __u64 addr; struct ublk_auto_buf_reg auto_reg; @@ -616,6 +625,32 @@ static wait_queue_head_t ublk_idr_wq; /* wait until on= e idr is freed */ static DEFINE_MUTEX(ublk_ctl_mutex); =20 =20 +static void ublk_batch_deinit_fetch_buf(const struct ublk_batch_io_data *d= ata, + struct ublk_batch_fcmd *fcmd, + int res) +{ + io_uring_cmd_done(fcmd->cmd, res, data->issue_flags); + fcmd->cmd =3D NULL; +} + +static int ublk_batch_fetch_post_cqe(struct ublk_batch_fcmd *fcmd, + struct io_br_sel *sel, + unsigned int issue_flags) +{ + if (io_uring_mshot_cmd_post_cqe(fcmd->cmd, sel, issue_flags)) + return -ENOBUFS; + return 0; +} + +static ssize_t ublk_batch_copy_io_tags(struct ublk_batch_fcmd *fcmd, + void __user *buf, const u16 *tag_buf, + unsigned int len) +{ + if (copy_to_user(buf, tag_buf, len)) + return -EFAULT; + return len; +} + #define UBLK_MAX_UBLKS UBLK_MINORS =20 /* @@ -1378,6 +1413,160 @@ static void ublk_dispatch_req(struct ublk_queue *ub= q, } } =20 +static bool __ublk_batch_prep_dispatch(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + unsigned short tag) +{ + struct ublk_device *ub =3D data->ub; + struct ublk_io *io =3D &ubq->ios[tag]; + struct request *req =3D blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], tag= ); + enum auto_buf_reg_res res =3D AUTO_BUF_REG_FALLBACK; + struct io_uring_cmd *cmd =3D data->cmd; + + if (!ublk_start_io(ubq, req, io)) + return false; + + if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) + res =3D __ublk_do_auto_buf_reg(ubq, req, io, cmd, + data->issue_flags); + + if (res =3D=3D AUTO_BUF_REG_FAIL) + return false; + + ublk_io_lock(io); + ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res); + ublk_io_unlock(io); + + return true; +} + +static bool ublk_batch_prep_dispatch(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + unsigned short *tag_buf, + unsigned int len) +{ + bool has_unused =3D false; + int i; + + for (i =3D 0; i < len; i +=3D 1) { + unsigned short tag =3D tag_buf[i]; + + if (!__ublk_batch_prep_dispatch(ubq, data, tag)) { + tag_buf[i] =3D UBLK_BATCH_IO_UNUSED_TAG; + has_unused =3D true; + } + } + + return has_unused; +} + +/* + * Filter out UBLK_BATCH_IO_UNUSED_TAG entries from tag_buf. + * Returns the new length after filtering. + */ +static unsigned int ublk_filter_unused_tags(unsigned short *tag_buf, + unsigned int len) +{ + unsigned int i, j; + + for (i =3D 0, j =3D 0; i < len; i++) { + if (tag_buf[i] !=3D UBLK_BATCH_IO_UNUSED_TAG) { + if (i !=3D j) + tag_buf[j] =3D tag_buf[i]; + j++; + } + } + + return j; +} + +#define MAX_NR_TAG 128 +static int __ublk_batch_dispatch(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + struct ublk_batch_fcmd *fcmd) +{ + unsigned short tag_buf[MAX_NR_TAG]; + struct io_br_sel sel; + size_t len =3D 0; + bool needs_filter; + int ret; + + sel =3D io_uring_cmd_buffer_select(fcmd->cmd, fcmd->buf_group, &len, + data->issue_flags); + if (sel.val < 0) + return sel.val; + if (!sel.addr) + return -ENOBUFS; + + /* single reader needn't lock and sizeof(kfifo element) is 2 bytes */ + len =3D min(len, sizeof(tag_buf)) / 2; + len =3D kfifo_out(&ubq->evts_fifo, tag_buf, len); + + needs_filter =3D ublk_batch_prep_dispatch(ubq, data, tag_buf, len); + /* Filter out unused tags before posting to userspace */ + if (unlikely(needs_filter)) { + int new_len =3D ublk_filter_unused_tags(tag_buf, len); + + if (!new_len) + return len; + len =3D new_len; + } + + sel.val =3D ublk_batch_copy_io_tags(fcmd, sel.addr, tag_buf, len * 2); + ret =3D ublk_batch_fetch_post_cqe(fcmd, &sel, data->issue_flags); + if (unlikely(ret < 0)) { + int i, res; + + /* + * Undo prep state for all IOs since userspace never received them. + * This restores IOs to pre-prepared state so they can be cleanly + * re-prepared when tags are pulled from FIFO again. + */ + for (i =3D 0; i < len; i++) { + struct ublk_io *io =3D &ubq->ios[tag_buf[i]]; + int index =3D -1; + + ublk_io_lock(io); + if (io->flags & UBLK_IO_FLAG_AUTO_BUF_REG) + index =3D io->buf.auto_reg.index; + io->flags &=3D ~(UBLK_IO_FLAG_OWNED_BY_SRV | UBLK_IO_FLAG_AUTO_BUF_REG); + io->flags |=3D UBLK_IO_FLAG_ACTIVE; + ublk_io_unlock(io); + + if (index !=3D -1) + io_buffer_unregister_bvec(data->cmd, index, + data->issue_flags); + } + + res =3D kfifo_in_spinlocked_noirqsave(&ubq->evts_fifo, + tag_buf, len, &ubq->evts_lock); + + pr_warn("%s: copy tags or post CQE failure, move back " + "tags(%d %zu) ret %d\n", __func__, res, len, + ret); + } + return ret; +} + +static __maybe_unused int +ublk_batch_dispatch(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, + struct ublk_batch_fcmd *fcmd) +{ + int ret =3D 0; + + while (!ublk_io_evts_empty(ubq)) { + ret =3D __ublk_batch_dispatch(ubq, data, fcmd); + if (ret <=3D 0) + break; + } + + if (ret < 0) + ublk_batch_deinit_fetch_buf(data, fcmd, ret); + + return ret; +} + static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd, unsigned int issue_flags) { --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD183211290 for ; Fri, 21 Nov 2025 02:00:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690409; cv=none; b=jh9BkWSX3nVvZRszW0vfxK3cSqCB+i0YzqsnixQOK5pikAP5p5YUGZkQpyxvqpIzR+WPA9+6z0nRr0C7Sqg+AEGLxIgwkxmMroYi97+8CQRy+IA0Hv4P3LKeT3h7CSyCdhUolw8f3TpSeeZ4cFHv5AZghhujfreTHcXJAGXgFv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690409; c=relaxed/simple; bh=WiGU+H/f5NrkMFceg+uWmAyESSawMBJPo2JD0liGf1U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A5eJboHxFbff4HT9W0Sluhwpk10+zamtnR0jUv0Xm+PTgKtEOhmVYtbg9e8uO5aqbqSGYQLRAhrrzM3jzKPgBNL5LMlet9aIn5Z9+/rRcMCHNjMK/1KvO4bhmRz05IrmE47hlKdch/362RavduniEJnddRKVKs4IVHf5EmxjCeg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RnpE9xAs; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RnpE9xAs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690405; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cwvE/8uniR6ddqYG6Zi/4OpYxngBgoC22WJDKYyPRgY=; b=RnpE9xAsO2OGQX9TebJr2XJLcLO//ykST0qJbt3E4Aozj3wjniQHoDNP0ytDMxxsU+dK25 2mv202G0NuyqikuyyinqkUBgfOjDf9I5S44NgcxwIGsyuzb9b9acLHNOKycOPpjiG5smNf xWD9YI9OpAlENF+xd8p7UvbpJ8Bk95g= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627-5Rm9G9p0OSqE_MDbV-aQaw-1; Thu, 20 Nov 2025 21:00:01 -0500 X-MC-Unique: 5Rm9G9p0OSqE_MDbV-aQaw-1 X-Mimecast-MFC-AGG-ID: 5Rm9G9p0OSqE_MDbV-aQaw_1763690400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E47E81800359; Fri, 21 Nov 2025 01:59:59 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1761E1800451; Fri, 21 Nov 2025 01:59:58 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 14/27] ublk: add UBLK_U_IO_FETCH_IO_CMDS for batch I/O processing Date: Fri, 21 Nov 2025 09:58:36 +0800 Message-ID: <20251121015851.3672073-15-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add UBLK_U_IO_FETCH_IO_CMDS command to enable efficient batch processing of I/O requests. This multishot uring_cmd allows the ublk server to fetch multiple I/O commands in a single operation, significantly reducing submission overhead compared to individual FETCH_REQ* commands. Key Design Features: 1. Multishot Operation: One UBLK_U_IO_FETCH_IO_CMDS can fetch many I/O commands, with the batch size limited by the provided buffer length. 2. Dynamic Load Balancing: Multiple fetch commands can be submitted simultaneously, but only one is active at any time. This enables efficient load distribution across multiple server task contexts. 3. Implicit State Management: The implementation uses three key variables to track state: - evts_fifo: Queue of request tags awaiting processing - fcmd_head: List of available fetch commands - active_fcmd: Currently active fetch command (NULL =3D none active) States are derived implicitly: - IDLE: No fetch commands available - READY: Fetch commands available, none active - ACTIVE: One fetch command processing events 4. Lockless Reader Optimization: The active fetch command can read from evts_fifo without locking (single reader guarantee), while writers (ublk_queue_rq/ublk_queue_rqs) use evts_lock protection. The memory barrier pairing plays key role for the single lockless reader optimization. Implementation Details: - ublk_queue_rq() and ublk_queue_rqs() save request tags to evts_fifo - __ublk_pick_active_fcmd() selects an available fetch command when events arrive and no command is currently active - ublk_batch_dispatch() moves tags from evts_fifo to the fetch command's buffer and posts completion via io_uring_mshot_cmd_post_cqe() - State transitions are coordinated via evts_lock to maintain consistency Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 412 +++++++++++++++++++++++++++++++--- include/uapi/linux/ublk_cmd.h | 7 + 2 files changed, 388 insertions(+), 31 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index cc9c92d97349..2e5e392c939e 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -93,6 +93,7 @@ =20 /* ublk batch fetch uring_cmd */ struct ublk_batch_fcmd { + struct list_head node; struct io_uring_cmd *cmd; unsigned short buf_group; }; @@ -117,7 +118,10 @@ struct ublk_uring_cmd_pdu { */ struct ublk_queue *ubq; =20 - u16 tag; + union { + u16 tag; + struct ublk_batch_fcmd *fcmd; /* batch io only */ + }; }; =20 struct ublk_batch_io_data { @@ -229,18 +233,36 @@ struct ublk_queue { struct ublk_device *dev; =20 /* - * Inflight ublk request tag is saved in this fifo + * Batch I/O State Management: + * + * The batch I/O system uses implicit state management based on the + * combination of three key variables below. + * + * - IDLE: list_empty(&fcmd_head) && !active_fcmd + * No fetch commands available, events queue in evts_fifo + * + * - READY: !list_empty(&fcmd_head) && !active_fcmd + * Fetch commands available but none processing events * - * There are multiple writer from ublk_queue_rq() or ublk_queue_rqs(), - * so lock is required for storing request tag to fifo + * - ACTIVE: active_fcmd + * One fetch command actively processing events from evts_fifo * - * Make sure just one reader for fetching request from task work - * function to ublk server, so no need to grab the lock in reader - * side. + * Key Invariants: + * - At most one active_fcmd at any time (single reader) + * - active_fcmd is always from fcmd_head list when non-NULL + * - evts_fifo can be read locklessly by the single active reader + * - All state transitions require evts_lock protection + * - Multiple writers to evts_fifo require lock protection */ struct { DECLARE_KFIFO_PTR(evts_fifo, unsigned short); spinlock_t evts_lock; + + /* List of fetch commands available to process events */ + struct list_head fcmd_head; + + /* Currently active fetch command (NULL =3D none active) */ + struct ublk_batch_fcmd *active_fcmd; }____cacheline_aligned_in_smp; =20 struct ublk_io ios[] __counted_by(q_depth); @@ -292,12 +314,20 @@ static void ublk_abort_queue(struct ublk_device *ub, = struct ublk_queue *ubq); static inline struct request *__ublk_check_and_get_req(struct ublk_device = *ub, u16 q_id, u16 tag, struct ublk_io *io, size_t offset); static inline unsigned int ublk_req_build_flags(struct request *req); +static void ublk_batch_dispatch(struct ublk_queue *ubq, + struct ublk_batch_io_data *data, + struct ublk_batch_fcmd *fcmd); =20 static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub) { return false; } =20 +static inline bool ublk_support_batch_io(const struct ublk_queue *ubq) +{ + return false; +} + static inline void ublk_io_lock(struct ublk_io *io) { spin_lock(&io->lock); @@ -624,13 +654,45 @@ static wait_queue_head_t ublk_idr_wq; /* wait until o= ne idr is freed */ =20 static DEFINE_MUTEX(ublk_ctl_mutex); =20 +static struct ublk_batch_fcmd * +ublk_batch_alloc_fcmd(struct io_uring_cmd *cmd) +{ + struct ublk_batch_fcmd *fcmd =3D kzalloc(sizeof(*fcmd), GFP_NOIO); + + if (fcmd) { + fcmd->cmd =3D cmd; + fcmd->buf_group =3D READ_ONCE(cmd->sqe->buf_index); + } + return fcmd; +} + +static void ublk_batch_free_fcmd(struct ublk_batch_fcmd *fcmd) +{ + kfree(fcmd); +} + +static void __ublk_release_fcmd(struct ublk_queue *ubq) +{ + WRITE_ONCE(ubq->active_fcmd, NULL); +} =20 -static void ublk_batch_deinit_fetch_buf(const struct ublk_batch_io_data *d= ata, +/* + * Nothing can move on, so clear ->active_fcmd, and the caller should stop + * dispatching + */ +static void ublk_batch_deinit_fetch_buf(struct ublk_queue *ubq, + const struct ublk_batch_io_data *data, struct ublk_batch_fcmd *fcmd, int res) { + spin_lock(&ubq->evts_lock); + list_del(&fcmd->node); + WARN_ON_ONCE(fcmd !=3D ubq->active_fcmd); + __ublk_release_fcmd(ubq); + spin_unlock(&ubq->evts_lock); + io_uring_cmd_done(fcmd->cmd, res, data->issue_flags); - fcmd->cmd =3D NULL; + ublk_batch_free_fcmd(fcmd); } =20 static int ublk_batch_fetch_post_cqe(struct ublk_batch_fcmd *fcmd, @@ -1491,6 +1553,8 @@ static int __ublk_batch_dispatch(struct ublk_queue *u= bq, bool needs_filter; int ret; =20 + WARN_ON_ONCE(data->cmd !=3D fcmd->cmd); + sel =3D io_uring_cmd_buffer_select(fcmd->cmd, fcmd->buf_group, &len, data->issue_flags); if (sel.val < 0) @@ -1548,23 +1612,94 @@ static int __ublk_batch_dispatch(struct ublk_queue = *ubq, return ret; } =20 -static __maybe_unused int -ublk_batch_dispatch(struct ublk_queue *ubq, - const struct ublk_batch_io_data *data, - struct ublk_batch_fcmd *fcmd) +static struct ublk_batch_fcmd *__ublk_acquire_fcmd( + struct ublk_queue *ubq) +{ + struct ublk_batch_fcmd *fcmd; + + lockdep_assert_held(&ubq->evts_lock); + + /* + * Ordering updating ubq->evts_fifo and checking ubq->active_fcmd. + * + * The pair is the smp_mb() in ublk_batch_dispatch(). + * + * If ubq->active_fcmd is observed as non-NULL, the new added tags + * can be visisible in ublk_batch_dispatch() with the barrier pairing. + */ + smp_mb(); + if (READ_ONCE(ubq->active_fcmd)) { + fcmd =3D NULL; + } else { + fcmd =3D list_first_entry_or_null(&ubq->fcmd_head, + struct ublk_batch_fcmd, node); + WRITE_ONCE(ubq->active_fcmd, fcmd); + } + return fcmd; +} + +static void ublk_batch_tw_cb(struct io_uring_cmd *cmd, + unsigned int issue_flags) +{ + struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + struct ublk_batch_fcmd *fcmd =3D pdu->fcmd; + struct ublk_batch_io_data data =3D { + .ub =3D pdu->ubq->dev, + .cmd =3D fcmd->cmd, + .issue_flags =3D issue_flags, + }; + + WARN_ON_ONCE(pdu->ubq->active_fcmd !=3D fcmd); + + ublk_batch_dispatch(pdu->ubq, &data, fcmd); +} + +static void ublk_batch_dispatch(struct ublk_queue *ubq, + struct ublk_batch_io_data *data, + struct ublk_batch_fcmd *fcmd) { + struct ublk_batch_fcmd *new_fcmd; + void *handle; + bool empty; int ret =3D 0; =20 +again: while (!ublk_io_evts_empty(ubq)) { ret =3D __ublk_batch_dispatch(ubq, data, fcmd); if (ret <=3D 0) break; } =20 - if (ret < 0) - ublk_batch_deinit_fetch_buf(data, fcmd, ret); + if (ret < 0) { + ublk_batch_deinit_fetch_buf(ubq, data, fcmd, ret); + return; + } =20 - return ret; + handle =3D io_uring_cmd_ctx_handle(fcmd->cmd); + __ublk_release_fcmd(ubq); + /* + * Order clearing ubq->active_fcmd from __ublk_release_fcmd() and + * checking ubq->evts_fifo. + * + * The pair is the smp_mb() in __ublk_acquire_fcmd(). + */ + smp_mb(); + empty =3D ublk_io_evts_empty(ubq); + if (likely(empty)) + return; + + spin_lock(&ubq->evts_lock); + new_fcmd =3D __ublk_acquire_fcmd(ubq); + spin_unlock(&ubq->evts_lock); + + if (!new_fcmd) + return; + if (handle =3D=3D io_uring_cmd_ctx_handle(new_fcmd->cmd)) { + data->cmd =3D new_fcmd->cmd; + fcmd =3D new_fcmd; + goto again; + } + io_uring_cmd_complete_in_task(new_fcmd->cmd, ublk_batch_tw_cb); } =20 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd, @@ -1576,13 +1711,27 @@ static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd, ublk_dispatch_req(ubq, pdu->req, issue_flags); } =20 -static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq) +static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq, boo= l last) { - struct io_uring_cmd *cmd =3D ubq->ios[rq->tag].cmd; - struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + if (ublk_support_batch_io(ubq)) { + unsigned short tag =3D rq->tag; + struct ublk_batch_fcmd *fcmd =3D NULL; =20 - pdu->req =3D rq; - io_uring_cmd_complete_in_task(cmd, ublk_cmd_tw_cb); + spin_lock(&ubq->evts_lock); + kfifo_put(&ubq->evts_fifo, tag); + if (last) + fcmd =3D __ublk_acquire_fcmd(ubq); + spin_unlock(&ubq->evts_lock); + + if (fcmd) + io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb); + } else { + struct io_uring_cmd *cmd =3D ubq->ios[rq->tag].cmd; + struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + + pdu->req =3D rq; + io_uring_cmd_complete_in_task(cmd, ublk_cmd_tw_cb); + } } =20 static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd, @@ -1600,14 +1749,44 @@ static void ublk_cmd_list_tw_cb(struct io_uring_cmd= *cmd, } while (rq); } =20 -static void ublk_queue_cmd_list(struct ublk_io *io, struct rq_list *l) +static void ublk_batch_queue_cmd_list(struct ublk_queue *ubq, struct rq_li= st *l) { - struct io_uring_cmd *cmd =3D io->cmd; - struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + unsigned short tags[MAX_NR_TAG]; + struct ublk_batch_fcmd *fcmd; + struct request *rq; + unsigned cnt =3D 0; + + spin_lock(&ubq->evts_lock); + rq_list_for_each(l, rq) { + tags[cnt++] =3D (unsigned short)rq->tag; + if (cnt >=3D MAX_NR_TAG) { + kfifo_in(&ubq->evts_fifo, tags, cnt); + cnt =3D 0; + } + } + if (cnt) + kfifo_in(&ubq->evts_fifo, tags, cnt); + fcmd =3D __ublk_acquire_fcmd(ubq); + spin_unlock(&ubq->evts_lock); =20 - pdu->req_list =3D rq_list_peek(l); rq_list_init(l); - io_uring_cmd_complete_in_task(cmd, ublk_cmd_list_tw_cb); + if (fcmd) + io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb); +} + +static void ublk_queue_cmd_list(struct ublk_queue *ubq, struct ublk_io *io, + struct rq_list *l, bool batch) +{ + if (batch) { + ublk_batch_queue_cmd_list(ubq, l); + } else { + struct io_uring_cmd *cmd =3D io->cmd; + struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + + pdu->req_list =3D rq_list_peek(l); + rq_list_init(l); + io_uring_cmd_complete_in_task(cmd, ublk_cmd_list_tw_cb); + } } =20 static enum blk_eh_timer_return ublk_timeout(struct request *rq) @@ -1686,7 +1865,7 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ct= x *hctx, return BLK_STS_OK; } =20 - ublk_queue_cmd(ubq, rq); + ublk_queue_cmd(ubq, rq, bd->last); return BLK_STS_OK; } =20 @@ -1698,11 +1877,25 @@ static inline bool ublk_belong_to_same_batch(const = struct ublk_io *io, (io->task =3D=3D io2->task); } =20 -static void ublk_queue_rqs(struct rq_list *rqlist) +static void ublk_commit_rqs(struct blk_mq_hw_ctx *hctx) +{ + struct ublk_queue *ubq =3D hctx->driver_data; + struct ublk_batch_fcmd *fcmd; + + spin_lock(&ubq->evts_lock); + fcmd =3D __ublk_acquire_fcmd(ubq); + spin_unlock(&ubq->evts_lock); + + if (fcmd) + io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb); +} + +static void __ublk_queue_rqs(struct rq_list *rqlist, bool batch) { struct rq_list requeue_list =3D { }; struct rq_list submit_list =3D { }; struct ublk_io *io =3D NULL; + struct ublk_queue *ubq =3D NULL; struct request *req; =20 while ((req =3D rq_list_pop(rqlist))) { @@ -1716,16 +1909,27 @@ static void ublk_queue_rqs(struct rq_list *rqlist) =20 if (io && !ublk_belong_to_same_batch(io, this_io) && !rq_list_empty(&submit_list)) - ublk_queue_cmd_list(io, &submit_list); + ublk_queue_cmd_list(ubq, io, &submit_list, batch); io =3D this_io; + ubq =3D this_q; rq_list_add_tail(&submit_list, req); } =20 if (!rq_list_empty(&submit_list)) - ublk_queue_cmd_list(io, &submit_list); + ublk_queue_cmd_list(ubq, io, &submit_list, batch); *rqlist =3D requeue_list; } =20 +static void ublk_queue_rqs(struct rq_list *rqlist) +{ + __ublk_queue_rqs(rqlist, false); +} + +static void ublk_batch_queue_rqs(struct rq_list *rqlist) +{ + __ublk_queue_rqs(rqlist, true); +} + static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data, unsigned int hctx_idx) { @@ -1743,6 +1947,14 @@ static const struct blk_mq_ops ublk_mq_ops =3D { .timeout =3D ublk_timeout, }; =20 +static const struct blk_mq_ops ublk_batch_mq_ops =3D { + .commit_rqs =3D ublk_commit_rqs, + .queue_rq =3D ublk_queue_rq, + .queue_rqs =3D ublk_batch_queue_rqs, + .init_hctx =3D ublk_init_hctx, + .timeout =3D ublk_timeout, +}; + static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *u= bq) { int i; @@ -2120,6 +2332,56 @@ static void ublk_cancel_cmd(struct ublk_queue *ubq, = unsigned tag, io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, issue_flags); } =20 +static void ublk_batch_cancel_cmd(struct ublk_queue *ubq, + struct ublk_batch_fcmd *fcmd, + unsigned int issue_flags) +{ + bool done; + + spin_lock(&ubq->evts_lock); + done =3D (ubq->active_fcmd !=3D fcmd); + if (done) + list_del(&fcmd->node); + spin_unlock(&ubq->evts_lock); + + if (done) { + io_uring_cmd_done(fcmd->cmd, UBLK_IO_RES_ABORT, issue_flags); + ublk_batch_free_fcmd(fcmd); + } +} + +static void ublk_batch_cancel_queue(struct ublk_queue *ubq) +{ + LIST_HEAD(fcmd_list); + + spin_lock(&ubq->evts_lock); + ubq->force_abort =3D true; + list_splice_init(&ubq->fcmd_head, &fcmd_list); + if (ubq->active_fcmd) + list_move(&ubq->active_fcmd->node, &ubq->fcmd_head); + spin_unlock(&ubq->evts_lock); + + while (!list_empty(&fcmd_list)) { + struct ublk_batch_fcmd *fcmd =3D list_first_entry(&fcmd_list, + struct ublk_batch_fcmd, node); + + ublk_batch_cancel_cmd(ubq, fcmd, IO_URING_F_UNLOCKED); + } +} + +static void ublk_batch_cancel_fn(struct io_uring_cmd *cmd, + unsigned int issue_flags) +{ + struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(cmd); + struct ublk_batch_fcmd *fcmd =3D pdu->fcmd; + struct ublk_queue *ubq =3D pdu->ubq; + + if (!ubq->canceling) + ublk_start_cancel(ubq->dev); + + ublk_batch_cancel_cmd(ubq, fcmd, issue_flags); +} + /* * The ublk char device won't be closed when calling cancel fn, so both * ublk device and queue are guaranteed to be live @@ -2171,6 +2433,11 @@ static void ublk_cancel_queue(struct ublk_queue *ubq) { int i; =20 + if (ublk_support_batch_io(ubq)) { + ublk_batch_cancel_queue(ubq); + return; + } + for (i =3D 0; i < ubq->q_depth; i++) ublk_cancel_cmd(ubq, i, IO_URING_F_UNLOCKED); } @@ -3091,6 +3358,74 @@ static int ublk_check_batch_cmd(const struct ublk_ba= tch_io_data *data) return ublk_check_batch_cmd_flags(uc); } =20 +static int ublk_batch_attach(struct ublk_queue *ubq, + struct ublk_batch_io_data *data, + struct ublk_batch_fcmd *fcmd) +{ + struct ublk_batch_fcmd *new_fcmd =3D NULL; + bool free =3D false; + + spin_lock(&ubq->evts_lock); + if (unlikely(ubq->force_abort || ubq->canceling)) { + free =3D true; + } else { + list_add_tail(&fcmd->node, &ubq->fcmd_head); + new_fcmd =3D __ublk_acquire_fcmd(ubq); + } + spin_unlock(&ubq->evts_lock); + + /* + * If the two fetch commands are originated from same io_ring_ctx, + * run batch dispatch directly. Otherwise, schedule task work for + * doing it. + */ + if (new_fcmd && io_uring_cmd_ctx_handle(new_fcmd->cmd) =3D=3D + io_uring_cmd_ctx_handle(fcmd->cmd)) { + data->cmd =3D new_fcmd->cmd; + ublk_batch_dispatch(ubq, data, new_fcmd); + } else if (new_fcmd) { + io_uring_cmd_complete_in_task(new_fcmd->cmd, + ublk_batch_tw_cb); + } + + if (free) { + ublk_batch_free_fcmd(fcmd); + return -ENODEV; + } + return -EIOCBQUEUED; +} + +static int ublk_handle_batch_fetch_cmd(struct ublk_batch_io_data *data) +{ + struct ublk_queue *ubq =3D ublk_get_queue(data->ub, data->header.q_id); + struct ublk_batch_fcmd *fcmd =3D ublk_batch_alloc_fcmd(data->cmd); + struct ublk_uring_cmd_pdu *pdu =3D ublk_get_uring_cmd_pdu(data->cmd); + + if (!fcmd) + return -ENOMEM; + + pdu->ubq =3D ubq; + pdu->fcmd =3D fcmd; + io_uring_cmd_mark_cancelable(data->cmd, data->issue_flags); + + return ublk_batch_attach(ubq, data, fcmd); +} + +static int ublk_validate_batch_fetch_cmd(struct ublk_batch_io_data *data, + const struct ublk_batch_io *uc) +{ + if (!(data->cmd->flags & IORING_URING_CMD_MULTISHOT)) + return -EINVAL; + + if (uc->elem_bytes !=3D sizeof(__u16)) + return -EINVAL; + + if (uc->flags !=3D 0) + return -E2BIG; + + return 0; +} + static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) { @@ -3113,6 +3448,11 @@ static int ublk_ch_batch_io_uring_cmd(struct io_urin= g_cmd *cmd, if (data.header.q_id >=3D ub->dev_info.nr_hw_queues) goto out; =20 + if (unlikely(issue_flags & IO_URING_F_CANCEL)) { + ublk_batch_cancel_fn(cmd, issue_flags); + return 0; + } + switch (cmd_op) { case UBLK_U_IO_PREP_IO_CMDS: ret =3D ublk_check_batch_cmd(&data); @@ -3126,6 +3466,12 @@ static int ublk_ch_batch_io_uring_cmd(struct io_urin= g_cmd *cmd, goto out; ret =3D ublk_handle_batch_commit_cmd(&data); break; + case UBLK_U_IO_FETCH_IO_CMDS: + ret =3D ublk_validate_batch_fetch_cmd(&data, uc); + if (ret) + goto out; + ret =3D ublk_handle_batch_fetch_cmd(&data); + break; default: ret =3D -EOPNOTSUPP; } @@ -3327,6 +3673,7 @@ static int ublk_init_queue(struct ublk_device *ub, in= t q_id) ret =3D ublk_io_evts_init(ubq, ubq->q_depth, numa_node); if (ret) goto fail; + INIT_LIST_HEAD(&ubq->fcmd_head); } ub->queues[q_id] =3D ubq; ubq->dev =3D ub; @@ -3451,7 +3798,10 @@ static void ublk_align_max_io_size(struct ublk_devic= e *ub) =20 static int ublk_add_tag_set(struct ublk_device *ub) { - ub->tag_set.ops =3D &ublk_mq_ops; + if (ublk_dev_support_batch_io(ub)) + ub->tag_set.ops =3D &ublk_batch_mq_ops; + else + ub->tag_set.ops =3D &ublk_mq_ops; ub->tag_set.nr_hw_queues =3D ub->dev_info.nr_hw_queues; ub->tag_set.queue_depth =3D ub->dev_info.queue_depth; ub->tag_set.numa_node =3D NUMA_NO_NODE; diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index 295ec8f34173..cd894c1d188e 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -120,6 +120,13 @@ #define UBLK_U_IO_COMMIT_IO_CMDS \ _IOWR('u', 0x26, struct ublk_batch_io) =20 +/* + * Fetch io commands to provided buffer in multishot style, + * `IORING_URING_CMD_MULTISHOT` is required for this command. + */ +#define UBLK_U_IO_FETCH_IO_CMDS \ + _IOWR('u', 0x27, struct ublk_batch_io) + /* only ABORT means that no re-fetch */ #define UBLK_IO_RES_OK 0 #define UBLK_IO_RES_NEED_GET_DATA 1 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C2C4150997 for ; Fri, 21 Nov 2025 02:00:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690412; cv=none; b=guBXA5x/o26OF1UNZmyx9SQlmn22sWP0XhASrFB+ExL+3jK64m6SNCJscQrBI+YMrDYUJK9+eNYgowR3zSi/VZpJpldgUR++c6aT+JXBAecllQoEcI14gC0NFcK5xo4Lpg70k2bE9ff5UvbkOVbQrGVnCuTfHmTiNGjIEslAzCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690412; c=relaxed/simple; bh=Gbvg+CMUY2qwHXspBmVcG3os7q7yA1lItaymIz9quJQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XnB94wuDuIWQ1utseE1N92/sNT5Fr3AYLRPYgtuNKDiLCqvcnQYUf61M7MGwLCuQ7nGJm/qlKwebcdSdtbSgAKeuNRdnt8cbaCfq9g/65fQUQocWuAGiY87lphQxY1Uu5WWvY2TJZoQNJkdxbdc/NKBpcovlmOo6idXIgH+xt9c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bS2tosAt; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bS2tosAt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TqanJXBXu9s6vrgb1WAyKqNpSdxT6oHl0Om7j+YSClQ=; b=bS2tosAtjMBanvHYPNdZNL7YBA+sv23wOXzbGkS8qvZ6Dx6+7udgnLIEqlCVwERci1TxCy zwV0KKfxsA2a/6wjfHL+zJsAFGDE1G/8j8X9BA/Mrk6R7Dj7iV157aC0d/h07+IwYyEnqy q3jUXGHVc59qFTqSLkNxYoIY0FVct84= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-241-7YGyixZBPpiTW5Dvuat1lA-1; Thu, 20 Nov 2025 21:00:05 -0500 X-MC-Unique: 7YGyixZBPpiTW5Dvuat1lA-1 X-Mimecast-MFC-AGG-ID: 7YGyixZBPpiTW5Dvuat1lA_1763690404 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E9F001956072; Fri, 21 Nov 2025 02:00:03 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CB2921956045; Fri, 21 Nov 2025 02:00:02 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 15/27] ublk: abort requests filled in event kfifo Date: Fri, 21 Nov 2025 09:58:37 +0800 Message-ID: <20251121015851.3672073-16-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" In case of BATCH_IO, any request filled in event kfifo, they don't get chance to be dispatched any more when releasing ublk char device, so we have to abort them too. Add ublk_abort_batch_queue() for aborting this kind of requests. Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 2e5e392c939e..849199771f86 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -2241,7 +2241,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_= area_struct *vma) static void __ublk_fail_req(struct ublk_device *ub, struct ublk_io *io, struct request *req) { - WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE); + WARN_ON_ONCE(!ublk_dev_support_batch_io(ub) && + io->flags & UBLK_IO_FLAG_ACTIVE); =20 if (ublk_nosrv_should_reissue_outstanding(ub)) blk_mq_requeue_request(req, false); @@ -2251,6 +2252,26 @@ static void __ublk_fail_req(struct ublk_device *ub, = struct ublk_io *io, } } =20 +/* + * Request tag may just be filled to event kfifo, not get chance to + * dispatch, abort these requests too + */ +static void ublk_abort_batch_queue(struct ublk_device *ub, + struct ublk_queue *ubq) +{ + while (true) { + struct request *req; + short tag; + + if (!kfifo_out(&ubq->evts_fifo, &tag, 1)) + break; + + req =3D blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], tag); + if (req && blk_mq_request_started(req)) + __ublk_fail_req(ub, &ubq->ios[tag], req); + } +} + /* * Called from ublk char device release handler, when any uring_cmd is * done, meantime request queue is "quiesced" since all inflight requests @@ -2269,6 +2290,9 @@ static void ublk_abort_queue(struct ublk_device *ub, = struct ublk_queue *ubq) if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV) __ublk_fail_req(ub, io, io->req); } + + if (ublk_support_batch_io(ubq)) + ublk_abort_batch_queue(ub, ubq); } =20 static void ublk_start_cancel(struct ublk_device *ub) --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E93427FD43 for ; Fri, 21 Nov 2025 02:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690419; cv=none; b=OiDKbOUH8XsS6x0pdK8zBk3X2i+/59cWtNuj/ozKR2kXH+T0cScTzkrGvOt5x5kcWr4emRCX21WNlTIaeSOTuMTlSkkmrsxyfMSmr36u5fOt5lxzIPV11oxrtvR9d8C7CvD+SPU2H084bawlAMDKlWh/ImsJwaC1CPpsej+LOJ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690419; c=relaxed/simple; bh=if3Ad8D9MLZgclpENQrpcBjRHrZ0O72fYyvYdbS9nD8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JVtIfZYzp5Ahn3FMTO+dNXqYN/DokgI5aDzsGJyT7uqwl3cCSSJoRoEBwVkO+owfJaId6JjN3qZMef8/eOmy/OUQ1vjvhNYWMFhPziGYOKPCMerjo7qSJaJ5yB8DdeANAhC2uLTJ9nFksvfoqi5mwqP0OxoltK7LEDXyZeCBgbk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZPHhB2y9; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZPHhB2y9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690416; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=APgPdgnWoBJ34YoCRwEfYGiHtu7xr5eSm1q+oTVRq1Y=; b=ZPHhB2y94CiZ93u+XRZsJiI7jtBtAlIUsPK8gOp4CDCt887xy2dTLidAmEgLUx1AKVAbR/ ezCQZIIZlK0zJ7o8b5Fe6tmE/3Ge/IkrqzEL5nFJWuNu6bhXXAtKjXpe0z3b4hKgXeXeGB NGtMFET3Cedf+fhDggGgywvXCfCfavE= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-130-982BNqtBMNK0LZjufiZ3wQ-1; Thu, 20 Nov 2025 21:00:09 -0500 X-MC-Unique: 982BNqtBMNK0LZjufiZ3wQ-1 X-Mimecast-MFC-AGG-ID: 982BNqtBMNK0LZjufiZ3wQ_1763690408 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E0ADA1956058; Fri, 21 Nov 2025 02:00:07 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E0D6C1940E82; Fri, 21 Nov 2025 02:00:06 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 16/27] ublk: add new feature UBLK_F_BATCH_IO Date: Fri, 21 Nov 2025 09:58:38 +0800 Message-ID: <20251121015851.3672073-17-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Add new feature UBLK_F_BATCH_IO which replaces the following two per-io commands: - UBLK_U_IO_FETCH_REQ - UBLK_U_IO_COMMIT_AND_FETCH_REQ with three per-queue batch io uring_cmd: - UBLK_U_IO_PREP_IO_CMDS - UBLK_U_IO_COMMIT_IO_CMDS - UBLK_U_IO_FETCH_IO_CMDS Then ublk can deliver batch io commands to ublk server in single multishort uring_cmd, also allows to prepare & commit multiple commands in batch style via single uring_cmd, communication cost is reduced a lot. This feature also doesn't limit task context any more for all supported commands, so any allowed uring_cmd can be issued in any task context. ublk server implementation becomes much easier. Meantime load balance becomes much easier to support with this feature. The command `UBLK_U_IO_FETCH_IO_CMDS` can be issued from multiple task contexts, so each task can adjust this command's buffer length or number of inflight commands for controlling how much load is handled by current task. Later, priority parameter will be added to command `UBLK_U_IO_FETCH_IO_CMDS` for improving load balance support. UBLK_U_IO_GET_DATA isn't supported in batch io yet, but it may be enabled in future via its batch pair. Signed-off-by: Ming Lei --- drivers/block/ublk_drv.c | 58 ++++++++++++++++++++++++++++++++--- include/uapi/linux/ublk_cmd.h | 16 ++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 849199771f86..90cd1863bc83 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -74,7 +74,8 @@ | UBLK_F_AUTO_BUF_REG \ | UBLK_F_QUIESCE \ | UBLK_F_PER_IO_DAEMON \ - | UBLK_F_BUF_REG_OFF_DAEMON) + | UBLK_F_BUF_REG_OFF_DAEMON \ + | UBLK_F_BATCH_IO) =20 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \ | UBLK_F_USER_RECOVERY_REISSUE \ @@ -320,12 +321,12 @@ static void ublk_batch_dispatch(struct ublk_queue *ub= q, =20 static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub) { - return false; + return ub->dev_info.flags & UBLK_F_BATCH_IO; } =20 static inline bool ublk_support_batch_io(const struct ublk_queue *ubq) { - return false; + return ubq->flags & UBLK_F_BATCH_IO; } =20 static inline void ublk_io_lock(struct ublk_io *io) @@ -3450,6 +3451,41 @@ static int ublk_validate_batch_fetch_cmd(struct ublk= _batch_io_data *data, return 0; } =20 +static int ublk_handle_non_batch_cmd(struct io_uring_cmd *cmd, + unsigned int issue_flags) +{ + const struct ublksrv_io_cmd *ub_cmd =3D io_uring_sqe_cmd(cmd->sqe); + struct ublk_device *ub =3D cmd->file->private_data; + unsigned tag =3D READ_ONCE(ub_cmd->tag); + unsigned q_id =3D READ_ONCE(ub_cmd->q_id); + unsigned index =3D READ_ONCE(ub_cmd->addr); + struct ublk_queue *ubq; + struct ublk_io *io; + int ret =3D -EINVAL; + + if (!ub) + return ret; + + if (q_id >=3D ub->dev_info.nr_hw_queues) + return ret; + + ubq =3D ublk_get_queue(ub, q_id); + if (tag >=3D ubq->q_depth) + return ret; + + io =3D &ubq->ios[tag]; + + switch (cmd->cmd_op) { + case UBLK_U_IO_REGISTER_IO_BUF: + return ublk_register_io_buf(cmd, ub, q_id, tag, io, index, + issue_flags); + case UBLK_U_IO_UNREGISTER_IO_BUF: + return ublk_unregister_io_buf(cmd, ub, index, issue_flags); + default: + return -EOPNOTSUPP; + } +} + static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) { @@ -3497,7 +3533,8 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring= _cmd *cmd, ret =3D ublk_handle_batch_fetch_cmd(&data); break; default: - ret =3D -EOPNOTSUPP; + ret =3D ublk_handle_non_batch_cmd(cmd, issue_flags); + break; } out: return ret; @@ -4163,9 +4200,13 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ct= rl_cmd *header) =20 ub->dev_info.flags |=3D UBLK_F_CMD_IOCTL_ENCODE | UBLK_F_URING_CMD_COMP_IN_TASK | - UBLK_F_PER_IO_DAEMON | + (ublk_dev_support_batch_io(ub) ? 0 : UBLK_F_PER_IO_DAEMON) | UBLK_F_BUF_REG_OFF_DAEMON; =20 + /* So far, UBLK_F_PER_IO_DAEMON won't be exposed for BATCH_IO */ + if (ublk_dev_support_batch_io(ub)) + ub->dev_info.flags &=3D ~UBLK_F_PER_IO_DAEMON; + /* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */ if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_BUF_REG)) @@ -4518,6 +4559,13 @@ static int ublk_wait_for_idle_io(struct ublk_device = *ub, unsigned int elapsed =3D 0; int ret; =20 + /* + * For UBLK_F_BATCH_IO ublk server can get notified with existing + * or new fetch command, so needn't wait any more + */ + if (ublk_dev_support_batch_io(ub)) + return 0; + while (elapsed < timeout_ms && !signal_pending(current)) { unsigned int queues_cancelable =3D 0; int i; diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index cd894c1d188e..5e8b1211b7f4 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -335,6 +335,22 @@ */ #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14) =20 + +/* + * Support the following commands for delivering & committing io command + * in batch. + * + * - UBLK_U_IO_PREP_IO_CMDS + * - UBLK_U_IO_COMMIT_IO_CMDS + * - UBLK_U_IO_FETCH_IO_CMDS + * - UBLK_U_IO_REGISTER_IO_BUF + * - UBLK_U_IO_UNREGISTER_IO_BUF + * + * The existing UBLK_U_IO_FETCH_REQ, UBLK_U_IO_COMMIT_AND_FETCH_REQ and + * UBLK_U_IO_GET_DATA uring_cmd are not supported for this feature. + */ +#define UBLK_F_BATCH_IO (1ULL << 15) + /* device state */ #define UBLK_S_DEV_DEAD 0 #define UBLK_S_DEV_LIVE 1 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECFCF2C11FE for ; Fri, 21 Nov 2025 02:00:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690422; cv=none; b=E5c0HJW7QG+j26bfYk0echcVMYULQ9ZlICWiuwI7C45E7xGV/12fHGoLooFSE1ts0/hRpofT4GS5pJsFUtGgKHODFfcf2saZ2imEZuF4JlWfwSZBFBVIuqlILtSxxAW2U46HTZKJ7VDjfdEHlxDKa7+RPyrd3Cqut++nHZbC1gs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690422; c=relaxed/simple; bh=W24sPvXLKsWUMmTQYF5re5V1HVlHHFAvi1CEPoSR5Qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=npeNjPP46JsboWefR2Obwn0T18b0pyF58RlppcG5CZ27edjsNrG2e8R7b8SHQBR5WBUhDvU6SbhdiJOEpBduWAR80HlV0GDGqoM444mtwxYtk3Yc1NTfp6m1LJZEuJoH4wFsIdkSiq1p8Ziy9Z8eX6ziaz9g9PODus23OtGGoFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aGeG95fn; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aGeG95fn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690420; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gBz4WFDn1U6Wgd37woEVm5QcxS/1722xZKJHV9Um/4s=; b=aGeG95fnrpGfg1CKQExqEU5l5eXdAS1+feSdqs1mPzCZOjXDlmtnnDrj8iW0qRggDy5s82 dnga1WhV4q1py7V8aRNu1JUnkbt2jQ7LAwsB2o5aV0eydk0AbZmFOzksubwINkCxzu/3HI OgqSvFKfgbkePe6OFjgDlnN9NOeLPYg= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-352-v7SR3R6MOhKf9EmG-aSIbw-1; Thu, 20 Nov 2025 21:00:14 -0500 X-MC-Unique: v7SR3R6MOhKf9EmG-aSIbw-1 X-Mimecast-MFC-AGG-ID: v7SR3R6MOhKf9EmG-aSIbw_1763690413 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F398D1954204; Fri, 21 Nov 2025 02:00:12 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BC9D718004A3; Fri, 21 Nov 2025 02:00:11 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 17/27] ublk: document feature UBLK_F_BATCH_IO Date: Fri, 21 Nov 2025 09:58:39 +0800 Message-ID: <20251121015851.3672073-18-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Document feature UBLK_F_BATCH_IO. Signed-off-by: Ming Lei --- Documentation/block/ublk.rst | 60 +++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/Documentation/block/ublk.rst b/Documentation/block/ublk.rst index 8c4030bcabb6..09a5604f8e10 100644 --- a/Documentation/block/ublk.rst +++ b/Documentation/block/ublk.rst @@ -260,9 +260,12 @@ The following IO commands are communicated via io_urin= g passthrough command, and each command is only for forwarding the IO and committing the result with specified IO tag in the command data: =20 -- ``UBLK_IO_FETCH_REQ`` +Traditional Per-I/O Commands +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ =20 - Sent from the server IO pthread for fetching future incoming IO requests +- ``UBLK_U_IO_FETCH_REQ`` + + Sent from the server I/O pthread for fetching future incoming I/O reques= ts destined to ``/dev/ublkb*``. This command is sent only once from the ser= ver IO pthread for ublk driver to setup IO forward environment. =20 @@ -278,7 +281,7 @@ with specified IO tag in the command data: supported by the driver, daemons must be per-queue instead - i.e. all I/= Os associated to a single qid must be handled by the same task. =20 -- ``UBLK_IO_COMMIT_AND_FETCH_REQ`` +- ``UBLK_U_IO_COMMIT_AND_FETCH_REQ`` =20 When an IO request is destined to ``/dev/ublkb*``, the driver stores the IO's ``ublksrv_io_desc`` to the specified mapped area; then the @@ -293,7 +296,7 @@ with specified IO tag in the command data: requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ`` is reused for both fetching request and committing back IO result. =20 -- ``UBLK_IO_NEED_GET_DATA`` +- ``UBLK_U_IO_NEED_GET_DATA`` =20 With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly issued to ublk server without data copy. Then, IO backend of ublk server @@ -322,6 +325,55 @@ with specified IO tag in the command data: ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy the server buffer (pages) read to the IO request pages. =20 +Batch I/O Commands (UBLK_F_BATCH_IO) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The ``UBLK_F_BATCH_IO`` feature provides an alternative high-performance +I/O handling model that replaces the traditional per-I/O commands with +per-queue batch commands. This significantly reduces communication overhead +and enables better load balancing across multiple server tasks. + +Key differences from traditional mode: + +- **Per-queue vs Per-I/O**: Commands operate on queues rather than individ= ual I/Os +- **Batch processing**: Multiple I/Os are handled in single operations +- **Multishot commands**: Use io_uring multishot for reduced submission ov= erhead +- **Flexible task assignment**: Any task can handle any I/O (no per-I/O da= emons) +- **Better load balancing**: Tasks can adjust their workload dynamically + +Batch I/O Commands: + +- ``UBLK_U_IO_PREP_IO_CMDS`` + + Prepares multiple I/O commands in batch. The server provides a buffer + containing multiple I/O descriptors that will be processed together. + This reduces the number of individual command submissions required. + +- ``UBLK_U_IO_COMMIT_IO_CMDS`` + + Commits results for multiple I/O operations in batch. The server provides + a buffer containing the results of multiple completed I/Os, allowing + efficient bulk completion of requests. + +- ``UBLK_U_IO_FETCH_IO_CMDS`` + + **Multishot command** for fetching I/O commands in batch. This is the key + command that enables high-performance batch processing: + + * Uses io_uring multishot capability for reduced submission overhead + * Single command can fetch multiple I/O requests over time + * Buffer size determines maximum batch size per operation + * Multiple fetch commands can be submitted for load balancing + * Only one fetch command is active at any time per queue + * Supports dynamic load balancing across multiple server tasks + + Each task can submit ``UBLK_U_IO_FETCH_IO_CMDS`` with different buffer + sizes to control how much work it handles. This enables sophisticated + load balancing strategies in multi-threaded servers. + +Migration: Applications using traditional commands (``UBLK_U_IO_FETCH_REQ`= `, +``UBLK_U_IO_COMMIT_AND_FETCH_REQ``) cannot use batch mode simultaneously. + Zero copy --------- =20 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21FAA2D130B for ; Fri, 21 Nov 2025 02:00:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690425; cv=none; b=V69Unu2PubJXXbwgBU2jEY4F88pMQNx2+v4lUK53933U0qnWUPbznU9/tIH/qoNMXRlwVRnXLFLGw/edoqaslm5hY/G5wjsHohGPSsrrobNpY01Fk8e9luA0RwhpwwbtQVOn/8NYGP9M+BfSkOmOKAf+MCEyzK+5Cdu8F6yU9ws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690425; c=relaxed/simple; bh=lZFQkkcIQFUAUsfrreKNnoPIzb8RfsdfF4lwfsW1fR8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TT0q9+GoCN+FOwDggBo41LlB5F1Z1pOCpA/9YfAXQ2KZNp8W88zIRNuriZ0H082GS3+8EP9SvC///G/64EffP/SrXkhJTaiQbZTFtNFzM/+5LPRYdArWCGU+fUY3sBef/3BmBWox30HDMqWD0p8olbJdNrgIp3kU2F0dy4gW2e0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eWlF22tS; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eWlF22tS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690423; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lYUBO2QVHXZ0L0CaVikfzhTp2/fJbjkXi40POyUJvkA=; b=eWlF22tSVkc4q0wWWtOYb74gJQxeQy3a/QHZskK9mgVsPAEmwCQhJqZD0jIgcx0AdrIiwC R3mKbEDlVFFueKultvsrBuUqJDRtPNhbynuMejGSIfFAtOYwzy1F5BoQl/9819xkwxgMx4 4VTGwdherQ4/syjwf1uMxSmyIMblgjA= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-481-s7UGkC_MMXKZVHrmbcZyQw-1; Thu, 20 Nov 2025 21:00:18 -0500 X-MC-Unique: s7UGkC_MMXKZVHrmbcZyQw-1 X-Mimecast-MFC-AGG-ID: s7UGkC_MMXKZVHrmbcZyQw_1763690417 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DF583180047F; Fri, 21 Nov 2025 02:00:16 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0733D1956045; Fri, 21 Nov 2025 02:00:15 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 18/27] ublk: implement batch request completion via blk_mq_end_request_batch() Date: Fri, 21 Nov 2025 09:58:40 +0800 Message-ID: <20251121015851.3672073-19-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Reduce overhead when completing multiple requests in batch I/O mode by accumulating them in an io_comp_batch structure and completing them together via blk_mq_end_request_batch(). This minimizes per-request completion overhead and improves performance for high IOPS workloads. The implementation adds an io_comp_batch pointer to struct ublk_io and initializes it in __ublk_fetch(). For batch I/O, the pointer is set to the batch structure in ublk_batch_commit_io(). The __ublk_complete_rq() function uses io->iob to call blk_mq_add_to_batch() for batch mode. After processing all batch I/Os, the completion callback is invoked in ublk_handle_batch_commit_cmd() to complete all accumulated requests efficiently. So far just covers direct completion. For deferred completion(zero copy, auto buffer reg), ublk_io_release() is often delayed in freeing buffer consumer io_uring request's code path, so this patch often doesn't work, also it is hard to pass the per-task 'struct io_comp_batch' for deferred completion. Signed-off-by: Ming Lei Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 90cd1863bc83..a5606c7111a4 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -130,6 +130,7 @@ struct ublk_batch_io_data { struct io_uring_cmd *cmd; struct ublk_batch_io header; unsigned int issue_flags; + struct io_comp_batch *iob; }; =20 /* @@ -642,7 +643,12 @@ static blk_status_t ublk_setup_iod_zoned(struct ublk_q= ueue *ubq, #endif =20 static inline void __ublk_complete_rq(struct request *req, struct ublk_io = *io, - bool need_map); + bool need_map, struct io_comp_batch *iob); + +static void ublk_complete_batch(struct io_comp_batch *iob) +{ + blk_mq_end_request_batch(iob); +} =20 static dev_t ublk_chr_devt; static const struct class ublk_chr_class =3D { @@ -912,7 +918,7 @@ static inline void ublk_put_req_ref(struct ublk_io *io,= struct request *req) return; =20 /* ublk_need_map_io() and ublk_need_req_ref() are mutually exclusive */ - __ublk_complete_rq(req, io, false); + __ublk_complete_rq(req, io, false, NULL); } =20 static inline bool ublk_sub_req_ref(struct ublk_io *io) @@ -1251,7 +1257,7 @@ static inline struct ublk_uring_cmd_pdu *ublk_get_uri= ng_cmd_pdu( =20 /* todo: handle partial completion */ static inline void __ublk_complete_rq(struct request *req, struct ublk_io = *io, - bool need_map) + bool need_map, struct io_comp_batch *iob) { unsigned int unmapped_bytes; blk_status_t res =3D BLK_STS_OK; @@ -1288,8 +1294,11 @@ static inline void __ublk_complete_rq(struct request= *req, struct ublk_io *io, =20 if (blk_update_request(req, BLK_STS_OK, io->res)) blk_mq_requeue_request(req, true); - else if (likely(!blk_should_fake_timeout(req->q))) + else if (likely(!blk_should_fake_timeout(req->q))) { + if (blk_mq_add_to_batch(req, iob, false, ublk_complete_batch)) + return; __blk_mq_end_request(req, BLK_STS_OK); + } =20 return; exit: @@ -2249,7 +2258,7 @@ static void __ublk_fail_req(struct ublk_device *ub, s= truct ublk_io *io, blk_mq_requeue_request(req, false); else { io->res =3D -EIO; - __ublk_complete_rq(req, io, ublk_dev_need_map_io(ub)); + __ublk_complete_rq(req, io, ublk_dev_need_map_io(ub), NULL); } } =20 @@ -2986,7 +2995,7 @@ static int ublk_ch_uring_cmd_local(struct io_uring_cm= d *cmd, if (req_op(req) =3D=3D REQ_OP_ZONE_APPEND) req->__sector =3D addr; if (compl) - __ublk_complete_rq(req, io, ublk_dev_need_map_io(ub)); + __ublk_complete_rq(req, io, ublk_dev_need_map_io(ub), NULL); =20 if (ret) goto out; @@ -3321,11 +3330,11 @@ static int ublk_batch_commit_io(struct ublk_queue *= ubq, if (req_op(req) =3D=3D REQ_OP_ZONE_APPEND) req->__sector =3D ublk_batch_zone_lba(uc, elem); if (compl) - __ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub)); + __ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub), data->iob); return 0; } =20 -static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *d= ata) +static int ublk_handle_batch_commit_cmd(struct ublk_batch_io_data *data) { const struct ublk_batch_io *uc =3D &data->header; struct io_uring_cmd *cmd =3D data->cmd; @@ -3334,10 +3343,15 @@ static int ublk_handle_batch_commit_cmd(const struc= t ublk_batch_io_data *data) .total =3D uc->nr_elem * uc->elem_bytes, .elem_bytes =3D uc->elem_bytes, }; + DEFINE_IO_COMP_BATCH(iob); int ret; =20 + data->iob =3D &iob; ret =3D ublk_walk_cmd_buf(&iter, data, ublk_batch_commit_io); =20 + if (iob.complete) + iob.complete(&iob); + return iter.done =3D=3D 0 ? ret : iter.done; } =20 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E10942D3A89 for ; Fri, 21 Nov 2025 02:00:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690429; cv=none; b=k6JAnxUVwgxLzjyt/WabxG/0Fic22iP7yh+rrVDpPLkBagrmgS5jgOHNxZZ2doggxcDoc+Zm3TGCiI3bmMklnYCB6JepiIENXaLRG1d5S3xgXsNz/tKH5dHecioXzO/Hv5OWMfMBDVvuo7SObvPxwx0YA7r5kQbScTRTKeZE7ko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690429; c=relaxed/simple; bh=KWNFHwP2V5eTnyugi3Vxb/nZ9rIjOOFKw2M2peA9OmQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mG0+D5FHi+YSVEafO7pPzF2bk3W6U7JZzPEBEWcdEqCciNSGLnm+POTkYuF21AoLG4UF7D/RPnTe5XqmV/04xUeRk16Z6PUB53X4M8dZbThRKvBmCNMX2wye+MWZuiJZfWpnbsKvkKIwoofCv7arj/AA8JoW6vPipQC7GWBiZNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Jw6cty9l; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Jw6cty9l" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690426; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FeY8RikIVqTL29w6QXtEpN3RtRWSb2qCvs8FH9UZtaY=; b=Jw6cty9lKqwZQPXTfqHgFK+XXKM7nsmujwOzyrkl4EaH6+iw1xmvcgOJckYPhMGs2BVKlv Gj62FR6Vl65Qw+v1V0VeBELpxvqPxlIaox8o8ZNDDJbUaBN5akCZsYliwaaAn7uHgI33f2 U8X+JdngvEtfot1ddwTyrPyRkYXliBc= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-617-kppmlYUFOHWeyrliy2BfaA-1; Thu, 20 Nov 2025 21:00:22 -0500 X-MC-Unique: kppmlYUFOHWeyrliy2BfaA-1 X-Mimecast-MFC-AGG-ID: kppmlYUFOHWeyrliy2BfaA_1763690421 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4F4E019560B7; Fri, 21 Nov 2025 02:00:21 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 28A861956045; Fri, 21 Nov 2025 02:00:19 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 19/27] selftests: ublk: fix user_data truncation for tgt_data >= 256 Date: Fri, 21 Nov 2025 09:58:41 +0800 Message-ID: <20251121015851.3672073-20-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" The build_user_data() function packs multiple fields into a __u64 value using bit shifts. Without explicit __u64 casts before shifting, the shift operations are performed on 32-bit unsigned integers before being promoted to 64-bit, causing data loss. Specifically, when tgt_data >=3D 256, the expression (tgt_data << 24) shifts on a 32-bit value, truncating the upper 8 bits before promotion to __u64. Since tgt_data can be up to 16 bits (assertion allows up to 65535), values >=3D 256 would have their high byte lost. Add explicit __u64 casts to both op and tgt_data before shifting to ensure the shift operations happen in 64-bit space, preserving all bits of the input values. user_data_to_tgt_data() is only used by stripe.c, in which the max supported member disks are 4, so won't trigger this issue. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/kublk.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index fe42705c6d42..38d80e60e211 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -220,7 +220,7 @@ static inline __u64 build_user_data(unsigned tag, unsig= ned op, _Static_assert(UBLK_MAX_QUEUES_SHIFT <=3D 7); assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7)); =20 - return tag | (op << 16) | (tgt_data << 24) | + return tag | ((__u64)op << 16) | ((__u64)tgt_data << 24) | (__u64)q_id << 56 | (__u64)is_target_io << 63; } =20 --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E3012D663F for ; Fri, 21 Nov 2025 02:00:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690435; cv=none; b=NfWbVbCGx9VaInDxpRb0hD9sOHvMVK05keMNs7k2/BMUSKwzsH+FWEWmx0ipCJXjRfkwMm/C4EFqrSVnPMg60d1aPXf5wOOuordWmnHx8xeQmwfuKLUyaQzYBFLn1jX9SPfvojtNSlUPQQXHynPxchM7yWRHIY4kFfBBproM8cY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690435; c=relaxed/simple; bh=h9nrZ+QTxjgEiuVLrumkTmK0/N8Hb7R1dqKgbtHQgyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dM3B5ewFzdfmmaMi0K2dSNqbroL1pAlAMqO6phiebVxTmeCFv/S4vYTBY0+vwqMSreBSaSvcQOagIfDc2wPPHYzO1x4Uw0EumHH/GR5BUlNwhtIAC016OY49sZ+SoieFO1CSzld01b0VpE8bzQWszdN4xnG1iiGR+06x8OtJfD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=G2p+eKGo; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="G2p+eKGo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690432; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VR5tYn7j4Lr6a5nnEyeVlgZjWK8/HDDfWoqimxA5mSI=; b=G2p+eKGo3zrhCTgzFTMWvaU6mi0sOOYh29h8UkliCzbebBIMIhqtL1DZ4/eYL428fyDDBU VEVKBHytfIiTqs82v/dOTZ9SwaHGsJK7CIFZpHfKbkH3ga7En3A9xPI6yRN/6DCglR+7gV 6pQU4gdxIx1JQKLCdg9gEfHuFGWbQq0= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-118-JNff8O66MxyvcJdtrKbsuA-1; Thu, 20 Nov 2025 21:00:26 -0500 X-MC-Unique: JNff8O66MxyvcJdtrKbsuA-1 X-Mimecast-MFC-AGG-ID: JNff8O66MxyvcJdtrKbsuA_1763690425 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 09EAE1800447; Fri, 21 Nov 2025 02:00:25 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 372ED1800451; Fri, 21 Nov 2025 02:00:23 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 20/27] selftests: ublk: replace assert() with ublk_assert() Date: Fri, 21 Nov 2025 09:58:42 +0800 Message-ID: <20251121015851.3672073-21-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Replace assert() with ublk_assert() since it is often triggered in daemon, and we may get nothing shown in terminal. Add ublk_assert(), so we can log something to syslog when assert() is triggered. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/common.c | 2 +- tools/testing/selftests/ublk/file_backed.c | 2 +- tools/testing/selftests/ublk/kublk.c | 2 +- tools/testing/selftests/ublk/kublk.h | 2 +- tools/testing/selftests/ublk/stripe.c | 10 +++++----- tools/testing/selftests/ublk/utils.h | 10 ++++++++++ 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/ublk/common.c b/tools/testing/selftest= s/ublk/common.c index 01580a6f8519..4c07bc37eb6d 100644 --- a/tools/testing/selftests/ublk/common.c +++ b/tools/testing/selftests/ublk/common.c @@ -16,7 +16,7 @@ int backing_file_tgt_init(struct ublk_dev *dev) { int fd, i; =20 - assert(dev->nr_fds =3D=3D 1); + ublk_assert(dev->nr_fds =3D=3D 1); =20 for (i =3D 0; i < dev->tgt.nr_backing_files; i++) { char *file =3D dev->tgt.backing_file[i]; diff --git a/tools/testing/selftests/ublk/file_backed.c b/tools/testing/sel= ftests/ublk/file_backed.c index cd9fe69ecce2..9e7dd3859ea9 100644 --- a/tools/testing/selftests/ublk/file_backed.c +++ b/tools/testing/selftests/ublk/file_backed.c @@ -10,7 +10,7 @@ static enum io_uring_op ublk_to_uring_op(const struct ubl= ksrv_io_desc *iod, int return zc ? IORING_OP_READ_FIXED : IORING_OP_READ; else if (ublk_op =3D=3D UBLK_IO_OP_WRITE) return zc ? IORING_OP_WRITE_FIXED : IORING_OP_WRITE; - assert(0); + ublk_assert(0); } =20 static int loop_queue_flush_io(struct ublk_thread *t, struct ublk_queue *q, diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index f8fa102a627f..bb8da9ff247d 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -750,7 +750,7 @@ static void ublk_handle_uring_cmd(struct ublk_thread *t, } =20 if (cqe->res =3D=3D UBLK_IO_RES_OK) { - assert(tag < q->q_depth); + ublk_assert(tag < q->q_depth); if (q->tgt_ops->queue_io) q->tgt_ops->queue_io(t, q, tag); } else if (cqe->res =3D=3D UBLK_IO_RES_NEED_GET_DATA) { diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index 38d80e60e211..f5c0978f30c2 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -218,7 +218,7 @@ static inline __u64 build_user_data(unsigned tag, unsig= ned op, { /* we only have 7 bits to encode q_id */ _Static_assert(UBLK_MAX_QUEUES_SHIFT <=3D 7); - assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7)); + ublk_assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> = 7)); =20 return tag | ((__u64)op << 16) | ((__u64)tgt_data << 24) | (__u64)q_id << 56 | (__u64)is_target_io << 63; diff --git a/tools/testing/selftests/ublk/stripe.c b/tools/testing/selftest= s/ublk/stripe.c index 791fa8dc1651..50874858a829 100644 --- a/tools/testing/selftests/ublk/stripe.c +++ b/tools/testing/selftests/ublk/stripe.c @@ -96,12 +96,12 @@ static void calculate_stripe_array(const struct stripe_= conf *conf, this->seq =3D seq; s->nr +=3D 1; } else { - assert(seq =3D=3D this->seq); - assert(this->start + this->nr_sects =3D=3D stripe_off); + ublk_assert(seq =3D=3D this->seq); + ublk_assert(this->start + this->nr_sects =3D=3D stripe_off); this->nr_sects +=3D nr_sects; } =20 - assert(this->nr_vec < this->cap); + ublk_assert(this->nr_vec < this->cap); this->vec[this->nr_vec].iov_base =3D (void *)(base + done); this->vec[this->nr_vec++].iov_len =3D nr_sects << 9; =20 @@ -120,7 +120,7 @@ static inline enum io_uring_op stripe_to_uring_op( return zc ? IORING_OP_READV_FIXED : IORING_OP_READV; else if (ublk_op =3D=3D UBLK_IO_OP_WRITE) return zc ? IORING_OP_WRITEV_FIXED : IORING_OP_WRITEV; - assert(0); + ublk_assert(0); } =20 static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue= *q, @@ -318,7 +318,7 @@ static int ublk_stripe_tgt_init(const struct dev_ctx *c= tx, struct ublk_dev *dev) if (!dev->tgt.nr_backing_files || dev->tgt.nr_backing_files > NR_STRIPE) return -EINVAL; =20 - assert(dev->nr_fds =3D=3D dev->tgt.nr_backing_files + 1); + ublk_assert(dev->nr_fds =3D=3D dev->tgt.nr_backing_files + 1); =20 for (i =3D 0; i < dev->tgt.nr_backing_files; i++) dev->tgt.backing_file_size[i] &=3D ~((1 << chunk_shift) - 1); diff --git a/tools/testing/selftests/ublk/utils.h b/tools/testing/selftests= /ublk/utils.h index a852e0b7153e..17eefed73690 100644 --- a/tools/testing/selftests/ublk/utils.h +++ b/tools/testing/selftests/ublk/utils.h @@ -43,6 +43,7 @@ static inline void ublk_err(const char *fmt, ...) =20 va_start(ap, fmt); vfprintf(stderr, fmt, ap); + va_end(ap); } =20 static inline void ublk_log(const char *fmt, ...) @@ -52,6 +53,7 @@ static inline void ublk_log(const char *fmt, ...) =20 va_start(ap, fmt); vfprintf(stdout, fmt, ap); + va_end(ap); } } =20 @@ -62,7 +64,15 @@ static inline void ublk_dbg(int level, const char *fmt, = ...) =20 va_start(ap, fmt); vfprintf(stdout, fmt, ap); + va_end(ap); } } =20 +#define ublk_assert(x) do { \ + if (!(x)) { \ + ublk_err("%s %d: assert!\n", __func__, __LINE__); \ + assert(x); \ + } \ +} while (0) + #endif --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BE59257842 for ; Fri, 21 Nov 2025 02:00:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690437; cv=none; b=ezxrNlKDPc+HwqQl2kJgaWm/kHDXRI9C8hkXDUSA4zOJGStjOchgyIIAlDKtIYifTr6N9cLYibQBTuMcdJ0dhIXMJHrClBDh8I5+2hDET58s1AtFSZpqTAAMsCbiGXQ0qhUWysQ0UkjPKsEpzCVymuQbboyU1TjRrPxgnxzT+m0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690437; c=relaxed/simple; bh=Bjxc3u1VfOLX3oh1ANR1xf03xFpWV1rTYMqcVZ5IOqM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K+tJEKRbfmHY0v2o54Lh5+Mt+VtS9XZNZ2QPzTR+/xyCobmWGEQcQ39sOFwoPPyM7IUvMNZeQOBMfswP2P2h1xPlmaof3+ivOExVi9ZhdJMXgeTXRXP0Bx2xi9Q+C/tz57jmB5lcnHRMZFLAHxXGthFBHdc+wYQ7Vj3RYG82SF0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Lhmbiqqz; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Lhmbiqqz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LYFu7jzKN/44Bm5wrZu0JZDWl33rMYQ9jaVmymxeGbM=; b=LhmbiqqzpyElQTX9uUDGHK8G/0e9VYAEQGCDuxe5TouUZush8v9JSS39FTrE4hAqbzlEl0 FlTmplKrRl0ewMMWN4kAdgpRlMhTfi1R3GG1pdXkKU/ovI1OMwwFk4VB2iG5KoiZFtI6w3 UtfHp7uu2cO5+OoPZgcbzSifTOU1wzc= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-358-A-9cMwmzO5GYLcMEbw-Rmw-1; Thu, 20 Nov 2025 21:00:30 -0500 X-MC-Unique: A-9cMwmzO5GYLcMEbw-Rmw-1 X-Mimecast-MFC-AGG-ID: A-9cMwmzO5GYLcMEbw-Rmw_1763690429 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 08CD01954B21; Fri, 21 Nov 2025 02:00:29 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E724A30044DB; Fri, 21 Nov 2025 02:00:27 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 21/27] selftests: ublk: add ublk_io_buf_idx() for returning io buffer index Date: Fri, 21 Nov 2025 09:58:43 +0800 Message-ID: <20251121015851.3672073-22-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Since UBLK_F_PER_IO_DAEMON is added, io buffer index may depend on current thread because the common way is to use per-pthread io_ring_ctx for issuing ublk uring_cmd. Add one helper for returning io buffer index, so we can hide the buffer index implementation details for target code. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/file_backed.c | 9 +++++---- tools/testing/selftests/ublk/kublk.c | 9 +++++---- tools/testing/selftests/ublk/kublk.h | 10 +++++++++- tools/testing/selftests/ublk/null.c | 18 ++++++++++-------- tools/testing/selftests/ublk/stripe.c | 7 ++++--- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/ublk/file_backed.c b/tools/testing/sel= ftests/ublk/file_backed.c index 9e7dd3859ea9..58ac59528b74 100644 --- a/tools/testing/selftests/ublk/file_backed.c +++ b/tools/testing/selftests/ublk/file_backed.c @@ -36,6 +36,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, st= ruct ublk_queue *q, enum io_uring_op op =3D ublk_to_uring_op(iod, zc | auto_zc); struct io_uring_sqe *sqe[3]; void *addr =3D (zc | auto_zc) ? NULL : (void *)iod->addr; + unsigned short buf_idx =3D ublk_io_buf_idx(t, q, tag); =20 if (!zc || auto_zc) { ublk_io_alloc_sqes(t, sqe, 1); @@ -47,7 +48,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, st= ruct ublk_queue *q, iod->nr_sectors << 9, iod->start_sector << 9); if (auto_zc) - sqe[0]->buf_index =3D tag; + sqe[0]->buf_index =3D buf_idx; io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE); /* bit63 marks us as tgt io */ sqe[0]->user_data =3D build_user_data(tag, ublk_op, 0, q->q_id, 1); @@ -56,7 +57,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, st= ruct ublk_queue *q, =20 ublk_io_alloc_sqes(t, sqe, 3); =20 - io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, ublk_get_io(q, tag)->= buf_index); + io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx); sqe[0]->flags |=3D IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK; sqe[0]->user_data =3D build_user_data(tag, ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1); @@ -64,11 +65,11 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, = struct ublk_queue *q, io_uring_prep_rw(op, sqe[1], ublk_get_registered_fd(q, 1) /*fds[1]*/, 0, iod->nr_sectors << 9, iod->start_sector << 9); - sqe[1]->buf_index =3D tag; + sqe[1]->buf_index =3D buf_idx; sqe[1]->flags |=3D IOSQE_FIXED_FILE | IOSQE_IO_HARDLINK; sqe[1]->user_data =3D build_user_data(tag, ublk_op, 0, q->q_id, 1); =20 - io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, ublk_get_io(q, tag)= ->buf_index); + io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, buf_idx); sqe[2]->user_data =3D build_user_data(tag, ublk_cmd_op_nr(sqe[2]->cmd_op)= , 0, q->q_id, 1); =20 return 2; diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index bb8da9ff247d..1665a7865af4 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -579,16 +579,17 @@ static void ublk_dev_unprep(struct ublk_dev *dev) close(dev->fds[0]); } =20 -static void ublk_set_auto_buf_reg(const struct ublk_queue *q, +static void ublk_set_auto_buf_reg(const struct ublk_thread *t, + const struct ublk_queue *q, struct io_uring_sqe *sqe, unsigned short tag) { struct ublk_auto_buf_reg buf =3D {}; =20 if (q->tgt_ops->buf_index) - buf.index =3D q->tgt_ops->buf_index(q, tag); + buf.index =3D q->tgt_ops->buf_index(t, q, tag); else - buf.index =3D q->ios[tag].buf_index; + buf.index =3D ublk_io_buf_idx(t, q, tag); =20 if (ublk_queue_auto_zc_fallback(q)) buf.flags =3D UBLK_AUTO_BUF_REG_FALLBACK; @@ -655,7 +656,7 @@ int ublk_queue_io_cmd(struct ublk_thread *t, struct ubl= k_io *io) cmd->addr =3D 0; =20 if (ublk_queue_use_auto_zc(q)) - ublk_set_auto_buf_reg(q, sqe[0], io->tag); + ublk_set_auto_buf_reg(t, q, sqe[0], io->tag); =20 user_data =3D build_user_data(io->tag, _IOC_NR(cmd_op), 0, q->q_id, 0); io_uring_sqe_set_data64(sqe[0], user_data); diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index f5c0978f30c2..5b951ad9b03d 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -143,7 +143,8 @@ struct ublk_tgt_ops { void (*usage)(const struct ublk_tgt_ops *ops); =20 /* return buffer index for UBLK_F_AUTO_BUF_REG */ - unsigned short (*buf_index)(const struct ublk_queue *, int tag); + unsigned short (*buf_index)(const struct ublk_thread *t, + const struct ublk_queue *, int tag); }; =20 struct ublk_tgt { @@ -351,6 +352,13 @@ static inline void ublk_set_sqe_cmd_op(struct io_uring= _sqe *sqe, __u32 cmd_op) addr[1] =3D 0; } =20 +static inline unsigned short ublk_io_buf_idx(const struct ublk_thread *t, + const struct ublk_queue *q, + unsigned tag) +{ + return q->ios[tag].buf_index; +} + static inline struct ublk_io *ublk_get_io(struct ublk_queue *q, unsigned t= ag) { return &q->ios[tag]; diff --git a/tools/testing/selftests/ublk/null.c b/tools/testing/selftests/= ublk/null.c index 280043f6b689..819f72ac2da9 100644 --- a/tools/testing/selftests/ublk/null.c +++ b/tools/testing/selftests/ublk/null.c @@ -43,12 +43,12 @@ static int ublk_null_tgt_init(const struct dev_ctx *ctx= , struct ublk_dev *dev) } =20 static void __setup_nop_io(int tag, const struct ublksrv_io_desc *iod, - struct io_uring_sqe *sqe, int q_id) + struct io_uring_sqe *sqe, int q_id, unsigned buf_idx) { unsigned ublk_op =3D ublksrv_get_op(iod); =20 io_uring_prep_nop(sqe); - sqe->buf_index =3D tag; + sqe->buf_index =3D buf_idx; sqe->flags |=3D IOSQE_FIXED_FILE; sqe->rw_flags =3D IORING_NOP_FIXED_BUFFER | IORING_NOP_INJECT_RESULT; sqe->len =3D iod->nr_sectors << 9; /* injected result */ @@ -60,18 +60,19 @@ static int null_queue_zc_io(struct ublk_thread *t, stru= ct ublk_queue *q, { const struct ublksrv_io_desc *iod =3D ublk_get_iod(q, tag); struct io_uring_sqe *sqe[3]; + unsigned short buf_idx =3D ublk_io_buf_idx(t, q, tag); =20 ublk_io_alloc_sqes(t, sqe, 3); =20 - io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, ublk_get_io(q, tag)->= buf_index); + io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx); sqe[0]->user_data =3D build_user_data(tag, ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1); sqe[0]->flags |=3D IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK; =20 - __setup_nop_io(tag, iod, sqe[1], q->q_id); + __setup_nop_io(tag, iod, sqe[1], q->q_id, buf_idx); sqe[1]->flags |=3D IOSQE_IO_HARDLINK; =20 - io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, ublk_get_io(q, tag)= ->buf_index); + io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, buf_idx); sqe[2]->user_data =3D build_user_data(tag, ublk_cmd_op_nr(sqe[2]->cmd_op)= , 0, q->q_id, 1); =20 // buf register is marked as IOSQE_CQE_SKIP_SUCCESS @@ -85,7 +86,7 @@ static int null_queue_auto_zc_io(struct ublk_thread *t, s= truct ublk_queue *q, struct io_uring_sqe *sqe[1]; =20 ublk_io_alloc_sqes(t, sqe, 1); - __setup_nop_io(tag, iod, sqe[0], q->q_id); + __setup_nop_io(tag, iod, sqe[0], q->q_id, ublk_io_buf_idx(t, q, tag)); return 1; } =20 @@ -136,11 +137,12 @@ static int ublk_null_queue_io(struct ublk_thread *t, = struct ublk_queue *q, * return invalid buffer index for triggering auto buffer register failure, * then UBLK_IO_RES_NEED_REG_BUF handling is covered */ -static unsigned short ublk_null_buf_index(const struct ublk_queue *q, int = tag) +static unsigned short ublk_null_buf_index(const struct ublk_thread *t, + const struct ublk_queue *q, int tag) { if (ublk_queue_auto_zc_fallback(q)) return (unsigned short)-1; - return q->ios[tag].buf_index; + return ublk_io_buf_idx(t, q, tag); } =20 const struct ublk_tgt_ops null_tgt_ops =3D { diff --git a/tools/testing/selftests/ublk/stripe.c b/tools/testing/selftest= s/ublk/stripe.c index 50874858a829..db281a879877 100644 --- a/tools/testing/selftests/ublk/stripe.c +++ b/tools/testing/selftests/ublk/stripe.c @@ -135,6 +135,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t= , struct ublk_queue *q, struct ublk_io *io =3D ublk_get_io(q, tag); int i, extra =3D zc ? 2 : 0; void *base =3D (zc | auto_zc) ? NULL : (void *)iod->addr; + unsigned short buf_idx =3D ublk_io_buf_idx(t, q, tag); =20 io->private_data =3D s; calculate_stripe_array(conf, iod, s, base); @@ -142,7 +143,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t= , struct ublk_queue *q, ublk_io_alloc_sqes(t, sqe, s->nr + extra); =20 if (zc) { - io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, io->buf_index); + io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx); sqe[0]->flags |=3D IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK; sqe[0]->user_data =3D build_user_data(tag, ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1); @@ -158,7 +159,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t= , struct ublk_queue *q, t->start << 9); io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE); if (auto_zc || zc) { - sqe[i]->buf_index =3D tag; + sqe[i]->buf_index =3D buf_idx; if (zc) sqe[i]->flags |=3D IOSQE_IO_HARDLINK; } @@ -168,7 +169,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t= , struct ublk_queue *q, if (zc) { struct io_uring_sqe *unreg =3D sqe[s->nr + 1]; =20 - io_uring_prep_buf_unregister(unreg, q, tag, q->q_id, io->buf_index); + io_uring_prep_buf_unregister(unreg, q, tag, q->q_id, buf_idx); unreg->user_data =3D build_user_data( tag, ublk_cmd_op_nr(unreg->cmd_op), 0, q->q_id, 1); } --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B28F72D8393 for ; Fri, 21 Nov 2025 02:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690440; cv=none; b=qPPhST4F9S7ss8X8Xj9ZTFxNaXe8hcr+LLVLzkEd6GD5ne7Aqw6A0nVU1yfjBywIcATyPJ9YUXGPzomgZSdT6NDCZR9CEy0giH/iL7AwU+2MAjlWdJFNJkkObjE4fmzFW7GAqC4nzoBx97e553EDzSHL26pppMff20ktcoCvZK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690440; c=relaxed/simple; bh=Vplh80tDZzcQvV50/huO616yo8zu/Jk+RWr5w+rPCKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dim5A0mNoPZrbnekXif9jTlD4rCvhFNaz9Xy1d8aCQJ5PrWZ5FNgo0oFqE9dNd8OdBrvOobxZ1uKw9mQf6BA2G70d2htsZOEFRdIzv9sJfrtBk4UQUBEk6T9vt9jS3ESmo91Lpc1uxnMojsrjPclIqNMf9MGz0vN5/8sBT6j4Lc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Hvwu7Htp; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Hvwu7Htp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jryq43Dub5x0CbBBeH5rpJnOwH4Y2nOa/k4WGNhRpu0=; b=Hvwu7Htp7Q7V628BUz4Gm/slBgRJgbVTVYb+XjxCw2E5n55t0ZRp3JvaW/G7QbX9YAC2l3 dvwWGiKGDIpuXhLBr2ww6raT5qARPoFs9aDphp5YXGgopejz3hFhew+Ssemz3c/2kjD/CG 91T6cIPjua1wgtdC2Z5RNiEXrVIHL0s= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-379-LKXlPrICPmS6JysFaifMZg-1; Thu, 20 Nov 2025 21:00:34 -0500 X-MC-Unique: LKXlPrICPmS6JysFaifMZg-1 X-Mimecast-MFC-AGG-ID: LKXlPrICPmS6JysFaifMZg_1763690433 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ECF6C18001FE; Fri, 21 Nov 2025 02:00:32 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DE9231800877; Fri, 21 Nov 2025 02:00:31 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 22/27] selftests: ublk: add batch buffer management infrastructure Date: Fri, 21 Nov 2025 09:58:44 +0800 Message-ID: <20251121015851.3672073-23-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add the foundational infrastructure for UBLK_F_BATCH_IO buffer management including: - Allocator utility functions for small sized per-thread allocation - Batch buffer allocation and deallocation functions - Buffer index management for commit buffers - Thread state management for batch I/O mode - Buffer size calculation based on device features This prepares the groundwork for handling batch I/O commands by establishing the buffer management layer needed for UBLK_U_IO_PREP_IO_CMDS and UBLK_U_IO_COMMIT_IO_CMDS operations. The allocator uses CPU sets for efficient per-thread buffer tracking, and commit buffers are pre-allocated with 2 buffers per thread to handle overlapping command operations. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/Makefile | 2 +- tools/testing/selftests/ublk/batch.c | 152 ++++++++++++++++++++++++++ tools/testing/selftests/ublk/kublk.c | 26 ++++- tools/testing/selftests/ublk/kublk.h | 53 +++++++++ tools/testing/selftests/ublk/utils.h | 54 +++++++++ 5 files changed, 283 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/ublk/batch.c diff --git a/tools/testing/selftests/ublk/Makefile b/tools/testing/selftest= s/ublk/Makefile index 770269efe42a..a724276622d0 100644 --- a/tools/testing/selftests/ublk/Makefile +++ b/tools/testing/selftests/ublk/Makefile @@ -44,7 +44,7 @@ TEST_GEN_PROGS_EXTENDED =3D kublk =20 include ../lib.mk =20 -$(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c common.c stripe.c= \ +$(TEST_GEN_PROGS_EXTENDED): kublk.c batch.c null.c file_backed.c common.c = stripe.c \ fault_inject.c =20 check: diff --git a/tools/testing/selftests/ublk/batch.c b/tools/testing/selftests= /ublk/batch.c new file mode 100644 index 000000000000..609e6073c9c0 --- /dev/null +++ b/tools/testing/selftests/ublk/batch.c @@ -0,0 +1,152 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: UBLK_F_BATCH_IO buffer management + */ + +#include "kublk.h" + +static inline void *ublk_get_commit_buf(struct ublk_thread *t, + unsigned short buf_idx) +{ + unsigned idx; + + if (buf_idx < t->commit_buf_start || + buf_idx >=3D t->commit_buf_start + t->nr_commit_buf) + return NULL; + idx =3D buf_idx - t->commit_buf_start; + return t->commit_buf + idx * t->commit_buf_size; +} + +/* + * Allocate one buffer for UBLK_U_IO_PREP_IO_CMDS or UBLK_U_IO_COMMIT_IO_C= MDS + * + * Buffer index is returned. + */ +static inline unsigned short ublk_alloc_commit_buf(struct ublk_thread *t) +{ + int idx =3D allocator_get(&t->commit_buf_alloc); + + if (idx >=3D 0) + return idx + t->commit_buf_start; + return UBLKS_T_COMMIT_BUF_INV_IDX; +} + +/* + * Free one commit buffer which is used by UBLK_U_IO_PREP_IO_CMDS or + * UBLK_U_IO_COMMIT_IO_CMDS + */ +static inline void ublk_free_commit_buf(struct ublk_thread *t, + unsigned short i) +{ + unsigned short idx =3D i - t->commit_buf_start; + + ublk_assert(idx < t->nr_commit_buf); + ublk_assert(allocator_get_val(&t->commit_buf_alloc, idx) !=3D 0); + + allocator_put(&t->commit_buf_alloc, idx); +} + +static unsigned char ublk_commit_elem_buf_size(struct ublk_dev *dev) +{ + if (dev->dev_info.flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_USER_COPY | + UBLK_F_AUTO_BUF_REG)) + return 8; + + /* one extra 8bytes for carrying buffer address */ + return 16; +} + +static unsigned ublk_commit_buf_size(struct ublk_thread *t) +{ + struct ublk_dev *dev =3D t->dev; + unsigned elem_size =3D ublk_commit_elem_buf_size(dev); + unsigned int total =3D elem_size * dev->dev_info.queue_depth; + unsigned int page_sz =3D getpagesize(); + + return round_up(total, page_sz); +} + +static void free_batch_commit_buf(struct ublk_thread *t) +{ + if (t->commit_buf) { + unsigned buf_size =3D ublk_commit_buf_size(t); + unsigned int total =3D buf_size * t->nr_commit_buf; + + munlock(t->commit_buf, total); + free(t->commit_buf); + } + allocator_deinit(&t->commit_buf_alloc); +} + +static int alloc_batch_commit_buf(struct ublk_thread *t) +{ + unsigned buf_size =3D ublk_commit_buf_size(t); + unsigned int total =3D buf_size * t->nr_commit_buf; + unsigned int page_sz =3D getpagesize(); + void *buf =3D NULL; + int ret; + + allocator_init(&t->commit_buf_alloc, t->nr_commit_buf); + + t->commit_buf =3D NULL; + ret =3D posix_memalign(&buf, page_sz, total); + if (ret || !buf) + goto fail; + + t->commit_buf =3D buf; + + /* lock commit buffer pages for fast access */ + if (mlock(t->commit_buf, total)) + ublk_err("%s: can't lock commit buffer %s\n", __func__, + strerror(errno)); + + return 0; + +fail: + free_batch_commit_buf(t); + return ret; +} + +void ublk_batch_prepare(struct ublk_thread *t) +{ + /* + * We only handle single device in this thread context. + * + * All queues have same feature flags, so use queue 0's for + * calculate uring_cmd flags. + * + * This way looks not elegant, but it works so far. + */ + struct ublk_queue *q =3D &t->dev->q[0]; + + t->commit_buf_elem_size =3D ublk_commit_elem_buf_size(t->dev); + t->commit_buf_size =3D ublk_commit_buf_size(t); + t->commit_buf_start =3D t->nr_bufs; + t->nr_commit_buf =3D 2; + t->nr_bufs +=3D t->nr_commit_buf; + + t->cmd_flags =3D 0; + if (ublk_queue_use_auto_zc(q)) { + if (ublk_queue_auto_zc_fallback(q)) + t->cmd_flags |=3D UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK; + } else if (!ublk_queue_no_buf(q)) + t->cmd_flags |=3D UBLK_BATCH_F_HAS_BUF_ADDR; + + t->state |=3D UBLKS_T_BATCH_IO; + + ublk_log("%s: thread %d commit(nr_bufs %u, buf_size %u, start %u)\n", + __func__, t->idx, + t->nr_commit_buf, t->commit_buf_size, + t->nr_bufs); +} + +int ublk_batch_alloc_buf(struct ublk_thread *t) +{ + ublk_assert(t->nr_commit_buf < 16); + return alloc_batch_commit_buf(t); +} + +void ublk_batch_free_buf(struct ublk_thread *t) +{ + free_batch_commit_buf(t); +} diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index 1665a7865af4..29594612edc9 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -423,6 +423,8 @@ static void ublk_thread_deinit(struct ublk_thread *t) { io_uring_unregister_buffers(&t->ring); =20 + ublk_batch_free_buf(t); + io_uring_unregister_ring_fd(&t->ring); =20 if (t->ring.ring_fd > 0) { @@ -505,15 +507,33 @@ static int ublk_thread_init(struct ublk_thread *t, un= signed long long extra_flag unsigned nr_ios =3D dev->dev_info.queue_depth * dev->dev_info.nr_hw_queu= es; unsigned max_nr_ios_per_thread =3D nr_ios / dev->nthreads; max_nr_ios_per_thread +=3D !!(nr_ios % dev->nthreads); - ret =3D io_uring_register_buffers_sparse( - &t->ring, max_nr_ios_per_thread); + + t->nr_bufs =3D max_nr_ios_per_thread; + } else { + t->nr_bufs =3D 0; + } + + if (ublk_dev_batch_io(dev)) + ublk_batch_prepare(t); + + if (t->nr_bufs) { + ret =3D io_uring_register_buffers_sparse(&t->ring, t->nr_bufs); if (ret) { - ublk_err("ublk dev %d thread %d register spare buffers failed %d", + ublk_err("ublk dev %d thread %d register spare buffers failed %d\n", dev->dev_info.dev_id, t->idx, ret); goto fail; } } =20 + if (ublk_dev_batch_io(dev)) { + ret =3D ublk_batch_alloc_buf(t); + if (ret) { + ublk_err("ublk dev %d thread %d alloc batch buf failed %d\n", + dev->dev_info.dev_id, t->idx, ret); + goto fail; + } + } + io_uring_register_ring_fd(&t->ring); =20 if (flags & UBLKS_Q_NO_UBLK_FIXED_FD) { diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index 5b951ad9b03d..e75c28680783 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -174,15 +174,40 @@ struct ublk_queue { struct ublk_io ios[UBLK_QUEUE_DEPTH]; }; =20 +/* align with `ublk_elem_header` */ +struct ublk_batch_elem { + __u16 tag; + __u16 buf_index; + __s32 result; + __u64 buf_addr; +}; + struct ublk_thread { struct ublk_dev *dev; unsigned idx; =20 #define UBLKS_T_STOPPING (1U << 0) #define UBLKS_T_IDLE (1U << 1) +#define UBLKS_T_BATCH_IO (1U << 31) /* readonly */ unsigned state; unsigned int cmd_inflight; unsigned int io_inflight; + + unsigned short nr_bufs; + + /* followings are for BATCH_IO */ + unsigned short commit_buf_start; + unsigned char commit_buf_elem_size; + /* + * We just support single device, so pre-calculate commit/prep flags + */ + unsigned short cmd_flags; + unsigned int nr_commit_buf; + unsigned int commit_buf_size; + void *commit_buf; +#define UBLKS_T_COMMIT_BUF_INV_IDX ((unsigned short)-1) + struct allocator commit_buf_alloc; + struct io_uring ring; }; =20 @@ -203,6 +228,27 @@ struct ublk_dev { =20 extern int ublk_queue_io_cmd(struct ublk_thread *t, struct ublk_io *io); =20 +static inline int __ublk_use_batch_io(__u64 flags) +{ + return flags & UBLK_F_BATCH_IO; +} + +static inline int ublk_queue_batch_io(const struct ublk_queue *q) +{ + return __ublk_use_batch_io(q->flags); +} + +static inline int ublk_dev_batch_io(const struct ublk_dev *dev) +{ + return __ublk_use_batch_io(dev->dev_info.flags); +} + +/* only work for handle single device in this pthread context */ +static inline int ublk_thread_batch_io(const struct ublk_thread *t) +{ + return t->state & UBLKS_T_BATCH_IO; +} + =20 static inline int ublk_io_auto_zc_fallback(const struct ublksrv_io_desc *i= od) { @@ -418,6 +464,13 @@ static inline int ublk_queue_no_buf(const struct ublk_= queue *q) return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q); } =20 +/* Initialize batch I/O state and calculate buffer parameters */ +void ublk_batch_prepare(struct ublk_thread *t); +/* Allocate and register commit buffers for batch operations */ +int ublk_batch_alloc_buf(struct ublk_thread *t); +/* Free commit buffers and cleanup batch allocator */ +void ublk_batch_free_buf(struct ublk_thread *t); + extern const struct ublk_tgt_ops null_tgt_ops; extern const struct ublk_tgt_ops loop_tgt_ops; extern const struct ublk_tgt_ops stripe_tgt_ops; diff --git a/tools/testing/selftests/ublk/utils.h b/tools/testing/selftests= /ublk/utils.h index 17eefed73690..aab522f26167 100644 --- a/tools/testing/selftests/ublk/utils.h +++ b/tools/testing/selftests/ublk/utils.h @@ -21,6 +21,60 @@ #define round_up(val, rnd) \ (((val) + ((rnd) - 1)) & ~((rnd) - 1)) =20 +/* small sized & per-thread allocator */ +struct allocator { + unsigned int size; + cpu_set_t *set; +}; + +static inline int allocator_init(struct allocator *a, unsigned size) +{ + a->set =3D CPU_ALLOC(size); + a->size =3D size; + + if (a->set) + return 0; + return -ENOMEM; +} + +static inline void allocator_deinit(struct allocator *a) +{ + CPU_FREE(a->set); + a->set =3D NULL; + a->size =3D 0; +} + +static inline int allocator_get(struct allocator *a) +{ + int i; + + for (i =3D 0; i < a->size; i +=3D 1) { + size_t set_size =3D CPU_ALLOC_SIZE(a->size); + + if (!CPU_ISSET_S(i, set_size, a->set)) { + CPU_SET_S(i, set_size, a->set); + return i; + } + } + + return -1; +} + +static inline void allocator_put(struct allocator *a, int i) +{ + size_t set_size =3D CPU_ALLOC_SIZE(a->size); + + if (i >=3D 0 && i < a->size) + CPU_CLR_S(i, set_size, a->set); +} + +static inline int allocator_get_val(struct allocator *a, int i) +{ + size_t set_size =3D CPU_ALLOC_SIZE(a->size); + + return CPU_ISSET_S(i, set_size, a->set); +} + static inline unsigned int ilog2(unsigned int x) { if (x =3D=3D 0) --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 966F828468D for ; Fri, 21 Nov 2025 02:00:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690444; cv=none; b=b3qfg0Irogd3EeGctf6LJBALcMjeDFxWePLSQklZ23CgX1cyi4qiIBZq44qbq/s8gPcLIG+wM8oGJAA52Gtwm+pVuNb0hsJu5MXTNZZzB/aFrK+afiHYQ7ltHyXQWBDLggMMy7nAfbxgT47JsUO/hRtqF9CGLQbNGIC0b4N8QFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690444; c=relaxed/simple; bh=xULntA9KaTCMNqtKCYzwq0qsTeENqxmaTtLL2r3pa4U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O1VVzVXr1oN8QUICkD0gyusFEmCqsb6555XjG2liCYQMq+cE3t8FmDQZlaDTy2nL0VNIf6Ox/VU7tt5i7MV9l2RTeGkI9UkI7Jufm+eUxYL085RGe2TxEnIDQ7/y+L/INis6BWM5AJgTl3Ou0cD7HUtY/PRocfdAJ4fJNVu3Dvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=G1rYgTPz; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="G1rYgTPz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SyjtOYHZ7lFXaCvIMC62T4TLifj3kcvBA2fFustx0E8=; b=G1rYgTPzRM7jTvQb1N4l24Flf0PSx1ZQhgp/DthHHX2TAhiZ5Tl2JxyAbCFb9jpiI+EC++ MLirgFjgJ0Jz5reiZ51gN+hSJOCYxhcGPP3wzRU4eIUaMluMkvQ6rO4xvVlbxQpuHOdB3w PnUguuVy9zH5+KULHyRtMpuHPVz0Io0= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-klqe1fvCPmSf9EnZpjvwJQ-1; Thu, 20 Nov 2025 21:00:38 -0500 X-MC-Unique: klqe1fvCPmSf9EnZpjvwJQ-1 X-Mimecast-MFC-AGG-ID: klqe1fvCPmSf9EnZpjvwJQ_1763690436 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C192D1800342; Fri, 21 Nov 2025 02:00:36 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AE96218004A3; Fri, 21 Nov 2025 02:00:35 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 23/27] selftests: ublk: handle UBLK_U_IO_PREP_IO_CMDS Date: Fri, 21 Nov 2025 09:58:45 +0800 Message-ID: <20251121015851.3672073-24-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Implement support for UBLK_U_IO_PREP_IO_CMDS in the batch I/O framework: - Add batch command initialization and setup functions - Implement prep command queueing with proper buffer management - Add command completion handling for prep and commit commands - Integrate batch I/O setup into thread initialization - Update CQE handling to support batch commands The implementation uses the previously established buffer management infrastructure to queue UBLK_U_IO_PREP_IO_CMDS commands. Commands are prepared in the first thread context and use commit buffers for efficient command batching. Key changes: - ublk_batch_queue_prep_io_cmds() prepares I/O command batches - ublk_batch_compl_cmd() handles batch command completions - Modified thread setup to use batch operations when enabled - Enhanced buffer index calculation for batch mode Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/batch.c | 113 +++++++++++++++++++++++++++ tools/testing/selftests/ublk/kublk.c | 46 ++++++++--- tools/testing/selftests/ublk/kublk.h | 22 ++++++ 3 files changed, 171 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/ublk/batch.c b/tools/testing/selftests= /ublk/batch.c index 609e6073c9c0..01f00c21dfdb 100644 --- a/tools/testing/selftests/ublk/batch.c +++ b/tools/testing/selftests/ublk/batch.c @@ -150,3 +150,116 @@ void ublk_batch_free_buf(struct ublk_thread *t) { free_batch_commit_buf(t); } + +static void ublk_init_batch_cmd(struct ublk_thread *t, __u16 q_id, + struct io_uring_sqe *sqe, unsigned op, + unsigned short elem_bytes, + unsigned short nr_elem, + unsigned short buf_idx) +{ + struct ublk_batch_io *cmd; + __u64 user_data; + + cmd =3D (struct ublk_batch_io *)ublk_get_sqe_cmd(sqe); + + ublk_set_sqe_cmd_op(sqe, op); + + sqe->fd =3D 0; /* dev->fds[0] */ + sqe->opcode =3D IORING_OP_URING_CMD; + sqe->flags =3D IOSQE_FIXED_FILE; + + cmd->q_id =3D q_id; + cmd->flags =3D 0; + cmd->reserved =3D 0; + cmd->elem_bytes =3D elem_bytes; + cmd->nr_elem =3D nr_elem; + + user_data =3D build_user_data(buf_idx, _IOC_NR(op), 0, q_id, 0); + io_uring_sqe_set_data64(sqe, user_data); + + t->cmd_inflight +=3D 1; + + ublk_dbg(UBLK_DBG_IO_CMD, "%s: thread %u qid %d cmd_op %x data %lx " + "nr_elem %u elem_bytes %u buf_size %u buf_idx %d " + "cmd_inflight %u\n", + __func__, t->idx, q_id, op, user_data, + cmd->nr_elem, cmd->elem_bytes, + nr_elem * elem_bytes, buf_idx, t->cmd_inflight); +} + +static void ublk_setup_commit_sqe(struct ublk_thread *t, + struct io_uring_sqe *sqe, + unsigned short buf_idx) +{ + struct ublk_batch_io *cmd; + + cmd =3D (struct ublk_batch_io *)ublk_get_sqe_cmd(sqe); + + /* Use plain user buffer instead of fixed buffer */ + cmd->flags |=3D t->cmd_flags; +} + +int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q) +{ + unsigned short nr_elem =3D q->q_depth; + unsigned short buf_idx =3D ublk_alloc_commit_buf(t); + struct io_uring_sqe *sqe; + void *buf; + int i; + + ublk_assert(buf_idx !=3D UBLKS_T_COMMIT_BUF_INV_IDX); + + ublk_io_alloc_sqes(t, &sqe, 1); + + ublk_assert(nr_elem =3D=3D q->q_depth); + buf =3D ublk_get_commit_buf(t, buf_idx); + for (i =3D 0; i < nr_elem; i++) { + struct ublk_batch_elem *elem =3D (struct ublk_batch_elem *)( + buf + i * t->commit_buf_elem_size); + struct ublk_io *io =3D &q->ios[i]; + + elem->tag =3D i; + elem->result =3D 0; + + if (ublk_queue_use_auto_zc(q)) + elem->buf_index =3D ublk_batch_io_buf_idx(t, q, i); + else if (!ublk_queue_no_buf(q)) + elem->buf_addr =3D (__u64)io->buf_addr; + } + + sqe->addr =3D (__u64)buf; + sqe->len =3D t->commit_buf_elem_size * nr_elem; + + ublk_init_batch_cmd(t, q->q_id, sqe, UBLK_U_IO_PREP_IO_CMDS, + t->commit_buf_elem_size, nr_elem, buf_idx); + ublk_setup_commit_sqe(t, sqe, buf_idx); + return 0; +} + +static void ublk_batch_compl_commit_cmd(struct ublk_thread *t, + const struct io_uring_cqe *cqe, + unsigned op) +{ + unsigned short buf_idx =3D user_data_to_tag(cqe->user_data); + + if (op =3D=3D _IOC_NR(UBLK_U_IO_PREP_IO_CMDS)) + ublk_assert(cqe->res =3D=3D 0); + else if (op =3D=3D _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) + ;//assert(cqe->res =3D=3D t->commit_buf_size); + else + ublk_assert(0); + + ublk_free_commit_buf(t, buf_idx); +} + +void ublk_batch_compl_cmd(struct ublk_thread *t, + const struct io_uring_cqe *cqe) +{ + unsigned op =3D user_data_to_op(cqe->user_data); + + if (op =3D=3D _IOC_NR(UBLK_U_IO_PREP_IO_CMDS) || + op =3D=3D _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) { + ublk_batch_compl_commit_cmd(t, cqe, op); + return; + } +} diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index 29594612edc9..e981fcf18475 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -795,28 +795,32 @@ static void ublk_handle_cqe(struct ublk_thread *t, { struct ublk_dev *dev =3D t->dev; unsigned q_id =3D user_data_to_q_id(cqe->user_data); - struct ublk_queue *q =3D &dev->q[q_id]; unsigned cmd_op =3D user_data_to_op(cqe->user_data); =20 if (cqe->res < 0 && cqe->res !=3D -ENODEV) - ublk_err("%s: res %d userdata %llx queue state %x\n", __func__, - cqe->res, cqe->user_data, q->flags); + ublk_err("%s: res %d userdata %llx thread state %x\n", __func__, + cqe->res, cqe->user_data, t->state); =20 - ublk_dbg(UBLK_DBG_IO_CMD, "%s: res %d (qid %d tag %u cmd_op %u target %d/= %d) stopping %d\n", - __func__, cqe->res, q->q_id, user_data_to_tag(cqe->user_data), - cmd_op, is_target_io(cqe->user_data), + ublk_dbg(UBLK_DBG_IO_CMD, "%s: res %d (thread %d qid %d tag %u cmd_op %x " + "data %lx target %d/%d) stopping %d\n", + __func__, cqe->res, t->idx, q_id, + user_data_to_tag(cqe->user_data), + cmd_op, cqe->user_data, is_target_io(cqe->user_data), user_data_to_tgt_data(cqe->user_data), (t->state & UBLKS_T_STOPPING)); =20 /* Don't retrieve io in case of target io */ if (is_target_io(cqe->user_data)) { - ublksrv_handle_tgt_cqe(t, q, cqe); + ublksrv_handle_tgt_cqe(t, &dev->q[q_id], cqe); return; } =20 t->cmd_inflight--; =20 - ublk_handle_uring_cmd(t, q, cqe); + if (ublk_thread_batch_io(t)) + ublk_batch_compl_cmd(t, cqe); + else + ublk_handle_uring_cmd(t, &dev->q[q_id], cqe); } =20 static int ublk_reap_events_uring(struct ublk_thread *t) @@ -873,6 +877,22 @@ static void ublk_thread_set_sched_affinity(const struc= t ublk_thread_info *info) info->dev->dev_info.dev_id, info->idx); } =20 +static void ublk_batch_setup_queues(struct ublk_thread *t) +{ + int i; + + /* setup all queues in the 1st thread */ + for (i =3D 0; i < t->dev->dev_info.nr_hw_queues; i++) { + struct ublk_queue *q =3D &t->dev->q[i]; + int ret; + + ret =3D ublk_batch_queue_prep_io_cmds(t, q); + ublk_assert(ret =3D=3D 0); + ret =3D ublk_process_io(t); + ublk_assert(ret >=3D 0); + } +} + static __attribute__((noinline)) int __ublk_io_handler_fn(struct ublk_thre= ad_info *info) { struct ublk_thread t =3D { @@ -893,8 +913,14 @@ static __attribute__((noinline)) int __ublk_io_handler= _fn(struct ublk_thread_inf ublk_dbg(UBLK_DBG_THREAD, "tid %d: ublk dev %d thread %u started\n", gettid(), dev_id, t.idx); =20 - /* submit all io commands to ublk driver */ - ublk_submit_fetch_commands(&t); + if (!ublk_thread_batch_io(&t)) { + /* submit all io commands to ublk driver */ + ublk_submit_fetch_commands(&t); + } else if (!t.idx) { + /* prepare all io commands in the 1st thread context */ + ublk_batch_setup_queues(&t); + } + do { if (ublk_process_io(&t) < 0) break; diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index e75c28680783..51fad0f4419b 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -398,10 +398,16 @@ static inline void ublk_set_sqe_cmd_op(struct io_urin= g_sqe *sqe, __u32 cmd_op) addr[1] =3D 0; } =20 +static inline unsigned short ublk_batch_io_buf_idx( + const struct ublk_thread *t, const struct ublk_queue *q, + unsigned tag); + static inline unsigned short ublk_io_buf_idx(const struct ublk_thread *t, const struct ublk_queue *q, unsigned tag) { + if (ublk_queue_batch_io(q)) + return ublk_batch_io_buf_idx(t, q, tag); return q->ios[tag].buf_index; } =20 @@ -464,6 +470,22 @@ static inline int ublk_queue_no_buf(const struct ublk_= queue *q) return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q); } =20 +/* + * Each IO's buffer index has to be calculated by this helper for + * UBLKS_T_BATCH_IO + */ +static inline unsigned short ublk_batch_io_buf_idx( + const struct ublk_thread *t, const struct ublk_queue *q, + unsigned tag) +{ + return tag; +} + +/* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */ +int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q); +/* Handle completion of batch I/O commands (prep/commit) */ +void ublk_batch_compl_cmd(struct ublk_thread *t, + const struct io_uring_cqe *cqe); /* Initialize batch I/O state and calculate buffer parameters */ void ublk_batch_prepare(struct ublk_thread *t); /* Allocate and register commit buffers for batch operations */ --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D08A28468D for ; Fri, 21 Nov 2025 02:00:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690451; cv=none; b=nCuiTRmcGul2gHhFUJjl5UakR+y9hrg5pD4LcDVGRy0XPxk2mm5+S4XnRGD/odGP7oIcEqpszQDJASvCtsWy+4vlnDumB7+Fw3dV6Q2UZ2t261c+oacYTKvoCLByBifYfZyUepmNJoXF7eabz3tpf8kq+HT5cHvCwOQa7ttTZs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690451; c=relaxed/simple; bh=bpMj+5iJHb0HItYhdwJPBNL7yd6120JB6BmDfWc4IIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C35diUMxmpBeSBmyM1hGNx49PX7G5fddvYCXRgGCpDJLG+s42qMTtAWfZdcRe8EGCWR1+/uAtbE29Q6+g5FtSM5ExD6cwsa0OqoaF+tqu2zMUrhnbFd7HwM0LR976I+ZcztyC2oGAC9TZegPyeY8CGpjdP7s7drFZ1x0QBC4RHI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GsntFpQt; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GsntFpQt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8yAOTot2+IdfJy0TEgJ6Iw4zloe0w//2T4DOJDh9HAg=; b=GsntFpQt4idx3sJtb9xJl17RrhyQAITmmWp16KXP+ZUvLZcENJkLf5+sx2zp3L3YvOUJEZ E1U9Afa0/86GIKLczhxdxAzfnrkmzk9BABO2+qSiJaYUf75lNnyc1lCunFK/DcyBqa29Ae BJvcr70yWYU2v8g/FJtgIefPexiq6do= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-139-_16O6borOS-k_ymtuWLJ8A-1; Thu, 20 Nov 2025 21:00:41 -0500 X-MC-Unique: _16O6borOS-k_ymtuWLJ8A-1 X-Mimecast-MFC-AGG-ID: _16O6borOS-k_ymtuWLJ8A_1763690440 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 59DA21956048; Fri, 21 Nov 2025 02:00:40 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 869B130044DB; Fri, 21 Nov 2025 02:00:39 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 24/27] selftests: ublk: handle UBLK_U_IO_COMMIT_IO_CMDS Date: Fri, 21 Nov 2025 09:58:46 +0800 Message-ID: <20251121015851.3672073-25-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Implement UBLK_U_IO_COMMIT_IO_CMDS to enable efficient batched completion of I/O operations in the batch I/O framework. This completes the batch I/O infrastructure by adding the commit phase that notifies the kernel about completed I/O operations: Key features: - Batch multiple I/O completions into single UBLK_U_IO_COMMIT_IO_CMDS - Dynamic commit buffer allocation and management per thread - Automatic commit buffer preparation before processing events - Commit buffer submission after processing completed I/Os - Integration with existing completion workflows Implementation details: - ublk_batch_prep_commit() allocates and initializes commit buffers - ublk_batch_complete_io() adds completed I/Os to current batch - ublk_batch_commit_io_cmds() submits batched completions to kernel - Modified ublk_process_io() to handle batch commit lifecycle - Enhanced ublk_complete_io() to route to batch or legacy completion The commit buffer stores completion information (tag, result, buffer details) for multiple I/Os, then submits them all at once, significantly reducing syscall overhead compared to individual I/O completions. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/batch.c | 74 ++++++++++++++++++++++++++-- tools/testing/selftests/ublk/kublk.c | 8 ++- tools/testing/selftests/ublk/kublk.h | 69 +++++++++++++++++--------- 3 files changed, 122 insertions(+), 29 deletions(-) diff --git a/tools/testing/selftests/ublk/batch.c b/tools/testing/selftests= /ublk/batch.c index 01f00c21dfdb..e240d4decedf 100644 --- a/tools/testing/selftests/ublk/batch.c +++ b/tools/testing/selftests/ublk/batch.c @@ -174,7 +174,7 @@ static void ublk_init_batch_cmd(struct ublk_thread *t, = __u16 q_id, cmd->elem_bytes =3D elem_bytes; cmd->nr_elem =3D nr_elem; =20 - user_data =3D build_user_data(buf_idx, _IOC_NR(op), 0, q_id, 0); + user_data =3D build_user_data(buf_idx, _IOC_NR(op), nr_elem, q_id, 0); io_uring_sqe_set_data64(sqe, user_data); =20 t->cmd_inflight +=3D 1; @@ -244,9 +244,11 @@ static void ublk_batch_compl_commit_cmd(struct ublk_th= read *t, =20 if (op =3D=3D _IOC_NR(UBLK_U_IO_PREP_IO_CMDS)) ublk_assert(cqe->res =3D=3D 0); - else if (op =3D=3D _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) - ;//assert(cqe->res =3D=3D t->commit_buf_size); - else + else if (op =3D=3D _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) { + int nr_elem =3D user_data_to_tgt_data(cqe->user_data); + + ublk_assert(cqe->res =3D=3D t->commit_buf_elem_size * nr_elem); + } else ublk_assert(0); =20 ublk_free_commit_buf(t, buf_idx); @@ -263,3 +265,67 @@ void ublk_batch_compl_cmd(struct ublk_thread *t, return; } } + +void ublk_batch_commit_io_cmds(struct ublk_thread *t) +{ + struct io_uring_sqe *sqe; + unsigned short buf_idx; + unsigned short nr_elem =3D t->commit.done; + + /* nothing to commit */ + if (!nr_elem) { + ublk_free_commit_buf(t, t->commit.buf_idx); + return; + } + + ublk_io_alloc_sqes(t, &sqe, 1); + buf_idx =3D t->commit.buf_idx; + sqe->addr =3D (__u64)t->commit.elem; + sqe->len =3D nr_elem * t->commit_buf_elem_size; + + /* commit isn't per-queue command */ + ublk_init_batch_cmd(t, t->commit.q_id, sqe, UBLK_U_IO_COMMIT_IO_CMDS, + t->commit_buf_elem_size, nr_elem, buf_idx); + ublk_setup_commit_sqe(t, sqe, buf_idx); +} + +static void ublk_batch_init_commit(struct ublk_thread *t, + unsigned short buf_idx) +{ + /* so far only support 1:1 queue/thread mapping */ + t->commit.q_id =3D t->idx; + t->commit.buf_idx =3D buf_idx; + t->commit.elem =3D ublk_get_commit_buf(t, buf_idx); + t->commit.done =3D 0; + t->commit.count =3D t->commit_buf_size / + t->commit_buf_elem_size; +} + +void ublk_batch_prep_commit(struct ublk_thread *t) +{ + unsigned short buf_idx =3D ublk_alloc_commit_buf(t); + + ublk_assert(buf_idx !=3D UBLKS_T_COMMIT_BUF_INV_IDX); + ublk_batch_init_commit(t, buf_idx); +} + +void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q, + unsigned tag, int res) +{ + struct batch_commit_buf *cb =3D &t->commit; + struct ublk_batch_elem *elem =3D (struct ublk_batch_elem *)(cb->elem + + cb->done * t->commit_buf_elem_size); + struct ublk_io *io =3D &q->ios[tag]; + + ublk_assert(q->q_id =3D=3D t->commit.q_id); + + elem->tag =3D tag; + elem->buf_index =3D ublk_batch_io_buf_idx(t, q, tag); + elem->result =3D res; + + if (!ublk_queue_no_buf(q)) + elem->buf_addr =3D (__u64) (uintptr_t) io->buf_addr; + + cb->done +=3D 1; + ublk_assert(cb->done <=3D cb->count); +} diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index e981fcf18475..6565e804679c 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -852,7 +852,13 @@ static int ublk_process_io(struct ublk_thread *t) return -ENODEV; =20 ret =3D io_uring_submit_and_wait(&t->ring, 1); - reapped =3D ublk_reap_events_uring(t); + if (ublk_thread_batch_io(t)) { + ublk_batch_prep_commit(t); + reapped =3D ublk_reap_events_uring(t); + ublk_batch_commit_io_cmds(t); + } else { + reapped =3D ublk_reap_events_uring(t); + } =20 ublk_dbg(UBLK_DBG_THREAD, "submit result %d, reapped %d stop %d idle %d\n= ", ret, reapped, (t->state & UBLKS_T_STOPPING), diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index 51fad0f4419b..0a355653d64c 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -182,6 +182,14 @@ struct ublk_batch_elem { __u64 buf_addr; }; =20 +struct batch_commit_buf { + unsigned short q_id; + unsigned short buf_idx; + void *elem; + unsigned short done; + unsigned short count; +}; + struct ublk_thread { struct ublk_dev *dev; unsigned idx; @@ -207,6 +215,7 @@ struct ublk_thread { void *commit_buf; #define UBLKS_T_COMMIT_BUF_INV_IDX ((unsigned short)-1) struct allocator commit_buf_alloc; + struct batch_commit_buf commit; =20 struct io_uring ring; }; @@ -416,30 +425,6 @@ static inline struct ublk_io *ublk_get_io(struct ublk_= queue *q, unsigned tag) return &q->ios[tag]; } =20 -static inline int ublk_complete_io(struct ublk_thread *t, struct ublk_queu= e *q, - unsigned tag, int res) -{ - struct ublk_io *io =3D &q->ios[tag]; - - ublk_mark_io_done(io, res); - - return ublk_queue_io_cmd(t, io); -} - -static inline void ublk_queued_tgt_io(struct ublk_thread *t, struct ublk_q= ueue *q, - unsigned tag, int queued) -{ - if (queued < 0) - ublk_complete_io(t, q, tag, queued); - else { - struct ublk_io *io =3D ublk_get_io(q, tag); - - t->io_inflight +=3D queued; - io->tgt_ios =3D queued; - io->result =3D 0; - } -} - static inline int ublk_completed_tgt_io(struct ublk_thread *t, struct ublk_queue *q, unsigned tag) { @@ -493,6 +478,42 @@ int ublk_batch_alloc_buf(struct ublk_thread *t); /* Free commit buffers and cleanup batch allocator */ void ublk_batch_free_buf(struct ublk_thread *t); =20 +/* Prepare a new commit buffer for batching completed I/O operations */ +void ublk_batch_prep_commit(struct ublk_thread *t); +/* Submit UBLK_U_IO_COMMIT_IO_CMDS with batched completed I/O operations */ +void ublk_batch_commit_io_cmds(struct ublk_thread *t); +/* Add a completed I/O operation to the current batch commit buffer */ +void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q, + unsigned tag, int res); + +static inline int ublk_complete_io(struct ublk_thread *t, struct ublk_queu= e *q, + unsigned tag, int res) +{ + if (ublk_queue_batch_io(q)) { + ublk_batch_complete_io(t, q, tag, res); + return 0; + } else { + struct ublk_io *io =3D &q->ios[tag]; + + ublk_mark_io_done(io, res); + return ublk_queue_io_cmd(t, io); + } +} + +static inline void ublk_queued_tgt_io(struct ublk_thread *t, struct ublk_q= ueue *q, + unsigned tag, int queued) +{ + if (queued < 0) + ublk_complete_io(t, q, tag, queued); + else { + struct ublk_io *io =3D ublk_get_io(q, tag); + + t->io_inflight +=3D queued; + io->tgt_ios =3D queued; + io->result =3D 0; + } +} + extern const struct ublk_tgt_ops null_tgt_ops; extern const struct ublk_tgt_ops loop_tgt_ops; extern const struct ublk_tgt_ops stripe_tgt_ops; --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FEEA2DCC1B for ; Fri, 21 Nov 2025 02:00:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690452; cv=none; b=Uuw/baTjzc51h6IwBoksuB9/lRm96QDRUttKzGorP0CLBWW1oksyYsfJ8qMAYlEdLmPalu7FPSEXQWkziLeoLDv6cBOd+0yKyafVsL56KnWQUraZZmhM0dgdfw+ktqMXAq4MOjBoI+zg+ybYB6BJITzEQEINdXKC5tIHjg62fGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690452; c=relaxed/simple; bh=0YiZ2Y84rU5lfo5m+i04VaZBD5DjMht7Ah3qPqgITq0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EjGW7CdeWvSJh2iVDlESxh2ohsCpVJKLfR/P1CHrb0iS2gw82m6u+7/Z5LdpeDEqZLMnZJf8lU5RT4HRa96IacomNdLNLymqLMPyHtTlXa0pvsobhvDXQof9qzIlCZQcp0dkesfLrU7LODVqxzy+eMpg2UM2PA9vO1IFGslDDB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TVxNc6L7; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TVxNc6L7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690449; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rJ9+bjNk9l3ZWyw+GB/6UrQf0WcvmWDX5OqUGGkYIGU=; b=TVxNc6L7gmMnuRRmpfVkiMDGQcwy33/pQmqkxvdAw/ji7aKCjeLENzGPZQcsIk6JRvs0hD IX14aEuh3gl0Eyk95v70Sus7SeVxbS2/6ZDOVw7CJN08PSY2mG9SEVyJiRSD7F7W3NKMFt GHB5eRBKx8RDvjBdMciaaYgppyij4IU= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-328-bAKQdnctPPe5SGkU09aQ_A-1; Thu, 20 Nov 2025 21:00:45 -0500 X-MC-Unique: bAKQdnctPPe5SGkU09aQ_A-1 X-Mimecast-MFC-AGG-ID: bAKQdnctPPe5SGkU09aQ_A_1763690444 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7B10E18002C8; Fri, 21 Nov 2025 02:00:44 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3C76E1956045; Fri, 21 Nov 2025 02:00:42 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 25/27] selftests: ublk: handle UBLK_U_IO_FETCH_IO_CMDS Date: Fri, 21 Nov 2025 09:58:47 +0800 Message-ID: <20251121015851.3672073-26-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add support for UBLK_U_IO_FETCH_IO_CMDS to enable efficient batch fetching of I/O commands using multishot io_uring operations. Key improvements: - Implement multishot UBLK_U_IO_FETCH_IO_CMDS for continuous command fetchi= ng - Add fetch buffer management with page-aligned, mlocked buffers - Process fetched I/O command tags from kernel-provided buffers - Integrate fetch operations with existing batch I/O infrastructure - Significantly reduce uring_cmd issuing overhead through batching The implementation uses two fetch buffers per thread with automatic requeuing to maintain continuous I/O command flow. Each fetch operation retrieves multiple command tags in a single syscall, dramatically improving performance compared to individual command fetching. Technical details: - Fetch buffers are page-aligned and mlocked for optimal performance - Uses IORING_URING_CMD_MULTISHOT for continuous operation - Automatic buffer management and requeuing on completion - Enhanced CQE handling for fetch command completions Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/batch.c | 134 ++++++++++++++++++++++++++- tools/testing/selftests/ublk/kublk.c | 14 ++- tools/testing/selftests/ublk/kublk.h | 13 +++ 3 files changed, 157 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/ublk/batch.c b/tools/testing/selftests= /ublk/batch.c index e240d4decedf..7db91f910944 100644 --- a/tools/testing/selftests/ublk/batch.c +++ b/tools/testing/selftests/ublk/batch.c @@ -140,15 +140,63 @@ void ublk_batch_prepare(struct ublk_thread *t) t->nr_bufs); } =20 +static void free_batch_fetch_buf(struct ublk_thread *t) +{ + int i; + + for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) { + io_uring_free_buf_ring(&t->ring, t->fetch[i].br, 1, i); + munlock(t->fetch[i].fetch_buf, t->fetch[i].fetch_buf_size); + free(t->fetch[i].fetch_buf); + } +} + +static int alloc_batch_fetch_buf(struct ublk_thread *t) +{ + /* page aligned fetch buffer, and it is mlocked for speedup delivery */ + unsigned pg_sz =3D getpagesize(); + unsigned buf_size =3D round_up(t->dev->dev_info.queue_depth * 2, pg_sz); + int ret; + int i =3D 0; + + for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) { + t->fetch[i].fetch_buf_size =3D buf_size; + + if (posix_memalign((void **)&t->fetch[i].fetch_buf, pg_sz, + t->fetch[i].fetch_buf_size)) + return -ENOMEM; + + /* lock fetch buffer page for fast fetching */ + if (mlock(t->fetch[i].fetch_buf, t->fetch[i].fetch_buf_size)) + ublk_err("%s: can't lock fetch buffer %s\n", __func__, + strerror(errno)); + t->fetch[i].br =3D io_uring_setup_buf_ring(&t->ring, 1, + i, IOU_PBUF_RING_INC, &ret); + if (!t->fetch[i].br) { + ublk_err("Buffer ring register failed %d\n", ret); + return ret; + } + } + + return 0; +} + int ublk_batch_alloc_buf(struct ublk_thread *t) { + int ret; + ublk_assert(t->nr_commit_buf < 16); - return alloc_batch_commit_buf(t); + + ret =3D alloc_batch_commit_buf(t); + if (ret) + return ret; + return alloc_batch_fetch_buf(t); } =20 void ublk_batch_free_buf(struct ublk_thread *t) { free_batch_commit_buf(t); + free_batch_fetch_buf(t); } =20 static void ublk_init_batch_cmd(struct ublk_thread *t, __u16 q_id, @@ -199,6 +247,76 @@ static void ublk_setup_commit_sqe(struct ublk_thread *= t, cmd->flags |=3D t->cmd_flags; } =20 +static void ublk_batch_queue_fetch(struct ublk_thread *t, + struct ublk_queue *q, + unsigned short buf_idx) +{ + unsigned short nr_elem =3D t->fetch[buf_idx].fetch_buf_size / 2; + struct io_uring_sqe *sqe; + + io_uring_buf_ring_add(t->fetch[buf_idx].br, t->fetch[buf_idx].fetch_buf, + t->fetch[buf_idx].fetch_buf_size, + 0, 0, 0); + io_uring_buf_ring_advance(t->fetch[buf_idx].br, 1); + + ublk_io_alloc_sqes(t, &sqe, 1); + + ublk_init_batch_cmd(t, q->q_id, sqe, UBLK_U_IO_FETCH_IO_CMDS, 2, nr_elem, + buf_idx); + + sqe->rw_flags=3D IORING_URING_CMD_MULTISHOT; + sqe->buf_group =3D buf_idx; + sqe->flags |=3D IOSQE_BUFFER_SELECT; + + t->fetch[buf_idx].fetch_buf_off =3D 0; +} + +void ublk_batch_start_fetch(struct ublk_thread *t, + struct ublk_queue *q) +{ + int i; + + for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) + ublk_batch_queue_fetch(t, q, i); +} + +static unsigned short ublk_compl_batch_fetch(struct ublk_thread *t, + struct ublk_queue *q, + const struct io_uring_cqe *cqe) +{ + unsigned short buf_idx =3D user_data_to_tag(cqe->user_data); + unsigned start =3D t->fetch[buf_idx].fetch_buf_off; + unsigned end =3D start + cqe->res; + void *buf =3D t->fetch[buf_idx].fetch_buf; + int i; + + if (cqe->res < 0) + return buf_idx; + + if ((end - start) / 2 > q->q_depth) { + ublk_err("%s: fetch duplicated ios offset %u count %u\n", _= _func__, start, cqe->res); + + for (i =3D start; i < end; i +=3D 2) { + unsigned short tag =3D *(unsigned short *)(buf + i); + + ublk_err("%u ", tag); + } + ublk_err("\n"); + } + + for (i =3D start; i < end; i +=3D 2) { + unsigned short tag =3D *(unsigned short *)(buf + i); + + if (tag >=3D q->q_depth) + ublk_err("%s: bad tag %u\n", __func__, tag); + + if (q->tgt_ops->queue_io) + q->tgt_ops->queue_io(t, q, tag); + } + t->fetch[buf_idx].fetch_buf_off =3D end; + return buf_idx; +} + int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q) { unsigned short nr_elem =3D q->q_depth; @@ -258,12 +376,26 @@ void ublk_batch_compl_cmd(struct ublk_thread *t, const struct io_uring_cqe *cqe) { unsigned op =3D user_data_to_op(cqe->user_data); + struct ublk_queue *q; + unsigned buf_idx; + unsigned q_id; =20 if (op =3D=3D _IOC_NR(UBLK_U_IO_PREP_IO_CMDS) || op =3D=3D _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) { ublk_batch_compl_commit_cmd(t, cqe, op); return; } + + /* FETCH command is per queue */ + q_id =3D user_data_to_q_id(cqe->user_data); + q =3D &t->dev->q[q_id]; + buf_idx =3D ublk_compl_batch_fetch(t, q, cqe); + + if (cqe->res < 0 && cqe->res !=3D -ENOBUFS) { + t->state |=3D UBLKS_T_STOPPING; + } else if (!(cqe->flags & IORING_CQE_F_MORE) || cqe->res =3D=3D -ENOBUFS)= { + ublk_batch_queue_fetch(t, q, buf_idx); + } } =20 void ublk_batch_commit_io_cmds(struct ublk_thread *t) diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index 6565e804679c..cb329c7aebc4 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -493,6 +493,10 @@ static int ublk_thread_init(struct ublk_thread *t, uns= igned long long extra_flag int ring_depth =3D dev->tgt.sq_depth, cq_depth =3D dev->tgt.cq_depth; int ret; =20 + /* FETCH_IO_CMDS is multishot, so increase cq depth for BATCH_IO */ + if (ublk_dev_batch_io(dev)) + cq_depth +=3D dev->dev_info.queue_depth; + ret =3D ublk_setup_ring(&t->ring, ring_depth, cq_depth, IORING_SETUP_COOP_TASKRUN | IORING_SETUP_SINGLE_ISSUER | @@ -797,7 +801,7 @@ static void ublk_handle_cqe(struct ublk_thread *t, unsigned q_id =3D user_data_to_q_id(cqe->user_data); unsigned cmd_op =3D user_data_to_op(cqe->user_data); =20 - if (cqe->res < 0 && cqe->res !=3D -ENODEV) + if (cqe->res < 0 && cqe->res !=3D -ENODEV && cqe->res !=3D -ENOBUFS) ublk_err("%s: res %d userdata %llx thread state %x\n", __func__, cqe->res, cqe->user_data, t->state); =20 @@ -922,9 +926,13 @@ static __attribute__((noinline)) int __ublk_io_handler= _fn(struct ublk_thread_inf if (!ublk_thread_batch_io(&t)) { /* submit all io commands to ublk driver */ ublk_submit_fetch_commands(&t); - } else if (!t.idx) { + } else { + struct ublk_queue *q =3D &t.dev->q[t.idx]; + /* prepare all io commands in the 1st thread context */ - ublk_batch_setup_queues(&t); + if (!t.idx) + ublk_batch_setup_queues(&t); + ublk_batch_start_fetch(&t, q); } =20 do { diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index 0a355653d64c..222501048c24 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -190,6 +190,13 @@ struct batch_commit_buf { unsigned short count; }; =20 +struct batch_fetch_buf { + struct io_uring_buf_ring *br; + void *fetch_buf; + unsigned int fetch_buf_size; + unsigned int fetch_buf_off; +}; + struct ublk_thread { struct ublk_dev *dev; unsigned idx; @@ -216,6 +223,9 @@ struct ublk_thread { #define UBLKS_T_COMMIT_BUF_INV_IDX ((unsigned short)-1) struct allocator commit_buf_alloc; struct batch_commit_buf commit; + /* FETCH_IO_CMDS buffer */ +#define UBLKS_T_NR_FETCH_BUF 2 + struct batch_fetch_buf fetch[UBLKS_T_NR_FETCH_BUF]; =20 struct io_uring ring; }; @@ -468,6 +478,9 @@ static inline unsigned short ublk_batch_io_buf_idx( =20 /* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */ int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q); +/* Start fetching I/O commands using multishot UBLK_U_IO_FETCH_IO_CMDS */ +void ublk_batch_start_fetch(struct ublk_thread *t, + struct ublk_queue *q); /* Handle completion of batch I/O commands (prep/commit) */ void ublk_batch_compl_cmd(struct ublk_thread *t, const struct io_uring_cqe *cqe); --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E750D2E041A for ; Fri, 21 Nov 2025 02:00:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690455; cv=none; b=kvvkVQhBn4Ok4GpxWf7eSEKV67vN9oBmK9UPETM2GfUCsYxjCgc1c1exjwPse9CE6HUyl06y3moGt2c6qp1Bj2YLDk24TIXHcIZFIzSVXHouUwh5LMhcCVzZxVGaW0cilxe+p6CSlKvbFJR15qf0s6KmZxAHKULxSCV3M5TcIj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690455; c=relaxed/simple; bh=+lj4XH59eLy07Pif77Sg8M6BrlekF/3Csdp6qluJgDo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FyqzizqwsniHrE/u6y2KYfYKeFBWV45bgA+zIpuE9vbkO8Y1AB9F7/ELN+HYO65boTXdM9ykenZrNHRE3NXEne9F8n3IExwZ/307hLUIsfY9GNTLFGSl5n8157WsZOph57WxcWQHVzyyw0xlzpoe9JsV5pbkgIDpXE/ib17+fEI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=a+EhGprI; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="a+EhGprI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690453; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XnIP039ix3jbD37KSqwje17mbJ/g+phy83hpuyRLDwE=; b=a+EhGprIdwkZbo/fYEjhRPXRal7l6yoVQCsYo/Y0qlXdkibkRhxq5QXJO9K7NA6fVDLP9P Of0k1bBHtPqg08d/hPHux8Xfu/cvy7CyTHiQFRHqZc36GoP9Lu5QM7G9YpunqXNn4Ct7N5 w7wp3/T2LkDwLSDTHkRYadOtX3fd930= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-98-BgRO1fFAPQmiWhz_YqvEoQ-1; Thu, 20 Nov 2025 21:00:49 -0500 X-MC-Unique: BgRO1fFAPQmiWhz_YqvEoQ-1 X-Mimecast-MFC-AGG-ID: BgRO1fFAPQmiWhz_YqvEoQ_1763690448 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 356001956046; Fri, 21 Nov 2025 02:00:48 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 45C981956045; Fri, 21 Nov 2025 02:00:46 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 26/27] selftests: ublk: add --batch/-b for enabling F_BATCH_IO Date: Fri, 21 Nov 2025 09:58:48 +0800 Message-ID: <20251121015851.3672073-27-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add --batch/-b for enabling F_BATCH_IO. Add generic_14 for covering its basic function. Add stress_06 and stress_07 for covering stress test. Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/Makefile | 3 ++ tools/testing/selftests/ublk/kublk.c | 13 +++++- .../testing/selftests/ublk/test_generic_14.sh | 32 +++++++++++++ .../testing/selftests/ublk/test_stress_06.sh | 45 +++++++++++++++++++ .../testing/selftests/ublk/test_stress_07.sh | 44 ++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/ublk/test_generic_14.sh create mode 100755 tools/testing/selftests/ublk/test_stress_06.sh create mode 100755 tools/testing/selftests/ublk/test_stress_07.sh diff --git a/tools/testing/selftests/ublk/Makefile b/tools/testing/selftest= s/ublk/Makefile index a724276622d0..cbf57113b1a6 100644 --- a/tools/testing/selftests/ublk/Makefile +++ b/tools/testing/selftests/ublk/Makefile @@ -21,6 +21,7 @@ TEST_PROGS +=3D test_generic_10.sh TEST_PROGS +=3D test_generic_11.sh TEST_PROGS +=3D test_generic_12.sh TEST_PROGS +=3D test_generic_13.sh +TEST_PROGS +=3D test_generic_14.sh =20 TEST_PROGS +=3D test_null_01.sh TEST_PROGS +=3D test_null_02.sh @@ -39,6 +40,8 @@ TEST_PROGS +=3D test_stress_02.sh TEST_PROGS +=3D test_stress_03.sh TEST_PROGS +=3D test_stress_04.sh TEST_PROGS +=3D test_stress_05.sh +TEST_PROGS +=3D test_stress_06.sh +TEST_PROGS +=3D test_stress_07.sh =20 TEST_GEN_PROGS_EXTENDED =3D kublk =20 diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index cb329c7aebc4..4c45482a847c 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -1476,6 +1476,7 @@ static int cmd_dev_get_features(void) FEAT_NAME(UBLK_F_QUIESCE), FEAT_NAME(UBLK_F_PER_IO_DAEMON), FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON), + FEAT_NAME(UBLK_F_BATCH_IO), }; struct ublk_dev *dev; __u64 features =3D 0; @@ -1571,6 +1572,7 @@ static void __cmd_create_help(char *exe, bool recover= y) printf("\t[--foreground] [--quiet] [-z] [--auto_zc] [--auto_zc_fallback] = [--debug_mask mask] [-r 0|1 ] [-g]\n"); printf("\t[-e 0|1 ] [-i 0|1] [--no_ublk_fixed_fd]\n"); printf("\t[--nthreads threads] [--per_io_tasks]\n"); + printf("\t[--batch|-b]\n"); printf("\t[target options] [backfile1] [backfile2] ...\n"); printf("\tdefault: nr_queues=3D2(max 32), depth=3D128(max 1024), dev_id= =3D-1(auto allocation)\n"); printf("\tdefault: nthreads=3Dnr_queues"); @@ -1633,6 +1635,7 @@ int main(int argc, char *argv[]) { "nthreads", 1, NULL, 0 }, { "per_io_tasks", 0, NULL, 0 }, { "no_ublk_fixed_fd", 0, NULL, 0 }, + { "batch", 0, NULL, 'b'}, { 0, 0, 0, 0 } }; const struct ublk_tgt_ops *ops =3D NULL; @@ -1654,9 +1657,12 @@ int main(int argc, char *argv[]) =20 opterr =3D 0; optind =3D 2; - while ((opt =3D getopt_long(argc, argv, "t:n:d:q:r:e:i:s:gaz", + while ((opt =3D getopt_long(argc, argv, "t:n:d:q:r:e:i:s:gazb", longopts, &option_idx)) !=3D -1) { switch (opt) { + case 'b': + ctx.flags |=3D UBLK_F_BATCH_IO; + break; case 'a': ctx.all =3D 1; break; @@ -1737,6 +1743,11 @@ int main(int argc, char *argv[]) } } =20 + if (ctx.per_io_tasks && (ctx.flags & UBLK_F_BATCH_IO)) { + ublk_err("per_io_task and F_BATCH_IO conflict\n"); + return -EINVAL; + } + /* auto_zc_fallback depends on F_AUTO_BUF_REG & F_SUPPORT_ZERO_COPY */ if (ctx.auto_zc_fallback && !((ctx.flags & UBLK_F_AUTO_BUF_REG) && diff --git a/tools/testing/selftests/ublk/test_generic_14.sh b/tools/testin= g/selftests/ublk/test_generic_14.sh new file mode 100755 index 000000000000..e197961b07f1 --- /dev/null +++ b/tools/testing/selftests/ublk/test_generic_14.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh + +TID=3D"generic_14" +ERR_CODE=3D0 + +if ! _have_feature "BATCH_IO"; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "generic" "test basic function of UBLK_F_BATCH_IO" + +_create_backfile 0 256M +_create_backfile 1 256M + +dev_id=3D$(_add_ublk_dev -t loop -q 2 -b "${UBLK_BACKFILES[0]}") +_check_add_dev $TID $? + +if ! _mkfs_mount_test /dev/ublkb"${dev_id}"; then + _cleanup_test "generic" + _show_result $TID 255 +fi + +dev_id=3D$(_add_ublk_dev -t stripe -b --auto_zc "${UBLK_BACKFILES[0]}" "${= UBLK_BACKFILES[1]}") +_check_add_dev $TID $? +_mkfs_mount_test /dev/ublkb"${dev_id}" +ERR_CODE=3D$? + +_cleanup_test "generic" +_show_result $TID $ERR_CODE diff --git a/tools/testing/selftests/ublk/test_stress_06.sh b/tools/testing= /selftests/ublk/test_stress_06.sh new file mode 100755 index 000000000000..190db0b4f2ad --- /dev/null +++ b/tools/testing/selftests/ublk/test_stress_06.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh +TID=3D"stress_06" +ERR_CODE=3D0 + +ublk_io_and_remove() +{ + run_io_and_remove "$@" + ERR_CODE=3D$? + if [ ${ERR_CODE} -ne 0 ]; then + echo "$TID failure: $*" + _show_result $TID $ERR_CODE + fi +} + +if ! _have_program fio; then + exit "$UBLK_SKIP_CODE" +fi + +if ! _have_feature "ZERO_COPY"; then + exit "$UBLK_SKIP_CODE" +fi +if ! _have_feature "AUTO_BUF_REG"; then + exit "$UBLK_SKIP_CODE" +fi +if ! _have_feature "BATCH_IO"; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "stress" "run IO and remove device(zero copy)" + +_create_backfile 0 256M +_create_backfile 1 128M +_create_backfile 2 128M + +ublk_io_and_remove 8G -t null -q 4 -b & +ublk_io_and_remove 256M -t loop -q 4 --auto_zc -b "${UBLK_BACKFILES[0]}" & +ublk_io_and_remove 256M -t stripe -q 4 --auto_zc -b "${UBLK_BACKFILES[1]}"= "${UBLK_BACKFILES[2]}" & +ublk_io_and_remove 8G -t null -q 4 -z --auto_zc --auto_zc_fallback -b & +wait + +_cleanup_test "stress" +_show_result $TID $ERR_CODE diff --git a/tools/testing/selftests/ublk/test_stress_07.sh b/tools/testing= /selftests/ublk/test_stress_07.sh new file mode 100755 index 000000000000..1b6bdb31da03 --- /dev/null +++ b/tools/testing/selftests/ublk/test_stress_07.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh +TID=3D"stress_07" +ERR_CODE=3D0 + +ublk_io_and_kill_daemon() +{ + run_io_and_kill_daemon "$@" + ERR_CODE=3D$? + if [ ${ERR_CODE} -ne 0 ]; then + echo "$TID failure: $*" + _show_result $TID $ERR_CODE + fi +} + +if ! _have_program fio; then + exit "$UBLK_SKIP_CODE" +fi +if ! _have_feature "ZERO_COPY"; then + exit "$UBLK_SKIP_CODE" +fi +if ! _have_feature "AUTO_BUF_REG"; then + exit "$UBLK_SKIP_CODE" +fi +if ! _have_feature "BATCH_IO"; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "stress" "run IO and kill ublk server(zero copy)" + +_create_backfile 0 256M +_create_backfile 1 128M +_create_backfile 2 128M + +ublk_io_and_kill_daemon 8G -t null -q 4 -z -b & +ublk_io_and_kill_daemon 256M -t loop -q 4 --auto_zc -b "${UBLK_BACKFILES[0= ]}" & +ublk_io_and_kill_daemon 256M -t stripe -q 4 -b "${UBLK_BACKFILES[1]}" "${U= BLK_BACKFILES[2]}" & +ublk_io_and_kill_daemon 8G -t null -q 4 -z --auto_zc --auto_zc_fallback -b= & +wait + +_cleanup_test "stress" +_show_result $TID $ERR_CODE --=20 2.47.0 From nobody Tue Dec 2 01:51:44 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 119472E5405 for ; Fri, 21 Nov 2025 02:00:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690458; cv=none; b=jNlCepw47IDujRNn/JoV71Ri8K81o/2GMxeQrCKCzpM597RBE7ReJ0kMTMZk8P4b/aBlfIPqVeQqdd83FGi4/yYkCq/ZiNncNIsPcC5iK3ZOOW/WsAejcjaDmTEEmFGotMq/LFg8Crw0IlSNtVD3GqOWIgTg6z9EoDjaqZHE4H0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763690458; c=relaxed/simple; bh=YY9mPoiqq5mS8+hnJbS4J35F/PfORQJLXUIK+tPEQUM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uKJiEGmBUG5prCwuZ9/sT/B4FzQkxvSZvNovyXDuEaGFdpr8wjmhgifcPQzyww8vqs0kDlD7c62N1LFvF2cLqDbYHlUutOyvvuuwLBBKy9ToDnSEf3SN1FPhBxEtosySYP1TkhCRDNKaDXQVQ8LObNgzUNouwbudggb+17SoHQQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gZyyBwUs; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gZyyBwUs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763690455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bBU4yUzkpU7XOjpcV4FFa6IcIMZ2uo+9+EbDZIGoy/A=; b=gZyyBwUseuASw0cNkMxJ4Bg7DTg1nIjB7yryoUcH1l6LOO0zth9KyQtdwQxBX42nFHwt0f qCZzgb+LkGxbHA6APVGs91KqenbFG6VuDFhK+GUi+eqp9FyKXqLxWx6j3wg07aLT0DJz4E /IXmIWxB+Yzf7bdz4b5QYMe84U4HaV0= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-160-9KSi000MOJyU0uH2BNu2Fg-1; Thu, 20 Nov 2025 21:00:53 -0500 X-MC-Unique: 9KSi000MOJyU0uH2BNu2Fg-1 X-Mimecast-MFC-AGG-ID: 9KSi000MOJyU0uH2BNu2Fg_1763690451 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C67D41956066; Fri, 21 Nov 2025 02:00:51 +0000 (UTC) Received: from localhost (unknown [10.72.116.211]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EBFAF1956045; Fri, 21 Nov 2025 02:00:50 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Caleb Sander Mateos , Uday Shankar , Stefani Seibold , Andrew Morton , linux-kernel@vger.kernel.org, Ming Lei Subject: [PATCH V4 27/27] selftests: ublk: support arbitrary threads/queues combination Date: Fri, 21 Nov 2025 09:58:49 +0800 Message-ID: <20251121015851.3672073-28-ming.lei@redhat.com> In-Reply-To: <20251121015851.3672073-1-ming.lei@redhat.com> References: <20251121015851.3672073-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Enable flexible thread-to-queue mapping in batch I/O mode to support arbitrary combinations of threads and queues, improving resource utilization and scalability. Key improvements: - Support N:M thread-to-queue mapping (previously limited to 1:1) - Dynamic buffer allocation based on actual queue assignment per thread - Thread-safe queue preparation with spinlock protection - Intelligent buffer index calculation for multi-queue scenarios - Enhanced validation for thread/queue combination constraints Implementation details: - Add q_thread_map matrix to track queue-to-thread assignments - Dynamic allocation of commit and fetch buffers per thread - Round-robin queue assignment algorithm for load balancing - Per-queue spinlock to prevent race conditions during prep - Updated buffer index calculation using queue position within thread This enables efficient configurations like: - Any other N:M combinations for optimal resource matching Testing: - Added test_generic_15.sh: 4 threads vs 1 queue - Added test_generic_16.sh: 1 thread vs 4 queues - Validates correctness across different mapping scenarios Signed-off-by: Ming Lei --- tools/testing/selftests/ublk/Makefile | 2 + tools/testing/selftests/ublk/batch.c | 199 +++++++++++++++--- tools/testing/selftests/ublk/kublk.c | 49 ++++- tools/testing/selftests/ublk/kublk.h | 40 +++- .../testing/selftests/ublk/test_generic_15.sh | 30 +++ .../testing/selftests/ublk/test_generic_16.sh | 30 +++ 6 files changed, 302 insertions(+), 48 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_15.sh create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh diff --git a/tools/testing/selftests/ublk/Makefile b/tools/testing/selftest= s/ublk/Makefile index cbf57113b1a6..3dbd9a857716 100644 --- a/tools/testing/selftests/ublk/Makefile +++ b/tools/testing/selftests/ublk/Makefile @@ -22,6 +22,8 @@ TEST_PROGS +=3D test_generic_11.sh TEST_PROGS +=3D test_generic_12.sh TEST_PROGS +=3D test_generic_13.sh TEST_PROGS +=3D test_generic_14.sh +TEST_PROGS +=3D test_generic_15.sh +TEST_PROGS +=3D test_generic_16.sh =20 TEST_PROGS +=3D test_null_01.sh TEST_PROGS +=3D test_null_02.sh diff --git a/tools/testing/selftests/ublk/batch.c b/tools/testing/selftests= /ublk/batch.c index 7db91f910944..db0747f13768 100644 --- a/tools/testing/selftests/ublk/batch.c +++ b/tools/testing/selftests/ublk/batch.c @@ -76,6 +76,7 @@ static void free_batch_commit_buf(struct ublk_thread *t) free(t->commit_buf); } allocator_deinit(&t->commit_buf_alloc); + free(t->commit); } =20 static int alloc_batch_commit_buf(struct ublk_thread *t) @@ -84,7 +85,13 @@ static int alloc_batch_commit_buf(struct ublk_thread *t) unsigned int total =3D buf_size * t->nr_commit_buf; unsigned int page_sz =3D getpagesize(); void *buf =3D NULL; - int ret; + int i, ret, j =3D 0; + + t->commit =3D calloc(t->nr_queues, sizeof(*t->commit)); + for (i =3D 0; i < t->dev->dev_info.nr_hw_queues; i++) { + if (t->q_map[i]) + t->commit[j++].q_id =3D i; + } =20 allocator_init(&t->commit_buf_alloc, t->nr_commit_buf); =20 @@ -107,6 +114,17 @@ static int alloc_batch_commit_buf(struct ublk_thread *= t) return ret; } =20 +static unsigned int ublk_thread_nr_queues(const struct ublk_thread *t) +{ + int i; + int ret =3D 0; + + for (i =3D 0; i < t->dev->dev_info.nr_hw_queues; i++) + ret +=3D !!t->q_map[i]; + + return ret; +} + void ublk_batch_prepare(struct ublk_thread *t) { /* @@ -119,10 +137,13 @@ void ublk_batch_prepare(struct ublk_thread *t) */ struct ublk_queue *q =3D &t->dev->q[0]; =20 + /* cache nr_queues because we don't support dynamic load-balance yet */ + t->nr_queues =3D ublk_thread_nr_queues(t); + t->commit_buf_elem_size =3D ublk_commit_elem_buf_size(t->dev); t->commit_buf_size =3D ublk_commit_buf_size(t); t->commit_buf_start =3D t->nr_bufs; - t->nr_commit_buf =3D 2; + t->nr_commit_buf =3D 2 * t->nr_queues; t->nr_bufs +=3D t->nr_commit_buf; =20 t->cmd_flags =3D 0; @@ -144,11 +165,12 @@ static void free_batch_fetch_buf(struct ublk_thread *= t) { int i; =20 - for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) { + for (i =3D 0; i < t->nr_fetch_bufs; i++) { io_uring_free_buf_ring(&t->ring, t->fetch[i].br, 1, i); munlock(t->fetch[i].fetch_buf, t->fetch[i].fetch_buf_size); free(t->fetch[i].fetch_buf); } + free(t->fetch); } =20 static int alloc_batch_fetch_buf(struct ublk_thread *t) @@ -159,7 +181,12 @@ static int alloc_batch_fetch_buf(struct ublk_thread *t) int ret; int i =3D 0; =20 - for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) { + /* double fetch buffer for each queue */ + t->nr_fetch_bufs =3D t->nr_queues * 2; + t->fetch =3D calloc(t->nr_fetch_bufs, sizeof(*t->fetch)); + + /* allocate one buffer for each queue */ + for (i =3D 0; i < t->nr_fetch_bufs; i++) { t->fetch[i].fetch_buf_size =3D buf_size; =20 if (posix_memalign((void **)&t->fetch[i].fetch_buf, pg_sz, @@ -185,7 +212,7 @@ int ublk_batch_alloc_buf(struct ublk_thread *t) { int ret; =20 - ublk_assert(t->nr_commit_buf < 16); + ublk_assert(t->nr_commit_buf < 2 * UBLK_MAX_QUEUES); =20 ret =3D alloc_batch_commit_buf(t); if (ret) @@ -271,13 +298,20 @@ static void ublk_batch_queue_fetch(struct ublk_thread= *t, t->fetch[buf_idx].fetch_buf_off =3D 0; } =20 -void ublk_batch_start_fetch(struct ublk_thread *t, - struct ublk_queue *q) +void ublk_batch_start_fetch(struct ublk_thread *t) { int i; + int j =3D 0; + + for (i =3D 0; i < t->dev->dev_info.nr_hw_queues; i++) { + if (t->q_map[i]) { + struct ublk_queue *q =3D &t->dev->q[i]; =20 - for (i =3D 0; i < UBLKS_T_NR_FETCH_BUF; i++) - ublk_batch_queue_fetch(t, q, i); + /* submit two fetch commands for each queue */ + ublk_batch_queue_fetch(t, q, j++); + ublk_batch_queue_fetch(t, q, j++); + } + } } =20 static unsigned short ublk_compl_batch_fetch(struct ublk_thread *t, @@ -317,7 +351,7 @@ static unsigned short ublk_compl_batch_fetch(struct ubl= k_thread *t, return buf_idx; } =20 -int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q) +static int __ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct u= blk_queue *q) { unsigned short nr_elem =3D q->q_depth; unsigned short buf_idx =3D ublk_alloc_commit_buf(t); @@ -354,6 +388,22 @@ int ublk_batch_queue_prep_io_cmds(struct ublk_thread *= t, struct ublk_queue *q) return 0; } =20 +int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q) +{ + int ret =3D 0; + + pthread_spin_lock(&q->lock); + if (q->flags & UBLKS_Q_PREPARED) + goto unlock; + ret =3D __ublk_batch_queue_prep_io_cmds(t, q); + if (!ret) + q->flags |=3D UBLKS_Q_PREPARED; +unlock: + pthread_spin_unlock(&q->lock); + + return ret; +} + static void ublk_batch_compl_commit_cmd(struct ublk_thread *t, const struct io_uring_cqe *cqe, unsigned op) @@ -398,59 +448,89 @@ void ublk_batch_compl_cmd(struct ublk_thread *t, } } =20 -void ublk_batch_commit_io_cmds(struct ublk_thread *t) +static void __ublk_batch_commit_io_cmds(struct ublk_thread *t, + struct batch_commit_buf *cb) { struct io_uring_sqe *sqe; unsigned short buf_idx; - unsigned short nr_elem =3D t->commit.done; + unsigned short nr_elem =3D cb->done; =20 /* nothing to commit */ if (!nr_elem) { - ublk_free_commit_buf(t, t->commit.buf_idx); + ublk_free_commit_buf(t, cb->buf_idx); return; } =20 ublk_io_alloc_sqes(t, &sqe, 1); - buf_idx =3D t->commit.buf_idx; - sqe->addr =3D (__u64)t->commit.elem; + buf_idx =3D cb->buf_idx; + sqe->addr =3D (__u64)cb->elem; sqe->len =3D nr_elem * t->commit_buf_elem_size; =20 /* commit isn't per-queue command */ - ublk_init_batch_cmd(t, t->commit.q_id, sqe, UBLK_U_IO_COMMIT_IO_CMDS, + ublk_init_batch_cmd(t, cb->q_id, sqe, UBLK_U_IO_COMMIT_IO_CMDS, t->commit_buf_elem_size, nr_elem, buf_idx); ublk_setup_commit_sqe(t, sqe, buf_idx); } =20 -static void ublk_batch_init_commit(struct ublk_thread *t, - unsigned short buf_idx) +void ublk_batch_commit_io_cmds(struct ublk_thread *t) +{ + int i; + + for (i =3D 0; i < t->nr_queues; i++) { + struct batch_commit_buf *cb =3D &t->commit[i]; + + if (cb->buf_idx !=3D UBLKS_T_COMMIT_BUF_INV_IDX) + __ublk_batch_commit_io_cmds(t, cb); + } + +} + +static void __ublk_batch_init_commit(struct ublk_thread *t, + struct batch_commit_buf *cb, + unsigned short buf_idx) { /* so far only support 1:1 queue/thread mapping */ - t->commit.q_id =3D t->idx; - t->commit.buf_idx =3D buf_idx; - t->commit.elem =3D ublk_get_commit_buf(t, buf_idx); - t->commit.done =3D 0; - t->commit.count =3D t->commit_buf_size / + cb->buf_idx =3D buf_idx; + cb->elem =3D ublk_get_commit_buf(t, buf_idx); + cb->done =3D 0; + cb->count =3D t->commit_buf_size / t->commit_buf_elem_size; } =20 -void ublk_batch_prep_commit(struct ublk_thread *t) +/* COMMIT_IO_CMDS is per-queue command, so use its own commit buffer */ +static void ublk_batch_init_commit(struct ublk_thread *t, + struct batch_commit_buf *cb) { unsigned short buf_idx =3D ublk_alloc_commit_buf(t); =20 ublk_assert(buf_idx !=3D UBLKS_T_COMMIT_BUF_INV_IDX); - ublk_batch_init_commit(t, buf_idx); + ublk_assert(!ublk_batch_commit_prepared(cb)); + + __ublk_batch_init_commit(t, cb, buf_idx); +} + +void ublk_batch_prep_commit(struct ublk_thread *t) +{ + int i; + + for (i =3D 0; i < t->nr_queues; i++) + t->commit[i].buf_idx =3D UBLKS_T_COMMIT_BUF_INV_IDX; } =20 void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q, unsigned tag, int res) { - struct batch_commit_buf *cb =3D &t->commit; - struct ublk_batch_elem *elem =3D (struct ublk_batch_elem *)(cb->elem + - cb->done * t->commit_buf_elem_size); + unsigned q_t_idx =3D ublk_queue_idx_in_thread(t, q); + struct batch_commit_buf *cb =3D &t->commit[q_t_idx]; + struct ublk_batch_elem *elem; struct ublk_io *io =3D &q->ios[tag]; =20 - ublk_assert(q->q_id =3D=3D t->commit.q_id); + if (!ublk_batch_commit_prepared(cb)) + ublk_batch_init_commit(t, cb); + + ublk_assert(q->q_id =3D=3D cb->q_id); =20 + elem =3D (struct ublk_batch_elem *)(cb->elem + cb->done * t->commit_buf_e= lem_size); elem->tag =3D tag; elem->buf_index =3D ublk_batch_io_buf_idx(t, q, tag); elem->result =3D res; @@ -461,3 +541,64 @@ void ublk_batch_complete_io(struct ublk_thread *t, str= uct ublk_queue *q, cb->done +=3D 1; ublk_assert(cb->done <=3D cb->count); } + +void ublk_batch_setup_map(unsigned char (*q_thread_map)[UBLK_MAX_QUEUES], + int nthreads, int queues) +{ + int i, j; + + /* + * Setup round-robin queue-to-thread mapping for arbitrary N:M combinatio= ns. + * + * This algorithm distributes queues across threads (and threads across q= ueues) + * in a balanced round-robin fashion to ensure even load distribution. + * + * Examples: + * - 2 threads, 4 queues: T0=3D[Q0,Q2], T1=3D[Q1,Q3] + * - 4 threads, 2 queues: T0=3D[Q0], T1=3D[Q1], T2=3D[Q0], T3=3D[Q1] + * - 3 threads, 3 queues: T0=3D[Q0], T1=3D[Q1], T2=3D[Q2] (1:1 mapping) + * + * Phase 1: Mark which queues each thread handles (boolean mapping) + */ + for (i =3D 0, j =3D 0; i < queues || j < nthreads; i++, j++) { + q_thread_map[j % nthreads][i % queues] =3D 1; + } + + /* + * Phase 2: Convert boolean mapping to sequential indices within each thr= ead. + * + * Transform from: q_thread_map[thread][queue] =3D 1 (handles queue) + * To: q_thread_map[thread][queue] =3D N (queue index within = thread) + * + * This allows each thread to know the local index of each queue it handl= es, + * which is essential for buffer allocation and management. For example: + * - Thread 0 handling queues [0,2] becomes: q_thread_map[0][0]=3D1, q_th= read_map[0][2]=3D2 + * - Thread 1 handling queues [1,3] becomes: q_thread_map[1][1]=3D1, q_th= read_map[1][3]=3D2 + */ + for (j =3D 0; j < nthreads; j++) { + unsigned char seq =3D 1; + + for (i =3D 0; i < queues; i++) { + if (q_thread_map[j][i]) + q_thread_map[j][i] =3D seq++; + } + } + +#if 0 + for (j =3D 0; j < nthreads; j++) { + printf("thread %0d: ", j); + for (i =3D 0; i < queues; i++) { + if (q_thread_map[j][i]) + printf("%03u ", i); + } + printf("\n"); + } + printf("\n"); + for (j =3D 0; j < nthreads; j++) { + for (i =3D 0; i < queues; i++) { + printf("%03u ", q_thread_map[j][i]); + } + printf("\n"); + } +#endif +} diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests= /ublk/kublk.c index 4c45482a847c..f88a12b5a368 100644 --- a/tools/testing/selftests/ublk/kublk.c +++ b/tools/testing/selftests/ublk/kublk.c @@ -442,6 +442,7 @@ static int ublk_queue_init(struct ublk_queue *q, unsign= ed long long extra_flags) int cmd_buf_size, io_buf_size; unsigned long off; =20 + pthread_spin_init(&q->lock, PTHREAD_PROCESS_PRIVATE); q->tgt_ops =3D dev->tgt.ops; q->flags =3D 0; q->q_depth =3D depth; @@ -495,7 +496,7 @@ static int ublk_thread_init(struct ublk_thread *t, unsi= gned long long extra_flag =20 /* FETCH_IO_CMDS is multishot, so increase cq depth for BATCH_IO */ if (ublk_dev_batch_io(dev)) - cq_depth +=3D dev->dev_info.queue_depth; + cq_depth +=3D dev->dev_info.queue_depth * 2; =20 ret =3D ublk_setup_ring(&t->ring, ring_depth, cq_depth, IORING_SETUP_COOP_TASKRUN | @@ -878,6 +879,7 @@ struct ublk_thread_info { sem_t *ready; cpu_set_t *affinity; unsigned long long extra_flags; + unsigned char (*q_thread_map)[UBLK_MAX_QUEUES]; }; =20 static void ublk_thread_set_sched_affinity(const struct ublk_thread_info *= info) @@ -891,14 +893,18 @@ static void ublk_batch_setup_queues(struct ublk_threa= d *t) { int i; =20 - /* setup all queues in the 1st thread */ for (i =3D 0; i < t->dev->dev_info.nr_hw_queues; i++) { struct ublk_queue *q =3D &t->dev->q[i]; int ret; =20 + /* + * Only prepare io commands in the mapped thread context, + * otherwise io command buffer index may not work as expected + */ + if (t->q_map[i] =3D=3D 0) + continue; + ret =3D ublk_batch_queue_prep_io_cmds(t, q); - ublk_assert(ret =3D=3D 0); - ret =3D ublk_process_io(t); ublk_assert(ret >=3D 0); } } @@ -912,6 +918,10 @@ static __attribute__((noinline)) int __ublk_io_handler= _fn(struct ublk_thread_inf int dev_id =3D info->dev->dev_info.dev_id; int ret; =20 + /* Copy per-thread queue mapping into thread-local variable */ + if (info->q_thread_map) + memcpy(t.q_map, info->q_thread_map[info->idx], sizeof(t.q_map)); + ret =3D ublk_thread_init(&t, info->extra_flags); if (ret) { ublk_err("ublk dev %d thread %u init failed\n", @@ -927,12 +937,8 @@ static __attribute__((noinline)) int __ublk_io_handler= _fn(struct ublk_thread_inf /* submit all io commands to ublk driver */ ublk_submit_fetch_commands(&t); } else { - struct ublk_queue *q =3D &t.dev->q[t.idx]; - - /* prepare all io commands in the 1st thread context */ - if (!t.idx) - ublk_batch_setup_queues(&t); - ublk_batch_start_fetch(&t, q); + ublk_batch_setup_queues(&t); + ublk_batch_start_fetch(&t); } =20 do { @@ -1006,6 +1012,7 @@ static int ublk_start_daemon(const struct dev_ctx *ct= x, struct ublk_dev *dev) struct ublk_thread_info *tinfo; unsigned long long extra_flags =3D 0; cpu_set_t *affinity_buf; + unsigned char (*q_thread_map)[UBLK_MAX_QUEUES] =3D NULL; void *thread_ret; sem_t ready; int ret, i; @@ -1025,6 +1032,16 @@ static int ublk_start_daemon(const struct dev_ctx *c= tx, struct ublk_dev *dev) if (ret) return ret; =20 + if (ublk_dev_batch_io(dev)) { + q_thread_map =3D calloc(dev->nthreads, sizeof(*q_thread_map)); + if (!q_thread_map) { + ret =3D -ENOMEM; + goto fail; + } + ublk_batch_setup_map(q_thread_map, dev->nthreads, + dinfo->nr_hw_queues); + } + if (ctx->auto_zc_fallback) extra_flags =3D UBLKS_Q_AUTO_BUF_REG_FALLBACK; if (ctx->no_ublk_fixed_fd) @@ -1047,6 +1064,7 @@ static int ublk_start_daemon(const struct dev_ctx *ct= x, struct ublk_dev *dev) tinfo[i].idx =3D i; tinfo[i].ready =3D &ready; tinfo[i].extra_flags =3D extra_flags; + tinfo[i].q_thread_map =3D q_thread_map; =20 /* * If threads are not tied 1:1 to queues, setting thread @@ -1066,6 +1084,7 @@ static int ublk_start_daemon(const struct dev_ctx *ct= x, struct ublk_dev *dev) for (i =3D 0; i < dev->nthreads; i++) sem_wait(&ready); free(affinity_buf); + free(q_thread_map); =20 /* everything is fine now, start us */ if (ctx->recovery) @@ -1234,7 +1253,8 @@ static int __cmd_dev_add(const struct dev_ctx *ctx) goto fail; } =20 - if (nthreads !=3D nr_queues && !ctx->per_io_tasks) { + if (nthreads !=3D nr_queues && (!ctx->per_io_tasks && + !(ctx->flags & UBLK_F_BATCH_IO))) { ublk_err("%s: threads %u must be same as queues %u if " "not using per_io_tasks\n", __func__, nthreads, nr_queues); @@ -1758,6 +1778,13 @@ int main(int argc, char *argv[]) return -EINVAL; } =20 + if ((ctx.flags & UBLK_F_AUTO_BUF_REG) && + (ctx.flags & UBLK_F_BATCH_IO) && + (ctx.nthreads > ctx.nr_hw_queues)) { + ublk_err("too many threads for F_AUTO_BUF_REG & F_BATCH_IO\n"); + return -EINVAL; + } + i =3D optind; while (i < argc && ctx.nr_files < MAX_BACK_FILES) { ctx.files[ctx.nr_files++] =3D argv[i++]; diff --git a/tools/testing/selftests/ublk/kublk.h b/tools/testing/selftests= /ublk/kublk.h index 222501048c24..565819cf2dfe 100644 --- a/tools/testing/selftests/ublk/kublk.h +++ b/tools/testing/selftests/ublk/kublk.h @@ -166,12 +166,16 @@ struct ublk_queue { const struct ublk_tgt_ops *tgt_ops; struct ublksrv_io_desc *io_cmd_buf; =20 -/* borrow one bit of ublk uapi flags, which may never be used */ +/* borrow three bit of ublk uapi flags, which may never be used */ #define UBLKS_Q_AUTO_BUF_REG_FALLBACK (1ULL << 63) #define UBLKS_Q_NO_UBLK_FIXED_FD (1ULL << 62) +#define UBLKS_Q_PREPARED (1ULL << 61) __u64 flags; int ublk_fd; /* cached ublk char device fd */ struct ublk_io ios[UBLK_QUEUE_DEPTH]; + + /* used for prep io commands */ + pthread_spinlock_t lock; }; =20 /* align with `ublk_elem_header` */ @@ -198,8 +202,12 @@ struct batch_fetch_buf { }; =20 struct ublk_thread { + /* Thread-local copy of queue-to-thread mapping for this thread */ + unsigned char q_map[UBLK_MAX_QUEUES]; + struct ublk_dev *dev; - unsigned idx; + unsigned short idx; + unsigned short nr_queues; =20 #define UBLKS_T_STOPPING (1U << 0) #define UBLKS_T_IDLE (1U << 1) @@ -222,10 +230,10 @@ struct ublk_thread { void *commit_buf; #define UBLKS_T_COMMIT_BUF_INV_IDX ((unsigned short)-1) struct allocator commit_buf_alloc; - struct batch_commit_buf commit; + struct batch_commit_buf *commit; /* FETCH_IO_CMDS buffer */ -#define UBLKS_T_NR_FETCH_BUF 2 - struct batch_fetch_buf fetch[UBLKS_T_NR_FETCH_BUF]; + unsigned short nr_fetch_bufs; + struct batch_fetch_buf *fetch; =20 struct io_uring ring; }; @@ -465,6 +473,21 @@ static inline int ublk_queue_no_buf(const struct ublk_= queue *q) return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q); } =20 +static inline int ublk_batch_commit_prepared(struct batch_commit_buf *cb) +{ + return cb->buf_idx !=3D UBLKS_T_COMMIT_BUF_INV_IDX; +} + +static inline unsigned ublk_queue_idx_in_thread(const struct ublk_thread *= t, + const struct ublk_queue *q) +{ + unsigned char idx; + + idx =3D t->q_map[q->q_id]; + ublk_assert(idx !=3D 0); + return idx - 1; +} + /* * Each IO's buffer index has to be calculated by this helper for * UBLKS_T_BATCH_IO @@ -473,14 +496,13 @@ static inline unsigned short ublk_batch_io_buf_idx( const struct ublk_thread *t, const struct ublk_queue *q, unsigned tag) { - return tag; + return ublk_queue_idx_in_thread(t, q) * q->q_depth + tag; } =20 /* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */ int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue= *q); /* Start fetching I/O commands using multishot UBLK_U_IO_FETCH_IO_CMDS */ -void ublk_batch_start_fetch(struct ublk_thread *t, - struct ublk_queue *q); +void ublk_batch_start_fetch(struct ublk_thread *t); /* Handle completion of batch I/O commands (prep/commit) */ void ublk_batch_compl_cmd(struct ublk_thread *t, const struct io_uring_cqe *cqe); @@ -498,6 +520,8 @@ void ublk_batch_commit_io_cmds(struct ublk_thread *t); /* Add a completed I/O operation to the current batch commit buffer */ void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q, unsigned tag, int res); +void ublk_batch_setup_map(unsigned char (*q_thread_map)[UBLK_MAX_QUEUES], + int nthreads, int queues); =20 static inline int ublk_complete_io(struct ublk_thread *t, struct ublk_queu= e *q, unsigned tag, int res) diff --git a/tools/testing/selftests/ublk/test_generic_15.sh b/tools/testin= g/selftests/ublk/test_generic_15.sh new file mode 100755 index 000000000000..0afd037235cf --- /dev/null +++ b/tools/testing/selftests/ublk/test_generic_15.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh + +TID=3D"generic_15" +ERR_CODE=3D0 + +if ! _have_feature "BATCH_IO"; then + exit "$UBLK_SKIP_CODE" +fi + +if ! _have_program fio; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "generic" "test UBLK_F_BATCH_IO with 4_threads vs. 1_queues" + +_create_backfile 0 512M + +dev_id=3D$(_add_ublk_dev -t loop -q 1 --nthreads 4 -b "${UBLK_BACKFILES[0]= }") +_check_add_dev $TID $? + +# run fio over the ublk disk +fio --name=3Djob1 --filename=3D/dev/ublkb"${dev_id}" --ioengine=3Dlibaio -= -rw=3Dreadwrite \ + --iodepth=3D32 --size=3D100M --numjobs=3D4 > /dev/null 2>&1 +ERR_CODE=3D$? + +_cleanup_test "generic" +_show_result $TID $ERR_CODE diff --git a/tools/testing/selftests/ublk/test_generic_16.sh b/tools/testin= g/selftests/ublk/test_generic_16.sh new file mode 100755 index 000000000000..32bcf4a3d0b4 --- /dev/null +++ b/tools/testing/selftests/ublk/test_generic_16.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh + +TID=3D"generic_16" +ERR_CODE=3D0 + +if ! _have_feature "BATCH_IO"; then + exit "$UBLK_SKIP_CODE" +fi + +if ! _have_program fio; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "generic" "test UBLK_F_BATCH_IO with 1_threads vs. 4_queues" + +_create_backfile 0 512M + +dev_id=3D$(_add_ublk_dev -t loop -q 4 --nthreads 1 -b "${UBLK_BACKFILES[0]= }") +_check_add_dev $TID $? + +# run fio over the ublk disk +fio --name=3Djob1 --filename=3D/dev/ublkb"${dev_id}" --ioengine=3Dlibaio -= -rw=3Dreadwrite \ + --iodepth=3D32 --size=3D100M --numjobs=3D4 > /dev/null 2>&1 +ERR_CODE=3D$? + +_cleanup_test "generic" +_show_result $TID $ERR_CODE --=20 2.47.0