From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC0991885A4; Mon, 30 Sep 2024 09:33:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.154 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688831; cv=none; b=RvEQ3qDNLD30FQBM9e9sU7gpsKUQUX5Fupe5CvnakD+0Vjrd13yKqD0e27EIj19AqjGzYNwp14vgSHJxxrc0O1g4U7NSF+Tr0ORT8UVYG6yuwcZIXwv40mY/UzbQpbTCu+qlL3Bjn2I7yXX2lUZwxEgNeW84MhNhTYWkh/SfbTc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688831; c=relaxed/simple; bh=ttw3/7AfZzhAFU54uuXkNQKKqdfEwEj/HVqjJllteuo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=VBbET7PbzPe/9TY/AO8R4r9X367ukgSWGR0W5xld6CiNSO0EcwpDQIV25z+S4wiAS6QHQsGQoOSVPewk9NBSBIA180aSI0rhSazt6qTgt1PziiYoMiPgN8uW//F5d11JLCfVI/2bNnFxSA3V+9ZUHRJU3x9+fvySNy5P5ZG1+as= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=14.137.139.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.18.186.29]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4XHFbP2LCDz9v7JS; Mon, 30 Sep 2024 17:08:01 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.27]) by mail.maildlp.com (Postfix) with ESMTP id 8CCC71404F6; Mon, 30 Sep 2024 17:33:45 +0800 (CST) Received: from [10.81.211.60] (unknown [10.81.211.60]) by APP2 (Coremail) with SMTP id GxC2BwD3psdocPpmCvLwAQ--.5053S2; Mon, 30 Sep 2024 10:33:44 +0100 (CET) Message-ID: Date: Mon, 30 Sep 2024 11:33:26 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Cc: Mathieu Desnoyers , 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> <8d20cf79-9fa5-4ced-aa91-232ccd545b59@huaweicloud.com> From: Jonas Oberhauser In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:GxC2BwD3psdocPpmCvLwAQ--.5053S2 X-Coremail-Antispam: 1UD129KBjvJXoWxJr1kKw48GFyUtrWxCFy3Jwb_yoW8Ar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M schrieb Alan Huang: > 2024年9月30日 17:15,Alan Huang 写道: >> >> 2024年9月30日 16:57,Jonas Oberhauser 写道: >>> >>> >>> >>> Am 9/29/2024 um 12:26 AM schrieb Alan Huang: >>>> 2024年9月28日 23:55,Mathieu Desnoyers wrote: >>>>> >>>>> 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. >> >> After barrier_data(&b), *b will be loaded from memory, you mean even if *b is loaded from memory, the address dependency goes to the wrong load still? > > Sorry, *b should b. That's exactly what I meant to say. In my understanding, it can happen like this: a = READ_ONCE(*p); ... b = READ_ONCE(*p); if (a == b) { b = a; // inserted by compiler barrier_data(&b); foo(*b); // compiler definitely use the current value in b } In the end, the address dependency is from the first load, and the CPU can speculatively (with register renaming, forwarding etc) execute a = READ_ONCE(*p); b2 = a; // speculatively tmp = load *b2 // speculatively b1 = READ_ONCE(*p); if (a == b1) { // confirmed foo(tmp); } best wishes, jonas