[PATCH v5 14/24] netfs: Fix potential deadlock in write-through mode

David Howells <[email protected]>
Newsgroups dev.linux.lists.netfs,org.kernel.vger.ceph-devel,org.kernel.vger.linux-cifs,org.kernel.vger.linux-fsdevel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Fix netfs_advance_writethrough() to always unlock the supplied folio.  It's
been marked for writeback and the caller is holding both inode->i_rwsem and
ictx->wb_lock, so there shouldn't be any danger of truncation, migration,
fallocation, flushing or other I/O interfering whilst the folio is
unlocked.

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 <[email protected]>
cc: Paulo Alcantara <[email protected]>
cc: [email protected]
cc: [email protected]
---
 fs/netfs/write_issue.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/fs/netfs/write_issue.c b/fs/netfs/write_issue.c
index b0e9690bb90c..0b07ebecb157 100644
--- a/fs/netfs/write_issue.c
+++ b/fs/netfs/write_issue.c
@@ -665,8 +665,10 @@ int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_c
 	}
 
 	wreq->len += copied;
-	if (!to_page_end)
+	if (!to_page_end) {
+		folio_unlock(folio);
 		return 0;
+	}
 
 	*writethrough_cache = NULL;
 	return netfs_write_folio(wreq, wbc, folio);
@@ -683,8 +685,10 @@ 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);
+	}
 
 	netfs_end_issue_write(wreq);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.