From: Jeff Layton <jlayton@kernel.org>
To: Trond Myklebust <trondmy@kernel.org>,
Anna Schumaker <anna@kernel.org>,
Benjamin Coddington <bcodding@hammerspace.com>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>
Cc: Jan Kara <jack@suse.cz>,
linux-nfs@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH 6/6] NFS: add nfs_flush_nowait()
Date: Fri, 04 Dec 2026 12:27:39 -0500 [thread overview]
Message-ID: <aabae9978bbd9b4784173e9cda6b008da99c7d86.camel@kernel.org> (raw)
In-Reply-To: <20261204-nfs-write-congestion-v1-6-f06cdcd58192@kernel.org>
On Fri, 2026-12-04 at 08:14 -0500, Jeff Layton wrote:
> close() under a write delegation should start writeback without
> waiting on congestion from other files on the server. filemap_flush()
> now throttles too, and keying "don't wait" off the wbc would also hit
> flusher passes, which must never give up (commit 2f1f31042ef0 ("nfs:
> Block on write congestion")). Add an NFS-local entry point:
>
> - __nfs_writepages(mapping, wbc, mode, how). nfs_writepages() passes
> NFS_CONGESTION_WAIT (unchanged behaviour).
> - NFS_CONGESTION_STOP (WB_SYNC_NONE only): on a WRITE to a congested
> client, zero nr_to_write so writeback_iter() exits normally. Queued
> requests are sent; the rest stays dirty.
> - nfs_flush_nowait(mapping): whole-file WB_SYNC_NONE flush in stop
> mode, inside wbc_attach_fdatawrite_inode()/wbc_detach_inode().
> - On an early stop, queue the rest to nfs_flush_async(), a
> system_dfl_wq work item doing a normal throttled flush. The flusher
> alone can take up to dirty_expire_centisecs (1GB close on a
> congested server: ~31s, vs ~1.2s with filemap_flush()).
> NFS_INO_FLUSH_ASYNC and _AGAIN allow one work item per inode without
> losing requests. It holds an inode and an active superblock
> reference, and is allocated GFP_NOWAIT; on failure the flusher
> writes the data later.
> - Pass FLUSH_COND_STABLE, as wb_priority() does for
> filemap_fdatawrite(): a file that fits in one WRITE goes FILE_SYNC
> with no COMMIT (~10% in a small-file create/write/close benchmark).
> nfs_generic_pgio() drops the flag after a pg_moreio WRITE, so larger
> files and data after a stop stay UNSTABLE + COMMIT. The async work
> item doesn't use the flag.
> - Tracepoints nfs_write_congestion_stop, nfs_flush_async_queue and
> nfs_flush_async_done.
>
> Use it on close() under a write delegation whose space_limit allows
> caching. Since commit 5445b1fbd123 ("NFSv4: Respect the server imposed
> limit on how many changes we may cache") that path uses
> filemap_fdatawrite() to avoid waiting on writeback, but as WB_SYNC_ALL
> it now waits on congestion in the writepages loop.
>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>
> ---
> Documentation/admin-guide/nfs/nfs-client.rst | 6 +-
> fs/nfs/internal.h | 8 ++
> fs/nfs/nfs4file.c | 5 +-
> fs/nfs/nfstrace.h | 60 +++++++-
> fs/nfs/write.c | 201 ++++++++++++++++++++++++++-
> include/linux/nfs_fs.h | 2 +
> 6 files changed, 274 insertions(+), 8 deletions(-)
>
> diff --git a/Documentation/admin-guide/nfs/nfs-client.rst b/Documentation/admin-guide/nfs/nfs-client.rst
> index 15dada7c454c..b94173b4a386 100644
> --- a/Documentation/admin-guide/nfs/nfs-client.rst
> +++ b/Documentation/admin-guide/nfs/nfs-client.rst
> @@ -74,7 +74,11 @@ completed. Once that count exceeds the limit, the client instance is
> marked congested, and a process writing back dirty data to it waits
> after its next WRITE until the count falls to three quarters of the
> limit. This applies to writeback by the flusher threads as well as to
> -fsync(), close() and sync(). Writes queued in the client but not yet
> +fsync(), close() and sync(). The exception is close() of a file under an
> +NFSv4 write delegation whose space_limit lets the client keep the data
> +cached: it starts writeback without waiting, and once the server is
> +congested it hands the remaining dirty data to a background work item,
> +which is throttled as usual. Writes queued in the client but not yet
> sent are not counted, and O_DIRECT writes are neither counted nor
> throttled. A value of 0 makes each writer wait after every WRITE until
> all WRITEs in flight to that server have completed.
> diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
> index c93ab19b6b4a..b6ca0f03d518 100644
> --- a/fs/nfs/internal.h
> +++ b/fs/nfs/internal.h
> @@ -627,6 +627,14 @@ int nfs_reconfigure(struct fs_context *);
> /* write.c */
> void nfs_write_congestion_charge(struct nfs_pgio_header *hdr);
> void nfs_write_congestion_uncharge(struct nfs_pgio_header *hdr);
> +/* What nfs_writepages() does when a WRITE goes to a congested client */
> +enum nfs_congestion_mode {
> + NFS_CONGESTION_WAIT, /* wait for the congestion to clear */
> + NFS_CONGESTION_STOP, /* stop; leave the rest to async writeback */
> +};
> +
> +int nfs_flush_nowait(struct address_space *mapping);
> +void nfs_flush_async(struct inode *inode, enum nfs_congestion_mode mode);
> extern void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
> struct inode *inode, int ioflags, bool force_mds,
> const struct nfs_pgio_completion_ops *compl_ops);
> diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c
> index 24f6be064c34..f0926b94e1c8 100644
> --- a/fs/nfs/nfs4file.c
> +++ b/fs/nfs/nfs4file.c
> @@ -123,10 +123,11 @@ nfs4_file_flush(struct file *file, fl_owner_t id)
>
> /*
> * If we're holding a write delegation, then check if we're required
> - * to flush the i/o on close. If not, then just start the i/o now.
> + * to flush the i/o on close. If not, then just start the i/o now,
> + * without waiting for congestion caused by other files.
> */
> if (!nfs4_delegation_flush_on_close(inode))
> - return filemap_fdatawrite(file->f_mapping);
> + return nfs_flush_nowait(file->f_mapping);
>
> /* Flush writes to the server and return any errors */
> since = filemap_sample_wb_err(file->f_mapping);
> diff --git a/fs/nfs/nfstrace.h b/fs/nfs/nfstrace.h
> index 066c7edec7bc..0855dc9a8029 100644
> --- a/fs/nfs/nfstrace.h
> +++ b/fs/nfs/nfstrace.h
> @@ -44,7 +44,9 @@
> { BIT(NFS_INO_LAYOUTCOMMIT), "NEED_LAYOUTCOMMIT" }, \
> { BIT(NFS_INO_LAYOUTCOMMITTING), "LAYOUTCOMMIT" }, \
> { BIT(NFS_INO_LAYOUTSTATS), "LAYOUTSTATS" }, \
> - { BIT(NFS_INO_ODIRECT), "ODIRECT" })
> + { BIT(NFS_INO_ODIRECT), "ODIRECT" }, \
> + { BIT(NFS_INO_FLUSH_ASYNC), "FLUSH_ASYNC" }, \
> + { BIT(NFS_INO_FLUSH_ASYNC_AGAIN), "FLUSH_ASYNC_AGAIN" })
>
> #define nfs_show_wb_flags(v) \
> __print_flags(v, "|", \
> @@ -1129,6 +1131,62 @@ DECLARE_EVENT_CLASS(nfs_write_congestion_event,
> DEFINE_NFS_WRITE_CONGESTION_EVENT(nfs_write_congestion_on);
> DEFINE_NFS_WRITE_CONGESTION_EVENT(nfs_write_congestion_off);
> DEFINE_NFS_WRITE_CONGESTION_EVENT(nfs_write_congestion_wait);
> +DEFINE_NFS_WRITE_CONGESTION_EVENT(nfs_write_congestion_stop);
> +
> +TRACE_DEFINE_ENUM(NFS_CONGESTION_WAIT);
> +TRACE_DEFINE_ENUM(NFS_CONGESTION_STOP);
> +
> +#define show_nfs_congestion_mode(mode) \
> + __print_symbolic(mode, \
> + { NFS_CONGESTION_WAIT, "WAIT" }, \
> + { NFS_CONGESTION_STOP, "STOP" })
> +
> +DECLARE_EVENT_CLASS(nfs_flush_async_event,
> + TP_PROTO(
> + const struct inode *inode,
> + enum nfs_congestion_mode mode,
> + int error
> + ),
> +
> + TP_ARGS(inode, mode, error),
> +
> + TP_STRUCT__entry(
> + __field(dev_t, dev)
> + __field(u32, fhandle)
> + __field(u64, fileid)
> + __field(unsigned int, mode)
> + __field(int, error)
> + ),
> +
> + TP_fast_assign(
> + __entry->dev = inode->i_sb->s_dev;
> + __entry->fileid = inode->i_ino;
> + __entry->fhandle = nfs_fhandle_hash(NFS_FH(inode));
> + __entry->mode = mode;
> + __entry->error = error < 0 ? error : 0;
> + ),
> +
> + TP_printk(
> + "fileid=%02x:%02x:%llu fhandle=0x%08x mode=%s error=%d",
> + MAJOR(__entry->dev), MINOR(__entry->dev),
> + (unsigned long long)__entry->fileid,
> + __entry->fhandle,
> + show_nfs_congestion_mode(__entry->mode),
> + __entry->error
> + )
> +);
> +
> +#define DEFINE_NFS_FLUSH_ASYNC_EVENT(name) \
> + DEFINE_EVENT(nfs_flush_async_event, name, \
> + TP_PROTO( \
> + const struct inode *inode, \
> + enum nfs_congestion_mode mode, \
> + int error \
> + ), \
> + TP_ARGS(inode, mode, error))
> +
> +DEFINE_NFS_FLUSH_ASYNC_EVENT(nfs_flush_async_queue);
> +DEFINE_NFS_FLUSH_ASYNC_EVENT(nfs_flush_async_done);
>
> DECLARE_EVENT_CLASS(nfs_kiocb_event,
> TP_PROTO(
> diff --git a/fs/nfs/write.c b/fs/nfs/write.c
> index 62b5ab371894..187470c6713f 100644
> --- a/fs/nfs/write.c
> +++ b/fs/nfs/write.c
> @@ -764,13 +764,31 @@ static void nfs_io_completion_commit(void *inode)
> nfs_commit_inode(inode, 0);
> }
>
> -int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> +static void nfs_writepages_stop(struct nfs_pageio_descriptor *pgio,
> + struct writeback_control *wbc)
> +{
> + struct nfs_client *clp = pgio->pg_congested_clp;
> +
> + trace_nfs_write_congestion_stop(clp, nfs_write_inflight(clp),
> + nfs_congestion_on_thresh());
> + nfs_pageio_clear_congested(pgio);
> + wbc->nr_to_write = 0;
> +}
> +
> +/*
> + * @how is added to the stability flags derived from @wbc, for the cases
> + * where nfs_writepages() would ask for them (no "write=eager", or a
> + * flusher pass).
> + */
> +static int __nfs_writepages(struct address_space *mapping,
> + struct writeback_control *wbc,
> + enum nfs_congestion_mode mode, int how)
> {
> struct inode *inode = mapping->host;
> struct nfs_pageio_descriptor pgio;
> struct nfs_io_completion *ioc = NULL;
> unsigned int mntflags = NFS_SERVER(inode)->flags;
> - bool killed = false;
> + bool killed = false, stopped = false;
> int priority = 0;
> int err;
>
> @@ -784,7 +802,7 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> if (ioc)
> nfs_io_completion_init(ioc, nfs_io_completion_commit,
> inode);
> - priority = wb_priority(wbc);
> + priority = wb_priority(wbc) | how;
> }
>
> do {
> @@ -799,6 +817,17 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> folio_unlock(folio);
> if (!pgio.pg_congested_clp)
> continue;
> + /*
> + * Stop instead of waiting: with nr_to_write at zero,
> + * writeback_iter() releases its batch and returns
> + * NULL. Folios not reached stay dirty.
> + */
> + if (mode == NFS_CONGESTION_STOP &&
> + wbc->sync_mode == WB_SYNC_NONE) {
> + nfs_writepages_stop(&pgio, wbc);
> + stopped = true;
> + continue;
> + }
> /*
> * Throttle before sending more. On a fatal signal,
> * stop waiting and let writeback_iter() see the
> @@ -818,7 +847,7 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> nfs_pageio_clear_congested(&pgio);
> if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
> break;
> - } while (err < 0 && !nfs_error_is_fatal(err));
> + } while (err < 0 && !nfs_error_is_fatal(err) && !stopped);
> nfs_io_completion_put(ioc);
>
> if (err > 0)
> @@ -827,6 +856,170 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> return err;
> }
>
> +int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
> +{
> + return __nfs_writepages(mapping, wbc, NFS_CONGESTION_WAIT, 0);
> +}
> +
> +/*
> + * WB_SYNC_NONE writeback of the whole of @mapping. If @stopped is not
> + * NULL, report whether the stop path ended it early; that is the only way
> + * nr_to_write, which starts at LONG_MAX, can drop to zero.
> + */
> +static int nfs_flush_mapping(struct address_space *mapping,
> + enum nfs_congestion_mode mode, int how,
> + bool *stopped)
> +{
> + struct writeback_control wbc = {
> + .sync_mode = WB_SYNC_NONE,
> + .nr_to_write = LONG_MAX,
> + .range_start = 0,
> + .range_end = LLONG_MAX,
> + };
> + int ret;
> +
> + if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
> + return 0;
> +
> + wbc_attach_fdatawrite_inode(&wbc, mapping->host);
> + ret = __nfs_writepages(mapping, &wbc, mode, how);
> + wbc_detach_inode(&wbc);
> + if (stopped)
> + *stopped = wbc.nr_to_write <= 0;
> + return ret;
> +}
> +
> +struct nfs_flush_async {
> + struct work_struct work;
> + struct inode *inode;
> + enum nfs_congestion_mode mode;
> +};
> +
> +static void nfs_flush_async_bits(enum nfs_congestion_mode mode,
> + int *active, int *again)
> +{
> + *active = NFS_INO_FLUSH_ASYNC;
> + *again = NFS_INO_FLUSH_ASYNC_AGAIN;
> +}
> +
> +static void nfs_flush_async_work(struct work_struct *work)
> +{
> + struct nfs_flush_async *fa =
> + container_of(work, struct nfs_flush_async, work);
> + struct inode *inode = fa->inode;
> + struct super_block *sb = inode->i_sb;
> + unsigned long *flags = &NFS_I(inode)->flags;
> + int active, again, ret;
> +
> + nfs_flush_async_bits(fa->mode, &active, &again);
> + for (;;) {
> + clear_bit(again, flags);
> + /*
> + * Clear @again before looking for dirty folios, so that a
> + * request for data dirtied after this pass started its
> + * lookup is seen by the check below.
> + */
> + smp_mb__after_atomic();
> + ret = nfs_flush_mapping(inode->i_mapping, fa->mode, 0, NULL);
> + trace_nfs_flush_async_done(inode, fa->mode, ret);
> + if (test_bit(again, flags))
> + continue;
> + /*
> + * Give up ownership, then look again: a request that set
> + * @again before we cleared @active found it set and relies
> + * on us. If someone else has already taken @active, they
> + * will do it.
> + */
> + clear_bit(active, flags);
> + /* Pairs with set_bit() + test_and_set_bit() in nfs_flush_async() */
> + smp_mb__after_atomic();
> + if (!test_bit(again, flags) || test_and_set_bit(active, flags))
> + break;
> + }
> + iput(inode);
> + nfs_sb_deactive(sb);
> + kfree(fa);
> +}
> +
> +/**
> + * nfs_flush_async - start writeback of an inode from a work item
> + * @inode: inode to write back
> + * @mode: how the writeback treats write congestion
> + *
> + * Queue WB_SYNC_NONE writeback of the whole file on a work item, without
> + * waiting for it. Each inode has at most one such work item per mode; a
> + * request made while one is queued or running makes it go round again
> + * instead of queueing another.
> + *
> + * Best effort: if the work item can't be set up, the flusher still writes
> + * the data back eventually. The caller must hold a reference to @inode.
> + */
> +void nfs_flush_async(struct inode *inode, enum nfs_congestion_mode mode)
> +{
> + unsigned long *flags = &NFS_I(inode)->flags;
> + struct nfs_flush_async *fa;
> + int active, again;
> +
> + if (!S_ISREG(inode->i_mode) ||
> + !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY))
> + return;
> +
> + nfs_flush_async_bits(mode, &active, &again);
> + set_bit(again, flags);
> + if (test_and_set_bit(active, flags))
> + return;
> +
> + fa = kmalloc_obj(*fa, GFP_NOWAIT);
> + if (!fa)
> + goto out_clear;
> + /* The caller holds a reference, so an iput() here can't be the last */
> + fa->inode = igrab(inode);
> + if (!fa->inode)
> + goto out_free;
> + /* Keep the superblock alive too: an inode reference doesn't */
> + if (!nfs_sb_active(inode->i_sb))
> + goto out_iput;
> + fa->mode = mode;
> + INIT_WORK(&fa->work, nfs_flush_async_work);
> + trace_nfs_flush_async_queue(inode, mode, 0);
> + queue_work(system_dfl_wq, &fa->work);
Self-review here:
This is going to be problematic. We need our own workqueue here at
least so we don't tie up all of the system_dfl_wq slots. I have a
respin in the works to fix that and some other things that agentic
review turned up. I'll send a v2 in a few days.
Let me know if you have issues with the general approach, however.
> + return;
> +
> +out_iput:
> + iput(fa->inode);
> +out_free:
> + kfree(fa);
> +out_clear:
> + clear_bit(active, flags);
> +}
> +
> +/**
> + * nfs_flush_nowait - start writeback without waiting on congestion
> + * @mapping: address space to flush
> + *
> + * Start writeback of all dirty data in @mapping, like filemap_flush(),
> + * but never wait for write congestion to clear. Once a WRITE goes to a
> + * congested client, stop, and hand the folios not yet reached to a work
> + * item that writes them back with the usual throttling.
> + *
> + * A file that fits in one WRITE is sent FILE_SYNC, as WB_SYNC_ALL
> + * writeback would, so that it needs no COMMIT.
> + *
> + * Return: 0, or a negative errno if starting writeback failed.
> + */
> +int nfs_flush_nowait(struct address_space *mapping)
> +{
> + bool stopped = false;
> + int ret;
> +
> + ret = nfs_flush_mapping(mapping, NFS_CONGESTION_STOP,
> + FLUSH_COND_STABLE, &stopped);
> + if (stopped)
> + nfs_flush_async(mapping->host, NFS_CONGESTION_WAIT);
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(nfs_flush_nowait);
> +
> /*
> * Insert a write request into an inode
> */
> diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
> index 29f59796ed57..4e1a6d8acc10 100644
> --- a/include/linux/nfs_fs.h
> +++ b/include/linux/nfs_fs.h
> @@ -347,6 +347,8 @@ struct nfs4_copy_state {
> #define NFS_INO_LAYOUTSTATS (11) /* layoutstats inflight */
> #define NFS_INO_ODIRECT (12) /* I/O setting is O_DIRECT */
> #define NFS_INO_REQ_DIR_DELEG (13) /* Request a directory delegation */
> +#define NFS_INO_FLUSH_ASYNC (14) /* async flush queued or running */
> +#define NFS_INO_FLUSH_ASYNC_AGAIN (15) /* async flush requested again */
>
> static inline struct nfs_inode *NFS_I(const struct inode *inode)
> {
--
Jeff Layton <jlayton@kernel.org>
prev parent reply other threads:[~2026-12-04 17:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 13:14 [PATCH 0/6] NFS: rework write congestion control Jeff Layton
2026-12-04 13:14 ` [PATCH 1/6] NFS: add tracepoints for write congestion Jeff Layton
2026-12-04 13:14 ` [PATCH 2/6] NFS: move write congestion state to struct nfs_client Jeff Layton
2026-12-04 13:14 ` [PATCH 3/6] NFS: charge write congestion at RPC submission, per target client Jeff Layton
2026-12-04 13:14 ` [PATCH 4/6] NFS: throttle writeback in the writepages loop, for all sync modes Jeff Layton
2026-12-04 13:14 ` [PATCH 5/6] NFS: document write congestion control and nfs_congestion_kb Jeff Layton
2026-12-04 13:14 ` [PATCH 6/6] NFS: add nfs_flush_nowait() Jeff Layton
2026-12-04 17:27 ` Jeff Layton [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aabae9978bbd9b4784173e9cda6b008da99c7d86.camel@kernel.org \
--to=jlayton@kernel.org \
--cc=anna@kernel.org \
--cc=bcodding@hammerspace.com \
--cc=corbet@lwn.net \
--cc=jack@suse.cz \
--cc=linux-doc@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
--cc=trondmy@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox