From: Avi Kivity nfs_direct_write_complete() unconditionally hands the tail of every direct write to nfsiod, costing a workqueue wakeup per request. The detour is only needed when there is real work left: a COMMIT to send or writes to resend. Both of those need a sleepable context, but the terminal case does not - it only zaps the mapping and runs the completion, neither of which can block. Split the tail out of the workqueue callback as nfs_direct_write_finish() and take the short cut when dreq->flags is zero and the commit lists are empty. Since the RPC completion already runs in process context this finishes the request on the spot. The commit list check keeps us honest: dreq->flags can be left at zero on an error path that still marked requests for commit, and skipping nfs_direct_write_clear_reqs() there would leak them. For aio this removes workqueue involvement from direct writes entirely, as aio_complete_rw() is happy to post the event from any context. Assisted-by: Claude:claude-opus-5 Signed-off-by: Avi Kivity --- fs/nfs/direct.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- fs/nfs/pnfs.h | 14 ++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c index a3c6e8f4ea06a..9531bb1a00b8b 100644 --- a/fs/nfs/direct.c +++ b/fs/nfs/direct.c @@ -735,13 +735,17 @@ static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq) nfs_release_request(req); nfs_unlock_and_release_request(req); } } -static void nfs_direct_write_schedule_work(struct work_struct *work) +/* + * Tail of a direct write. Must run in a context that may sleep, since it can + * take the commit mutex and issue further RPC calls. It does not wait for + * those calls to complete. + */ +static void nfs_direct_write_finish(struct nfs_direct_req *dreq) { - struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); int flags = dreq->flags; dreq->flags = 0; switch (flags) { case NFS_ODIRECT_DO_COMMIT: @@ -755,13 +759,52 @@ static void nfs_direct_write_schedule_work(struct work_struct *work) nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); nfs_direct_complete(dreq); } } +static void nfs_direct_write_schedule_work(struct work_struct *work) +{ + struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); + + nfs_direct_write_finish(dreq); +} + +/* + * Is anything still sitting on this request's commit lists? Only called once + * the final reference has been dropped, so the counters cannot change under us + * and no locking is needed. + */ +static bool nfs_direct_has_commit_reqs(struct nfs_direct_req *dreq) +{ + struct nfs_commit_info cinfo; + + if (atomic_long_read(&dreq->mds_cinfo.ncommit) != 0) + return true; + nfs_init_cinfo_from_dreq(&cinfo, dreq); + return pnfs_ds_has_commit_reqs(&cinfo); +} + static void nfs_direct_write_complete(struct nfs_direct_req *dreq) { trace_nfs_direct_write_complete(dreq); + + /* + * dreq->flags is stable here: this is the final reference, so no other + * I/O against this request is outstanding. + * + * A zero value means there is no COMMIT to send and no write to + * resend. If the commit lists are empty as well then nothing is left + * that can block, so finish in the RPC completion context rather than + * paying for a round trip through nfsiod. This is the common case for + * a direct write. + */ + if (dreq->flags == 0 && !nfs_direct_has_commit_reqs(dreq)) { + nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); + nfs_direct_complete(dreq); + return; + } + queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ } static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) { diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h index 2774d4adf4c31..ce98fe5264acd 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -653,10 +653,18 @@ pnfs_recover_commit_reqs(struct list_head *head, struct nfs_commit_info *cinfo) if (fl_cinfo && fl_cinfo->nwritten != 0) fl_cinfo->ops->recover_commit_reqs(head, cinfo); } +static inline bool +pnfs_ds_has_commit_reqs(struct nfs_commit_info *cinfo) +{ + struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; + + return fl_cinfo && fl_cinfo->nwritten != 0; +} + /* Should the pNFS client commit and return the layout upon a setattr */ static inline bool pnfs_ld_layoutret_on_setattr(struct inode *inode) { if (!pnfs_enabled_sb(NFS_SERVER(inode))) @@ -953,10 +961,16 @@ pnfs_scan_commit_lists(struct inode *inode, struct nfs_commit_info *cinfo, static inline void pnfs_recover_commit_reqs(struct list_head *head, struct nfs_commit_info *cinfo) { } +static inline bool +pnfs_ds_has_commit_reqs(struct nfs_commit_info *cinfo) +{ + return false; +} + static inline int pnfs_layoutcommit_inode(struct inode *inode, bool sync) { return 0; } -- 2.56.1 From: Avi Kivity When a direct write does need the sleepable tail - an unstable reply requiring a COMMIT, or writes to resend - it still goes through nfsiod. For an asynchronous request the submitting task is a better place to run it: nfs_direct_write_finish() only submits further RPC calls, it never waits for them, so it is safe from task_work, and the submitter is about to return to userspace anyway. That removes the workqueue wakeup. Record the submitting task on the dreq and prefer task_work_add() over queue_work(), falling back to nfsiod if the task is on its way out. aio is deliberately left on nfsiod. task_work_add() must use TWA_SIGNAL to break the TASK_INTERRUPTIBLE sleep in read_events(), and io_getevents() turns a pending TIF_NOTIFY_SIGNAL into -EINTR, so aio would see a spurious error on every completion. io_uring drains task_work in its own wait loop and has no such problem. Kernel threads and io-wq workers are excluded as well, since they need not reach the exit-to-user work loop. Assisted-by: Claude:claude-opus-5 Signed-off-by: Avi Kivity --- fs/nfs/direct.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- fs/nfs/internal.h | 2 ++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c index 9531bb1a00b8b..736af5cf2f6eb 100644 --- a/fs/nfs/direct.c +++ b/fs/nfs/direct.c @@ -45,10 +45,11 @@ #include #include #include #include #include +#include #include #include #include #include @@ -228,10 +229,12 @@ static inline struct nfs_direct_req *nfs_direct_req_alloc(void) static void nfs_direct_req_free(struct kref *kref) { struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode); + if (dreq->task != NULL) + put_task_struct(dreq->task); if (dreq->l_ctx != NULL) nfs_put_lock_context(dreq->l_ctx); if (dreq->ctx != NULL) put_nfs_open_context(dreq->ctx); kmem_cache_free(nfs_direct_cachep, dreq); @@ -240,10 +243,38 @@ static void nfs_direct_req_free(struct kref *kref) static void nfs_direct_req_release(struct nfs_direct_req *dreq) { kref_put(&dreq->kref, nfs_direct_req_free); } +/* + * Remember the submitting task so that the tail of the write completion can be + * run from task_work instead of being handed to nfsiod. That keeps the + * COMMIT/resend machinery on the CPU that issued the I/O and saves a workqueue + * wakeup per request. + * + * task_work is only usable when the submitter will actually run it soon: + * + * - aio is excluded because task_work_add() has to raise TIF_NOTIFY_SIGNAL to + * break the io_getevents() sleep, which userspace would see as a spurious + * -EINTR. aio does not need it anyway: aio_complete_rw() can post the + * event from any context. + * - kernel threads and io-wq workers are excluded since they may never reach + * the exit-to-user work loop. + * + * Anything else - io_uring task context in particular - drains task_work + * before it waits, so the completion is picked up immediately. + */ +static void nfs_direct_req_set_task(struct nfs_direct_req *dreq, + struct kiocb *iocb) +{ + if (is_sync_kiocb(iocb) || (iocb->ki_flags & IOCB_AIO_RW)) + return; + if (current->flags & (PF_KTHREAD | PF_IO_WORKER | PF_EXITING)) + return; + dreq->task = get_task_struct(current); +} + ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq, loff_t offset) { loff_t start = offset - dreq->io_start; return dreq->max_count - start; } @@ -766,10 +797,18 @@ static void nfs_direct_write_schedule_work(struct work_struct *work) struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); nfs_direct_write_finish(dreq); } +static void nfs_direct_write_task_work(struct callback_head *cb) +{ + struct nfs_direct_req *dreq = container_of(cb, struct nfs_direct_req, + task_work); + + nfs_direct_write_finish(dreq); +} + /* * Is anything still sitting on this request's commit lists? Only called once * the final reference has been dropped, so the counters cannot change under us * and no locking is needed. */ @@ -801,10 +840,18 @@ static void nfs_direct_write_complete(struct nfs_direct_req *dreq) nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); nfs_direct_complete(dreq); return; } + /* Otherwise prefer the submitting task over nfsiod. */ + if (dreq->task) { + init_task_work(&dreq->task_work, nfs_direct_write_task_work); + if (!task_work_add(dreq->task, &dreq->task_work, TWA_SIGNAL)) + return; + /* The task is exiting; fall back to the workqueue. */ + } + queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ } static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) { @@ -1061,12 +1108,14 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, result = PTR_ERR(l_ctx); nfs_direct_req_release(dreq); goto out_release; } dreq->l_ctx = l_ctx; - if (!is_sync_kiocb(iocb)) + if (!is_sync_kiocb(iocb)) { dreq->iocb = iocb; + nfs_direct_req_set_task(dreq, iocb); + } pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode); if (swap) { requested = nfs_direct_write_schedule_iovec(dreq, iter, pos, FLUSH_STABLE); diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index 5af6a032f1454..a7142982d5cd6 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -1034,10 +1034,12 @@ struct nfs_direct_req { /* commit state */ struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ struct work_struct work; + struct callback_head task_work; /* completion via task_work */ + struct task_struct *task; /* submitter, if task_work usable */ int flags; /* for write */ #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ /* for read */ -- 2.56.1 From: Avi Kivity nfs_file_direct_write() ignored IOCB_NOWAIT and blocked unconditionally in nfs_start_io_direct(), so it could not be advertised as a non-blocking write path. Use nfs_start_io_direct_nowait() instead, mirroring what the read side already does. If the mapping still holds pages, refuse with -EAGAIN: the invalidation that follows the write cannot be done without blocking, and skipping it would leave a stale page cache behind for buffered readers. A synchronous kiocb with RWF_NOWAIT gets -EAGAIN as well, since there is no way to report the result later. This matches nfs_file_direct_read(). Assisted-by: Claude:claude-opus-5 Signed-off-by: Avi Kivity --- fs/nfs/direct.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c index 736af5cf2f6eb..e265faee5f824 100644 --- a/fs/nfs/direct.c +++ b/fs/nfs/direct.c @@ -1111,18 +1111,36 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, } dreq->l_ctx = l_ctx; if (!is_sync_kiocb(iocb)) { dreq->iocb = iocb; nfs_direct_req_set_task(dreq, iocb); + } else if (iocb->ki_flags & IOCB_NOWAIT) { + /* A synchronous kiocb has no way to report completion later */ + result = -EAGAIN; + nfs_direct_req_release(dreq); + goto out_release; } pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode); if (swap) { requested = nfs_direct_write_schedule_iovec(dreq, iter, pos, FLUSH_STABLE); } else { - result = nfs_start_io_direct(inode); + if (iocb->ki_flags & IOCB_NOWAIT) { + result = nfs_start_io_direct_nowait(inode); + /* + * The post-write invalidation below cannot be done + * without blocking, so refuse the request rather than + * skip it and leave a stale page cache behind. + */ + if (!result && mapping->nrpages) { + nfs_end_io_direct(inode); + result = -EAGAIN; + } + } else { + result = nfs_start_io_direct(inode); + } if (result) { /* release the reference that would usually be * consumed by nfs_direct_write_schedule_iovec() */ nfs_direct_req_release(dreq); -- 2.56.1 From: Avi Kivity FMODE_NOWAIT was only set on read-only files, and only by nfs_file_open() - nfs4_file_open() never set it at all, so on NFSv4 mounts even read-only files did not get it. io_uring gates REQ_F_SUPPORT_NOWAIT on this flag, so every read and write on an O_DIRECT NFS file was punted to an io-wq worker before the request ever reached NFS. Now that nfs_file_direct_write() honours IOCB_NOWAIT, set the flag regardless of the access mode from all three open paths: nfs_file_open(), nfs4_file_open(), and nfs_atomic_open(). The last one matters because on NFSv4 a file opened with O_CREAT goes through ->atomic_open() and never reaches nfs4_file_open(); without it every newly created file would still lack the flag. The buffered write path returns -EAGAIN instead of warning; move that check below the O_DIRECT dispatch so direct writes are not caught by it. pNFS is excluded. pnfs_update_layout() is reached from ->pg_init() in the submission loop and blocks - on NFS_LAYOUT_DRAIN, on a pending layoutreturn, and on the LAYOUTGET RPC itself - so claiming NOWAIT there would only move the stall into the io_uring submitter. Making that path non-blocking needs a nowait flag plumbed through pnfs_update_layout() and the layout drivers, which is left for later. Assisted-by: Claude:claude-opus-5 Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Avi Kivity --- fs/nfs/dir.c | 4 ++++ fs/nfs/file.c | 24 ++++++++++++++++-------- fs/nfs/nfs4file.c | 3 +++ fs/nfs/pnfs.h | 5 +++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index 49437037f948d..a3089cbdc1cf2 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -42,10 +42,11 @@ #include "delegation.h" #include "iostat.h" #include "internal.h" #include "fscache.h" +#include "pnfs.h" #include "nfstrace.h" /* #define NFS_DEBUG_VERBOSE 1 */ @@ -2209,10 +2210,13 @@ int nfs_atomic_open(struct inode *dir, struct dentry *dentry, break; } goto out; } file->f_mode |= FMODE_CAN_ODIRECT; + /* See the FMODE_NOWAIT comment in nfs_file_open() */ + if (!pnfs_enabled_sb(NFS_SERVER(dir))) + file->f_mode |= FMODE_NOWAIT; if (test_bit(NFS_CONTEXT_O_DIRECT, &ctx->flags)) { file->f_flags |= O_DIRECT; open_flags |= O_DIRECT; } diff --git a/fs/nfs/file.c b/fs/nfs/file.c index 38f830a6467c9..8e119b55577d9 100644 --- a/fs/nfs/file.c +++ b/fs/nfs/file.c @@ -71,12 +71,22 @@ nfs_file_open(struct inode *inode, struct file *filp) return res; res = nfs_open(inode, filp); if (res == 0) { filp->f_mode |= FMODE_CAN_ODIRECT; - /* flag NOWAIT on read-only files only */ - if (!(filp->f_mode & FMODE_WRITE)) + /* + * Both nfs_file_read() and nfs_file_write() honour IOCB_NOWAIT: + * the O_DIRECT paths make a genuine non-blocking attempt, and + * the buffered paths report -EAGAIN. Without FMODE_NOWAIT + * io_uring punts every request on a writable file to an io-wq + * worker before it ever reaches us. + * + * pNFS is excluded: issuing an I/O for which no layout segment + * is cached requires a synchronous LAYOUTGET, and there is no + * way to ask for that without blocking yet. + */ + if (!pnfs_enabled_sb(NFS_SERVER(inode))) filp->f_mode |= FMODE_NOWAIT; } return res; } @@ -743,23 +753,21 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) errseq_t since; int error; trace_nfs_file_write(iocb, from); - /* - * FMODE_NOWAIT is not set for writable files - */ - if (WARN_ON_ONCE(iocb->ki_flags & IOCB_NOWAIT)) - return -EAGAIN; - result = nfs_key_timeout_notify(file, inode); if (result) return result; if (iocb->ki_flags & IOCB_DIRECT) return nfs_file_direct_write(iocb, from, false); + /* NOWAIT is only supported on direct writes */ + if (iocb->ki_flags & IOCB_NOWAIT) + return -EAGAIN; + dprintk("NFS: write(%pD2, %zu@%Ld)\n", file, iov_iter_count(from), (long long) iocb->ki_pos); if (IS_SWAPFILE(inode)) goto out_swapfile; diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c index 9a434f5dda8d3..1cbb4160377c3 100644 --- a/fs/nfs/nfs4file.c +++ b/fs/nfs/nfs4file.c @@ -89,10 +89,13 @@ nfs4_file_open(struct inode *inode, struct file *filp) nfs_file_set_open_context(filp, ctx); nfs_fscache_open_file(inode, filp); err = 0; filp->f_mode |= FMODE_CAN_ODIRECT; + /* See the FMODE_NOWAIT comment in nfs_file_open() */ + if (!pnfs_enabled_sb(NFS_SERVER(inode))) + filp->f_mode |= FMODE_NOWAIT; if (test_bit(NFS_CONTEXT_O_DIRECT, &ctx->flags)) filp->f_flags |= O_DIRECT; out_put_ctx: put_nfs_open_context(ctx); diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h index ce98fe5264acd..13b4ee7c6febb 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -908,10 +908,15 @@ static inline void set_pnfs_layoutdriver(struct nfs_server *s, static inline void unset_pnfs_layoutdriver(struct nfs_server *s) { } +static inline int pnfs_enabled_sb(struct nfs_server *nfss) +{ + return 0; +} + static inline int pnfs_commit_list(struct inode *inode, struct list_head *mds_pages, int how, struct nfs_commit_info *cinfo) { return PNFS_NOT_ATTEMPTED; -- 2.56.1 From: Avi Kivity NFS direct writes take inode->i_rwsem shared, via nfs_start_io_direct(), so they already run in parallel against a single inode. Without FOP_DIO_PARALLEL_WRITE io_uring hashes any O_DIRECT write it has to punt onto a per-inode work queue, needlessly serialising them. Assisted-by: Claude:claude-opus-5 Signed-off-by: Avi Kivity --- fs/nfs/file.c | 2 +- fs/nfs/nfs4file.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/nfs/file.c b/fs/nfs/file.c index 8e119b55577d9..0259c3a07f72b 100644 --- a/fs/nfs/file.c +++ b/fs/nfs/file.c @@ -1015,8 +1015,8 @@ const struct file_operations nfs_file_operations = { .lock = nfs_lock, .flock = nfs_flock, .splice_read = nfs_file_splice_read, .splice_write = iter_file_splice_write, .check_flags = nfs_check_flags, - .fop_flags = FOP_DONTCACHE, + .fop_flags = FOP_DONTCACHE | FOP_DIO_PARALLEL_WRITE, }; EXPORT_SYMBOL_GPL(nfs_file_operations); diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c index 1cbb4160377c3..bdb8012f3d2e9 100644 --- a/fs/nfs/nfs4file.c +++ b/fs/nfs/nfs4file.c @@ -459,7 +459,7 @@ const struct file_operations nfs4_file_operations = { .fallocate = nfs42_fallocate, .remap_file_range = nfs42_remap_file_range, #else .llseek = nfs_file_llseek, #endif - .fop_flags = FOP_DONTCACHE, + .fop_flags = FOP_DONTCACHE | FOP_DIO_PARALLEL_WRITE, }; -- 2.56.1