From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) (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 4CFEE28934C for ; Mon, 4 Aug 2025 16:44:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=172.234.252.31 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754325863; cv=none; b=fI79AQViPlsWPiMjUzOxt7LrOrhqDohKFU3GpFFg+IIAM733mMbRwxlByunQBXmQusakxTgUg1ijlnSkd5IBRIp4+jjhOhl//WeUskCciomNHBxezlCCH2M0ICx1j40dsqgHd8M2FlK5+NC5PPbrXLuAMQNIuR2ogfH2i3VQ1Yo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754325863; c=relaxed/simple; bh=WqXHH+IfirIk9LkIKK7Nl4nbWfgsz7d0eW6zTgrbVtU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PxStlra0bKdUl9/NhxhtuITIG5dj6u1U6MWbkUUvntmuWJbwkXm56UT6ZmwVo6gAAGl/AdjnCa23kZhmsGONTGNU7TeYuSCdHnrYGgwMJ5dGEMLYhOsSI8hfojv4ZNYv6sZEOf5Y9StWtNAMDKkGP13MnSfj1lV5Hhw/zKRwLpc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Uoz8nJLp; arc=none smtp.client-ip=172.234.252.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uoz8nJLp" Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 7AFEA45E85; Mon, 4 Aug 2025 16:44:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54FE0C116C6; Mon, 4 Aug 2025 16:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754325858; bh=WqXHH+IfirIk9LkIKK7Nl4nbWfgsz7d0eW6zTgrbVtU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Uoz8nJLpEg7RJxFDS0SaUDuM3JC3++G29Vs6O3SIq0AlyDw/PtKWwF1Ek74YZtizN ekkMeDZWxmiNkJ5aPmGW7zltBAJVWret0M3QxnUJdR3SxNof1VjZ4TmJar71HRDHcq B+zcU7Vys78NH8p6reQ5SGIWBT2pUfK39dgXZLf/Ay+nfJf8ARpm2byEo1d/BEtLra cRvm5KLXsLdsqhKT3FSA6mH91RMS9AEoSb+vpfE2AY0ZiMOdTviO7cCLtBrLdWkl3F 1gHFyE6a5/HGEDwdrlrj5ZEst307NwD/hZQKvss84KcsXukQxuTCU7N4W3s7jh1DJ+ UJV5giwhSkI8g== From: Kees Cook To: linux-arch@vger.kernel.org Cc: Kees Cook , linux-kernel@vger.kernel.org, x86@kernel.org, linux-alpha@vger.kernel.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH 10/17] openrisc: Add __attribute_const__ to ffs()-family implementations Date: Mon, 4 Aug 2025 09:44:06 -0700 Message-Id: <20250804164417.1612371-10-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250804163910.work.929-kees@kernel.org> References: <20250804163910.work.929-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-m68k@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2965; i=kees@kernel.org; h=from:subject; bh=WqXHH+IfirIk9LkIKK7Nl4nbWfgsz7d0eW6zTgrbVtU=; b=owGbwMvMwCVmps19z/KJym7G02pJDBkTHkflff/nrZrjf/rkzv6m+Qd3bffrennIcv+d+/X3C wSz1gr/6ShlYRDjYpAVU2QJsnOPc/F42x7uPlcRZg4rE8gQBi5OAZiImC7DP/OJ8fypE74U3uRN eLGrj6P1bbxeg8jxiBUOnz7942gyYmH473XulIbBzg0OV94+C85feW9dXFGt1vywDWGtj4yvTa+ O4gMA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit While tracking down a problem where constant expressions used by BUILD_BUG_ON() suddenly stopped working[1], we found that an added static initializer was convincing the compiler that it couldn't track the state of the prior statically initialized value. Tracing this down found that ffs() was used in the initializer macro, but since it wasn't marked with __attribute__const__, the compiler had to assume the function might change variable states as a side-effect (which is not true for ffs(), which provides deterministic math results). Add missing __attribute_const__ annotations to OpenRISC's implementations of ffs(), __ffs(), fls(), and __fls() functions. These are pure mathematical functions that always return the same result for the same input with no side effects, making them eligible for compiler optimization. Build tested ARCH=openrisc defconfig with GCC or1k-linux 15.1.0. Link: https://github.com/KSPP/linux/issues/364 [1] Signed-off-by: Kees Cook --- arch/openrisc/include/asm/bitops/__ffs.h | 2 +- arch/openrisc/include/asm/bitops/__fls.h | 2 +- arch/openrisc/include/asm/bitops/ffs.h | 2 +- arch/openrisc/include/asm/bitops/fls.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/openrisc/include/asm/bitops/__ffs.h b/arch/openrisc/include/asm/bitops/__ffs.h index 1e224b616fdf..4827b66530b2 100644 --- a/arch/openrisc/include/asm/bitops/__ffs.h +++ b/arch/openrisc/include/asm/bitops/__ffs.h @@ -11,7 +11,7 @@ #ifdef CONFIG_OPENRISC_HAVE_INST_FF1 -static inline unsigned long __ffs(unsigned long x) +static inline __attribute_const__ unsigned long __ffs(unsigned long x) { int ret; diff --git a/arch/openrisc/include/asm/bitops/__fls.h b/arch/openrisc/include/asm/bitops/__fls.h index 9658446ad141..637cc76fe4b7 100644 --- a/arch/openrisc/include/asm/bitops/__fls.h +++ b/arch/openrisc/include/asm/bitops/__fls.h @@ -11,7 +11,7 @@ #ifdef CONFIG_OPENRISC_HAVE_INST_FL1 -static inline unsigned long __fls(unsigned long x) +static inline __attribute_const__ unsigned long __fls(unsigned long x) { int ret; diff --git a/arch/openrisc/include/asm/bitops/ffs.h b/arch/openrisc/include/asm/bitops/ffs.h index b4c835d6bc84..536a60ab9cc3 100644 --- a/arch/openrisc/include/asm/bitops/ffs.h +++ b/arch/openrisc/include/asm/bitops/ffs.h @@ -10,7 +10,7 @@ #ifdef CONFIG_OPENRISC_HAVE_INST_FF1 -static inline int ffs(int x) +static inline __attribute_const__ int ffs(int x) { int ret; diff --git a/arch/openrisc/include/asm/bitops/fls.h b/arch/openrisc/include/asm/bitops/fls.h index 6b77f6556fb9..77da7639bb3e 100644 --- a/arch/openrisc/include/asm/bitops/fls.h +++ b/arch/openrisc/include/asm/bitops/fls.h @@ -11,7 +11,7 @@ #ifdef CONFIG_OPENRISC_HAVE_INST_FL1 -static inline int fls(unsigned int x) +static inline __attribute_const__ int fls(unsigned int x) { int ret; -- 2.34.1