NFSD: keep direct-mode I/O out of the page cache and elide COMMITs
Subsystems (3)
cover NFSD: keep direct-mode I/O out of the page cache and elide COMMITs
Hi,
This series builds on NFSD_IO_DIRECT. When a READ or WRITE in a direct mode cannot be direct I/O it now falls back to DONTCACHE rather than to cached buffered I/O, a direct-mode WRITE can report the stability it actually has so the client skips its COMMIT, and new tracepoints show which path each request took.
Patch 1 interlocks io_cache_read and io_cache_write so READ and WRITE are never left on opposite sides of the buffered/direct divide.
Patches 2-4 cover what cannot or should not be direct: the direct middle of a split WRITE is also marked IOCB_DONTCACHE, for when XFS falls back to buffered I/O (-ENOTBLK); a WRITE is split only when that buys a worthwhile direct middle (direct_misaligned_num_pages, default 2); and a READ smaller than its alignment no longer costs a full aligned device read.
Patch 5 is Chuck's "Enable return of an updated stable_how to NFS clients", reworked onto the @iocb_flags argument nfsd_write() now takes in nfsd-next. The Reviewed-by tags from its first posting are dropped because the argument changed.
Patch 6 adds io_cache_write modes 3 and 4, which issue direct I/O like NFSD_IO_DIRECT and raise the reply's stable_how to at least DATA_SYNC or FILE_SYNC. On a pNFS flexfiles share where every write is split across two data servers, the COMMITs alone cost NFSD_IO_DIRECT 31% more server CPU and 42% more client CPU for the same bytes.
Patch 7 persists a synchronous direct-mode WRITE once, after all of its segments, instead of up to three fsyncs per WRITE.
Patch 8 keeps the page that two misaligned WRITEs share in the page cache until both have written it, so the second one no longer has to read it back from disk: with 32 interleaved writers, 704 device reads for 42895 WRITEs where there were 45144 for 45664.
Patch 9 (Jonathan) adds the direct_misaligned_dontcache debugfs knob, default Y; set to N, the parts of a direct-mode WRITE that cannot be direct use cached buffered I/O instead of DONTCACHE.
Patch 10 adds tracepoints for how each direct-mode READ and WRITE was serviced, including why a WRITE was not direct.
Documentation/filesystems/nfs/nfsd-io-modes.rst is updated throughout. The series applies to cel/nfsd-next (ac04dab23b5f) and each patch builds cleanly with W=1.
All review appreciated, thanks.
Mike
Chuck Lever (1):
NFSD: Enable return of an updated stable_how to NFS clients
Jonathan Flynn (1):
NFSD: add direct_misaligned_dontcache debugfs knob
Mike Snitzer (8):
NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
NFSD: mark the direct middle of a split WRITE IOCB_DONTCACHE as well
NFSD: only split a direct-mode WRITE for a worthwhile direct middle
NFSD: do not use direct I/O for a READ smaller than its alignment
NFSD: let a direct-mode WRITE raise stable_how and elide the client's COMMIT
NFSD: persist a synchronous direct-mode WRITE once, after all of its segments
NFSD: keep boundary page of a split direct-mode WRITE until both writers complete
NFSD: add tracing for how direct-mode READ and WRITE are serviced
.../filesystems/nfs/nfsd-io-modes.rst | 164 ++++++++- fs/nfsd/debugfs.c | 94 +++++- fs/nfsd/nfs3proc.c | 16 +- fs/nfsd/nfs4proc.c | 15 +- fs/nfsd/nfsd.h | 4 + fs/nfsd/nfsproc.c | 3 +- fs/nfsd/trace.h | 90 +++++ fs/nfsd/vfs.c | 318 +++++++++++++++--- fs/nfsd/vfs.h | 26 +- fs/nfsd/xdr3.h | 2 +- 10 files changed, 665 insertions(+), 67 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/10 NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Now that NFSD supports NFSD_IO_DIRECT for both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to use NFSD_IO_DIRECT.
Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although io_cache_read and io_cache_write remain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:
- Setting io_cache_read to NFSD_IO_DIRECT elevates a BUFFERED or DONTCACHE io_cache_write to NFSD_IO_DIRECT. A WRITE mode that is already direct is left as it is.
- Setting io_cache_write to any direct mode elevates a BUFFERED or DONTCACHE io_cache_read to NFSD_IO_DIRECT.
- Setting io_cache_read to NFSD_IO_BUFFERED or NFSD_IO_DONTCACHE demotes a direct io_cache_write to that same mode.
- Setting io_cache_write to NFSD_IO_BUFFERED or NFSD_IO_DONTCACHE demotes a direct io_cache_read to that same mode.
- Enabling splice for READ, by writing 0 to disable-splice-read, forces io_cache_read to NFSD_IO_BUFFERED and demotes a direct io_cache_write along with it.
The demotion is factored into nfsd_io_cache_write_demote() so that its three callers stay in sync.
Both DONTCACHE and DIRECT READ must copy into the RPC reply buffer, so splice for READ is disabled whenever either interface leaves nfsd_io_cache_read above NFSD_IO_BUFFERED, regardless of which one was written.
Document the complete interlock inDocumentation/filesystems/nfs/nfsd-io-modes.rst.
--- .../filesystems/nfs/nfsd-io-modes.rst | 35 ++++++++++++ fs/nfsd/debugfs.c | 54 ++++++++++++++++--- 2 files changed, 83 insertions(+), 6 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +35 -0
@@ -41,6 +41,41 @@ corresponding IO operation's debugfs interface, e.g.:: If you experiment with NFSD's IO modes on a recent kernel and have interesting results, please report them to [email protected] +READ and WRITE IO mode interlock +================================ + +Although io_cache_read and io_cache_write are separate interfaces, NFSD +keeps them from being configured such that one of READ or WRITE uses +DIRECT IO while the other uses the page cache. Mixing DIRECT and +buffered IO to the same file causes needless page cache invalidation and +writeback (see the DIRECT IO discussion in the Linux open(2) manpage), +so writing one interface may adjust the other: + +- Setting io_cache_read to NFSD_IO_DIRECT (2) elevates io_cache_write + to NFSD_IO_DIRECT (2) if it was BUFFERED or DONTCACHE. A WRITE mode + that is already DIRECT (2, 3 or 4) is left unchanged. +- Setting io_cache_write to any DIRECT mode (2, 3 or 4) elevates + io_cache_read to NFSD_IO_DIRECT (2) if it was BUFFERED or DONTCACHE. +- Setting io_cache_read to NFSD_IO_BUFFERED (0) or NFSD_IO_DONTCACHE (1) + while io_cache_write is a DIRECT mode demotes io_cache_write to that + same value (0 or 1). A WRITE mode that is already BUFFERED or + DONTCACHE is left unchanged. +- Setting io_cache_write to NFSD_IO_BUFFERED (0) or NFSD_IO_DONTCACHE + (1) while io_cache_read is NFSD_IO_DIRECT demotes io_cache_read to + that same value (0 or 1). + +Setting either interface to a value other than NFSD_IO_BUFFERED also +disables NFSD's use of splice for READ, because both DONTCACHE and +DIRECT READ must copy into the RPC reply buffer. This is reflected in +/sys/kernel/debug/nfsd/disable-splice-read reading as 1. Writing 0 to +disable-splice-read re-enables splice, which requires buffered READ, so +it forces io_cache_read back to NFSD_IO_BUFFERED (0) and, if +io_cache_write was a DIRECT mode, demotes it to NFSD_IO_BUFFERED (0) as +well. + +Always read both interfaces back after writing either of them to +confirm the resulting configuration. + NFSD DONTCACHE ==============
fs/nfsd/debugfs.c +48 -6
94 lines not on this page · raw message
Signature · 1 line
2.52.0
Chuck Lever
maintainerRe: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:
Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
Later patches in the series assume DONTCACHE is the fallback for certain cases. Jeff has measured substantial performance deficits for that mode, and maybe removing DONTCACHE would be a better direction to take.
The patch that reports the actual stability of a WRITE was dropped because it broke something (although I don't remember what). As I recall, even a direct WRITE needs a subsequent COMMIT. Have you demonstrated that the client COMMIT is a latency problem or that the memory that is pinned on the client has a noticeable impact? We suspect that it might, but every time I've measured, avoiding COMMIT with NFSD has shown no impact on throughput, which is why NFSD doesn't already do this optimization.
So even if these patches apply mechanically and benefit your workloads, it would be helpful if we could step back and understand what you are trying to achieve and how it should coordinate/align with where Jeff and I want to see this mechanism go. Can we start with that conversation?
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Re: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 16 lines · Chuck Lever
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Quoted 6 lines
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
The basis for the interlock is that O_DIRECT mode is intended to avoid bloating memory with page cache and the excess CPU burn of managing the page cache. Using read caching in conjunction with O_DIRECT writes knee-caps the wins of O_DIRECT. But sure, you can do it.. and I suppose the freedom of choice (and enough rope to hang) is perfectly fine. I can drop the interlock and send v2 after more time for review of the other patches (but there were some real bugs fixed in the interlock patch so will require some care to adjust).
direct_misaligned_dontcache=N shows the excessive 630 GiB page cache bloat (across 4 servers) I've had to confront with a larger scale test that really sticks its finger in this particular wound (as documented at end of the "NFSD: keep boundary page of a split direct-mode WRITE until both writers complete" patch):
On a four-server pNFS flexfiles rig, three clients writing 47008-byte records for 240 s against 640 nfsd threads per server, compared with the same servers before this change: device reads fall from 0.014-0.015 per record to 0.003, page-cache growth over the write phase falls from about 11 GiB to 2.9 GiB, and write throughput rises by 2.4% to 4.9% across NFSD_IO_DIRECT and both stable_how floors, with read throughput unchanged. Records of that size always take the split path, since their direct middle is at least 38818 bytes, so the figures are the split path alone; the whole-WRITE fallback needs records below about 12 KB to come into play. Keeping every boundary page cached instead, which a later commit makes possible with a direct_misaligned_dontcache=N debugfs knob, grows the page cache by about 630 GiB over the same runs.
The "11 GiB" is what's left resident in page cache _without_ the "NFSD: keep boundary page of a split direct-mode WRITE until both writers complete". _WITH_ that patch (to make more informed use of DONTCACHE) it drops to the quoted 2.9 GiB while still benefitting from RMW avoidance buffered IO makes possible -- pretty fantastic result.
Quoted 2 lines
Later patches in the series assume DONTCACHE is the fallback for certain cases.
That's the existing default that current upstream code has. But with this patchset the fallback is buffered, with buffered DONTCACHE the default unless debugfs direct_misaligned_dontcache=N configured.
It would be quite bad to remove DONTCACHE support because it actually does offer pretty solid wins (especially for the workload I quoted above). If pure buffered IO used (direct_misaligned_dontcache=N) WRITE performance suffers: "only" 31,297 MiB/s for O_DIRECT + buffered, whereas with O_DIRECT + DONTCACHE, overall result were (more runs needed to get stddev):
4 - FILE_SYNC floor 35,567 MiB/s 2 - NFSD_IO_DIRECT 35,302 MiB/s 3 - DATA_SYNC floor 36,163 MiB/s
Quoted 2 lines
Jeff has measured substantial performance deficits for that mode, and maybe removing DONTCACHE would be a better direction to take.
But to be clear, I'm not referring to DONTCACHE only mode=1, I'm most interested in hybrid of O_DIRECT+DONTCACHE (more on that at the end below).
My series has been fully tested with Jeff's more recent DONTCACHE
commits in place:
88d6f128d06d mm: track DONTCACHE dirty pages per bdi_writeback f3122ce09a51 mm: kick writeback flusher for IOCB_DONTCACHE with targeted dirty tracking
Quoted 2 lines
The patch that reports the actual stability of a WRITE was dropped because it broke something (although I don't remember what).
Haven't seen any issues with it. Been carrying it ever since you posted it.
Quoted 7 lines
As I recall, even a direct WRITE needs a subsequent COMMIT. Have you demonstrated that the client COMMIT is a latency problem or that the memory that is pinned on the client has a noticeable impact?
We suspect that it might, but every time I've measured, avoiding COMMIT with NFSD has shown no impact on throughput, which is why NFSD doesn't already do this optimization.
Yes, in the header for "NFSD: let a direct-mode WRITE raise stable_how and elide the client's COMMIT":
What this removes is the COMMIT traffic, and it is worth most where a client's writes are carved into several WRITE RPCs, because each piece is then committed separately. Measured on a pNFS flexfiles share where every write straddles two data servers, so every write becomes two WRITE RPCs and, under NFSD_IO_DIRECT, two COMMITs: the two modes do identical durability work, one fsync per COMMIT against one fsync per WRITE on identical WRITE counts, and the COMMIT RPCs alone cost NFSD_IO_DIRECT 31% more server CPU and 42% more client CPU for the same bytes, about 20 us of server CPU per COMMIT plus a client cost that grows with the range committed. Where only one write in 22 is split the same effect is a couple of cores on each side and no resolvable throughput difference, and writes that fit a single RPC send no COMMIT in either mode.
Quoted 5 lines
So even if these patches apply mechanically and benefit your workloads, it would be helpful if we could step back and understand what you are trying to achieve and how it should coordinate/align with where Jeff and I want to see this mechanism go. Can we start with that conversation?
OK, please review what I've provided further and we can then have a more detailed discussion about anything you like.
Claude helped summarize what this series fixes:
What this advance is, precisely. It is not "NFSD uses DONTCACHE". It is NFSD's direct write path - io_cache_write modes 2, 3 and 4, where an aligned WRITE goes to the filesystem as O_DIRECT - with DONTCACHE used only for the buffered fragments a misaligned WRITE cannot issue directly. The page cache is bypassed for the bulk of the data and bounded for the remainder. That distinction runs through everything below, and it is exactly what separates this from io_cache_write=1, which is plain buffered DONTCACHE with no direct I/O at all and does not reach this code.
A misaligned WRITE in one of those direct modes is split into a buffered prefix, an O_DIRECT middle and a buffered suffix. The boundary pages are shared by exactly two WRITEs that may arrive in either order, from different clients, at once. Marking both DONTCACHE bounded the page cache but cost the second writer a read from disk every time - 0.81 device reads per record on the rig, 585 GiB read back during an 8.1 TiB write.
Thanks,
Mike
Chuck Lever
maintainerRe: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 24 lines · Mike Snitzer
Quoted 16 lines · Chuck Lever
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Quoted 6 lines
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
Quoted 4 lines
The basis for the interlock is that
O_DIRECTmode is intended to avoid bloating memory with page cache and the excess CPU burn of managing the page cache. Using read caching in conjunction withO_DIRECTwrites knee-caps the wins ofO_DIRECT.
Jeff and I have never seen that, and it's a counterintuitive result. Buffered READ with DIRECT WRITE seems to work very well and the server is easily capable of managing the page cache in this case, since reclaiming a clean page doesn't mean having to flush dirty data. Evicting clean pages is not slow.
So I'd like to see a quantification of the penalties in this mixed mode before adjudicating it as a hazard. That is, you might be right, but so far I've seen no direct evidence that having a substantial page cache presence is a general deficit for WRITEs. If there are certain cases where even caching READs is a problem, then by all means, set the READ IO mode to DIRECT too in those cases.
I don't see this as an argument for forcing the server's mode setting in every case.
Quoted 3 lines
But sure, you can do it.. and
I suppose the freedom of choice (and enough rope to hang) is perfectly fine.
"Enough rope to hang" is the usual approach for Linux tunables. I should also point out that both of these settings are still debugfs, not the final shape of the administrative UI. It doesn't seem sensible to me to restrict them at this point, but I'll keep this idea in mind as we continue to develop our thinking about what the eventual non- debug admin UI will look like.
Quoted 3 lines
I can drop the interlock and send v2 after more time for review of the other patches (but there were some real bugs fixed in the interlock patch so will require some care to adjust).
The usual policy for "real bugs" is that those need to be fixed in separate, backport-able patches before making behavioral changes. Stable wants the fixes, and does not want the behavior changes, so these need to be separable.
You can send the bug fixes any time without waiting for a v2.
Quoted 11 lines
Quoted 7 lines
As I recall, even a direct WRITE needs a subsequent COMMIT. Have you demonstrated that the client COMMIT is a latency problem or that the memory that is pinned on the client has a noticeable impact?
We suspect that it might, but every time I've measured, avoiding COMMIT with NFSD has shown no impact on throughput, which is why NFSD doesn't already do this optimization.
Yes, in the header for "NFSD: let a direct-mode WRITE raise
stable_howand elide the client's COMMIT":
Quoted 13 lines
What this removes is the COMMIT traffic, and it is worth most where a client's writes are carved into several WRITE RPCs, because each piece is then committed separately. Measured on a pNFS flexfiles share where every write straddles two data servers, so every write becomes two WRITE RPCs and, under
NFSD_IO_DIRECT, two COMMITs: the two modes do identical durability work, one fsync per COMMIT against one fsync per WRITE on identical WRITE counts, and the COMMIT RPCs alone costNFSD_IO_DIRECT31% more server CPU and 42% more client CPU for the same bytes, about 20 us of server CPU per COMMIT plus a client cost that grows with the range committed. Where only one write in 22 is split the same effect is a couple of cores on each side and no resolvable throughput difference, and writes that fit a single RPC send no COMMIT in either mode.
I asked about latency, above. This paragraph is about CPU utilization.
Based on this description, it seems to me the client has full visibility of the data to be pushed back and how it's sharded, it has information about the network RTT (that's where the real throughput impact is for COMMIT), and it has control over the selection of UNSTABLE vs. FILE_SYNC.
The problem here might be that when a large WRITE payload is sharded across multiple servers, the client still thinks it is sending them via UNSTABLE WRITES to one server, and plans for only one COMMIT after the server completes the WRITEs.
But for the pNFS scenario, the client sends one UNSTABLE WRITE followed by a COMMIT to each server. If the sharded WRITES are all single RPCs to distinct servers, then they should each be FILE_SYNC.
The client can be smarter about how it writes data back to multiple servers, can't it?
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Re: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 45 lines · Chuck Lever
Quoted 24 lines · Mike Snitzer
Quoted 16 lines · Chuck Lever
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Quoted 6 lines
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
Quoted 4 lines
The basis for the interlock is that
O_DIRECTmode is intended to avoid bloating memory with page cache and the excess CPU burn of managing the page cache. Using read caching in conjunction withO_DIRECTwrites knee-caps the wins ofO_DIRECT.Jeff and I have never seen that, and it's a counterintuitive result. Buffered READ with DIRECT WRITE seems to work very well and the server is easily capable of managing the page cache in this case, since reclaiming a clean page doesn't mean having to flush dirty data. Evicting clean pages is not slow.
So I'd like to see a quantification of the penalties in this mixed mode before adjudicating it as a hazard. That is, you might be right, but so far I've seen no direct evidence that having a substantial page cache presence is a general deficit for WRITEs. If there are certain cases where even caching READs is a problem, then by all means, set the READ IO mode to DIRECT too in those cases.
Quoted 2 lines
I don't see this as an argument for forcing the server's mode setting in every case.
Yes, I understand and I've dropped the interlock patch in v2 (just posted).
Quoted 10 lines
Quoted 3 lines
But sure, you can do it.. and
I suppose the freedom of choice (and enough rope to hang) is perfectly fine."Enough rope to hang" is the usual approach for Linux tunables. I should also point out that both of these settings are still debugfs, not the final shape of the administrative UI. It doesn't seem sensible to me to restrict them at this point, but I'll keep this idea in mind as we continue to develop our thinking about what the eventual non- debug admin UI will look like.
Ack.
Quoted 10 lines
Quoted 3 lines
I can drop the interlock and send v2 after more time for review of the other patches (but there were some real bugs fixed in the interlock patch so will require some care to adjust).
The usual policy for "real bugs" is that those need to be fixed in separate, backport-able patches before making behavioral changes. Stable wants the fixes, and does not want the behavior changes, so these need to be separable.
You can send the bug fixes any time without waiting for a v2.
The bug was something I found and fixed in code I had been carrying privately. So not applicable to upstream, sorry for the noise.
Quoted 25 lines
Quoted 11 lines
Quoted 7 lines
As I recall, even a direct WRITE needs a subsequent COMMIT. Have you demonstrated that the client COMMIT is a latency problem or that the memory that is pinned on the client has a noticeable impact?
We suspect that it might, but every time I've measured, avoiding COMMIT with NFSD has shown no impact on throughput, which is why NFSD doesn't already do this optimization.
Yes, in the header for "NFSD: let a direct-mode WRITE raise
stable_howand elide the client's COMMIT":Quoted 13 lines
What this removes is the COMMIT traffic, and it is worth most where a client's writes are carved into several WRITE RPCs, because each piece is then committed separately. Measured on a pNFS flexfiles share where every write straddles two data servers, so every write becomes two WRITE RPCs and, under
NFSD_IO_DIRECT, two COMMITs: the two modes do identical durability work, one fsync per COMMIT against one fsync per WRITE on identical WRITE counts, and the COMMIT RPCs alone costNFSD_IO_DIRECT31% more server CPU and 42% more client CPU for the same bytes, about 20 us of server CPU per COMMIT plus a client cost that grows with the range committed. Where only one write in 22 is split the same effect is a couple of cores on each side and no resolvable throughput difference, and writes that fit a single RPC send no COMMIT in either mode.
Quoted 1 line
I asked about latency, above. This paragraph is about CPU utilization.
Fair point, latency wasn't measured.
Quoted 10 lines
Based on this description, it seems to me the client has full visibility of the data to be pushed back and how it's sharded, it has information about the network RTT (that's where the real throughput impact is for COMMIT), and it has control over the selection of UNSTABLE vs.
FILE_SYNC.The problem here might be that when a large WRITE payload is sharded across multiple servers, the client still thinks it is sending them via UNSTABLE WRITES to one server, and plans for only one COMMIT after the server completes the WRITEs.
Quoted 6 lines
But for the pNFS scenario, the client sends one UNSTABLE WRITE followed by a COMMIT to each server. If the sharded WRITES are all single RPCs to distinct servers, then they should each be
FILE_SYNC.The client can be smarter about how it writes data back to multiple servers, can't it?
When the server is operating in direct mode it isn't something exposed to the client. That the client could be smarter and/or already has adequate controls to achieve the same FILE_SYNC result is besides the point. The point is, if the server has already done the work then it should, within reason, convey as much back to the client to elide COMMIT work that isn't needed.
Mike
Chuck Lever
maintainerRe: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 46 lines · Mike Snitzer
Quoted 45 lines · Chuck Lever
Quoted 24 lines · Mike Snitzer
Quoted 16 lines · Chuck Lever
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Quoted 6 lines
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
Quoted 4 lines
The basis for the interlock is that
O_DIRECTmode is intended to avoid bloating memory with page cache and the excess CPU burn of managing the page cache. Using read caching in conjunction withO_DIRECTwrites knee-caps the wins ofO_DIRECT.Jeff and I have never seen that, and it's a counterintuitive result. Buffered READ with DIRECT WRITE seems to work very well and the server is easily capable of managing the page cache in this case, since reclaiming a clean page doesn't mean having to flush dirty data. Evicting clean pages is not slow.
So I'd like to see a quantification of the penalties in this mixed mode before adjudicating it as a hazard. That is, you might be right, but so far I've seen no direct evidence that having a substantial page cache presence is a general deficit for WRITEs. If there are certain cases where even caching READs is a problem, then by all means, set the READ IO mode to DIRECT too in those cases.
Quoted 4 lines
Quoted 2 lines
I don't see this as an argument for forcing the server's mode setting in every case.
Yes, I understand and I've dropped the interlock patch in v2 (just posted).
Please give reviewers a chance to digest and review, as requested in Documentation/filesystems/nfs/nfsd-maintainer-entry-profile.rst :
"As always, please avoid reposting series revisions more than once
every 24 hours."
Trust me, it saves a lot of confusion.
Quoted 17 lines
Quoted 10 lines
Based on this description, it seems to me the client has full visibility of the data to be pushed back and how it's sharded, it has information about the network RTT (that's where the real throughput impact is for COMMIT), and it has control over the selection of UNSTABLE vs.
FILE_SYNC.The problem here might be that when a large WRITE payload is sharded across multiple servers, the client still thinks it is sending them via UNSTABLE WRITES to one server, and plans for only one COMMIT after the server completes the WRITEs.
Quoted 6 lines
But for the pNFS scenario, the client sends one UNSTABLE WRITE followed by a COMMIT to each server. If the sharded WRITES are all single RPCs to distinct servers, then they should each be
FILE_SYNC.The client can be smarter about how it writes data back to multiple servers, can't it?
Quoted 6 lines
When the server is operating in direct mode it isn't something exposed to the client. That the client could be smarter and/or already has adequate controls to achieve the same
FILE_SYNCresult is besides the point. The point is, if the server has already done the work then it should, within reason, convey as much back to the client to elide COMMIT work that isn't needed.
Your point assumes the other patches in the series are applied to make DIRECT UNSTABLE WRITEs completely persistent.
The current IOCB flags do not include IOCB_DSYNC on a DIRECT UNSTABLE WRITE for a very good reason: that makes them slower and more expensive. The current server logic is working exactly as we designed it last year, and I'm not enthusiastic about changing that. I expect at least one other reviewer will have a similar reaction, once he sobers up from ALPSS.
If the client wants to avoid the COMMIT, the standing rule is to send a FILE_SYNC WRITE. That benefits all WRITE I/O modes on the server.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Re: [PATCH 01/10] NFSD: interlock the use of NFSD_IO_DIRECT for NFS READ and WRITE
Quoted 90 lines · Chuck Lever
Quoted 46 lines · Mike Snitzer
Quoted 45 lines · Chuck Lever
Quoted 24 lines · Mike Snitzer
Quoted 16 lines · Chuck Lever
Quoted 9 lines · Mike Snitzer
Now that NFSD supports
NFSD_IO_DIRECTfor both READ and WRITE it is much safer to avoid needless buffered vs direct contention if/when only one of them has been configured to useNFSD_IO_DIRECT.Mixing direct and buffered I/O to the same file causes needless page cache invalidation and writeback, so although
io_cache_readandio_cache_writeremain separate interfaces, writing either one adjusts the other so that READ and WRITE are never left on opposite sides of the buffered/direct divide:Jeff and I have been discussing making DIRECT the default for WRITEs and BUFFERED the default for READs. This patch takes us in the opposite direction.
Quoted 6 lines
Nothing here convinces me that mixing the modes is a bad thing to do. "Needless page cache invalidation" needs some demonstration, and it needs to show why the right thing to do is make it impossible to mix modes rather than explore the issue as one or more bugs that can be fixed. Or... why not let admins explore this for themselves? Where is the hazard and why does it need to be forbidden by the admin UI?
Quoted 4 lines
The basis for the interlock is that
O_DIRECTmode is intended to avoid bloating memory with page cache and the excess CPU burn of managing the page cache. Using read caching in conjunction withO_DIRECTwrites knee-caps the wins ofO_DIRECT.Jeff and I have never seen that, and it's a counterintuitive result. Buffered READ with DIRECT WRITE seems to work very well and the server is easily capable of managing the page cache in this case, since reclaiming a clean page doesn't mean having to flush dirty data. Evicting clean pages is not slow.
So I'd like to see a quantification of the penalties in this mixed mode before adjudicating it as a hazard. That is, you might be right, but so far I've seen no direct evidence that having a substantial page cache presence is a general deficit for WRITEs. If there are certain cases where even caching READs is a problem, then by all means, set the READ IO mode to DIRECT too in those cases.
Quoted 4 lines
Quoted 2 lines
I don't see this as an argument for forcing the server's mode setting in every case.
Yes, I understand and I've dropped the interlock patch in v2 (just posted).
Please give reviewers a chance to digest and review, as requested in
Documentation/filesystems/nfs/nfsd-maintainer-entry-profile.rst:"As always, please avoid reposting series revisions more than once
every 24 hours."Trust me, it saves a lot of confusion.
Quoted 17 lines
Quoted 10 lines
Based on this description, it seems to me the client has full visibility of the data to be pushed back and how it's sharded, it has information about the network RTT (that's where the real throughput impact is for COMMIT), and it has control over the selection of UNSTABLE vs.
FILE_SYNC.The problem here might be that when a large WRITE payload is sharded across multiple servers, the client still thinks it is sending them via UNSTABLE WRITES to one server, and plans for only one COMMIT after the server completes the WRITEs.
Quoted 6 lines
But for the pNFS scenario, the client sends one UNSTABLE WRITE followed by a COMMIT to each server. If the sharded WRITES are all single RPCs to distinct servers, then they should each be
FILE_SYNC.The client can be smarter about how it writes data back to multiple servers, can't it?
Quoted 6 lines
When the server is operating in direct mode it isn't something exposed to the client. That the client could be smarter and/or already has adequate controls to achieve the same
FILE_SYNCresult is besides the point. The point is, if the server has already done the work then it should, within reason, convey as much back to the client to elide COMMIT work that isn't needed.Your point assumes the other patches in the series are applied to make DIRECT UNSTABLE WRITEs completely persistent.
Quoted 6 lines
The current IOCB flags do not include
IOCB_DSYNCon a DIRECT UNSTABLE WRITE for a very good reason: that makes them slower and more expensive. The current server logic is working exactly as we designed it last year, and I'm not enthusiastic about changing that. I expect at least one other reviewer will have a similar reaction, once he sobers up from ALPSS.
You haven't reviewed the changes, yet you are passing judgement.
Please review. I think you'll find I have only made things better.
And as for Mr ALPSS, the "NFSD: do not use direct I/O for a READ smaller than its alignment" and "NFSD: only split a direct-mode WRITE for a worthwhile direct middle" patches were motivated by his feedback last year! (I have been carrying them since then, albeit in a less polished form than I have now made available in this series).
Quoted 2 lines
If the client wants to avoid the COMMIT, the standing rule is to send a
FILE_SYNCWRITE. That benefits all WRITE I/O modes on the server.
I haven't precluded DIRECT UNSTABLE WRITE from operating as it does now _at all_.
I've merely exposed additional optional controls that require opt-in. And provided a natural evolution to what we have that has proven beneficial.
PATCH 2/10 NFSD: mark the direct middle of a split WRITE IOCB_DONTCACHE as well
In reply to Mike Snitzernfsd_write_dio_iters_init() issues the DIO-aligned middle of a misaligned WRITE with IOCB_DIRECT. A file system is free to service that request with buffered I/O instead: xfs_file_write_iter() retries via xfs_file_buffered_write() with the same kiocb whenever xfs_file_dio_write() returns -ENOTBLK, which iomap_dio_rw() produces when the invalidation it runs before issuing the direct I/O cannot drop page cache overlapping the range.
That happens routinely in a streaming misaligned WRITE workload with more than one request in flight. Request N's direct middle ends inside the same page that request N+1's buffered prefix starts in. If N+1's prefix dirties that page between N's filemap_write_and_wait_range() and N's invalidate_inode_pages2_range(), the invalidation returns -EBUSY, iomap returns -ENOTBLK, and XFS re-issues N's entire middle, tens of kilobytes, as a normal cached buffered write. Nothing ever drops those pages. On a device with dma_alignment=3 (XFS then reports dio_mem_align=4, which XDR-aligned RPC payloads always satisfy) this is the only remaining path by which a stream of small misaligned WRITEs in DIRECT mode fills the page cache.
Set IOCB_DONTCACHE on the middle segment alongside IOCB_DIRECT when the file system supports FOP_DONTCACHE. The direct path ignores the flag, and generic_write_sync() only issues a harmless flusher kick; but if the file system falls back to buffered I/O the write is now DONTCACHE rather than cached. The nfsd_write_direct trace event is unchanged since it keys on IOCB_DIRECT; the fallback itself remains observable via the iomap_dio_invalidate_fail event.
--- Documentation/filesystems/nfs/nfsd-io-modes.rst | 10 ++++++++++ fs/nfsd/vfs.c | 9 +++++++++ 2 files changed, 19 insertions(+)
Documentation/filesystems/nfs/nfsd-io-modes.rst +10 -0
@@ -165,6 +165,16 @@ Misaligned WRITE: segments because using normal buffered IO offers significant RMW performance benefit when handling streaming misaligned WRITEs. + The O_DIRECT middle segment also carries the DONTCACHE flag. It has + no effect while the IO really is O_DIRECT, but a filesystem may + decide on its own to service the segment with buffered IO instead + (XFS does so when it cannot invalidate page cache that overlaps the + segment, which can happen when another WRITE's buffered start or + end segment dirties the shared boundary page at the same time). + The flag makes that fallback DONTCACHE buffered IO rather than + normal buffered IO. Such fallbacks are visible through the + iomap_dio_invalidate_fail trace event; see Tracing below. + Tracing: The nfsd_read_direct trace event shows how NFSD expands any misaligned READ to the next DIO-aligned block (on either end of the
fs/nfsd/vfs.c +9 -0
@@ -1341,6 +1341,15 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,if (iov_iter_bvec_offset (&segments[nsegs].iter) & (mem_align -1 )) goto no_dio; segments[nsegs].flags |=IOCB_DIRECT ; +/* +* Also mark the direct middle DONTCACHE: the file system may fall +* back to buffered I/O on its own (e.g. XFS on -ENOTBLK when it +* cannot invalidate page cache that a concurrent buffered prefix or +* suffix of an adjacent WRITE just dirtied), and it reuses this kiocb +* to do so. On the direct path itself the flag is inert. +*/ +if (nf->nf_file->f_op->fop_flags &FOP_DONTCACHE ) + segments[nsegs].flags |=IOCB_DONTCACHE ; nsegs++;if (suffix)
Signature · 1 line
2.52.0
PATCH 3/10 NFSD: only split a direct-mode WRITE for a worthwhile direct middle
In reply to Mike Snitzernfsd_write_dio_iters_init() decides how a WRITE issued in a direct mode is carried out: split into a buffered prefix, a direct middle and a buffered suffix, or issued whole as a single buffered segment.
Only split when the split buys direct I/O for a worthwhile middle. Add direct_misaligned_num_pages (debugfs knob, default 2) and decline to split when the aligned middle is smaller than that many pages and the WRITE has a prefix or a suffix. A WRITE whose payload memory is misaligned for the block device already declines: no segment of it can be direct I/O, so a split would only turn one buffered write into three.
Decide every segment's flags in nfsd_write_dio_iters_init() rather than in the write loop. The buffered prefix and suffix of a split WRITE, and the whole WRITE when it is not split, are issued IOCB_DONTCACHE when the file system supports FOP_DONTCACHE and as plain buffered I/O
otherwise: the operator chose a direct mode to keep NFSD out of the page cache, so a WRITE that cannot be direct gets the next best thing.
Document it in the "Misaligned WRITE" section of nfsd-io-modes.rst.
--- .../filesystems/nfs/nfsd-io-modes.rst | 17 ++++- fs/nfsd/debugfs.c | 14 ++++ fs/nfsd/nfsd.h | 1 + fs/nfsd/vfs.c | 73 +++++++++++++------ 4 files changed, 79 insertions(+), 26 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +14 -3
@@ -161,9 +161,9 @@ Misaligned WRITE: middle and end as needed. The large middle segment is DIO-aligned and the start and/or end are misaligned. Buffered IO is used for the misaligned segments and O_DIRECT is used for the middle DIO-aligned - segment. DONTCACHE buffered IO is _not_ used for the misaligned - segments because using normal buffered IO offers significant RMW - performance benefit when handling streaming misaligned WRITEs. + segment. If the underlying filesystem supports FOP_DONTCACHE, the + misaligned segments use DONTCACHE buffered IO so that their pages + are dropped from the page cache once written back. The O_DIRECT middle segment also carries the DONTCACHE flag. It has no effect while the IO really is O_DIRECT, but a filesystem may
@@ -175,6 +175,17 @@ Misaligned WRITE: normal buffered IO. Such fallbacks are visible through the iomap_dio_invalidate_fail trace event; see Tracing below. + Whenever no part of a WRITE can use O_DIRECT, the whole WRITE is + issued as a single DONTCACHE buffered IO (normal buffered IO if the + filesystem lacks FOP_DONTCACHE). This covers: a filesystem that + advertises no DIO alignment requirements at all; a WRITE smaller + than the larger of the offset and memory alignments; a WRITE whose + DIO-aligned middle segment is smaller than + /sys/kernel/debug/nfsd/direct_misaligned_num_pages pages (default 2) + while also having a misaligned start or end; and a WRITE whose + payload memory is not aligned to the block device's dma_alignment, + which rules out O_DIRECT for the middle segment as well. + Tracing: The nfsd_read_direct trace event shows how NFSD expands any misaligned READ to the next DIO-aligned block (on either end of the
fs/nfsd/debugfs.c +14 -0
@@ -170,6 +170,17 @@ void nfsd_debugfs_exit(void) nfsd_top_dir =NULL ; } +/* +* /sys/kernel/debug/nfsd/direct_misaligned_num_pages +* +* The smallest DIO-aligned middle segment, in pages, that is worth +* splitting a misaligned direct-mode WRITE into three segments for. A +* WRITE whose middle is smaller than this, and which has a misaligned +* start or end, is issued as a single buffered segment instead. +* +* Default 2. Not yet tuned by benchmarking. +*/ +void nfsd_debugfs_init (void ) { nfsd_top_dir =debugfs_create_dir ("nfsd" ,NULL );
@@ -182,6 +193,9 @@ void nfsd_debugfs_init(void)debugfs_create_file ("io_cache_write" ,0644 , nfsd_top_dir,NULL , &nfsd_io_cache_write_fops); + +debugfs_create_u32 ("direct_misaligned_num_pages" ,0644 , nfsd_top_dir, + &nfsd_direct_misaligned_num_pages);#ifdef CONFIG_NFSD_V4 debugfs_create_bool ("delegated_timestamps" ,0644 , nfsd_top_dir, &nfsd_delegts_enabled);
fs/nfsd/nfsd.h +1 -0
@@ -142,6 +142,7 @@ enum {extern u64 nfsd_io_cache_read__read_mostly ;extern u64 nfsd_io_cache_write__read_mostly ; +extern u32 nfsd_direct_misaligned_num_pages__read_mostly ;extern int nfsd_max_blksize;
fs/nfsd/vfs.c +50 -23
129 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 4/10 NFSD: do not use direct I/O for a READ smaller than its alignment
In reply to Mike Snitzernfsd_direct_read() expands a misaligned READ out to DIO-aligned
boundaries: it reads from round_down(offset, dio_read_offset_align) to round_up(offset + count, dio_read_offset_align) and returns only the requested bytes from within that window. When the READ is smaller than the alignment, that window is always at least one full alignment unit, and two when the READ straddles a boundary, so a few hundred bytes of payload can cost a 4K or 64K device read.
Decline direct I/O for those. A READ smaller than dio_read_offset_align now falls through to the DONTCACHE path, which issues DONTCACHE buffered I/O when the file system supports FOP_DONTCACHE and normal buffered I/O otherwise. This mirrors the WRITE side, which already declines direct I/O for a WRITE smaller than the larger of its offset and memory alignments.
Only dio_read_offset_align is consulted, because the READ path fills page-aligned pages from rq_bvec and so has no memory alignment to satisfy. The threshold only bites when the file system advertises a large alignment; where it reports 512 almost no READ is excluded.
Document it in the "Misaligned READ" section of nfsd-io-modes.rst.
--- Documentation/filesystems/nfs/nfsd-io-modes.rst | 7 +++++++ fs/nfsd/vfs.c | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +7 -0
@@ -156,6 +156,13 @@ Misaligned READ: verified to have proper offset/len (logical_block_size) and dma_alignment checking. + A READ smaller than dio_read_offset_align is not issued as O_DIRECT + at all. Expanding it would read a whole alignment unit, or two when + the READ straddles a boundary, to return those few bytes. Such a + READ is issued as DONTCACHE buffered IO instead (normal buffered IO + if the filesystem lacks FOP_DONTCACHE), mirroring the WRITE that is + smaller than its own alignment. + Misaligned WRITE: If NFSD_IO_DIRECT is used, split any misaligned WRITE into a start, middle and end as needed. The large middle segment is DIO-aligned
fs/nfsd/vfs.c +3 -3
@@ -1186,7 +1186,7 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, unsigned int base, u32 *eof) { struct file *file = nf->nf_file; -unsigned long v, total; +unsigned long v, total = *count;struct iov_iter iter;struct kiocb kiocb;ssize_t host_err;
@@ -1199,7 +1199,8 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,break ;case NFSD_IO_DIRECT :/* When dio_read_offset_align is zero, dio is not supported */ -if (nf->nf_dio_read_offset_align && !rqstp->rq_res.page_len) +if (nf->nf_dio_read_offset_align && !rqstp->rq_res.page_len && + total >= nf->nf_dio_read_offset_align)return nfsd_direct_read (rqstp, fhp, nf, offset, count, eof); fallthrough;
@@ -1212,7 +1213,6 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, kiocb.ki_pos = offset; v =0 ; - total = *count;while (total && v < rqstp->rq_maxpages && rqstp->rq_next_page < rqstp->rq_page_end) { len =min_t (size_t, total,PAGE_SIZE - base);
Signature · 1 line
2.52.0
PATCH 5/10 NFSD: Enable return of an updated stable_how to NFS clients
In reply to Mike SnitzerFrom: Chuck Lever <[email protected]>
NFSv3 and newer protocols enable clients to perform a two-phase WRITE. A client requests an UNSTABLE WRITE, which sends dirty data to the NFS server, but does not persist it. The server replies that it performed the UNSTABLE WRITE, and the client is then obligated to follow up with a COMMIT request before it can remove the dirty data from its own page cache. The COMMIT reply is the client's guarantee that the written data has been persisted on the server.
The purpose of this protocol design is to enable clients to send a large amount of data via multiple WRITE requests to a server, and then wait for persistence just once. The server is able to start persisting the data as soon as it gets it, to shorten the length of time the client has to wait for the final COMMIT to complete.
It's also possible for the server to respond to an UNSTABLE WRITE request in a way that indicates that the data was persisted anyway. In that case, the client can skip the COMMIT and remove the dirty data from its memory immediately. NetApp filers, for example, do this because they have a battery-backed cache and can guarantee that written data is persisted quickly and immediately.
NFSD has never implemented this kind of promotion. UNSTABLE WRITE requests are unconditionally treated as UNSTABLE. However, in a subsequent patch, nfsd_vfs_write() will be able to promote an UNSTABLE WRITE to be a FILE_SYNC WRITE. This will be because NFSD will handle some WRITE requests locally with O_DIRECT, which persists written data immediately. The FILE_SYNC WRITE response indicates to the client that no follow-up COMMIT is necessary.
This patch prepares for that change by making the @iocb_flags argument of nfsd_write() and nfsd_vfs_write() bi-directional. A caller passes in the IOCB_* flags that express the stability its client asked for; on return the argument holds the flags that were actually satisfied. Each protocol version maps that result back to its own on-the-wire value when it encodes the WRITE reply, using the new nfsd3_stable_how() and nfsd4_stable_how() helpers, so that NFS stable_how values stay out of NFSD's generic VFS API. No behavior change is expected.
[snitzer: reworked onto the @iocb_flags argument introduced by
commit 6dcddbb70b08 ("NFSD: Replace nfsd_write()'s "stable" argument with "iocb_flags""), which was applied after this patch was first posted.
The original passed a "u32 *stable_how" instead. Carrying the value as IOCB_* flags keeps the NFSv3 XDR value out of the VFS API, and lets a
later patch raise the achieved stability with a plain bitwise OR rather than an ordering comparison. The Reviewed-by tags from the original
posting are dropped because the argument's type and direction changed.]
--- fs/nfsd/nfs3proc.c | 16 +++++++++++++--- fs/nfsd/nfs4proc.c | 15 +++++++++++++-- fs/nfsd/nfsproc.c | 3 ++- fs/nfsd/vfs.c | 18 +++++++++++------- fs/nfsd/vfs.h | 4 ++-- fs/nfsd/xdr3.h | 2 +- 6 files changed, 42 insertions(+), 16 deletions(-)
fs/nfsd/nfs3proc.c +13 -3
@@ -102,6 +102,15 @@ static int nfsd3_iocb_flags(enum nfs3_stable_how how) } } +static enum nfs3_stable_how nfsd3_stable_how (int iocb_flags) +{ +if (iocb_flags &IOCB_SYNC ) +return NFS_FILE_SYNC ; +if (iocb_flags &IOCB_DSYNC ) +return NFS_DATA_SYNC ; +return NFS_UNSTABLE ; +} +static __be32 nfsd3_map_status (__be32 status) {switch (status) {
@@ -299,6 +308,7 @@ nfsd3_proc_write(struct svc_rqst *rqstp)struct nfsd3_writeargs *argp = rqstp->rq_argp;struct nfsd3_writeres *resp = rqstp->rq_resp;unsigned long cnt = argp->len; +int iocb_flags; dprintk("nfsd: WRITE(3) %s %d bytes at %Lu%s\n", SVCFH_fmt(&argp->fh),
@@ -312,11 +322,11 @@ nfsd3_proc_write(struct svc_rqst *rqstp)return rpc_success;fh_copy (&resp->fh, &argp->fh); - resp->committed = argp->stable; + iocb_flags =nfsd3_iocb_flags (argp->stable); resp->status =nfsd_write (rqstp, &resp->fh, argp->offset, - &argp->payload, &cnt, -nfsd3_iocb_flags (resp->committed), + &argp->payload, &cnt, &iocb_flags, resp->verf); + resp->committed =nfsd3_stable_how (iocb_flags); resp->count = cnt; resp->status =nfsd3_map_status (resp->status);return rpc_success;
fs/nfsd/nfs4proc.c +13 -2
@@ -109,6 +109,15 @@ static const struct nfsd_access_maps nfsd4_access_maps = { .other = nfsd4_otheraccess, }; +static enum stable_how4 nfsd4_stable_how (int iocb_flags) +{ +if (iocb_flags &IOCB_SYNC ) +return FILE_SYNC4 ; +if (iocb_flags &IOCB_DSYNC ) +return DATA_SYNC4 ; +return UNSTABLE4 ; +} +static int nfsd4_iocb_flags (enum stable_how4 how) {switch (how) {
@@ -1432,6 +1441,7 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,struct nfsd_file *nf =NULL ;__be32 status = nfs_ok;unsigned long cnt; +int iocb_flags;if (write->wr_offset > (u64 )OFFSET_MAX || write->wr_offset + write->wr_buflen > (u64 )OFFSET_MAX )
@@ -1450,11 +1460,12 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,nfs4_put_stid (stid); } - write->wr_how_written = write->wr_stable_how; + iocb_flags =nfsd4_iocb_flags (write->wr_stable_how); status =nfsd_vfs_write (rqstp, &cstate->current_fh, nf, write->wr_offset, &write->wr_payload, - &cnt,nfsd4_iocb_flags (write->wr_how_written), + &cnt, &iocb_flags, (__be32 *)write->wr_verifier.data); + write->wr_how_written =nfsd4_stable_how (iocb_flags);nfsd_file_put (nf); write->wr_bytes_written = cnt;
fs/nfsd/nfsproc.c +2 -1
@@ -274,6 +274,7 @@ nfsd_proc_write(struct svc_rqst *rqstp)struct nfsd_writeargs *argp = rqstp->rq_argp;struct nfsd_attrstat *resp = rqstp->rq_resp;unsigned long cnt = argp->len; +int iocb_flags =IOCB_DSYNC ; dprintk("nfsd: WRITE %s %u bytes at %d\n", SVCFH_fmt(&argp->fh),
@@ -281,7 +282,7 @@ nfsd_proc_write(struct svc_rqst *rqstp)fh_copy (&resp->fh, &argp->fh); resp->status =nfsd_write (rqstp, &resp->fh, argp->offset, - &argp->payload, &cnt,IOCB_DSYNC ,NULL ); + &argp->payload, &cnt, &iocb_flags,NULL );if (resp->status == nfs_ok) resp->status =fh_getattr (&resp->fh, &resp->stat);else if (resp->status == nfserr_jukebox)
fs/nfsd/vfs.c +11 -7
@@ -1444,7 +1444,9 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,* @offset: Byte offset of start * @payload: xdr_buf containing the write payload * @cnt: IN: number of bytes to write, OUT: number of bytes actually written -* @iocb_flags: VFS IOCB_* flags expressing the requested write stability +* @iocb_flags: IN: VFS IOCB_* flags expressing the requested write +* stability; OUT: the flags actually satisfied, which may be +* higher than requested * @verf: NFS WRITE verifier * * Upon return, caller must invoke fh_put on @fhp.
@@ -1456,7 +1458,7 @@ __be32 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf, loff_t offset, const struct xdr_buf *payload, unsigned long *cnt, - int iocb_flags, __be32 *verf) + int *iocb_flags, __be32 *verf) { struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);struct file *file = nf->nf_file;
@@ -1493,11 +1495,11 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, exp = fhp->fh_export;if (!EX_ISSYNC (exp)) - iocb_flags =0 ; + *iocb_flags =0 ;init_sync_kiocb (&kiocb, file); kiocb.ki_pos = offset;if (likely (!fhp->fh_use_wgather)) - kiocb.ki_flags |= iocb_flags; + kiocb.ki_flags |= *iocb_flags; nvecs =xdr_buf_to_bvec (rqstp->rq_bvec, rqstp->rq_maxpages, payload);if (nvecs <0 ) {
@@ -1538,7 +1540,7 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, goto out_nfserr; } -if (iocb_flags && fhp->fh_use_wgather) { +if (*iocb_flags && fhp->fh_use_wgather) { host_err =wait_for_concurrent_writes (file);if (host_err <0 )nfsd_maybe_reset_write_verifier (nn, rqstp, host_err);
@@ -1629,7 +1631,9 @@ __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,* @offset: Byte offset of start * @payload: xdr_buf containing the write payload * @cnt: IN: number of bytes to write, OUT: number of bytes actually written -* @iocb_flags: VFS IOCB_* flags expressing the requested write stability +* @iocb_flags: IN: VFS IOCB_* flags expressing the requested write +* stability; OUT: the flags actually satisfied, which may be +* higher than requested * @verf: NFS WRITE verifier * * Upon return, caller must invoke fh_put on @fhp.
@@ -1640,7 +1644,7 @@ __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,__be32 nfsd_write (struct svc_rqst *rqstp,struct svc_fh *fhp,loff_t offset,const struct xdr_buf *payload,unsigned long *cnt, -int iocb_flags,__be32 *verf) +int *iocb_flags,__be32 *verf) {struct nfsd_file *nf;__be32 err;
fs/nfsd/vfs.h +2 -2
@@ -154,12 +154,12 @@ __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *eof);__be32 nfsd_write (struct svc_rqst *rqstp,struct svc_fh *fhp,loff_t offset,const struct xdr_buf *payload, -unsigned long *cnt,int iocb_flags, +unsigned long *cnt,int *iocb_flags,__be32 *verf);__be32 nfsd_vfs_write (struct svc_rqst *rqstp,struct svc_fh *fhp,struct nfsd_file *nf,loff_t offset,const struct xdr_buf *payload, -unsigned long *cnt,int iocb_flags, +unsigned long *cnt,int *iocb_flags,__be32 *verf);__be32 nfsd_readlink (struct svc_rqst *,struct svc_fh *,char *,int *);
fs/nfsd/xdr3.h +1 -1
@@ -153,7 +153,7 @@ struct nfsd3_writeres {__be32 status;struct svc_fh fh;unsigned long count; -int committed; +u32 committed;__be32 verf[2 ]; };
Signature · 1 line
2.52.0
PATCH 6/10 NFSD: let a direct-mode WRITE raise stable_how and elide the client's COMMIT
In reply to Mike SnitzerA WRITE serviced in a direct mode is already persistent when NFSD
replies: the aligned middle is O_DIRECT, and a synchronous WRITE fsyncs whatever was buffered before the reply is sent. The reply still says UNSTABLE, so the client dutifully sends a COMMIT for data that is already on stable storage, and NFSD answers it with an fsync that has nothing left to write.
Add two io_cache_write modes that issue direct I/O exactly like NFSD_IO_DIRECT and raise the stable_how of the reply to a floor:
NFSD_IO_DIRECT_WRITE_DATA_SYNC (3): at least NFS_DATA_SYNC NFSD_IO_DIRECT_WRITE_FILE_SYNC (4): at least NFS_FILE_SYNC
A client that asked for more is left alone, and the WRITE is persisted to the level the reply reports before that reply is sent. A client told FILE_SYNC has no reason to COMMIT and sends none.
What this removes is the COMMIT traffic, and it is worth most where a client's writes are carved into several WRITE RPCs, because each piece is then committed separately. Measured on a pNFS flexfiles share where every write straddles two data servers, so every write becomes two WRITE RPCs and, under NFSD_IO_DIRECT, two COMMITs: the two modes do identical durability work, one fsync per COMMIT against one fsync per WRITE on identical WRITE counts, and the COMMIT RPCs alone cost NFSD_IO_DIRECT 31% more server CPU and 42% more client CPU for the same bytes, about 20 us of server CPU per COMMIT plus a client cost that grows with the range committed. Where only one write in 22 is split the same effect is a couple of cores on each side and no resolvable throughput difference, and writes that fit a single RPC send no COMMIT in either mode.
Choose 4 when the export services WRITEs in a direct mode and clients split their writes; choose 3 to promise only the data, leaving a client that needs metadata durability to COMMIT for it. Both are inert for READ, and for a WRITE the client already marked FILE_SYNC.
--- .../filesystems/nfs/nfsd-io-modes.rst | 17 ++++++++-- fs/nfsd/debugfs.c | 5 +++ fs/nfsd/nfsd.h | 2 ++ fs/nfsd/vfs.c | 33 +++++++++++++++++-- 4 files changed, 52 insertions(+), 5 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +15 -2
@@ -25,12 +25,14 @@ Based on the configured settings, NFSD's IO will either be: - cached using page cache (NFSD_IO_BUFFERED=0) - cached but removed from page cache on completion (NFSD_IO_DONTCACHE=1) - not cached stable_how=NFS_UNSTABLE (NFSD_IO_DIRECT=2) +- not cached stable_how=NFS_DATA_SYNC (NFSD_IO_DIRECT_WRITE_DATA_SYNC=3) +- not cached stable_how=NFS_FILE_SYNC (NFSD_IO_DIRECT_WRITE_FILE_SYNC=4) -To set an NFSD IO mode, write a supported value (0 - 2) to the +To set an NFSD IO mode, write a supported value (0 - 4) to the corresponding IO operation's debugfs interface, e.g.:: echo 2 > /sys/kernel/debug/nfsd/io_cache_read - echo 2 > /sys/kernel/debug/nfsd/io_cache_write + echo 4 > /sys/kernel/debug/nfsd/io_cache_write To check which IO mode NFSD is using for READ or WRITE, simply read the corresponding IO operation's debugfs interface, e.g.::
@@ -38,6 +40,17 @@ corresponding IO operation's debugfs interface, e.g.:: cat /sys/kernel/debug/nfsd/io_cache_read cat /sys/kernel/debug/nfsd/io_cache_write +The two NFSD_IO_DIRECT_WRITE_*_SYNC modes raise the stable_how of every +WRITE to at least NFS_DATA_SYNC or NFS_FILE_SYNC, persist the WRITE +accordingly before replying, and return the raised value to the client; +a client that asked for a higher stable_how is left alone. With +NFSD_IO_DIRECT_WRITE_FILE_SYNC the client sends no COMMIT. Against +NFSD_IO_DIRECT the durability work is the same, one fsync per WRITE +instead of one per COMMIT; what NFSD_IO_DIRECT adds is the COMMIT RPCs +themselves, tens of microseconds of server CPU each plus a client cost +that grows with the range committed, which matters in proportion to how +many of a client's WRITEs need a COMMIT. + If you experiment with NFSD's IO modes on a recent kernel and have interesting results, please report them to [email protected]
fs/nfsd/debugfs.c +5 -0
@@ -113,6 +113,9 @@ DEFINE_DEBUGFS_ATTRIBUTE(nfsd_io_cache_read_fops, nfsd_io_cache_read_get,* Contents: * %0: NFS WRITE will use buffered IO * %1: NFS WRITE will use dontcache (buffered IO w/ dropbehind) +* %2: NFS WRITE will use direct IO with stable_how=NFS_UNSTABLE +* %3: NFS WRITE will use direct IO with stable_how=NFS_DATA_SYNC +* %4: NFS WRITE will use direct IO with stable_how=NFS_FILE_SYNC * * This setting takes immediate effect for all NFS versions, * all exports, and in all NFSD net namespaces.
@@ -132,6 +135,8 @@ static int nfsd_io_cache_write_set(void *data, u64 val)case NFSD_IO_BUFFERED :case NFSD_IO_DONTCACHE :case NFSD_IO_DIRECT : +case NFSD_IO_DIRECT_WRITE_DATA_SYNC : +case NFSD_IO_DIRECT_WRITE_FILE_SYNC : nfsd_io_cache_write = val;/* * Adjust nfsd_io_cache_{read,write} to avoid
fs/nfsd/nfsd.h +2 -0
@@ -138,6 +138,8 @@ enum {NFSD_IO_BUFFERED ,NFSD_IO_DONTCACHE ,NFSD_IO_DIRECT , +NFSD_IO_DIRECT_WRITE_DATA_SYNC , + NFSD_IO_DIRECT_WRITE_FILE_SYNC, };extern u64 nfsd_io_cache_read__read_mostly ;
fs/nfsd/vfs.c +30 -3
@@ -1399,17 +1399,42 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,return 1 ; } +/* +* Raise the stability of this WRITE to at least @floor_iocb_flags, and +* record what was achieved in @iocb_flags so the reply can report it. +* A client that asked for more is left alone. +*/ +static void +nfsd_write_raise_stability (int floor_iocb_flags,struct kiocb *kiocb, +int *iocb_flags) +{ +if ((*iocb_flags & floor_iocb_flags) == floor_iocb_flags) +return ;/* already at or above the floor */ + + *iocb_flags |= floor_iocb_flags; + kiocb->ki_flags |= floor_iocb_flags; +} + static noinline_for_stack intnfsd_direct_write (struct svc_rqst *rqstp,struct svc_fh *fhp, -struct nfsd_file *nf,unsigned int nvecs, +struct nfsd_file *nf,int *iocb_flags,unsigned int nvecs,unsigned long *cnt,struct kiocb *kiocb) {struct nfsd_write_dio_seg segments[3 ]; +int floor_iocb_flags =0 ;struct file *file = nf->nf_file;unsigned int nsegs, i;ssize_t host_err;size_t expected; +if (nfsd_io_cache_write ==NFSD_IO_DIRECT_WRITE_FILE_SYNC ) + floor_iocb_flags =IOCB_DSYNC |IOCB_SYNC ; +else if (nfsd_io_cache_write ==NFSD_IO_DIRECT_WRITE_DATA_SYNC ) + floor_iocb_flags =IOCB_DSYNC ; +if (floor_iocb_flags) +nfsd_write_raise_stability (floor_iocb_flags, kiocb, + iocb_flags); + nsegs =nfsd_write_dio_iters_init (nf, rqstp->rq_bvec, nvecs, kiocb, *cnt, segments);
@@ -1513,8 +1538,10 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp,switch (nfsd_io_cache_write) {case NFSD_IO_DIRECT : - host_err =nfsd_direct_write (rqstp, fhp, nf, nvecs, - cnt, &kiocb); +case NFSD_IO_DIRECT_WRITE_DATA_SYNC : +case NFSD_IO_DIRECT_WRITE_FILE_SYNC : + host_err =nfsd_direct_write (rqstp, fhp, nf, iocb_flags, + nvecs, cnt, &kiocb);break ;case NFSD_IO_DONTCACHE :if (file->f_op->fop_flags &FOP_DONTCACHE )
Signature · 1 line
2.52.0
PATCH 7/10 NFSD: persist a synchronous direct-mode WRITE once, after all of its segments
In reply to Mike Snitzernfsd_direct_write() may issue a WRITE as up to three segments: a buffered prefix, a direct middle and a buffered suffix. For a FILE_SYNC or DATA_SYNC WRITE (from the client, or as the floor imposed by NFSD_IO_DIRECT_WRITE_{DATA,FILE}_SYNC) the kiocb carries IOCB_DSYNC and every segment inherits it, so generic_write_sync() runs a range fsync after each segment: up to three cache flushes and log forces per WRITE, and each one writes back and drops the boundary page it just touched.
Strip IOCB_DSYNC and IOCB_SYNC from the per-segment flags and persist the WRITE once with vfs_fsync_range() over the bytes actually written, after the last segment. Durability is unchanged: the reply is not sent until the fsync completes, and datasync mirrors the previous per-segment choice (IOCB_SYNC present means metadata too). An fsync failure is returned like a write failure.
Besides the fewer flushes, this puts the sync under NFSD's control, which the next change uses to keep the boundary pages of a split WRITE cached until the partner WRITE completes them.
--- fs/nfsd/vfs.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +19 -1
@@ -1423,6 +1423,8 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,struct nfsd_write_dio_seg segments[3 ];int floor_iocb_flags =0 ;struct file *file = nf->nf_file; +loff_t start = kiocb->ki_pos; +bool sync, datasync;unsigned int nsegs, i;ssize_t host_err;size_t expected;
@@ -1435,12 +1437,21 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,nfsd_write_raise_stability (floor_iocb_flags, kiocb, iocb_flags); +/* +* A synchronous WRITE (client FILE_SYNC/DATA_SYNC, or a floor set by +* the IO mode) is persisted once, after all of its segments, rather +* than by generic_write_sync() after each segment: one cache flush +* and log force instead of up to three. +*/ + sync = kiocb->ki_flags &IOCB_DSYNC ; + datasync = !(kiocb->ki_flags &IOCB_SYNC ); + nsegs =nfsd_write_dio_iters_init (nf, rqstp->rq_bvec, nvecs, kiocb, *cnt, segments); *cnt =0 ;for (i =0 ; i < nsegs; i++) { - kiocb->ki_flags = segments[i].flags; + kiocb->ki_flags = segments[i].flags & ~(IOCB_DSYNC |IOCB_SYNC );if (kiocb->ki_flags &IOCB_DIRECT )trace_nfsd_write_direct (rqstp, fhp, kiocb->ki_pos, segments[i].iter.count);
@@ -1458,6 +1469,13 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,break ;/* partial write */ } +if (sync && *cnt) { + host_err =vfs_fsync_range (file, start, start + *cnt -1 , + datasync); +if (host_err <0 ) +return host_err; + } +return 0 ; }
Signature · 1 line
2.52.0
PATCH 8/10 NFSD: keep boundary page of a split direct-mode WRITE until both writers complete
In reply to Mike SnitzerA direct-mode WRITE whose ends are not aligned for direct I/O is split into a buffered prefix, a direct middle and a buffered suffix, and the prefix and suffix are issued IOCB_DONTCACHE so their pages are dropped once written back. The page holding such a segment is shared by exactly two WRITEs, the one ending in it and the one starting in it, which may arrive in either order, from different clients, at the same time. If it is dropped as soon as the first writer's data is written back, the second has to read the block from disk before it can complete it: one 4 KiB read per WRITE. With many clients interleaving small misaligned O_DIRECT records into one shared file, that was 0.81 device reads per record (585 GiB read while writing 8.1 TiB).
Keep the page until both writers have had it, with no state in NFSD and no knowledge of how clients stripe their I/O. Immediately before a boundary segment is written, nfsd_write_dio_boundary_claim() puts an empty folio in the page cache if there is not one there already. The folio is inserted unmarked, which is what makes this work: a DONTCACHE write only marks a folio it allocated itself, so neither writer's write marks this one and it never enters WB_DONTCACHE_DIRTY. That counter is what the DONTCACHE writeback kick targets, and a boundary page counted there is written back, and then dropped, in the gap between the two WRITEs that share it.
The add is atomic, so of two concurrent partners exactly one gets the folio. The other one finds it (-EEXIST) and is therefore the second of the two: it completes the page, and once its data is in it nfsd_write_dio_boundary_complete() marks the folio dropbehind. The mark has to come after the write, because a clean marked folio is dropped by whatever writeback completes next, and it has to stay unaccounted, because counting a folio marked while dirty hands the page straight back to the kick. Whichever writeback cleans the page then drops it:
the WRITE's own sync for FILE_SYNC and DATA_SYNC, the flusher or the client's COMMIT for UNSTABLE.
A WRITE that cannot use direct I/O at all (memory-misaligned payload, or too small for a direct middle) is issued as one buffered DONTCACHE segment. Its first and last pages are shared with the neighbouring WRITEs in the same way, and are claimed the same way.
Measured with 32 interleaved writers over an emulated 4Kn NVMe, io_cache_write=4, direct_misaligned_dontcache=Y, each arm starting from a freshly made file system. 47008-byte records, which split: 704 device reads for 42895 WRITEs, against 45144 for 45664 WRITEs with the mechanism compiled out. 6000-byte records, which never get a direct middle and so are one buffered segment whose end pages are shared: 385 reads against 196589. The DONTCACHE flusher stays idle in both, and the pages are dropped: 81 of the written file's 524066 pages are still resident afterwards.
On a four-server pNFS flexfiles rig, three clients writing 47008-byte records for 240 s against 640 nfsd threads per server, compared with the same servers before this change: device reads fall from 0.014-0.015 per record to 0.003, page-cache growth over the write phase falls from about 11 GiB to 2.9 GiB, and write throughput rises by 2.4% to 4.9% across NFSD_IO_DIRECT and both stable_how floors, with read throughput unchanged. Records of that size always take the split path, since their direct middle is at least 38818 bytes, so the figures are the split path alone; the whole-WRITE fallback needs records below about 12 KB to come into play. Keeping every boundary page cached instead, which a later commit makes possible with a direct_misaligned_dontcache=N debugfs knob, grows the page cache by about 630 GiB over the same runs.
--- .../filesystems/nfs/nfsd-io-modes.rst | 28 ++++ fs/nfsd/vfs.c | 131 ++++++++++++++++-- 2 files changed, 151 insertions(+), 8 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +28 -0
@@ -185,6 +185,34 @@ Misaligned WRITE: misaligned segments use DONTCACHE buffered IO so that their pages are dropped from the page cache once written back. + A FILE_SYNC or DATA_SYNC WRITE (requested by the client, or imposed + as the floor by NFSD_IO_DIRECT_WRITE_FILE_SYNC and + NFSD_IO_DIRECT_WRITE_DATA_SYNC) is persisted once, after all of its + segments have been written, rather than after each segment. + + The page holding a start or end segment is shared by exactly two + WRITEs, the one ending in it and the one starting in it, which may + arrive in either order, from different clients, and at the same + time. Both segments are issued as DONTCACHE buffered IO, so the page + would be dropped as soon as the first writer's data is written back, + leaving the second to read it back. Immediately before writing a + start or end segment NFSD therefore puts an empty page in the page + cache if there is not one already. That page is not marked "drop + behind" when it is created, so it does not count towards the + DONTCACHE writeback backlog and the writeback kick does not write it + back, and drop it, between the two WRITEs that share it. + + Whichever WRITE finds the page already there is the second of the + two: it completes the page and marks it "drop behind" once its data + is in it, so whichever writeback cleans it afterwards (the WRITE's + own sync for FILE_SYNC or DATA_SYNC, the flusher or the client's + COMMIT for UNSTABLE) drops it. A WRITE that gets no direct middle at + all is issued as a single buffered DONTCACHE segment, and its first + and last pages are shared and handled the same way. The retained + page cache is the set of half-written boundary pages, which grows + with how far concurrent writers drift apart, not with bytes + written. + The O_DIRECT middle segment also carries the DONTCACHE flag. It has no effect while the IO really is O_DIRECT, but a filesystem may decide on its own to service the segment with buffered IO instead
fs/nfsd/vfs.c +123 -8
182 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 9/10 NFSD: add direct_misaligned_dontcache debugfs knob
In reply to Mike SnitzerFrom: Jonathan Flynn <[email protected]>
The parts of a direct-mode WRITE that cannot be direct I/O, the misaligned prefix and suffix of a split WRITE and the whole WRITE when it is not split, are issued IOCB_DONTCACHE so their pages are dropped once written back. That is the right default for the workloads a direct mode is chosen for, but it is a policy rather than a requirement: a workload that reads back what it just wrote, or that keeps rewriting the same partial pages, is better served by those pages staying in the page cache.
Add a bool debugfs knob, /sys/kernel/debug/nfsd/direct_misaligned_dontcache (default Y), to choose between the two:
Y: IOCB_DONTCACHE when the file system supports it, with a split's
boundary page kept in the page cache until both WRITEs sharing it have written it (nfsd_write_dio_boundary_claim()).
N: ordinary cached buffered I/O; nothing is claimed or marked, and the
pages stay until reclaim.
The direct middle and the once-per-WRITE persist are unaffected. The knob is sampled once per WRITE, so a change takes effect immediately and without a remount. It sits beside io_cache_read and io_cache_write, the other controls over how NFSD issues its I/O.
--- .../filesystems/nfs/nfsd-io-modes.rst | 9 ++++++ fs/nfsd/debugfs.c | 21 ++++++++++++++ fs/nfsd/nfsd.h | 1 + fs/nfsd/vfs.c | 28 ++++++++++++------- 4 files changed, 49 insertions(+), 10 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +9 -0
@@ -213,6 +213,15 @@ Misaligned WRITE: with how far concurrent writers drift apart, not with bytes written. + Whether those pages are dropped at all is a policy choice, selected + by /sys/kernel/debug/nfsd/direct_misaligned_dontcache (default Y). + Write N to issue the start and end segments, and the whole-WRITE + fallbacks, as ordinary cached buffered IO: nothing is claimed or + marked and the pages stay until reclaim, which suits a workload that + reads back or rewrites what it just wrote. The O_DIRECT middle + segment is unaffected. The knob is sampled once per WRITE, so a + change takes effect immediately. + The O_DIRECT middle segment also carries the DONTCACHE flag. It has no effect while the IO really is O_DIRECT, but a filesystem may decide on its own to service the segment with buffered IO instead
fs/nfsd/debugfs.c +21 -0
@@ -186,6 +186,24 @@ void nfsd_debugfs_exit(void)* Default 2. Not yet tuned by benchmarking. */ +/* +* /sys/kernel/debug/nfsd/direct_misaligned_dontcache +* +* How a direct-mode WRITE issues the I/O that cannot be direct: the +* misaligned start and end of a split WRITE, and the whole WRITE when it +* is not split. +* +* Contents: +* Y: DONTCACHE when the filesystem supports it, with the boundary page +* of a split kept in the page cache only until both WRITEs sharing +* it have written it +* N: ordinary cached buffered IO, left in the page cache until +* reclaim, for A/B comparison against the DONTCACHE path +* +* Sampled once per WRITE, so it takes effect immediately. The direct +* middle segment is unaffected. +*/ +void nfsd_debugfs_init (void ) { nfsd_top_dir =debugfs_create_dir ("nfsd" ,NULL );
@@ -201,6 +219,9 @@ void nfsd_debugfs_init(void)debugfs_create_u32 ("direct_misaligned_num_pages" ,0644 , nfsd_top_dir, &nfsd_direct_misaligned_num_pages); + +debugfs_create_bool ("direct_misaligned_dontcache" ,0644 , nfsd_top_dir, + &nfsd_direct_misaligned_dontcache);#ifdef CONFIG_NFSD_V4 debugfs_create_bool ("delegated_timestamps" ,0644 , nfsd_top_dir, &nfsd_delegts_enabled);
fs/nfsd/nfsd.h +1 -0
@@ -145,6 +145,7 @@ enum {extern u64 nfsd_io_cache_read__read_mostly ;extern u64 nfsd_io_cache_write__read_mostly ;extern u32 nfsd_direct_misaligned_num_pages__read_mostly ; +extern bool nfsd_direct_misaligned_dontcache__read_mostly ;extern int nfsd_max_blksize;
fs/nfsd/vfs.c +18 -10
@@ -54,6 +54,7 @@ bool nfsd_disable_splice_read __read_mostly;u64 nfsd_io_cache_read__read_mostly =NFSD_IO_BUFFERED ;u64 nfsd_io_cache_write__read_mostly =NFSD_IO_BUFFERED ;u32 nfsd_direct_misaligned_num_pages__read_mostly =2 ; +bool nfsd_direct_misaligned_dontcache__read_mostly = true;/** * nfserrno - Map Linux errnos to NFS errnos
@@ -1373,16 +1374,21 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,size_t prefix, middle, suffix;loff_t offset = iocb->ki_pos;unsigned int dontcache_flags =0 ; +unsigned int buffered_flags;unsigned int nsegs =0 ;if (nf->nf_file->f_op->fop_flags &FOP_DONTCACHE ) dontcache_flags =IOCB_DONTCACHE ; +/* Buffered segments follow the knob; the direct middle does not. */ + buffered_flags =READ_ONCE (nfsd_direct_misaligned_dontcache) ? + dontcache_flags :0 ;/* * Whenever direct I/O cannot be used for the WRITE, fall back to a -* single DONTCACHE buffered I/O when the file system supports it, so -* the WRITE's pages are dropped from the page cache once written -* back, and to a single cached buffered I/O otherwise. +* single DONTCACHE buffered I/O when the file system supports it (and +* nfsd_direct_misaligned_dontcache is set), so the WRITE's pages are +* dropped from the page cache once written back, and to a single +* cached buffered I/O otherwise. * * If the file system doesn't advertise any alignment requirements, * don't try to issue direct I/O at all.
@@ -1421,13 +1427,15 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,* its page with the neighbouring WRITE; see * nfsd_write_dio_boundary_claim(), which nfsd_direct_write() calls right * before issuing each of them, for how the page is held for the -* partner and dropped once both have written it. +* partner and dropped once both have written it. With +* nfsd_direct_misaligned_dontcache=N both are plain cached writes and +* nothing is held or dropped: the pages stay until reclaim. */ if (prefix) {nfsd_write_dio_seg_init (&segments[nsegs], bvec, nvecs, total,0 , prefix, iocb); - segments[nsegs].flags |= dontcache_flags; - segments[nsegs++].boundary = !!dontcache_flags; + segments[nsegs].flags |= buffered_flags; + segments[nsegs++].boundary = !!buffered_flags; } nfsd_write_dio_seg_init(&segments[nsegs], bvec, nvecs,
@@ -1458,8 +1466,8 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,if (suffix) {nfsd_write_dio_seg_init (&segments[nsegs], bvec, nvecs, total, prefix + middle, suffix, iocb); - segments[nsegs].flags |= dontcache_flags; - segments[nsegs++].boundary = !!dontcache_flags; + segments[nsegs].flags |= buffered_flags; + segments[nsegs++].boundary = !!buffered_flags; }return nsegs;
@@ -1473,8 +1481,8 @@ nfsd_write_dio_iters_init(struct nfsd_file *nf, struct bio_vec *bvec,*/ nfsd_write_dio_seg_init (&segments[0 ], bvec, nvecs, total,0 , total, iocb); - segments[0 ].flags |= dontcache_flags; - segments[0 ].edges = !!dontcache_flags; + segments[0 ].flags |= buffered_flags; + segments[0 ].edges = !!buffered_flags;return 1 ; }
Signature · 1 line
2.52.0
PATCH 10/10 NFSD: add tracing for how direct-mode READ and WRITE are serviced
In reply to Mike SnitzerWhen io_cache_read or io_cache_write selects a direct I/O mode, a request may still be serviced with DONTCACHE buffered or normal buffered I/O, and a WRITE may be split into up to three segments that each take a different path depending on the request's offset/length alignment, the payload's memory alignment, and the filesystem's FOP_DONTCACHE support. Until now the only visibility was nfsd_{read,write}_direct for O_DIRECT and nfsd_{read,write}_vector for everything else, so the DONTCACHE and cached buffered cases were indistinguishable and the reason a WRITE was not issued as direct I/O was not recorded at all.
Add:
- nfsd_write_dio_split, emitted once per direct-mode WRITE before any segment is issued. It records the advertised offset and memory alignments, the payload's memory offset within its page, the start, middle and end segment sizes, the number of segments issued, and a disposition describing which path was taken (direct, no_alignment, too_small, no_middle, mem_misaligned), with a dontcache flag set when the buffered segments were issued IOCB_DONTCACHE, so a run with direct_misaligned_dontcache=N is distinguishable from one with the file system lacking FOP_DONTCACHE.
- nfsd_write_dontcache and nfsd_read_dontcache, emitted for WRITE segments and READs serviced with IOCB_DONTCACHE. nfsd_write_vector and nfsd_read_vector now fire only for normal buffered I/O.
The disposition enum lives in vfs.h so trace.h can reference it, and TRACE_DEFINE_ENUM entries are provided for user-space decoding.
Document the new events in nfsd-io-modes.rst, including the iomap iomap_dio_invalidate_fail event that reveals an O_DIRECT segment which the filesystem silently serviced with buffered I/O.
--- .../filesystems/nfs/nfsd-io-modes.rst | 41 ++++++++- fs/nfsd/trace.h | 90 +++++++++++++++++++ fs/nfsd/vfs.c | 44 ++++++--- fs/nfsd/vfs.h | 22 +++++ 4 files changed, 183 insertions(+), 14 deletions(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +38 -3
@@ -246,21 +246,56 @@ Misaligned WRITE: Tracing: The nfsd_read_direct trace event shows how NFSD expands any misaligned READ to the next DIO-aligned block (on either end of the - original READ, as needed). + original READ, as needed). A READ that is serviced with buffered IO + instead emits nfsd_read_dontcache (DONTCACHE buffered IO) or + nfsd_read_vector (normal buffered IO). This combination of trace events is useful for READs:: echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_read_vector/enable + echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_read_dontcache/enable echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_read_direct/enable echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_read_io_done/enable echo 1 > /sys/kernel/tracing/events/xfs/xfs_file_direct_read/enable - The nfsd_write_direct trace event shows how NFSD splits a given - misaligned WRITE into a DIO-aligned middle segment. + The nfsd_write_dio_split trace event is emitted once per WRITE + serviced in a DIRECT IO mode, before any IO is issued, and records + how the WRITE was split: the offset and memory alignments the + filesystem advertised, the memory offset of the WRITE payload, the + sizes of the start, middle and end segments, the number of segments + actually issued, and a disposition naming the reason:: + + direct aligned middle segment uses O_DIRECT + mem_misaligned payload memory is misaligned; one buffered segment + no_alignment filesystem advertises no DIO alignment; one + buffered segment + too_small WRITE is smaller than the larger of the two + alignments; one buffered segment + no_middle no (or too small) aligned middle; one buffered + segment + + Whether those buffered segments are DONTCACHE or normal buffered IO + is reported separately, by dontcache=1 or dontcache=0, because it is + the same answer for every disposition: the buffered segments are + DONTCACHE when the filesystem supports FOP_DONTCACHE. For the direct + disposition it describes the prefix and suffix of the split, the + middle being O_DIRECT. + + Each segment then emits one of nfsd_write_direct (O_DIRECT), + nfsd_write_dontcache (DONTCACHE buffered IO) or nfsd_write_vector + (normal buffered IO) with the segment's offset and length. This combination of trace events is useful for WRITEs:: echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_opened/enable + echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_dio_split/enable echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_direct/enable + echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_dontcache/enable + echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_vector/enable echo 1 > /sys/kernel/tracing/events/nfsd/nfsd_write_io_done/enable echo 1 > /sys/kernel/tracing/events/xfs/xfs_file_direct_write/enable + echo 1 > /sys/kernel/tracing/events/iomap/iomap_dio_invalidate_fail/enable + + iomap_dio_invalidate_fail indicates an O_DIRECT middle segment that + the filesystem silently serviced with normal buffered IO because it + could not invalidate overlapping page cache first.
fs/nfsd/trace.h +90 -0
113 lines not on this page · raw message
fs/nfsd/vfs.c +33 -11
118 lines not on this page · raw message
fs/nfsd/vfs.h +22 -0
@@ -189,4 +189,26 @@ __be32 nfsd_permission(struct svc_cred *cred, struct svc_export *exp,void nfsd_filp_close (struct file *fp); +/* +* How nfsd_write_dio_iters_init() disposed of an NFSD_IO_DIRECT WRITE. +* "DONTCACHE segment" degrades to a cached segment when the file system +* lacks FOP_DONTCACHE. +*/ +/* +* Which exit nfsd_write_dio_iters_init() took. Whether the buffered +* segments carry IOCB_DONTCACHE is reported separately, by +* nfsd_write_dio_split's @dontcache, because it is the same answer for +* every exit below. +*/ +enum nfsd_write_dio_disposition { +NFSD_WRITE_DIO_DIRECT ,/* aligned middle uses direct I/O */ +NFSD_WRITE_DIO_MEM_MISALIGNED ,/* payload memory misaligned: one segment */ +NFSD_WRITE_DIO_NO_ALIGN ,/* fs advertises no alignment: one segment */ +NFSD_WRITE_DIO_TOO_SMALL ,/* len < max(offset_align, mem_align): one segment */ +NFSD_WRITE_DIO_NO_MIDDLE ,/* no or tiny aligned middle: one segment */ + +/* ORed in: the WRITE's buffered segments carry IOCB_DONTCACHE */ +NFSD_WRITE_DIO_DONTCACHE =0x80 , +}; +#endif /* LINUX_NFSD_VFS_H */
Signature · 1 line
2.52.0