From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380312; b=UmfFEQHWsB9Ehp+NK9QZQTCW0fXh/xL5pED8wJZH/dSKKmLw3L+nBpsIIfzidjnsOGlA 7u9Fhziq4nkZD75pdiL4jhGVlqsEegp3MLjqWIV/hKfyaM9jlDghqvh9TwVf7OLVcoyek WLY2hIjM/vVUIcbjsvrGWfXTuDPB0DW4Tb7X/KaQPjL3u/XYfInJgovQHQ7Jp7jOd1G2b 51XmDu+URJ/bs9fPa8RiHzWjFV93npJlTfLPlTaWSO+2TNLVogcUiwiiakSE+YJ8bzf8p pC5ypd2ToUzeQ+by7R42Km3z6REsT1SipcRQDvjSi4Qpx/CjYV9NWOcJqVfhb2BtEww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380312; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=6775R7CQL8pmoWpQjypp2q7Gg8qTCwLNJhy34uIDIKQ=; b=K+xY0CZWvwKXoVWCta6HngrWXHd+WK4R8vR5/+vZgvmfXdM16erojNYtxgdMa1PQeZFE Q5ph7JHF+w6t3xrjvz3MW2svdcBsfKmm21jlSRBPEvx6mVrVF/A2FPolvIvJuO03eeKWf LeOWkPLtGxIRzTKHn9hO77mpl7/DHMuhs/WGvV3QuZuzK9Hkk7l4SSpK3gmho3wBz3fO/ XojleoewB5ActLwqEVbTE8N+lDXQARpyYduOhb5txF3vHZjxE2krhv2GGDlSn91AEJxde 0kX+OaW2P+WbajTygJoU3z/5WmAg3Ws9Bo8STFAAJhDN2GwYqJ3ias02UUqIqRvyLrw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RItwgtmD; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7806606A32C; Fri, 4 Dec 2026 10:31:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RItwgtmD Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DBAE1F00897; Fri, 4 Dec 2026 10:31:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380312; bh=6775R7CQL8pmoWpQjypp2q7Gg8qTCwLNJhy34uIDIKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RItwgtmDXLkGGH/2c+AisRBwfpHVNEb+QYCbBQ3BNAPGvp5+lcU4DgmUvXyFQvtFZ duN6WiSWta5ejfg83wUH2FTB1YkjFRDTd1WL71jZYYPCUBhR+twxoLExNifl9jK/uL HwU5cnVj0VJPLeXTBBt5iWZ/IVJGlS50wTOsPQc0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qu Wenruo , Filipe Manana , David Sterba , Sasha Levin Subject: [PATCH 7.3 217/742] btrfs: fix race between concurrent sync direct IO writes Date: Fri, 4 Dec 2026 19:01:21 +0900 Message-ID: <20261204100640.843980314@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana [ Upstream commit ad4bb983915d8588dc9dd3aeb60cf58aff574e3b ] If we have a direct IO write within i_size, we lock the inode's VFS lock in shared mode, which allows for concurrent direct IO writes within different ranges of the file, which is an important optimization for many applications such as databases. However if we have a sync direct IO write within i_size, it locks the inode in shared mode and then enters fsync through the call to the iomap_dio_complete() function from iomap using the shared lock. This means that if we have another concurrent direct IO write that locks the inode in shared mode and the fsync is in fast mode (the inode flag BTRFS_INODE_NEEDS_FULL_SYNC is not set), the fsync does not wait for the ordered extent created by the concurrent direct IO write to complete before logging its extent map. The fsync code relies on direct IO writes to create ordered extents and wait for their IO to finish while holding the inode's lock, while the fsync should hold the inode lock in exclusive mode. This means that this race can end up persisting a log tree with file extent items that point to extents that were not yet written when the log is synced. Fix this by always taking the inode lock in exclusive mode if we have a sync write. This issue was identified by Sashiko in the link below, which identifies two different races between direct IO writes and fsync. The race fixed here is the second case identified by Sashiko in the link below. The first race is already being fixed in a separate patch from Josef. Link: https://sashiko.dev/#/bug/linux-ee047626-2fa7-46d5-b543-861136993fc7 Fixes: 939b656bc8ab ("btrfs: fix corruption after buffer fault in during direct IO append write") Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/direct-io.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c index ed3914257d395..e7c3647c0f739 100644 --- a/fs/btrfs/direct-io.c +++ b/fs/btrfs/direct-io.c @@ -896,9 +896,13 @@ ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) * If the write DIO is within EOF, use a shared lock and also only if * security bits will likely not be dropped by file_remove_privs() called * from btrfs_write_check(). Either will need to be rechecked after the - * lock was acquired. + * lock was acquired. Also if this is a sync write we will need to fsync + * and therefore we must lock in exclusive mode, otherwise we would race + * with other concurrent DIO writes and the fsync would not wait for + * their ordered extent to finish IO before logging the new extent maps. */ - if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode) && IS_NOSEC(inode)) + if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode) && + IS_NOSEC(inode) && !iocb_is_dsync(iocb)) ilock_flags |= BTRFS_ILOCK_SHARED; relock: @@ -920,6 +924,13 @@ ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) goto relock; } + /* Sync writes must use exclusive locking, see the comment above. */ + if ((ilock_flags & BTRFS_ILOCK_SHARED) && iocb_is_dsync(iocb)) { + btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); + ilock_flags &= ~BTRFS_ILOCK_SHARED; + goto relock; + } + ret = generic_write_checks(iocb, from); if (ret <= 0) { btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); -- 2.53.0