From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796418319; b=tX5r/cS+ki6vGFwV10L83zmdqsA/nJfo7tacgFV85gKQQUPxiPGG8cQCHJWlq/DjF0Pm wn7lcqLAm/BOfOmlT0haHFimzaz0Yk+Li4INFHZksAbhyR54Ik6Dsj2+wz8uM16iLiJ0+ hi0WNrxfnA/zLbJqb6ZEHQHvBqvbYfOEpecSYeLYEGYWN/Fr5gqTU9PZhMVEySsAtzmUW QyucssStV2tkf7K8qzYaiNNa7mNBvQXIxTfHbFzErT8GQG8RuQA+7s68DMXd0IDBQX4q7 7DsYBH0NHqdASGHA4jLSzee1Xx0cmQMXJth/rCeRS/KP9v9VN1zY06izc7mt3eYpZVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796418319; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:Autocrypt:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=kHLwXCvEp7YRlUvQfRFECmwscx+lPpJu819oZBFeI3s=; b=dCydhjGuDsrfGZbj9NOusMjR8V+am6vdT+3fEPXDCWLwNfaXqyTgHI+YXyjzhz75xGoM DPdM+4Z0Xt2ii8LQlNhUvnlbJgmb/YDGegkODTVUolDvcg2mJaf7M7sQYkYW1JCfjE8hr RC1PKkC2mQMJoP7koGUydkPMQ2toGL1tsIgyDpBLmJaUg3ehyE/6zEOtz3CNmomWentZ3 xTqeb+0pPp+1kQJoHAECJbu/rMSK+AL2bCD3m6jGFsDUPYBt+kq31YtEHbQP3d0jmNqkU J2pqTRgxTfsXmzVZlQYux7I4jIf1e95ff5ThG13bWfS0EjIC3gPNJn7oDExVDvDDcqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=beBv2zCy; arc=none smtp.remote-ip=209.85.208.52 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AF55606A322 for ; Fri, 4 Dec 2026 21:05:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6AF55606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=beBv2zCy Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-6b167ffd132so1741700a12.1 for ; Fri, 04 Dec 2026 13:05:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1796418317; x=1797023117; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=kHLwXCvEp7YRlUvQfRFECmwscx+lPpJu819oZBFeI3s=; b=beBv2zCyF4kWK12fhxc0/C5Lq/pWTfhIgw80eMTPV3OWKqNcVXV9aHX5ZT8tgMvc/p wWN+LFJhC+I6eCPHIZs4JPanTF2wpxPr2OpGgOAWNmqXP/VfMk2gbdjT7edBEx9orKLa VacAwMU6mrvnph4mWEwUe64VV/oZsxhIEeFGSnbX4xAyj04n/F5tF951MY50bYfOOhg7 WIHQW8nOd13sNJLxekztO6Lnyyo76Wdb1VM61DNjyv/G+eqac8hOFBfD860wymsckfYc MIg3sZADluO7X3q45dm4uJqqaihZHB/Aw9xU46x1qhOuTHybw0Mobxd12xVbnzTeK1AS X4nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796418317; x=1797023117; h=content-transfer-encoding:content-type:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=kHLwXCvEp7YRlUvQfRFECmwscx+lPpJu819oZBFeI3s=; b=gNoCH2ebApkRkNWUhwaY6GkvyzMc5f6GYSuAuxycBuJaM/416wfxeW2/JnPzJTAplM a5+ugan7c9dqAeBfIjyG42OJJ4CT5AVFbNBcir8euScY72moz+imXFZcAcN99FGACr2o Y9AWdGIe+2hI5n2NO9dc/fIUidKwB2D1C5xrqpqgAG4xUWRevfZ4tTgTPXdr74Pm/h7L CeQFu6rENjCMThEwLn85hQcb9Pq7vqN/Mei3T+7bHWqiYYdlsdveVZfvIQhwXejRo+fx DEJia1/MVZnmxgQF5wWY4Jw0OyM7psA2k8Xt27e48ahLC7kvxgcbxYVeuE5YwvTGlD2X d91A== X-Forwarded-Encrypted: i=1; AL+hErFav2H6qzFyMRx0rD0aiaOlPQm+5HIxDo4GrCk7KIoMeTUhss+bqwNvLpB343o0yD5OjrkIXcnC5P6FmQ==@vger.kernel.org X-Gm-Message-State: AB4q+uHlW00gmWsmV9jbdW/utxs9DzmX1oD7cbz8pJbbwUVGPwd5+6TI vGwqjSjkUSt83MFtVkoyrPm4U/IgcZwE2LlrX09bst+dlz/oJh8jYqGGnuq7dzQjaHA= X-Gm-Gg: AZFdTZeBNDcGW8qa/WUjUmhFbrFopxLc90ISHLjBn2uh0jZlHEBblQGIQuJ32XKX6xh CV0KzdLL5aIDd/biMo1g7kv7+OkdteEeUuZy5m/eyx2fA5BYMFtXjP7P3gxbduEH0G6O6lZY7O0 ECwpsUzx6aKkeA6VHT2PIDFagWfoO6nsRcYNVmbAwzgSJUPjV7QYiPkBIzGiFp/cuemFV6psKYk n7vbpC7KPRtnCzz9h5pAYLhysUkyUXN0xoXX7Cbu4Ts+y+2jCLPCr5dZIYgyCxjjsLM8mPYrGMp nuCJ97XT5AmMmaUIOZhQuef31+rGjD5EEWeeTGEJPO0gggJr+31qZK3cmsYRF/LHse+UyZ8xGhE YMUPS9+yQdzpEB6lXQQ/wxxRrO9ffc7zLk5TTFtUjKKjHUo0MSF5v4AQUQZDrCkP8WMz0nKfxBq sXLe3uhEPq95l5IT5zwT0BRl5W64PRnvoRSVRZ1DN5KlAFdZpFD1+EK+CSARqpi3Z6T2TcOPYvd FSWWvgUIP16JJlSBdMr0CycxZd0kaVCqn0cKcppLTjL93j4WJMCmSa7Fv/FdcN2xpkoSL15Rq5A 0B4zJ5Bd5mfHia9GNEjJMcfWw/V7vZ25VUQ8gq3fLw== X-Received: by 2002:a05:6402:510e:b0:6bc:91a2:2d24 with SMTP id 4fb4d7f45d1cf-6bcc1643a4cmr2793730a12.11.1796418316769; Fri, 04 Dec 2026 13:05:16 -0800 (PST) Received: from [172.16.0.229] ([159.196.52.54]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b616aebc8csm303108a91.3.2026.12.04.13.05.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Dec 2026 13:05:15 -0800 (PST) Message-ID: Date: Sat, 5 Dec 2026 07:35:03 +1030 X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] btrfs: re-attach folio private when a released folio gets dirtied To: Pedro Falcato Cc: Brahmajit Das , dsterba@suse.com, linux-btrfs@vger.kernel.org, clm@fb.com, mason@kernel.org, david@kernel.org, jgg@ziepe.ca, jhubbard@nvidia.com, peterx@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Brahmajit Das , syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com, willy@infradead.org, jack@suse.cz, hch@lst.de References: <20261202041853.4184717-1-listout@listout.xyz> Content-Language: en-US From: Qu Wenruo Autocrypt: addr=wqu@suse.com; keydata= xsBNBFnVga8BCACyhFP3ExcTIuB73jDIBA/vSoYcTyysFQzPvez64TUSCv1SgXEByR7fju3o 8RfaWuHCnkkea5luuTZMqfgTXrun2dqNVYDNOV6RIVrc4YuG20yhC1epnV55fJCThqij0MRL 1NxPKXIlEdHvN0Kov3CtWA+R1iNN0RCeVun7rmOrrjBK573aWC5sgP7YsBOLK79H3tmUtz6b 9Imuj0ZyEsa76Xg9PX9Hn2myKj1hfWGS+5og9Va4hrwQC8ipjXik6NKR5GDV+hOZkktU81G5 gkQtGB9jOAYRs86QG/b7PtIlbd3+pppT0gaS+wvwMs8cuNG+Pu6KO1oC4jgdseFLu7NpABEB AAHNGFF1IFdlbnJ1byA8d3F1QHN1c2UuY29tPsLAlAQTAQgAPgIbAwULCQgHAgYVCAkKCwIE FgIDAQIeAQIXgBYhBC3fcuWlpVuonapC4cI9kfOhJf6oBQJq3ujyBQkUpLw5AAoJEMI9kfOh Jf6o8OIH/0mqW+TQM4m2x3hCsnK+oB4yJH1qnGvIhFF/Lhs48wI02sGh6/4aA0hkCDfd9LHQ +ci4FUepZ9y4VS01+oIExbg/8ytzGYTzLtb44jK2GBOBVSJkMiha6jp22XFt7WUkT/tvbreu WWdGsaJIRQaZChy7xsMI8lqQ2WdmluZJ4LT0wD3l/l6QU1ZseMGySebdY9sVOQEOJNV7Qj92 /KOAFNmeZshV/qDI8Iw1RTs6TvLqCcqmlZLCmJkYSVhT1RonWRren8xIRcFEgMe004IDnt7V qdQhk/nhTS9x9MBAhMRl0TTE1VKDLvVALsud3iZLrmmtYvwPApzGJk052f0m28vOwE0EWdWB rwEIAKpT62HgSzL9zwGe+WIUCMB+nOEjXAfvoUPUwk+YCEDcOdfkkM5FyBoJs8TCEuPXGXBO Cl5P5B8OYYnkHkGWutAVlUTV8KESOIm/KJIA7jJA+Ss9VhMjtePfgWexw+P8itFRSRrrwyUf E+0WcAevblUi45LjWWZgpg3A80tHP0iToOZ5MbdYk7YFBE29cDSleskfV80ZKxFv6koQocq0 vXzTfHvXNDELAuH7Ms/WJcdUzmPyBf3Oq6mKBBH8J6XZc9LjjNZwNbyvsHSrV5bgmu/THX2n g/3be+iqf6OggCiy3I1NSMJ5KtR0q2H2Nx2Vqb1fYPOID8McMV9Ll6rh8S8AEQEAAcLAfAQY AQgAJgIbDBYhBC3fcuWlpVuonapC4cI9kfOhJf6oBQJq3ukKBQkUzDspAAoJEMI9kfOhJf6o SNsH/1ysUWOKmzY0rGS3ZdS61QgqMGhSWD/EJ4UiPw+xO4l4+GtX0fOAqxqsmBKqje3vyhBa Z5o/1Y+gNiN+E6RBcSz4b1TAd78sQ2bvT5DWOvBjeeVxHdiuQMIn4S8xk5ZpySBw5ifjoiuL 0M33UVtJOzM8yfAeL+d/Rp4leJw72SutjkLyZTG/gJIp4pJGD/kFHn24OLL0IxEcO0gOekNZ os5e1R6jAJ2ILk7+MDnvU8wXHcCiVzuDhJLI1tih1uUIE50muHQJWbYuVBbdJeu8WlIDDssx UOCxLylNG3gDXV5MhLubPTOpp9OiFc/XC37nvkgEcrlmlOFBv4/NJCbECpo= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 在 2026/12/4 20:35, Pedro Falcato 写道: > +CC willy, jack, hch > > On Fri, Dec 04, 2026 at 07:13:08AM +1030, Qu Wenruo wrote: >> >> >> 在 2026/12/3 23:58, Pedro Falcato 写道: >> [...] >>>> When the read completes, the folio is re-dirtied without notifying the >>>> fs: >>>> >>>> bio_release_pages() >>>> folio_mark_dirty() >>>> btrfs_data_dirty_folio() >>>> filemap_dirty_folio() >>> >>> FWIW, this sounds like a variant of >>> https://lore.kernel.org/linux-mm/20261119-gup-dirty-what-v1-0-236d69f25c24@suse.de/. >>> (which I will respin soon) >> >> One assumption doesn't meet the current code. >> >>> - ->dirty_folio() relies on buffers/IFS/etc being attached to the >>> folios. It _cannot_ attach them itself, as dirty_folio() cannot >>> block. >> >> Iomap is already attaching it: >> >> iomap_dirty_folio() >> ifs_alloc(inode, folio, 0); >> gfp = GFP_NOFS | __GFP_NOFAIL; >> ifs = kzalloc_flex(..., gfp); >> >> Either it's already a bug, or we won't hit such folio without private for >> the non-sleeping routes. > > This is, strictly speaking, a bug. Per Documentation/filesystems/locking.rst: > > locking rules: > All except dirty_folio and free_folio may block > > I think currently, de facto, it's not very likely to be hit[1]. Mostly because, > for writenotify filesystems/VMAs, you'll always trip over ->page_mkwrite() > (marking the folio dirty), and cleaning will always write-protect it before > clearing the dirty flag off the folio. > > [1] I _think_ there are ways to hit it, if you go down funky folio_cancel_dirty() > paths. > >> >>> >>>> >>>> If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not >>>> touch any btrfs state, and the next writeback calls folio_invalidate() >>>> on a folio without private, triggering the above ASSERT() in the >>> >>> ? why is writeback calling folio_invalidate()? >> >> If a folio is beyond EOF, we have no need to write it, thus can go >> invalidating it directly. > > Why would you not rely on truncation itself to invalidate it? The truncation did try to invalidate the folio, by even removing the folio private. But the folio can not be removed from filemap because it's pinned by the direct IO. Did you mean we just skip the write, and let future truncation to invalidate it again? That also feels a little weird, yes the folio will eventually be truncated when evicting the inode, but still doesn't feel so correct. And this also means, we need to manually copy the invalidating handling to release reserved space, just not try to detach the private. I guess for now, following the not-so-correct iomap behavior is the most simple solution from the perspective of fs, but it still leave a pretty bad taste. > >> >> Or is there any better solution from the perspective of MM? > > My understanding is that the truncation logic should correctly handle > the semantics you want. > > As for the GUP problem, yes, this is yet another issue. It is known. > ->dirty_folio() implementations cannot assume the memory allocation will > succeed. And there's no possible fallback to "failure to dirty a folio". > > The real solution (in my series) is to simply not discard this at-will. > invalidate_folio() seems to be yet another variant of this issue. > > For what it's worth, I observed a similar issue in mm/truncate code. I don't > know what the correct approach would be. Keep the private around for GUP pins? > Reject the truncate altogether? > > (I don't think it's possible to do this last option; keeping the private is > also weird, and I don't see how you can possibly ditch it after the folio is > orphaned) I had a not-so-good idea, that will require invalidate_folio() callback to be split into two parts: - invalidate_folio_fs() The usual fs specific part. - invalidate_folio_detach() The private detaching part. The second part is only called after the folio is properly removed from the filemap. So this ensures all folios in the filemap still have their private attached. But again, I'm not an expert in MM. So this can just be a stupid idea and can conflict with other things like large folio splitting. Thanks, Qu