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