Skip to contentSkip to conversation

io_uring futex cleanup and fix

PATCH New Jens Axboe 1 participant 3 messages Jun 4, 2025 io_uring

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover io_uring futex cleanup and fix

Hi,

Patch 1 is just a cleanup that I came across while looking into an issue and patch 2 fixes and issue introduced in this merge window with io_uring futex handling. Patch 2 works around the fact that since:

commit 80367ad01d93ac781b0e1df246edaf006928002f

Author: Sebastian Andrzej Siewior <[email protected]>
Date:   Wed Apr 16 18:29:12 2025 +0200

futex: Add basic infrastructure for local task local hash

futex is reliant on ->mm staying alive for the duration of the futex or futexv wait requests, if those requests are using FUTEX2_PRIVATE. These types of futex requests use a per-task private hash queue, and will actively remove those at __mmput() time.

 io_uring/futex.c    | 11 ++++++-----
 io_uring/io_uring.c |  7 ++++++-
 io_uring/io_uring.h |  1 +
 3 files changed, 13 insertions(+), 6 deletions(-)
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/2 io_uring/futex: get rid of struct io_futex addr union

Rather than use a union of a u32 and struct futex_waitv user address, consolidate it into a single void __user pointer instead. This also makes prep easier to use as the store happens to the member that will be used.

No functional changes in this patch.

---
 io_uring/futex.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
io_uring/futex.c +2 -5
index fa374afbaa51..5a3991b0d1a7 100644
@@ -14,10 +14,7 @@
 
 struct io_futex {
 	struct file	*file;
-	union {
-		u32 __user			*uaddr;
-		struct futex_waitv __user	*uwaitv;
-	};
+	void __user	*uaddr;
 	unsigned long	futex_val;
 	unsigned long	futex_mask;
 	unsigned long	futexv_owned;
@@ -186,7 +183,7 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	if (!futexv)
 		return -ENOMEM;
 
-	ret = futex_parse_waitv(futexv, iof->uwaitv, iof->futex_nr,
+	ret = futex_parse_waitv(futexv, iof->uaddr, iof->futex_nr,
 				io_futex_wakev_fn, req);
 	if (ret) {
 		kfree(futexv);
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH 2/2 io_uring/futex: mark wait requests as inflight

In reply to Jens Axboe

Inflight marking is used so that do_exit() -> io_uring_files_cancel() will find requests with files that reference an io_uring instance, so they can get appropriately canceled before the files go away. However, it's also called before the mm goes away.

Mark futex/futexv wait requests as being inflight, so that io_uring_files_cancel() will prune them. This ensures that the mm stays alive, which is important as an exiting mm will also free the futex private hash buckets. An io_uring futex request with FUTEX2_PRIVATE set relies on those being alive until the request has completed. A recent commit added these futex private hashes, which get killed when the mm goes away.

Fixes 80367ad01d93 ("futex: Add basic infrastructure for local task local hash")
---
 io_uring/futex.c    | 4 ++++
 io_uring/io_uring.c | 7 ++++++-
 io_uring/io_uring.h | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)
io_uring/futex.c +4 -0
index 5a3991b0d1a7..692462d50c8c 100644
@@ -145,6 +145,8 @@ int io_futex_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	    !futex_validate_input(iof->futex_flags, iof->futex_mask))
 		return -EINVAL;
 
+	/* Mark as inflight, so file exit cancelation will find it */
+	io_req_track_inflight(req);
 	return 0;
 }
 
@@ -190,6 +192,8 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		return ret;
 	}
 
+	/* Mark as inflight, so file exit cancelation will find it */
+	io_req_track_inflight(req);
 	iof->futexv_owned = 0;
 	iof->futexv_unqueued = 0;
 	req->flags |= REQ_F_ASYNC_DATA;
io_uring/io_uring.c +6 -1
index c7a9cecf528e..cf759c172083 100644
@@ -408,7 +408,12 @@ static void io_clean_op(struct io_kiocb *req)
 	req->flags &= ~IO_REQ_CLEAN_FLAGS;
 }
 
-static inline void io_req_track_inflight(struct io_kiocb *req)
+/*
+ * Mark the request as inflight, so that file cancelation will find it.
+ * Can be used if the file is an io_uring instance, or if the request itself
+ * relies on ->mm being alive for the duration of the request.
+ */
+inline void io_req_track_inflight(struct io_kiocb *req)
 {
 	if (!(req->flags & REQ_F_INFLIGHT)) {
 		req->flags |= REQ_F_INFLIGHT;
io_uring/io_uring.h +1 -0
index 0ea7a435d1de..d59c12277d58 100644
@@ -83,6 +83,7 @@ void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags);
 void __io_commit_cqring_flush(struct io_ring_ctx *ctx);
 
+void io_req_track_inflight(struct io_kiocb *req);
 struct file *io_file_get_normal(struct io_kiocb *req, int fd);
 struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
 			       unsigned issue_flags);
Signature · 1 line
2.49.0