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