From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 A9B8A25DB12 for ; Thu, 18 Dec 2025 09:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766048600; cv=none; b=SWttFlMWnaMqE7mcJ/bdHeDIm6nlRod5JKnIeIc3oV1nA5/PU0S8Huf/OUVIy4VBejIDTOJVtntcp1OMv1xLIJz0yArXsieNku/Bk32Mc8TgnD76D1BCHJGtn++Gn2GMcmrXAr1pGCcE91Tj5avm7eVXbelaZPfUKKGcvfnfDyc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766048600; c=relaxed/simple; bh=aeYoCOYIi3+P5KIBHHFXDn7aAVa7SVhn6e3fXFtEleU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Is1sOAU/j7LaNhbbz79MhPLvQ6l0PJgSA71NGMW+gVRh2mMnDqJwU/alFAR52gHQh9HrQVAfadKunfoQhke5AgWQ9PGl4541aeJRXyfMK8G736r4q1fjlE99wGr3VmnpCFm6bSclSRsq6lvkntGJHIcbpR7Mn2cZooSiM7UCCGg= 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=gIyhlhzh; arc=none smtp.client-ip=209.85.221.51 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="gIyhlhzh" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-42b3d7c1321so183271f8f.3 for ; Thu, 18 Dec 2025 01:03:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766048597; x=1766653397; 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=RDxH7qSfMcvkHLsC5apj9tDI0YXojM/cFgm0fm580dk=; b=gIyhlhzhiL6WNzZFk1040BikAMBATrYt4mgBbBhgjAOGoPgElYEEqUiueeISXE/hYM Me1Hsjqq99kMssu10EfZ4w3GA2oyVR749fcrYRXqMZD36+L5azrLerq3pNsj/FnYs1EC Ji/kzXOES2x6IQKAJoYmAUOK0x70SYWSHLH+dTGNbbXpmPR6SQ0cXMeMG2xmyfv5c5/m 2cW5Z5FH6EHizBvBJNpJSPom9IubV+wHx9Km10wPzawwkiCY2VWFXEbjLsyn/Is4w/JE k8Nj+3K9F86u0syIJg4bqfTGC8HLS5w9OJJCB9IT3leWgnKnFMLbam5KzKVgtz3FLBFq lBXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766048597; x=1766653397; 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=RDxH7qSfMcvkHLsC5apj9tDI0YXojM/cFgm0fm580dk=; b=oimZDfZOwlHlRTp6DMKBd6SzbOv4+ZUbRi9zBlm/+xUhaKRMlKbwnmyw6Ws7mtHArI MbiBRx61sFeHgrjNOc0A8fCgWBsrfAifzLz/ATPiOs8ToEM6vMO89EIje2y6pzIs6H5R WFbsTYmJid6O1Cqu3i/XXnw1Q7gWj3qkoArSFKPHGgtqs77YmsgfSN+JOUDooBBl2RC7 Bk5M6NAkhLaKFRJ7SgzsXJVeL4Vg6rloeXF3DCjj6BCijmjMShR0vsHR+9OhkZQjG8QD bcS/Pz6GhF7Loa6bJyU3EgqIDgVAdYU1XjKu7Kw2JOxeHyp6dUpqA6spZEIPReqOKgCm zfyw== X-Forwarded-Encrypted: i=1; AJvYcCVCoxU/8XERbi+6HxBonWmIPW9vWKNHkyVs4z3eENO5PyeO3YITnWQ+CMracKC9FT8oAOnvxVoSmpbxqu4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0E0nRIff3XPYaI667SaMQHU+3B43izXTlK85H0OyKOOO3HCiq w2cHArIMXUcjvUGWJIESILSt32cletsh3NgJm9QJJ5HiGfd4ME92su+r X-Gm-Gg: AY/fxX7ESPOMUMeRfaZ046w8SB4YpokvjCXzoLPQ5EYyhPStoT1wwAz2gMN74yE2SzM 7TaZ0nM1lDET8b8x9PJyFl0bgMu8JznSaxfuFe3RTT0zxzAy/UrC7YHdFzl+unkQ/fDVoDIgtyG 2Coi5Rnjq+Z3AZ8COQns6lquBF4GtSWOgZy4ED7y2EJk0eRX5DrSIXB9xmx1QHvbfv2N9vH3UyV GpXeJ9Rsko9O+OMYaheBePDwTEseSER9+tC//kFrFl7uZHLDVCyaW84w52agg96L9RFRdGFGG5N 7KvNYZ9OuRuqTkkKhe7Jik8cBlgt1nQZz681TRNycBCfe75sqaZV24UGvQtlBRcyM8YTWo2KU2o esxWIkDZpYt/1QXm4sMoWnpdwNdm1QbJR/JCwuYjL6M5xbKQ0p3z2//ORcWJ/5mSR8UaQV00kW8 a5JC0XgCnNv3pWYO6GckJt+hlByU2Ux3UitKGohtYnSlIRzqjY5LA4 X-Google-Smtp-Source: AGHT+IEbh0hfUiD9R3GvyhXdWOj5sIjfGCLoQ1ahL8e/00qynhJrsgNXG94C0F/okiDwtop3UZ3GBA== X-Received: by 2002:a05:6000:1848:b0:430:f97a:6f43 with SMTP id ffacd0b85a97d-430f97a7049mr16299902f8f.53.1766048596303; Thu, 18 Dec 2025 01:03:16 -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 ffacd0b85a97d-4324493fe27sm3770797f8f.12.2025.12.18.01.03.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 01:03:15 -0800 (PST) Date: Thu, 18 Dec 2025 09:03:13 +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: <20251218090313.33923750@pumpkin> In-Reply-To: <20251218014531.3793471-2-mathieu.desnoyers@efficios.com> References: <20251218014531.3793471-1-mathieu.desnoyers@efficios.com> <20251218014531.3793471-2-mathieu.desnoyers@efficios.com> 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 Wed, 17 Dec 2025 20:45:28 -0500 Mathieu Desnoyers wrote: > Compiler CSE and SSA GVN optimizations can cause the address dependency > of addresses returned by rcu_dereference to be lost when comparing those > pointers with either constants or previously loaded pointers. > > Introduce ptr_eq() to compare two addresses while preserving the address > dependencies for later use of the address. It should be used when > comparing an address returned by rcu_dereference(). > > This is needed to prevent the compiler CSE and SSA GVN optimizations > from using @a (or @b) in places where the source refers to @b (or @a) > based on the fact that after the comparison, the two are known to be > equal, 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. > > The same logic applies with @a and @b swapped. > > Suggested-by: Linus Torvalds > Suggested-by: Boqun Feng > Signed-off-by: Mathieu Desnoyers > Reviewed-by: Boqun Feng > Reviewed-by: Joel Fernandes (Google) > Tested-by: Joel Fernandes (Google) > Acked-by: "Paul E. McKenney" > Acked-by: Alan Stern > Cc: Greg Kroah-Hartman > Cc: Sebastian Andrzej Siewior > Cc: "Paul E. McKenney" > Cc: Will Deacon > Cc: Peter Zijlstra > Cc: Boqun Feng > Cc: Alan Stern > Cc: John Stultz > Cc: Neeraj Upadhyay > Cc: Linus Torvalds > Cc: Boqun Feng > Cc: Frederic Weisbecker > Cc: Joel Fernandes > Cc: Josh Triplett > Cc: Uladzislau Rezki > Cc: Steven Rostedt > Cc: Lai Jiangshan > Cc: Zqiang > Cc: Ingo Molnar > Cc: Waiman Long > Cc: Mark Rutland > Cc: Thomas Gleixner > Cc: Vlastimil Babka > Cc: maged.michael@gmail.com > Cc: Mateusz Guzik > Cc: Gary Guo > Cc: Jonas Oberhauser > Cc: rcu@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: lkmm@lists.linux.dev > Cc: Nikita Popov > Cc: llvm@lists.linux.dev > --- > Changes since v0: > - Include feedback from Alan Stern. > --- > include/linux/compiler.h | 63 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 63 insertions(+) > > diff --git a/include/linux/compiler.h b/include/linux/compiler.h > index 5b45ea7dff3e..c5ca3b54c112 100644 > --- a/include/linux/compiler.h > +++ b/include/linux/compiler.h > @@ -163,6 +163,69 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, > __asm__ ("" : "=r" (var) : "0" (var)) > #endif > > +/* > + * Compare two addresses while preserving the address dependencies for > + * later use of the address. It should be used when comparing an address > + * returned by rcu_dereference(). > + * > + * This is needed to prevent the compiler CSE and SSA GVN optimizations > + * from using @a (or @b) in places where the source refers to @b (or @a) > + * based on the fact that after the comparison, the two are known to be > + * equal, 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. > + * > + * The same logic applies with @a and @b swapped. > + * > + * Return value: true if pointers are equal, false otherwise. > + * > + * 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. David > + */ > +static __always_inline > +int ptr_eq(const volatile void *a, const volatile void *b) > +{ > + OPTIMIZER_HIDE_VAR(a); > + OPTIMIZER_HIDE_VAR(b); > + return a == b; > +} > + > #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) > > /**