Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796390092; b=MNf6KmSitWaltSWXiNyGIrurfHyzajxZ6EzqEMP0XMOMr63Bz/z4wgbfv72MD2rd33cX BrlIgP9vjoOpkY1eHMxVMkgtfC4FyFks25e6jx8r70SEiAsgkmVp3vG8Y/EJOfD4xntXk z3l0AbawqUxdpwCQLYXj8QPIkBu4WI5Nr4hv7rITTBRt45yBHKgmEFYqvSNmCOh/ZO0UA +Go7sRNfrZqnVha1YZlxLKfGlT/N2C5MD4x+oKsAhdsa/8jA9Qn5u3xqPGpZ9vDiNCqCF BB+3pdVPis9SOBNEHg3D5mhUGVagh5SR/GvrqhA3y0+6ad7TC3YjniDUGANjZu1RJ9w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796390092; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=vOGjawtW8+niaiMOh6beoUX9fK3CL77fsgegUx5WQig=; b=WIPHUij00kSL2BQrMz40Mt6s8o9qhTA3tH3DkXl+z9FVRkYePeEm4Q8imDOPko2K8lBu erA4rORYcumIava2xtT3maAX3I38X/zrIZuRlDLM8jLlK1J5t+UND2/Dq8Usp1UrKLat0 K28p0DUKcg+wO4PT8UAzKZ7+9sMVt2x2D1tClDjpYuByiLo7ToLOUfOLyBas4i9pvA7u6 6Ps3503GcBRLZp6fwGsXC1OPmku5mkWCVTd3nPTgP1Y3XUu8GaOFmM7hGIbXIhCfPj2co CExjcVh9s6YoR6LQ6GWfk6kufVkI1hN6wsA8O67DsFy0fFl6u7xWqXHUVeU2m8I/nXQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Q2ZtQ//g"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F538606A322; Fri, 4 Dec 2026 13:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q2ZtQ//g Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DCD21F00933; Fri, 4 Dec 2026 13:14:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796390091; bh=vOGjawtW8+niaiMOh6beoUX9fK3CL77fsgegUx5WQig=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Q2ZtQ//gHADaxEov+6Q++nWG7VrdTU9Qpu6YoAj+PpMtKWXIAaz2UkJaAC/v1afPo c00VUY0H7N8rKUyOwB6tZT4lKcKsJAZGKh72/4IQvhIsvf9vSKp4nRnaBccP8ItjZ0 pa35Ekd2HUYlsIKOJMiZp0TY//7w35hne0L4lyxuJxY58gaap2HJ2q8yZa36PsrVkT 3yavZS7XvNbXJyK7Abw6IzPBhp9fnfRZpqf8+8SVZbeN5EsT0rDhf9g65DMIYj0ks5 /LSoxtu4iMCAKGDG+nHqzQPNMpOzANhi17DF6i9uQjpMBkioS/OxU/Rgoh5G5vwh+V tq9O5PxUQ2gRw== From: Jeff Layton Date: Fri, 04 Dec 2026 08:14:18 -0500 Subject: [PATCH 4/6] NFS: throttle writeback in the writepages loop, for all sync modes X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-nfs-write-congestion-v1-4-f06cdcd58192@kernel.org> References: <20261204-nfs-write-congestion-v1-0-f06cdcd58192@kernel.org> In-Reply-To: <20261204-nfs-write-congestion-v1-0-f06cdcd58192@kernel.org> To: Trond Myklebust , Anna Schumaker , Benjamin Coddington , Jonathan Corbet , Shuah Khan , Randy Dunlap Cc: Jan Kara , Jeff Layton , linux-nfs@vger.kernel.org, linux-doc@vger.kernel.org X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=9573; i=jlayton@kernel.org; h=from:subject:message-id; bh=4dqYe33azap+lDCXsyQAYC6lLh5PAnZxUr21lspr9wY=; b=owEBbQKS/ZANAwAKAQAOaEEZVoIVAcsmYgBrErzFv9TxaGSUE59rxsr5+urEY30yonR5esG/4 cJqY9asisqJAjMEAAEKAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCaxK8xQAKCRAADmhBGVaC Fd6MEACFbIXKm9oxPSa9qTnoopOhmpPnT3WSsium6acljvn6lnFrJlYZ2+fKdWl4vAdvzuWo8SZ JDa7f9HSgbJdUESM3N4f9U8g1skjLNvC+nFnVBdw8Y8eph1izBMTXc3AxlintFeGA5ZLaukvaHY e33yxf800h6mbttnHPJRaJ2JKrYk3iP/n0i2+PQZd2OFXIsUmufI1c8gHLU5jTkBzkX3U2aDJ8E SrlM3Zl/UoaSe+T9N6Xbm1faiXSQ3pu2A/giK6VDm3MTUWBL55L7vEsjt1Hw9qUFHR+uo5n0eoe F4+eblR3ya+5PNdJj3GQkg+KJM3frXqMtYYLiI9LYs6OkMuqsQCmgy/G16w2E9+4Ekl9lt/tUoG L0EIzW8G8MHPZ5fNcP1CLzoHbvSz/aFlb0b+9dTiLb6lNTeXKeHWnhwYug1HlDm7RuGARlRyXcs rDkZsM9w3lfEo5aRXCUCt9BxWRMp2I6ubjewU2xrEEoum+ZVEROdEVc0+HL0Mvt6ZXZE+O3noAk oVqqTB+ZuWZ7EPSEgZgrbZPAqASJEi4rHoSfK9YGC3JZ7Jr8XmUF2gOtOEeXDHAGO0VQj2VZYL4 1KswYWwrBqQsJqkSYFy2Sg0Lf50zqVtt6g4hUVGLgTUgA28BaR4Zgw27/XkohJWJsO08/GA/2/M OPSCZ+JvfzUTjvQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 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 --- 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