Bug #13384


tsc_read() should use lfence when prudent

Added by Patrick Mooney 11 months ago. Updated 10 months ago.

Start date:
Due date:
% Done:


Estimated time:
Gerrit CR:


At startup, tsc_read() contains text which uses CPUID to serialize the instruction stream before issuing a RDTSC. Once information has been gathered about the available CPU features, that function is potentially updated to use more efficient serialization methods. Those currently are:
1. RDTSCP (which is serializing itself, so it requires no extra)
2. MFENCE (on AMD chips with family <= 0xf)
3. LFENCE (on Intel chips with family <= 6 and SSE2)

With i386 support removed, all CPUs are presumed to support SSE2 (a requirement in the amd64 specification), which means they will all support LFENCE, although it is not necessarily serializing. On later model AMD chips, the LFENCE serialization behavior is controlled by the DE_CFG MSR, which we're inclined to enable for Spectre retpolines anyway. In the absence of RDTSCP (the best tsc_read() choice), we should prefer LFENCE when we can ensure it is serializing. It would remove the need for the MFENCE implementation (since the chips where it is currently preferred have a serializing LFENCE) and improve performance in cases were RDTSCP may not be exposed.

Related issues

Related to illumos gate - Bug #13383: generalize check for serializing LFENCEClosedPatrick Mooney

Actions #1

Updated by Patrick Mooney 11 months ago

  • Related to Bug #13383: generalize check for serializing LFENCE added
Actions #2

Updated by Electric Monk 11 months ago

  • Gerrit CR set to 1112
Actions #3

Updated by Patrick Mooney 10 months ago

This is primarily relevant for illumos running under bhyve, where we currently do not have RDTSCP support presented to guests. Absent that, it would normally fall back to CPUID for serializing the instruction stream for RDTSC measurements. Given that CPUID results in a guest exit, this is especially expensive. Prior to the fix, tsc_type was reported as 0x1 (CPUID-RDTSC) in those guests. With the fix in place (and doing proper lfence-serialize detection through #13383), the tsc_type value is the expected 0x3 (LFENCE-RDTSC).

Actions #4

Updated by Electric Monk 10 months ago

  • Status changed from In Progress to Closed
  • % Done changed from 0 to 100

git commit beed421eff10640141fe8f9da29a5c8033a97f52

commit  beed421eff10640141fe8f9da29a5c8033a97f52
Author: Patrick Mooney <>
Date:   2021-01-26T21:27:49.000Z

    13383 generalize check for serializing LFENCE
    13384 tsc_read() should use lfence when prudent
    13387 bhyve should expose lfence serialize in DE_CFG MSR
    Reviewed by: Robert Mustacchi <>
    Approved by: Dan McDonald <>


Also available in: Atom PDF