Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Documentation/ABI/testing/sysfs-driver-apple-sep
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,31 @@ Description:
no handler for, as an unsigned decimal. The first few are
logged. Normally 0.

What: /sys/bus/platform/devices/<unit>.sep/diag/sensor_bringup_failures
Date: November 2026
KernelVersion: 7.3
Contact: Ryan Murray <ryan@aurorasilicon.org>
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/<unit>.sep/diag/capture_frame_errors
Date: November 2026
KernelVersion: 7.3
Contact: Ryan Murray <ryan@aurorasilicon.org>
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/<unit>.sep/diag/sleep
What: /sys/bus/platform/devices/<unit>.sep/diag/sleeps
Date: November 2026
Expand Down
17 changes: 17 additions & 0 deletions drivers/soc/apple/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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);
}

Expand Down Expand Up @@ -481,6 +483,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) {
Expand Down
51 changes: 51 additions & 0 deletions drivers/soc/apple/diag_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ static struct {
atomic_t skdl;
atomic_t xarm_failures;
atomic_t unhandled;
/* 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. */
Expand All @@ -127,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)
Expand Down Expand Up @@ -704,6 +720,22 @@ 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 sep_diag_count_emit(buf, &sep_diag.bind.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 sep_diag_count_emit(buf, &sep_diag.bind.capture_frame_errors);
}
static DEVICE_ATTR_RO(capture_frame_errors);

static ssize_t sleeps_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
Expand Down Expand Up @@ -770,6 +802,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,
&dev_attr_trusted.attr,
Expand Down Expand Up @@ -801,6 +835,8 @@ 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. */
sep_diag_bind_counts_reset(&sep_diag.bind);

ret = devm_device_add_group(dev, &sep_diag_group);
if (ret)
Expand Down Expand Up @@ -903,6 +939,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.bind.sensor_bringup_failures);
}

unsigned int sep_diag_capture_frame_error(void)
{
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. */
void sep_diag_sleep(bool entering)
{
Expand Down Expand Up @@ -1107,3 +1153,8 @@ void sep_diag_bringup_ended(void)
SEP_DIAG_TOUCHID_PENDING)
sep_diag_notify_kn(&sep_diag.touchid_kn);
}

/* 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
52 changes: 52 additions & 0 deletions drivers/soc/apple/diag_shim_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 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 <kunit/test.h>

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);
/* 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 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. */
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[] = {
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);
7 changes: 7 additions & 0 deletions drivers/soc/apple/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,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
/// <https://github.com/aurora-silicon/linux/issues/2324>.
pub(crate) struct KeyGrant {
op: TouchOp,
/// The user bag the record lives under (`HANDLE_USER_BAG`), if any.
Expand Down
52 changes: 48 additions & 4 deletions drivers/soc/apple/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/// <https://github.com/aurora-silicon/linux/issues/2322>.
pub(crate) fn retry_stage(stage: u32, nonce_session: bool, retry: u32) -> u32 {
if nonce_session || retry % 2 == 1 {
stage
Expand Down Expand Up @@ -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
/// <https://github.com/aurora-silicon/linux/issues/2322>.
pub(crate) const ATTACH: Retry = Retry {
attempts: 5,
first_pause_ms: 500,
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading