Skip to contentSkip to conversation

io_uring: Allow relative lookups

PATCH New Jens Axboe 2 participants 6 messages Feb 9, 2020 – Feb 11, 2020

Subsystems (3)
lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH v2 1/1 io_uring: Allow relative lookups

Due to an oversight on my part, AT_FDCWD lookups only work when the lookup can be done inline, not async. This patchset rectifies that, aiming for 5.6 for this one as it would be a shame to have openat etc without that.

Just 3 small simple patches - add io-wq support for passing in fs and setting it, and add a ->needs_fs to the opcode table list of requirements for opcodes that could potentially do lookups.

Last patch just ensures we allow AT_FDCWD.

Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/3 io-wq: add support for inheriting ->fs

Some work items need this for relative path lookup, make it available like the other inherited credentials/mm/etc.

---
 fs/io-wq.c | 19 +++++++++++++++----
 fs/io-wq.h |  4 +++-
 2 files changed, 18 insertions(+), 5 deletions(-)
fs/io-wq.c +15 -4
index cb60a42b9fdf..58b1891bcfe5 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/kthread.h>
 #include <linux/rculist_nulls.h>
+#include <linux/fs_struct.h>
 
 #include "io-wq.h"
 
@@ -59,6 +60,7 @@ struct io_worker {
 	const struct cred *cur_creds;
 	const struct cred *saved_creds;
 	struct files_struct *restore_files;
+	struct fs_struct *restore_fs;
 };
 
 #if BITS_PER_LONG == 64
@@ -141,13 +143,17 @@ static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
 		worker->cur_creds = worker->saved_creds = NULL;
 	}
 
-	if (current->files != worker->restore_files) {
+	if ((current->files != worker->restore_files) ||
+	    (current->fs != worker->restore_fs)) {
 		__acquire(&wqe->lock);
 		spin_unlock_irq(&wqe->lock);
 		dropped_lock = true;
 
 		task_lock(current);
-		current->files = worker->restore_files;
+		if (current->files != worker->restore_files)
+			current->files = worker->restore_files;
+		if (current->fs != worker->restore_fs)
+			current->fs = worker->restore_fs;
 		task_unlock(current);
 	}
 
@@ -311,6 +317,7 @@ static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
 
 	worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
 	worker->restore_files = current->files;
+	worker->restore_fs = current->fs;
 	io_wqe_inc_running(wqe, worker);
 }
 
@@ -476,9 +483,13 @@ static void io_worker_handle_work(struct io_worker *worker)
 		if (work->flags & IO_WQ_WORK_CB)
 			work->func(&work);
 
-		if (work->files && current->files != work->files) {
+		if ((work->files && current->files != work->files) ||
+		    (work->fs && current->fs != work->fs)) {
 			task_lock(current);
-			current->files = work->files;
+			if (work->files && current->files != work->files)
+				current->files = work->files;
+			if (work->fs && current->fs != work->fs)
+				current->fs = work->fs;
 			task_unlock(current);
 		}
 		if (work->mm != worker->mm)
fs/io-wq.h +3 -1
index 50b3378febf2..f152ba677d8f 100644
@@ -74,6 +74,7 @@ struct io_wq_work {
 	struct files_struct *files;
 	struct mm_struct *mm;
 	const struct cred *creds;
+	struct fs_struct *fs;
 	unsigned flags;
 };
 
@@ -81,10 +82,11 @@ struct io_wq_work {
 	do {						\
 		(work)->list.next = NULL;		\
 		(work)->func = _func;			\
-		(work)->flags = 0;			\
 		(work)->files = NULL;			\
 		(work)->mm = NULL;			\
 		(work)->creds = NULL;			\
+		(work)->fs = NULL;			\
+		(work)->flags = 0;			\
 	} while (0)					\
 
 typedef void (get_work_fn)(struct io_wq_work *);
Signature · 1 line
2.25.0
maintainer
#2 lore raw

Re: [PATCH 1/3] io-wq: add support for inheriting ->fs

Hi,

[This is an automated email]

This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: 5.3+

The bot has tested the following trees: v5.5.2, v5.4.18.

v5.5.2: Failed to apply! Possible dependencies:
05f3fb3c5397 ("io_uring: avoid ring quiesce for fixed file set unregister and update") 15b71abe7b52 ("io_uring: add support for IORING_OP_OPENAT") 66f4af93da57 ("io_uring: add support for probing opcodes")
b5dba59e0cf7 ("io_uring: add support for IORING_OP_CLOSE")
d3656344fea0 ("io_uring: add lookup table for various opcode needs") d63d1b5edb7b ("io_uring: add support for fallocate()")
eddc7ef52a6b ("io_uring: add support for IORING_OP_STATX")
f86cd20c9454 ("io_uring: fix linked command file table usage")

v5.4.18: Failed to apply! Possible dependencies:
561fb04a6a22 ("io_uring: replace workqueue usage with io-wq") 771b53d033e8 ("io-wq: small threadpool implementation for io_uring") ba5290ccb6b5 ("io_uring: replace s->needs_lock with s->in_async") ba816ad61fdf ("io_uring: run dependent links inline if possible") c5def4ab8494 ("io-wq: add support for bounded vs unbunded work") c826bd7a743f ("io_uring: add set of tracing events")
fcb323cc53e2 ("io_uring: io_uring: add support for async work inheriting files")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks,
Sasha
author maintainer
#3 lore raw

PATCH 2/3 io_uring: grab ->fs as part of async preparation

In reply to Jens Axboe

This passes it in to io-wq, so it assumes the right fs_struct when executing async work that may need to do lookups.

---
 fs/io_uring.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
fs/io_uring.c +28 -0
index 1a3ca6577a10..2a7bb178986e 100644
@@ -75,6 +75,7 @@
 #include <linux/fsnotify.h>
 #include <linux/fadvise.h>
 #include <linux/eventpoll.h>
+#include <linux/fs_struct.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/io_uring.h>
@@ -611,6 +612,8 @@ struct io_op_def {
 	unsigned		not_supported : 1;
 	/* needs file table */
 	unsigned		file_table : 1;
+	/* needs ->fs */
+	unsigned		needs_fs : 1;
 };
 
 static const struct io_op_def io_op_defs[] = {
@@ -653,12 +656,14 @@ static const struct io_op_def io_op_defs[] = {
 		.needs_mm		= 1,
 		.needs_file		= 1,
 		.unbound_nonreg_file	= 1,
+		.needs_fs		= 1,
 	},
 	[IORING_OP_RECVMSG] = {
 		.async_ctx		= 1,
 		.needs_mm		= 1,
 		.needs_file		= 1,
 		.unbound_nonreg_file	= 1,
+		.needs_fs		= 1,
 	},
 	[IORING_OP_TIMEOUT] = {
 		.async_ctx		= 1,
@@ -689,6 +694,7 @@ static const struct io_op_def io_op_defs[] = {
 		.needs_file		= 1,
 		.fd_non_neg		= 1,
 		.file_table		= 1,
+		.needs_fs		= 1,
 	},
 	[IORING_OP_CLOSE] = {
 		.needs_file		= 1,
@@ -702,6 +708,7 @@ static const struct io_op_def io_op_defs[] = {
 		.needs_mm		= 1,
 		.needs_file		= 1,
 		.fd_non_neg		= 1,
+		.needs_fs		= 1,
 	},
 	[IORING_OP_READ] = {
 		.needs_mm		= 1,
@@ -733,6 +740,7 @@ static const struct io_op_def io_op_defs[] = {
 		.needs_file		= 1,
 		.fd_non_neg		= 1,
 		.file_table		= 1,
+		.needs_fs		= 1,
 	},
 	[IORING_OP_EPOLL_CTL] = {
 		.unbound_nonreg_file	= 1,
@@ -907,6 +915,16 @@ static inline void io_req_work_grab_env(struct io_kiocb *req,
 	}
 	if (!req->work.creds)
 		req->work.creds = get_current_cred();
+	if (!req->work.fs && def->needs_fs) {
+		spin_lock(&current->fs->lock);
+		if (!current->fs->in_exec) {
+			req->work.fs = current->fs;
+			req->work.fs->users++;
+		} else {
+			req->work.flags |= IO_WQ_WORK_CANCEL;
+		}
+		spin_unlock(&current->fs->lock);
+	}
 }
 
 static inline void io_req_work_drop_env(struct io_kiocb *req)
@@ -919,6 +937,16 @@ static inline void io_req_work_drop_env(struct io_kiocb *req)
 		put_cred(req->work.creds);
 		req->work.creds = NULL;
 	}
+	if (req->work.fs) {
+		struct fs_struct *fs = req->work.fs;
+
+		spin_lock(&req->work.fs->lock);
+		if (--fs->users)
+			fs = NULL;
+		spin_unlock(&req->work.fs->lock);
+		if (fs)
+			free_fs_struct(fs);
+	}
 }
 
 static inline bool io_prep_async_work(struct io_kiocb *req,
Signature · 1 line
2.25.0
maintainer
#4 lore raw

Re: [PATCH 2/3] io_uring: grab ->fs as part of async preparation

Hi,

[This is an automated email]

This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: 5.3+

The bot has tested the following trees: v5.5.2, v5.4.18.

v5.5.2: Failed to apply! Possible dependencies:
05f3fb3c5397 ("io_uring: avoid ring quiesce for fixed file set unregister and update") 15b71abe7b52 ("io_uring: add support for IORING_OP_OPENAT") 3a6820f2bb8a ("io_uring: add non-vectored read/write commands") 3e4827b05d2a ("io_uring: add support for epoll_ctl(2)")
4840e418c2fc ("io_uring: add IORING_OP_FADVISE")
b5dba59e0cf7 ("io_uring: add support for IORING_OP_CLOSE")
d3656344fea0 ("io_uring: add lookup table for various opcode needs") d63d1b5edb7b ("io_uring: add support for fallocate()")
eddc7ef52a6b ("io_uring: add support for IORING_OP_STATX")

v5.4.18: Failed to apply! Possible dependencies:
15b71abe7b52 ("io_uring: add support for IORING_OP_OPENAT")
2665abfd757f ("io_uring: add support for linked SQE timeouts")
3e4827b05d2a ("io_uring: add support for epoll_ctl(2)")
3fbb51c18f5c ("io_uring: move all prep state for IORING_OP_CONNECT to prep handler") 4840e418c2fc ("io_uring: add IORING_OP_FADVISE")
561fb04a6a22 ("io_uring: replace workqueue usage with io-wq")
771b53d033e8 ("io-wq: small threadpool implementation for io_uring") 8ed8d3c3bc32 ("io_uring: any deferred command must have stable sqe data") 9adbd45d6d32 ("io_uring: add and use struct io_rw for read/writes") ad8a48acc23c ("io_uring: make req->timeout be dynamically allocated") b29472ee7b53 ("io_uring: make IORING_OP_TIMEOUT_REMOVE deferrable") ba5290ccb6b5 ("io_uring: replace s->needs_lock with s->in_async") ba816ad61fdf ("io_uring: run dependent links inline if possible") c826bd7a743f ("io_uring: add set of tracing events")
e47293fdf989 ("io_uring: move all prep state for IORING_OP_{SEND,RECV}_MGS to prep handler") fbf23849b172 ("io_uring: make IORING_OP_CANCEL_ASYNC deferrable") fcb323cc53e2 ("io_uring: io_uring: add support for async work inheriting files")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks,
Sasha
author maintainer
#5 lore raw

PATCH 3/3 io_uring: allow AT_FDCWD for non-file openat/openat2/statx

In reply to Jens Axboe

Don't just check for dirfd == -1, we should allow AT_FDCWD as well for relative lookups.

---
 fs/io_uring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/io_uring.c +1 -1
index 2a7bb178986e..e6247b94c29d 100644
@@ -4543,7 +4543,7 @@ static int io_req_needs_file(struct io_kiocb *req, int fd)
 {
 	if (!io_op_defs[req->opcode].needs_file)
 		return 0;
-	if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
+	if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
 		return 0;
 	return 1;
 }
Signature · 1 line
2.25.0