Skip to content

soc: apple: rtkit-helper: add KUnit tests - #2941

Merged
DjDeveloperr merged 1 commit into
mainfrom
kunit/apple-rtkit-helper
Nov 24, 2026
Merged

DjDeveloperr merged 1 commit into
mainfrom
kunit/apple-rtkit-helper

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem. The RTKit helper (the generic co-processor driver used for the MTP and others) had no test of its own callbacks: firmware buffer placement, the inherited buffer list, and its sleep state machine.

Change. A new apple-rtkit-helper suite (drivers/soc/apple/rtkit-helper-test.c, included from the driver). It calls the helper's callbacks directly on a test context. The AP power request goes through a small wrapper, apple_rtkit_helper_send_ap_power(), that a KUnit static stub can redirect; otherwise it is the same call.

  • Shared-memory setup: DMA allocation for a request without an address; SRAM placement inside, at the edges, one byte outside, straddling; zero and huge sizes; an iova+size that wraps; no SRAM at all; adopted strict and legacy sessions; repeated setup and destroy.
  • The inherited buffer list from a test devicetree node: count limits, bad endpoints, zero size, a bad mapped flag, wrap, the 44-bit edge, and one bad entry rejecting the whole list.
  • The sleep state machine: prepare, suspend_late, suspend, resume and complete; the PM notifier; idle notices that go unanswered, are answered late, or are still pending at the next sleep; a crashed co-processor; adopted s2idle with and without the CPU running; freeze; input preservation.

Plus a standalone drivers/soc/apple/rtkit-helper.kunitconfig. Tests only, no functional change.

Testing.

  • kunit.py run --arch=arm64 (QEMU, gcc): 36/36, standalone kunitconfig 38/39 (the extra suite pulled in has one unrelated skip, apple_g17_rx_util_min_test, needs UCLAMP_TASK), and again with this wave's three branches merged on the integration/aurora tip.
  • Negative controls: an off-by-one buffer end in setup (3 failures); the "answer came late" check inverted at the end of a sleep (6); the wrap check removed from setup (2).
  • checkpatch --strict: 0 errors; the warning is the MAINTAINERS reminder for new files.
Notes for reviewers
  • shmem_destroy is a no-op today, so a buffer that setup allocated with dma_alloc_coherent() is not freed when RTKit hands it back. soc: apple: start and stop the T8140 MTP without a live handoff #1764 (draft) changes this on purpose only under free_buffers, because a quiesced firmware may still write into its buffer. This suite therefore does not assert either behaviour; a case for it can join that PR.
  • The size-0 check in the inherited-list parser is redundant: a size-0 entry is also refused by the wrap check right after it (a mutant that removes it changes nothing).
  • The test builds a struct apple_rtkit from rtkit-internal.h (same directory), so it follows that struct's layout.
  • Not covered: probe/remove, the live-handoff check and the reserved-memory path.

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

Add a suite for the RTKit helper's own callbacks, run directly on a test
context with the AP power request behind a KUnit static stub (a small
wrapper, apple_rtkit_helper_send_ap_power(), that is the same call
otherwise):

- shared-memory setup: DMA allocation for a request without an address,
  SRAM placement inside, at the edges, one byte outside, straddling,
  zero and huge sizes, an iova+size that wraps, and no SRAM at all;
  adopted strict and legacy sessions; repeated setup and destroy;
- the inherited buffer list from a test devicetree node: count limits,
  bad endpoints, zero size, a bad mapped flag, wrap, the 44-bit edge,
  and one bad entry rejecting the list;
- the sleep state machine: prepare, suspend_late, suspend, resume and
  complete, the PM notifier, idle notices unanswered, answered late or
  still pending at the next sleep, a crashed co-processor, adopted
  s2idle with and without the CPU running, freeze, and input
  preservation.

Tests only, no functional change.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at a5dbac8.

  1. drivers/soc/apple/Kconfig:158: depends on APPLE_RTKIT_HELPER=y && KUNIT=y plus the #include "rtkit-helper-test.c" at rtkit-helper.c:633 means the suite never builds where the helper and KUnit are modules (the usual distro/fleet config). Please make it a tristate test module: depends on APPLE_RTKIT_HELPER && KUNIT, build rtkit-helper-test.o separately, and expose the callbacks and apple_rtkit_helper_send_ap_power() with VISIBLE_IF_KUNIT / EXPORT_SYMBOL_IF_KUNIT.
  2. rtkit-helper-test.c:110-127 writes the global pm_suspend_global_flags / pm_suspend_target_state. That is fine built in (it runs before userspace), but a loadable test can race a real suspend. Take lock_system_sleep() in init and release it in exit once it is a module.
    The wrapper change is behaviour-identical (the only apple_rtkit_ap_power() call now goes through it, and the redirect compiles out without the test). The suite runs on its own kunit device and a zeroed struct apple_rtkit, so it never touches a bound helper instance.

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

Formal pre-PR checklist review at a5dbac8de4515535e24273c1a7f5cdf8af5016f1.

Blockers

  1. Missing OF Kconfig dependency (drivers/soc/apple/Kconfig:158): APPLE_RTKIT_HELPER depends on APPLE_RTKIT -> APPLE_MAILBOX (ARCH_APPLE || (64BIT && COMPILE_TEST)), not OF. Under COMPILE_TEST=y, CONFIG_OF=n, CONFIG_SUSPEND=y, CONFIG_KUNIT=y, of_find_property() and of_device_is_compatible() compile to no-op stubs returning NULL/false, causing all 8 rtkh_inherit_* test cases to fail at runtime. Add && OF.
  2. NULL dereference and global PM state corruption on rtkh_test_init() abort (drivers/soc/apple/rtkit-helper-test.c:108-161): KUnit executes suite->exit(test) even when suite->init(test) aborts early via KUNIT_ASSERT_*. If kunit_kzalloc() fails (test->priv == NULL), rtkh_test_exit() dereferences ctx->dev (NULL pointer dereference). If kunit_device_register(), ctx->asc, or ctx->sram_mem fails before line 143, ctx->saved_flags/ctx->saved_target are 0 and rtkh_test_exit() clobbers global pm_suspend_global_flags and pm_suspend_target_state to 0. Guard if (!ctx) return; and restore the PM globals via kunit_add_action_or_reset() immediately after saving them.
  3. Kconfig prevents building on asahi.config / modular KUnit + global PM lock (drivers/soc/apple/Kconfig:158, rtkit-helper-test.c:143-160): CONFIG_APPLE_RTKIT_HELPER=m in arch/arm64/configs/asahi.config, so depends on APPLE_RTKIT_HELPER=y && KUNIT=y && SUSPEND never builds on the fleet/distro config. Convert to a tristate test module (VISIBLE_IF_KUNIT / EXPORT_SYMBOL_IF_KUNIT) and wrap the pm_suspend_global_flags / pm_suspend_target_state mutations with lock_system_sleep() / unlock_system_sleep() so a loadable test cannot race a real system suspend.

Non-blocking notes & CI

  • Production drivers/soc/apple/rtkit-helper.c change (apple_rtkit_helper_send_ap_power()) is properly guarded by #if IS_ENABLED(CONFIG_APPLE_RTKIT_HELPER_KUNIT_TEST) and is behavior-identical when tests are disabled.
  • dev->of_node = NULL in rtkh_test_exit() and devres teardown order for rtkh_pm_register() are sound (given ctx != NULL).
  • CI note: rust-build is in progress and DWC3/ATC builtin-no-atc / builtin-modular-atc are still queued. .github/workflows/sep-source.yml's kunit job only runs apple_sep*, not apple-rtkit-helper.

Checklist (§1–§6)

  • §1–§3, §6: Pass (synthetic SRAM/ASC buffers and zeroed struct apple_rtkit).
  • §4: Pass (human author/committer + human Signed-off-by:, no AI trailers in commits, PR Assisted-by: present).
  • §5: Satisfied for production driver (strictly no functional change when CONFIG_APPLE_RTKIT_HELPER_KUNIT_TEST=n).

@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (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
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at a5dbac8d: not ready yet

Check Detail
CI at head ok 7 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head no no line with the head SHA a5dbac8d, a Mac and a result in the PR body or a comment
Conflicts ok not computed yet
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:platform

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

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

Verdict: MERGE at a5dbac8de451

Blockers: none.

Checked: no Apple material, no runtime change: the PR adds a KUnit suite, and the driver change only exposes internals to it (built only with the test option). Tests that run only in CI need no hardware row. CI green. This replaces my earlier review.

Evidence

Follow-up, not blocking: under COMPILE_TEST with CONFIG_OF=n, the rtkh_inherit_* cases fail because the OF helpers are stubs. Add depends on OF to the test option.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 24, 2026 20:00

Superseded by the review at a5dbac8

@DjDeveloperr
DjDeveloperr merged commit d4e9bcf into main Nov 24, 2026
8 checks passed
@RyanTheTide
RyanTheTide deleted the kunit/apple-rtkit-helper branch November 27, 2026 13:21
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) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (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.

2 participants