Skip to content

ASoC: apple: t8152-aop-audio: check the sense reader's pointer before using it - #3312

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/t8152-sense-appl-ptr
Nov 29, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/t8152-sense-appl-ptr

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: The speaker sense ring callback now checks the sense reader's appl_ptr before computing how many frames are unread. Before, a value past hw_ptr plus the boundary (for example ULONG_MAX written through the mapped control page) underflowed hw_ptr + boundary - appl_ptr. With Rust overflow checks that panics, a BUG() in the DMA callback. An out-of-range pointer now stops the stream with an XRUN. The sense front-end also sets SNDRV_PCM_INFO_SYNC_APPLPTR, so its reader moves appl_ptr through ALSA's checked path instead of a writable control page.
Tested: KUnit at dfc98f5 (arm64 QEMU, SND_SOC_APPLE_T8152_AOP_AUDIO=y, RUST_OVERFLOW_CHECKS=y, RUST_DEBUG_ASSERTIONS=y): snd_soc_apple_t8152_aop_audio 8/8 (3 new), the whole run 48 passed and 1 skipped (unrelated). With the old arithmetic restored, the new test panics with attempt to subtract with overflow and kernel BUG at rust/helpers/bug.c:7. Hardware row still owed (below).
Risk: two-way door · Macs with the driver-owned sense ring: T6050 (J714s, J716c) and T8142 (J813, J815). T8152 (J873g) has no sense ring and is not affected.
Not yet: J714s row at this head on the Speaker Sense capture PCM: speaker protection running at 5% volume with sense frames flowing, the control page no longer mappable, a malformed appl_ptr refused, and 0 Rust panics, BUG or Oops.

Details
  • sense_fe_unread(hw_ptr, appl_ptr, boundary) returns the unread frames, or None unless the boundary is non-zero and both pointers are below it. The modular case uses checked_add.
  • sense_fe_takes() combines that with the existing sense_ring_has_fe_space(), which was already checked. deliver() stops the stream with an XRUN when it says no, as it did before for a full buffer.
    • boundary == 0 already ended in an XRUN, through unread = frames; it still does.
  • appl_ptr is read once, with read_volatile, since the reader can change it at any time. hw_ptr and boundary are kernel-owned, but they are range-checked too.
  • SNDRV_PCM_INFO_SYNC_APPLPTR is set only on the sense front-end (sense_pcm_hw()).
    • ALSA then refuses to map the control page (pcm_control_mmap_allowed()). The status page stays mappable for alsa-lib 2.0.14 and later.
    • alsa-lib falls back to SNDRV_PCM_IOCTL_SYNC_PTR on its own. pcm_lib_apply_appl_ptr() refuses an appl_ptr at or past the boundary there.
    • PipeWire and alsa-lib readers need no change. The cost is one ioctl per pointer sync at the sense period rate.
    • The other front-ends keep their mapped control page. The driver-side check stays either way.
  • Sibling code: aop_lpai.rs and aop_t8140.rs keep driver-owned hw_ptr_bytes and never read appl_ptr. mca.c uses dmaengine PCM. None has this pattern.
  • The tests come first in the test so that a regression shows the overflow: appl_ptr = usize::MAX, then appl_ptr > hw_ptr + boundary. They also cover appl_ptr == boundary, boundaries of 1, 4096, 2^30, usize::MAX / 2 and usize::MAX, boundary == 0, hw_ptr at or past the boundary, and both sides of the wrap.
Evidence

Head dfc98f5efa461c4c78aca946c8aa7611e815daac, base main ef26a43be753.

./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=t8152.kunitconfig
# t8152.kunitconfig: KUNIT RUST RUST_OVERFLOW_CHECKS RUST_DEBUG_ASSERTIONS ARCH_APPLE PM MAILBOX
#   SOUND SND SND_SOC SND_SOC_APPLE_T8152_AOP_AUDIO DMADEVICES APPLE_ADMAC (all =y)
Tree Result
this head snd_soc_apple_t8152_aop_audio 8/8; run total 49: 48 passed, 1 skipped (apple_g17_rx_util_min_test, needs CONFIG_UCLAMP_TASK)
this head with sense_fe_unread() doing the old arithmetic rust_kernel: panicked at sound/soc/apple/t8152_aop_audio.rs:395:9: attempt to subtract with overflow, kernel BUG at rust/helpers/bug.c:7!, Internal error: Oops - BUG; both new pointer tests failed

DMADEVICES and APPLE_ADMAC are in the config because the driver calls dma_request_chan(), which only exists with DMA_ENGINE. Production configs already have it. checkpatch.pl: clean. rustfmt: the changed hunks are clean; the file has older formatting drift that this change doesn't touch.

… using it

The speaker sense ring callback works out how many frames the sense
front-end's reader has not taken yet as hw_ptr - appl_ptr modulo the
boundary. appl_ptr is the reader's: it can write it through the mapped
control page at any time, with any value. A value past hw_ptr plus the
boundary, such as ULONG_MAX, makes hw_ptr + boundary - appl_ptr
underflow, and with Rust overflow checks that panics, which is a BUG()
in the DMA callback.

Read appl_ptr once, and take both pointers only as positions below a
non-zero boundary. Otherwise stop the stream with an XRUN, as when the
batch does not fit. Set SNDRV_PCM_INFO_SYNC_APPLPTR on the sense
front-end too, so that ALSA gives its reader no writable control page:
the reader then moves appl_ptr through ALSA, which checks it. alsa-lib
does this on its own when the page is not mapped.

Add KUnit tests for the pointer checks (ULONG_MAX, the boundary, past
hw_ptr plus the boundary, no boundary, a hardware pointer out of
range, and wrap-around), for the XRUN decision, and for the flag.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 20:40
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at dfc98f5efa46: ready. The J714s hardware row listed under Not yet is still owed before merge.

Blockers: none.

Checked:

  • Arithmetic. sense_fe_unread() returns None unless boundary > 0, hw_ptr < boundary and appl_ptr < boundary. Within that:
    • hw_ptr - appl_ptr (taken only when hw_ptr >= appl_ptr) and boundary - appl_ptr can't underflow.
    • hw_ptr + (boundary - appl_ptr) stays below boundary, so the checked_add can't fail.
    • sense_ring_has_fe_space() was already checked.
    • The rest of deliver() does arithmetic only on values the driver or kernel owns. pending stays below 2 * buffer_size before its % frames, and fe_pos is the driver's own counter.
    • So nothing a reader can write overflows with RUST_OVERFLOW_CHECKS=y.
  • XRUN. Every refusal goes through the same snd_pcm_stop(sub, XRUN), under the stream lock, as the existing full-buffer case. boundary == 0 still ends in an XRUN, as it did before through unread = frames.
  • Reading appl_ptr. It is read once with read_volatile into a local, and only the local is used after that.
    • With the new flag, ALSA changes appl_ptr only in SYNC_PTR, read, forward and rewind. All of those run under the stream lock that deliver() holds, so the value is stable as well.
    • hw_ptr comes from the status page, which userspace can only map read-only, and boundary is the kernel's. Checking them costs nothing.
  • SYNC_APPLPTR reaches the runtime. sense_open() sets runtime->hw = sense_pcm_hw(...). On the ASoC front-end path, dpcm_runtime_setup_fe() and soc_pcm_hw_init() change only rates, channels and formats, so hw.info keeps the flag.
    • pcm_control_mmap_allowed() then refuses the control page with ENXIO.
    • pcm_status_mmap_allowed() keeps the status page for readers that report PCM protocol 2.0.14 or later.
    • In SYNC_PTR, pcm_lib_apply_appl_ptr() refuses appl_ptr >= boundary with EINVAL.
  • Readers.
    • The sense reader is PipeWire's api.alsa.pcm.source from speakerguardd's packaging (for example sgv2-j815.conf). It goes through alsa-lib, which falls back to SNDRV_PCM_IOCTL_SYNC_PTR when the control page can't be mapped. That is the path USB audio, SOF and qdsp6 PCMs already use with this flag.
    • speakerguardd's own raw ioctls are on /dev/snd/controlC*, not on the PCM.
    • The other front-ends still use pcm_hw() unchanged, and the new test checks that.
  • Sibling drivers. None of aop_lpai.rs, aop_t8140.rs, aop_audio.rs, aop_spkr.rs or mca.c reads appl_ptr.
  • Tests.
    • The three new cases cover:
      • usize::MAX;
      • appl > hw + boundary;
      • appl == boundary;
      • boundaries from 1 to usize::MAX;
      • boundary == 0;
      • hw_ptr at or past the boundary;
      • both sides of the wrap;
      • the XRUN decision: fits, full, and 8 invalid pointer sets;
      • the flag on all three sense configurations, against pcm_hw().
    • deliver() itself isn't unit-tested, since it needs a live PCM runtime, but it passes the values it read straight to sense_fe_takes().
    • With the old arithmetic put back, two of the new tests fail: one hits the overflow panic, the other an assertion (Evidence).
  • Rules and text. There is no runtime switch, no new constant outside the tests, and no SoC gate. The commit and PR text are plain bug-fix wording, Signed-off-by DjDeveloperr, with no attribution lines.
  • Optional wording. The PR body says "alsa-lib 2.0.14 and later". 2.0.14 is the PCM protocol version a reader reports (alsa-lib does it through USER_PVERSION), not an alsa-lib release.
  • CI. host-tests, pr-text and readiness were still pending when I posted this.
  • Owed before merge: the J714s row from Not yet. That means speaker protection running at 5 % volume with sense frames flowing, the control page refused, a malformed appl_ptr refused, and no Rust panic, BUG or Oops.
Evidence

Head dfc98f5efa461c4c78aca946c8aa7611e815daac, base main ef26a43be753. Linux aarch64 host, rustc 1.100.0, bindgen 0.73.2.

./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 --jobs 4 --kunitconfig=t8152.kunitconfig
# KUNIT RUST RUST_OVERFLOW_CHECKS RUST_DEBUG_ASSERTIONS ARCH_APPLE PM MAILBOX SOUND SND SND_SOC
# SND_SOC_APPLE_T8152_AOP_AUDIO DMADEVICES APPLE_ADMAC (all =y; .config confirmed)
Tree Result
this head snd_soc_apple_t8152_aop_audio 8/8; run total 49: 48 passed, 1 skipped (apple_g17_rx_util_min_test, needs CONFIG_UCLAMP_TASK)
this head with sense_fe_unread() doing the old arithmetic (hw_ptr + boundary - appl_ptr, no range checks) rust_kernel: panicked at sound/soc/apple/t8152_aop_audio.rs:395:14, kernel BUG at rust/helpers/bug.c:7!, Internal error: Oops - BUG; sense_fe_unread_rejects_out_of_range_pointers faulted, sense_fe_takes_xruns_on_bad_pointers failed its assertion; suite 6/8, run 46 passed, 2 failed, 1 skipped

ALSA and ASoC paths read at this head: pcm_control_mmap_allowed() and pcm_status_mmap_allowed() (sound/core/pcm_native.c), snd_pcm_sync_ptr() taking the stream lock around pcm_lib_apply_appl_ptr() (sound/core/pcm_lib.c, refuses appl_ptr >= boundary), and dpcm_fe_dai_startup() → dpcm_runtime_setup_fe() → soc_pcm_hw_init(hw, false) (sound/soc/soc-pcm.c, hw.info untouched).

@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) blast:m5 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 Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at dfc98f5e: not ready yet

Check Detail
CI at head ok 2 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at dfc98f5e
Conflicts ok none
PR text ok Risk: two-way · Macs with the driver-owned sense ring: T6050 (J714s, J716c) and T8142 (J813, J815). T8152 (J873g) has no sense ring and is not affected.

Areas: area:audio

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

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row at dfc98f5efa46: PASS.

  • Kernel: 7.3.0-m5-rows-d3310, a warm native build of main 2094ccb2a036 + this PR at dfc98f5efa46 + linux#3310 ef9eb65196aa (iommu only, no file overlap). This PR's file is identical to the head. Booted with the default command line; the default kernel (7.3.0-m5-cdirect16) was restored afterwards with a clean systemctl reboot.
  • Base: the default kernel, in the same session.

1. Capture device: arecord -l lists card 1: AppleJ714 [MacBook Pro J714], device 2: Speaker Sense (pcmC1D2c).

2. Playback through speakerguardd: pink noise for 60 s at 5 % sink volume, through sgv2.voicing.j714, with speakerguardd and PipeWire running normally. The sense capture's hw_ptr was sampled every 2 s. The sink was set back to its previous volume (0.00) afterwards.

sense pointer moving XRUN state seen speakerguardd stall/refusal/xrun lines PipeWire ERR during the 60 s
base (default kernel) 28 of 28 samples 0 0 +2
candidate 28 of 28 samples 0 0 +1
candidate, after the sleep 28 of 28 samples 0 0 +8

3. Idle probe: speakerguardd stopped, and the sense PCM free (closed, no owner). The probe was a read-only mmap of /dev/snd/pcmC1D2c at 0x83000000 (SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW); nothing was written.

  • Candidate: REFUSED (ENXIO).
  • Default kernel: also ENXIO. It's a perf branch rather than main, so it isn't a mainline baseline for this check, and a main build without this PR wasn't booted for it.
  • speakerguardd was started again after each probe and reported "protection ready: every check holds".

4. One deep mem sleep (RTC wake after 20 s) with audio stopped: 1 of 1 succeeded. Step 2 was repeated after the resume (third row above), and speakerguardd logged "the engine beats again after the sleep".

5. Kernel log: 0 panicked, 0 kernel BUG, 0 Oops, and 0 snd/ASoC/t8152 error lines on the candidate. Failed units: tailscaled.service only, the same as on the default boot. A second candidate boot, for the probe only, had 0 error-level lines that the default boot doesn't.

A full power-off wasn't run: this unattended lab Mac has no remote way to power back on. The clean reboot path was exercised.

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

Verdict at dfc98f5e: code OK. Merge after the J714s Speaker Sense row the body lists, since this touches the speaker-protection data path.

Checked

  • The sense reader's pointer is checked before use. With the old arithmetic the new KUnit case panics (subtract overflow), and with this change it passes: 8/8, with overflow checks and debug asserts on.
  • T8152 (no sense ring) is unaffected.

Before merge: a J714s row at this head on the Speaker Sense capture PCM, as described in the body: speakerguardd v2 running, quiet start, sense frames flowing, no guard trip.

@RyanTheTide
RyanTheTide disabled auto-merge November 27, 2026 07:00

@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 dfc98f5 after hosted checks

Checked bounded pointer arithmetic, invalid pointers taking the XRUN path, and the Sense-only synchronized application pointer flag. The J714s receipt covers protected playback, Sense capture and deep resume on the reviewed source.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping @DjDeveloperr: closeout merge of your PR at dfc98f5e. The App approved at this head (12:1x), the path-filtered CI is green, and the J714s Speaker Sense exact-head row meets Ryan's condition. It is a pointer check before use in the sense reader and does not change protection behaviour. Merging under the 6-point rule 1 h from now unless you say hold.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at dfc98f5e. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J714s-SpeakerSense@head;NULL-check-only. (5) There's no blocking review. (6) Dj was pinged here 60 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit dd2ad1d into main Nov 29, 2026
5 checks passed
@RyanTheTide
RyanTheTide deleted the fix/t8152-sense-appl-ptr branch November 29, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:audio Area from CODEOWNERS (bot) blast:m5 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants