{"id":2225009,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2225009/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260420083705.1009074-8-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":"<20260420083705.1009074-8-dhowells@redhat.com>","date":"2026-04-20T08:36:58","name":"[07/11] netfs: Fix folio->private handling in netfs_perform_write()","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"d64e60201ece686dbf4a2a6309276f32cfa508c9","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/20260420083705.1009074-8-dhowells@redhat.com/mbox/","series":[{"id":500568,"url":"http://patchwork.ozlabs.org/api/1.1/series/500568/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/list/?series=500568","date":"2026-04-20T08:36:52","name":"netfs: Further miscellaneous fixes","version":1,"mbox":"http://patchwork.ozlabs.org/series/500568/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2225009/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2225009/checks/","tags":{},"headers":{"Return-Path":"\n <linux-cifs+bounces-10930-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=cFFRJWYP;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=linux-cifs+bounces-10930-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=\"cFFRJWYP\"","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 sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fzfCy5ZpLz1yGs\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 20 Apr 2026 18:43:58 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id 6CCC130707FC\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 20 Apr 2026 08:38:23 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 6FE6D38A706;\n\tMon, 20 Apr 2026 08:38:00 +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 C6E36288C08\n\tfor <linux-cifs@vger.kernel.org>; Mon, 20 Apr 2026 08:37:58 +0000 (UTC)","from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-404--ZYaFUxaOtG8rCXPe9qVwg-1; Mon,\n 20 Apr 2026 04:37:51 -0400","from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4])\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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id D09631800578;\n\tMon, 20 Apr 2026 08:37:49 +0000 (UTC)","from warthog.procyon.org.com (unknown [10.44.48.17])\n\tby mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 6AB753000C20;\n\tMon, 20 Apr 2026 08:37:46 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1776674280; cv=none;\n b=p39FLIa6eWLlCXOt9TnNRF82YBtLMmnQl/yutWSwLPNEPA4G31uw/XFqgQqknmxyJOvp56aFra/Tmz40hsGLmw6Qtbi1b/4hVW+Wic8xfdWG46OaIEpDBWZOhISOVqetrazJOg0KsQ9o5SbpnnLivBzL8wB1eBe5qeNJsStDcWw=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1776674280; c=relaxed/simple;\n\tbh=/KQmIgTJOCbv3hSfJO6jdQBY7LkyB0snaK0Rln8lc44=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=K+xPhwKmG3/Q/9rQcVBYeLFrzvamQGhIIpPT28S/iVmoIdJKuQESg/UYRbPmzF8vsFMOU9zhUyLrHN/+oVRKPnPhHhPTStrNfEWGEh5qzX/ej+YiSw4U5z+KqhRV2W/vs14FiFfH0957mnivqRRM3/HM96zGIlIXxvU1lI2HMDo=","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=cFFRJWYP; 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=1776674278;\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=9ftIxL41sEcAmEulwDX61ztIHIDAalQGRDwa5IsOQFo=;\n\tb=cFFRJWYPpCX542CjY+AOYwlimH1drmZsLKitYLdn2uA21zTbKkNSN7f6dNwl3O+i7wh8oB\n\tefjZLEZdqbdUaUreAa1Yh4FfPUZULrKRVkK6DG70B8F7DL8AHqGmrQP1wHBoWnlJE631ZS\n\tF3Dl3kUYDiGM1hcSotV6pbJNndBjai0=","X-MC-Unique":"-ZYaFUxaOtG8rCXPe9qVwg-1","X-Mimecast-MFC-AGG-ID":"-ZYaFUxaOtG8rCXPe9qVwg_1776674269","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 07/11] netfs: Fix folio->private handling in\n netfs_perform_write()","Date":"Mon, 20 Apr 2026 09:36:58 +0100","Message-ID":"<20260420083705.1009074-8-dhowells@redhat.com>","In-Reply-To":"<20260420083705.1009074-1-dhowells@redhat.com>","References":"<20260420083705.1009074-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.4"},"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 de82eb6ed473..0ca8e922790d 100644\n--- a/fs/netfs/buffered_write.c\n+++ b/fs/netfs/buffered_write.c\n@@ -13,12 +13,6 @@\n #include <linux/pagevec.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@@ -158,6 +152,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@@ -213,8 +208,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@@ -238,24 +234,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@@ -271,8 +265,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@@ -306,6 +302,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@@ -314,10 +311,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@@ -346,7 +341,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@@ -362,13 +357,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":["07/11"]}