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 B7B27CDD1B8 for ; Fri, 27 Sep 2024 15:24:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A0796B0101; Fri, 27 Sep 2024 11:24:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 451486B0102; Fri, 27 Sep 2024 11:24:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F16D6B0103; Fri, 27 Sep 2024 11:24:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0CDF66B0101 for ; Fri, 27 Sep 2024 11:24:17 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AD080A0FE7 for ; Fri, 27 Sep 2024 15:24:16 +0000 (UTC) X-FDA: 82610889312.30.81BE710 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf10.hostedemail.com (Postfix) with ESMTP id D995DC0010 for ; Fri, 27 Sep 2024 15:24:13 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=UMjDRB8g; spf=pass (imf10.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727450617; 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:dkim-signature; bh=14vkOn3K2RjPdrNteHJ3rWXvuepbsJWpb9HZnrrycMc=; b=hatLRYfuGUPi8/ljyQrzAay5j0PGkot3+VlqCwt3GQEggHLL4WXwoZz2gj+L/8EdIG+8iv b/VeEAzhclM9w/agqJVb2hkrbeCcPiT70pMXoCI+eRRv3HqCxtQv3mXeP02Ocd2+LEooiC 3eG4lkIRt6WGajyLwfao8UhUGWhJ8Ek= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=UMjDRB8g; spf=pass (imf10.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727450617; a=rsa-sha256; cv=none; b=zQThF68Hbpxl3UlTdtibJFF8/PC9USj9uwJ9Yt0p8UgBh2y4z/6WGxev+I2HOtFxe/YOP2 qM2Q98V7g5gpGcsYQzFjBnK2BZBp1qfvR8mBooZeh2Kpq1eu0YAISg/0PPgAzwsK5KbBCY 2eL2o6H4x1q8FOgz30kfOvTl0b2OnSU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727450652; bh=tFhKGedYSLt2FB/2OyeFSMYVEXgh7bqsTO9vhWhnYQc=; h=Date:Subject:From:To:Cc:References:In-Reply-To:From; b=UMjDRB8g9Od4LQQDbOo2DhG9D9mt3ujSTm1Ik0H31/GWwOyoPmPLGh1scv+CqygLK E8qlAlChVX4SZ9rvHVOUsmp57qGTWaoFB4FZCC4eZRxA40es1W4kbDic6DdClNcRov fcwV4IM9Sxib2xf5gJi10EDbShKkwu16sIoVh6FcXEXMffg4tktJW+jIZ7CGdd6o9s YsOjBQJWIMXqc4STmXaotKTCwsxgvsi/oNKbU3nsGHYdCtLRYZQN8iXYIAq6HVSNSu ZXRYhVNzekv5rasLIttlSdN5KJJ5x4fgTLuI7gjlYdCO7vOEy2V4VS9OJEIHnonFXx Qt0fNe0ndqlmg== Received: from [IPV6:2606:6d00:100:4000:cacb:9855:de1f:ded2] (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XFZ4r3svSz1PNM; Fri, 27 Sep 2024 11:24:12 -0400 (EDT) Message-ID: Date: Fri, 27 Sep 2024 11:22:07 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 1/4] hazptr: Add initial implementation of hazard pointers From: Mathieu Desnoyers To: Boqun Feng , Linus Torvalds Cc: Jonas Oberhauser , linux-kernel@vger.kernel.org, rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , "Uladzislau Rezki (Sony)" , rostedt , Lai Jiangshan , Zqiang , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Mark Rutland , Thomas Gleixner , Kent Overstreet , Vlastimil Babka , maged.michael@gmail.com, Neeraj Upadhyay References: <48992c9f-6c61-4716-977c-66e946adb399@efficios.com> <2b2aea37-06fe-40cb-8458-9408406ebda6@efficios.com> <55633835-242c-4d7f-875b-24b16f17939c@huaweicloud.com> <54487a36-f74c-46c3-aed7-fc86eaaa9ca2@huaweicloud.com> <0b262fe5-2fc5-478d-bf66-f208723238d5@efficios.com> <8aceaf4f-5578-4fca-8be7-3448d7b89721@efficios.com> <08423acb-1f8e-407a-80fe-c424c6ee2861@efficios.com> Content-Language: en-US In-Reply-To: <08423acb-1f8e-407a-80fe-c424c6ee2861@efficios.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: isdqdnoqeuoq3frsdzxd44n5nd9aeti8 X-Rspamd-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lM8tqwr7nP07UC4r2+2xi66W/wkM1970rHOHu7fRbxKpRq4u9Dm5i24P3TcJDJpatI/IOcloypN7Vy4/jsQr1cehoBx40rDnAo2j6jX7uXk+W+WSxbSqskincw8YHJ85wvI7Uwmee7MN8n+Qd1W7k1jlK4j1K4dAZTyKuN7bvBOjihTPfE7pJeRMt9tcTh6Jkujk7beSucmsQQaVHbchsXHwgYqrMXGmTKnAr+EGJAmNCYNTHW+U3Iof6NV4Ll99KjFy7yDxJNm/4lTliw4xlV3RQMk/BrL2A2uAPYvs2CUe/Vd7sA0zUf+qJywj5fceQl//X5pkFid2/9Dc8VYKPnhOudiORYiJXAgwTa50Tzn6L24amE+wv9QnLaiE54t8swNIjqSmNW0gwYAqVCMUd1UugauiWF8UrvtnpwK7KCMtd56g3rsN49Tx9T3lZf X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2024-09-27 16:43, Mathieu Desnoyers wrote: > On 2024-09-27 12:59, Mathieu Desnoyers wrote: >> On 2024-09-27 06:28, Boqun Feng wrote: > [...] >>> I replaced ADDRESS_EQ(a, b) with ADDRESS_EQ(b, a), and the compile >>> result shows it can prevent the issue: >> >> I see, yes. It prevents the issue by making the compiler create >> a copy of the value "modified" by the asm before doing the equality >> comparison. >> >> This means the compiler cannot derive the value for b from the first >> load when b is used after after the equality comparison. >> >> The only downside of OPTIMIZER_HIDE_VAR() is that it adds an extra >> "mov" instruction to move the content across registers. I don't think >> it matters performance wise though, so that solution is appealing >> because it is arch-agnostic. >> >> One small improvement over your proposed solution would be to apply >> OPTIMIZER_HIDE_VAR() on both inputs. Because this is not a volatile >> asm, it is simply optimized away if var1 or var2 is unused following >> the equality comparison. It is more convenient to prevent replacement >> of both addresses being compared by the other rather than providing >> the guarantee only on a single parameter: > > Actually, your approach is better (only preserving the address > dependency on the first parameter), because it allows the second > parameter to be a constant. > > Here is a diff. Please let me know if I need to improve anything wrt > comments or implementation: > > diff --git a/include/linux/compiler.h b/include/linux/compiler.h > index 2df665fa2964..52434eccd715 100644 > --- a/include/linux/compiler.h > +++ b/include/linux/compiler.h > @@ -186,6 +186,32 @@ void ftrace_likely_update(struct ftrace_likely_data > *f, int val, >      __asm__ ("" : "=r" (var) : "0" (var)) >  #endif > > +/* > + * Compare an address with an expression while preserving the address > + * dependencies for later use of the address. It should be used when > + * comparing an address returned by rcu_dereference() with another > + * address (either constant or in registers). > + * > + * This is needed to prevent the compiler SSA GVN optimization pass from > + * replacing the register holding @addr by @expr (either a constant or a Actually, I've done a bit on testing on godbolt, and it appears that disabling this specific optimization makes the problem disappear: -fcse-follow-jumps (by use of -fno-cse-follow-jumps). I will update this comment to state that both CSE and SSA GVN optimizations can cause issues there. Thanks, Mathieu > + * register) based on their equality, which does not preserve address > + * dependencies and allows the following misordering speculations: > + * > + * - If @expr is a constant, the compiler can issue the loads which depend > + *   on @addr before the load of @addr. > + * - If @expr is a register populated by a prior load, weakly-ordered > + *   CPUs can speculate loads which depend on @addr before the load of the > + *   address they depend on. > + */ > +#ifndef ADDRESS_EQ > +#define ADDRESS_EQ(addr, expr)                    \ > +    ({                            \ > +        bool __res = (addr) == (expr);            \ > +        OPTIMIZER_HIDE_VAR(addr);            \ > +        __res;                        \ > +    }) > +#endif > + >  #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), > __COUNTER__) > >  /** > -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com