nfs: support FMODE_NOWAIT on O_DIRECT reads
Subsystems (3)
v2 cover nfs: support FMODE_NOWAIT on O_DIRECT reads
I had noticed that io_uring always punts O_DIRECT NFS reads to a background thread since the file does not advertise FMODE_NOWAIT.
I am not very familiar with the NFS codebase, but looking around suggests a simple change to nfs_start_io_direct is all that is required to properly support this functionality. On the request issue side, it seems everything in NFS is actually run in the background (post this lock change), and the completion codepaths all look to have no similar locking semantics.
I have restricted this to read-only files initially, as the code paths are simpler.
I unfortunately do not have the means to test the performance improvement, since even without this change my local network is the bottleneck here.
However I do suspect that there are people that would want this fix ([1]). Applying a similar patch on that GitHub issue did give performance gains.
To convince myself this works at all I did trace io_uring events through with and without the patch.
Using a test app ([2]) to issue O_DIRECT io_uring reads calls io_uring_queue_async_work without this patch, while with it the call is skipped and the completion is queued into io_uring directly from nfs_direct_read_completion.
Patch 1 here adds an unused nfs_start_io_direct_nowait which patch 2 uses in order to safely advertise FMODE_NOWAIT.
v2: Suggestions from Sashiko:
* Handle file flags changing
* Do not use mapping_empty anymore as it was apparently racy
[1]: https://github.com/axboe/liburing/issues/1499
[2]: https://github.com/DylanZA/liburing/commit/264c06f1939dfd6b6bc4c967ada5960c4f4f2db3
Dylan Yudaken (2):
nfs: add nowait version of nfs_start_io_direct
nfs: expose FMODE_NOWAIT for read-only files
fs/nfs/direct.c | 12 ++++++++++-- fs/nfs/file.c | 16 +++++++++++++++- fs/nfs/internal.h | 1 + fs/nfs/io.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-)
base-commit: a2be31abc3fac6a20f662f6118815b9c40c371c9
Signature · 1 line
2.50.1
PATCH v2 1/2 nfs: add nowait version of nfs_start_io_direct
nfs_start_io_direct might block on existing operations to the same inode. In order to support NOWAIT O_DIRECT reads, add a non-blocking version of this nfs_start_io_direct that just returns -EAGAIN if locks could not be taken.
--- fs/nfs/internal.h | 1 + fs/nfs/io.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
fs/nfs/internal.h +1 -0
@@ -532,6 +532,7 @@ extern void nfs_end_io_read(struct inode *inode);extern __must_check int nfs_start_io_write (struct inode *inode);extern void nfs_end_io_write (struct inode *inode);extern __must_check int nfs_start_io_direct (struct inode *inode); +extern __must_check int nfs_start_io_direct_nowait (struct inode *inode);extern void nfs_end_io_direct (struct inode *inode);static inline bool nfs_file_io_is_buffered (struct nfs_inode *nfsi)
fs/nfs/io.c +41 -0
53 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v2 2/2 nfs: expose FMODE_NOWAIT for read-only files
In reply to Dylan YudakenNFS O_DIRECT reads already (mostly) handle async requests, with the exception of locking the inode for direct.
Handle async requests properly by using nfs_start_io_direct_nowait, and then expose FMODE_NOWAIT since it's now supported for direct reads.
--- fs/nfs/direct.c | 12 ++++++++++-- fs/nfs/file.c | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-)
fs/nfs/direct.c +10 -2
@@ -466,14 +466,22 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, goto out_release; } dreq->l_ctx = l_ctx; -if (!is_sync_kiocb (iocb)) +if (!is_sync_kiocb (iocb)) { dreq->iocb = iocb; + }else if (iocb->ki_flags &IOCB_NOWAIT ) { + result = -EAGAIN ; +nfs_direct_req_release (dreq); + goto out_release; + }if (user_backed_iter (iter)) dreq->flags =NFS_ODIRECT_SHOULD_DIRTY ;if (!swap) { - result =nfs_start_io_direct (inode); +if (iocb->ki_flags &IOCB_NOWAIT ) + result =nfs_start_io_direct_nowait (inode); +else + result =nfs_start_io_direct (inode);if (result) {/* release the reference that would usually be * consumed by nfs_direct_read_schedule_iovec()
fs/nfs/file.c +15 -1
@@ -72,8 +72,12 @@ nfs_file_open(struct inode *inode, struct file *filp)return res; res =nfs_open (inode, filp); -if (res ==0 ) +if (res ==0 ) { filp->f_mode |=FMODE_CAN_ODIRECT ; +/* flag NOWAIT on read-only files only */ +if (!(filp->f_mode &FMODE_WRITE )) + filp->f_mode |=FMODE_NOWAIT ; + }return res; }
@@ -166,6 +170,10 @@ nfs_file_read(struct kiocb *iocb, struct iov_iter *to)if (iocb->ki_flags &IOCB_DIRECT )return nfs_file_direct_read (iocb, to, false); +/* NOWAIT only supported on direct reads */ +if (iocb->ki_flags &IOCB_NOWAIT ) +return -EAGAIN ; +dprintk ("NFS: read(%pD2, %zu@%lu)\n" , iocb->ki_filp,iov_iter_count (to), (unsigned long ) iocb->ki_pos);
@@ -705,6 +713,12 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)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;
Signature · 1 line
2.50.1