{"id":2229001,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2229001/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260427154639.180684-20-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":"<20260427154639.180684-20-dhowells@redhat.com>","date":"2026-04-27T15:46:34","name":"[v4,19/22] netfs: Fix folio->private handling in netfs_perform_write()","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"27e4a1da4aadce162f7a8fc031e8be34378e81e6","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/20260427154639.180684-20-dhowells@redhat.com/mbox/","series":[{"id":501682,"url":"http://patchwork.ozlabs.org/api/1.1/series/501682/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/list/?series=501682","date":"2026-04-27T15:46:15","name":"netfs: Miscellaneous fixes","version":4,"mbox":"http://patchwork.ozlabs.org/series/501682/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2229001/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2229001/checks/","tags":{},"headers":{"Return-Path":"\n <linux-cifs+bounces-11175-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=OJh7r26D;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c09:e001:a7::12fc:5321; helo=sto.lore.kernel.org;\n envelope-from=linux-cifs+bounces-11175-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=\"OJh7r26D\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=170.10.133.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 sto.lore.kernel.org (sto.lore.kernel.org\n [IPv6:2600:3c09:e001:a7::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 4g47Xn1cY2z1yHv\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 01:59:05 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sto.lore.kernel.org (Postfix) with ESMTP id 3093F301285B\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 27 Apr 2026 15:51:36 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id C174D35BDA4;\n\tMon, 27 Apr 2026 15:48:22 +0000 (UTC)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.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 F0CAD35B137\n\tfor <linux-cifs@vger.kernel.org>; Mon, 27 Apr 2026 15:48:20 +0000 (UTC)","from mx-prod-mc-01.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-672-kY-tnVtcNB66Q38Ojw__PQ-1; Mon,\n 27 Apr 2026 11:48:15 -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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id 2A56B195608F;\n\tMon, 27 Apr 2026 15:48:14 +0000 (UTC)","from warthog.procyon.org.com (unknown [10.44.32.126])\n\tby mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 3B4DF180045E;\n\tMon, 27 Apr 2026 15:48:11 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1777304902; cv=none;\n b=uotpr8x/AzDsWj26NMwZXBNK3gVpONWKM5Zg4RmeZEsIJ9zTDPB0qBQxohkapeg6PmmzD97Vabom8yh3MX9kRgCnWIcvAVTDIBBzmykolGuXsR9e9veoj+yqrg3Bb3nMZLpGz2G9zmoSFUKWL1ZfxtFNGIXe4SWV83GuzI7cgLQ=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1777304902; c=relaxed/simple;\n\tbh=XvI6FlmLo/S8fiCao0RLwRtkMMeM/s7ZEvhLOSP+1qU=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=Gw+0R/KIXUhmoVgNSVYlW/h0GgD8CD4C4MvJ0oK88HwkrM66zn+JGpJnsZfh3eOPBQmkk4vBP6GD9PjQ7SZw5FcVGRsaOhHvAWxmaqw07XX/a85RNcadTCEy7WFXAgpHFEklXVMy1ww7UpuvKQhZf4nh9tPfEEuvp6UG2kFirjM=","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=OJh7r26D; arc=none smtp.client-ip=170.10.133.124","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1777304900;\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=diqcvgX9yY2xKRLwPEtaVSpqSiejOkpwkFqrTvXL1WI=;\n\tb=OJh7r26DRtoJI2ok/khOWLS9jC1fOUIMknP1C7RnuBr0Am4SmHAwrr8pys6LwLZcfX09Ao\n\tSxa7aarZG7LpkJAysl4MVQejrXoMl+kPrA8fInn3v4s0sD/nhrgzJm0CvlScXHu2jn9gLS\n\t6esSgvX57R/d5qLC31H7cgd95icyK5c=","X-MC-Unique":"kY-tnVtcNB66Q38Ojw__PQ-1","X-Mimecast-MFC-AGG-ID":"kY-tnVtcNB66Q38Ojw__PQ_1777304894","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 v4 19/22] netfs: Fix folio->private handling in\n netfs_perform_write()","Date":"Mon, 27 Apr 2026 16:46:34 +0100","Message-ID":"<20260427154639.180684-20-dhowells@redhat.com>","In-Reply-To":"<20260427154639.180684-1-dhowells@redhat.com>","References":"<20260427154639.180684-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    | 109 ++++++++++++++++++++---------------\n include/trace/events/netfs.h |   1 +\n 2 files changed, 63 insertions(+), 47 deletions(-)","diff":"diff --git a/fs/netfs/buffered_write.c b/fs/netfs/buffered_write.c\nindex b782cf83fb57..20ce8d04d874 100644\n--- a/fs/netfs/buffered_write.c\n+++ b/fs/netfs/buffered_write.c\n@@ -12,24 +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-\n-\tif (unlikely(priv != netfs_group)) {\n-\t\tif (netfs_group && (!priv || priv == NETFS_FOLIO_COPY_TO_CACHE))\n-\t\t\tfolio_attach_private(folio, netfs_get_group(netfs_group));\n-\t\telse if (!netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)\n-\t\t\tfolio_detach_private(folio);\n-\t}\n-}\n-\n /*\n  * Grab a folio for writing and lock it.  Attempt to allocate as large a folio\n  * as possible to hold as much of the remaining length as possible in one go.\n@@ -157,6 +139,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 +195,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@@ -222,9 +206,8 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tcopied = copy_folio_from_iter_atomic(folio, offset, part, iter);\n \t\t\tif (unlikely(copied == 0))\n \t\t\t\tgoto copy_failed;\n-\t\t\tnetfs_set_group(folio, netfs_group);\n \t\t\ttrace = netfs_folio_is_uptodate;\n-\t\t\tgoto copied;\n+\t\t\tgoto copied_uptodate;\n \t\t}\n \n \t\t/* If the page is above the zero-point then we assume that the\n@@ -237,24 +220,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@@ -272,7 +253,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tfinfo->dirty_len += finfo->dirty_offset;\n \t\t\tif (finfo->dirty_len == flen) {\n \t\t\t\ttrace = netfs_whole_folio_modify_filled_efault;\n-\t\t\t\tgoto folio_now_filled;\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@@ -300,11 +281,11 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,\n \t\t\tcopied = copy_folio_from_iter_atomic(folio, offset, part, iter);\n \t\t\tif (unlikely(copied == 0))\n \t\t\t\tgoto copy_failed;\n-\t\t\tnetfs_set_group(folio, netfs_group);\n \t\t\ttrace = netfs_just_prefetch;\n-\t\t\tgoto copied;\n+\t\t\tgoto copied_uptodate;\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@@ -313,10 +294,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@@ -345,7 +324,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@@ -361,13 +340,36 @@ 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\tfolio_mark_uptodate(folio);\n-\t\tkfree(finfo);\n+\n+\tcopied_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+\n \tcopied:\n \t\ttrace_netfs_folio(folio, trace);\n \t\tflush_dcache_folio(folio);\n@@ -530,6 +532,7 @@ vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_gr\n \tstruct inode *inode = file_inode(file);\n \tstruct netfs_inode *ictx = netfs_inode(inode);\n \tvm_fault_t ret = VM_FAULT_NOPAGE;\n+\tvoid *priv;\n \tint err;\n \n \t_enter(\"%lx\", folio->index);\n@@ -572,7 +575,19 @@ vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_gr\n \t\ttrace_netfs_folio(folio, netfs_folio_trace_mkwrite_plus);\n \telse\n \t\ttrace_netfs_folio(folio, netfs_folio_trace_mkwrite);\n-\tnetfs_set_group(folio, netfs_group);\n+\n+\tpriv = folio_get_private(folio);\n+\tif (priv != netfs_group) {\n+\t\tif (!netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)\n+\t\t\tfolio_detach_private(folio);\n+\t\telse if (netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)\n+\t\t\tfolio_change_private(folio, netfs_get_group(netfs_group));\n+\t\telse if (netfs_group && !priv)\n+\t\t\tfolio_attach_private(folio, netfs_get_group(netfs_group));\n+\t\telse\n+\t\t\tWARN_ON_ONCE(1);\n+\t}\n+\n \tfile_update_time(file);\n \tset_bit(NETFS_ICTX_MODIFIED_ATTR, &ictx->flags);\n \tif (ictx->ops->post_modify)\ndiff --git a/include/trace/events/netfs.h b/include/trace/events/netfs.h\nindex aa9940ba307b..082cb03c6131 100644\n--- a/include/trace/events/netfs.h\n+++ b/include/trace/events/netfs.h\n@@ -181,6 +181,7 @@\n \tEM(netfs_whole_folio_modify_filled,\t\"mod-whole-f+\")\t\\\n \tEM(netfs_whole_folio_modify_filled_efault, \"mod-whole-f+!\") \\\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":["v4","19/22"]}