Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Dave Chinner <david@fromorbit.com>
To: Eric Biggers <ebiggers@kernel.org>
Cc: Marco Elver <elver@google.com>,
	syzbot <syzbot+0f1e470df6a4316e0a11@syzkaller.appspotmail.com>,
	akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	syzkaller-bugs@googlegroups.com,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Will Deacon <will@kernel.org>
Subject: Re: KCSAN: data-race in generic_file_buffered_read / generic_file_buffered_read
Date: Thu, 16 Jul 2020 09:42:03 +1000	[thread overview]
Message-ID: <20200715234203.GK5369@dread.disaster.area> (raw)
In-Reply-To: <20200715163256.GB1167@sol.localdomain>

On Wed, Jul 15, 2020 at 09:32:56AM -0700, Eric Biggers wrote:
> [+Cc linux-fsdevel]
> 
> On Wed, Jul 15, 2020 at 05:29:12PM +0200, 'Marco Elver' via syzkaller-bugs wrote:
> > On Wed, Jul 15, 2020 at 08:16AM -0700, syzbot wrote:
> > > Hello,
> > > 
> > > syzbot found the following issue on:
> > > 
> > > HEAD commit:    e9919e11 Merge branch 'for-linus' of git://git.kernel.org/..
> > > git tree:       upstream
> > > console output: https://syzkaller.appspot.com/x/log.txt?x=1217a83b100000
> > > kernel config:  https://syzkaller.appspot.com/x/.config?x=570eb530a65cd98e
> > > dashboard link: https://syzkaller.appspot.com/bug?extid=0f1e470df6a4316e0a11
> > > compiler:       clang version 11.0.0 (https://github.com/llvm/llvm-project.git ca2dcbd030eadbf0aa9b660efe864ff08af6e18b)
> > > 
> > > Unfortunately, I don't have any reproducer for this issue yet.
> > > 
> > > IMPORTANT: if you fix the issue, please add the following tag to the commit:
> > > Reported-by: syzbot+0f1e470df6a4316e0a11@syzkaller.appspotmail.com
> > > 
> > > ==================================================================
> > > BUG: KCSAN: data-race in generic_file_buffered_read / generic_file_buffered_read
> > 
> > Our guess is that this is either misuse of an API from userspace, or a
> > bug. Can someone clarify?
> > 
> > Below are the snippets of code around these accesses.
> 
> Concurrent reads on the same file descriptor are allowed.  Not with sys_read(),
> as that implicitly uses the file position.  But it's allowed with sys_pread(),
> and also with sys_sendfile() which is the case syzbot is reporting here.

Concurrent read()s are fine, they'll just read from the same offset.


> 
> > 
> > > write to 0xffff8880968747b0 of 8 bytes by task 6336 on cpu 0:
> > >  generic_file_buffered_read+0x18be/0x19e0 mm/filemap.c:2246
> > 
> > 	...
> > 	would_block:
> > 		error = -EAGAIN;
> > 	out:
> > 		ra->prev_pos = prev_index;
> > 		ra->prev_pos <<= PAGE_SHIFT;
> > 2246)		ra->prev_pos |= prev_offset;
> > 
> > 		*ppos = ((loff_t)index << PAGE_SHIFT) + offset;
> > 		file_accessed(filp);
> > 		return written ? written : error;
> > 	}
> > 	EXPORT_SYMBOL_GPL(generic_file_buffered_read);
> > 	...
> 
> Well, it's a data race.  Each open file descriptor has just one readahead state
> (struct file_ra_state), and concurrent reads of the same file descriptor
> use/change that readahead state without any locking.
> 
> Presumably this has traditionally been considered okay, since readahead is
> "only" for performance and doesn't affect correctness.  And for performance
> reasons, we want to avoid locking during file reads.
> 
> So we may just need to annotate all access to file_ra_state with
> READ_ONCE() and WRITE_ONCE()...

Please, no. Can we stop making the code hard to read, more difficult
to maintain and preventing the compiler from optimising it by doing
stupid "turn off naive static checker warnings" stuff like this?

If the code is fine with races, then -leave it alone-. If it's not
fine with a data race, then please go and work out the correct
ordering and place well documented barriers and/or release/acquire
ordering semantics in the code so that we do not need to hide data
races behind a compiler optimisation defeating macro....

Yes, I know data_race() exists to tell the tooling that it should
ignore data races in the expression, but that makes just as much
mess of the code as READ_ONCE/WRITE_ONCE being spewed everywhere
indiscriminately because <some tool said we need to do that>.

Cheers,

Dave.
-- 
Dave Chinner
david@fromorbit.com

  parent reply	other threads:[~2020-07-15 23:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <0000000000004a4d6505aa7c688a@google.com>
     [not found] ` <20200715152912.GA2209203@elver.google.com>
2020-07-15 16:32   ` Eric Biggers
2020-07-15 16:45     ` Marco Elver
2020-07-15 23:42     ` Dave Chinner [this message]
2020-07-16  3:03       ` Eric Biggers
2020-07-16  6:24         ` Nicholas Piggin
2020-07-16  6:54           ` Eric Biggers
2020-07-16  7:52             ` Nicholas Piggin
2020-07-16 12:56               ` Marco Elver
2020-07-16 13:19             ` Matthew Wilcox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200715234203.GK5369@dread.disaster.area \
    --to=david@fromorbit.com \
    --cc=akpm@linux-foundation.org \
    --cc=ebiggers@kernel.org \
    --cc=elver@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=paulmck@kernel.org \
    --cc=syzbot+0f1e470df6a4316e0a11@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=will@kernel.org \
    --subject='Re: KCSAN: data-race in generic_file_buffered_read / generic_file_buffered_read' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).