From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796024941; b=ZvWrsJxZK5uYW5mDdtqgSU7v5nM8KaxSahprC/1lNtPL5UG3OQf8oypoPjE8NF/D2sLX +acEBUGLF6JINxoH4B/a5qQ9lCeYQd3HzljuwynUdp9n+vVQmQ8hkOOQCaSzoL0JZL9ha lnlayMfsmCk8cahY28XLaKcszy8HSd2lILBmcJYDNkcrc23FXhnCoJrAFSfBTTvEFZwvh 7Uj5vjliiXB4wOkHhb5x6IyOXQ9x4wPPDGBedaTJlV2eXOxmtzRBCgwaBhZz6YbECn8e3 EdktO6B+ryH+xArR2EXfPvwwIL5LhalRfT4TNHmpGkWBcQyvuhyxgBFnhHSP9KNt4qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796024941; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=xV17Ufs/m+kr+OUQjB0FhMS6QdbnXQoxcCpsFsk5BCI=; b=vY+immnuy+MrsK9LORb/Vex71E6TwbskhQDR2LtwkN3Qk1sD28ftSIeTt3o1Mz5ng2MW MWuWe/X3Omo58Um/d75yXA0UrTcUQZC94YFdKJL+YgIJBvWB+KFfF6mlLkQRsaaCIFf+9 e5P3MrltYr/47lKLYuohslgq8oKmHaVUZ82pz+iXgRveXEtWWSMvfcS/SG2Z1Nns9skSl aT9qfJP4FGhovbCRla0GoLhbgEp+uztNDjnqt5oZ1ro1pcWvvfxla8J5Ytcuo1Exx+va9 oo5gEOi7zsleCtJJwSWHNs7AJqE9LvVb/5UwCPFLNaq6ndtIjVlpxMUgJXNLY2bS30w== 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=NLzjA6Sg; arc=none smtp.remote-ip=209.85.128.47 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 948E7606A32D for ; Mon, 30 Nov 2026 07:49:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 948E7606A32D 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=NLzjA6Sg Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4a2d6d01ecbso53146015e9.0 for ; Sun, 29 Nov 2026 23:49:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1796024939; x=1796629739; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=xV17Ufs/m+kr+OUQjB0FhMS6QdbnXQoxcCpsFsk5BCI=; b=NLzjA6SgNBx0MX05SAqdpxX7mQbPLb+ykq4kjM170tt8NQKPApuH+qxizxWdFNPzTZ vvVP1VmkDalsU3i+tXbYNociu3AhrHrL4XJSjh9iPllS1pHe32pBHz0EywQxAtFpibfZ YEzW+r14eV4UfMYFkwCp/cJbUyf7RP25pLUSrnl3lwkTDvmPcwySAjUDNHGGrlZ3gytl OHAll2pi8v4YpQsgAnpBBUL15i7thalfWGEBeYpTM87+uoxNd3HeFZwe0ISQlaTafUCD abXr44U63L5NMY1k9/xTpabpkQ1KiFK7vSS5UYoVex2twaWmns3BPXflnDPI3JDxseTB lC5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796024939; x=1796629739; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xV17Ufs/m+kr+OUQjB0FhMS6QdbnXQoxcCpsFsk5BCI=; b=0R4TD/8GQkhMiknafjQ7y/GQIx9AyHSPdAlwc2goUf9MO/DeuZhA4zN30ws7CbuXUV 45HhWzv8EAIgsnD8rTarOh6cQpcepke6BVdozYg08WpWpaCtiUej8qvKVn/U+BX2fAZ1 ulZckpcJ6/lT9qQli9FBZ0VCcBiRf/3fMctv0jvNqJV3FUO+6CtT1JdZ/lp5UYO9OIG5 czE6mCA0/VIgeVz5QSD6+gKAw0/aIwhy0/VMeg9Jz1WzbzTVZR0EqbEUKJMHScaGaEdf bCYtibOIKBy3UEuKbG9HUKP4XN65kSJsQ4MU2s4j82joK/NOecT/M2NlR7447d9X53IN /PZA== X-Forwarded-Encrypted: i=1; AL+hErHfq6oh3fpzKxcMCwBOthkgcPYVs1wQN3ICB3yWFHTs9E25vjYK2WJ5gjCosWqfqzeRfRo4HjcnopwmKco=@vger.kernel.org X-Gm-Message-State: APuU4Z8yLr4+BXKbLVaKaUQkv6vaBd9LbJWY1DGRt5xsvVV5mdf39vMZ rPWxFRFEHgQRC+OkDEAW7TcMhGKJJHFdy8NDUexb26ywI5VMu5lFsm0s0tDif29WWg== X-Gm-Gg: AZFdTZcioTh2VyaSa6nYn2pElhX57onrf6w9evqbZ+kqxSi6HwysxVSloXcEVavcMOu ScLv2sM4haOVzgXOKX1EQEnXjhtpQLaK5UYCJPgW6tobt2nvTyK9qBKfferm9574+w51xBoUgQb LrQKAQiLU72KGXCgHoOvrI/zwLRFxlZfA/yryJEo85/MDqLkrn3SWuqpWlvfsonALUfONVV+8u+ MLGid0y8Gkxd6DGJf6pkHMaelcF/A1v+KSVDE+QbpLtP2C3bMKaNxGez0N2gk/1rYHOktgQ2fTU gYWIeQJjFKXbhC8gqrEMmgdFRaVWyP/jQ+THzKDCaHeFO1CL4gdZYki9u1vOfvgAJ0agBVF3lkR NPS7Zo0ahUBJA8BxR6bGkV3lCwwKIOCSQC559yHqRQms9yOQUXwvJ3wirUbBNwSyMqe9URxBI2+ fpCRPoSjXl31Nz/i6uqfdEAvWEfTmDDfbCMkC/CIiJplShYXg9yvKOqEdAJAUFHOCL9peXwXKFI so1VXejxzFSZao2HiLmgwY4WO5BFcy6LnH2rooiMjLsb4eyBFt2dlA5cOj7aGjAddqc6l9piM3j fRN2d1EC4ydUQsjwzG+/vONc+25LFFfR742+kPrnRYp+/qzkTPMIW22XjymyaaAXN+EixjUDwQC /ZmrmLYTzncsCQNZJqWOBoJwbmixK2MtKX3/F1F+ep/UiCjvU5i4e88Vf99FZ6Nn+ X-Received: by 2002:a05:600c:1d09:b0:4bb:6b59:aac3 with SMTP id 5b1f17b1804b1-4bb88816050mr682569945e9.29.1796024938823; Sun, 29 Nov 2026 23:48:58 -0800 (PST) Received: from autotest-wegao.qe.prg2.suse.org ([2a07:de40:b240:0:2ad6:ed42:2ad6:ed42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884865b1sm927393095e9.5.2026.11.29.23.48.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 23:48:58 -0800 (PST) Date: Mon, 30 Nov 2026 07:48:56 +0000 From: Wei Gao To: Dave Chinner Cc: Carlos Maiolino , linux-xfs@vger.kernel.org, "Darrick J . Wong" , Chandan Babu R , linux-kernel@vger.kernel.org Subject: Re: [PATCH] xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down Message-ID: References: <20261127100929.32124-1-wegao@suse.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Sun, Nov 29, 2026 at 09:12:38AM +1100, Dave Chinner wrote: > On Fri, Nov 27, 2026 at 10:09:29AM +0000, Wei Gao wrote: > > Commit f1204d96450f ("xfs: only free posteof blocks on first close") > > introduced XFS_EOFBLOCKS_RELEASED to avoid freeing speculative > > post-EOF preallocations across repeated open/write/close cycles. > > To reset this flag when a file is truncated down, xfs_file_release() > > relies on the XFS_ITRUNCATED flag: > > > > if (xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED)) { > > xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED); > > ... > > } > > > > However, XFS_ITRUNCATED is an ephemeral flush-on-close flag that is > > cleared as soon as dirty pages are written back to disk by background > > writeback (xfs_vm_writepages()) or explicit fsync (xfs_file_fsync()). > > Yes, ITRUNCATED is a flush-on-close mitigation for an unsafe > application overwrite anti-pattern, and fsync() before close negates > the need for mitigation (i.e. application is attempting to write > safely) and so removes ITRUNCATED as flush-on-close is no longer > needed to mitigate broken application file overwrite behaviour. > > If we trigger flush on close, the implication is that the > application is not going to come back to this file and write it > again. Hence in that case, it is better to also trigger post-eof > block removal whilst this inode is hot in the CPU cache, rather than > having to do it at inode reclaim time or via background GC on a cold > CPU cache. > > > If writeback occurs while the file is being written, or if fsync() > > is called before close(), XFS_ITRUNCATED is cleared. Consequently, > > when close() is subsequently called, xfs_iflags_test_and_clear() > > evaluates to false, XFS_EOFBLOCKS_RELEASED is never cleared, and > > xfs_free_eofblocks() is skipped. > > Which is perfectly fine, and behaviour as intended. There has been > an open/write/close behaviour observed, fsync has negated the need > for flush-on-close but it implies nothing about the already observed > open-write-close IO pattern that has triggered the > XFS_EOFBLOCKS_RELEASED file fragmentation mitigation. > > > If an open reference (such as a loop device) pins the inode in > > memory, the post-EOF blocks remain allocated permanently, causing > > tools and tests (such as LTP swapon04) to observe leaked blocks > > beyond EOF. > > This behaviour does not 'leak blocks beyond EOF'. XFS will reclaim > then periodically if the inode goes idle (background block GC runs > every 5 minutes), low free space triggers block GC, or when the > inode is evicted from the cache. > > There is no specification that requires 'st_blocks must exactly > match the amount of data the application wrote'. XFS has had this > "post eof block allocation observable by stat" behaviour for a long > time (i.e. 25+ years). > > Further, think about what reflink copies and deduplication imply for > st_blocks. Do you account shared blocks as allocated to that file or > not? That's not defined in any specification, either... > > Hence any test that assumes all filesystems will have the exact same > block count reported for a specific amount of data written by an > application is simply a broken test. > > > The issue can be easily and deterministically reproduced on XFS using > > fsync(): > > 1. Create a file, write data, and close it (sets in-core > > XFS_EOFBLOCKS_RELEASED = 1). > > Yup, it removed post-eof blocks on close(). > > > 2. Re-open with O_TRUNC (sets XFS_ITRUNCATED = 1) and write 16 MiB > > sequentially (allocates 32 MiB / 65408 blocks due to speculative > > preallocation). > > Yup, we now have an repeated open/write/close pattern, and we have a > need for flush-on-close because this is an unsafe overwrite pattern > (i.e. ITRUNCATED is set) > > > 3. Call fsync(fd) before close(): xfs_file_fsync() clears > > XFS_ITRUNCATED. > > Yup, no need for flush-on-close any more because we don't have to > mitigate an unsafe application write pattern. > > > 4. Call close(fd): xfs_file_release() sees XFS_ITRUNCATED is 0, > > leaves XFS_EOFBLOCKS_RELEASED set, and skips xfs_free_eofblocks(). > > Yup, as intended - there has been an repeated open/write/close > pattern observed. > > > 5. stat() reports st_blocks = 65408 (32 MiB) instead of 32768 (16 MiB). > > Any subsequent swapon discard punches only the 16 MiB swap area, > > leaving 32648 blocks and failing discard assertions. > > Yup, st_blocks is is correct, we've got 32MB of blocks allocated at > this point for the file, and when the inode is reclaimed or blockgc > runs, it will be cleaned up back to match EOF. > > Everything seems to be working as intended... Hi Dave, Thanks a lot for the thorough explanation! I have submitted a fix to LTP swapon04 instead to verify the delta of discarded blocks rather than asserting an exact remaining block count: https://patchwork.kernel.org/project/ltp/patch/20261130073943.1827-1-wegao@suse.com/ Thanks again for your time. Best regards, Wei > > Cheers, > > Dave. > -- > Dave Chinner > dgc@kernel.org