From nobody Mon Sep 28 04:48:47 2026 Received: from mta1.migadu.com (out-2.mta1.migadu.com [95.215.58.2]) (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 D609C41D206 for ; Wed, 26 Aug 2026 12:26:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747179; cv=none; b=ZLnTLXUUemySRRCM2ViP6LA8RnOSdWppi0EqyKb6djW1lwsHI+tWRsd5CzXUK4Ibhf71wW/UtRgJEAW58Y/exEZP7Cl+tYxcY1ik/P6mCt6ZtSJ9p5MkcDoZLkaI+YANm+fvH4n6BMZr/sMueZax3pazuQIQBvqCgY0AnGlWRbo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747179; c=relaxed/simple; bh=l6nTEFp0UV9ZvkvD6VZyygRqqcTl8rulFcr+21jrYx8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CU1A3JcbuV1BeLCcZZ+R4k2sFqhttj7jnVx/PTQRUT1jZBHk4vnXZW4p+U81ZLHGI9Msvjl+xHny1BkcP4GQFdrzXcCyKF45Yaqoe5zEhTchLPaz59rT6nKsnVA8sz+/i7IJV7kCyKG33T+hcR6aWRHuELKJyGm4DCicuJbq0Nw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=HHPT098t; arc=none smtp.client-ip=95.215.58.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="HHPT098t" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=l6nTEFp0UV9ZvkvD6VZyygRqqcTl8rulFcr+21jrYx8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787747175; v=1; x=1788351975; b=HHPT098tDWJ4r04hQ9nyQ30iXd+b18zHORfGJ2rpa4LHxMrxiD5wTMEm0090YK8uxw6WCtAw L1i7kTvnp+STUcDg20/Ny1/5UojIcd1qG05oEkLkQhOWfwrT61qFRrqg7DZ8ss5tXEi4gXZM9fZ 6HilZcoxLfuO74iRaXq/oJ4I= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost (2a03:2880:10ff:2e::) by mta11.migadu.com with ESMTPS id d057031fdbaecd4f; Wed, 26 Aug 2026 12:26:15 +0000 X-Mizu-Trace-ID: d057031fdbaecd4f X-Migadu-Flow: FLOW_OUT From: Usama Arif To: dsterba@suse.com, linux-kernel@vger.kernel.org, terrelln@fb.com, linux-crypto@vger.kernel.org, yosry@kernel.org Cc: hannes@cmpxchg.org, nphamcs@gmail.com, chengming.zhou@linux.dev, shakeel.butt@linux.dev, kernel-team@meta.com, Usama Arif Subject: [PATCH 1/3] zstd: use ZSTD_cpuSupportsBmi2() in ZSTD_initStaticCCtx() Date: Wed, 26 Aug 2026 05:25:36 -0700 Message-ID: <20260826122558.2662013-2-usama.arif@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260826122558.2662013-1-usama.arif@linux.dev> References: <20260826122558.2662013-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ZSTD_initStaticCCtx() open-codes its BMI2 probe as ZSTD_cpuid_bmi2(ZSTD_cpuid()). Every other context setup - ZSTD_initCCtx() and ZSTD_initDCtx_internal() - goes through ZSTD_cpuSupportsBmi2(), which requires BMI1 as well as BMI2. Requiring both is the correct test. The alternate function bodies the flag selects are tagged BMI2_TARGET_ATTRIBUTE, which is TARGET_ATTRIBUTE("lzcnt,bmi,bmi2"), so the compiler may emit BMI1 and LZCNT instructions in them; BMI2 alone does not make that safe. No shipping x86 CPU implements BMI2 without BMI1, so this is a latent inconsistency rather than an observed failure. Route the static path through the same helper. That also leaves a single place where the probe is issued, which the following patches build on. No functional change on any CPU in the field. Signed-off-by: Usama Arif --- lib/zstd/compress/zstd_compress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/zstd/compress/zstd_compress.c b/lib/zstd/compress/zstd_com= press.c index c41a747413e01..4ab67d679bb63 100644 --- a/lib/zstd/compress/zstd_compress.c +++ b/lib/zstd/compress/zstd_compress.c @@ -142,7 +142,7 @@ ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t = workspaceSize) cctx->blockState.nextCBlock =3D (ZSTD_compressedBlockState_t*)ZSTD_cwk= sp_reserve_object(&cctx->workspace, sizeof(ZSTD_compressedBlockState_t)); cctx->tmpWorkspace =3D ZSTD_cwksp_reserve_object(&cctx->workspace, TMP= _WORKSPACE_SIZE); cctx->tmpWkspSize =3D TMP_WORKSPACE_SIZE; - cctx->bmi2 =3D ZSTD_cpuid_bmi2(ZSTD_cpuid()); + cctx->bmi2 =3D ZSTD_cpuSupportsBmi2(); return cctx; } =20 --=20 2.53.0-Meta From nobody Mon Sep 28 04:48:47 2026 Received: from mta0.migadu.com (out-44.mta0.migadu.com [91.218.175.44]) (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 74E6141D135 for ; Wed, 26 Aug 2026 12:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747182; cv=none; b=bi34oSZdjB5Yu5/FyKeyTpH9WPOp1lr60TSYCpei771883+RM7l1zpvG2jGUgykrIc18Hts3mcGK+xvHi3QYaxZwwxzIWuoGAgdDgkLsotWOB7E3/a8uh4WazBSmHZ/raLMGq7Wi5ytiDOy1URLvN5ctukRkUV1OtMMK4GP2F/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747182; c=relaxed/simple; bh=c0pqy4XVDGw+0BiJFyaIQlWNslTBqgjLRS48kM+/rag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SvbE1s5edxeC9cbuQOLYNS+MFenjapG4iQ+SYzXn4AkwXtVQDyZ4sY1bKCFwWi+7Q8MD/d9GLQBXf/MQ3HEJbOvB762x9hkDpXtwJtESzlL/mjdHwEzs8NJex1mr+wzJTQ255fdg3gHo9QEJV9Smj/lsoCfU34trT9nIOfL5arg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=d24VJgJa; arc=none smtp.client-ip=91.218.175.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="d24VJgJa" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=c0pqy4XVDGw+0BiJFyaIQlWNslTBqgjLRS48kM+/rag=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787747178; v=1; x=1788351978; b=d24VJgJaht/U8d7lAwLJKmQA9YbjD9S9kF9+wp/bowgjno4uV44gAJSiyIXtrkPqvSSdOWUa KQhZ288LPyzh4Js2XJEejahr8+WNiyDJkJ68x1fNPIh1CA58k86TUyK7SUtG74cMyqSAlNb64di sZa8lMnjs2mlpLyMlYZ9p2nA= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost (2a03:2880:10ff:55::) by mta10.migadu.com with ESMTPS id 4c60a4187e1d0492; Wed, 26 Aug 2026 12:26:18 +0000 X-Mizu-Trace-ID: 4c60a4187e1d0492 X-Migadu-Flow: FLOW_OUT From: Usama Arif To: dsterba@suse.com, linux-kernel@vger.kernel.org, terrelln@fb.com, linux-crypto@vger.kernel.org, yosry@kernel.org Cc: hannes@cmpxchg.org, nphamcs@gmail.com, chengming.zhou@linux.dev, shakeel.butt@linux.dev, kernel-team@meta.com, Usama Arif Subject: [PATCH 2/3] zstd: skip the BMI2 probe when dynamic BMI2 dispatch is disabled Date: Wed, 26 Aug 2026 05:25:37 -0700 Message-ID: <20260826122558.2662013-3-usama.arif@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260826122558.2662013-1-usama.arif@linux.dev> References: <20260826122558.2662013-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When DYNAMIC_BMI2 is 0 - GCC older than 11, a non-x86 target, or a build that already has BMI2 on globally - nothing reads the flag. HUF_compress1X_usingCTable_internal(), FSE_decompress_wksp_bmi2(), HUF_readStats_body() and the rest all resolve to the default body without consulting it. ZSTD_cpuSupportsBmi2() nevertheless issues CPUID, which on x86 is two serializing instructions, and throws the answer away. ZSTD_initDCtx_internal() already wraps its assignment in #if DYNAMIC_BMI2 - it has to, because the dctx->bmi2 field is itself declared under that #if. The two compress-side callers have no such guard, and ZSTD_CCtx_s::bmi2 is unconditional, so they probe unconditionally. Put the test inside ZSTD_cpuSupportsBmi2() so every caller gets it without having to remember. Signed-off-by: Usama Arif --- lib/zstd/common/zstd_internal.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/zstd/common/zstd_internal.h b/lib/zstd/common/zstd_interna= l.h index 52a79435caf66..41f190b533209 100644 --- a/lib/zstd/common/zstd_internal.h +++ b/lib/zstd/common/zstd_internal.h @@ -311,8 +311,13 @@ size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbS= eqPtr, */ MEM_STATIC int ZSTD_cpuSupportsBmi2(void) { +#if DYNAMIC_BMI2 ZSTD_cpuid_t cpuid =3D ZSTD_cpuid(); return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid); +#else + /* Nothing looks at the flag in this configuration. */ + return 0; +#endif } =20 #endif /* ZSTD_CCOMMON_H_MODULE */ --=20 2.53.0-Meta From nobody Mon Sep 28 04:48:47 2026 Received: from mta0.migadu.com (out-47.mta0.migadu.com [91.218.175.47]) (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 2D99741D11F for ; Wed, 26 Aug 2026 12:26:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747189; cv=none; b=UN6X4ujVWiTOsn0aNp9DYWhDsWB+47V+he90Z7jEO1dg8xdaD+KomC0NMtxrFo/e3OA9fGivXtqsYRnQW42tvIJndS5J3DwseXbVZaDDlKAvbVQtTpIahskIVx/dsTPHhE9itYVNBiTVsfKFSKX4i6OpU/Bz+T8LtKYmUarFsAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787747189; c=relaxed/simple; bh=sTe+fYRuhgrZdEVDlpsG1pnfBxhxGZuM+7Lo4jhTagQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fBSzj/0VzIkffMnI5MtCTWZoJBMttjLT3RYN2tKHDWsH+yC14t9LrH1SjTBK+gJRNK4dtUBRGOZnlcZmCJlCa1Mnpah5lGDPQTTDt6czvZ+pnhogPZVrcig11anjb0gHBkafUpJVfF6CBT/LyjQTX22KkoCpa93hnPMOJy7olqU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ta99Bdv3; arc=none smtp.client-ip=91.218.175.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ta99Bdv3" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=sTe+fYRuhgrZdEVDlpsG1pnfBxhxGZuM+7Lo4jhTagQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787747184; v=1; x=1788351984; b=ta99Bdv3WURZwh63HHZ3aIHtWSpxTRsrsSFcZvL2yOeNDh0AumnNK5RnXHteCXzh9HEZLU7d pFv/HuDPsOc8h16NKmOXgkGRNtjEF0tHFCZHCgXVUQnZmwAUSqFyTbinFnvwlCGBROX5BmNe8+x p1o57545rilOOosaNvLJiCTQ= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost (2a03:2880:10ff:14::) by mta10.migadu.com with ESMTPS id f3a616fa6cd3bc9b; Wed, 26 Aug 2026 12:26:24 +0000 X-Mizu-Trace-ID: f3a616fa6cd3bc9b X-Migadu-Flow: FLOW_OUT From: Usama Arif To: dsterba@suse.com, linux-kernel@vger.kernel.org, terrelln@fb.com, linux-crypto@vger.kernel.org, yosry@kernel.org Cc: hannes@cmpxchg.org, nphamcs@gmail.com, chengming.zhou@linux.dev, shakeel.butt@linux.dev, kernel-team@meta.com, Usama Arif Subject: [PATCH 3/3] zstd: probe the CPU for BMI2 support only once Date: Wed, 26 Aug 2026 05:25:38 -0700 Message-ID: <20260826122558.2662013-4-usama.arif@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260826122558.2662013-1-usama.arif@linux.dev> References: <20260826122558.2662013-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ZSTD_cpuSupportsBmi2() issues CPUID on every context setup for an answer that cannot change while the kernel is running. On x86 that is two serializing CPUID instructions, and the callers are not rare: squashfs, erofs, btrfs, f2fs and crypto/zstd all initialise a context per operation, so a busy squashfs or zswap workload pays for it per block or per page. Under KVM it is worse, because CPUID is an unconditional VM exit. Cache the result. Keeping the cache as a single int with a negative sentinel, rather than a copy of ZSTD_cpuid_t, keeps it to one word: a racing pair of probes computes the same value from the same CPUID leaf, so the unsynchronized access is benign, and READ_ONCE()/WRITE_ONCE() keep the compiler and KCSAN in agreement about that. ZSTD_cpuSupportsBmi2() is MEM_STATIC, so each translation unit that inlines it gets its own cache - three in a modular build, plus one in each preboot decompressor. That is a handful of ints in bss and one extra probe apiece, not worth avoiding. The cached answer is the one the probing CPU reported. zstd could already be migrated between the probe and the use of the flag, so this does not introduce a heterogeneity question that was not there before. Suggested-by: Yosry Ahmed Signed-off-by: Usama Arif --- lib/zstd/common/zstd_internal.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/zstd/common/zstd_internal.h b/lib/zstd/common/zstd_interna= l.h index 41f190b533209..b179f44753598 100644 --- a/lib/zstd/common/zstd_internal.h +++ b/lib/zstd/common/zstd_internal.h @@ -312,8 +312,21 @@ size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbS= eqPtr, MEM_STATIC int ZSTD_cpuSupportsBmi2(void) { #if DYNAMIC_BMI2 - ZSTD_cpuid_t cpuid =3D ZSTD_cpuid(); - return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid); + /* + * The answer cannot change over the life of the kernel, so probe + * once. Racing probes compute the same value, so the unsynchronized + * access is benign; the annotations are there to keep it that way. + */ + static int supported =3D -1; + int s =3D READ_ONCE(supported); + + if (s < 0) { + ZSTD_cpuid_t const cpuid =3D ZSTD_cpuid(); + + s =3D ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid); + WRITE_ONCE(supported, s); + } + return s; #else /* Nothing looks at the flag in this configuration. */ return 0; --=20 2.53.0-Meta