Repository navigation
iio: aop_motion: calibrate once per boot and idle-stop direct reads - #2910
Conversation
Commit 9538a1d ("iio: aop_motion: run the reports only while someone reads them") stopped the AOP accelerometer and gyroscope reports when nobody reads them, saving ~100 AP wakeups/s on an idle desktop. However, its direct-read path calls Control::start() and Control::stop() around every individual sysfs axis read, and start() unconditionally resends all three calibration packets (properties 75, 76 and 77) before setting the report interval. On T6041 (M4 Max, J616C), Chromium polling the raw X/Y/Z sysfs channels at 25-60 Hz drives ~73 start/stop cycles/s (~365 EPIC RPCs/s). On every start, properties 75 and 76 return kIOReturnUnsupported (0xe00002c7) while property 77 is re-accepted, logging "2 of 3 calibration packets kept from an earlier start" over 1,200 times in 17 seconds until the AOP firmware crashes on endpoint 0x24, wedging ALS and audio for the rest of the boot. Keep the 0 Hz idle behaviour while eliminating both the calibration rewrite storm and per-axis start/stop thrashing: - Track calibration state in Stream so the three calibration packets are sent once at probe, and subsequent starts for buffers, direct reads and s2idle resume only write PROP_INTERVAL. On s2idle (!pm_suspend_via_firmware()), the AOP core does nothing so calibration stays valid. Across firmware suspend (pm_suspend_via_firmware()), hibernation and restore (PM_POST_HIBERNATION / PM_POST_RESTORE), clear calibrated and active conservatively so calibration is sent once on the next start (on firmwares that kept it, packets 75/76 return STATUS_UNSUPPORTED benignly once). Downgrade the kept-packets log message from dev_info! to dev_dbg!. - Hold direct-read reports active for a 2 s idle window via a DelayedWork item on system_dfl_wq. Back-to-back X/Y/Z reads and polling callers share the running 50 Hz stream and extend the deadline with no extra EPIC calls, while quiet periods return the AOP to interval 0. - Back off direct reads for DIRECT_IDLE_MS after a failed start so a polling client cannot hammer a failing service or spam refused() logs; buffer enable, sleep transitions and shutdown clear the backoff. - Only clear active after stop_reports() succeeds, and park on PM_RESTORE_PREPARE alongside suspend/hibernation prepare. Cancel and join the idle work synchronously with the stream mutex dropped on sleep prepare (cancel_delayed_work_sync) and permanently disable it on unbind or a failed iio.register() in probe (disable_delayed_work_sync), reclaiming any pending Arc reference cleanly. - Extend the host selftest in tools/testing/selftests/drivers/apple/aop-motion.py to cover the production Stream state machine (including failed-start backoff and failed-stop / reset_cal handling) and update Documentation/iio/aop-sensors.rst. Fixes: 9538a1d ("iio: aop_motion: run the reports only while someone reads them") Suggested-by: Ryan Murray <[email protected]> Signed-off-by: AxxzyWasTaken <[email protected]>
|
J616C (MacBook Pro 16" M4 Max, T6041) exact-head hardware soak at
Moving out of Draft — ready for review. (Also checked upcoming Assisted-by: LLM (Google:Gemini 4 Neon) |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Full adversarial review of 65ddaa0adcccd3d635eae59ec704279b39242392 against pre-pr-checklist.md (§1–§6): APPROVE.
Code, Concurrency & Safety Audit (§1–§4, §6) — clean
- §1 No model gating: Zero chip, SoC, board, or firmware-version gates. Uses standard kernel
pm_suspend_via_firmware()to distinguish s2idle (AOP stays up, calibration preserved) from firmware suspend/hibernation/restore (conservatively clears bothcalibratedandactive). Validated across 3 Mac families (T6041 M4 Max, T6040 M4 Pro, T6000 M1 Pro). - §2 Production-ready, no hacks: Works on the default kernel command line with no module parameters or race-workaround sleeps.
DIRECT_IDLE_MS = 2_000cleanly coalesces multi-axis sysfs reads and polling clients onto the running 50 Hz stream while returning the AOP to 0 Hz when idle;Stream::failed_untilcaps failing-service retries andrefused()log spam to at most once per idle window. - §3 Safety, locking & lifecycle:
- Calibration packets continue to come solely from device tree properties (
apple,range-intrinsic-cal,apple,nominal-extrinsic-cal,apple,range-extrinsic-cal). - Deadlock / lock ordering: Both
set_parked(true, _)andshutdown()sets.parked = trueands.direct_until = Noneunderstream.lock(), release the mutex, and only then callstop_idle_sync(), preventing AB-BA deadlock withControl::run()and preventing any racingread_now()orControl::run()from re-armingidle_work. - Unbind / probe teardown: Both
unbind()and theiio.register()error path inprobe()unregisterPmHook(synchronizing against in-flightpm_eventcallbacks) before callingcontrol.shutdown(), which usesdisable_delayed_work_syncto cancel, drain, and permanently block requeues. - Rust
unsafe&Arcrefcounting:stop_idle_sync()extractsbindings::delayed_workfrom the pinnedDelayedWork<Control>insideArc<Control>and reclaims exactly oneArcviaArc::from_raw(Arc::as_ptr(self))iffcancel_delayed_work_sync/disable_delayed_work_syncreports a pending item was cancelled (matchingkernel/workqueue.c__cancel_work_syncsemantics andaop_devmotion.rs::disable_poll).Instant<Monotonic>subtraction +Delta::as_millis_ceil()handles positive, zero, and negative deltas without overflow or off-by-one errors;bindings::pm_suspend_via_firmware()compiles cleanly with and withoutCONFIG_SUSPEND. - State fidelity:
s.activeis cleared only afterstop_reports()succeeds, andset_parked(false, true)resets bothcalibratedandactive. - Tests & docs: Host selftest in
tools/testing/selftests/drivers/apple/aop-motion.pycompiles and verifies the productionStream<T>/SyncAction/IdleStepstate machine (including backoff and failed-stop/reset_calpaths), andDocumentation/iio/aop-sensors.rstmatches the code.
- Calibration packets continue to come solely from device tree properties (
- §4 Clean-room & attribution / §6 Naming: Single commit
65ddaa0adcccd3d635eae59ec704279b39242392with human author/committer (AxxzyWasTaken) and humanSigned-off-by:only, zero AI trailers in commits; PR body carriesAssisted-by:; naming is clean.
§5 Evidence & CI — satisfied
- CI:
Host tests / host-testsSUCCESSon65ddaa0adcccd3d635eae59ec704279b39242392;MERGEABLE(CLEAN), non-draft. - J616C (M4 Max, T6041, exact head
65ddaa0adccc,7.3.0-j616c-aopfix):accel.1.auto+gyro.2.autobound, idle3.6 recv/s(0.0 send/s), 2 s direct-read idle-stop returns to3.6 recv/s, 90.5 s 60 Hz sysfs soak (16,209accel +16,203gyro reads,0errors,103 recv/smedian), 60 s headless Chromium Generic Sensor soak (3,520devicemotion,2,910accel/gyro,err: []), 3×unbind/bindinside the 2 s hold (0–1 ms,0warnings), 2×rtcwake -m freeze -s 10(2/2PASS, immediate post-resume reads), siblingals/laslive,0AOP crashes,0calib-keptspam,0failed units,0AVC denials. - J616s (M4 Pro, T6040) & M1 Pro (T6000) A/B (
@RyanTheTide): AOP mailbox IRQ/s reduced from ~714–740/s (with 114–115/scalib-keptspam) to ~103–107/s (0 spam), 300–600 s soaks,bufmix,unbindhold×3, ands2idle×2 all clean.
(Non-blocking notes: (1) the PR body still has a stale Draft placeholder line for J616C soak that can be refreshed from the comment; (2) when rebasing alongside #2936, #2936's new iio_aop_motion KUnit test will need Stream::new() + idle_work <- new_delayed_work!(...) in its Control initializer — already noted by the author.)
Assisted-by: LLM (Gemini 4 Neon)
|
Merge-train review at 65ddaa0 (non-author pass): no blockers. I read the whole Lifecycle and locking
Selftest and mutants (tools/testing/selftests/drivers/apple/aop-motion.py, run at this head)10/10 pass. I applied each mutant to the driver source one at a time and reran:
Notes, not blocking
Assisted-by: LLM (Claude:Opus 5.5) |
5129ba7
into
aurora-silicon:integration/aurora
Problem
Commit
9538a1d2111f("iio: aop_motion: run the reports only while someone reads them") stopped the AOP accelerometer and gyroscope reports when idle, which saved ~100 AP wakeups/s on an idle desktop. However, its direct-read path (State::read_now()) callsControl::start()andControl::stop()around every individual sysfs axis read (in_accel_{x,y,z}_raw/in_anglvel_{x,y,z}_raw), andControl::start()unconditionally re-sends all three calibration packets (properties 75, 76, 77) before writingPROP_INTERVAL = 20_000.On T6041 (M4 Max, J616C,
7.3.0-j616c-ia34), opening a web page that polls the Generic Sensor API in Chromium (x.cominhelium-browser) polls X/Y/Z sysfs continuously at ~25-60 Hz:start()/stop()cycle (~73 cycles/s, ~365 EPIC RPCs/s).start(), properties 75 and 76 return0xe00002c7(STATUS_UNSUPPORTED) while property 77 returns0(re-accepted every time), logging2 of 3 calibration packets kept from an earlier start1,241 times in 17 seconds.apple_aop 510c10000.aop: RTKit: co-processor has crashed, mailbox history 100% endpoint0x24, taskrtk_ep_work), permanently freezing ALS and breaking speaker endpoint 34 for the rest of the boot.Root Cause
start()repeatedly triggers property 77 handling and spamsdmesg.read_raw()calls. Starting and stoppingPROP_INTERVALinside each individualread_now()turns a single 3-axis read into 3 start/stop cycles, and turns browser polling into hundreds of EPIC RPCs/sec.Fix
Preserves
9538a1d2111f's 0 Hz idle AOP goal while eliminating both the calibration re-write storm and per-axis start/stop thrashing (incorporating review feedback from@RyanTheTide):Stream::calibratedunderControl::stream. Probe runsprobe_init()(calibrate once, verify interval, stop to 0 Hz). Subsequent starts (buffer enable, direct read, s2idlePM_POST_SUSPEND) only writePROP_INTERVAL = 20_000.pm_event(), handlePM_SUSPEND_PREPARE | PM_HIBERNATION_PREPARE | PM_RESTORE_PREPAREby parking,PM_POST_SUSPENDwithreset_cal = pm_suspend_via_firmware(), andPM_POST_HIBERNATION | PM_POST_RESTOREwithreset_cal = true. On s2idle (!pm_suspend_via_firmware()), the AOP core does nothing so calibration stays valid; across firmware suspend, hibernation, and restore,calibratedandactiveare reset conservatively so calibration is sent once on the next start (firmwares that kept it answer packets 75/76 withSTATUS_UNSUPPORTEDbenignly once)."N of 3 calibration packets kept from an earlier start"message fromdev_info!todev_dbg!.DIRECT_IDLE_MS = 2_000) + failed-start backoff:read_now()without an enabled buffer starts reports if idle, waits up toFIRST_SAMPLE_MSfor the first fresh sample, setsdirect_until = now + 2s, and schedules aDelayedWorkitem (Control::idle_work) onsystem_dfl_wq.direct_untilwith zero EPIC RPCs and zero timer churn.idle_workfires, it checks remaining time viaStream::check_idle(); if more reads extended the deadline it re-arms for the remaining delta, otherwise it clearsdirect_untiland stops reports (PROP_INTERVAL = 0), returning the AOP to 0 Hz.Stream::failed_untilremembers(deadline, error)forDIRECT_IDLE_MSandStream::check_backoff()returns that error without calling the service again until the deadline passes (bounding failing-service traffic andrefused()logs to at most once perDIRECT_IDLE_MS). Enabling a buffer, system sleep transitions, andshutdown()clear the backoff.s.active = falseinsync_locked()afterstop_reports()succeeds;set_parked(false, true)clears bothcalibratedandactive.direct_until; disabling the buffer stops immediately.PM_SUSPEND_PREPARE/PM_HIBERNATION_PREPARE/PM_RESTORE_PREPAREstop reports under the mutex, release the mutex, and callcancel_delayed_work_sync()(reclaiming any pendingArcreference);unbind()andprobe()failure afteriio.register()unregisterpmand callshutdown()withdisable_delayed_work_sync()before teardown.tools/testing/selftests/drivers/apple/aop-motion.pyto compile and test the productionStream<T>,SyncAction, andIdleStepstate machine (single calibration, direct-stream reuse, idle expiry/re-arm, buffer handoff, suspend vs hibernation calibration policy, failed-start backoff, and failed-stop /reset_calhandling), and updatedDocumentation/iio/aop-sensors.rst.Hardware A/B Results (credit:
@RyanTheTide)Measured via runtime module swap against stock fleet kernels (kit
20261123-aop-motion2):J616s (M4 Pro, T6040,
7.3.0-aurora-fleet81)axb1)axb1)bo1)bufmix(60 Hz direct reads across 6 buffer enable/disable cycles): 0 direct-read errors, buffer ~1.2 kB/s accel, idle 7/s after.unbindhold(rmmodinside the 2 s hold x3): 8-16 ms, 0 WARN/BUG/refcount lines, reloads cleanly.s2idle15 s x2 per arm: OK, direct reads immediately after resume OK, idle 7/s after; ALS, LAS, als-temp, and both AOP mics unchanged.M1 Pro (T6000,
7.3.0-aurora-fleet82)f82axb1)f82axb1)s2idlex2 OK;bufmix0 errors;unbindholdx3 18-22 ms, 0 WARN. (Pre-existing gap on stock too: M1 Pro accel and gyro read0 0 0.)M4 Max (J616C, T6041,
7.3.0-j616c-aopfixat65ddaa0adccc)accel.1.auto+gyro.2.autobound,0failed units; idle AOP mailbox IRQ3.6 recv/s(0.0 send/s).102.5 recv/sduring hold, returns cleanly to3.6 recv/s.poll.py):16,209accel +16,203gyro reads,0errors,103 recv/smedian,0AOP crashes,0calib-keptspam.--headless=new --mute-audio):3,520devicemotion,2,910accel,2,910gyro,err: [],0AOP crashes.unbind/bindinside 2 s hold (3xaccel.1.auto):0–1 ms,0warnings; 2xrtcwake -d /dev/rtc1 -m freeze -s 10:2/2PASS, immediate post-resume reads; siblingals/laslive,0AVC denials.Local Verification
python3 tools/testing/selftests/drivers/apple/aop-motion.py: 10/10 tests pass (-Dwarnings)python3 tools/testing/selftests/drivers/apple/aop-las.pyandtools/asahi/aop-*.py: all passrustfmt --check --edition 2021 drivers/iio/common/aop_sensors/aop_motion.rs: clean (0 diffs)scripts/checkpatch.pl --strict --git HEAD~1..HEAD: 0 errors, 0 warnings, 0 checksmake ARCH=arm64 LLVM=1 W=1 KBUILD_MODPOST_WARN=1 O=... drivers/iio/common/aop_sensors/aop_motion.ko: clean compile and link (0 warnings, 0 errors;KBUILD_MODPOST_WARN=1only becausevmlinux.owas not built in the isolated objdir)Fixes: 9538a1d ("iio: aop_motion: run the reports only while someone reads them")
Suggested-by: Ryan Murray [email protected]
Assisted-by: LLM (Anthropic:Claude Opus 5.5, Google:Gemini 4 Neon)