Merge tag 'timers-core-2024-03-10' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / kernel / time / timekeeping.c
index 0ff065c..b58dffc 100644 (file)
@@ -1180,13 +1180,15 @@ static int adjust_historical_crosststamp(struct system_time_snapshot *history,
 }
 
 /*
- * cycle_between - true if test occurs chronologically between before and after
+ * timestamp_in_interval - true if ts is chronologically in [start, end]
+ *
+ * True if ts occurs chronologically at or after start, and before or at end.
  */
-static bool cycle_between(u64 before, u64 test, u64 after)
+static bool timestamp_in_interval(u64 start, u64 end, u64 ts)
 {
-       if (test > before && test < after)
+       if (ts >= start && ts <= end)
                return true;
-       if (test < before && before > after)
+       if (start > end && (ts >= start || ts <= end))
                return true;
        return false;
 }
@@ -1247,7 +1249,7 @@ int get_device_system_crosststamp(int (*get_time_fn)
                 */
                now = tk_clock_read(&tk->tkr_mono);
                interval_start = tk->tkr_mono.cycle_last;
-               if (!cycle_between(interval_start, cycles, now)) {
+               if (!timestamp_in_interval(interval_start, now, cycles)) {
                        clock_was_set_seq = tk->clock_was_set_seq;
                        cs_was_changed_seq = tk->cs_was_changed_seq;
                        cycles = interval_start;
@@ -1260,10 +1262,8 @@ int get_device_system_crosststamp(int (*get_time_fn)
                                      tk_core.timekeeper.offs_real);
                base_raw = tk->tkr_raw.base;
 
-               nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
-                                                    system_counterval.cycles);
-               nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
-                                                   system_counterval.cycles);
+               nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, cycles);
+               nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, cycles);
        } while (read_seqcount_retry(&tk_core.seq, seq));
 
        xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
@@ -1278,13 +1278,13 @@ int get_device_system_crosststamp(int (*get_time_fn)
                bool discontinuity;
 
                /*
-                * Check that the counter value occurs after the provided
+                * Check that the counter value is not before the provided
                 * history reference and that the history doesn't cross a
                 * clocksource change
                 */
                if (!history_begin ||
-                   !cycle_between(history_begin->cycles,
-                                  system_counterval.cycles, cycles) ||
+                   !timestamp_in_interval(history_begin->cycles,
+                                          cycles, system_counterval.cycles) ||
                    history_begin->cs_was_changed_seq != cs_was_changed_seq)
                        return -EINVAL;
                partial_history_cycles = cycles - system_counterval.cycles;