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=1796381975; b=EZqOVRe/bh25IVQn9pi+Kd9Dz5IHTzstOQqx3oGMCdnQnQCOSsU8MCuvqjkNApOLq5sP qx/Sjj1Y/D/Jkvk+O6zVCglYBEEtMmz6jogA8/mynoUQj9ZlL/4Ri+foQcUOplV1smoKF guRU2/CSf6uzqa0S/xKGJh82QPU45wwCSfCHAFYPdRzwRuTsE0a06cb7zbmx/AF7Ax/xd loRdooqdE1kbXbcDdsUpXMJrf+jkOo565+cybgoBQ9psI5DgoBbRgEYIYEQLAMnz5YzEU jkJ75jwvlcOQfGEgLqz3p7w/uKADXrG04PkzcvaKXLCpmXyB23SzaYR5QL0SBNk9B/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381975; 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=rv++eKc1pcVl7Sjs5WA3atCAM3L/9FcW6bAmvKeBEgo=; b=sjYseKkyiqF8GdzQjf8HCgc42YnKDRr+tGfGpr+lpcqynpIzBJDXsME+HC9iMn0jUfAf ru53Je7/hQeDCv5V/aip+edjoVdr2YSwhaoj86Js7zOoVJAsiCIbHpC5dwItkgSBh39gS fEttxFZx3NZnYamAfqxoFYhGdgCHOlthUyCQtvqIjXO96tVEg9vBNuc5kjjmUFQA3yGFm 1JyzXsetxmF/1bcg3yZA0lMqnCqphZW9SbCSnzfw4TVFdZw37QQ474QGpYVF1+vz/p8e4 w9u1mvnxonZm9LZvoON413zuQyapRMfsvRprIsnh6+MIXsn2UciB5hO/Z5momd8gScw== 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=GLShWZFc; 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 5274E606A322; Fri, 4 Dec 2026 10:59:35 +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=GLShWZFc Received: by smtp.kernel.org (Postfix) with ESMTPSA id C08951F000C2; Fri, 4 Dec 2026 10:59:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381975; bh=rv++eKc1pcVl7Sjs5WA3atCAM3L/9FcW6bAmvKeBEgo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GLShWZFcDWrbUI7D9neYUju0pO2Q8CKurEKzZp80zw2RvHH2/H839iImLeIZsalhn yOtfAxbq1RnTaDg89bS3Mb083duEhAt6lz6X79QImkYX28KrHOpPIG6+V+IEtz2sKH ca8Plhs8qNMqUZF5SX9D5JA71qSxFgq2GD/wpD4Q= 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 6.18 254/703] btrfs: fix race between concurrent sync direct IO writes Date: Fri, 4 Dec 2026 19:02:33 +0900 Message-ID: <20261204100634.551886825@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-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 a5f7f1bb2364b..52cfe07ed9d04 100644 --- a/fs/btrfs/direct-io.c +++ b/fs/btrfs/direct-io.c @@ -882,9 +882,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; /* @@ -916,6 +920,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