Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp592856ybp; Wed, 9 Oct 2019 00:46:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqzvJxbaTJ7l+CW+zM2c8LIBErU31bq90GkpvD24q+kGhf8eMU4+HU+L8sEyMJWpSGHXqSkG X-Received: by 2002:aa7:cd0f:: with SMTP id b15mr1711744edw.3.1570607200887; Wed, 09 Oct 2019 00:46:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570607200; cv=none; d=google.com; s=arc-20160816; b=WH0ocGfFHd3K1pD737cEjZ6FJb9ABjX2CqBWnTTGcrQN/e6PNjiTT6+RleFxm4QaY2 W9UQ6xS+ch7k+7rm6EpogU2SmMYcGxUbgBcjWu+juIrS+NIdnbQExn5Ok1h/k6M0TlHd RtJ5oRKjVO50C1kolOV95Fa7Aq575KjUdQkBN6cOLleuFaPqRSwDNRXwpdsBqST9Z6Us H7vopgmtop4Zglzje6eFDE7nmS+gS2PytC9uhtjVbNJtRxQLWJFDgoApDGEbkEGln449 mkEToHjS7gpKjhjkIPIchmfg+28Uo2TBe4lGxW+CgRFwy0M8oAVhVBBlceNCfMsNqMlB TH5Q== 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=sxXKt3AG8Tk8Yu+PG3kbL4A+1NeXOKutWUO/KuZh7Ek=; b=kzbGb9IgOHBKuDaF4ShBkSzQjRlp/xG76lqFeC0Yv2PEAdF+p2cqYm0avaScIz5AR0 /u2xIDhBAffdyfU5Xc5giiQh15Hj5X/5tqUUh8nr9+3BjqUiNzu7ZpBEGwbKvvTqCbuq 3GDACp+tGqHaEEFnBJhzEpMo8fofXyevukK//QKfYQiKEY9yestfTZNCr4+5vycKAbVt ewvMluWoc3B3Ow2xS+h8ChXsO3qWEEDPYLpWSIfnRfjnKRf6UapMX0tizhjrBxy/Tfsz Uuz6IuZtwRVUaZdedK4waE0nnYSxomslzGBHFf0h58GFc/BZs5qGoLE3IgSsDjaaBfBw 2LcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nJbDYOXG; 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 f57si854226edb.165.2019.10.09.00.46.17; Wed, 09 Oct 2019 00:46:40 -0700 (PDT) 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=nJbDYOXG; 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 S1727629AbfJIHqG (ORCPT + 99 others); Wed, 9 Oct 2019 03:46:06 -0400 Received: from mail-qk1-f196.google.com ([209.85.222.196]:34622 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725848AbfJIHqG (ORCPT ); Wed, 9 Oct 2019 03:46:06 -0400 Received: by mail-qk1-f196.google.com with SMTP id q203so1359493qke.1 for ; Wed, 09 Oct 2019 00:46:04 -0700 (PDT) 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=sxXKt3AG8Tk8Yu+PG3kbL4A+1NeXOKutWUO/KuZh7Ek=; b=nJbDYOXGjSV4xCIvw8szYDZLh/tIZvHGG5t5fcB/SCio6Cw8Puq2TMJP8wpsW27smT 6O/1Hfk1Gu1j9wQnhqlubBMDm/uyfAlQ1RiMK8LeCAj7iRub/6yg8SpoHnUoSFdjwhJD KyLCx6fFRt5jb5Nhbf9bfx0SBNRImDoxWFjWtwXcGc9nMJDMuQY279hmDiu6eHYZRTb/ IpGfPAN05DxUhjn99xNZWk72/p6Sdde+zFG73uDMJPeGtwb7fsooBf8KCElm232E9HEO ZKP//XHQtiDJEJB4cp1OBt4A3pgR45AobXIESeqpzwFDp2Lz9bQRQuynl+NPRSHKOrTg SUgg== 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=sxXKt3AG8Tk8Yu+PG3kbL4A+1NeXOKutWUO/KuZh7Ek=; b=GR7BagkzSS0g1y0Zyb13mNJJ6pGk2UAdUVuc8udW9+xS8XShseH17rxYsK/6gAtT+3 kv9IQvs4fa3suPVnulO5zW6M9sRMj02Z185C75e7OKqN12iXlMKmDhyMpTbwBeuK5pLs CZ79ZpdAfBpLv5RdtknaLP38VWZ1/yjqNfy/qey6vRGYSUOnFOl+zQB2qzMbg780SCKW OTDX2eHIEup1fqCto9g8opMbnC1oogy3r35QGO+muQ6qZSf+7HHKdP328TNc90GeTIv0 7hP2VKNC/NRJezQMZ9tMmj5pcgTLZE7b4J4WdUIzo/FFnsIHel7VxjnSukmGG/4fgtmN MQug== X-Gm-Message-State: APjAAAWE8NZUYfhSJr3YiX65TRoJ0v74bW6xJoA6lics1Aq6YeWUSlZK 5kkVrhcldUzHq+L8MZ1HV4nlpmQCo4qTzAjEXr/TiQ== X-Received: by 2002:a37:4a87:: with SMTP id x129mr2264716qka.43.1570607163183; Wed, 09 Oct 2019 00:46:03 -0700 (PDT) MIME-Version: 1.0 References: <20190920155420.rxiflqdrpzinncpy@willie-the-truck> <0715d98b-12e9-fd81-31d1-67bcb752b0a1@gmail.com> In-Reply-To: From: Dmitry Vyukov Date: Wed, 9 Oct 2019 09:45:50 +0200 Message-ID: Subject: Re: Kernel Concurrency Sanitizer (KCSAN) To: Eric Dumazet Cc: Will Deacon , Marco Elver , kasan-dev , LKML , Andrey Konovalov , Alexander Potapenko , "Paul E. McKenney" , Paul Turner , Daniel Axtens , Anatol Pomazau , Andrea Parri , Alan Stern , LKMM Maintainers -- Akira Yokosawa , Nicholas Piggin , Boqun Feng , Daniel Lustig , Jade Alglave , Luc Maranget 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 Sat, Oct 5, 2019 at 6:16 AM Dmitry Vyukov wrote: > > On Sat, Oct 5, 2019 at 2:58 AM Eric Dumazet wrote: > > > This one is tricky. What I think we need to avoid is an onslaught of > > > patches adding READ_ONCE/WRITE_ONCE without a concrete analysis of the > > > code being modified. My worry is that Joe Developer is eager to get their > > > first patch into the kernel, so runs this tool and starts spamming > > > maintainers with these things to the point that they start ignoring KCSAN > > > reports altogether because of the time they take up. > > > > > > I suppose one thing we could do is to require each new READ_ONCE/WRITE_ONCE > > > to have a comment describing the racy access, a bit like we do for memory > > > barriers. Another possibility would be to use atomic_t more widely if > > > there is genuine concurrency involved. > > > > > > > About READ_ONCE() and WRITE_ONCE(), we will probably need > > > > ADD_ONCE(var, value) for arches that can implement the RMW in a single instruction. > > > > WRITE_ONCE(var, var + value) does not look pretty, and increases register pressure. > > FWIW modern compilers can handle this if we tell them what we are trying to do: > > void foo(int *p, int x) > { > x += __atomic_load_n(p, __ATOMIC_RELAXED); > __atomic_store_n(p, x, __ATOMIC_RELAXED); > } > > $ clang test.c -c -O2 && objdump -d test.o > > 0000000000000000 : > 0: 01 37 add %esi,(%rdi) > 2: c3 retq > > We can have syntactic sugar on top of this of course. An interesting precedent come up in another KCSAN bug report. Namely, it may be reasonable for a compiler to use different optimization heuristics for concurrent and non-concurrent code. Consider there are some legal code transformations, but it's unclear if they are profitable or not. It may be the case that for non-concurrent code the expectation is that it's a profitable transformation, but for concurrent code it is not. So that may be another reason to communicate to compiler what we want to do, rather than trying to trick and play against each other. I've added the concrete example here: https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance