* [PATCH 1/6] NFS: add tracepoints for write congestion
2026-12-04 13:14 [PATCH 0/6] NFS: rework write congestion control Jeff Layton
@ 2026-12-04 13:14 ` Jeff Layton
2026-12-04 13:14 ` [PATCH 2/6] NFS: move write congestion state to struct nfs_client Jeff Layton
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
The NFS client throttles buffered writeback once too much data is
under writeback to a server, but nothing reports when that happens.
That makes it hard to tell whether the throttle is doing anything, or
to compare its behaviour across changes.
Add three tracepoints:
- nfs_write_congestion_on: the in-flight count rose above the ON
threshold and the congested flag was set
- nfs_write_congestion_off: the count fell below the OFF threshold and
the flag was cleared
- nfs_write_congestion_wait: a writer is about to block on congestion
Each reports the server (the nfs_client's hostname, and whether it is
a pNFS data server), the in-flight count and the threshold that
applied. Set and clear the congested flag with xchg() so that only the
transitions are traced. The wakeups and the flag's meaning are
unchanged.
For now the count is per nfs_server and in folios, and the client
hostname does not distinguish two mounts of one server. Later patches
move the count to the nfs_client and switch it to bytes; the events
keep their names and report the new count.
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/nfs/nfstrace.h | 43 +++++++++++++++++++++++++++++++++++++++++++
fs/nfs/write.c | 21 ++++++++++++++++-----
2 files changed, 59 insertions(+), 5 deletions(-)
diff --git a/fs/nfs/nfstrace.h b/fs/nfs/nfstrace.h
index a32c76df72db..066c7edec7bc 100644
--- a/fs/nfs/nfstrace.h
+++ b/fs/nfs/nfstrace.h
@@ -1087,6 +1087,49 @@ DEFINE_NFS_FOLIO_EVENT_DONE(nfs_write_end_done);
DEFINE_NFS_FOLIO_EVENT(nfs_writepages);
DEFINE_NFS_FOLIO_EVENT_DONE(nfs_writepages_done);
+DECLARE_EVENT_CLASS(nfs_write_congestion_event,
+ TP_PROTO(
+ const struct nfs_client *clp,
+ long inflight,
+ long limit
+ ),
+
+ TP_ARGS(clp, inflight, limit),
+
+ TP_STRUCT__entry(
+ __string(dstaddr, clp->cl_hostname)
+ __field(long, inflight)
+ __field(long, limit)
+ __field(bool, ds)
+ ),
+
+ TP_fast_assign(
+ __assign_str(dstaddr);
+ __entry->inflight = inflight;
+ __entry->limit = limit;
+ __entry->ds = test_bit(NFS_CS_DS, &clp->cl_flags);
+ ),
+
+ TP_printk(
+ "dstaddr=%s%s inflight=%ld limit=%ld",
+ __get_str(dstaddr), __entry->ds ? " (DS)" : "",
+ __entry->inflight, __entry->limit
+ )
+);
+
+#define DEFINE_NFS_WRITE_CONGESTION_EVENT(name) \
+ DEFINE_EVENT(nfs_write_congestion_event, name, \
+ TP_PROTO( \
+ const struct nfs_client *clp, \
+ long inflight, \
+ long limit \
+ ), \
+ TP_ARGS(clp, inflight, limit))
+
+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);
+
DECLARE_EVENT_CLASS(nfs_kiocb_event,
TP_PROTO(
const struct kiocb *iocb,
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index af8735d0d67f..354ef195995a 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -286,20 +286,28 @@ int nfs_congestion_kb;
static void nfs_folio_set_writeback(struct folio *folio)
{
struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
+ long writeback;
folio_start_writeback(folio);
- if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH)
- nfss->write_congested = 1;
+ writeback = atomic_long_inc_return(&nfss->writeback);
+ if (writeback > NFS_CONGESTION_ON_THRESH &&
+ !xchg(&nfss->write_congested, 1))
+ trace_nfs_write_congestion_on(nfss->nfs_client, writeback,
+ NFS_CONGESTION_ON_THRESH);
}
static void nfs_folio_end_writeback(struct folio *folio)
{
struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
+ long writeback;
folio_end_writeback_no_dropbehind(folio);
- if (atomic_long_dec_return(&nfss->writeback) <
- NFS_CONGESTION_OFF_THRESH) {
- nfss->write_congested = 0;
+ writeback = atomic_long_dec_return(&nfss->writeback);
+ if (writeback < NFS_CONGESTION_OFF_THRESH) {
+ if (xchg(&nfss->write_congested, 0))
+ trace_nfs_write_congestion_off(nfss->nfs_client,
+ writeback,
+ NFS_CONGESTION_OFF_THRESH);
wake_up_all(&nfss->write_congestion_wait);
}
}
@@ -666,6 +674,9 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
/* Wait with writeback until write congestion eases */
if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) {
+ trace_nfs_write_congestion_wait(nfss->nfs_client,
+ atomic_long_read(&nfss->writeback),
+ NFS_CONGESTION_OFF_THRESH);
err = wait_event_killable(nfss->write_congestion_wait,
nfss->write_congested == 0);
if (err)
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 2/6] NFS: move write congestion state to struct nfs_client
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 ` Jeff Layton
2026-12-04 13:14 ` [PATCH 3/6] NFS: charge write congestion at RPC submission, per target client Jeff Layton
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
The write congestion count, flag and waitqueue live in struct
nfs_server, one per superblock. Two mounts of the same server get
separate nfs_server structs that share one nfs_client and one RPC
transport, yet each gets a full nfs_congestion_kb budget, so the
effective limit on a server grows with the number of mounts.
Move the state into a struct nfs_write_congestion embedded in struct
nfs_client, so that every mount sharing a client shares the budget.
This also gives pNFS data server clients a place for their own state,
which a later patch uses.
The accounting itself is unchanged: folios are still counted at
writeback start and end against the inode's nfs_server->nfs_client.
The congested flag becomes a bit in the new flags word, and waiters
are now only woken when the flag is actually cleared rather than on
every completion below the OFF threshold.
Since the accounting still looks up NFS_SERVER(inode)->nfs_client at
both ends, a migration that switches the server to a different client
while folios are under writeback leaves both counts off by the folios
in flight. The next patch records the charged client with each WRITE
and removes that window.
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/nfs/client.c | 4 +---
fs/nfs/write.c | 46 ++++++++++++++++++++++++++++------------------
include/linux/nfs_fs_sb.h | 16 +++++++++++++---
3 files changed, 42 insertions(+), 24 deletions(-)
diff --git a/fs/nfs/client.c b/fs/nfs/client.c
index 43e2acb2af14..3de07a9dd075 100644
--- a/fs/nfs/client.c
+++ b/fs/nfs/client.c
@@ -175,6 +175,7 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
INIT_LIST_HEAD(&clp->cl_superblocks);
clp->cl_rpcclient = ERR_PTR(-EINVAL);
+ init_waitqueue_head(&clp->cl_wcong.wait);
clp->cl_flags = cl_init->init_flags;
clp->cl_proto = cl_init->proto;
@@ -1111,9 +1112,6 @@ struct nfs_server *nfs_alloc_server(void)
server->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
- init_waitqueue_head(&server->write_congestion_wait);
- atomic_long_set(&server->writeback, 0);
-
atomic64_set(&server->owner_ctr, 0);
pnfs_init_server(server);
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 354ef195995a..475742f2afbb 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -283,32 +283,43 @@ int nfs_congestion_kb;
#define NFS_CONGESTION_OFF_THRESH \
(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
+static bool nfs_write_congested(struct nfs_client *clp)
+{
+ return test_bit(NFS_WCONG_CONGESTED, &clp->cl_wcong.flags);
+}
+
+static long nfs_write_inflight(struct nfs_client *clp)
+{
+ return atomic_long_read(&clp->cl_wcong.inflight);
+}
+
static void nfs_folio_set_writeback(struct folio *folio)
{
- struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
+ struct nfs_client *clp = NFS_SERVER(folio->mapping->host)->nfs_client;
+ struct nfs_write_congestion *wcong = &clp->cl_wcong;
long writeback;
folio_start_writeback(folio);
- writeback = atomic_long_inc_return(&nfss->writeback);
+ writeback = atomic_long_inc_return(&wcong->inflight);
if (writeback > NFS_CONGESTION_ON_THRESH &&
- !xchg(&nfss->write_congested, 1))
- trace_nfs_write_congestion_on(nfss->nfs_client, writeback,
+ !test_and_set_bit(NFS_WCONG_CONGESTED, &wcong->flags))
+ trace_nfs_write_congestion_on(clp, writeback,
NFS_CONGESTION_ON_THRESH);
}
static void nfs_folio_end_writeback(struct folio *folio)
{
- struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
+ struct nfs_client *clp = NFS_SERVER(folio->mapping->host)->nfs_client;
+ struct nfs_write_congestion *wcong = &clp->cl_wcong;
long writeback;
folio_end_writeback_no_dropbehind(folio);
- writeback = atomic_long_dec_return(&nfss->writeback);
- if (writeback < NFS_CONGESTION_OFF_THRESH) {
- if (xchg(&nfss->write_congested, 0))
- trace_nfs_write_congestion_off(nfss->nfs_client,
- writeback,
- NFS_CONGESTION_OFF_THRESH);
- wake_up_all(&nfss->write_congestion_wait);
+ writeback = atomic_long_dec_return(&wcong->inflight);
+ if (writeback < NFS_CONGESTION_OFF_THRESH &&
+ test_and_clear_bit(NFS_WCONG_CONGESTED, &wcong->flags)) {
+ trace_nfs_write_congestion_off(clp, writeback,
+ NFS_CONGESTION_OFF_THRESH);
+ wake_up_all(&wcong->wait);
}
}
@@ -666,19 +677,18 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
struct nfs_pageio_descriptor pgio;
struct nfs_io_completion *ioc = NULL;
unsigned int mntflags = NFS_SERVER(inode)->flags;
- struct nfs_server *nfss = NFS_SERVER(inode);
+ struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
int priority = 0;
int err;
trace_nfs_writepages(inode, wbc->range_start, wbc->range_end - wbc->range_start);
/* Wait with writeback until write congestion eases */
- if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) {
- trace_nfs_write_congestion_wait(nfss->nfs_client,
- atomic_long_read(&nfss->writeback),
+ if (wbc->sync_mode == WB_SYNC_NONE && nfs_write_congested(clp)) {
+ trace_nfs_write_congestion_wait(clp, nfs_write_inflight(clp),
NFS_CONGESTION_OFF_THRESH);
- err = wait_event_killable(nfss->write_congestion_wait,
- nfss->write_congested == 0);
+ err = wait_event_killable(clp->cl_wcong.wait,
+ !nfs_write_congested(clp));
if (err)
goto out_err;
}
diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h
index df3fae763e41..c46604f2f4f2 100644
--- a/include/linux/nfs_fs_sb.h
+++ b/include/linux/nfs_fs_sb.h
@@ -23,6 +23,17 @@ struct nfs4_minor_version_ops;
struct nfs41_server_scope;
struct nfs41_impl_id;
+/*
+ * Write congestion state, kept per nfs_client so that all mounts sharing
+ * a client, and all users of a pNFS data server, share one budget.
+ */
+struct nfs_write_congestion {
+ atomic_long_t inflight; /* folios under writeback */
+ unsigned long flags;
+#define NFS_WCONG_CONGESTED 0 /* writers should wait */
+ wait_queue_head_t wait; /* wait until congestion eases */
+};
+
/*
* The nfs_client identifies our client state to the server.
*/
@@ -69,6 +80,8 @@ struct nfs_client {
const char * cl_principal; /* used for machine cred */
struct xprtsec_parms cl_xprtsec; /* xprt security policy */
+ struct nfs_write_congestion cl_wcong; /* write congestion control */
+
#if IS_ENABLED(CONFIG_NFS_V4)
struct list_head cl_ds_clients; /* auth flavor data servers */
u64 cl_clientid; /* constant */
@@ -153,9 +166,6 @@ struct nfs_server {
struct rpc_clnt * client_acl; /* ACL RPC client handle */
struct nlm_host *nlm_host; /* NLM client handle */
struct nfs_iostats __percpu *io_stats; /* I/O statistics */
- wait_queue_head_t write_congestion_wait; /* wait until write congestion eases */
- atomic_long_t writeback; /* number of writeback pages */
- unsigned int write_congested;/* flag set when writeback gets too high */
unsigned int flags; /* various flags */
/* The following are for internal use only. Also see uapi/linux/nfs_mount.h */
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/6] NFS: charge write congestion at RPC submission, per target client
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 ` Jeff Layton
2026-12-04 13:14 ` [PATCH 4/6] NFS: throttle writeback in the writepages loop, for all sync modes Jeff Layton
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
Write congestion is accounted per folio: the count goes up in
nfs_do_writepage() when the folio is marked for writeback and down
when writeback ends. That has three problems.
First, the count goes up before the request is sent. Requests queued
in a pageio descriptor are counted even though they are not in flight,
so throttling inside the writeback loop is not safe: N writers whose
queued requests together exceed the OFF threshold could all wait and
never be woken.
Second, the thresholds are in pages but the count is in folios, and
regular files use large folios. The effective limit is loose by up to
the folio size.
Third, all of it is charged to the MDS (NFS_SERVER(inode)->nfs_client)
wherever the data goes, so every pNFS data server shares one cap and
one waitqueue with the MDS.
Count bytes in WRITE RPCs instead. Charge hdr->args.count in
nfs_initiate_pgio(), which MDS writes and file and flexfiles data
server writes all go through, to hdr->ds_clp if set and otherwise to
the MDS client. Record the client (with a reference) and the byte count
in the header, and uncharge in nfs_pgio_header_free(), which every
header goes through. Short writes and rpc_restart_call() reuse the
header and are not charged again; resends to another DS or the MDS
build a new header and charge the new target. Each flexfiles mirror
has its own header, so each mirror DS is charged. Charging is skipped
for a header that is already charged.
Folio writeback goes back to plain folio_start_writeback() and
folio_end_writeback_no_dropbehind().
Not counted any more:
- Requests queued in a descriptor but not yet sent. That is what makes
a wait inside the writeback loop safe, which a later patch adds.
- Block and SCSI layout writes. They submit bios directly and never
call nfs_initiate_pgio(); the block layer applies its own
backpressure.
Still not counted: O_DIRECT. It also goes through nfs_initiate_pgio()
but is never throttled, so charging it would let O_DIRECT hold a
client above the OFF threshold indefinitely while buffered writers
wait. Localio writes through the page cache are charged like any
other write.
nfs_congestion_kb is now converted to bytes, and a value of zero or
less is treated as zero. Waiters are released once the count drops to
the OFF threshold instead of below it, so that a limit of zero no
longer leaves them waiting forever.
The tracepoints now report bytes.
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/nfs/internal.h | 2 ++
fs/nfs/pagelist.c | 4 +++
fs/nfs/write.c | 92 ++++++++++++++++++++++++++++++++++-------------
include/linux/nfs_fs_sb.h | 2 +-
include/linux/nfs_xdr.h | 2 ++
5 files changed, 77 insertions(+), 25 deletions(-)
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index f3ca9be5e08a..c93ab19b6b4a 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -625,6 +625,8 @@ int nfs_show_stats(struct seq_file *, struct dentry *);
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);
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/pagelist.c b/fs/nfs/pagelist.c
index c76c32a33be5..0e06a9575e68 100644
--- a/fs/nfs/pagelist.c
+++ b/fs/nfs/pagelist.c
@@ -712,6 +712,7 @@ static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
*/
void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
{
+ nfs_write_congestion_uncharge(hdr);
nfs_pgio_data_destroy(hdr);
hdr->rw_ops->rw_free_header(hdr);
}
@@ -809,6 +810,9 @@ int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
hdr->args.count,
(unsigned long long)hdr->args.offset);
+ /* The hdr may be freed as soon as the I/O is started */
+ nfs_write_congestion_charge(hdr);
+
if (localio)
return nfs_local_doio(NFS_SERVER(hdr->inode)->nfs_client,
localio, hdr, call_ops);
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 475742f2afbb..8c76e0aa49fb 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -275,13 +275,31 @@ static int wb_priority(struct writeback_control *wbc)
/*
* NFS congestion control
+ *
+ * Each nfs_client counts the bytes in WRITE RPCs it has outstanding. The
+ * count is charged when a WRITE is sent (nfs_initiate_pgio()) and
+ * uncharged when its header is freed. Above nfs_congestion_kb the client
+ * is marked congested, and buffered writers to it wait until the count
+ * drops back to three quarters of the limit.
*/
int nfs_congestion_kb;
-#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
-#define NFS_CONGESTION_OFF_THRESH \
- (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
+static long nfs_congestion_on_thresh(void)
+{
+ int kb = READ_ONCE(nfs_congestion_kb);
+
+ if (kb <= 0)
+ return 0;
+ return min_t(long, kb, LONG_MAX >> 10) << 10;
+}
+
+static long nfs_congestion_off_thresh(void)
+{
+ long on = nfs_congestion_on_thresh();
+
+ return on - (on >> 2);
+}
static bool nfs_write_congested(struct nfs_client *clp)
{
@@ -293,41 +311,67 @@ static long nfs_write_inflight(struct nfs_client *clp)
return atomic_long_read(&clp->cl_wcong.inflight);
}
-static void nfs_folio_set_writeback(struct folio *folio)
+/**
+ * nfs_write_congestion_charge - account a WRITE against its target client
+ * @hdr: header of the WRITE about to be sent
+ *
+ * Charge the WRITE's byte count to the client it is sent to: the pNFS
+ * data server if there is one, otherwise the MDS. O_DIRECT writes are
+ * not charged, and neither is a header that has been charged already.
+ */
+void nfs_write_congestion_charge(struct nfs_pgio_header *hdr)
{
- struct nfs_client *clp = NFS_SERVER(folio->mapping->host)->nfs_client;
- struct nfs_write_congestion *wcong = &clp->cl_wcong;
- long writeback;
+ struct nfs_write_congestion *wcong;
+ struct nfs_client *clp;
+ long inflight, limit;
- folio_start_writeback(folio);
- writeback = atomic_long_inc_return(&wcong->inflight);
- if (writeback > NFS_CONGESTION_ON_THRESH &&
+ if (!(hdr->rw_mode & FMODE_WRITE) || hdr->dreq || hdr->wcong_clp ||
+ !hdr->args.count)
+ return;
+
+ clp = hdr->ds_clp ?: NFS_SERVER(hdr->inode)->nfs_client;
+ refcount_inc(&clp->cl_count);
+ hdr->wcong_clp = clp;
+ hdr->wcong_bytes = hdr->args.count;
+
+ wcong = &clp->cl_wcong;
+ inflight = atomic_long_add_return(hdr->wcong_bytes, &wcong->inflight);
+ limit = nfs_congestion_on_thresh();
+ if (inflight > limit &&
!test_and_set_bit(NFS_WCONG_CONGESTED, &wcong->flags))
- trace_nfs_write_congestion_on(clp, writeback,
- NFS_CONGESTION_ON_THRESH);
+ trace_nfs_write_congestion_on(clp, inflight, limit);
}
-static void nfs_folio_end_writeback(struct folio *folio)
+/**
+ * nfs_write_congestion_uncharge - release the charge taken for a WRITE
+ * @hdr: header being freed
+ */
+void nfs_write_congestion_uncharge(struct nfs_pgio_header *hdr)
{
- struct nfs_client *clp = NFS_SERVER(folio->mapping->host)->nfs_client;
- struct nfs_write_congestion *wcong = &clp->cl_wcong;
- long writeback;
+ struct nfs_client *clp = hdr->wcong_clp;
+ struct nfs_write_congestion *wcong;
+ long inflight, limit;
- folio_end_writeback_no_dropbehind(folio);
- writeback = atomic_long_dec_return(&wcong->inflight);
- if (writeback < NFS_CONGESTION_OFF_THRESH &&
+ if (!clp)
+ return;
+ hdr->wcong_clp = NULL;
+
+ wcong = &clp->cl_wcong;
+ inflight = atomic_long_sub_return(hdr->wcong_bytes, &wcong->inflight);
+ limit = nfs_congestion_off_thresh();
+ if (inflight <= limit &&
test_and_clear_bit(NFS_WCONG_CONGESTED, &wcong->flags)) {
- trace_nfs_write_congestion_off(clp, writeback,
- NFS_CONGESTION_OFF_THRESH);
+ trace_nfs_write_congestion_off(clp, inflight, limit);
wake_up_all(&wcong->wait);
}
+ nfs_put_client(clp);
}
static void nfs_page_end_writeback(struct nfs_page *req)
{
if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
nfs_unlock_request(req);
- nfs_folio_end_writeback(nfs_page_to_folio(req));
+ folio_end_writeback_no_dropbehind(nfs_page_to_folio(req));
} else
nfs_unlock_request(req);
}
@@ -618,7 +662,7 @@ static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc,
return PTR_ERR(req);
trace_nfs_do_writepage(req);
- nfs_folio_set_writeback(folio);
+ folio_start_writeback(folio);
WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
/* If there is a fatal error that covers this write, just exit */
@@ -686,7 +730,7 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
/* Wait with writeback until write congestion eases */
if (wbc->sync_mode == WB_SYNC_NONE && nfs_write_congested(clp)) {
trace_nfs_write_congestion_wait(clp, nfs_write_inflight(clp),
- NFS_CONGESTION_OFF_THRESH);
+ nfs_congestion_off_thresh());
err = wait_event_killable(clp->cl_wcong.wait,
!nfs_write_congested(clp));
if (err)
diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h
index c46604f2f4f2..0fc5ebc5433d 100644
--- a/include/linux/nfs_fs_sb.h
+++ b/include/linux/nfs_fs_sb.h
@@ -28,7 +28,7 @@ struct nfs41_impl_id;
* a client, and all users of a pNFS data server, share one budget.
*/
struct nfs_write_congestion {
- atomic_long_t inflight; /* folios under writeback */
+ atomic_long_t inflight; /* bytes in WRITE RPCs */
unsigned long flags;
#define NFS_WCONG_CONGESTED 0 /* writers should wait */
wait_queue_head_t wait; /* wait until congestion eases */
diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
index c0e29b4dfa62..4acf649aa122 100644
--- a/include/linux/nfs_xdr.h
+++ b/include/linux/nfs_xdr.h
@@ -1694,6 +1694,8 @@ struct nfs_pgio_header {
u32 ds_commit_idx; /* ds index if ds_clp is set */
u32 pgio_mirror_idx;/* mirror index in pgio layer */
struct nfs4_deviceid_node *ds_dev; /* device node ref held across the I/O */
+ struct nfs_client *wcong_clp; /* client charged for this WRITE */
+ unsigned int wcong_bytes; /* bytes charged to wcong_clp */
};
struct nfs_mds_commit_info {
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 4/6] NFS: throttle writeback in the writepages loop, for all sync modes
2026-12-04 13:14 [PATCH 0/6] NFS: rework write congestion control Jeff Layton
` (2 preceding siblings ...)
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 ` 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
5 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
nfs_writepages() checks congestion once, on entry, and only for
WB_SYNC_NONE:
- The WB_SYNC_NONE limit dates from when the VM skipped a congested
bdi (commit 89a09141df6a ("[PATCH] nfs: fix congestion control")),
which only non-integrity writeback may do. Commit 2f1f31042ef0 ("nfs:
Block on write congestion") made the skip a wait but kept the limit.
Waiting doesn't break integrity, so fsync/close/sync need not be
exempt.
- One call with nr_to_write = LONG_MAX (filemap_fdatawrite(),
filemap_flush()) passes the check once and is then unbounded.
- It checks the MDS; with pNFS the DS isn't known yet.
The previous patch counts only sent WRITEs, so a writer can wait
inside the loop without its own queued requests holding the count up:
- New descriptor flag pg_throttle. nfs_pgheader_init() points the
header at the descriptor's pg_congested_clp; charging a WRITE to a
congested client stores the client there, with a reference. Only the
first is kept: one folio can need several RPCs (large folio,
mirrors, striping). The pointer is used only during submission,
while the descriptor lives, and is cleared at charge time; the header
may be freed once the WRITE starts.
- After each folio, if a WRITE hit a congested client, nfs_writepages()
sends what the descriptor still holds, then waits killably on that
client's waitqueue with nothing held. Those requests are locked with
folios under writeback, so without the send, callers of
folio_wait_writeback() or nfs_lock_and_join_requests() would wait out
the congestion too. Cost: one short RPC per congestion event.
- A fatal signal is passed through writeback_iter() so it releases its
folio batch; no further waits.
- No wait after the final nfs_pageio_complete().
- Entry check removed. Overshoot is at most one descriptor per mirror
(wsize or layout segment boundary), for any pNFS DS.
Resend descriptors (MDS fallback, nfs_pageio_resend()), O_DIRECT and
nfs_writepage_locked() don't set pg_throttle, so nothing waits in
rpciod or nfsiod.
WB_SYNC_ALL callers now wait on other WRITEs to the same client. Safe
because:
- The state manager and callback thread, which completions may need,
do no writeback.
- Locks these callers hold (i_rwsem in setattr, rename and copy/clone
before nfs_wb_all()/nfs_sync_inode()) are per-inode and not needed by
completion or recovery.
- No per-client lock is held across the wait, and these callers
already relied on recovery for their own WRITEs.
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/nfs/pagelist.c | 4 +++
fs/nfs/write.c | 79 ++++++++++++++++++++++++++++++++++++++++--------
include/linux/nfs_page.h | 2 ++
include/linux/nfs_xdr.h | 1 +
4 files changed, 74 insertions(+), 12 deletions(-)
diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c
index 0e06a9575e68..414c73b6bd6f 100644
--- a/fs/nfs/pagelist.c
+++ b/fs/nfs/pagelist.c
@@ -113,6 +113,8 @@ void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
hdr->completion_ops->init_hdr(hdr);
hdr->pgio_mirror_idx = desc->pg_mirror_idx;
+ /* Only valid until the WRITE is sent; see nfs_write_congestion_charge() */
+ hdr->wcong_feedback = desc->pg_throttle ? &desc->pg_congested_clp : NULL;
}
EXPORT_SYMBOL_GPL(nfs_pgheader_init);
@@ -876,6 +878,8 @@ void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
int io_flags)
{
desc->pg_moreio = 0;
+ desc->pg_throttle = 0;
+ desc->pg_congested_clp = NULL;
desc->pg_inode = inode;
desc->pg_ops = pg_ops;
desc->pg_completion_ops = compl_ops;
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 8c76e0aa49fb..62b5ab371894 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -318,13 +318,21 @@ static long nfs_write_inflight(struct nfs_client *clp)
* Charge the WRITE's byte count to the client it is sent to: the pNFS
* data server if there is one, otherwise the MDS. O_DIRECT writes are
* not charged, and neither is a header that has been charged already.
+ *
+ * If the header came from a descriptor that asked for congestion
+ * feedback (pg_throttle) and the target client is congested, record the
+ * client in the descriptor, unless an earlier WRITE already did. The
+ * descriptor's slot is only touched here, synchronously during
+ * submission, while the descriptor is known to be alive.
*/
void nfs_write_congestion_charge(struct nfs_pgio_header *hdr)
{
+ struct nfs_client **feedback = hdr->wcong_feedback;
struct nfs_write_congestion *wcong;
struct nfs_client *clp;
long inflight, limit;
+ hdr->wcong_feedback = NULL;
if (!(hdr->rw_mode & FMODE_WRITE) || hdr->dreq || hdr->wcong_clp ||
!hdr->args.count)
return;
@@ -340,6 +348,47 @@ void nfs_write_congestion_charge(struct nfs_pgio_header *hdr)
if (inflight > limit &&
!test_and_set_bit(NFS_WCONG_CONGESTED, &wcong->flags))
trace_nfs_write_congestion_on(clp, inflight, limit);
+
+ if (feedback && !*feedback && nfs_write_congested(clp)) {
+ refcount_inc(&clp->cl_count);
+ *feedback = clp;
+ }
+}
+
+/* Drop the descriptor's reference to a congested client, if any */
+static void nfs_pageio_clear_congested(struct nfs_pageio_descriptor *pgio)
+{
+ struct nfs_client *clp = pgio->pg_congested_clp;
+
+ if (clp) {
+ pgio->pg_congested_clp = NULL;
+ nfs_put_client(clp);
+ }
+}
+
+/*
+ * A WRITE sent from @pgio went to a congested client. Send what the
+ * descriptor still holds first: those requests are locked and their
+ * folios are under writeback, so anyone waiting on them would otherwise
+ * also wait out the congestion. Then wait for the congestion to clear.
+ * Nothing is held across the wait.
+ */
+static int nfs_writepages_throttle(struct nfs_pageio_descriptor *pgio)
+{
+ struct nfs_client *clp;
+ int ret;
+
+ pgio->pg_moreio = 1;
+ nfs_pageio_complete(pgio);
+
+ clp = pgio->pg_congested_clp;
+ pgio->pg_congested_clp = NULL;
+ trace_nfs_write_congestion_wait(clp, nfs_write_inflight(clp),
+ nfs_congestion_off_thresh());
+ ret = wait_event_killable(clp->cl_wcong.wait,
+ !nfs_write_congested(clp));
+ nfs_put_client(clp);
+ return ret;
}
/**
@@ -721,22 +770,12 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
struct nfs_pageio_descriptor pgio;
struct nfs_io_completion *ioc = NULL;
unsigned int mntflags = NFS_SERVER(inode)->flags;
- struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
+ bool killed = false;
int priority = 0;
int err;
trace_nfs_writepages(inode, wbc->range_start, wbc->range_end - wbc->range_start);
- /* Wait with writeback until write congestion eases */
- if (wbc->sync_mode == WB_SYNC_NONE && nfs_write_congested(clp)) {
- trace_nfs_write_congestion_wait(clp, nfs_write_inflight(clp),
- nfs_congestion_off_thresh());
- err = wait_event_killable(clp->cl_wcong.wait,
- !nfs_write_congested(clp));
- if (err)
- goto out_err;
- }
-
nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
@@ -754,12 +793,29 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
nfs_pageio_init_write(&pgio, inode, priority, false,
&nfs_async_write_completion_ops);
pgio.pg_io_completion = ioc;
+ pgio.pg_throttle = 1;
while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
err = nfs_do_writepage(folio, wbc, &pgio);
folio_unlock(folio);
+ if (!pgio.pg_congested_clp)
+ continue;
+ /*
+ * Throttle before sending more. On a fatal signal,
+ * stop waiting and let writeback_iter() see the
+ * error so that it releases its folio batch.
+ */
+ if (err || killed) {
+ nfs_pageio_clear_congested(&pgio);
+ continue;
+ }
+ err = nfs_writepages_throttle(&pgio);
+ if (err)
+ killed = true;
}
pgio.pg_error = 0;
nfs_pageio_complete(&pgio);
+ /* No point waiting after the last send */
+ nfs_pageio_clear_congested(&pgio);
if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
break;
} while (err < 0 && !nfs_error_is_fatal(err));
@@ -767,7 +823,6 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
if (err > 0)
err = 0;
-out_err:
trace_nfs_writepages_done(inode, wbc->range_start, wbc->range_end - wbc->range_start, err);
return err;
}
diff --git a/include/linux/nfs_page.h b/include/linux/nfs_page.h
index c38e4b380be5..a0b4e70a5d2b 100644
--- a/include/linux/nfs_page.h
+++ b/include/linux/nfs_page.h
@@ -120,6 +120,8 @@ struct nfs_pageio_descriptor {
u32 pg_mirror_idx; /* current mirror */
unsigned short pg_maxretrans;
unsigned char pg_moreio : 1;
+ unsigned char pg_throttle : 1; /* report write congestion */
+ struct nfs_client *pg_congested_clp; /* congested target, ref held */
};
/* arbitrarily selected limit to number of mirrors */
diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
index 4acf649aa122..e000b6ad569b 100644
--- a/include/linux/nfs_xdr.h
+++ b/include/linux/nfs_xdr.h
@@ -1696,6 +1696,7 @@ struct nfs_pgio_header {
struct nfs4_deviceid_node *ds_dev; /* device node ref held across the I/O */
struct nfs_client *wcong_clp; /* client charged for this WRITE */
unsigned int wcong_bytes; /* bytes charged to wcong_clp */
+ struct nfs_client **wcong_feedback; /* desc->pg_congested_clp */
};
struct nfs_mds_commit_info {
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 5/6] NFS: document write congestion control and nfs_congestion_kb
2026-12-04 13:14 [PATCH 0/6] NFS: rework write congestion control Jeff Layton
` (3 preceding siblings ...)
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 ` Jeff Layton
2026-12-04 13:14 ` [PATCH 6/6] NFS: add nfs_flush_nowait() Jeff Layton
5 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
nfs_congestion_kb has never been documented, and the previous patches
changed what it means. It used to bound the number of pages under
writeback per mount, which made it a bound on memory pinned by NFS
writeback. It now bounds the bytes in WRITE RPCs outstanding to each
nfs_client, and each pNFS data server has its own nfs_client. With
pNFS the total in flight can reach (number of data servers) times the
limit, so the sysctl now sets a per-server queue depth; the VM's dirty
limits still bound the total.
Describe the limit in the NFS client admin guide: what is counted and
what isn't, which mounts share a limit, how it applies to pNFS data
servers, and the limits of per-data-server isolation when background
writeback for a mount runs in a single flusher thread.
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
Documentation/admin-guide/nfs/nfs-client.rst | 42 ++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/Documentation/admin-guide/nfs/nfs-client.rst b/Documentation/admin-guide/nfs/nfs-client.rst
index 36760685dd34..15dada7c454c 100644
--- a/Documentation/admin-guide/nfs/nfs-client.rst
+++ b/Documentation/admin-guide/nfs/nfs-client.rst
@@ -60,6 +60,48 @@ containers unless it is overridden by a value written to
namespace of the process which writes.
+Write congestion control
+========================
+
+The NFS client limits how much buffered write data it has in flight to a
+server. The limit is set in /proc/sys/fs/nfs/nfs_congestion_kb. By
+default it scales with the square root of system memory, capped at
+256 MB.
+
+The limit applies to each NFS client instance separately, and counts the
+bytes in WRITE requests that have been sent to the server and not yet
+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
+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.
+
+All mounts of one server that use the same NFS version and minor
+version, transport protocol, network namespace and transport security
+policy share a client instance, and therefore one limit. Mounts that
+differ in any of these get separate instances and separate limits.
+
+With pNFS, each data server has its own client instance and its own
+limit, separate from the metadata server's, and a writer waits only when
+its own WRITE goes to a congested server. The data in flight to all
+servers together can therefore reach the limit times the number of
+servers: nfs_congestion_kb bounds the queue to each server, not the
+total memory under writeback, which is still bounded by the VM's dirty
+limits. A server that acts as both metadata server and data server for
+a client gets two limits, because a client instance is never shared
+between the two roles. Writes through the block and SCSI layouts go
+directly to block devices and are not counted.
+
+Background writeback for a mount runs in one flusher thread, and the
+files layout stripes the WRITEs for a file across its data servers in
+turn. A slow data server can therefore still stall background writeback
+for the whole mount. Data servers are isolated from each other only for
+independent writers, such as fsync() of separate files.
+
+
The DNS resolver
================
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 6/6] NFS: add nfs_flush_nowait()
2026-12-04 13:14 [PATCH 0/6] NFS: rework write congestion control Jeff Layton
` (4 preceding siblings ...)
2026-12-04 13:14 ` [PATCH 5/6] NFS: document write congestion control and nfs_congestion_kb Jeff Layton
@ 2026-12-04 13:14 ` Jeff Layton
2026-12-04 17:27 ` Jeff Layton
5 siblings, 1 reply; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 13:14 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, Jeff Layton, linux-nfs, linux-doc
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);
+ 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)
{
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 6/6] NFS: add nfs_flush_nowait()
2026-12-04 13:14 ` [PATCH 6/6] NFS: add nfs_flush_nowait() Jeff Layton
@ 2026-12-04 17:27 ` Jeff Layton
0 siblings, 0 replies; 8+ messages in thread
From: Jeff Layton @ 2026-12-04 17:27 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Benjamin Coddington,
Jonathan Corbet, Shuah Khan, Randy Dunlap
Cc: Jan Kara, linux-nfs, linux-doc
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>
^ permalink raw reply [flat|nested] 8+ messages in thread