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 DE981CF34AD for ; Thu, 3 Oct 2024 13:25:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21F066B0284; Thu, 3 Oct 2024 09:25:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1AB3A6B028F; Thu, 3 Oct 2024 09:25:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E797D6B0284; Thu, 3 Oct 2024 09:25:39 -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 C9CB46B0246 for ; Thu, 3 Oct 2024 09:25:39 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6D3A8C1543 for ; Thu, 3 Oct 2024 13:25:39 +0000 (UTC) X-FDA: 82632363198.23.67CC40E Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf12.hostedemail.com (Postfix) with ESMTP id 8BA8340012 for ; Thu, 3 Oct 2024 13:25:36 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=wP35DN2E; spf=pass (imf12.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=1727961765; 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=FqV0g+j9Y/7PUHVNRBVtOeQYqUcybq/xcPImnSckTFE=; b=Po415equWnHNO3LYhMbjZQbbMucl0Q74/nxKYuUu6x6QnqO1prEy2RHkgFDCYcp1FKxzpm J9/jTDpkmdMPWQEdir99hZ/dAF2Dg3sDbIUF+96lrjP+l4PUGA5JVW+LLLqmQ7boi+jX8T +/oDCbRoeTMHzaVmPjv+qM+/O90OQ0A= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=wP35DN2E; spf=pass (imf12.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=1727961765; a=rsa-sha256; cv=none; b=Q7076yECjXVHSMKOFwr5ypxe/ZDAj6L6SKcCr28y3zwtNHkeaBIjDJc4TMDZNaiY8AkepL mnqVkKU1B8u6DlRPdk+u9A+sPJuLogLFzQwIDkFV74z1vcoQl2uwCTqSXRq4PnXRA3XDkS EFkzOiAQkfh5n2qZNvAvvl/Wv38zy/4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727961933; bh=0Bq3tDtnRpX1pbJsNPejO+Bs7zMzKXxBWm7bWQUcwiE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=wP35DN2EtpOm07PoWCz0T0mMaUcxto2ytWDs1yziiOGWC+j+FxhxOq3eC1qy+b9q+ qLO1bAlnX0N0Zlw1ZGvD7iJMGAVGLmou2zQdfOP+pWMZ4h+xNjWqdvt3jRp/enzwQk tofficLJX+6zWFrzrnXzjz0qiwslONzUNtSt9nQyznJ6Kp3/nJlxI0jmloXbEAzF1S 0cCdDSKfA/epuSKCZuuT0eGajBd0gPVIJsWlRUiN5aVMbhR58+K6sTuUyNOwS3R8Z8 tj16vbvLgKlkiEk7Y4D2WThHi3pO1STu+DDdJHFH3vL30MOwt/bNO6KWSxegxqIWhT 7Z6AB4hAYPUcg== Received: from [172.16.0.134] (96-127-217-162.qc.cable.ebox.net [96.127.217.162]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XKC992Gdzz4sn; Thu, 3 Oct 2024 09:25:33 -0400 (EDT) Message-ID: Date: Thu, 3 Oct 2024 09:23:31 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency To: 'Alan Stern' , David Laight Cc: Jonas Oberhauser , Linus Torvalds , "linux-kernel@vger.kernel.org" , 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 , Steven Rostedt , Lai Jiangshan , Zqiang , Ingo Molnar , Waiman Long , Mark Rutland , Thomas Gleixner , Vlastimil Babka , "maged.michael@gmail.com" , Mateusz Guzik , Gary Guo , "rcu@vger.kernel.org" , "linux-mm@kvack.org" , "lkmm@lists.linux.dev" References: <02c63e79-ec8c-4d6a-9fcf-75f0e67ea242@rowland.harvard.edu> <9539c551-5c91-42db-8ac1-cff1d6d7c293@huaweicloud.com> <2cdda043-1ad9-40cf-a157-0c16a0ffb046@rowland.harvard.edu> <5d7d8a59-57f5-4125-95bb-fda9c193b9cf@huaweicloud.com> <82e97ad5-17ad-418d-8791-22297acc7af4@rowland.harvard.edu> <2b1caba3-48fa-43b9-bd44-cf60b9a141d7@rowland.harvard.edu> <22638e2fe1274eb0834fa3e43b44184e@AcuMS.aculab.com> <68dc00b3-1ca1-42bc-8f1e-78ace10e4d64@rowland.harvard.edu> From: Mathieu Desnoyers Content-Language: en-US In-Reply-To: <68dc00b3-1ca1-42bc-8f1e-78ace10e4d64@rowland.harvard.edu> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 8BA8340012 X-Stat-Signature: r4oyfybicxnx117r97jzaihuc61g56hk X-Rspam-User: X-HE-Tag: 1727961936-609502 X-HE-Meta: U2FsdGVkX1+23Vhv1Ym3P3evS+B0R8mSvfPVkTFn+D6lkt5lmHxyMn79DqAXuSqxnkP9v1nXaR7q3jw9mlHSba9NQIauwUTK8uCjv1ZfgHp3GExIs7NqHiJt3udVxyFgFUEFDtPEYRl++KWLOuL0TZQZGR582hScLzXDlgHwLb9ErhdkIU/01vpsCWgJCp9dP4lI0MdVrPG7F26F+APswz4wXQExTDeEmMFWqBu4DCLBrNElxS8RvpzeLcSiIDpRGgFyCOTWXMIMvoxpKuqAzzjg18Y/ST0OMW3s3OA81W9dUn9VfMO5vnDfk/kAFmv8+PGnTrgt+WfMNglvP2Ri0L9JQBRLXX3+lZJMLcEk0QrSSWKmFQ4fxnFW5LDfsH/C8Ql3owqbQPHI2wIjxSXwtB4+sovPsea5KFSNRsbNeueo1Pgd3kvKg7qnGeLEMtv4xa8S0WDgkUquhKFMJNDfeqS2bfmCu7xg3d8gKmh06fnup68Z00uTvJIrY7bbfaG9d1OU/4jy6Fp6y7P08XhfHK8rc0ZRmayqI6QNvxRwYEZB3yKVQ28lyd+4aV8yjR18LY4AN0uDKuOjDGrw6SRn9DuJD07vFYf5gl1u4PKlDVZwNNwRBhcYpclXA5tfwBZNlXQmUalsyaNLb3qooGh9me2zifzig8ALZVhZCBra5cITZLZkduH7R80Rxq3WzgntNCR58prvDV/XZm+nKrq3lCC30CnlzWHZ4uK+WWZ9FOCQk9bQUq0BPKaY9vCs/DlUQhVHrxARmi26F1kyL8jICxy4ihwRWQXli6XKZDEvhMjhwEQlLJnYysARW/7Ej1po0T/yHdlkJDFmmmiWLrMZWKGyn/k7kgnLvZqfTzGFQ86kkS9v8fNtW1voohxiem9FSINXFVUkMNA5vcf8lqKJbVpY/Vqvk2Bb2Pgq2sOLoiVOnzGt/tEQQtqTVZ09g9zp6KSWEf/NoEaA921a30z g8frcPax uY8NZxYcHTNYtUo42fPD1fW3sCT7h4xZFvVaJoLH1pcmaFc5DZZtf+Yv8PCujgqWay8Xhi+cl0vMl9jGJB+iNxiqRUSLkyIqdJbNxb3yOC8uVc7vUNTQtI+8R/K1T8YdAZKO6vuE3sQD8waBSQYbcy4uonZdTHTyUAp0/kN3t+a5Hl7C6pPeM0ErRyK53xavsIcDkqZoSzTSSqN9sIWGGTx86rAZ9uI1eeGgJhqPmY3te6kaDiMFUgCmqwEg8BBeWUXs04JpPZIV5ArkC5RPHxNBV0YZtA8LC1tgdxBxweqNdUmJGG0kDGU9AFOab/d68BUzg4WNb+93w68OKqsS+3FnpzGiD1bvjFL0d6fbIPOK7OqkVGIe2UYRBWMa/rvRXvCX0+oOtz3n2xpY/ZA36M9pKGgu+p2Hkb6oemlq6BgqrTOKEUydR2EMVCFPA5/qzi8qmkqwOhxpjEXq5nmI8/0Q6Td2IfMI8vQRmDNws/x80MXxwxyw+lWizIFBZIM9UVB/pTyZpUKo+6BBbmMS7AyY0m5xUAdCHH29gfdmTPOLU7onC5HD012A+ig== 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-10-03 03:50, 'Alan Stern' wrote: > On Wed, Oct 02, 2024 at 03:24:45PM +0000, David Laight wrote: >> I think I know what you are trying to do, and you just fail. >> Whether something can work is another matter, but that code >> can't ever work. >> >> Inside if (a == b) the compiler will always use the same register >> for references to a and b - because it knows they have the same value. > > According to the other people in this discussion who have actually tried > using this code, it _does_ work (at least some of the time). > > However, I'm not one of those people and so I leave it up to them to > decide how to respond to this critique. I suspect that David's comment is about this specific example that was given in this leg of the email thread: https://lore.kernel.org/lkml/5d7d8a59-57f5-4125-95bb-fda9c193b9cf@huaweicloud.com/ > > > > > int fct_hide(void) > > > > > > { > > > > > > int *a, *b; > > > > > > > > > > > > do { > > > > > > a = READ_ONCE(p); > > > > > > asm volatile ("" : : : "memory"); > > > > > > b = READ_ONCE(p); > > > > > > } while (a != b); > > > > > > OPTIMIZER_HIDE_VAR(b); > > > > > > return *b; > > > > > > } This indeed cannot work because the hide var is done on @b after it was compared with @a, so after the compiler was free to use any of the registers due to the equality. Another example that does *not* work is if we try to hide vars on the inputs of the equality, and then proceed to do the comparison on the resulting temporaries, e.g.: int fct_hide(void) { int *a, *b; do { a = READ_ONCE(p); asm volatile ("" : : : "memory"); b = READ_ONCE(p); } while (OPTIMIZER_HIDE_VAR(a) != OPTIMIZER_HIDE_VAR(b)); return *b; } The reason why this does *not* work is because the compiler is free to use either temporaries for *b at the end, because they were deemed identical. What _does_ work however are the following two approaches: 1) Perform the equality check on the original variables, creating new versions (with OPTIMIZER_HIDE_VAR) of both variables for the rest of their use, therefore making sure the pointer dereference are not derived from versions of the variables which were compared with another pointer. (as suggested by Boqun) 2) Perform the equality check on the versions resulting of hiding both variables, making sure those versions of the variables are not dereferenced afterwards. (as suggested by Linus) Thanks, Mathieu > > Alan > >> Possibly something like: >> c = b; >> OPTIMISER_HIDE_VAR(c); >> if (a == c) { >> *b >> will ensure that there isn't a speculative load from *a. >> You'll get at least one register-register move - but they are safe. >> Otherwise you'll need to put the condition inside an asm block. >> >> David -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com