Skip to content

soc: apple: pmp: avoid unaligned read, panicking unwraps, and vector desync - #4864

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
stepanoid2005:fix/pmp-msg-buffer-safety
Dec 6, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
stepanoid2005:fix/pmp-msg-buffer-safety

Conversation

@stepanoid2005

Copy link
Copy Markdown
Contributor

What: Harden PMP RTKit message handlers in drivers/soc/apple/pmp.rs: replace the unaligned raw *const u64 read in set_buf() with u64::from_le_bytes(), replace panicking .unwrap() calls in register_ioreg() with error propagation, guard DT tunable length conversion with u32::try_from(), reserve both ioreg_entries and ioreg_ids before pushing to prevent OOM desync, use lossless u64::from() widening in register_ioreg()/set_ioreg(), and document display_send() and PmpData Send/Sync safety contracts.
Tested: J713 (MacBook Air 13" M4, T8132) at 743e985843ea (default kernel command line with user4k=on, candidate 7.3.0-j713-20261206+ containing 743e985843ea unchanged on upstream/main 9d46508f02b6): boots with 10/10 CPUs online, 0 failed units, 0 PMP errors; apple_pmp, apple_pmp_thermal, and apple_pmp_power bind cleanly (PMP firmware and endpoint ready, J713 PMP vote bridge ready; retained existing PMP owner, pmp_sum_w=0.080 W median, pmp_hotspot_c=50.0 C median).
Risk: two-way door · Apple Silicon Macs using apple_pmp (M1 Pro/Max/Ultra, M2–M5, Neo); preserves existing valid-path behavior while replacing panics and unaligned pointer reads with error returns.
Not yet: Hardware runs on non-T8132 PMP Macs (M1 Pro/Max/Ultra, M2, M3, M4 Pro/Max, M5, Neo).

Details
  • set_buf(): replaces unsafe { *(ptr_buf.as_ptr() as *const u64) } with u64::from_le_bytes() over the bounds-checked 8-byte slice.
  • register_ioreg(): propagates errors instead of .unwrap() on CStr::from_bytes_until_nul() and state.value_buf, checks u32::try_from(len), widens with u64::from(), and calls reserve(1, GFP_KERNEL) on both state.ioreg_entries and state.ioreg_ids before pushing so an OOM on the second push cannot desynchronize the parallel vectors.
  • Safety comments: adds # Safety documentation on display_send() and // SAFETY: comments on unsafe impl Send/Sync for PmpData, and removes a stale // SAFETY: comment above safe dev.get_drvdata().
Evidence
  • Commit: 743e985843ea on upstream/main (9d46508f02b6).
  • Static/compile checks: scripts/checkpatch.pl --strict --codespell 0 errors / 0 warnings; rustfmt --edition 2021 --check drivers/soc/apple/pmp.rs PASS; ARCH=arm64 LLVM=1 CLIPPY=1 drivers/soc/apple/pmp.o 0 warnings on touched lines.
  • Live J713 (T8132, 7.3.0-j713-20261206+): apple_pmp, apple_pmp_thermal, and apple_pmp_power (hwmon0/hwmon8) active with pmp_sum_w=0.080 W median and pmp_hotspot_c=50.0 C median, 0 PMP errors, 10/10 CPUs online, 0 failed units.

…desync

Harden PMP RTKit message handlers in drivers/soc/apple/pmp.rs:

- Replace the unaligned raw pointer dereference in set_buf() with
  u64::from_le_bytes() over the checked 8-byte slice.
- Replace unchecked .unwrap() calls in register_ioreg() with error
  propagation, guard the u32 conversion of DT tunable length with
  u32::try_from(), use lossless u64::from() widening in
  register_ioreg() and set_ioreg(), and reserve capacity in both
  state.ioreg_entries and state.ioreg_ids before pushing so OOM on the
  second push cannot leave the parallel vectors out of sync.
- Add missing SAFETY documentation on display_send() and PmpData
  Send/Sync impls and remove a stale SAFETY comment above safe
  dev.get_drvdata().

Signed-off-by: Stepan Gulyaev <stepanoid2005@users.noreply.github.com>
@stepanoid2005
stepanoid2005 requested review from a team as code owners December 6, 2026 15:10
@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) blast:m1 Macs affected, from Merge risk (bot) blast:m2 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 6, 2026
@github-actions

github-actions Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Merge readiness at 743e9858: ready to merge

Check Detail
CI at head ok 6 checks passed
Review ok approved by RyanTheTide, aurora-silicon-review, satchlj
Requested changes ok none
Hardware row at head ok the PR body at 743e9858
Conflicts ok none
PR text ok Risk: two-way · Apple Silicon Macs using applepmp (M1 Pro/Max/Ultra, M2–M5, Neo); preserves existing valid-path behavior while replacing panics and unaligned pointer reads with error returns.

Areas: area:platform
Merge: gh pr merge 4864 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 743e985843ea (code-only; non-T8132 PMP hardware untested here):

  • set_buf() replaces the unaligned *const u64 dereference with u64::from_le_bytes() over the bounds-checked [..8] slice (ptr_buf.size() >= 8 verified directly above).
  • register_ioreg() replaces all panicking .unwrap() calls with error propagation (hdr[0x40..0x44] / hdr[0..0x30] guarded by msg_buf.size() >= 0x44, hdr[0x48..0x4c] guarded by hdr.len() >= 0x4c), validates u32::try_from(len), widens losslessly via u64::from(), and reserves capacity on both state.ioreg_entries and state.ioreg_ids before pushing so an OOM cannot desynchronize the parallel vectors.
  • display_send() and Send/Sync safety comments are clear and accurate.

@RyanTheTide RyanTheTide 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.

Checked that message lengths precede the byte reads and the existing little-endian fields and reply layout are preserved. Both vectors reserve capacity under the same state lock before either length changes. The reported J713 boot covers the live PMP path.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 6, 2026

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: MERGE at 743e985
Checked: The DMA reads stay within the checked byte-slice bounds and preserve little-endian decoding. Both vectors reserve under the same state mutex before either gains an entry. Verified the exact KVec implementation takes the no-allocation path for those subsequent pushes. The checked tunable length is converted before copying. The contributor reports clean J713 PMP, thermal and power initialization.

@satchlj

satchlj commented Dec 6, 2026

Copy link
Copy Markdown
Member

Hardware-tested on MacBook Air 13" M3 (J613, T8122, 7.3.0-pmp4864-ARCH+ built from 743e985843ea7dd153587603e87029705e995a84 on the machine's standard kernel command line) — PASS.

Checks run (2d0500000.pmp / apple_pmp)

  • Probe & tunable load (register_ioreg / set_buf): apple-t8122-display populated 61 PMP values on /soc/pmp@2d0500000; apple_pmp 2d0500000.pmp completed RTKit protocol v12 init (PMP firmware and endpoint ready, RTKit: syslog message: io.cpp:61: PMP started) and parsed 6 temperature records (0x59dcc..0x5a100, data from 0x36000).
  • Consumer & debugfs checks: apple-pmp-report 2d03c0000.pmp_report reported running: 1, start: ready, start_result: 0, enabled: 1, PMP ready: PTD status 0x1 request 0x81800 ack 0x18f0; both 28ec00000.dcp and 31ac00000.dcp logged PMP acknowledged the display request; memory floor raise/release transitions worked normally.
  • hwmon (apple_pmp, hwmon2) live reads:
    • Pre-sleep: PMP aggregate die hotspot 34750 mC (52000 mC under load), Ta000m 34500, Te000m 34750, Te001m 34000
    • Post 12 s s2idle (/dev/rtc1 wake): PMP aggregate die hotspot 41250 mC (52000 mC under load), Ta000m 35000, Te000m 35500
    • Post 15 s deep (/dev/rtc1 wake, including full RTKit sleep return: ASC control 0x0, setting RUN + protocol v12 re-init): PMP aggregate die hotspot 35000 mC (52000 mC under load), Ta000m 34750, Te000m 35000, Te001m 34000, Tp001m 34000
  • Health: 0 failed systemd units; 0 PMP/RTKit/panic/Oops/BUG/WARNING lines in dmesg across boot, s2idle, and deep.

Untested on hardware

  • Error paths for malformed/undersized RTKit message payloads (set_buf / ioreg unaligned or short-buffer rejection), OOM Vec::try_reserve failure, and apple,pio-ranges (not present on this T8122 DT node) were verified by code review only.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Verified exact merge-tree against origin/main (ca9993483846) locally (Hosted CI override for skipped checks):

  • ./scripts/checkpatch.pl --strict --git 743e985843ea: 0 errors, 0 warnings
  • make ARCH=arm64 LLVM=1 drivers/soc/apple/pmp.o: clean build

Triple-approved (satchlj + RyanTheTide + aurora-silicon-review) with exact-head J613 hardware verification (satchlj) and readiness green; merging.

@DjDeveloperr
DjDeveloperr merged commit 00fb4cc into aurora-silicon:main Dec 6, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform Area from CODEOWNERS (bot) blast:m1 Macs affected, from Merge risk (bot) blast:m2 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants