linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/19] timekeeping: Handle potential multiplication overflow
@ 2024-03-08 13:14 Adrian Hunter
  2024-03-08 13:14 ` [PATCH 01/19] vdso: Consolidate vdso_calc_delta() Adrian Hunter
                   ` (18 more replies)
  0 siblings, 19 replies; 22+ messages in thread
From: Adrian Hunter @ 2024-03-08 13:14 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: Peter Zijlstra, Dave Hansen, John Stultz, H. Peter Anvin,
	Alexander Gordeev, Vincenzo Frascino, linux-s390, Arnd Bergmann,
	x86, Aneesh Kumar K.V, Ingo Molnar, Naveen N. Rao,
	Christian Borntraeger, Vasily Gorbik, Heiko Carstens,
	Nicholas Piggin, Borislav Petkov, Andy Lutomirski, Bjorn Helgaas,
	Anna-Maria Behnsen, Stephen Boyd, Randy Dunlap, linux-kernel,
	Sven Schnelle, linuxppc-dev

Hi

Kernel timekeeping calculates a clock value by keeping a base value and
adding the number of nanoseconds since that time. Those nanoseconds are
calculated from the clocksource delta. Then periodically, the base value is
moved forwards (refer timekeeping_advance()) which is done by the local
timer interrupt handler. It is designed such that there will always be a
timer interrupt before the delta becomes big enough to overflow the 64-bit
multiplication used in the conversion of delta to nanoseconds (refer
timekeeping_delta_to_ns()). Obviously if timer interrupts are stopped, then
the multiplication does eventually overflow.

Timekeeping multiplication overflow results in a "time loop", typically
cycling about every 15 minutes with x86 TSC, for example starting at 10:00:

  10:00, 10:01, 10:02 ... 10:15, 10:00, 10:01, ... 10:15, 10:00, 10:01 ...

Because a VMM can deliberately stop timer interrupts for a guest, a virtual
machine can be exposed to this issue.

TDX maintains a monotonically increasing virtual TSC for a TDX guest, so
the overflow is allowing a backwards movement of timekeeping that would not
happen otherwise.

It is considered this could break security of cryptographic protocols that
rely on the timestamps for freshness / replay protection, and consequently
the kernel should prevent such a time loop.

Handle multiplication overflows by falling back to higher precision
calculation when the possibility of an overflow is detected.

Extend the facility also to VDSO, dependent on new config option
GENERIC_VDSO_OVERFLOW_PROTECT which is selected by x86 only, so other
architectures are not affected. The result is a calculation that has
similar performance as before. Most machines showed performance benefit,
except Skylake-based hardware such as Intel Kaby Lake which was seen <1%
worse.


Adrian Hunter (19):
      vdso: Consolidate vdso_calc_delta()
      vdso: Consolidate nanoseconds calculation
      vdso: Add CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT
      math64: Tidy mul_u64_u32_shr()
      vdso: math64: Provide mul_u64_u32_add_u64_shr()
      vdso: Add vdso_data::max_cycles
      vdso: Make delta calculation overflow safe
      x86/vdso: Make delta calculation overflow safe
      timekeeping: Move timekeeping helper functions
      timekeeping: Rename fast_tk_get_delta_ns() to __timekeeping_get_ns()
      timekeeping: Tidy timekeeping_cycles_to_ns() slightly
      timekeeping: Reuse timekeeping_cycles_to_ns()
      timekeeping: Refactor timekeeping helpers
      timekeeping: Consolidate timekeeping helpers
      timekeeping: Fold in timekeeping_delta_to_ns()
      timekeeping: Prepare timekeeping_cycles_to_ns() for overflow safety
      timekeeping: Make delta calculation overflow safe
      timekeeping: Let timekeeping_cycles_to_ns() handle both under and overflow
      clocksource: Make watchdog and suspend-timing multiplication overflow safe

 arch/powerpc/include/asm/vdso/gettimeofday.h |  17 +----
 arch/s390/include/asm/vdso/gettimeofday.h    |   7 +-
 arch/x86/Kconfig                             |   1 +
 arch/x86/include/asm/vdso/gettimeofday.h     |  42 +++++++----
 include/linux/math64.h                       |   8 +-
 include/vdso/datapage.h                      |   4 +
 include/vdso/math64.h                        |  38 ++++++++++
 kernel/time/clocksource.c                    |  42 +++++------
 kernel/time/timekeeping.c                    | 106 ++++++++++++++-------------
 kernel/time/vsyscall.c                       |   6 ++
 lib/vdso/Kconfig                             |   7 ++
 lib/vdso/gettimeofday.c                      |  55 +++++++++-----
 12 files changed, 199 insertions(+), 134 deletions(-)


Regards
Adrian

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2024-03-09 10:20 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-08 13:14 [PATCH 00/19] timekeeping: Handle potential multiplication overflow Adrian Hunter
2024-03-08 13:14 ` [PATCH 01/19] vdso: Consolidate vdso_calc_delta() Adrian Hunter
2024-03-09  2:09   ` John Stultz
2024-03-09  7:48   ` Christophe Leroy
2024-03-08 13:14 ` [PATCH 02/19] vdso: Consolidate nanoseconds calculation Adrian Hunter
2024-03-08 13:14 ` [PATCH 03/19] vdso: Add CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT Adrian Hunter
2024-03-08 13:14 ` [PATCH 04/19] math64: Tidy mul_u64_u32_shr() Adrian Hunter
2024-03-08 13:14 ` [PATCH 05/19] vdso: math64: Provide mul_u64_u32_add_u64_shr() Adrian Hunter
2024-03-08 13:14 ` [PATCH 06/19] vdso: Add vdso_data::max_cycles Adrian Hunter
2024-03-08 13:15 ` [PATCH 07/19] vdso: Make delta calculation overflow safe Adrian Hunter
2024-03-08 13:15 ` [PATCH 08/19] x86/vdso: " Adrian Hunter
2024-03-08 13:15 ` [PATCH 09/19] timekeeping: Move timekeeping helper functions Adrian Hunter
2024-03-08 13:15 ` [PATCH 10/19] timekeeping: Rename fast_tk_get_delta_ns() to __timekeeping_get_ns() Adrian Hunter
2024-03-08 13:15 ` [PATCH 11/19] timekeeping: Tidy timekeeping_cycles_to_ns() slightly Adrian Hunter
2024-03-08 13:15 ` [PATCH 12/19] timekeeping: Reuse timekeeping_cycles_to_ns() Adrian Hunter
2024-03-08 13:15 ` [PATCH 13/19] timekeeping: Refactor timekeeping helpers Adrian Hunter
2024-03-08 13:15 ` [PATCH 14/19] timekeeping: Consolidate " Adrian Hunter
2024-03-08 13:15 ` [PATCH 15/19] timekeeping: Fold in timekeeping_delta_to_ns() Adrian Hunter
2024-03-08 13:15 ` [PATCH 16/19] timekeeping: Prepare timekeeping_cycles_to_ns() for overflow safety Adrian Hunter
2024-03-08 13:15 ` [PATCH 17/19] timekeeping: Make delta calculation overflow safe Adrian Hunter
2024-03-08 13:15 ` [PATCH 18/19] timekeeping: Let timekeeping_cycles_to_ns() handle both under and overflow Adrian Hunter
2024-03-08 13:15 ` [PATCH 19/19] clocksource: Make watchdog and suspend-timing multiplication overflow safe Adrian Hunter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).