From nobody Sun May 24 22:36:03 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 AC6621FC7 for ; Thu, 21 May 2026 00:04:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779321886; cv=none; b=nODSbvZqE9TEEyo+T4z+ua18FSTC5IEeCJxcEmhpdVUA9FQbGb0KjuzS9TNwEBEDp0aT/H1Bh13h380vv4GNWkA8Qi+4qtCif13ry7ZXmvJFLVhhwXgXwRKGXhBmiYd1ecXDp7y4FxnUC0c1TEQRJ6/hu3lSocR7IMhMAV2+GMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779321886; c=relaxed/simple; bh=JcFRC5A7uQxkNg7P+YCGnKt01JnIirftfAS6ME+UAgY=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=WYWzZ7hWQr9+Mv/7NlrU1pNZ1nY0bmkZ+DOCUnM3XMKtYTMDUL4dllbCP6akeJ0vY71TjlJnSj8pGWcniHBI4AWwyM5N/IxSvuNLgk6w1eawERCmL+Gr2GWTpoGw8P7T5Oh4Hckq5nwTYTfNx0WwHse2SO7Wy+XVRx5sc8nW7rM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--elver.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VZnJw+Td; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--elver.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VZnJw+Td" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-45ea38c03a1so114972f8f.1 for ; Wed, 20 May 2026 17:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779321883; x=1779926683; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=UAkabFJHWBLwajif3qDHLP2pVGG9efW8qvp23UJA7Kg=; b=VZnJw+TdpCQJIpOZHyLzkN6OrIEVLgQWW922HzXqcBo1Mj6bD1MuBZEd3GoazaQzo9 fjPTiXjLw4yUhqOsPJ8n7XO0K0+/Zd6Vth6R5f5JYbuCbWJjzhGsHR6qqMO204U6KlhR Sj2soTx1nsSoqhaJxUQVY/nshGe9XNs++2t6pgxw4NtHoyV5njuv8gqkiFRPa6WJbu09 AodF76n8ItkuY68nMhZaTfoIwuoPKsMYYIRVNQh9V7bDB2xxCx8+y9tigppNuah71kYS IRz34Cb9TTIcMzoK17zDv/aSqr3NEycOZlackZrTtNHs63rkBK99i3ncVBmXZWMW9rYa zx1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779321883; x=1779926683; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=UAkabFJHWBLwajif3qDHLP2pVGG9efW8qvp23UJA7Kg=; b=cuF5vwTxAksVb7szaaru2OgGR+oBS8nbbwzIEKSbwhfKui/4pX9JTJgC8wN6hXdIz5 TiL+OxZyFOqvAL8K1lTLXmSwBHjY+SnPxUx+DANWE2KywR+e+wg6YsiSRqqGXWLuuCPP CHRSsYmUUhRH72nfv/Hw3p3ctrxJIy9dU6T8I9ZEQFeti5f8BSi0nJ6rjrDulJj9gaPo +DK74y00Xml3zj9FdW927U1QylSBZ/+E7CqbcwWcQs9FJT6oEE30bDn5tqTGPWdw+e7l 9oF1AXE4RkyiLFOIrw558eqvdbffWgQDWQifI8MEGfFqd3MkD/y9hVu5W5MQjKNAIwCy dHEA== X-Forwarded-Encrypted: i=1; AFNElJ+chIzSJU5c7HmPeFdN2EHJ04YtnBuwkD4TWeRfhBxp8XnojurSKhH66ZvdV2cG4u9qLqjgbVl9lL3Az+4=@vger.kernel.org X-Gm-Message-State: AOJu0YyXEQjU5472Lkp/g1/jyTptPZlzVsyxkhK/M3qsmLsl04/sNR2n I2LBmnzTvOVNGuAPzRn36gTm87n1klDeD4OYL6IfULxyxC4ky4JA+/CbAfu1StbvVlS1xGA/CNu 46w== X-Received: from wmaj9.prod.google.com ([2002:a05:600c:6c09:b0:485:3bc4:b1d6]) (user=elver job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c16:b0:48f:d2b5:d7 with SMTP id 5b1f17b1804b1-49036039043mr6867595e9.12.1779321883037; Wed, 20 May 2026 17:04:43 -0700 (PDT) Date: Thu, 21 May 2026 02:04:33 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.54.0.746.g67dd491aae-goog Message-ID: <20260521000436.3931067-1-elver@google.com> Subject: [PATCH] riscv: Implement _THIS_IP_ using inline asm From: Marco Elver To: elver@google.com Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Both GCC [1] and Clang [2] consider the generic version of _THIS_IP_ to be broken: #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__= here; }) In particular, the address of a label is only expected to be used with a computed goto. While the generic version more or less works today, it is known to be brittle and may break with current and future optimizations. For example, Clang -O2 always returns 1 when this function is inlined: static inline unsigned long get_ip(void) { return ({ __label__ __here; __here: (unsigned long)&&__here; }); } Fix it by overriding _THIS_IP_ in (which is included by ) using an architecture-specific inline asm version. Additionally, avoiding taking the address of a label prevents compilers from emitting spurious indirect branch targets (e.g. ENDBR or BTI) under control-flow integrity schemes. Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D120071 [1] Link: https://github.com/llvm/llvm-project/issues/138272 [2] Signed-off-by: Marco Elver --- arch/riscv/include/asm/linkage.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/riscv/include/asm/linkage.h b/arch/riscv/include/asm/link= age.h index 9e88ba23cd2b..7e0210ef4eb4 100644 --- a/arch/riscv/include/asm/linkage.h +++ b/arch/riscv/include/asm/linkage.h @@ -9,4 +9,6 @@ #define __ALIGN .balign 4 #define __ALIGN_STR ".balign 4" =20 +#define _THIS_IP_ ({ unsigned long __ip; asm volatile("auipc %0, 0" : "=3D= r" (__ip)); __ip; }) + #endif /* _ASM_RISCV_LINKAGE_H */ --=20 2.54.0.746.g67dd491aae-goog