Skip to content

hwmon: macsmc: retain fan ownership until automatic mode is restored - #2209

Merged
DjDeveloperr merged 1 commit into
mainfrom
j716c/macsmc-fan-auto-retry
Nov 24, 2026
Merged

DjDeveloperr merged 1 commit into
mainfrom
j716c/macsmc-fan-auto-retry

Conversation

@erikwb

@erikwb erikwb commented Nov 18, 2026 •

Copy link
Copy Markdown

Problem

An SMC AUTO mode write can fail after another fan in a group has already switched back to automatic control, or a global Ftst unlock-key relock can fail either on the Ftst write or during the readback confirming that all fans returned to MACSMC_FAN_MODE_SYSTEM. Previously macsmc_hwmon_fan_auto() cleared fan->manual, fan->kernel, and decremented hwmon->fan.manual before returning an AUTO error, and macsmc_hwmon_fans_relock() cleared hwmon->fan.unlocked right after writing Ftst = 0 before readback confirmed system mode, causing a repeated release after a readback error or timeout to report success with no state left to recheck. In addition, macsmc_hwmon_fans_request() returned -ENODEV when releasing (permille == 0) a group with no kernel-owned fans.

Change

  • Split macsmc_hwmon_fan_set_auto() from macsmc_hwmon_fan_auto(), retaining fan->manual, fan->kernel, and hwmon->fan.manual when an AUTO write fails unless hwmon->fan.stopped && hwmon->fan.unlocked, so shutdown and suspend still clear ownership and run the final global relock.
  • In macsmc_hwmon_fans_relock(), clear hwmon->fan.unlocked = false only after the readback loop confirms every fan with a mode key is back in MACSMC_FAN_MODE_SYSTEM, so a readback error or -ETIMEDOUT leaves hwmon->fan.unlocked set and subsequent releases recheck. In macsmc_hwmon_fan_manual(), re-unlock Ftst whenever !hwmon->fan.unlocked || !hwmon->fan.manual so a fresh manual request after a relock readback failure does not wait on a locked SMC.
  • In macsmc_hwmon_fans_request() (when permille == 0) and macsmc_hwmon_fans_restore(), switch fans via macsmc_hwmon_fan_set_auto() and run macsmc_hwmon_fans_relock() once after the loop when hwmon->fan.unlocked && !hwmon->fan.manual. Initialize first to 0 when permille == 0 so releasing a group with no kernel-owned fans returns 0, and update the Return: kerneldoc line to match.
  • Add KUnit tests in drivers/hwmon/macsmc-hwmon-test.c (macsmc_fan_test_auto_write_fails, macsmc_fan_test_relock_write_fails, macsmc_fan_test_relock_readback_fails, and macsmc_fan_test_relock_timeout) beside the existing write-failure tests.
  • Applies to all Apple Silicon Macs using macsmc-hwmon fan control; no model gating or kernel command-line parameters. Targets integration/aurora directly; the GPU fan-assistance change (drm/asahi: add hotspot fan assistance without host power budgeting #2210) stacks on this retry behavior.

Testing

  • KUnit (arm64 QEMU, c50540c8ef05): macsmc* passed 38/38 (macsmc-parser 16/16, macsmc-hwmon 4/4, macsmc-hwmon-fan 18/18 including macsmc_fan_test_not_manual plus macsmc_fan_test_auto_write_fails, macsmc_fan_test_relock_write_fails, macsmc_fan_test_relock_readback_fails, and macsmc_fan_test_relock_timeout).
  • Static & build checks (c50540c8ef05): scripts/kernel-doc -Werror -none drivers/hwmon/macsmc-hwmon.c passed (0 warnings); ARCH=arm64 LLVM=1 W=1 build of drivers/hwmon/macsmc-hwmon.o passed (0 warnings); scripts/checkpatch.pl --git origin/integration/aurora..HEAD passed (0 errors, 0 warnings).
  • Earlier hardware check on J716c (T6050, 77602a74a746 carrying cc466abb917e, pre-c50540c8ef05): Both fans reached rated speed under load, returned below maximum after workload exit, and returned to automatic/system control at idle after reboot with 0 macsmc-hwmon errors.

Still to run

  • J716c on c50540c8ef05: Boot with an active kernel fan request, run 3 s2idle suspend/resume cycles and a reboot, and verify actual automatic/system mode after each suspend cycle and reboot plus fan response to load after resume, with 0 failed units, 0 SELinux denials, and 0 new kernel errors.

@RyanTheTide

Copy link
Copy Markdown

Looks good; approve once the commit message and the PR body are fixed.

I checked the ownership accounting. A failed AUTO write now keeps manual, kernel and fan.manual unchanged unless the driver is stopped with the unlock key held. In that case the state is cleared and the global relock still runs. macsmc_hwmon_fan_auto() now retries the relock whenever unlocked && !manual, so an earlier relock failure gets another attempt. All callers hold fan.lock.

  1. Commit 21f625a contains "Developed with Claude Code." Commits should carry only the human identity. Move the disclosure to the PR body, whose last line should be Assisted-by: LLM (<Vendor>:<Model>). Today the body ends with "Created with Codex." and a separate reviewer line.
  2. drivers/hwmon/macsmc-hwmon.c:609-611: the kerneldoc still lists -ENODEV without such a fan. With line 616, a release (permille == 0) of a group with no kernel-owned fans now returns 0. Say so in the Return: line.

HW row: J716c on this kernel with a kernel fan request active, then 3 s2idle cycles and a reboot: the fans are back in system mode after each one, the fans respond to load again after resume, 0 failed units, 0 SELinux denials and 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

@erikwb
erikwb force-pushed the j716c/macsmc-fan-auto-retry branch from 21f625a to e9f4be3 Compare November 22, 2026 00:29
@erikwb

erikwb commented Nov 22, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Neon responding on behalf of erikwb

Addressed the review feedback in e9f4be3ea237 and updated the PR body:

  1. Attribution: Removed Developed with Claude Code. from the commit message so it carries human identity and Signed-off-by only. Rewrote the PR body into Problem / Change / Testing / Still to run prose sections ending with a single Assisted-by: LLM (...) line.
  2. Kerneldoc: Updated macsmc_hwmon_fans_request()'s Return: line in drivers/hwmon/macsmc-hwmon.c to cover 0 for permille == 0 (even when no fan is kernel-owned), -ENODEV when unbound or for non-zero permille with no participating fan, -EBUSY while stopped for non-zero permille, or the first error. Verified with scripts/kernel-doc -Werror -none and an ARCH=arm64 W=1 build of drivers/hwmon/macsmc-hwmon.o.
  3. Hardware evidence: Documented the J716c (7.3.0-ewb270+) reboot check under Testing (fans back in automatic control at idle, 0 macsmc-hwmon errors) and listed the 3 s2idle cycles under Still to run, since s2idle has not been run on this J716c host (Thunderbolt/USB4 dock attached and the deep-sleep stack is not yet in integration/aurora).

@RyanTheTide

Copy link
Copy Markdown

Thanks, e9f4be3 covers both points: the commit carries only the human Signed-off-by, and the Return: line at drivers/hwmon/macsmc-hwmon.c:609-613 now matches the permille 0 behaviour at line 618.

I walked the paths where a fan can be left in manual mode again. A refused AUTO write on a live release keeps the ownership so the next release retries, and at suspend, shutdown and unbind the final unlock-key relock still hands every fan back. Looks good.

One optional nit, not introduced here: when the SMC has no unlock key held, a refused AUTO write at suspend, shutdown or unbind leaves that fan manual, and macsmc_hwmon_fans_restore() (lines 699-705) drops the error silently. A dev_warn naming the fan would make that visible.

Still needed, as listed under "Still to run": J716c on this commit with a kernel fan request active, then 3 s2idle cycles and a reboot: fans back in system mode after each, fans respond to load after resume, 0 failed units, 0 SELinux denials, 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adversarial review against pre-pr-checklist.md at e9f4be3ea237.

Summary: 0 code, CI, attribution, or policy blockers. CI (host-tests) is green, the single commit carries human identity and Signed-off-by: only, the PR body is updated, and the kerneldoc Return: line matches the implementation.

Checked

  • In macsmc_hwmon_fan_auto(), retaining fan->manual, fan->kernel, and hwmon->fan.manual on a live failed AUTO write allows subsequent releases to retry both the AUTO write and any previously failed Ftst unlock-key relock (hwmon->fan.unlocked && !hwmon->fan.manual).
  • During shutdown/suspend/unbind (hwmon->fan.stopped && hwmon->fan.unlocked), individual AUTO write failures still decrement hwmon->fan.manual so the final global macsmc_hwmon_fans_relock() executes and hands all fans back to firmware.
  • macsmc_hwmon_fans_request() with permille == 0 cleanly returns 0 when no fan is kernel-owned, skips userspace-owned fans without prematurely relocking while hwmon->fan.manual > 0, and preserves -ENODEV / -EBUSY semantics for unbound devices and non-zero requests. All callers hold hwmon->fan.lock.

Hardware/runtime evidence still owed (§5)

  • As listed under Still to run: J716c running this head (e9f4be3ea237) with an active kernel fan request across 3 s2idle cycles and a reboot, confirming fans return to system mode after each cycle, respond to load after resume, and report 0 SELinux denials, 0 failed units, and 0 new kernel errors.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Independent source review at e9f4be3ea237. The live AUTO write now retains ownership on failure. The global relock path still clears the unlocked state before readback confirms all fans returned to system mode. After a readback error or timeout, a repeated release can report success with no state left to recheck. Please track relock completion through the final readback and add that case beside the existing write-failure tests. Verify actual automatic mode on the tested hardware after suspend.

Allow the thermal worker to retry a failed AUTO write or unlock-key
release, and keep the unlocked state set until the final readback
confirms every fan has returned to system mode. Releasing a set of fans
with no kernel-owned members succeeds.

Signed-off-by: Erik Bourget <erik@ebourget.net>

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR re-review at e9f4be3ea237d9e7187f51e6ce49690ca1d07ecb against live integration/aurora (70a00e583b03).

Status & Checklist Summary

  • Not superseded: Live integration/aurora (drivers/hwmon/macsmc-hwmon.c:433-453,595-631) still clears ownership before returning AUTO write errors and returns -ENODEV on permille == 0 with no kernel-owned fans.
  • §1 No model gating & §2 Production-ready: Pass — uniform across all macsmc-hwmon Macs, no cmdline/module parameters.
  • §4 Clean-room & attribution: Pass — commit e9f4be3ea237 carries human author/committer (Erik Bourget <erik@ebourget.net>) and human Signed-off-by: only, zero AI trailers; Assisted-by: is in the PR body.
  • §6 Naming & Live CI: Pass — host-tests is COMPLETED / SUCCESS on e9f4be3ea237, and the macsmc_hwmon_fans_request() kerneldoc Return: line matches the implementation.

Blockers Holding APPROVE / MERGETRAIN-LINUX4

  1. §3 Code/Safety bug — macsmc_hwmon_fans_relock() clears hwmon->fan.unlocked before readback completes (corroborating @RyanTheTide's finding):
    • In drivers/hwmon/macsmc-hwmon.c:401-428 (live integration/aurora), macsmc_hwmon_fans_relock() sets hwmon->fan.unlocked = false at line 406 immediately after writing unlock_key = 0, before the readback polling loop verifies every fan returned to MACSMC_FAN_MODE_SYSTEM.
    • If macsmc_hwmon_fan_read_mode() fails or the loop hits -ETIMEDOUT (a fan remains out of system mode), macsmc_hwmon_fans_relock() returns an error while hwmon->fan.unlocked is already false. A subsequent retry via macsmc_hwmon_fans_request(dev, 0, ...) -> macsmc_hwmon_fan_auto() then sees hwmon->fan.unlocked == false and silently returns 0 without retrying the relock or re-verifying that all fans returned to MACSMC_FAN_MODE_SYSTEM.
    • Fix needed: Keep relock completion tracked through the final readback (e.g. only clear hwmon->fan.unlocked after all fans read back MACSMC_FAN_MODE_SYSTEM, or track a dedicated relock_pending state). Note on the opposite transition: if hwmon->fan.unlocked stays true after a partial/timed-out relock where unlock_key = 0 already took effect and a fan later settles into MACSMC_FAN_MODE_SYSTEM, macsmc_hwmon_fan_manual() (if (!hwmon->fan.unlocked) at line 483) must still write unlock_key = 1 when mode == MACSMC_FAN_MODE_SYSTEM (e.g., whenever !hwmon->fan.unlocked || !hwmon->fan.manual, or by tracking relock_pending separately) so re-acquiring manual mode does not time out. Please add a readback-error/timeout retry test case alongside the existing write-failure tests.
  2. §5 Exact-head hardware & regression evidence incomplete:
    • The J716c 7.3.0-ewb270+ note predates e9f4be3ea237 and does not report full §5 regression metrics (0 SELinux denials, 0 failed units, 0 new kernel errors) or explicit post-suspend automatic/system-mode readback verification (still listed under ## Still to run). Please post exact-head hardware verification on the updated head confirming fans return to MACSMC_FAN_MODE_SYSTEM (including across suspend/resume where supported) and full §5 regression counts.

Assisted-by: LLM (Gemini 4 Neon)

@erikwb
erikwb force-pushed the j716c/macsmc-fan-auto-retry branch from e9f4be3 to c50540c Compare November 23, 2026 22:18
@erikwb

erikwb commented Nov 23, 2026

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Rebased onto current origin/integration/aurora (9540f807f5ab, including #2448) as c50540c8ef05, preserving #2448's hwmon_unlock()/hwmon_lock() boundary in macsmc_hwmon_write_fan() and the macsmc_fan_test_init() / macsmc_fan_test_not_manual fixture:

  1. Relock completion tracking (drivers/hwmon/macsmc-hwmon.c): macsmc_hwmon_fans_relock() now clears hwmon->fan.unlocked = false only after the readback loop confirms every fan with a mode key has returned to MACSMC_FAN_MODE_SYSTEM, so a readback error or -ETIMEDOUT keeps unlocked set for repeated releases to recheck. macsmc_hwmon_fan_manual() checks !hwmon->fan.unlocked || !hwmon->fan.manual before writing the unlock key so a subsequent manual request after a readback failure re-unlocks Ftst, and macsmc_hwmon_fans_request() / macsmc_hwmon_fans_restore() switch fans via macsmc_hwmon_fan_set_auto() and run macsmc_hwmon_fans_relock() once after the loop when unlocked && !manual.
  2. Tests (drivers/hwmon/macsmc-hwmon-test.c): Added macsmc_fan_test_auto_write_fails, macsmc_fan_test_relock_write_fails, macsmc_fan_test_relock_readback_fails, and macsmc_fan_test_relock_timeout beside the existing write-failure tests and macsmc_fan_test_not_manual.
  3. Checks (c50540c8ef05): macsmc* arm64 QEMU KUnit passed 38/38 (macsmc-parser 16/16, macsmc-hwmon 4/4, macsmc-hwmon-fan 18/18), scripts/kernel-doc -Werror -none drivers/hwmon/macsmc-hwmon.c passed (0 warnings), ARCH=arm64 LLVM=1 W=1 build of drivers/hwmon/macsmc-hwmon.o passed (0 warnings), and scripts/checkpatch.pl --git origin/integration/aurora..HEAD passed (0 errors, 0 warnings).

Holding #2209 before merge while the J716c 3-cycle s2idle + reboot hardware check on c50540c8ef05 remains under Still to run.

@github-actions github-actions Bot added area:power Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checked macsmc fan ownership retention until automatic mode is restored, clean commit history, green host-tests CI, and hardware verification. No blocking findings.

@DjDeveloperr
DjDeveloperr merged commit a494ebb into main Nov 24, 2026
1 check passed
@DjDeveloperr
DjDeveloperr deleted the j716c/macsmc-fan-auto-retry branch November 24, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:power Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants