Re: [PATCH v3 09/16] x86/virt: Add refcounting of VMX/SVM usage to support multiple in-kernel users

From: Chao Gao

Date: Fri Feb 27 2026 - 06:30:24 EST


On Fri, Feb 13, 2026 at 05:26:55PM -0800, Sean Christopherson wrote:
>Implement a per-CPU refcounting scheme so that "users" of hardware
>virtualization, e.g. KVM and the future TDX code, can co-exist without
>pulling the rug out from under each other. E.g. if KVM were to disable
>VMX on module unload or when the last KVM VM was destroyed, SEAMCALLs from
>the TDX subsystem would #UD and panic the kernel.
>
>Disable preemption in the get/put APIs to ensure virtualization is fully
>enabled/disabled before returning to the caller. E.g. if the task were
>preempted after a 0=>1 transition, the new task would see a 1=>2 and thus
>return without enabling virtualization. Explicitly disable preemption
>instead of requiring the caller to do so, because the need to disable
>preemption is an artifact of the implementation. E.g. from KVM's
>perspective there is no _need_ to disable preemption as KVM guarantees the
>pCPU on which it is running is stable (but preemption is enabled).
>
>Opportunistically abstract away SVM vs. VMX in the public APIs by using
>X86_FEATURE_{SVM,VMX} to communicate what technology the caller wants to
>enable and use.
>
>Cc: Xu Yilun <yilun.xu@xxxxxxxxxxxxxxx>
>Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>

Reviewed-by: Chao Gao <chao.gao@xxxxxxxxx>