[PATCH v5 6/6] platform/chrome: cros_ec_sensorhub: Drop sensor samples with past timestamps

From: Satyam Gupta

Date: Wed Oct 14 2026 - 03:44:58 EST


From: Scott Collyer <scollyer@xxxxxxxxxx>

When a sensor event timestamp is strictly earlier than the newest
timestamp previously seen for that sensor, drop the Out Of Order (OOO)
sample instead of clamping its timestamp to last_ts. Clamping past
samples to last_ts previously caused downstream batch spreading logic
(cros_ec_sensor_ring_spread_add()) to treat stale events as a batch of
identical timestamps.

Rename cros_ec_sensor_ring_check_for_past_timestamp() to
cros_ec_sensor_ring_is_past_timestamp() returning true when the sample
timestamp is in the past.

Signed-off-by: Scott Collyer <scollyer@xxxxxxxxxx>
Signed-off-by: Satyam Gupta <sgsatyam@xxxxxxxxxx>
---
.../platform/chrome/cros_ec_sensorhub_ring.c | 36 +++++++++++--------
1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index 7740bb42b1df..a5215955470f 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -427,23 +427,29 @@ cros_ec_sensor_ring_fix_overflow(s64 *ts,
state->last = *ts;
}

-static void
-cros_ec_sensor_ring_check_for_past_timestamp(struct cros_ec_sensorhub
- *sensorhub,
- struct cros_ec_sensors_ring_sample
- *sample)
+static bool
+cros_ec_sensor_ring_is_past_timestamp(struct cros_ec_sensorhub *sensorhub,
+ struct cros_ec_sensors_ring_sample *sample)
{
const u8 sensor_id = sample->sensor_id;

- /* If this event is earlier than one we saw before... */
+ /*
+ * If this event is strictly earlier than one we saw before, it is truly
+ * out-of-order and must be dropped to preserve monotonicity.
+ *
+ * We intentionally allow duplicate timestamps (==) to pass through here
+ * because the EC/ISH may report a batch of samples with the same raw
+ * timestamp. These duplicates are required by the downstream spreading
+ * logic (cros_ec_sensor_ring_spread_add) to interpolate unique,
+ * monotonic timestamps for each sample in the batch.
+ */
if (sensorhub->batch_state[sensor_id].newest_sensor_event >
sample->timestamp)
- /* mark it for spreading. */
- sample->timestamp =
- sensorhub->batch_state[sensor_id].last_ts;
- else
- sensorhub->batch_state[sensor_id].newest_sensor_event =
- sample->timestamp;
+ return true;
+
+ sensorhub->batch_state[sensor_id].newest_sensor_event =
+ sample->timestamp;
+ return false;
}

/**
@@ -576,8 +582,10 @@ cros_ec_sensor_ring_process_event(struct cros_ec_sensorhub *sensorhub,
for (axis = 0; axis < 3; axis++)
out->vector[axis] = in->data[axis];

- if (sensorhub->tight_timestamps)
- cros_ec_sensor_ring_check_for_past_timestamp(sensorhub, out);
+ if (sensorhub->tight_timestamps) {
+ if (cros_ec_sensor_ring_is_past_timestamp(sensorhub, out))
+ return false;
+ }
return true;
}

--
2.56.0.385.gd3acb90ef8-goog