From patchwork Fri Nov 22 13:06:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao-ran Zheng X-Patchwork-Id: 13883171 Received: from zg8tmja5ljk3lje4ms43mwaa.icoremail.net (zg8tmja5ljk3lje4ms43mwaa.icoremail.net [209.97.181.73]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3BFE1D86C4; Fri, 22 Nov 2024 13:07:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.97.181.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732280827; cv=none; b=CY7UPyxSeag2k89wlH2R/gqDWw/Ly0xc5qf7DQYE1wrWTCNIkp7t4BW58IMc+1G3d9uQj6FvQeqAThrFV4SYdDeiElJnAjcwRl4kBcwaKMuKQebK+aWzir+dQWg71WIoQD9+2hI16dwBSqtEG61XmER3ws2vtxal4Q5y1d1DtcA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732280827; c=relaxed/simple; bh=x7tFZcZy7XSEazP6YWhgtnVxbuaNUGiVwGGms/rTpw0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DNebq8wE8fKUi9s7Lr3e62VyLW0qw8amm08PcrLBk7TQdIuM3zFjsaPWH7dzkY7NmtH0W1pC7AIcQu4KfGzj6UD0oumEB3A3IkZGD/cGao21FQj6z7uqxb7tZxzHY6Uw+WVfvZ0gRlt5Myk9Wfx64Xebn+4LkY1wiYeGUs/p+3w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=buaa.edu.cn; spf=pass smtp.mailfrom=buaa.edu.cn; dkim=fail (0-bit key) header.d=buaa.edu.cn header.i=@buaa.edu.cn header.b=N7I7W+2Y reason="key not found in DNS"; arc=none smtp.client-ip=209.97.181.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=buaa.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=buaa.edu.cn Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=buaa.edu.cn header.i=@buaa.edu.cn header.b="N7I7W+2Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=buaa.edu.cn; s=buaa; h=Received:From:To:Cc:Subject:Date: Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=cO962UiYVYKGBhx79KwrZuQCf+O4p104xI fPYBbWRu8=; b=N7I7W+2YqAh5NK+NHX5HUx+vGfPFccrTW6y+yv3rBbLmwF0JUJ fYAAg51QzOqhoaDG8ASDE81MdQCOsmn6QyR244WF/waZwNe7t0uH+EViHZwKaj8K oEsLQYGx/b/Mc1gF2VwG0ltAhwuSEOZ3jOGCI57SktDTEXI+/gX2isd5A= Received: from s1eepy-QiTianM540-A739.. (unknown [10.130.145.81]) by coremail-app1 (Coremail) with SMTP id OCz+CgDXBRDjgUBng+1CAQ--.19878S2; Fri, 22 Nov 2024 21:06:46 +0800 (CST) From: Hao-ran Zheng To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: baijiaju1990@gmail.com, zhenghaoran@buaa.edu.cn, 21371365@buaa.edu.cn Subject: [PATCH v3] fs: Fix data race in inode_set_ctime_to_ts Date: Fri, 22 Nov 2024 21:06:42 +0800 Message-Id: <20241122130642.460929-1-zhenghaoran@buaa.edu.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122112228.6him45jdtibue26s@quack3> References: <20241122112228.6him45jdtibue26s@quack3> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: OCz+CgDXBRDjgUBng+1CAQ--.19878S2 X-Coremail-Antispam: 1UD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` and the function `inode_get_ctime_sec()` are executed concurrently. When two threads call `aio_read` and `aio_write` respectively, they will be distributed to the read and write functions of the corresponding file system respectively. Taking the btrfs file system as an example, the `btrfs_file_read_iter` and `btrfs_file_write_iter` functions are finally called. These two functions created a data race when they finally called `inode_get_ctime_sec()` and `inode_set_ctime_to_ns()`. The specific call stack that appears during testing is as follows: ============DATA_RACE============ btrfs_delayed_update_inode+0x1f61/0x7ce0 [btrfs] btrfs_update_inode+0x45e/0xbb0 [btrfs] btrfs_dirty_inode+0x2b8/0x530 [btrfs] btrfs_update_time+0x1ad/0x230 [btrfs] touch_atime+0x211/0x440 filemap_read+0x90f/0xa20 btrfs_file_read_iter+0xeb/0x580 [btrfs] aio_read+0x275/0x3a0 io_submit_one+0xd22/0x1ce0 __se_sys_io_submit+0xb3/0x250 do_syscall_64+0xc1/0x190 entry_SYSCALL_64_after_hwframe+0x77/0x7f ============OTHER_INFO============ btrfs_write_check+0xa15/0x1390 [btrfs] btrfs_buffered_write+0x52f/0x29d0 [btrfs] btrfs_do_write_iter+0x53d/0x1590 [btrfs] btrfs_file_write_iter+0x41/0x60 [btrfs] aio_write+0x41e/0x5f0 io_submit_one+0xd42/0x1ce0 __se_sys_io_submit+0xb3/0x250 do_syscall_64+0xc1/0x190 entry_SYSCALL_64_after_hwframe+0x77/0x7f To address this issue, it is recommended to add WRITE_ONCE when writing the `inode->i_ctime_sec` variable.and add READ_ONCE when reading in function `inode_get_ctime_sec()` and `inode_get_ctime_nsec()`. Signed-off-by: Hao-ran Zheng --- V2 -> V3: Added READ_ONCE in inode_get_ctime_nsec() and addressed review comments V1 -> V2: Added READ_ONCE in inode_get_ctime_sec() --- include/linux/fs.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 3559446279c1..c18f9a9ee5e7 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1655,12 +1655,12 @@ static inline struct timespec64 inode_set_mtime(struct inode *inode, static inline time64_t inode_get_ctime_sec(const struct inode *inode) { - return inode->i_ctime_sec; + return READ_ONCE(inode->i_ctime_sec); } static inline long inode_get_ctime_nsec(const struct inode *inode) { - return inode->i_ctime_nsec; + return READ_ONCE(inode->i_ctime_nsec); } static inline struct timespec64 inode_get_ctime(const struct inode *inode) @@ -1674,8 +1674,8 @@ static inline struct timespec64 inode_get_ctime(const struct inode *inode) static inline struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 ts) { - inode->i_ctime_sec = ts.tv_sec; - inode->i_ctime_nsec = ts.tv_nsec; + WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec); + WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec); return ts; }