Skip to content

soc: apple: clpc: test the frequency step, ceiling order, vote hold and floor word - #2821

Open
RyanTheTide wants to merge 2 commits into
integration/aurorafrom
test-coverage/clpc-policy
Open

RyanTheTide wants to merge 2 commits into
integration/aurorafrom
test-coverage/clpc-policy

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Problem. The CLPC low-power ceiling and the rail floors are built from four small helpers: apple_clpc_step_khz(), apple_clpc_steps_first(), apple_clpc_vote() and apple_clpc_floor(). None of them had a test. The existing apple-clpc-gpu suite covers the GPU/ANE demand inputs only.

Change. A new KUnit suite, apple-clpc-policy, sits in the existing KUnit block of clpc.c:

  • step_khz, on an unordered table with an invalid entry: one step either way, off-table values, both ends, and the low-power top round-up (low_power_khz - 1, up), for an exact entry, a value in between, and nothing above the table.
  • steps_first: going down, the higher ceiling steps first (the faster cluster on a tie); going up, the lower one (the slower cluster). Identical clusters keep the pick.
  • vote: a new demand replaces the state at once, down as well as up. With no demand the state holds for APPLE_CLPC_HOLD_MS and then drops, including across the jiffies wrap.
  • floor: each rail's state goes in the nibble its table rail number names, masked so it can't spill into another rail.

Tests only, no functional change.

Testing.

  • kunit.py run --arch=arm64 (qemu, LLVM=1, KASAN + UBSAN from clpc.kunitconfig), filter apple-clpc*: 15/15 (11 existing + 4 new).
  • Negative control: a mutant that flips the steps_first tie-break, uses time_after() for the hold, drops the nibble mask and lets a down step return @khz makes all four new tests fail (plus the existing ane_demand_hold test).
  • tools/asahi/clpc-gpu-demand-tests.py and clpc-high-power-cleanup-tests.py still pass. They extract code from clpc.c.
  • checkpatch: 0 errors, 0 warnings.

Still to run. Nothing on hardware: the change is test-only.

Note on the kunitconfig

The stock drivers/soc/apple/clpc.kunitconfig currently fails to link because apple_smc_read/apple_smc_write are undefined without the SMC driver. #2693 fixes that separately. These runs added CONFIG_MFD_MACSMC=y plus ARCH_APPLE, MAILBOX, APPLE_MAILBOX and APPLE_RTKIT to it.

Assisted-by: LLM (Claude:Opus 5.5)

…nd floor word

The low-power ceiling and the rail floors are built from four small
helpers that had no test: apple_clpc_step_khz() (the next cpufreq table
entry up or down, and the low-power top round-up), apple_clpc_steps_first()
(which cluster's ceiling moves next), apple_clpc_vote() (the demand hold)
and apple_clpc_floor() (the PMC floor word).

Add a KUnit suite, apple-clpc-policy, next to apple-clpc-gpu:

- step_khz on an unordered table with an invalid entry: one step either
  way, off-table values, both ends, and the first entry at or above
  low_power_khz (exact and in between, and none above the table).
- steps_first: down takes the higher ceiling (the faster cluster on a
  tie), up the lower (the slower), and identical clusters keep the pick.
- vote: a demand replaces the state at once, down as well as up; with no
  demand it holds for APPLE_CLPC_HOLD_MS and then drops, across the
  jiffies wrap.
- floor: each rail's state in the nibble its table rail number names,
  masked so a wide state cannot spill into another rail.

No functional change. A mutant that flips the steps_first tie-break,
uses time_after() for the hold, drops the nibble mask and lets a down
step return @khz itself fails all four new tests.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at 309a55f.

All four tests call the real static helpers in clpc.c (no copies). I checked every expectation by hand against apple_clpc_step_khz() (781), apple_clpc_steps_first() (838), apple_clpc_vote() (566) and apple_clpc_floor() (580). The tie-break, time_after() and nibble-mask mutants are each caught. No per-chip values are added to the driver; the frequencies are synthetic and live only in the test. Points:

  1. clpc.c:2165: t0 = ULONG_MAX - hold / 2 does not test the wrap. With it, a wrap-unsafe hold check (now >= last_demand + hold) passes every expectation for hold = 4, 8 and 32 (HZ 100/250/1000), because last_demand + hold and now wrap at the same time. t0 = ULONG_MAX - (hold - 1) makes the t0 + hold - 1 sample land on ULONG_MAX while the deadline has wrapped to 0, and then the naive compare fails.
  2. clpc.c:2119-2120: the line under "The invalid entry is not a step" can't detect that: CPUFREQ_ENTRY_INVALID is above 1200000, so a down step never picks it. The cases that do catch it are the up steps at 2000000 and 2100000. Move the comment, or add an up step that would land on the invalid entry.
  3. clpc.c:2131-2139 says it tests the low-power top "as apple_clpc_profile_limit() takes it", but the round-up check and the max_freq fallback (clpc.c:887-890) are in profile_limit() and aren't exercised. Either reword the comment to cover only step_khz, or pull the top computation into a helper and test that.
  4. As the body says, the stock clpc.kunitconfig doesn't link at this head without soc: apple: clpc: link without the SMC driver #2693, so this suite can't be run from the tree as is. Merge after soc: apple: clpc: link without the SMC driver #2693.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial §1–§6 review at 309a55f9c7d147d26852732ffb43b5273c341d9c: REQUEST_CHANGES (not ready for MERGETRAIN-LINUX4).

Diff & Scope Verification

  • Zero functional kernel change: Adds 137 lines strictly inside the existing #if IS_ENABLED(CONFIG_KUNIT) block of drivers/soc/apple/clpc.c, calling the real static production helpers (apple_clpc_step_khz(), apple_clpc_steps_first(), apple_clpc_vote(), apple_clpc_floor()).
  • CI: All 7 checks green (host-tests, SEP source checks), though note that none of the CI jobs build or execute apple-clpc-policy (the SEP kunit job only runs SEP KUnit suites).

Blocking Findings

  1. Broken jiffies-wrap coverage in apple_clpc_vote_test() (drivers/soc/apple/clpc.c:2165-2177):
    • With APPLE_CLPC_HOLD_MS = 32, hold = msecs_to_jiffies(32) is even (4, 8, or 32 at HZ 100/250/1000), and t0 = ULONG_MAX - hold / 2.
    • Every hold/drop sample checked after t0 (t0 + hold - 1, t0 + hold, t0 + 2 * hold) has already wrapped past 0 along with vote->last_demand + hold (e.g., for hold = 8: last_demand + hold == 3, and the samples are 2, 3, 11).
    • As a result, a completely wrap-unsafe implementation (now >= vote->last_demand + hold) passes 100% of the assertions in apple_clpc_vote_test() (the PR's negative control only mutated time_after_eq() to time_after(), which tests the off-by-one boundary rather than wrap safety).
    • Fix: Sample at least one hold check before the wrap while last_demand + hold has already wrapped (e.g., set t0 = ULONG_MAX - (hold - 1) so t0 + hold - 1 == ULONG_MAX while last_demand + hold == 0, or also assert at t0 + hold / 2 == ULONG_MAX), and add a wrap-unsafe now >= last_demand + hold mutant to the negative control.
  2. In-tree KUnit link dependency (drivers/soc/apple/clpc.kunitconfig + #2693):
    • As noted in the PR body, stock drivers/soc/apple/clpc.kunitconfig fails to link at integration/aurora HEAD without #2693 (still open), so this new suite cannot currently be run out of the tree as-is. Please sequence/merge after #2693.

Should-Fix / Minor Findings

  1. Misplaced invalid-entry comment (clpc.c:2119-2120): CPUFREQ_ENTRY_INVALID (~0u) is greater than 1200000, so stepping down from 1200000 can never select it even if cpufreq_for_each_valid_entry were broken. The cases that actually catch CPUFREQ_ENTRY_INVALID are the upward steps at 2000000 and 2100000 (clpc.c:2125, 2127). Please move/update the comment accordingly.
  2. Overstated apple_clpc_profile_limit() comment & weak < check (clpc.c:2131-2139): apple_clpc_profile_limit()'s fallback (if (cl->top_khz < low_power_khz) cl->top_khz = max_freq at clpc.c:887-890) is not exercised here, only apple_clpc_step_khz(..., low_power_khz - 1, true). Also, line 2139 uses KUNIT_EXPECT_LT(..., 2100000u) where KUNIT_EXPECT_EQ(..., 2100000u - 1) is strictly tighter (a buggy return of 600000 or 0 would pass LT).

§1–§6 Checklist

  • §1 No model gating: Pass (synthetic test frequencies only; zero driver changes).
  • §2 Production-ready / no hacks: Needs fix on the jiffies-wrap test and #2693 ordering so the suite actually tests what it claims and links from the tree.
  • §3 Safety: Pass (test-only).
  • §4 Clean-room & attribution: Pass (human author/committer + human Signed-off-by: only, zero AI trailers in commit; PR body discloses Assisted-by:).
  • §5 Evidence: Local KUnit run reported (15/15), but negative control missed wrap-unsafe comparison and CI does not execute apple-clpc-policy.
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

t0 = ULONG_MAX - hold / 2 puts every held sample past the wrap together
with last_demand + hold, so a plain 'now >= last_demand + hold' passes
the test. Start at ULONG_MAX - (hold - 1) instead: the last held sample
is ULONG_MAX while the deadline has wrapped to 0.

Also move the invalid-entry comment to the up steps that can pick it,
say the max_freq fallback of apple_clpc_profile_limit() is not covered,
and expect the exact value above the table instead of a bound.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Ran apple-clpc-policy at 309a55f merged onto integration/aurora 120d6aa (clean merge), kunit.py arm64/qemu, LLVM=1, HZ=250.

  • The stock drivers/soc/apple/clpc.kunitconfig still fails to link (undefined apple_smc_read / apple_smc_write) while soc: apple: clpc: link without the SMC driver #2693 is open. With MFD_MACSMC added, the suite passes 4/4 (all suites in that config: 17 pass, 1 skip).
  • The jiffies-wrap case did not test the wrap: with apple_clpc_vote() mutated to now >= vote->last_demand + msecs_to_jiffies(APPLE_CLPC_HOLD_MS) the suite still passed 4/4, because with t0 = ULONG_MAX - hold / 2 every held sample wraps together with the deadline.
  • An invalid-entry mutant in apple_clpc_step_khz() is caught only by the up steps at 2000000, 2100000 and 2099999, not by the down step the comment sat on. step_khz(2100000 - 1, true) returns 2099999, so EXPECT_EQ is the tighter check.

fb37343 (pushed on top, test only) starts the vote test at t0 = ULONG_MAX - (hold - 1), so the last held sample is ULONG_MAX while the deadline has wrapped to 0, and asserts that. It also moves the invalid-entry comment to the up steps, says the profile_limit() max_freq fallback is not covered, and uses EXPECT_EQ above the table. Results with it: real code 4/4 (all suites 17 pass, 1 skip); the wrap-unsafe mutant now fails at t0 + hold - 1; the time_after() and invalid-entry mutants still fail. checkpatch clean.

This still needs #2693 first (or MFD_MACSMC in the kunitconfig) before the suite runs from the tree as is.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial §1–§6 re-review at fb37343c17f8466efbd278dd256d25b6015b3fa6 (following Wave 36B REQUEST_CHANGES on 309a55f9c7d1): REQUEST_CHANGES (code is clean, but still blocked on #2693 before MERGETRAIN-LINUX4).

Verification of Prior Findings

  1. Prior Blocker 1 (apple_clpc_vote_test() wraparound coverage) — RESOLVED:
    • drivers/soc/apple/clpc.c:2177-2189 now starts at t0 = ULONG_MAX - (hold - 1) and asserts hold > 1 and t0 + hold - 1 == ULONG_MAX.
    • With vote.last_demand = t0, the hold deadline t0 + hold wraps to 0 while the final held sample t0 + hold - 1 is still ULONG_MAX: real time_after_eq(ULONG_MAX, 0) evaluates false (holds 5), whereas a wrap-unsafe now >= last_demand + hold evaluates ULONG_MAX >= 0 (drops early to 0) and is reliably killed. The t0 + hold == 0 check continues to kill time_after().
  2. Prior Minor Items 3 & 4 — RESOLVED:
    • The CPUFREQ_ENTRY_INVALID comment was moved to the upward-step cases that actually exercise it (clpc.c:2125-2131).
    • The apple_clpc_profile_limit() comment accurately states its max_freq fallback is not covered (clpc.c:2134-2137), and line 2143 is tightened to KUNIT_EXPECT_EQ(..., 2100000u - 1).
  3. Prior Blocker 2 (standalone in-tree clpc.kunitconfig link dependency on #2693) — STILL OPEN:
    • aurora-silicon/linux#2693 (soc: apple: clpc: link without the SMC driver) is still open (merged=False), and stock drivers/soc/apple/clpc.kunitconfig still fails to link (undefined apple_smc_read / apple_smc_write) from the tree without it (as also confirmed in your latest run comment).
    • All 7 CI checks are green at fb37343c, though none of the CI jobs build/run clpc.kunitconfig.

Readiness & §1–§6 Checklist

  • Code quality: Ready — zero driver changes, test logic and negative controls are sound at fb37343c.
  • Merge / MERGETRAIN-LINUX4 gate: Hold until #2693 lands (or drivers/soc/apple/clpc.kunitconfig builds and links standalone) so apple-clpc-policy is runnable directly from integration/aurora.
  • §1 No model gating: Pass (synthetic test frequencies only).
  • §2 Production-ready / no hacks: Test code passes; blocked only on #2693 ordering for in-tree clpc.kunitconfig buildability.
  • §3 Safety: Pass (test-only).
  • §4 Clean-room & attribution: Pass (both commits 309a55f9c7d1 and fb37343c17f8 have human author/committer + human Signed-off-by: only, zero AI commit trailers; PR body discloses Assisted-by:).
  • §5 Evidence: Local KUnit run (4/4 apple-clpc-policy, 17 pass / 1 skip overall) + wrap-unsafe / time_after() / invalid-entry mutation checks verified; CI 7/7 green.
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

The clpc.kunitconfig link blocker now sits in #2846 (r2 of #2693, rebased on the tip, Dj's probe-path blocker fixed). With it the stock kunitconfig builds and runs on arm64 qemu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants