From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DD8ACF649D for ; Mon, 30 Sep 2024 08:58:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 069188001E; Mon, 30 Sep 2024 04:58:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 017C880017; Mon, 30 Sep 2024 04:58:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D86F38001E; Mon, 30 Sep 2024 04:58:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B61CA80017 for ; Mon, 30 Sep 2024 04:58:16 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3177A8098B for ; Mon, 30 Sep 2024 08:58:16 +0000 (UTC) X-FDA: 82620802992.27.CD15606 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by imf02.hostedemail.com (Postfix) with ESMTP id 421D080007 for ; Mon, 30 Sep 2024 08:58:12 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of jonas.oberhauser@huaweicloud.com designates 14.137.139.23 as permitted sender) smtp.mailfrom=jonas.oberhauser@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727686630; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7FIiTC1qVOHqJkQgp7oCbotzu4G1sYgU0kT07OLRETQ=; b=dR9A7yU5g66+Z5H/3JMKr655cRThXOLWy3vI64q+HPMupI6yVBrv2eJapN2TGT1izMYq0K 1SAqP/x0C5ZMRnIkPPRp8VeGCWgF+oUiH+2jkCVQd2rPzHglaz5KmKsiMHBhGCgH+kVOal O61j8KHx9eagA8Lc9+KQ+NCwp90VbVc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of jonas.oberhauser@huaweicloud.com designates 14.137.139.23 as permitted sender) smtp.mailfrom=jonas.oberhauser@huaweicloud.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727686631; a=rsa-sha256; cv=none; b=huLTE2hcb+OvC0JW9j/G1tnEiR0Db9gjwavL2vBc5Ghi08gl9GOFDIHP6yVoFVtkhFg+4m 1RAGZwCEd6HzRGPjutcLfx9owCZlZ25JBuuc7EOs7K3Jd0kmoR9J6fXL4Xl87ArJjShJ4H 53fSYg8+Sz2uhepd1Fdrn53kBldaWtc= Received: from mail.maildlp.com (unknown [172.18.186.29]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4XHDx22wFLz9v7Hk for ; Mon, 30 Sep 2024 16:38:14 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.27]) by mail.maildlp.com (Postfix) with ESMTP id BF07E140391 for ; Mon, 30 Sep 2024 16:57:56 +0800 (CST) Received: from [10.81.211.60] (unknown [10.81.211.60]) by APP2 (Coremail) with SMTP id GxC2BwCXRscEaPpmNIbwAQ--.5120S2; Mon, 30 Sep 2024 09:57:56 +0100 (CET) Message-ID: <8d20cf79-9fa5-4ced-aa91-232ccd545b59@huaweicloud.com> Date: Mon, 30 Sep 2024 10:57:37 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency To: Alan Huang , Mathieu Desnoyers Cc: Alan Stern , Linus Torvalds , LKML , Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , Peter Zijlstra , Boqun Feng , John Stultz , Neeraj upadhyay , Frederic Weisbecker , Joel Fernandes , Josh Triplett , "Uladzislau Rezki (Sony)" , Steven Rostedt , Lai Jiangshan , Zqiang , Ingo Molnar , Waiman Long , Mark Rutland , Thomas Gleixner , Vlastimil Babka , maged.michael@gmail.com, Mateusz Guzik , Gary Guo , RCU , linux-mm@kvack.org, lkmm@lists.linux.dev References: <20240928135128.991110-1-mathieu.desnoyers@efficios.com> <20240928135128.991110-2-mathieu.desnoyers@efficios.com> <02c63e79-ec8c-4d6a-9fcf-75f0e67ea242@rowland.harvard.edu> <2091628c-2d96-4492-99d9-0f6a61b08d1d@efficios.com> From: Jonas Oberhauser In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:GxC2BwCXRscEaPpmNIbwAQ--.5120S2 X-Coremail-Antispam: 1UD129KBjvJXoWxZFyrGr4xCFykJF4Duw45KFg_yoW5AF4DpF W7Ka17KF4kJF1akr90y348uFy5trn7tFyYv3Z5tr1xCws0gF1fZr43tFyYkasxCwn7t34j vr1Yv3sIvasxAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxV AFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AF wI0_GFv_Wryl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4 xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5 MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I 0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWU JVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUIF 4iUUUUU X-CM-SenderInfo: 5mrqt2oorev25kdx2v3u6k3tpzhluzxrxghudrp/ X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 421D080007 X-Stat-Signature: s6ch1e1kr4qe7xk3eu54cszgt8si1qk4 X-HE-Tag: 1727686692-7497 X-HE-Meta: U2FsdGVkX19PoXOQVDKoZopbTf64mnxSbhK3uaTBgWXGgtV6+sIBgSE2QOvwNrhMYkul/Tu7kVmyb2MEenMw2hbV0EumpCjBl6QwI91tfa/tn2+H7NLqDBolcNtOUCpQWkGyCtIKuWhxAecJYbRQUA0hqE81vuOaMZvDFRpujNCG20hKGblAy7Yn6t0b7GDj92BusRk/sKy1t0pLqVdMkmj8lkB7JuIKS5yZIuNnQEC7UjyM7v0S6rTZm+Hmh2XK6fJDVB5VajNREoC3/PpqaAElUKrATqzi+/Q3GxOG9LqgZFYY5eTC7lX3/66NgTNCvY9KAGdoHKzZIr2nTamwQNUeLQlvXzTVxucbld//5CqTuKNj7jiYch56U3Kggu5zCeDbU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ence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Am 9/29/2024 um 12:26 AM schrieb Alan Huang: > 2024年9月28日 23:55,Mathieu Desnoyers wrote: >> >> On 2024-09-28 17:49, Alan Stern wrote: >>> On Sat, Sep 28, 2024 at 11:32:18AM -0400, Mathieu Desnoyers wrote: >>>> On 2024-09-28 16:49, Alan Stern wrote: >>>>> On Sat, Sep 28, 2024 at 09:51:27AM -0400, Mathieu Desnoyers wrote: >>>>>> equality, which does not preserve address dependencies and allows the >>>>>> following misordering speculations: >>>>>> >>>>>> - If @b is a constant, the compiler can issue the loads which depend >>>>>> on @a before loading @a. >>>>>> - If @b is a register populated by a prior load, weakly-ordered >>>>>> CPUs can speculate loads which depend on @a before loading @a. >>>>> >>>>> It shouldn't matter whether @a and @b are constants, registers, or >>>>> anything else. All that matters is that the compiler uses the wrong >>>>> one, which allows weakly ordered CPUs to speculate loads you wouldn't >>>>> expect it to, based on the source code alone. >>>> >>>> I only partially agree here. >>>> >>>> On weakly-ordered architectures, indeed we don't care whether the >>>> issue is caused by the compiler reordering the code (constant) >>>> or the CPU speculating the load (registers). >>>> >>>> However, on strongly-ordered architectures, AFAIU, only the constant >>>> case is problematic (compiler reordering the dependent load), because >>> I thought you were trying to prevent the compiler from using one pointer >>> instead of the other, not trying to prevent it from reordering anything. >>> Isn't this the point the documentation wants to get across when it says >>> that comparing pointers can be dangerous? >> >> The motivation for introducing ptr_eq() is indeed because the >> compiler barrier is not sufficient to prevent the compiler from >> using one pointer instead of the other. > > barrier_data(&b) prevents that. I don't think one barrier_data can garantuee preventing this, because right after doing the comparison, the compiler still could do b=a. In that case you would be guaranteed to use the value in b, but that value is not the value loaded into b originally but rather the value loaded into a, and hence your address dependency goes to the wrong load still. However, doing barrier_data(&b); if (a == b) { barrier(); foo(*b); } might maybe prevent it, because after the address of b is escaped, the compiler might no longer be allowed to just do b=a;, but I'm not sure if that is completely correct, since the compiler knows b==a and no other thread can be concurrently modifying a or b. Therefore, given that the compiler knows the hardware, it might know that assigning b=a would not cause any race-related issues even if another thread was reading b concurrently. Finally, it may be only a combination of barrier_data and making b volatile could be guaranteed to solve the issue, but the code will be very obscure compared to using ptr_eq. jonas