{"id":2228136,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2228136/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260425125426.3855807-15-dhowells@redhat.com/","project":{"id":12,"url":"http://patchwork.ozlabs.org/api/1.1/projects/12/?format=json","name":"Linux CIFS Client","link_name":"linux-cifs-client","list_id":"linux-cifs.vger.kernel.org","list_email":"linux-cifs@vger.kernel.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260425125426.3855807-15-dhowells@redhat.com>","date":"2026-04-25T12:54:18","name":"[v3,14/19] netfs: Fix folio->private handling in netfs_perform_write()","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"f32013861ce2c8ac155059c1db86765ee062dd67","submitter":{"id":59,"url":"http://patchwork.ozlabs.org/api/1.1/people/59/?format=json","name":"David Howells","email":"dhowells@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260425125426.3855807-15-dhowells@redhat.com/mbox/","series":[{"id":501437,"url":"http://patchwork.ozlabs.org/api/1.1/series/501437/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/list/?series=501437","date":"2026-04-25T12:54:04","name":"netfs: Miscellaneous fixes","version":3,"mbox":"http://patchwork.ozlabs.org/series/501437/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2228136/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2228136/checks/","tags":{},"headers":{"Return-Path":"\n <linux-cifs+bounces-11123-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-cifs@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=SMVJlzmo;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c04:e001:36c::12fc:5321; helo=tor.lore.kernel.org;\n envelope-from=linux-cifs+bounces-11123-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com\n header.b=\"SMVJlzmo\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=170.10.129.124","smtp.subspace.kernel.org;\n dmarc=pass (p=quarantine dis=none) header.from=redhat.com","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=redhat.com"],"Received":["from tor.lore.kernel.org (tor.lore.kernel.org\n [IPv6:2600:3c04:e001:36c::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g2qjb0Nn9z1xvV\n\tfor <incoming@patchwork.ozlabs.org>; Sat, 25 Apr 2026 23:02:11 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby tor.lore.kernel.org (Postfix) with ESMTP id 9376E307D4ED\n\tfor <incoming@patchwork.ozlabs.org>; Sat, 25 Apr 2026 12:56:39 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id DD7D435BDD5;\n\tSat, 25 Apr 2026 12:55:52 +0000 (UTC)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 4390B3976A9\n\tfor <linux-cifs@vger.kernel.org>; Sat, 25 Apr 2026 12:55:51 +0000 (UTC)","from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-503-6NAkiPYJNwigtP-mOOAp-A-1; Sat,\n 25 Apr 2026 08:55:43 -0400","from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n\t(No client certificate requested)\n\tby mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id 301DF195609F;\n\tSat, 25 Apr 2026 12:55:42 +0000 (UTC)","from warthog.procyon.org.com (unknown [10.44.48.17])\n\tby mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 3F52E1800351;\n\tSat, 25 Apr 2026 12:55:39 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1777121752; cv=none;\n b=UYpcwncGOjap3jcT+9j/2OU5Wt6Ue/DhBkbcVr0SEX4m4vVrVlOLcLVCU3LV/yNm+szcGIyQBhPG72qgNMA12mMRXI3RMY8c8iAP25j0kcQIR84os+6GwjnJ39F1HUyqXEpCV82Eaqfi4Z3Jc3/I3tHYpXvyDOIkem0E1DpNwuU=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1777121752; c=relaxed/simple;\n\tbh=DRDJmYHK2ygS9wWeSeTfVKgUxwyN0xYD1MJVVOt5lpk=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=dvmmgwXaBIoiQjn8ohs6S+SZ9I3/KLcVz+9SOeqajSclY2LeKjQ7LeHzSYbqbsyTpPukxrU1EXwFuKQ1Yf8JIyWF7paeus1/tv5LXMpkNQqxiLRUBkAnZus+GrS3tIshoLuwv+BUV67syeux1dB2Wrr4W1ZLQbrqp8rt/6DFaio=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=quarantine dis=none) header.from=redhat.com;\n spf=pass smtp.mailfrom=redhat.com;\n dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com\n header.b=SMVJlzmo; arc=none smtp.client-ip=170.10.129.124","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1777121750;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\t to:to:cc:cc:mime-version:mime-version:\n\t content-transfer-encoding:content-transfer-encoding:\n\t in-reply-to:in-reply-to:references:references;\n\tbh=9+HCNdbgI96Ai70MhVVTZqmH4nl/dl7bHmAA/USf8Fc=;\n\tb=SMVJlzmo074/67126zbcjKw8DL9NHq4KO+uWMyZ8bowV13NZk8muDY7ouFTsA9bm7jlOWH\n\tT1lUwXsrfmxX5xltRDvmk5YBvaDNZnHJQGZcOtDhJLnusFh6MRF3lUnTxy35CnWjTLD51u\n\tb2n5FcYJsvwxcNKdnvW3V1LlwIM039A=","X-MC-Unique":"6NAkiPYJNwigtP-mOOAp-A-1","X-Mimecast-MFC-AGG-ID":"6NAkiPYJNwigtP-mOOAp-A_1777121742","From":"David Howells <dhowells@redhat.com>","To":"Christian Brauner <christian@brauner.io>","Cc":"David Howells <dhowells@redhat.com>,\n\tPaulo Alcantara <pc@manguebit.org>,\n\tnetfs@lists.linux.dev,\n\tlinux-afs@lists.infradead.org,\n\tlinux-cifs@vger.kernel.org,\n\tceph-devel@vger.kernel.org,\n\tlinux-fsdevel@vger.kernel.org,\n\tlinux-kernel@vger.kernel.org,\n\tMatthew Wilcox <willy@infradead.org>","Subject":"[PATCH v3 14/19] netfs: Fix folio->private handling in\n netfs_perform_write()","Date":"Sat, 25 Apr 2026 13:54:18 +0100","Message-ID":"<20260425125426.3855807-15-dhowells@redhat.com>","In-Reply-To":"<20260425125426.3855807-1-dhowells@redhat.com>","References":"<20260425125426.3855807-1-dhowells@redhat.com>","Precedence":"bulk","X-Mailing-List":"linux-cifs@vger.kernel.org","List-Id":"<linux-cifs.vger.kernel.org>","List-Subscribe":"<mailto:linux-cifs+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-cifs+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.111"},"content":"Under some circumstances, netfs_perform_write() doesn't correctly\nmanipulate folio->private between NULL, NETFS_FOLIO_COPY_TO_CACHE, pointing\nto a group and pointing to a netfs_folio struct, leading to potential\nmultiple attachments of private data with associated folio ref leaks and\nalso leaks of netfs_folio structs or netfs_group refs.\n\nFix this by consolidating the place at which a folio is marked uptodate in\none place and having that look at what's attached to folio->private and\ndecide how to clean it up and then set the new group.  Also, the content\nshouldn't be flushed if group is NULL, even if a group is specified in the\nnetfs_group parameter, as that would be the case for a new folio.  A\nfilesystem should always specify netfs_group or never specify netfs_group.\n\nThe Sashiko auto-review tool noted that it was theoretically possible that\nthe fpos >= ctx->zero_point section might leak if it modified a streaming\nwrite folio.  This is unlikely, but with a network filesystem, third party\nchanges can happen.  It also pointed out that __netfs_set_group() would\nleak if called multiple times on the same folio from the \"whole folio\nmodify section\".\n\nFixes: 8f52de0077ba (\"netfs: Reduce number of conditional branches in netfs_perform_write()\")\nCloses: https://sashiko.dev/#/patchset/20260414082004.3756080-1-dhowells%40redhat.com\nSigned-off-by: David Howells <dhowells@redhat.com>\ncc: Paulo Alcantara <pc@manguebit.org>\ncc: Matthew Wilcox <willy@infradead.org>\ncc: netfs@lists.linux.dev\ncc: linux-fsdevel@vger.kernel.org\n---\n fs/netfs/buffered_write.c    | 77 +++++++++++++++++++++---------------\n include/trace/events/netfs.h |  2 +\n 2 files changed, 48 insertions(+), 31 deletions(-)","diff":"diff --git a/fs/netfs/buffered_write.c b/fs/netfs/buffered_write.c\nindex c7b49b38a710..0439a4c2e003 100644\n--- a/fs/netfs/buffered_write.c\n+++ b/fs/netfs/buffered_write.c\n@@ -12,12 +12,6 @@\n #include <linux/slab.h>\n #include \"internal.h\"\n \n-static void __netfs_set_group(struct folio *folio, struct netfs_group *netfs_group)\n-{\n-\tif (netfs_group)\n-\t\tfolio_attach_private(folio, netfs_get_group(netfs_group));\n-}\n-\n static void netfs_set_group(struct folio *folio, struct netfs_group *netfs_group)\n {\n \tvoid *priv = folio_get_private(folio);\n@@ -157,6 +151,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\tsize_t offset;\t/* Offset into pagecache folio */\n \t\tsize_t part;\t/* Bytes to write to folio */\n \t\tsize_t copied;\t/* Bytes copied from user */\n+\t\tvoid *priv;\n \n \t\toffset = pos & (max_chunk - 1);\n \t\tpart = min(max_chunk - offset, iov_iter_count(iter));\n@@ -212,8 +207,9 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\tfinfo = netfs_folio_info(folio);\n \t\tgroup = netfs_folio_group(folio);\n \n-\t\tif (unlikely(group != netfs_group) &&\n-\t\t    group != NETFS_FOLIO_COPY_TO_CACHE)\n+\t\tif (unlikely(group) &&\n+\t\t    group != NETFS_FOLIO_COPY_TO_CACHE &&\n+\t\t    group != netfs_group)\n \t\t\tgoto flush_content;\n \n \t\tif (folio_test_uptodate(folio)) {\n@@ -237,24 +233,22 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tif (unlikely(copied == 0))\n \t\t\t\tgoto copy_failed;\n \t\t\tfolio_zero_segment(folio, offset + copied, flen);\n-\t\t\t__netfs_set_group(folio, netfs_group);\n-\t\t\tfolio_mark_uptodate(folio);\n-\t\t\ttrace = netfs_modify_and_clear;\n-\t\t\tgoto copied;\n+\t\t\tif (finfo)\n+\t\t\t\ttrace = netfs_modify_and_clear_rm_finfo;\n+\t\t\telse\n+\t\t\t\ttrace = netfs_modify_and_clear;\n+\t\t\tgoto mark_uptodate;\n \t\t}\n \n \t\t/* See if we can write a whole folio in one go. */\n \t\tif (!maybe_trouble && offset == 0 && part >= flen) {\n \t\t\tcopied = copy_folio_from_iter_atomic(folio, offset, part, iter);\n \t\t\tif (likely(copied == part)) {\n-\t\t\t\tif (finfo) {\n+\t\t\t\tif (finfo)\n \t\t\t\t\ttrace = netfs_whole_folio_modify_filled;\n-\t\t\t\t\tgoto folio_now_filled;\n-\t\t\t\t}\n-\t\t\t\t__netfs_set_group(folio, netfs_group);\n-\t\t\t\tfolio_mark_uptodate(folio);\n-\t\t\t\ttrace = netfs_whole_folio_modify;\n-\t\t\t\tgoto copied;\n+\t\t\t\telse\n+\t\t\t\t\ttrace = netfs_whole_folio_modify;\n+\t\t\t\tgoto mark_uptodate;\n \t\t\t}\n \t\t\tif (copied == 0)\n \t\t\t\tgoto copy_failed;\n@@ -270,8 +264,10 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\t * accept the partial write.\n \t\t\t */\n \t\t\tfinfo->dirty_len += finfo->dirty_offset;\n-\t\t\tif (finfo->dirty_len == flen)\n-\t\t\t\tgoto folio_now_filled;\n+\t\t\tif (finfo->dirty_len == flen) {\n+\t\t\t\ttrace = netfs_whole_folio_modify_filled_efault;\n+\t\t\t\tgoto mark_uptodate;\n+\t\t\t}\n \t\t\tif (copied > finfo->dirty_len)\n \t\t\t\tfinfo->dirty_len = copied;\n \t\t\tfinfo->dirty_offset = 0;\n@@ -303,6 +299,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tgoto copied;\n \t\t}\n \n+\t\t/* Do a streaming write on a folio that has nothing in it yet. */\n \t\tif (!finfo) {\n \t\t\tret = -EIO;\n \t\t\tif (WARN_ON(folio_get_private(folio)))\n@@ -311,10 +308,8 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tif (unlikely(copied == 0))\n \t\t\t\tgoto copy_failed;\n \t\t\tif (offset == 0 && copied == flen) {\n-\t\t\t\t__netfs_set_group(folio, netfs_group);\n-\t\t\t\tfolio_mark_uptodate(folio);\n \t\t\t\ttrace = netfs_streaming_filled_page;\n-\t\t\t\tgoto copied;\n+\t\t\t\tgoto mark_uptodate;\n \t\t\t}\n \n \t\t\tfinfo = kzalloc_obj(*finfo);\n@@ -343,7 +338,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tfinfo->dirty_len += copied;\n \t\t\tif (finfo->dirty_offset == 0 && finfo->dirty_len == flen) {\n \t\t\t\ttrace = netfs_streaming_cont_filled_page;\n-\t\t\t\tgoto folio_now_filled;\n+\t\t\t\tgoto mark_uptodate;\n \t\t\t}\n \t\t\ttrace = netfs_streaming_write_cont;\n \t\t\tgoto copied;\n@@ -359,13 +354,33 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tgoto out;\n \t\tcontinue;\n \n-\tfolio_now_filled:\n-\t\tif (finfo->netfs_group)\n-\t\t\tfolio_change_private(folio, finfo->netfs_group);\n-\t\telse\n-\t\t\tfolio_detach_private(folio);\n+\t\t/* Mark a folio as being up to data when we've filled it\n+\t\t * completely.  If the folio has a group attached, then it must\n+\t\t * be the same group, otherwise we should have flushed it out\n+\t\t * above.  We have to get rid of the netfs_folio struct if\n+\t\t * there was one.\n+\t\t */\n+\tmark_uptodate:\n+\t\tpriv = folio_get_private(folio);\n+\t\tif (likely(priv == netfs_group)) {\n+\t\t\t/* Already set correctly; no change required. */\n+\t\t} else if (priv == NETFS_FOLIO_COPY_TO_CACHE) {\n+\t\t\tif (!netfs_group)\n+\t\t\t\tfolio_detach_private(folio);\n+\t\t\telse\n+\t\t\t\tfolio_change_private(folio, netfs_get_group(netfs_group));\n+\t\t} else if (!priv) {\n+\t\t\tfolio_attach_private(folio, netfs_get_group(netfs_group));\n+\t\t} else {\n+\t\t\tWARN_ON_ONCE(!finfo);\n+\t\t\tif (netfs_group)\n+\t\t\t\t/* finfo->netfs_group has a ref */\n+\t\t\t\tfolio_change_private(folio, netfs_group);\n+\t\t\telse\n+\t\t\t\tfolio_detach_private(folio);\n+\t\t\tkfree(finfo);\n+\t\t}\n \t\tfolio_mark_uptodate(folio);\n-\t\tkfree(finfo);\n \tcopied:\n \t\ttrace_netfs_folio(folio, trace);\n \t\tflush_dcache_folio(folio);\ndiff --git a/include/trace/events/netfs.h b/include/trace/events/netfs.h\nindex 67f6d56c94ce..1f5e3a5af08a 100644\n--- a/include/trace/events/netfs.h\n+++ b/include/trace/events/netfs.h\n@@ -179,7 +179,9 @@\n \tEM(netfs_whole_folio_modify,\t\t\"mod-whole-f\")\t\\\n \tEM(netfs_whole_folio_modify_efault,\t\"mod-whole-f!\")\t\\\n \tEM(netfs_whole_folio_modify_filled,\t\"mod-whole-f+\")\t\\\n+\tEM(netfs_whole_folio_modify_filled_efault, \"mod-whole-f+!\")\t\\\n \tEM(netfs_modify_and_clear,\t\t\"mod-n-clear\")\t\\\n+\tEM(netfs_modify_and_clear_rm_finfo,\t\"mod-n-clear+\")\t\\\n \tEM(netfs_streaming_write,\t\t\"mod-streamw\")\t\\\n \tEM(netfs_streaming_write_cont,\t\t\"mod-streamw+\")\t\\\n \tEM(netfs_flush_content,\t\t\t\"flush\")\t\\\n","prefixes":["v3","14/19"]}