From ae80856c4ad64673d16c8e644dcca2d53706aed7 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:36:22 +1100 Subject: [PATCH 1/6] soc: apple: sep: Count failed sensor bring-ups and refused capture frames The Touch ID sensor bring-up is retried at attach (five spaced attempts and a late round), before a verify (three attempts) and once more when the attach left the restored enrolment unproven. Each failed attempt is logged, but nothing counts them. A boot that needed four attempts looks the same in diag as one that needed a single attempt: sensor_attach reads "attached" in both cases. The failures themselves are real. On the J700, 7 boots in 29 needed a second attempt; on an M1 Pro, 2 in 4 lost the sensor before the attach was retried. In those attempts the sensor answers its first status reads and then falls silent part-way through the registration. Their cause is not known yet, so the retries stay. The same holds for capture frames that fail their checksum or advertise a length that cannot be a frame. The capture ends and the user is asked to touch again, which is the right recovery. A sensor whose frames keep arriving garbled still cannot be told apart from a user who keeps lifting their finger. Count both in diag: - sensor_bringup_failures: every failed attempt of every round, whether or not a later attempt brought the sensor up. - capture_frame_errors: every capture whose frame was refused for its checksum or its length. The enrol log lines for refused frames said "retrying", although the driver retries nothing itself. They now say "asking for another touch". The restore retry before a verify had its own copy of the restart rule: bring the sensor up, and if that failed, reset the stage to FRESH by hand and try again. Run it through registration::bring_up() with a two-attempt policy, registration::RESTORE, that counts on from the failed attach like the late round. On a profile without a nonce session the second attempt starts from a fresh registration, exactly as before. On a nonce-session profile retry_stage() now keeps the stage instead of forcing FRESH. That sends the same requests: such a profile identifies the sensor on every attempt, and the enclave is cleared whenever it holds the sensor, which it always does after the first attempt. The restore attempts are now logged and counted like every other round's. A verify that is cancelled between them no longer makes the second one. The ATTACH comment said the policy applied to nonce-session profiles only, but every profile has used it since commit 9c98cb5030ea ("soc: apple: sep: Retry the sensor attach on profiles without a nonce session"). The comment now says so, states what is known about the failure and links the tracking issue. The retry_stage() comment now says that its alternation is a hedge between two observed enclave behaviours, not a protocol step. KUnit: apple_sep_registration gains a case for the RESTORE policy and the stages its attempts start from. Link: https://github.com/aurora-silicon/linux/issues/2322 Signed-off-by: Ryan Murray --- .../ABI/testing/sysfs-driver-apple-sep | 25 +++++++++ drivers/soc/apple/diag.rs | 17 ++++++ drivers/soc/apple/diag_shim.c | 32 ++++++++++++ drivers/soc/apple/registration.rs | 52 +++++++++++++++++-- drivers/soc/apple/sbio.rs | 49 +++++++++++------ drivers/soc/apple/shim.h | 2 + 6 files changed, 156 insertions(+), 21 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-driver-apple-sep b/Documentation/ABI/testing/sysfs-driver-apple-sep index 32d4a16c8cdc1..cb3529f446e48 100644 --- a/Documentation/ABI/testing/sysfs-driver-apple-sep +++ b/Documentation/ABI/testing/sysfs-driver-apple-sep @@ -578,6 +578,31 @@ Description: no handler for, as an unsigned decimal. The first few are logged. Normally 0. +What: /sys/bus/platform/devices/.sep/diag/sensor_bringup_failures +Date: November 2026 +KernelVersion: 7.3 +Contact: Ryan Murray +Description: + (RO) How many Touch ID sensor bring-up attempts have failed + since probe, as an unsigned decimal: attach-time, late, + verify-time and restore attempts alike, whether or not a + later attempt brought the sensor up. Each failure is logged + with the step that failed. sensor_attach says whether the + sensor came up in the end; this says how often it took more + than one attempt. Normally 0. + +What: /sys/bus/platform/devices/.sep/diag/capture_frame_errors +Date: November 2026 +KernelVersion: 7.3 +Contact: Ryan Murray +Description: + (RO) How many Touch ID captures since probe returned a frame + the driver refused, for a checksum that did not match or a + length the sensor advertised that cannot be a frame, as an + unsigned decimal. Each such capture ends with a request to + touch again; nothing from the frame reaches the enclave. + Normally 0. + What: /sys/bus/platform/devices/.sep/diag/sleep What: /sys/bus/platform/devices/.sep/diag/sleeps Date: November 2026 diff --git a/drivers/soc/apple/diag.rs b/drivers/soc/apple/diag.rs index 98814d4929b21..c3796f747d695 100644 --- a/drivers/soc/apple/diag.rs +++ b/drivers/soc/apple/diag.rs @@ -63,6 +63,8 @@ extern "C" { fn sep_diag_sse_event() -> c_uint; fn sep_diag_xarm_failed() -> c_uint; fn sep_diag_unhandled() -> c_uint; + fn sep_diag_sensor_bringup_failed() -> c_uint; + fn sep_diag_capture_frame_error() -> c_uint; fn sep_diag_log_summary(dev: *mut c_void); } @@ -421,6 +423,21 @@ pub(crate) fn unhandled() -> u32 { unsafe { sep_diag_unhandled() } } +/// One sensor bring-up attempt failed, whether or not another follows. +/// Returns how many have since probe, this one included. +pub(crate) fn sensor_bringup_failed() -> u32 { + // SAFETY: no preconditions; updates one word. + unsafe { sep_diag_sensor_bringup_failed() } +} + +/// A capture's frame was refused for a bad checksum or an unusable length, +/// and the user was asked to touch again. Returns how many have since +/// probe, this one included. +pub(crate) fn capture_frame_error() -> u32 { + // SAFETY: no preconditions; updates one word. + unsafe { sep_diag_capture_frame_error() } +} + /// Logs the one-line summary of the boot-time bring-up. Call it after /// [`bringup_ended`]. pub(crate) fn log_summary(dev: &device::Device) { diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index 00754ef808cae..bbe05a2a7018a 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -117,6 +117,8 @@ static struct { atomic_t skdl; atomic_t xarm_failures; atomic_t unhandled; + atomic_t sensor_bringup_failures; + atomic_t capture_frame_errors; atomic_t sleeps; atomic_t sleeping; } sep_diag; @@ -668,6 +670,24 @@ static ssize_t unhandled_messages_show(struct device *dev, } static DEVICE_ATTR_RO(unhandled_messages); +static ssize_t sensor_bringup_failures_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%u\n", + (unsigned int)atomic_read(&sep_diag.sensor_bringup_failures)); +} +static DEVICE_ATTR_RO(sensor_bringup_failures); + +static ssize_t capture_frame_errors_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%u\n", + (unsigned int)atomic_read(&sep_diag.capture_frame_errors)); +} +static DEVICE_ATTR_RO(capture_frame_errors); + static ssize_t sleeps_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -717,6 +737,8 @@ static struct attribute *sep_diag_attrs[] = { &dev_attr_skdl.attr, &dev_attr_xarm_failures.attr, &dev_attr_unhandled_messages.attr, + &dev_attr_sensor_bringup_failures.attr, + &dev_attr_capture_frame_errors.attr, &dev_attr_sleeps.attr, &dev_attr_sleep.attr, NULL @@ -827,6 +849,16 @@ unsigned int sep_diag_unhandled(void) return (unsigned int)atomic_inc_return(&sep_diag.unhandled); } +unsigned int sep_diag_sensor_bringup_failed(void) +{ + return (unsigned int)atomic_inc_return(&sep_diag.sensor_bringup_failures); +} + +unsigned int sep_diag_capture_frame_error(void) +{ + return (unsigned int)atomic_inc_return(&sep_diag.capture_frame_errors); +} + /* From the PM notifier (pm_shim.c), which runs its calls one at a time. */ void sep_diag_sleep(bool entering) { diff --git a/drivers/soc/apple/registration.rs b/drivers/soc/apple/registration.rs index a49b75e42488f..08438937ac906 100644 --- a/drivers/soc/apple/registration.rs +++ b/drivers/soc/apple/registration.rs @@ -75,6 +75,11 @@ pub(crate) fn patch_loaded(nonce_session: bool, accepted: bool, needs_patch: boo /// a reboot does and as a verify's second try does: after a failed sequence /// counter init a J314s refused the CLEAR_STATE re-registration for the /// rest of the boot, while a fresh one recovered. +/// +/// The alternation is not a step the enclave protocol defines. It covers +/// two enclave behaviours each seen on one machine, and neither recovery +/// works for both. Why the attempt before it failed is still open: +/// . pub(crate) fn retry_stage(stage: u32, nonce_session: bool, retry: u32) -> u32 { if nonce_session || retry % 2 == 1 { stage @@ -187,10 +192,18 @@ pub(crate) struct Retry { budget_ms: u64, } -/// At attach, on a profile with a nonce session. Across 29 J700 boots, 7 -/// needed a second attempt and one failed three attempts made back to back. -/// Pauses of 0.5, 1, 2 and 4 s give a sensor or an enclave that is still -/// settling time to do so, and five attempts end within about 10 s. +/// At attach, on every profile. Across 29 J700 boots, 7 needed a second +/// attempt and one failed three attempts made back to back; an M1 Pro lost +/// the sensor on 2 boots in 4 with a single attempt. Pauses of 0.5, 1, 2 and +/// 4 s give a sensor or an enclave that is still settling time to do so, +/// and five attempts end within about 10 s. +/// +/// What the failed attempts wait for is not known: the sensor answers its +/// first status reads and then falls silent part-way through the sequence, +/// after the SBIO endpoint is up and INIT_SBIO_COMMUNICATION was accepted. +/// The sensor has no power-good line to wait on. Every failed attempt is +/// counted in `diag/sensor_bringup_failures`; the cause is tracked in +/// . pub(crate) const ATTACH: Retry = Retry { attempts: 5, first_pause_ms: 500, @@ -205,6 +218,17 @@ pub(crate) const VERIFY: Retry = Retry { budget_ms: u64::MAX, }; +/// Before a verify on a boot whose attach-time bring-up left the restored +/// enrolment unproven: the attempt the attach did not finish, then one from +/// a fresh registration on a profile without a nonce session (see +/// [`retry_stage`]; the round counts on from the failed attach), back to +/// back. +pub(crate) const RESTORE: Retry = Retry { + attempts: 2, + first_pause_ms: 0, + budget_ms: u64::MAX, +}; + impl Retry { /// The pause before attempt `next`, counted from 1, when the first /// attempt began `elapsed_ms` ago; `None` if there is no such attempt. @@ -656,6 +680,26 @@ mod tests { assert_eq!(m.failed, 2); } + #[test] + fn a_restore_makes_two_attempts_the_second_from_a_fresh_registration() { + assert_eq!(RESTORE.pause_before(2, 0), Some(0)); + assert_eq!(RESTORE.pause_before(3, 0), None); + + let mut m = Model::new(); + m.nonce = [false; 8]; + assert_eq!(bring_up(&RESTORE, &mut m), Outcome::GaveUp(2)); + assert_eq!(m.paused, 0); + assert_eq!(m.failed, 1); + + // The round counts on from the failed attach: its first attempt + // keeps the stage the attach left, its second starts fresh on a + // profile without a nonce session, and a nonce-session profile + // keeps clearing. + assert_eq!(retry_stage(ESTABLISHED, false, 1), ESTABLISHED); + assert_eq!(retry_stage(ESTABLISHED, false, 2), FRESH); + assert_eq!(retry_stage(ESTABLISHED, true, 2), ESTABLISHED); + } + #[test] fn a_retry_clears_first_then_tries_a_fresh_registration() { // Without a nonce session: the first retry keeps the stage, so a diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 0c3b733941936..a1cd687dada42 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2057,8 +2057,8 @@ impl SepData { wanted, began_ns: shim::boottime_ns(), attach: kind == Bringup::Attach, - // The late round follows the failed attach-time one. - retry: u32::from(kind == Bringup::Late), + // The late and the restore rounds follow a failed attach-time one. + retry: u32::from(matches!(kind, Bringup::Late | Bringup::Restore)), }; registration::bring_up(retry, &mut attempts) } @@ -2530,6 +2530,12 @@ impl SepData { // undone and every verify refused until reboot. The restore itself is // still a valid candidate, so bring the sensor up once more and let // complete_bringup() finish the proof before refusing. + // + // After a failed first patch, the enclave can refuse the serial + // re-registration that follows CLEAR_STATE, while a reboot's fresh + // registration works: registration::RESTORE makes the second attempt + // from a fresh registration, as registration::retry_stage decides for + // every other round. fn retry_restore_proof(&self) { if self.templates_restored.load(Relaxed) || !self.cold_restore_candidate.load(Relaxed) @@ -2541,18 +2547,14 @@ impl SepData { self.dev, "verify: attach-time bring-up left the device-view proof undone; retrying the sensor bring-up\n" ); - if !self.bring_sensor_online() { - // After a failed first patch, the enclave can refuse the serial - // re-registration that follows CLEAR_STATE, while a reboot's - // fresh registration works. Retry once from the fresh stage. - dev_warn_ratelimited!( - self.dev, - "verify: retrying the sensor bring-up from a fresh registration\n" - ); - self.bringup.store(BRINGUP_FRESH, Relaxed); - if !self.bring_sensor_online() { - dev_err_ratelimited!(self.dev, "verify: the retried sensor bring-up failed\n"); - } + let outcome = self.bring_sensor_online_retrying( + c"restore", + Bringup::Restore, + ®istration::RESTORE, + || self.verify_wanted(), + ); + if !outcome.online() { + dev_err_ratelimited!(self.dev, "verify: the retried sensor bring-up failed\n"); } let _ = sensor::idle(); self.refresh_match_credential(); @@ -3072,6 +3074,7 @@ impl SepData { advertised: sent, computed, }) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, "verify: frame checksum mismatch (sensor sent {:#06x}, computed {:#06x})\n", @@ -3082,6 +3085,7 @@ impl SepData { return bio::VerifyOutcome::Failed(ENROL_STATUS_RETRY); } Err(sensor::CaptureError::Length(n)) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, "verify: the sensor advertised an unusable frame length {}\n", @@ -4180,9 +4184,10 @@ impl SepData { advertised: sent, computed, }) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, - "enrol: capture {} frame checksum mismatch (sensor sent {:#06x}, computed {:#06x}); retrying\n", + "enrol: capture {} frame checksum mismatch (sensor sent {:#06x}, computed {:#06x}); asking for another touch\n", counter, sent, computed @@ -4190,9 +4195,10 @@ impl SepData { return ImageOutcome::Retry(bio::Guidance::LiftAndMove); } Err(sensor::CaptureError::Length(n)) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, - "enrol: capture {} advertised an unusable frame length {}; retrying\n", + "enrol: capture {} advertised an unusable frame length {}; asking for another touch\n", counter, n ); @@ -6384,6 +6390,9 @@ enum Bringup { Late, /// Before a verify. Verify, + /// Before a verify, when the attach left the restored enrolment + /// unproven. + Restore, } /// A sensor bring-up with retries, as [`registration::bring_up`] drives it. @@ -6410,11 +6419,17 @@ impl bool> registration::Attempts for BringupAttempts<'_, W> { self.sep.bringup.store(stage, Relaxed); } self.retry += 1; - if self.attach { + let online = if self.attach { self.sep.attach_bring_sensor_online() } else { self.sep.bring_sensor_online() + }; + if !online { + // Each failure is logged at the step that failed; the count + // shows how often the sensor needs another attempt (#2322). + diag::sensor_bringup_failed(); } + online } fn wanted(&self) -> bool { diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 938e9b9a529a7..482d4d1a27de4 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -246,6 +246,8 @@ void sep_diag_set_offer(u8 endpoint, int state); unsigned int sep_diag_sse_event(void); unsigned int sep_diag_xarm_failed(void); unsigned int sep_diag_unhandled(void); +unsigned int sep_diag_sensor_bringup_failed(void); +unsigned int sep_diag_capture_frame_error(void); void sep_diag_sleep(bool entering); void sep_diag_log_summary(struct device *dev); From 42ae50ed57dd474463b0bc613ab2d3a740988338 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:36:40 +1100 Subject: [PATCH 2/6] soc: apple: sep: Explain the key-store timeout, grant retry and guard skips Three places in the SEP driver look like a hack but are bounded or fail closed. Each now says why, and links an issue where a cause is still open. - EXCHANGE_TIMEOUT_MS (90 s) bounds two waits that end on the event itself: the EP_SKS advertisement and trusted_ready. A healthy boot never waits this long. The value comes from a J700 that advertised the key store about 43 s after boot (commit fdcf9537f07b ("soc: apple: sep: wait for late J700 key-store discovery")). Regular-root boots now see the key store within about 2 s of attach on every Mac. Why the J700 was that slow, and whether an early initrd load still is, has not been found. Until it is, lowering the bound could break that path. The comment also points at the open issue about the trusted-key wait running inside key_construction_mutex. - KeyGrant's one retry after an outright EKEYREJECTED is not a step the protocol asks for. The operation runs where macOS runs it (after MATCH_CANCEL, before the evaluation is completed), and once more after the completion only because which point the enclave accepts has not been measured. At most one operation runs per match either way. - The xART guard skips a damaged one-time allowance and list entries that do not decode. Both fail closed. A damaged allowance grants nothing, and the file is emptied before it is read, so a later boot cannot use it either. An entry that does not decode lists nothing as Linux's, so a record it may have named stays protected. It still counts towards the list's bound and its load count. No functional change. Link: https://github.com/aurora-silicon/linux/issues/2323 Link: https://github.com/aurora-silicon/linux/issues/2324 Signed-off-by: Ryan Murray --- drivers/soc/apple/key.rs | 7 +++++++ drivers/soc/apple/sep.rs | 13 ++++++++++--- drivers/soc/apple/xart_guard.rs | 6 ++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 9a3047d0d1157..290f96d3ab67f 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -911,6 +911,13 @@ pub(crate) enum TouchOp { /// operation with. Only an outright refusal by the enclave (`EKEYREJECTED`: /// it performed nothing) hands the grant back, once, for the one retry /// the verify makes after it completes the evaluation. +/// +/// The retry is not one the enclave protocol asks for: the operation runs +/// first where macOS runs it (after MATCH_CANCEL, before the evaluation is +/// completed), and again after the completion only because which of the two +/// the enclave accepts has not been measured. Either way at most one +/// operation runs per match. Tracked in +/// . pub(crate) struct KeyGrant { op: TouchOp, /// The user bag the record lives under (`HANDLE_USER_BAG`), if any. diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 02fcc78b25564..7bcc24193b39d 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -143,9 +143,16 @@ const LATE_ATTACH_DEFERS: u32 = 3; // result is still wanted. const BRINGUP_PAUSE_STEP_MS: u32 = 100; -// J700 traces show the key-store endpoint arriving around 43 s after boot, -// well after the first infrastructure endpoints. The wait exits promptly on -// other machines when their key store becomes ready. +// How long the bring-up waits for the key store to be advertised, and a +// trusted-key caller for the bring-up to end. Both waits end on the event +// itself (the EP_SKS advertisement, `trusted_ready`), so a healthy boot never +// waits this long; on expiry the key store counts as absent and callers get +// an error. The bound covers a J700 that advertised the key store about 43 s +// after boot (commit fdcf9537f07b), about twice that. Regular-root boots now +// see it within 2 s of attach on every Mac; why the J700 was that slow, and +// whether an early (initrd) load still is, is open: +// https://github.com/aurora-silicon/linux/issues/2323. A trusted-key caller +// waits inside key_construction_mutex: see issue 2313. const EXCHANGE_TIMEOUT_MS: time::Msecs = 90000; const PHASE_ATTACH: u32 = 0; diff --git a/drivers/soc/apple/xart_guard.rs b/drivers/soc/apple/xart_guard.rs index f5e9b9490b44e..1712fe17880b6 100644 --- a/drivers/soc/apple/xart_guard.rs +++ b/drivers/soc/apple/xart_guard.rs @@ -386,6 +386,8 @@ fn take_allowance() -> Option { "xART guard: this boot may rewrite the first existing kind-{} record the enclave asks for, once, for a boot policy change\n", kind ), + // Fail-closed: a damaged file grants nothing, and it is emptied + // above, so it cannot grant anything at a later boot either. None => pr_warn_ratelimited!("xART guard: ignored a damaged one-time allowance\n"), } allowance @@ -548,6 +550,10 @@ impl Guard { None => torn += 1, } } + // Fail-closed: an entry that does not decode lists nothing as + // Linux's, so a record it may have named stays protected if it + // existed at open. It still counts towards MAX_ENTRIES and + // `loaded_entries`, so may_retry_create sees the list as it is. if torn != 0 { pr_warn_ratelimited!("xART guard: ignored {} unreadable list entries\n", torn); } From 2575740821b853c146087b45e7260f469cdbc6e2 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 02:36:06 +1100 Subject: [PATCH 3/6] soc: apple: sep: start the failure counts at each probe sensor_bringup_failures and capture_frame_errors are documented as counts since probe, but they live in the static sep_diag object, and sep_diag_register() reset only the trusted-key state. A later bind in the same driver lifetime would have reported the earlier bind's failures. Reset both counters in sep_diag_register(), which runs before the mailbox and the sensor attach, next to the trusted-key reset. Reported by the aurora-silicon review of #2363. Signed-off-by: Ryan Murray --- drivers/soc/apple/diag_shim.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index 21d91d3a3b27e..000306ed98c74 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -823,6 +823,9 @@ int sep_diag_register(struct device *dev, const char *profile, sep_diag.probe_ns = ktime_get_boottime_ns(); /* A refusal belongs to the bind that saw it. */ atomic_set(&sep_diag.trusted, SEP_DIAG_TRUSTED_UNKNOWN); + /* So do the failure counts: they are documented as counts since probe. */ + atomic_set(&sep_diag.sensor_bringup_failures, 0); + atomic_set(&sep_diag.capture_frame_errors, 0); ret = devm_device_add_group(dev, &sep_diag_group); if (ret) From 6778a000820fe830bba9222b2a18ef9c7e3766a9 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 05:27:09 +1100 Subject: [PATCH 4/6] soc: apple: sep: test that the failure counts start at each probe A KUnit case for the diag shim: one registration on a test device, two failed bring-ups and a refused frame (the counts read 2 and 1), then a second registration in the same module lifetime, after which both read 0 and count on from there. Without the reset in sep_diag_register() it fails at the first read after the second registration. Asked for by the aurora-silicon review of #2363. Signed-off-by: Ryan Murray --- drivers/soc/apple/diag_shim.c | 4 ++ drivers/soc/apple/diag_shim_test.c | 61 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 drivers/soc/apple/diag_shim_test.c diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index 000306ed98c74..edfeb8948624a 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -1142,3 +1142,7 @@ void sep_diag_bringup_ended(void) SEP_DIAG_TOUCHID_PENDING) sep_diag_notify_kn(&sep_diag.touchid_kn); } + +#if IS_BUILTIN(CONFIG_KUNIT) && IS_ENABLED(CONFIG_APPLE_SEP_KUNIT_TEST) +#include "diag_shim_test.c" +#endif diff --git a/drivers/soc/apple/diag_shim_test.c b/drivers/soc/apple/diag_shim_test.c new file mode 100644 index 0000000000000..0e9430a7211ae --- /dev/null +++ b/drivers/soc/apple/diag_shim_test.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KUnit tests for the diag shim's failure counts: they count since the probe + * that registered the group, so a later registration starts them at 0. + * Included from diag_shim.c. Runs with no SEP: the group goes on a KUnit device. + */ +#include +#include + +static unsigned int diag_test_read(struct kunit *test, struct device *dev, + ssize_t (*show)(struct device *, struct device_attribute *, char *), + struct device_attribute *attr) +{ + char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); + unsigned int value; + + KUNIT_ASSERT_NOT_NULL(test, buf); + KUNIT_ASSERT_GT(test, show(dev, attr, buf), 0); + KUNIT_ASSERT_EQ(test, kstrtouint(strim(buf), 10, &value), 0); + return value; +} + +static void diag_failure_counts_start_at_each_probe(struct kunit *test) +{ + struct device *first = kunit_device_register(test, "sep-diag-test.0"); + struct device *second = kunit_device_register(test, "sep-diag-test.1"); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, first); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, second); + + /* A bind that registered and then failed its attach. */ + KUNIT_ASSERT_EQ(test, sep_diag_register(first, "test", 4, false, false, false), 0); + sep_diag_sensor_bringup_failed(); + sep_diag_sensor_bringup_failed(); + sep_diag_capture_frame_error(); + KUNIT_EXPECT_EQ(test, diag_test_read(test, first, sensor_bringup_failures_show, + &dev_attr_sensor_bringup_failures), 2U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, first, capture_frame_errors_show, + &dev_attr_capture_frame_errors), 1U); + + /* The next probe in the same module lifetime counts from 0. */ + KUNIT_ASSERT_EQ(test, sep_diag_register(second, "test", 4, false, false, false), 0); + KUNIT_EXPECT_EQ(test, diag_test_read(test, second, sensor_bringup_failures_show, + &dev_attr_sensor_bringup_failures), 0U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, second, capture_frame_errors_show, + &dev_attr_capture_frame_errors), 0U); + sep_diag_capture_frame_error(); + KUNIT_EXPECT_EQ(test, diag_test_read(test, second, capture_frame_errors_show, + &dev_attr_capture_frame_errors), 1U); +} + +static struct kunit_case diag_shim_test_cases[] = { + KUNIT_CASE(diag_failure_counts_start_at_each_probe), + {} +}; + +static struct kunit_suite diag_shim_test_suite = { + .name = "apple-sep-diag-shim", + .test_cases = diag_shim_test_cases, +}; +kunit_test_suite(diag_shim_test_suite); From 527de53244c9e237e7a82ff27de5886e2af58b35 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 05:53:00 +1100 Subject: [PATCH 5/6] soc: apple: sep: test the per-bind failure counts on fixture state The diag shim test registered two KUnit devices against the production sep_diag singleton. The second registration overwrote the first device's kernfs references without dropping them, and on a KUnit-enabled Mac the test could overwrite a real SEP device's state. Put the two per-bind counts in their own struct sep_diag_bind_counts, with sep_diag_bind_counts_reset() (called from sep_diag_register()) and sep_diag_count_emit() (the sysfs formatting the attributes use). The test now runs the same nonzero-to-zero sequence on a fixture-owned struct, read back through sep_diag_count_emit(), and never touches sep_diag. Reported by the aurora-silicon review of #2363. Signed-off-by: Ryan Murray --- drivers/soc/apple/diag_shim.c | 33 +++++++++++++------ drivers/soc/apple/diag_shim_test.c | 51 ++++++++++++------------------ 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index edfeb8948624a..f47530018b917 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -67,6 +67,23 @@ /* The SEP's endpoint numbers are one byte. */ #define SEP_DIAG_MAX_EP 256 +/* Failure counts that cover one bind: they start at 0 when it registers. */ +struct sep_diag_bind_counts { + atomic_t sensor_bringup_failures; + atomic_t capture_frame_errors; +}; + +static void sep_diag_bind_counts_reset(struct sep_diag_bind_counts *counts) +{ + atomic_set(&counts->sensor_bringup_failures, 0); + atomic_set(&counts->capture_frame_errors, 0); +} + +static ssize_t sep_diag_count_emit(char *buf, const atomic_t *count) +{ + return sysfs_emit(buf, "%u\n", (unsigned int)atomic_read(count)); +} + static struct { /* Set at probe, before the group is added. */ char profile[32]; @@ -118,8 +135,7 @@ static struct { atomic_t skdl; atomic_t xarm_failures; atomic_t unhandled; - atomic_t sensor_bringup_failures; - atomic_t capture_frame_errors; + struct sep_diag_bind_counts bind; atomic_t sleeps; atomic_t sleeping; /* SEP_DIAG_TRUSTED_*, with the refusal status above it. */ @@ -710,8 +726,7 @@ static ssize_t sensor_bringup_failures_show(struct device *dev, struct device_attribute *attr, char *buf) { - return sysfs_emit(buf, "%u\n", - (unsigned int)atomic_read(&sep_diag.sensor_bringup_failures)); + return sep_diag_count_emit(buf, &sep_diag.bind.sensor_bringup_failures); } static DEVICE_ATTR_RO(sensor_bringup_failures); @@ -719,8 +734,7 @@ static ssize_t capture_frame_errors_show(struct device *dev, struct device_attribute *attr, char *buf) { - return sysfs_emit(buf, "%u\n", - (unsigned int)atomic_read(&sep_diag.capture_frame_errors)); + return sep_diag_count_emit(buf, &sep_diag.bind.capture_frame_errors); } static DEVICE_ATTR_RO(capture_frame_errors); @@ -824,8 +838,7 @@ int sep_diag_register(struct device *dev, const char *profile, /* A refusal belongs to the bind that saw it. */ atomic_set(&sep_diag.trusted, SEP_DIAG_TRUSTED_UNKNOWN); /* So do the failure counts: they are documented as counts since probe. */ - atomic_set(&sep_diag.sensor_bringup_failures, 0); - atomic_set(&sep_diag.capture_frame_errors, 0); + sep_diag_bind_counts_reset(&sep_diag.bind); ret = devm_device_add_group(dev, &sep_diag_group); if (ret) @@ -930,12 +943,12 @@ unsigned int sep_diag_unhandled(void) unsigned int sep_diag_sensor_bringup_failed(void) { - return (unsigned int)atomic_inc_return(&sep_diag.sensor_bringup_failures); + return (unsigned int)atomic_inc_return(&sep_diag.bind.sensor_bringup_failures); } unsigned int sep_diag_capture_frame_error(void) { - return (unsigned int)atomic_inc_return(&sep_diag.capture_frame_errors); + return (unsigned int)atomic_inc_return(&sep_diag.bind.capture_frame_errors); } /* From the PM notifier (pm_shim.c), which runs its calls one at a time. */ diff --git a/drivers/soc/apple/diag_shim_test.c b/drivers/soc/apple/diag_shim_test.c index 0e9430a7211ae..3b0f6bac36d46 100644 --- a/drivers/soc/apple/diag_shim_test.c +++ b/drivers/soc/apple/diag_shim_test.c @@ -1,52 +1,43 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* * KUnit tests for the diag shim's failure counts: they count since the probe - * that registered the group, so a later registration starts them at 0. - * Included from diag_shim.c. Runs with no SEP: the group goes on a KUnit device. + * that registered the group, so a registration starts them at 0. Included from + * diag_shim.c. Works on fixture-owned counts only: the production sep_diag + * state, its sysfs group and kernfs references are never touched. */ -#include #include -static unsigned int diag_test_read(struct kunit *test, struct device *dev, - ssize_t (*show)(struct device *, struct device_attribute *, char *), - struct device_attribute *attr) +static unsigned int diag_test_read(struct kunit *test, const atomic_t *count) { char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); unsigned int value; KUNIT_ASSERT_NOT_NULL(test, buf); - KUNIT_ASSERT_GT(test, show(dev, attr, buf), 0); + /* The same formatting the sysfs attributes use. */ + KUNIT_ASSERT_GT(test, sep_diag_count_emit(buf, count), 0); KUNIT_ASSERT_EQ(test, kstrtouint(strim(buf), 10, &value), 0); return value; } static void diag_failure_counts_start_at_each_probe(struct kunit *test) { - struct device *first = kunit_device_register(test, "sep-diag-test.0"); - struct device *second = kunit_device_register(test, "sep-diag-test.1"); - - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, first); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, second); + struct sep_diag_bind_counts *counts = kunit_kzalloc(test, sizeof(*counts), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, counts); /* A bind that registered and then failed its attach. */ - KUNIT_ASSERT_EQ(test, sep_diag_register(first, "test", 4, false, false, false), 0); - sep_diag_sensor_bringup_failed(); - sep_diag_sensor_bringup_failed(); - sep_diag_capture_frame_error(); - KUNIT_EXPECT_EQ(test, diag_test_read(test, first, sensor_bringup_failures_show, - &dev_attr_sensor_bringup_failures), 2U); - KUNIT_EXPECT_EQ(test, diag_test_read(test, first, capture_frame_errors_show, - &dev_attr_capture_frame_errors), 1U); - - /* The next probe in the same module lifetime counts from 0. */ - KUNIT_ASSERT_EQ(test, sep_diag_register(second, "test", 4, false, false, false), 0); - KUNIT_EXPECT_EQ(test, diag_test_read(test, second, sensor_bringup_failures_show, - &dev_attr_sensor_bringup_failures), 0U); - KUNIT_EXPECT_EQ(test, diag_test_read(test, second, capture_frame_errors_show, - &dev_attr_capture_frame_errors), 0U); - sep_diag_capture_frame_error(); - KUNIT_EXPECT_EQ(test, diag_test_read(test, second, capture_frame_errors_show, - &dev_attr_capture_frame_errors), 1U); + sep_diag_bind_counts_reset(counts); + atomic_inc(&counts->sensor_bringup_failures); + atomic_inc(&counts->sensor_bringup_failures); + atomic_inc(&counts->capture_frame_errors); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->sensor_bringup_failures), 2U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 1U); + + /* The next probe's registration starts both at 0; they count on from there. */ + sep_diag_bind_counts_reset(counts); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->sensor_bringup_failures), 0U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 0U); + atomic_inc(&counts->capture_frame_errors); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 1U); } static struct kunit_case diag_shim_test_cases[] = { From c0c78eff1573bfebb9f32951976b06c4603960e1 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 08:25:26 +1100 Subject: [PATCH 6/6] soc: apple: sep: keep diag_shim.c buildable by its host tests The sep-source host tests build diag_shim.c outside the kernel. test_diag_policy extracts only the 'static struct { ... } sep_diag;' block, so it lost the separately defined struct sep_diag_bind_counts. test_diag_sensor compiles the whole file without the kernel's IS_BUILTIN() and IS_ENABLED() macros, and broke on the KUnit include guard. Declare the tagged struct inside the sep_diag block, with its two helpers right after it, and guard the KUnit include with defined(CONFIG_KUNIT) && defined(CONFIG_APPLE_SEP_KUNIT_TEST): the same condition for this builtin bool option. Signed-off-by: Ryan Murray --- drivers/soc/apple/diag_shim.c | 37 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index f47530018b917..0fe57d0f4e33b 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -67,23 +67,6 @@ /* The SEP's endpoint numbers are one byte. */ #define SEP_DIAG_MAX_EP 256 -/* Failure counts that cover one bind: they start at 0 when it registers. */ -struct sep_diag_bind_counts { - atomic_t sensor_bringup_failures; - atomic_t capture_frame_errors; -}; - -static void sep_diag_bind_counts_reset(struct sep_diag_bind_counts *counts) -{ - atomic_set(&counts->sensor_bringup_failures, 0); - atomic_set(&counts->capture_frame_errors, 0); -} - -static ssize_t sep_diag_count_emit(char *buf, const atomic_t *count) -{ - return sysfs_emit(buf, "%u\n", (unsigned int)atomic_read(count)); -} - static struct { /* Set at probe, before the group is added. */ char profile[32]; @@ -135,7 +118,11 @@ static struct { atomic_t skdl; atomic_t xarm_failures; atomic_t unhandled; - struct sep_diag_bind_counts bind; + /* Failure counts that cover one bind: they start at 0 when it registers. */ + struct sep_diag_bind_counts { + atomic_t sensor_bringup_failures; + atomic_t capture_frame_errors; + } bind; atomic_t sleeps; atomic_t sleeping; /* SEP_DIAG_TRUSTED_*, with the refusal status above it. */ @@ -145,6 +132,17 @@ static struct { struct kernfs_node *touchid_kn; } sep_diag; +static void sep_diag_bind_counts_reset(struct sep_diag_bind_counts *counts) +{ + atomic_set(&counts->sensor_bringup_failures, 0); + atomic_set(&counts->capture_frame_errors, 0); +} + +static ssize_t sep_diag_count_emit(char *buf, const atomic_t *count) +{ + return sysfs_emit(buf, "%u\n", (unsigned int)atomic_read(count)); +} + static DEFINE_SPINLOCK(sep_diag_kn_lock); static void sep_diag_put_kns(void *data) @@ -1156,6 +1154,7 @@ void sep_diag_bringup_ended(void) sep_diag_notify_kn(&sep_diag.touchid_kn); } -#if IS_BUILTIN(CONFIG_KUNIT) && IS_ENABLED(CONFIG_APPLE_SEP_KUNIT_TEST) +/* CONFIG_KUNIT=y and the (bool) SEP test option; plain defined() so host tests can build the file. */ +#if defined(CONFIG_KUNIT) && defined(CONFIG_APPLE_SEP_KUNIT_TEST) #include "diag_shim_test.c" #endif