1. Mar 05, 2024
    • David Woodhouse's avatar
      KVM: x86/xen: inject vCPU upcall vector when local APIC is enabled · 8e62bf2b
      David Woodhouse authored
      Linux guests since commit b1c3497e ("x86/xen: Add support for
      HVMOP_set_evtchn_upcall_vector") in v6.0 onwards will use the per-vCPU
      upcall vector when it's advertised in the Xen CPUID leaves.
      
      This upcall is injected through the guest's local APIC as an MSI, unlike
      the older system vector which was merely injected by the hypervisor any
      time the CPU was able to receive an interrupt and the upcall_pending
      flags is set in its vcpu_info.
      
      Effectively, that makes the per-CPU upcall edge triggered instead of
      level triggered, which results in the upcall being lost if the MSI is
      delivered when the local APIC is *disabled*.
      
      Xen checks the vcpu_info->evtchn_upcall_pending flag when the local APIC
      for a vCPU is software enabled (in fact, on any write to the SPIV
      register which doesn't disable the APIC). Do the same in KVM since KVM
      doesn't provide a way for userspace to intervene and trap accesses to
      the SPIV register of a local APIC emulated by KVM.
      
      Fixes: fde0451b
      
       ("KVM: x86/xen: Support per-vCPU event channel upcall via local APIC")
      Signed-off-by: default avatarDavid Woodhouse <dwmw@amazon.co.uk>
      Reviewed-by: default avatarPaul Durrant <paul@xen.org>
      Cc: stable@vger.kernel.org
      Link: https://lore.kernel.org/r/20240227115648.3104-3-dwmw2@infradead.org
      
      
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      8e62bf2b
    • David Woodhouse's avatar
      KVM: x86/xen: improve accuracy of Xen timers · 451a7078
      David Woodhouse authored
      A test program such as http://david.woodhou.se/timerlat.c confirms user
      reports that timers are increasingly inaccurate as the lifetime of a
      guest increases. Reporting the actual delay observed when asking for
      100µs of sleep, it starts off OK on a newly-launched guest but gets
      worse over time, giving incorrect sleep times:
      
      root@ip-10-0-193-21:~# ./timerlat -c -n 5
      00000000 latency 103243/100000 (3.2430%)
      00000001 latency 103243/100000 (3.2430%)
      00000002 latency 103242/100000 (3.2420%)
      00000003 latency 103245/100000 (3.2450%)
      00000004 latency 103245/100000 (3.2450%)
      
      The biggest problem is that get_kvmclock_ns() returns inaccurate values
      when the guest TSC is scaled. The guest sees a TSC value scaled from the
      host TSC by a mul/shift conversion (hopefully done in hardware). The
      guest then converts that guest TSC value into nanoseconds using the
      mul/shift conversion given to it by the KVM pvclock information.
      
      But get_kvmclock_ns() performs only a single conversion directly from
      host TSC to nanoseconds, giving a different result. A test program at
      http://david.woodhou.se/tsdrift.c demonstrates the cumulative error
      over a day.
      
      It's non-trivial to fix get_kvmclock_ns(), although I'll come back to
      that. The actual guest hv_clock is per-CPU, and *theoretically* each
      vCPU could be running at a *different* frequency. But this patch is
      needed anyway because...
      
      The other issue with Xen timers was that the code would snapshot the
      host CLOCK_MONOTONIC at some point in time, and then... after a few
      interrupts may have occurred, some preemption perhaps... would also read
      the guest's kvmclock. Then it would proceed under the false assumption
      that those two happened at the *same* time. Any time which *actually*
      elapsed between reading the two clocks was introduced as inaccuracies
      in the time at which the timer fired.
      
      Fix it to use a variant of kvm_get_time_and_clockread(), which reads the
      host TSC just *once*, then use the returned TSC value to calculate the
      kvmclock (making sure to do that the way the guest would instead of
      making the same mistake get_kvmclock_ns() does).
      
      Sadly, hrtimers based on CLOCK_MONOTONIC_RAW are not supported, so Xen
      timers still have to use CLOCK_MONOTONIC. In practice the difference
      between the two won't matter over the timescales involved, as the
      *absolute* values don't matter; just the delta.
      
      This does mean a new variant of kvm_get_time_and_clockread() is needed;
      called kvm_get_monotonic_and_clockread() because that's what it does.
      
      Fixes: 53639526
      
       ("KVM: x86/xen: handle PV timers oneshot mode")
      Signed-off-by: default avatarDavid Woodhouse <dwmw@amazon.co.uk>
      Reviewed-by: default avatarPaul Durrant <paul@xen.org>
      Link: https://lore.kernel.org/r/20240227115648.3104-2-dwmw2@infradead.org
      
      
      [sean: massage moved comment, tweak if statement formatting]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      451a7078
  2. Feb 22, 2024
  3. Feb 20, 2024
  4. Feb 08, 2024
  5. Feb 07, 2024
  6. Feb 06, 2024
    • Nathan Chancellor's avatar
      x86/coco: Define cc_vendor without CONFIG_ARCH_HAS_CC_PLATFORM · e4596477
      Nathan Chancellor authored
      After commit a9ef2774 ("x86/kvm: Fix SEV check in
      sev_map_percpu_data()"), there is a build error when building
      x86_64_defconfig with GCOV using LLVM:
      
        ld.lld: error: undefined symbol: cc_vendor
        >>> referenced by kvm.c
        >>>               arch/x86/kernel/kvm.o:(kvm_smp_prepare_boot_cpu) in archive vmlinux.a
      
      which corresponds to
      
        if (cc_vendor != CC_VENDOR_AMD ||
            !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
                  return;
      
      Without GCOV, clang is able to eliminate the use of cc_vendor because
      cc_platform_has() evaluates to false when CONFIG_ARCH_HAS_CC_PLATFORM is
      not set, meaning that if statement will be true no matter what value
      cc_vendor has.
      
      With GCOV, the instrumentation keeps the use of cc_vendor around for
      code coverage purposes but cc_vendor is only declared, not defined,
      without CONFIG_ARCH_HAS_CC_PLATFORM, leading to the build error above.
      
      Provide a macro definition of cc_vendor when CONFIG_ARCH_HAS_CC_PLATFORM
      is not set with a value of CC_VENDOR_NONE, so that the first condition
      can always be evaluated/eliminated at compile time, avoiding the build
      error altogether. This is very similar to the situation prior to
      commit da86eb96
      
       ("x86/coco: Get rid of accessor functions").
      
      Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      Acked-by: default avatarBorislav Petkov (AMD) <bp@alien8.de>
      Message-Id: <20240202-provide-cc_vendor-without-arch_has_cc_platform-v1-1-09ad5f2a3099@kernel.org>
      Fixes: a9ef2774
      
       ("x86/kvm: Fix SEV check in sev_map_percpu_data()", 2024-01-31)
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      e4596477
    • Linus Torvalds's avatar
      Merge tag 'bcachefs-2024-02-05' of https://evilpiepirate.org/git/bcachefs · 99bd3cb0
      Linus Torvalds authored
      Pull bcachefs fixes from Kent Overstreet:
       "Two serious ones here that we'll want to backport to stable: a fix for
        a race in the thread_with_file code, and another locking fixup in the
        subvolume deletion path"
      
      * tag 'bcachefs-2024-02-05' of https://evilpiepirate.org/git/bcachefs:
        bcachefs: time_stats: Check for last_event == 0 when updating freq stats
        bcachefs: install fd later to avoid race with close
        bcachefs: unlock parent dir if entry is not found in subvolume deletion
        bcachefs: Fix build on parisc by avoiding __multi3()
      99bd3cb0
    • Kees Cook's avatar
      LoongArch: vDSO: Disable UBSAN instrumentation · cca5efe7
      Kees Cook authored
      
      
      The vDSO executes in userspace, so the kernel's UBSAN should not
      instrument it. Solves these kind of build errors:
      
        loongarch64-linux-ld: arch/loongarch/vdso/vgettimeofday.o: in function `vdso_shift_ns':
        lib/vdso/gettimeofday.c:23:(.text+0x3f8): undefined reference to `__ubsan_handle_shift_out_of_bounds'
      
      Reported-by: default avatarkernel test robot <lkp@intel.com>
      Closes: https://lore.kernel.org/oe-kbuild-all/202401310530.lZHCj1Zl-lkp@intel.com/
      
      
      Cc: Huacai Chen <chenhuacai@kernel.org>
      Cc: WANG Xuerui <kernel@xen0n.name>
      Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: Masahiro Yamada <masahiroy@kernel.org>
      Cc: Fangrui Song <maskray@google.com>
      Cc: loongarch@lists.linux.dev
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Signed-off-by: default avatarHuacai Chen <chenhuacai@loongson.cn>
      cca5efe7