Path: csiph.com!aioe.org!bofh.it!news.nic.it!robomod From: Sergey Fedorov Newsgroups: linux.kernel Subject: Documentation/memory-barriers.txt: How can READ_ONCE() and WRITE_ONCE() provide cache coherence? Date: Fri, 26 Feb 2016 22:20:01 +0100 Message-ID: Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=to:from:subject:cc:message-id:date:user-agent:mime-version :content-transfer-encoding; bh=OxHfjsWLjEaEudQQCnXDVUE4fii4oO5HYsQrVEQMTaY=; b=nPCUVcRM9FpItYd+rmlJ+oTkVZesi1k3kBPTNoxufRuTv5/TD+qhQLfBX7+W1/pwiQ yXmUv9il7cjkbgmncbmMENczg3ih3yrhTOMwGcqRTe1k8XpNFtXMB3oA8/N9cuQfIMGk sfDq+R5FAj/aPWw7NJR/RbMXVbFVbaNqOrO7i2zDE3iCxWbG1CC4+e1d/GQLfYaB//8C Npf7Oq5TrtZWHnOzn3iAYh/mgXL+7+2FeY2SKHL+MOH+5HTuJNxKY5x3MmJ6mlED8wQm sMKPHfSD+Qbgk8iftC+qFbxFcEWW6QHJvKwJ5yEFTaBITtGlRKN+3yXJCPTR0nRpW2aB BQCw== X-Google-Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:cc:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=OxHfjsWLjEaEudQQCnXDVUE4fii4oO5HYsQrVEQMTaY=; b=ZkUta2H0rjCvC852wSRbDtDgjbSnqM/ylZr2kRFOGN5hdT2U3wXVK4qejq7FHOo5v5 sJCnJMbjiTwXJUbhUp32JYuL2UxEl7taOX6CPCiuZ/rpaER/ANEbzru7YvnxF+u0RCQ4 g9mlNqLX1MWN3jJtPcPpT3ICq6F8TgLByQChszXEJqheIr3iGR7GWow6dZoF43mK3VB0 DgXqjDxHtkPwF3zEsEvE0QyC9fg1f4C5sfYA8uR8A4gx27APUoD5cINB8O3Lrve3Rfgf JVipIB6uBdSUTTqVZ3Yrejb8F+hbN6okZgY2tKlXeCPF+mrRiExd2swuFMvVDnDz0A0t MdbQ== X-Gm-Message-State: AD7BkJLQGRaV2bQDOsy6MxLjXrZWISqhoM/10iW7P76G1xaRMUk8xl6gfxJpcg95RRa5/w== X-Received: by 10.25.150.84 with SMTP id y81mr1472950lfd.56.1456521263504; Fri, 26 Feb 2016 13:14:23 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: robomod@news.nic.it List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Approved: robomod@news.nic.it Lines: 32 Organization: linux.* mail to news gateway X-Original-Cc: "Paul E. McKenney" X-Original-Date: Sat, 27 Feb 2016 00:14:21 +0300 X-Original-Message-ID: <56D0C02D.6000905@gmail.com> X-Original-Sender: linux-kernel-owner@vger.kernel.org Xref: csiph.com linux.kernel:1344684 Hi, I just can't understand how this kind of compiler barrier macros may provide any form of cache coherence. Sure, such kind of compiler barrier is necessary to "reliably" access a variable from multiple CPUs. But why it is stated that these macros *provide* cache coherence? From Documentation/memory-barriers.txt: > The READ_ONCE() and WRITE_ONCE() functions can prevent any number of > optimizations that, while perfectly safe in single-threaded code, can > be fatal in concurrent code. Here are some examples of these sorts > of optimizations: > > (*) The compiler is within its rights to reorder loads and stores > to the same variable, and in some cases, the CPU is within its > rights to reorder loads to the same variable. This means that > the following code: > > a[0] = x; > a[1] = x; > > Might result in an older value of x stored in a[1] than in a[0]. > Prevent both the compiler and the CPU from doing this as follows: > > a[0] = READ_ONCE(x); > a[1] = READ_ONCE(x); > > In short, READ_ONCE() and WRITE_ONCE() provide cache coherence for > accesses from multiple CPUs to a single variable. Thanks, Sergey