Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4066081ybv; Tue, 25 Feb 2020 12:28:58 -0800 (PST) X-Google-Smtp-Source: APXvYqzwYmfvJ3l2TupPaMxpwiastXd1i8goum+Dc+D/7oS3dj9200woNyhUJng3QwZ0lhPM5rmQ X-Received: by 2002:a9d:6457:: with SMTP id m23mr316783otl.162.1582662538749; Tue, 25 Feb 2020 12:28:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582662538; cv=none; d=google.com; s=arc-20160816; b=cfZTvjxe7+0vdfvsTJQc7XuJvuSibsxvw0D48paWuq0TvQ+G4CnCnoDDU2KTxGxYqn ijS6fGK3G2qiBn8O6HZvEnZXD8fWAvslrELfiPMBa1d8P20rSO7DmluNIrvBqWEgZOIi OwvB/2Q/d/a68R1YE+aOYZq6HuWxHdmc5ZUpbU/ezFsUpHQ1TYCiEHyJXTBCZab+gkZH tP3daUU8LM3fYXHjTaCcXU1DbbmsLenAXatwNcVscf+6yfDkV5mrMJ05Y0t2yoq6jW9w plXiqT7tZg8wM75lq/VM9eg8OjYdBBy22ot+sgzFrSmVWa3EKmEodaWEnD+LsEhrRaXd TrlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=h8zKM3vL3MUBQAymrTc6L3H6hz70LO6qXSQaWI0IU04=; b=oUMdkSf4U01h+la8tB4hkYpHjUYOyKYX7KCNXxoOz1YmMteHuzX8/JdVgdvRCKMmcR X6Y2zf75jUEWe8S/2nEcwlHk56pUHrqn3vGZqLUX6Q7Y/aAXp3a6M+Z174EP2h8cu5Hp BIXiN1XYbm5PQy5wMODdscnYXLQSJgtxTX4T03cYqkv7bOwrEQludpoJiqK8Bu8nSOzI BYSIpR8+inybD9rmDxvZwBRcsan9UmU+fCrGxVLmupOziulBADspQg1G+oOUU3DL9zAz 6MNR8mkozG8W9rL8NMTPsJbiFPlYcw2qvueL00lSjHbqncmC81kIUx099YDxZtqYSuP2 oCCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WmQJlJHU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r12si135369oij.113.2020.02.25.12.28.46; Tue, 25 Feb 2020 12:28:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WmQJlJHU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731785AbgBYU1R (ORCPT + 99 others); Tue, 25 Feb 2020 15:27:17 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:38343 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731779AbgBYU1Q (ORCPT ); Tue, 25 Feb 2020 15:27:16 -0500 Received: by mail-ot1-f67.google.com with SMTP id z9so780230oth.5 for ; Tue, 25 Feb 2020 12:27:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=h8zKM3vL3MUBQAymrTc6L3H6hz70LO6qXSQaWI0IU04=; b=WmQJlJHUD9G/rwl2N7kvGdTxCz/wKi5OzBRrKCoh3fWb2iwQEyy5OTrQ3kTGU8gZyJ QoU1CnpgLKviImgkkBCl2TIxhwmNPXOc3RQPaoCxAOZ/1k8ZC8kC/6nM+/P/H78ODdIH QXDJysRZ7/58K1841F4afX1gfkTmlynl9xfZ4kc9BVGwJV3Et7sVsHDedh/buArpiD++ UkmuL5eU2z4h1uMVp1unlGbbZTHDbRubOAtgR5D/tOymvzaQqLTNUtKMs1Uuvyc0Vt7n dOZqcoeeV3L/ochfkusVJC7kPWylzVi874qFU6UTI9Pwws/Nqw8b4MIj99KZ/QLZ21fo lj2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=h8zKM3vL3MUBQAymrTc6L3H6hz70LO6qXSQaWI0IU04=; b=c/lZl1bUZrBg4Q91ridbQAB01A2gT6GT2hy0OngGplLFnzO4RaZzhOkoqgk9NQj68s 4zK/NEyGjYMElpDQFW80TroymyJpXNi2hjGeVyCO1/QyQO3sayy2A28Njh0xjbpC/VVq 5MtzfQ8knZi0GbRycusE5cqZxlBBSh230uN6PyWqPyLaf/7eNnMCpCA6Ph+GOpGNPYPg W8pTixl1DEZpn4/otwvK3UHyiLOXjvS8BLczWIB2Bp02QHgKyAbdMDOI8n0j1BmED302 5n5+hdMhiQHOX00UJw50Zq/rfrwNrwukiBZkdfDZXXZfUC9Q5PAVvbAit2MBB7JIk5Uk CDIA== X-Gm-Message-State: APjAAAWb83KaoQfCEEqp1cxs7xvNLhcMDLiN1TqSAk9bV59cRPWQR4IX F3pxo1WAR7Y5YS18GEQPkljMakga3uPg/ohA6N0pIw== X-Received: by 2002:a9d:66d1:: with SMTP id t17mr292149otm.233.1582662435427; Tue, 25 Feb 2020 12:27:15 -0800 (PST) MIME-Version: 1.0 References: <1582661385-30210-1-git-send-email-cai@lca.pw> In-Reply-To: <1582661385-30210-1-git-send-email-cai@lca.pw> From: Marco Elver Date: Tue, 25 Feb 2020 21:27:03 +0100 Message-ID: Subject: Re: [PATCH] xfs: fix data races in inode->i_*time To: Qian Cai Cc: "Darrick J. Wong" , linux-xfs@vger.kernel.org, LKML , kasan-dev Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 25 Feb 2020 at 21:09, Qian Cai wrote: > > inode->i_*time could be accessed concurrently. The plain reads in > xfs_vn_getattr() is lockless which result in data races. To avoid bad > compiler optimizations like load tearing, adding pairs of > READ|WRITE_ONCE(). While at it, also take care of xfs_setattr_time() > which presumably could run concurrently with xfs_vn_getattr() as well. > The data races were reported by KCSAN, > > write to 0xffff9275a1920ad8 of 16 bytes by task 47311 on cpu 46: > xfs_vn_update_time+0x1b0/0x400 [xfs] > xfs_vn_update_time at fs/xfs/xfs_iops.c:1122 So this one is doing concurrent writes and reads of the whole struct, which is 16 bytes. This will always be split into multiple loads/stores. Is it intentional? Sadly, this is pretty much guaranteed to tear, even with the READ/WRITE_ONCE. The *ONCE will just make KCSAN not tell us about this one, which is probably not what we want right now, unless we know for sure the race is intentional. Thanks, -- Marco > update_time+0x57/0x80 > file_update_time+0x143/0x1f0 > __xfs_filemap_fault+0x1be/0x3d0 [xfs] > xfs_filemap_page_mkwrite+0x25/0x40 [xfs] > do_page_mkwrite+0xf7/0x250 > do_fault+0x679/0x920 > __handle_mm_fault+0xc9f/0xd40 > handle_mm_fault+0xfc/0x2f0 > do_page_fault+0x263/0x6f9 > page_fault+0x34/0x40 > > 4 locks held by doio/47311: > #0: ffff9275e7d70808 (&mm->mmap_sem#2){++++}, at: do_page_fault+0x143/0x6f9 > #1: ffff9274864394d8 (sb_pagefaults){.+.+}, at: __xfs_filemap_fault+0x19b/0x3d0 [xfs] > #2: ffff9274864395b8 (sb_internal){.+.+}, at: xfs_trans_alloc+0x2af/0x3c0 [xfs] > #3: ffff9275a1920920 (&xfs_nondir_ilock_class){++++}, at: xfs_ilock+0x116/0x2c0 [xfs] > irq event stamp: 42649 > hardirqs last enabled at (42649): [] _raw_spin_unlock_irqrestore+0x53/0x60 > hardirqs last disabled at (42648): [] _raw_spin_lock_irqsave+0x21/0x60 > softirqs last enabled at (42306): [] __do_softirq+0x34c/0x57c > softirqs last disabled at (42299): [] irq_exit+0xa2/0xc0 > > read to 0xffff9275a1920ad8 of 16 bytes by task 47312 on cpu 40: > xfs_vn_getattr+0x20c/0x6a0 [xfs] > xfs_vn_getattr at fs/xfs/xfs_iops.c:551 > vfs_getattr_nosec+0x11a/0x170 > vfs_statx_fd+0x54/0x90 > __do_sys_newfstat+0x40/0x90 > __x64_sys_newfstat+0x3a/0x50 > do_syscall_64+0x91/0xb05 > entry_SYSCALL_64_after_hwframe+0x49/0xbe > > no locks held by doio/47312. > irq event stamp: 43883 > hardirqs last enabled at (43883): [] do_syscall_64+0x39/0xb05 > hardirqs last disabled at (43882): [] trace_hardirqs_off_thunk+0x1a/0x1c > softirqs last enabled at (43844): [] __do_softirq+0x34c/0x57c > softirqs last disabled at (43141): [] irq_exit+0xa2/0xc0 > > Signed-off-by: Qian Cai > --- > fs/xfs/xfs_iops.c | 18 +++++++++--------- > 1 file changed, 9 insertions(+), 9 deletions(-) > > diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c > index 81f2f93caec0..2d5ca13ee9da 100644 > --- a/fs/xfs/xfs_iops.c > +++ b/fs/xfs/xfs_iops.c > @@ -547,9 +547,9 @@ > stat->uid = inode->i_uid; > stat->gid = inode->i_gid; > stat->ino = ip->i_ino; > - stat->atime = inode->i_atime; > - stat->mtime = inode->i_mtime; > - stat->ctime = inode->i_ctime; > + stat->atime = READ_ONCE(inode->i_atime); > + stat->mtime = READ_ONCE(inode->i_mtime); > + stat->ctime = READ_ONCE(inode->i_ctime); > stat->blocks = > XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); > > @@ -614,11 +614,11 @@ > ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); > > if (iattr->ia_valid & ATTR_ATIME) > - inode->i_atime = iattr->ia_atime; > + WRITE_ONCE(inode->i_atime, iattr->ia_atime); > if (iattr->ia_valid & ATTR_CTIME) > - inode->i_ctime = iattr->ia_ctime; > + WRITE_ONCE(inode->i_ctime, iattr->ia_ctime); > if (iattr->ia_valid & ATTR_MTIME) > - inode->i_mtime = iattr->ia_mtime; > + WRITE_ONCE(inode->i_mtime, iattr->ia_mtime); > } > > static int > @@ -1117,11 +1117,11 @@ > > xfs_ilock(ip, XFS_ILOCK_EXCL); > if (flags & S_CTIME) > - inode->i_ctime = *now; > + WRITE_ONCE(inode->i_ctime, *now); > if (flags & S_MTIME) > - inode->i_mtime = *now; > + WRITE_ONCE(inode->i_mtime, *now); > if (flags & S_ATIME) > - inode->i_atime = *now; > + WRITE_ONCE(inode->i_atime, *now); > > xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); > xfs_trans_log_inode(tp, ip, log_flags); > -- > 1.8.3.1 >