From nobody Fri Dec 19 00:21:37 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 E7494255252; Tue, 25 Mar 2025 09:05:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742893547; cv=none; b=jRvHQFa+tU2c6EcgxUKQ7BDiusHwtSPnPR3uRh93eUuLV70yoA7PM2krF8Bla0YJOX8iLQCUJYMVe5HsBGq6IUyvDuRTbKsIVO0/abq39aEReR0dOFFmC1AgBaoMAzDXctgqlQh4jdAmnwUpterGvGpBTkc+NSFjPIZlKtPd7qU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742893547; c=relaxed/simple; bh=q5Yrvu4POrN7xFo27iP4UrzSqMh45skr+MXffiNfW3A=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=AmgLB4PbKuNTZLs6q2Ql28x/4dHk8Ze5dBET6urUO4olmr05dF9Qb+vUpHEPam6YCzSh0/PwqxTLVA9FeilbhuEmlwuKJUTFB8jqbkRtAOavbz3vc/3QpjGV+gDVsX1P6Jb+7Emuzm2ljP3l7mR16DGeFGuiaRXg9Ww/WKokltc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qc/K+1m6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UgDNeygu; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qc/K+1m6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UgDNeygu" Date: Tue, 25 Mar 2025 09:05:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742893542; h=from:from:sender:sender:reply-to: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; bh=wp7J1TATaR4+mNWhc4+Yazig/WENgnK4sFlaFBLxSBo=; b=qc/K+1m6dnFmxdfUjnrepEgH8oykQFumgfm9akEB1fo8wFTpkqQzUYo3DjGVOUccCIliua SA793rkb/WnrUUhWqI4c/6qOWmoFgaIze4m9+4ZiHoQ73xVdDPQsvHBEkf0fMuvm4exa/u qsNDrAhMfV2Gs2WOXOU32q00vAUTXIuyUL5w6cyutUTnWSLdeRLZ95BH4gL3OREeocv12h 21ScuNKnPqE1+RhKOZ7q07smrzfq1hliFB+VZd0R+onEZHh2Lh0Xh2BodSFnBhHt1DMr8L V+O1gErsK0HQbQboLydrPXOkaxoK1FcG3pw2m1pZOFNv7XwJMkorXfC+cj4e6Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742893542; h=from:from:sender:sender:reply-to: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; bh=wp7J1TATaR4+mNWhc4+Yazig/WENgnK4sFlaFBLxSBo=; b=UgDNeygu1KXLaCdmWD74vNYx+/bOrxsv8Ms+X2PfHNbH11l0uRGzIWtwIA90SCePBgC7Ge 5i7QLuHImci2gVDw== From: "tip-bot2 for Ahmed S. Darwish" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/cpu] tools/x86/kcpuid: Use intrinsics Cc: "Ahmed S. Darwish" , Ingo Molnar , "H. Peter Anvin" , Linus Torvalds , Josh Poimboeuf , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250324142042.29010-11-darwi@linutronix.de> References: <20250324142042.29010-11-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <174289354186.14745.673118297329233119.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/cpu branch of tip: Commit-ID: c479a84488d10b3d4259186d80839f99e26b1706 Gitweb: https://git.kernel.org/tip/c479a84488d10b3d4259186d80839f99e= 26b1706 Author: Ahmed S. Darwish AuthorDate: Mon, 24 Mar 2025 15:20:31 +01:00 Committer: Ingo Molnar CommitterDate: Tue, 25 Mar 2025 09:53:45 +01:00 tools/x86/kcpuid: Use intrinsics Use the __cpuid_count() intrinsic, provided by GCC and LLVM, instead of rolling a manual version. Both of the kernel's minimum required GCC version (5.1) and LLVM version (13.0.1) supports it, and it is heavily used across standard Linux user-space tooling. This also makes the CPUID call sites more readable. Signed-off-by: Ahmed S. Darwish Signed-off-by: Ingo Molnar Cc: H. Peter Anvin Cc: Linus Torvalds Cc: Josh Poimboeuf Link: https://lore.kernel.org/r/20250324142042.29010-11-darwi@linutronix.de --- tools/arch/x86/kcpuid/kcpuid.c | 37 +++++++++++++-------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c index 413e5da..25388a5 100644 --- a/tools/arch/x86/kcpuid/kcpuid.c +++ b/tools/arch/x86/kcpuid/kcpuid.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE =20 +#include #include #include #include @@ -86,16 +87,16 @@ static u32 user_index =3D 0xFFFFFFFF; static u32 user_sub =3D 0xFFFFFFFF; static int flines; =20 -static inline void cpuid(u32 *eax, u32 *ebx, u32 *ecx, u32 *edx) -{ - /* ecx is often an input as well as an output. */ - asm volatile("cpuid" - : "=3Da" (*eax), - "=3Db" (*ebx), - "=3Dc" (*ecx), - "=3Dd" (*edx) - : "0" (*eax), "2" (*ecx)); -} +/* + * Force using __cpuid_count() instead of __cpuid(). The + * latter leaves ECX uninitialized, which can break CPUID queries. + */ + +#define cpuid(leaf, a, b, c, d) \ + __cpuid_count(leaf, 0, a, b, c, d) + +#define cpuid_count(leaf, subleaf, a, b, c, d) \ + __cpuid_count(leaf, subleaf, a, b, c, d) =20 static inline bool has_subleafs(u32 f) { @@ -195,12 +196,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) u32 max_func, idx_func; u32 eax, ebx, ecx, edx; =20 - eax =3D input_eax; - ebx =3D ecx =3D edx =3D 0; - cpuid(&eax, &ebx, &ecx, &edx); - - max_func =3D eax; - idx_func =3D (max_func & 0xffff) + 1; + cpuid(input_eax, max_func, ebx, ecx, edx); =20 range =3D malloc(sizeof(struct cpuid_range)); if (!range) @@ -211,6 +207,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) else range->is_ext =3D false; =20 + idx_func =3D (max_func & 0xffff) + 1; range->funcs =3D malloc(sizeof(struct cpuid_func) * idx_func); if (!range->funcs) err(EXIT_FAILURE, NULL); @@ -222,9 +219,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) u32 max_subleaf =3D MAX_SUBLEAF_NUM; bool allzero; =20 - eax =3D f; - ecx =3D 0; - cpuid(&eax, &ebx, &ecx, &edx); + cpuid(f, eax, ebx, ecx, edx); =20 allzero =3D cpuid_store(range, f, 0, eax, ebx, ecx, edx); if (allzero) @@ -251,9 +246,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) max_subleaf =3D 5; =20 for (u32 subleaf =3D 1; subleaf < max_subleaf; subleaf++) { - eax =3D f; - ecx =3D subleaf; - cpuid(&eax, &ebx, &ecx, &edx); + cpuid_count(f, subleaf, eax, ebx, ecx, edx); =20 allzero =3D cpuid_store(range, f, subleaf, eax, ebx, ecx, edx); if (allzero)