[PATCH AUTOSEL 6.18-5.10] bitfield: wire __bf_shf to __builtin_ctzll
From: Sasha Levin
Date: Mon Aug 31 2026 - 10:28:19 EST
From: Yury Norov <ynorov@xxxxxxxxxx>
[ Upstream commit 09472f591aa0b72c2dd6c693f48b2d6fea66c7ba ]
__bf_shf() is currently based on built-in ffsll. It's more
straightforward to wire it to __builtin_ctzll, which makes it a pure
rename.
Worth to notice that __builtin_ffsll() is buggy on GCC before 14.1:
int main() {
sizeof(struct {
int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
});
}
test.c: In function 'main':
test.c:3:21: error: bit-field 't' width not an integer constant
3 | int t : !(__builtin_ffsll(~0ULL) + 1 < 0);
| ^
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699
Reported-by: Matt Coster <matt.coster@xxxxxxxxxx>
Closes: https://lore.kernel.org/oe-kbuild-all/202603222211.A2XiR1YU-lkp@xxxxxxxxx/
Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
LLM Generated explanations, may be completely bogus:
# Stable Backport Analysis: `bitfield: wire __bf_shf to __builtin_ctzll`
**Local tree:** `v6.18.44` (6.18.44) — HEAD is `1efe5d048a391 Linux
6.18.44`
---
## PHASE 1: COMMIT MESSAGE FORENSICS
### Step 1.1: Subject line
**Record:** `[bitfield]` `[wire]` — Replace `__bf_shf` implementation
from `__builtin_ffsll(x)-1` with `__builtin_ctzll`.
### Step 1.2: Tags
**Record:**
| Tag | Value |
|-----|-------|
| Reported-by | Matt Coster \<matt.coster@xxxxxxxxxx\> |
| Link | https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124699 |
| Closes | https://lore.kernel.org/oe-kbuild-all/202603222211.A2XiR1YU-
lkp@xxxxxxxxx/ |
| Signed-off-by | Yury Norov \<ynorov@xxxxxxxxxx\> |
**Notable patterns:** Real reporter (IMG engineer); closes an **oe-
kbuild-all** CI build failure; references a documented **GCC compiler
bug** (BZ#124699). No Fixes:, Cc: stable, Tested-by, or Reviewed-by tags
(absence of Cc: stable is expected per instructions).
### Step 1.3: Body analysis
**Record:**
- **Bug:** `__builtin_ffsll()` does not evaluate correctly in compile-
time constant expressions on GCC before 14.1.
- **Symptom:** Compile failure — `error: bit-field 't' width not an
integer constant` when `__bf_shf` is used inside `BUILD_BUG_ON*` /
`FIELD_PREP_CONST` constant-expression checks.
- **Root cause:** `__bf_shf(x)` was defined as `(__builtin_ffsll(x) -
1)`; for power-of-2 masks this is semantically equivalent to
`__builtin_ctzll(x)`, but only `__builtin_ctzll` works reliably as a
constant expression on affected GCC versions.
- **Version info:** GCC bug affects versions **before 14.1**.
### Step 1.4: Hidden bug fix?
**Record:** Not disguised — this is an explicit **build fix** for a
compiler bug affecting compile-time bitfield macro validation. No
runtime behavior change for valid masks.
---
## PHASE 2: DIFF ANALYSIS
### Step 2.1: Inventory
**Record:**
- **Files:** `include/linux/bitfield.h` (+1 / -1)
- **Functions modified:** None (macro-only change)
- **Scope:** Single-file, surgical (1 line)
### Step 2.2: Code flow per hunk
**Record:**
- **Before:** `__bf_shf(_mask)` → `(__builtin_ffsll(_mask) - 1)` — shift
amount from 1-based find-first-set.
- **After:** `__bf_shf(_mask)` → `__builtin_ctzll(_mask)` — shift amount
from count-trailing-zeros.
- **Affected paths:** All compile-time uses in `__BF_FIELD_CHECK`,
`FIELD_MAX`, `FIELD_FIT`, `FIELD_PREP`, `FIELD_PREP_CONST`,
`FIELD_GET`, `FIELD_MODIFY` (lines 69–173 of `bitfield.h`). Runtime
uses of `__bf_shf` in drivers are also affected but produce identical
results for valid power-of-2 masks.
### Step 2.3: Bug mechanism
**Record:** **Build fix / compiler interaction bug (category
h-adjacent).** `BUILD_BUG_ON*` macros expand `__bf_shf(_mask)` in
constant-expression contexts. On GCC \< 14.1, `__builtin_ffsll` fails
constant-folding, breaking kernel compilation. `__builtin_ctzll`
constant-folds correctly.
### Step 2.4: Fix quality
**Record:** Obviously correct for valid masks (non-zero, power-of-2 —
already enforced by existing `BUILD_BUG_ON` checks). Minimal change.
**Regression risk: very low** — mathematically equivalent for all valid
inputs; `__builtin_ctzll` is already used extensively elsewhere in the
kernel.
---
## PHASE 3: GIT HISTORY INVESTIGATION
### Step 3.1: Blame
**Record:** `__bf_shf(x) (__builtin_ffsll(x) - 1)` introduced in commit
`3e9b3112ec74f` ("add basic register-field manipulation macros", Jakub
Kicinski, 2016-08-31). Confirmed ancestor of HEAD — present in this tree
since 2016.
### Step 3.2: Fixes: tag
**Record:** No Fixes: tag present. N/A.
### Step 3.3: Related file history
**Record:** Recent `bitfield.h` changes in 6.18.y include
`FIELD_MODIFY()` (a256ae22570ee), `FIELD_PREP_CONST()` (e2192de59e457,
2023), `FIELD_MAX()`/`FIELD_FIT()`. The `FIELD_PREP_CONST` addition
increased compile-time `__bf_shf` usage in initializers. Standalone
1-line fix; not part of a multi-patch dependency chain for backport
purposes.
### Step 3.4: Author context
**Record:** Yury Norov is a regular bitfield/bitmap contributor (signed
off on e2b02d382ae0c in this tree). Author of the broader 7.2 bitmap
series on mainline.
### Step 3.5: Dependencies
**Record:** No prerequisites. Patch applies cleanly (`git apply --check`
exit 0). Does not depend on `FIELD_GET_SIGNED` or other 7.2-only
additions.
---
## PHASE 4: MAILING LIST AND EXTERNAL RESEARCH
### Step 4.1: Original discussion
**Record:** Found at
https://lkml.iu.edu/hypermail/linux/kernel/2604.3/05643.html (also
https://lists.openwall.net/linux-kernel/2026/04/27/2584). `b4 dig -c`
failed because commit is not in this tree. Single patch, not a multi-
revision series for this specific fix.
### Step 4.2: Reviewers
**Record:** CC'd to Rasmus Villemoes (bitfield maintainer area),
multiple IMG engineers (reporters of the build failure), Vincent
Mailhol. David Laight replied with a style suggestion only ("I'd leave
in the (x)") — not a NAK.
### Step 4.3: Bug report
**Record:** Closes oe-kbuild-all report from 2026-03-22 (kernel CI build
robot — concrete compile failure). Matt Coster (IMG) reported. GCC
BZ#124699 documents the compiler defect (lore.kernel.org fetch blocked
by bot protection; GCC bugzilla returned 403).
### Step 4.4: Related patches
**Record:** Part of Yury's "bitmap-for-7.2" series on mainline (patch
11/19), but this specific change is fully self-contained.
### Step 4.5: Stable list history
**Record:** No stable-specific discussion found. No reviewer explicitly
nominated Cc: stable, but build fixes are routinely backported.
---
## PHASE 5: CODE SEMANTIC ANALYSIS
### Step 5.1: Key symbols
**Record:** `__bf_shf` macro; consumers: `__BF_FIELD_CHECK`,
`FIELD_MAX`, `FIELD_FIT`, `FIELD_PREP`, `FIELD_PREP_CONST`, `FIELD_GET`,
`FIELD_MODIFY`.
### Step 5.2: Callers
**Record:** `FIELD_GET`/`FIELD_PREP`/`FIELD_PREP_CONST` used in hundreds
of files across drivers, net, sound, GPU, PCI, etc. Direct `__bf_shf()`
calls in drivers (spi-dw-core, mv88e6xxx, iwlwifi, nfp, etc.). Very
broad impact surface.
### Step 5.3: Callees
**Record:** Changes compiler builtin from `__builtin_ffsll` to
`__builtin_ctzll`. No kernel function calls.
### Step 5.4: Reachability
**Record:** Triggered at **compile time** when any translation unit
using `FIELD_*` macros is built with GCC \< 14.1. Affects all
developers/distributions building 6.18.y with GCC 12/13 (common
toolchain versions).
### Step 5.5: Similar patterns
**Record:** Kernel already uses `__builtin_ctzll` widely (e.g.,
`lib/math/div64.c`, `fs/btrfs/volumes.h`). The `ffsll(x)-1` ≡ `ctzll(x)`
equivalence for power-of-2 values is standard.
---
## PHASE 6: CROSS-REFERENCE AGAINST LOCAL TREE
### Step 6.1: Buggy code exists?
**Record:** **YES.** Line 45 of `include/linux/bitfield.h`:
```45:45:include/linux/bitfield.h
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
```
Present since 2016 in this tree. Commit under review is **not yet
applied** (`git log --grep="wire __bf_shf"` returns empty).
### Step 6.2: Backport complications
**Record:** **Clean apply** — verified with `git apply --check`. No
conflicts expected.
### Step 6.3: Related fixes already present?
**Record:** None. `git log --grep="wire __bf_shf"` and `git log
--grep="__builtin_ctzll" -- include/linux/bitfield.h` show no equivalent
fix in this tree.
**Note:** `tools/include/linux/bitfield.h` line 43 still has the old
definition; the upstream commit also only touches
`include/linux/bitfield.h`. Minor gap for tools-only builds, not a
reason to reject the kernel header fix.
---
## PHASE 7: SUBSYSTEM AND MAINTAINER CONTEXT
### Step 7.1: Subsystem criticality
**Record:** **CORE** — `include/linux/bitfield.h` is a foundational
header used across virtually every driver subsystem.
### Step 7.2: Activity
**Record:** Moderately active in 6.18.y (FIELD_MODIFY, __must_check
additions in 2025). The underlying `__bf_shf` definition has been stable
since 2016; the compiler interaction is the issue, not recent kernel
churn.
---
## PHASE 8: IMPACT AND RISK ASSESSMENT
### Step 8.1: Who is affected
**Record:** Anyone building kernel 6.18.y with **GCC \< 14.1** (GCC
12.x, 13.x — standard for RHEL, Debian stable, many embedded SDKs).
Universal compile-time impact across all subsystems using `FIELD_*`
macros.
### Step 8.2: Trigger conditions
**Record:** Building any config that compiles a file using
`FIELD_GET`/`FIELD_PREP`/`FIELD_PREP_CONST`/etc. with affected GCC.
Documented CI failure (oe-kbuild-all, March 2026). Not userspace-
triggerable; not a runtime bug.
### Step 8.3: Failure mode severity
**Record:** **Build failure** (compilation error) — severity **HIGH**
for affected builders (cannot compile kernel). No runtime crash,
corruption, or security impact.
### Step 8.4: Risk-benefit
**Record:**
- **Benefit: HIGH** — restores ability to build with common GCC
versions; fixes documented CI failure.
- **Risk: VERY LOW** — 1-line semantic rename for valid inputs; no API
change; no runtime behavior change.
- **Ratio:** Strongly favors backport.
---
## PHASE 9: FINAL SYNTHESIS
### Step 9.1: Evidence summary
**FOR backport:**
- Documented real build failure (oe-kbuild-all CI, March 2026)
- User report from Matt Coster (IMG)
- GCC BZ#124699 — known compiler bug
- Build-fix category (explicit stable exception per rules)
- 1 line, applies cleanly to 6.18.44
- Buggy code present since 2016 in this tree
- `FIELD_*` macros used in hundreds of files
- Mathematically equivalent fix, minimal regression risk
**AGAINST backport:**
- Only compile-time impact, not runtime
- Bug latent since 2016 — may be config/GCC-version specific
- `tools/include/linux/bitfield.h` not updated in same commit
- Not yet in this tree's git history (must land in mainline first per
stable rules)
**Unresolved:** Exact oe-kbuild-all log unavailable (lore bot
protection). Could not test on GCC \< 14.1 locally (only GCC 15.3
available; test passed). GCC bugzilla page returned 403.
### Step 9.2: Stable rules checklist
| Criterion | Result |
|-----------|--------|
| 1. Obviously correct and tested? | **PASS** — semantic equivalence
proven; CI failure closed |
| 2. Fixes real bug affecting users? | **PASS** — kernel build failure
with GCC \< 14.1 |
| 3. Important issue? | **PASS** — build error (stable-kernel-rules.rst
line 19) |
| 4. Small and contained? | **PASS** — 1 line, 1 file |
| 5. No new features/APIs? | **PASS** — internal macro implementation
change only |
| 6. Can apply to local tree? | **PASS** — clean apply verified |
### Step 9.3: Exception category
**Record:** **BUILD FIX** — explicitly listed as stable-worthy.
### Step 9.4: Decision rationale
This commit fixes a documented kernel **compilation failure** on GCC
versions before 14.1, which remain widely used for building stable
kernels. The buggy `__bf_shf` definition has been in 6.18.y since 2016
and is used by core `FIELD_*` macros throughout the tree. The fix is a
one-line, semantically equivalent substitution that applies cleanly to
v6.18.44 with negligible regression risk.
---
## Verification
- **[Phase 1]** Parsed subject, tags (Reported-by, Link, Closes), body;
identified GCC \< 14.1 build failure
- **[Phase 2]** Diff: 1 line in `include/linux/bitfield.h`;
`__builtin_ffsll(x)-1` → `__builtin_ctzll`
- **[Phase 3]** `git blame -L 45,45`: introduced `3e9b3112ec74f` (2016);
confirmed ancestor of HEAD
- **[Phase 3]** `git log --oneline -20 -- include/linux/bitfield.h`:
recent changes listed; no duplicate fix
- **[Phase 3]** `git apply --check`: patch applies cleanly (exit 0)
- **[Phase 4]** lkml.iu.edu/2604.3/05643.html: found original submission
- **[Phase 4]** openwall.net 2026/04/28/111: David Laight style comment
only, no objection
- **[Phase 4]** `b4 dig -c`: FAILED — commit not in local tree
- **[Phase 4]** lore.kernel.org oe-kbuild-all link: BLOCKED (bot
protection)
- **[Phase 4]** gcc.gnu.org BZ#124699: BLOCKED (403)
- **[Phase 5]** `grep FIELD_GET/FIELD_PREP`: hundreds of .c files across
tree
- **[Phase 5]** `grep __bf_shf`: 10 uses in bitfield.h + many direct
driver uses
- **[Phase 6]** `git describe HEAD`: v6.18.44
- **[Phase 6]** `make -s kernelversion`: 6.18.44
- **[Phase 6]** Read `include/linux/bitfield.h` line 45: buggy
definition confirmed present
- **[Phase 6]** `git log --grep="wire __bf_shf"`: empty — fix not yet in
tree
- **[Phase 8]** GCC 15.3 test of `__builtin_ffsll(~0ULL)` constant expr:
compiles (exit 0); UNVERIFIED on GCC \< 14.1 (not installed)
**YES**The background checks finished and match what the analysis
already used:
- **Commit search:** `git log --grep="wire __bf_shf"` returned nothing —
this fix is not in the local `6.18.44` tree yet.
- **`b4 dig`:** Failed (`--subject` is not a valid `b4 dig` option). The
patch thread was found via lkml/openwall instead.
- **`FIELD_PREP_CONST` history:** Added in `e2192de59e457` (Jan 2023),
which increased compile-time `__bf_shf` use in constant expressions.
- **Tree state:** HEAD is `1efe5d048a391` (`Linux 6.18.44`); line 45 of
`include/linux/bitfield.h` still has the old `__builtin_ffsll`
definition.
**Verdict remains YES** — one-line build fix for GCC \< 14.1, applies
cleanly to this tree.
include/linux/bitfield.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 5355f8f806a97..2b3127e712b93 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -42,7 +42,7 @@
* FIELD_MODIFY(REG_FIELD_C, ®, c);
*/
-#define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_shf __builtin_ctzll
#define __scalar_type_to_unsigned_cases(type) \
unsigned type: (unsigned type)0, \
--
2.53.0