Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp911159ybh; Wed, 15 Jul 2020 20:05:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoWVcRYbtApt8wwAMMjl7e3V/D7u0OuYfwvs4RupMEnbNsByLMt6xg8YjrHe1R1kBYQ88F X-Received: by 2002:a17:906:3fd2:: with SMTP id k18mr1895951ejj.387.1594868700473; Wed, 15 Jul 2020 20:05:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594868700; cv=none; d=google.com; s=arc-20160816; b=NDXi5UWae9QMlurIi9ThUYoDfp7DR9k9v+IZgoKLQEY0HvxFqou3f4geeH9l2KPnem kM7uqHDL6HSs47AV9OF6CNM2NaWN3kmySgki57tiFbgAMsJZ/kNG1dikEtL5YvavQXrN yy/oc0l8dxwpInzRWHBFFFzeFG27Y9TvvpKe1i18rYObXZBsYxD0Fq85Rj0DLh8hyutp Dbew6eyY/FZR0PPa7Bl3LYW9xHfkZWzR9Z2CTyGTWG1Fwyka86lLWbubYB2cFvzqu320 DTE/mEVgx2haoWXlwjlcJkF3Vwm/WZSaibTnNVxBiQ/6uwbVFkmKWkItF+yagMnN/kgC HNug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=L25ezA55ceHvQFkI2fcLTJCEMUT+P+fIlnml2bUCx08=; b=TUWjYrp27/AwPJ6VaMWpaYE7+UgtUcd88baHE/6Ns6K9WacToFJtkRkIQQXmPbv/lr uBbw9ZeaMKRVpeOaUkbXu30tDuCQNA2hCVdsf1/ImMFUNxabpkVyrUzGF3/+/78Lf0Gy 6h1S3UnppQoCJZERdabkDIZTFwDZjkwxiCyjxkhexq/qRn2bMrEoHkRTwn1BA9afjTuR Qcouz4aXS8o38PU/TRS3fI9oYyQYBc6DJ6grbMakYxkWTHfAx7WP3HjpI1oW1x63LH6w 6QOXbqGEYJ/JElvbJQgIphE+0EGq7OBTE0Bz0nIKZPUJiRs88JxTT3tO3vU3Kitm/pX4 VOsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MAls4WTW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h20si2390296ejk.652.2020.07.15.20.04.36; Wed, 15 Jul 2020 20:05:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MAls4WTW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727999AbgGPDD7 (ORCPT + 99 others); Wed, 15 Jul 2020 23:03:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:39394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727034AbgGPDD7 (ORCPT ); Wed, 15 Jul 2020 23:03:59 -0400 Received: from sol.localdomain (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8A75D2076C; Thu, 16 Jul 2020 03:03:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594868638; bh=v3Bqa4cOJJvz9YfwAqdgDudvEV9FyMcI2b5gw82oJMk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MAls4WTWjsI5YbeQ94NmG1Ky2Dcn/OjsqhjAMI+0sxn0L0tOx7DwjQkLX+SPJF14B CLn8vyq0lzGY3pr/8nApMvH3xFH2bk3Wwg3lkrmSvhto3HVxVHgy3MEPm2uq+RG3HL AcNIGbr/Fg6EYDdnV6u+gUNyMHKM2USKEGXc4WuY= Date: Wed, 15 Jul 2020 20:03:57 -0700 From: Eric Biggers To: Dave Chinner Cc: Marco Elver , syzbot , 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" , Will Deacon Subject: Re: KCSAN: data-race in generic_file_buffered_read / generic_file_buffered_read Message-ID: <20200716030357.GE1167@sol.localdomain> References: <0000000000004a4d6505aa7c688a@google.com> <20200715152912.GA2209203@elver.google.com> <20200715163256.GB1167@sol.localdomain> <20200715234203.GK5369@dread.disaster.area> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200715234203.GK5369@dread.disaster.area> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 16, 2020 at 09:42:03AM +1000, Dave Chinner wrote: > 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. > Actually the VFS serializes concurrent read()'s on the same fd, at least for regular files. Anyway, doesn't matter since we can consider pread() instead. > > > > > > > > > > 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 . > Data races are undefined behavior, so it's never guaranteed "fine". We can only attempt to conclude that it's fine "in practice" and is too difficult to fix, and therefore doesn't meet the bar to be fixed (for now). Of course, in most cases the preferred solution for data races is to introduce proper synchronization. As I said, I'm not sure that's feasible here. Memory barriers aren't the issue here; we'd need *locking*, which would mean concurrent readers would start contending for the lock. Other suggestions appreciated... - Eric