public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ming Lei <[email protected]>
To: Jens Axboe <[email protected]>, [email protected]
Cc: [email protected],
	Pavel Begunkov <[email protected]>,
	Kevin Wolf <[email protected]>, Ming Lei <[email protected]>
Subject: [RFC PATCH V2 6/9] io_uring: support sqe group with members depending on leader
Date: Tue,  7 May 2024 00:22:42 +0800	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Generic sqe group provides flexible way for supporting N:M dependency
between groups.

However, some resource can't cross OPs, such as kernel buffer, otherwise
the buffer may be leaked easily in case that any OP failure or application
panic.

Add flag REQ_F_SQE_GROUP_DEP for allowing members to depend on group leader,
so that group members won't be queued until the leader request is completed,
and we still commit leader's CQE after all members CQE are posted. With this
way, the kernel resource lifetime can be aligned with group leader or group,
one typical use case is to support zero copy for device internal buffer.

This use case may not be generic enough, so set it only for specific OP which
can serve as group leader, meantime we have run out of sqe flags.

Signed-off-by: Ming Lei <[email protected]>
---
 include/linux/io_uring_types.h |  3 +++
 io_uring/io_uring.c            | 36 +++++++++++++++++++++++++---------
 io_uring/io_uring.h            |  3 +++
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 62311b0f0e0b..5cbc9d3346a7 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -477,6 +477,7 @@ enum {
 	REQ_F_BL_NO_RECYCLE_BIT,
 	REQ_F_BUFFERS_COMMIT_BIT,
 	REQ_F_SQE_GROUP_LEADER_BIT,
+	REQ_F_SQE_GROUP_DEP_BIT,
 
 	/* not a real bit, just to check we're not overflowing the space */
 	__REQ_F_LAST_BIT,
@@ -561,6 +562,8 @@ enum {
 	REQ_F_BUFFERS_COMMIT	= IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
 	/* sqe group lead */
 	REQ_F_SQE_GROUP_LEADER	= IO_REQ_FLAG(REQ_F_SQE_GROUP_LEADER_BIT),
+	/* sqe group with members depending on leader */
+	REQ_F_SQE_GROUP_DEP	= IO_REQ_FLAG(REQ_F_SQE_GROUP_DEP_BIT),
 };
 
 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, struct io_tw_state *ts);
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 465f4244fa0b..236765bc786c 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -936,7 +936,14 @@ static __always_inline void io_req_commit_cqe(struct io_kiocb *req,
 
 static inline bool need_queue_group_members(struct io_kiocb *req)
 {
-	return req_is_group_leader(req) && !!req->grp_link;
+	if (likely(!(req->flags & REQ_F_SQE_GROUP)))
+		return false;
+
+	if (!(req->flags & REQ_F_SQE_GROUP_LEADER) ||
+			(req->flags & REQ_F_SQE_GROUP_DEP))
+		return false;
+
+	return !!req->grp_link;
 }
 
 /* Can only be called after this request is issued */
@@ -969,7 +976,7 @@ static void io_fail_group_members(struct io_kiocb *req)
 	req->grp_link = NULL;
 }
 
-static void io_queue_group_members(struct io_kiocb *req, bool async)
+void io_queue_group_members(struct io_kiocb *req, bool async)
 {
 	struct io_kiocb *member = req->grp_link;
 
@@ -980,13 +987,20 @@ static void io_queue_group_members(struct io_kiocb *req, bool async)
 		struct io_kiocb *next = member->grp_link;
 
 		member->grp_link = req;
-		trace_io_uring_submit_req(member);
-		if (async)
-			member->flags |= REQ_F_FORCE_ASYNC;
-		if (member->flags & REQ_F_FORCE_ASYNC)
-			io_req_task_queue(member);
-		else
-			io_queue_sqe(member);
+
+		/* members have to be failed if they depends on leader */
+		if (unlikely((req->flags & REQ_F_FAIL) &&
+			     (req->flags & REQ_F_SQE_GROUP_DEP))) {
+			io_req_task_queue_fail(member, -ECANCELED);
+		} else {
+			trace_io_uring_submit_req(member);
+			if (async)
+				member->flags |= REQ_F_FORCE_ASYNC;
+			if (member->flags & REQ_F_FORCE_ASYNC)
+				io_req_task_queue(member);
+			else
+				io_queue_sqe(member);
+		}
 		member = next;
 	}
 	req->grp_link = NULL;
@@ -1065,6 +1079,10 @@ static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
 		return;
 	}
 
+	/* now queue members if they depends on this leader */
+	if (req_is_group_leader(req) && (req->flags & REQ_F_SQE_GROUP_DEP))
+		io_queue_group_members(req, true);
+
 	io_cq_lock(ctx);
 	if (!(req->flags & REQ_F_CQE_SKIP))
 		io_req_commit_cqe(req, false);
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 416c14c6f050..6cf27204503a 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -69,6 +69,7 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags);
 void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
 bool __io_complete_group_req(struct io_kiocb *req,
 			     struct io_kiocb *lead);
+void io_queue_group_members(struct io_kiocb *req, bool async);
 
 struct file *io_file_get_normal(struct io_kiocb *req, int fd);
 struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
@@ -374,6 +375,8 @@ static inline void io_req_complete_defer(struct io_kiocb *req)
 	if (likely(!req_is_group_leader(req)) ||
 	    __io_complete_group_req(req, req))
 		wq_list_add_tail(&req->comp_list, &state->compl_reqs);
+	else if (req_is_group_leader(req) && (req->flags & REQ_F_SQE_GROUP_DEP))
+		io_queue_group_members(req, false);
 }
 
 static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx)
-- 
2.42.0


  parent reply	other threads:[~2024-05-06 16:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06 16:22 [RFC PATCH V2 0/9] io_uring: support sqe group and provide group kbuf Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 1/9] io_uring: add io_link_req() helper Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 2/9] io_uring: add io_submit_fail_link() helper Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 3/9] io_uring: add helper of io_req_commit_cqe() Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 4/9] io_uring: move marking REQ_F_CQE_SKIP out of io_free_req() Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 5/9] io_uring: support SQE group Ming Lei
2024-05-06 16:22 ` Ming Lei [this message]
2024-05-06 16:22 ` [RFC PATCH V2 7/9] io_uring: support providing sqe group buffer Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 8/9] io_uring/uring_cmd: support provide group kernel buffer Ming Lei
2024-05-06 16:22 ` [RFC PATCH V2 9/9] ublk: support provide io buffer Ming Lei
2024-05-10 14:02 ` [RFC PATCH V2 0/9] io_uring: support sqe group and provide group kbuf Ming Lei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox