From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76E711E32D6 for ; Thu, 18 Dec 2025 17:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766079310; cv=none; b=qh0hSejMkRItuqI6APKWEupqgRqLUDLLHWyIHF4CSFG0N1FWZmx8xbdx4vqfk6JJI5ApQF1uygWfB3z+6XwOqSmmRe4Eu+iqJsJs5GLQvgfxM7HTVCOkBAx+y59/u9tdwo67W54e8G0mP2dV59zWaTfnsHFP7TNhhALzWJI9kp0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766079310; c=relaxed/simple; bh=wETgaVi/4Srcrvn11NsykVRNrUKwSV1mN9QfsLJ8b7M=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hcPP+NW7ZwcdezU6XgLOpww+xyWfFv/oH9hnzjt9iEp2R8glaC0E+IrOSxWjrrsE7t3JHTRpQtRTFCSnWsI7U+27F3UdFHrZVOjqBOP+kmCdUHnG/+p+s01LbUlmS6mhTTp91MuNZq++E/a9oBrf9t0bOTiHVPTwNOgeuhsGSUs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Vtzb1lnM; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vtzb1lnM" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-b735487129fso158891566b.0 for ; Thu, 18 Dec 2025 09:35:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766079307; x=1766684107; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=CAT4NIB50wSSQJ2eRS7sx9I/iK0vGJNZ4n3UlMbS9UY=; b=Vtzb1lnMdynG5m8o+aRS5PFdrBjY5UWOoP00Z1JkwfNMPzgHUC8bhFGUCUbqzQTeUV STehLc4WXEWVInYmLjIxisgzzQnB/jDvE/rUSWDjMMUrmsfS3xqoy3wpEbhueXhyE5qU c47ZVR3LvS4cGiRELnabWc9c6emvLKNmQ40wSGF1zRZNClCTRRRIDcSyD8ykHGoAlpjJ yXwQ2NkGdOf/a3W8Uz18OPkRNbutxf2WfzI6B1kHUv4MHioDOQqIfB5Gl48ygP/JoqIY AHOcBPOXLV40H78kllmGM1zD+bZNNGYIgSaaTF6WMKmrrG76cDG9Z7MTLniJ9zJtEITL Qbtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766079307; x=1766684107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=CAT4NIB50wSSQJ2eRS7sx9I/iK0vGJNZ4n3UlMbS9UY=; b=ntnvFSjf1Byexj6wkEzn2+sQRQrcl51dMMoRxM+wSns+bh1hVRfyNZeIqn4wFAMnQE HJSaFpXZPqqt+hcWfYFHuhLqlZoWzLc/YmyG3B9czcXAL7yLiTJ0Fj+3M/4xukkuzJu/ LPMbffSny3yijkB2h1zvUaArD9pzBSFdxVVI573ilz/VEwFLlxnu2FhRog2MAldP5SlS Ov/UQS2DEl3pCTAvJ3X9+dN87E+spZrEdLyezxV4k9JHh3p9GOKOWoqZoQVoL06Acs/B t81YEjXMCr7Q7d+5L41IBFe07NMNTcUlUKI+TNAO8tC4fgpkvTIuClBlboYaX/0HMBbY lQcw== X-Forwarded-Encrypted: i=1; AJvYcCWvDBmwThQCzqpoG3dvI8q9Y0Ja3HRbiz3LRUHVgA42AlZb1+gjvTGNSCYXKbq6VK5y6MauFnWS2eGClLs=@vger.kernel.org X-Gm-Message-State: AOJu0YyPz56j6qvk+LtVogcTQGvnA1Pi1eVivMjj6fDnvyQ0EC6ItLrz jS4MP2JhEX7b6Ebwge3KXgc4n+rRWu9oGvvJOAa43XNAS7muSPVmlaFy6BP1Bw== X-Gm-Gg: AY/fxX43mgSMtDVtUvQVrx3Du5WiUL+17G8tzdeMZSIDXw6kl7+L7O+vQ11jlr4bX4k xZSrhU2WU/FA9M++yXxuV7DTlJNpe7hCTmCTqvfrLhF9dTJTqLQc6QZ2Z28ZfO9MO7kmg6oAXtM II0WeoQ5p1gheGw7f+oFggzJ9DSXAhEHdmokZnzF8RLyDj2X7yWXCGbf40+y2MjWXhcJN3PmSZu P5lDE+j/5IovwUm4r2V3Zit8Aj6oy0Rgkb+f9dXj/7LCdYSVXxza6KC2cDYjxcnicXkkrmELwQ4 F41/+QeZSQc7JYPQyOvSejARCXoBXcD5wfeluTWZox5y1BYQShX0nNhmy2IYCKKODg8I+0GqxQa WIbCi7egTBNZ4DDtZkrNsdGhHzGrXVhMOyuVrDeJmNTMc5rQ339kjVJKAZLPv5y/zHpcWlM9mVv jQEPV5SR58VrTImkM2rtAkK/4WYHmGaFIvlPBjJi1oVHOHRetas1Jf X-Google-Smtp-Source: AGHT+IEX3LAgYnT5Lo93smJLzWDPMc1uQ4Aj1gMjYO0ty7+7jdOdeZ5GXQKimSTh9jzRGuiit8vdow== X-Received: by 2002:a05:600d:1b:b0:477:a219:cdb7 with SMTP id 5b1f17b1804b1-47a8fa81e14mr202186375e9.0.1766073295465; Thu, 18 Dec 2025 07:54:55 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47be27b749esm48092275e9.14.2025.12.18.07.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 07:54:55 -0800 (PST) Date: Thu, 18 Dec 2025 15:54:52 +0000 From: David Laight To: Mathieu Desnoyers Cc: Boqun Feng , Joel Fernandes , "Paul E. McKenney" , linux-kernel@vger.kernel.org, Nicholas Piggin , Michael Ellerman , Greg Kroah-Hartman , Sebastian Andrzej Siewior , Will Deacon , Peter Zijlstra , Alan Stern , John Stultz , Neeraj Upadhyay , Linus Torvalds , Andrew Morton , Frederic Weisbecker , 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 , Jonas Oberhauser , rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, Gary Guo , Nikita Popov , llvm@lists.linux.dev Subject: Re: [RFC PATCH v4 1/4] compiler.h: Introduce ptr_eq() to preserve address dependency Message-ID: <20251218155452.6ae47481@pumpkin> In-Reply-To: References: <20251218014531.3793471-1-mathieu.desnoyers@efficios.com> <20251218014531.3793471-2-mathieu.desnoyers@efficios.com> <20251218090313.33923750@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 18 Dec 2025 08:51:02 -0500 Mathieu Desnoyers wrote: > On 2025-12-18 04:03, David Laight wrote: > [...] > >> + * > >> + * The compiler barrier() is ineffective at fixing this issue. It does > >> + * not prevent the compiler CSE from losing the address dependency: > >> + * > >> + * int fct_2_volatile_barriers(void) > >> + * { > >> + * int *a, *b; > >> + * > >> + * do { > >> + * a = READ_ONCE(p); > >> + * asm volatile ("" : : : "memory"); > >> + * b = READ_ONCE(p); > >> + * } while (a != b); > >> + * asm volatile ("" : : : "memory"); <-- barrier() > >> + * return *b; > >> + * } > >> + * > >> + * With gcc 14.2 (arm64): > >> + * > >> + * fct_2_volatile_barriers: > >> + * adrp x0, .LANCHOR0 > >> + * add x0, x0, :lo12:.LANCHOR0 > >> + * .L2: > >> + * ldr x1, [x0] <-- x1 populated by first load. > >> + * ldr x2, [x0] > >> + * cmp x1, x2 > >> + * bne .L2 > >> + * ldr w0, [x1] <-- x1 is used for access which should depend on b. > >> + * ret > >> + * > >> + * On weakly-ordered architectures, this lets CPU speculation use the > >> + * result from the first load to speculate "ldr w0, [x1]" before > >> + * "ldr x2, [x0]". > >> + * Based on the RCU documentation, the control dependency does not > >> + * prevent the CPU from speculating loads. > > > > I'm not sure that example (of something that doesn't work) is really necessary. > > The simple example of, given: > > return a == b ? *a : 0; > > the generated code might speculatively dereference 'b' (not a) before returning > > zero when the pointers are different. > > In the past discussion that led to this new API, AFAIU, Linus made it > clear that this counter example needs to be in a comment: I might remember that... But if you read the proposed comment it starts looking like an example. It is also very long for the file it is in - even if clearly marked as why the same effect can't be achieved with barrier(). Maybe the long gory comment belongs in the rst file? I do wonder if some places need this: #define OPTIMISER_HIDE_VAL(x) ({ auto _x = x; OPTIMISER_HIDE_VAR(_x); _x; }) Then you could do: #define ptr_eq(x, y) (OPTIMISER_HIDE_VAL(x) == OPTIMISER_HIDE_VAL(y)) which includes the check that the pointers are the same type. But it would be more generally useful for hiding constants from the optimiser. David > > https://lore.kernel.org/lkml/CAHk-=wgBgh5U+dyNaN=+XCdcm2OmgSRbcH4Vbtk8i5ZDGwStSA@mail.gmail.com/ > > This counter-example is what convinced him that this addresses a real > issue. > > Thanks, > > Mathieu > >