Linux network filesystem support library
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, David Howells <dhowells@redhat.com>,
	Paulo Alcantara <pc@manguebit.org>,
	netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org,
	Christian Brauner <brauner@kernel.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH 7.0 261/461] netfs: Fix potential deadlock in write-through mode
Date: Thu, 28 May 2026 21:46:30 +0200	[thread overview]
Message-ID: <20260528194654.715365977@linuxfoundation.org> (raw)
In-Reply-To: <20260528194646.819809818@linuxfoundation.org>

7.0-stable review patch.  If anyone has any objections, please let me know.

------------------

From: David Howells <dhowells@redhat.com>

[ Upstream commit b6a4ae1634b3ad2aaa05222e53d36da532852faf ]

Fix netfs_advance_writethrough() to always unlock the supplied folio and to
mark it dirty if it isn't yet written to the end.  Unfortunately, it can't
be marked for writeback until the folio is done with as that may cause a
deadlock against mmapped reads and writes.

Even though it has been marked dirty, premature writeback can't occur as
the caller is holding both inode->i_rwsem (which will prevent concurrent
truncation, fallocation, DIO and other writes) and ictx->wb_lock (which
will cause flushing to wait and writeback to skip or wait).

Note that this may be easier to deal with once the queuing of folios is
split from the generation of subrequests.

Fixes: 288ace2f57c9 ("netfs: New writeback implementation")
Closes: https://sashiko.dev/#/patchset/20260427154639.180684-1-dhowells%40redhat.com
Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://patch.msgid.link/20260512123404.719402-15-dhowells@redhat.com
cc: Paulo Alcantara <pc@manguebit.org>
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 fs/netfs/write_issue.c | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/fs/netfs/write_issue.c b/fs/netfs/write_issue.c
index b0e9690bb90ce..03961622996be 100644
--- a/fs/netfs/write_issue.c
+++ b/fs/netfs/write_issue.c
@@ -414,12 +414,7 @@ static int netfs_write_folio(struct netfs_io_request *wreq,
 	if (streamw)
 		netfs_issue_write(wreq, cache);
 
-	/* Flip the page to the writeback state and unlock.  If we're called
-	 * from write-through, then the page has already been put into the wb
-	 * state.
-	 */
-	if (wreq->origin == NETFS_WRITEBACK)
-		folio_start_writeback(folio);
+	folio_start_writeback(folio);
 	folio_unlock(folio);
 
 	if (fgroup == NETFS_FOLIO_COPY_TO_CACHE) {
@@ -647,29 +642,41 @@ int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_c
 			       struct folio *folio, size_t copied, bool to_page_end,
 			       struct folio **writethrough_cache)
 {
+	int ret;
+
 	_enter("R=%x ic=%zu ws=%u cp=%zu tp=%u",
 	       wreq->debug_id, wreq->buffer.iter.count, wreq->wsize, copied, to_page_end);
 
-	if (!*writethrough_cache) {
-		if (folio_test_dirty(folio))
-			/* Sigh.  mmap. */
-			folio_clear_dirty_for_io(folio);
+	/* The folio is locked. */
 
+	if (*writethrough_cache != folio) {
+		if (*writethrough_cache) {
+			/* Did the folio get moved? */
+			folio_put(*writethrough_cache);
+			*writethrough_cache = NULL;
+		}
 		/* We can make multiple writes to the folio... */
-		folio_start_writeback(folio);
 		if (wreq->len == 0)
 			trace_netfs_folio(folio, netfs_folio_trace_wthru);
 		else
 			trace_netfs_folio(folio, netfs_folio_trace_wthru_plus);
 		*writethrough_cache = folio;
+		folio_get(folio);
 	}
 
 	wreq->len += copied;
-	if (!to_page_end)
+
+	if (!to_page_end) {
+		folio_mark_dirty(folio);
+		folio_unlock(folio);
 		return 0;
+	}
 
+	ret = netfs_write_folio(wreq, wbc, folio);
+	folio_put(*writethrough_cache);
 	*writethrough_cache = NULL;
-	return netfs_write_folio(wreq, wbc, folio);
+	wreq->submitted = wreq->len;
+	return ret;
 }
 
 /*
@@ -683,8 +690,12 @@ ssize_t netfs_end_writethrough(struct netfs_io_request *wreq, struct writeback_c
 
 	_enter("R=%x", wreq->debug_id);
 
-	if (writethrough_cache)
+	if (writethrough_cache) {
+		folio_lock(writethrough_cache);
 		netfs_write_folio(wreq, wbc, writethrough_cache);
+		folio_put(writethrough_cache);
+		wreq->submitted = wreq->len;
+	}
 
 	netfs_end_issue_write(wreq);
 
-- 
2.53.0




  parent reply	other threads:[~2026-05-28 20:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260528194646.819809818@linuxfoundation.org>
2026-05-28 19:46 ` [PATCH 7.0 250/461] netfs: Fix cancellation of a DIO and single read subrequests Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 251/461] netfs: Fix missing locking around retry adding new subreqs Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 252/461] netfs: Fix missing barriers when accessing stream->subrequests locklessly Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 253/461] netfs: Fix netfs_read_to_pagecache() to pause on subreq failure Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 254/461] netfs: Fix potential for tearing in ->remote_i_size and ->zero_point Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 255/461] netfs: Fix zeropoint update where i_size > remote_i_size Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 256/461] netfs: fix VM_BUG_ON_FOLIO() issue in netfs_write_begin() call Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 257/461] netfs: Fix overrun check in netfs_extract_user_iter() Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 258/461] netfs: Fix netfs_invalidate_folio() to clear dirty bit if all changes gone Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 259/461] netfs: Defer the emission of trace_netfs_folio() Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 260/461] netfs: Fix streaming write being overwritten Greg Kroah-Hartman
2026-05-28 19:46 ` Greg Kroah-Hartman [this message]
2026-05-28 19:46 ` [PATCH 7.0 262/461] netfs: Fix read-gaps to remove netfs_folio from filled folio Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 263/461] netfs: Fix write streaming disablement if fd open O_RDWR Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 264/461] netfs: Fix early put of sink folio in netfs_read_gaps() Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 265/461] netfs: Fix leak of request in netfs_write_begin() error handling Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 266/461] netfs: Fix potential UAF in netfs_unlock_abandoned_read_pages() Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 267/461] netfs: Fix partial invalidation of streaming-write folio Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 268/461] netfs: Fix folio->private handling in netfs_perform_write() Greg Kroah-Hartman
2026-05-28 19:46 ` [PATCH 7.0 269/461] netfs: Fix netfs_read_folio() to wait on writeback Greg Kroah-Hartman
2026-05-28 19:48 ` [PATCH 7.0 377/461] cifs: Fix undefined variables Greg Kroah-Hartman

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=20260528194654.715365977@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=brauner@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=netfs@lists.linux.dev \
    --cc=patches@lists.linux.dev \
    --cc=pc@manguebit.org \
    --cc=sashal@kernel.org \
    --cc=stable@vger.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