From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753390AbbJNREY (ORCPT ); Wed, 14 Oct 2015 13:04:24 -0400 Received: from e31.co.us.ibm.com ([32.97.110.149]:41866 "EHLO e31.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751889AbbJNREW (ORCPT ); Wed, 14 Oct 2015 13:04:22 -0400 X-IBM-Helo: d03dlp01.boulder.ibm.com X-IBM-MailFrom: paulmck@linux.vnet.ibm.com X-IBM-RcptTo: linux-kernel@vger.kernel.org;linux-tip-commits@vger.kernel.org Date: Wed, 14 Oct 2015 10:04:24 -0700 From: "Paul E. McKenney" To: Dmitry Vyukov Cc: tip-bot for Andrey Ryabinin , linux-tip-commits@vger.kernel.org, kasan-dev , Ingo Molnar , Kostya Serebryany , Borislav Petkov , Andrey Ryabinin , Andrew Morton , LKML , Peter Zijlstra , Andy Lutomirski , Linus Torvalds , Thomas Gleixner , Sasha Levin , Denys Vlasenko , Wolfram Gloger , Andrey Konovalov , "H. Peter Anvin" , Alexander Potapenko Subject: Re: [tip:locking/urgent] compiler, atomics: Provide READ_ONCE_NOCHECK () Message-ID: <20151014170424.GY3910@linux.vnet.ibm.com> Reply-To: paulmck@linux.vnet.ibm.com References: <1444750088-24444-2-git-send-email-aryabinin@virtuozzo.com> <20151014154532.GV3910@linux.vnet.ibm.com> <20151014160146.GW3910@linux.vnet.ibm.com> <20151014162036.GX3910@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15101417-8236-0000-0000-000012B230BD Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 14, 2015 at 06:32:58PM +0200, Dmitry Vyukov wrote: > On Wed, Oct 14, 2015 at 6:20 PM, Paul E. McKenney > wrote: > > On Wed, Oct 14, 2015 at 06:08:16PM +0200, Dmitry Vyukov wrote: > >> On Wed, Oct 14, 2015 at 6:01 PM, Paul E. McKenney > >> wrote: > >> > On Wed, Oct 14, 2015 at 05:50:34PM +0200, Dmitry Vyukov wrote: > >> >> On Wed, Oct 14, 2015 at 5:45 PM, Paul E. McKenney > >> >> wrote: > >> >> > On Wed, Oct 14, 2015 at 08:28:43AM -0700, tip-bot for Andrey Ryabinin wrote: > >> >> >> Commit-ID: 4115ffdf4d6f8986a7abe1dd522c163f599bc0e6 > >> >> >> Gitweb: http://git.kernel.org/tip/4115ffdf4d6f8986a7abe1dd522c163f599bc0e6 > >> >> >> Author: Andrey Ryabinin > >> >> >> AuthorDate: Tue, 13 Oct 2015 18:28:07 +0300 > >> >> >> Committer: Ingo Molnar > >> >> >> CommitDate: Wed, 14 Oct 2015 16:44:06 +0200 > >> >> >> > >> >> >> compiler, atomics: Provide READ_ONCE_NOCHECK() > >> >> >> > >> >> >> Some code may perform racy by design memory reads. This could be > >> >> >> harmless, yet such code may produce KASAN warnings. > >> >> >> > >> >> >> To hide such accesses from KASAN this patch introduces > >> >> >> READ_ONCE_NOCHECK() macro. KASAN will not check the memory > >> >> >> accessed by READ_ONCE_NOCHECK(). > >> >> >> > >> >> >> This patch creates __read_once_size_nocheck() a clone of > >> >> >> __read_once_size_check() (renamed __read_once_size()). > >> >> >> The only difference between them is 'no_sanitized_address' > >> >> >> attribute appended to '*_nocheck' function. This attribute tells > >> >> >> the compiler that instrumentation of memory accesses should not > >> >> >> be applied to that function. We declare it as static > >> >> >> '__maybe_unsed' because GCC is not capable to inline such > >> >> >> function: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 > >> >> >> > >> >> >> With KASAN=n READ_ONCE_NOCHECK() is just a clone of READ_ONCE(). > >> >> > > >> >> > So I add READ_ONCE_NOCHECK() for accesses for which the compiler cannot > >> >> > prove safe address for KASAN's benefit, but READ_ONCE() suffices for > >> >> > the data-race-detection logic in KTSAN, correct? > >> >> > >> >> KTSAN also needs READ_ONCE_NOCHECK() here. KTSAN will flag races > >> >> between get_wchan() and the thread accesses to own stack even more > >> >> aggressively than KASAN, because KTSAN won't like get_wchan() accesses > >> >> even to non-poisoned areas of other thread stack. > >> > > >> > So to keep KTSAN happy, any read from some other thread's stack requires > >> > READ_ONCE_NOCHECK()? What if the access is via a locking primitive or > >> > read-modify-write atomic operation? > >> > > >> > This is of some interest in RCU, which implements synchronous grace > >> > periods using completions that are allocated on the calling task's stack > >> > and manipulated by RCU callbacks that are likely executing elsewhere. > >> > >> KTSAN does not have any special logic for stacks. It just generally > >> flags pairs of accesses when (1) at least one access is not atomic, > >> (2) at least one access is a write and (3) these accesses are not > >> synchronized by means of other synchronization. > >> There is a bunch of cases when kernel code allocates objects on stack > >> and then passes them to other threads, but as far as there is proper > >> synchronization it is OK. > > > > OK, so let me see if I understand this. ;-) > > > > KASAN requires READ_ONCE_NOCHECK() for get_wchan(). KTSAN would be > > just as happy with READ_ONCE(), but READ_ONCE_NOCHECK() works for > > both. > > > > Did I get it right? > > > No, KTSAN also needs READ_ONCE_NOCHECK. > READ_ONCE in get_wchan can lead to a data race report. > Consider: > > // the other thead > some_stack_var = ...; > > // get_wchan > bp = READ_ONCE(p); // where p happens to point to some_stack_var in > the other thread > > This is generally not atomic and not safe. And this is a data race by > all possible definitions. > Only READ_ONCE on reading side is not enough to ensure atomicity, also > all concurrent writes must be done with atomic operations. OK. However, this is specific to get_wchan()'s out-of-bounds stack, right? If I have multiple tasks accessing some other task's on-stack variable, then as long as all other potentially concurrent accesses use atomic operations, READ_ONCE() suffices. Or am I still missing something here? Thanx, Paul