From nobody Tue Dec 2 02:04:07 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