From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73BB72E4274 for ; Thu, 13 Nov 2025 23:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763075666; cv=none; b=BQWYcH8Wt8fMy2aHKyTTNIyT6Ah7+7/x431j77fOn9lNk7CNXeiutBnTZPEH7gi9EbKROkmNkRcqa/iFcPWazX5RPj5OlrdlPaFyp1MedxUZcO306HTcn/F8pierwPaSzSstCKwiDMBJtWoYEi7JLpSgNOqYpt9+oR9OYf6iv0U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763075666; c=relaxed/simple; bh=1Ug4NZTEkBDsYl1zyrkpie035MkE/Yu9FxatwqEx2Yg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YAT6/60niSDegM6WktaRMCzeMAV5G1vv7RLBPwKJrxs/uz4g5100IgxXJ/D6T4EyZQ+BitHiYqfUICZ9bVuMgJImo9Dl6o3cel/QBWSPIcV956/etC7/6MZaAW/jRX1YcCpqCRa9/PvDgKGi9+uwGLy8iav0KKl/+6QTUQssOSM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z/zFbC4x; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z/zFbC4x" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3418ad76023so3397502a91.0 for ; Thu, 13 Nov 2025 15:14:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763075665; x=1763680465; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=vCo8Zg1Lol/2z7vSj/8+cq61xRbdddn2wswjFAmZyfk=; b=Z/zFbC4xEuj32qWbtHHdBcsbo2YCaxNfqf2o9QDf/Z9X2E0k2h7iicw9on8bLQ7BXG XTTox7ZkQRNrjGPvRJkdA0IBBqDYf2epKsfrUkrXNfRLVh/ZY3uN1Ejr2kkQ4dCbdBLd qasUEpRdhCFFPJgKBEdc3LalBwbpwcxLSSjUBpjIWGI+yJIRv4MO3InBH1UpT5iF8RGJ 8P6+N0H9a/hlHLoU3DAWXxP118NYxB8Hd/1KLG9CM5jpY/fvAC8+wRvKAmrG1SedBK15 gfDIzP9syq0ay/A5FvhLCrlG6cSWT5rX61TJ2wci9fLJrCb/o+KKcZ/3hNUt3yOzxrff z8nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763075665; x=1763680465; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vCo8Zg1Lol/2z7vSj/8+cq61xRbdddn2wswjFAmZyfk=; b=WrWH3MCBAOAwwbGoiWwLHmUNnUT1tP6a1ckjOvtA9BcQ2R7OkL3CGVpXrmxl3Qbct3 4sHQIWtzosdtvRZEGm0xdF3cH7vMJ9i0/UTNXynGhlAKEhj/OhKLw6mc8BP4LKn27WqX 3sjp2fBbTXG7ozQGLlJTSE9zuKTRjsw/JggISFikx6hlLdrKJlKEEUKFRQKE9MvdLz14 rqbDTEVWYAzUQBW+eDdgPVVDJKmqKejk7thsR5c0TZ9M7m3rJ+Oq6jp3kZb6puoAlYUu oFjRqqkHdjfccvM/KfiWqwlmqOY8+Q6+Pg/T9ARiPYwJw0MIG59/8v5GhZUhh1KyMI3F BIrg== X-Forwarded-Encrypted: i=1; AJvYcCU8wgFUagBji+CCYMH7PGadCEHhvX6AH92RD8tXKJG8R/sIyAi5Rkost6GQKzJMiETduGvWDMLZaZ1wCuM=@vger.kernel.org X-Gm-Message-State: AOJu0YzTSyoMToihB/54w/pbHkNAfQ8KWuOYDDYD0AQQxZlGHKKklWc8 Y5ggFM/EH4XKj3FQaGF6lInv7WEpTja+codt8paflHzUk11KD2AW8HFfUtXLVrK4Fbi2lkdUaxn 2XffORg== X-Google-Smtp-Source: AGHT+IEFwvnN7d2SCi3Xn3bEw9b3VihFAGbMKQStxBMCtqnE7tAxZvBuVANAwKnHuBD3pCb1qrR0SthNMvM= X-Received: from pjbha6.prod.google.com ([2002:a17:90a:f3c6:b0:33b:dccb:b328]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:43:b0:343:6a79:6c75 with SMTP id 98e67ed59e1d1-343fa75c570mr989265a91.29.1763075664710; Thu, 13 Nov 2025 15:14:24 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 13 Nov 2025 15:14:16 -0800 In-Reply-To: <20251113231420.1695919-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251113231420.1695919-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.rc1.455.g30608eb744-goog Message-ID: <20251113231420.1695919-2-seanjc@google.com> Subject: [PATCH 1/5] KVM: SVM: Serialize updates to global OS-Visible Workarounds variables From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Guard writes to the global osvw_status and osvw_len variables with a spinlock to ensure enabling virtualization on multiple CPUs in parallel doesn't effectively drop any writes due to writing back stale data. Don't bother taking the lock when the boot CPU doesn't support the feature, as that check is constant for all CPUs, i.e. racing writes will always write the same value (zero). Note, the bug was inadvertently "fixed" by commit 9a798b1337af ("KVM: Register cpuhp and syscore callbacks when enabling hardware"), which effectively serialized calls to enable virtualization due to how the cpuhp framework "brings up" CPU. But KVM shouldn't rely on the mechanics of cphup to provide serialization. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index fc42bcdbb520..5612e46e481c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -77,6 +77,7 @@ static bool erratum_383_found __read_mostly; * are published and we know what the new status bits are */ static uint64_t osvw_len = 4, osvw_status; +static DEFINE_SPINLOCK(osvw_lock); static DEFINE_PER_CPU(u64, current_tsc_ratio); @@ -554,16 +555,19 @@ static int svm_enable_virtualization_cpu(void) if (!err) err = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, &status); - if (err) + guard(spinlock)(&osvw_lock); + + if (err) { osvw_status = osvw_len = 0; - else { + } else { if (len < osvw_len) osvw_len = len; osvw_status |= status; osvw_status &= (1ULL << osvw_len) - 1; } - } else + } else { osvw_status = osvw_len = 0; + } svm_init_erratum_383(); -- 2.52.0.rc1.455.g30608eb744-goog