From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796168835; b=iCwtT3363z1bVVSyuhn01f5pR6zmS0CCZOIIQ8mIvfsBJ/6r6SPiuY3SaTftPc5ZJFtT gEphhBhiYzOruZzktRyE521rftyi8QCZv5koZwjUvANYhaqZEYiZRNhlYV9Y+u021k+rT Dl50qS5ednoXvZb4VcSbcMVc4TLA20c9XM+9ElPPrSZF4Qkro1EWMHTV3O+SzW8oDJElK HjO26WEIs9jA+4CovmpYkwu4yf0yV4ZichN3CVmQRl+/Iu4akREaNoYLIAFJUn/VSf6zG cc2Q2V3kqRdhXms8tw+gHOxGGIhiBS3PFkQQ/PczoqA7GOkvWDohKXYLQ1hpBn1IPoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796168835; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:Message-ID:Subject:From:To:Cc:Content-Type; bh=6iqCKUDR5dbApGIrGr9L7Ce+QcZG6vFcL2m3MEoY3l4=; b=j4DztCF0T50sMGA43Z8a34wXhuMdlWn3sYwg81ZBXnsHQI06uEB77ie2LNkrhNryAKeu g5UwZxlhjaOQALbMz5SSXjZ79DhYLFIbj+yOcJtQJ0per/WAsSKsRel0RzSmXeOEM4t4M XZIEDfGIRyB0+jllWeIihTUNgX1ntnEnNRdios6x/13ZbBrtf7pitPXDySBo+xNm/BZLg Kla8oaAntLWUdIMhdYBFN68eh+wV8Sk1XWBNxIYadu2pnuoj3ldCBb+kbmSqGgQA3cACr 74ptnLUq8ZfmWuLxRGQY2UwEsAGnlaeaw72/rbNvPto8DcVAYnOuwgvRnIU7iVzIMng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=cL1DZXrp; arc=none smtp.remote-ip=209.85.214.197 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82B7D606A320 for ; Tue, 1 Dec 2026 23:47:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 82B7D606A320 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.a=rsa-sha256 header.s=20260707 header.b=cL1DZXrp Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2f0651873abso96777545ad.2 for ; Tue, 01 Dec 2026 15:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796168835; x=1796773635; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6iqCKUDR5dbApGIrGr9L7Ce+QcZG6vFcL2m3MEoY3l4=; b=cL1DZXrpc8rk2TlFUpB1qBsagGwl0GTs6gYljn4rSsnM/LkV4phS6EN9aMJDSSSgKG zgaAtYfEWqTznMwMSUCh+MQV37Zal7vnQK6DGy2WfnmzU6jbd5TXOniCnYna5kMFblVq jrI1pMN3cwjkN5bOKLOiu71Cd5JpkZt9nZoZnPaYFW9oAt5G0x2xeLQnlcalm7nuP8tJ tJJSufaAa1hpyxOXPh87m9rBAP3jxN/0PvtdiktbgMch+j457D0EGhr2H5L3jNBV51IR Ey+6FjNONKETCz276Bb0rvGSx5FBF60lXWiVcRnRTblhOA+8426tTyvMJmbnRjIYPAxA cEgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796168835; x=1796773635; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6iqCKUDR5dbApGIrGr9L7Ce+QcZG6vFcL2m3MEoY3l4=; b=U8490AIWLKbA66IVBJ5dGrQxJQRQVtpa5QPEOsWw3xDh9KLrXk7FijyinD1lSWJtjX YcLqVPV94Q7sSt00ZUhh8Jkw27wAaawgIz4jTGFKClv+rR3dEMl4DjHiEtJEQ21GVshB pISpxUcMWw3aQwz3MjIM8jiljdDNCO3/4GudiZqeaoXssqQu25T07sLnN1q6wrFEqVHa hyvAajiP991OaAz3EEt9uCH441wmuPMslxo1ObcQqO662pWRTjgkPAo7W6x28YKfUuTg VyncyEyJHzBXdpbY7HIwgmFVDPdP7ggnurhqbybeKbIK4la5Vpw0uJzoimhp5Zn5bgDo gjlA== X-Forwarded-Encrypted: i=1; AL+hErHMreFnsSM4zMiYHCJZxsP3IQtAiC+fI/RfLtR4SnTJpB5VzLRaZkssSQWeIZcRl6XP6s31ONp7fhAPLNk=@vger.kernel.org X-Gm-Message-State: APuU4Z8WMX/qCG/o0RS6PKSMvSl5wEyD3k1TPO1h/w6awm6VdC3oeqiT LdUeA3KbDuEBOkVK5sYgn//oZNhCNgQrpqJrGuC1bCjvOprk/CYGMNRXLwlx8ItDiZOYZ+KLK3Z UnMkj9A== X-Received: from plbmf15.prod.google.com ([2002:a17:902:fc8f:b0:2ef:e9a7:dfde]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c9:b0:2e2:e0f8:e57c with SMTP id d9443c01a7336-2f114345b77mr156142685ad.70.1796168834530; Tue, 01 Dec 2026 15:47:14 -0800 (PST) Date: Tue, 1 Dec 2026 15:47:13 -0800 In-Reply-To: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261125182703.1694998-1-yosry@kernel.org> <20261125182703.1694998-3-yosry@kernel.org> Message-ID: Subject: Re: [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions From: Sean Christopherson To: Yosry Ahmed Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Tue, Dec 01, 2026, Yosry Ahmed wrote: > On Tue, Dec 01, 2026 at 09:14:16AM -0800, Sean Christopherson wrote: > > On Wed, Nov 25, 2026, Yosry Ahmed wrote: > > > Always defer PMU counter reprogramming via KVM_REQ_PMU on SVM nested > > > transitions and EFER.SVME toggles instead of invoking > > > kvm_pmu_handle_event() synchronously. The motivation for synchronous > > > handling was to ensure to-be-emulated PMCs are updated before > > > instruction emulation completes, such that instructions count in the > > > mode they retire in (as per the hardware behavior). > > > > > > However, since kvm_pmu_request_counters_reprogram() now recalculates > > > to-be-emulated PMCs synchronously, and kvm_pmu_recalc_pmc_emulation() -> > > > pmc_is_locally_enabled() already accounts for Host-Only and Guest-Only > > > bits on AMD, VMRUN or WRMSR(MSR_EFER) would correctly be counted in the > > > mode they retire in. > > > > > > In addition to being a nice cleanup, this fixes an unprotected SRCU > > > access (and potential UAF) when userspace updates EFER.SVME via > > > KVM_SET_SREGS[2], as svm_set_efer() ends up calling > > > kvm_mediated_pmu_refresh_event_filter() -> pmc_is_event_allowed(), which > > > dereferences kvm->arch.pmu_event_filter via srcu_dereference(). This > > > results in the following splat (with CONFIG_PROVE_RCU): > > > > > > WARNING: suspicious RCU usage > > > arch/x86/kvm/pmu.c:539 suspicious rcu_dereference_check() usage! > > > ... > > > pmc_is_event_allowed+0x27c/0x2b0 [kvm] > > > kvm_pmu_handle_event+0x365/0x880 [kvm] > > > svm_set_efer+0x16b/0x1a0 [kvm_amd] > > > __set_sregs_common+0x2fd/0x6a0 [kvm] > > > __set_sregs+0x37/0xd0 [kvm] > > > kvm_arch_vcpu_ioctl_set_sregs+0x3e/0x50 [kvm] > > > kvm_vcpu_ioctl+0x53a/0x6b0 [kvm] > > > > > > Fixes: a02a25a65246 ("KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions") > > > > I strongly prefer we fix the immediate SRCU issue and then do the more agressive > > cleanup. I don't want to get into a game of whack-a-mole with stable@ backports, > > e.g. if it turns out there's a flaw with always deferring updates. > > Yeah I figured you might prefer this (and called it out in the cover > letter). > > > > > IIUC, it would be this? > > Actually what I had was more localized, in kvm_pmu_handle_event(). It's > consistent with kvm_pmu_trigger_event(), and avoids hiding other bugs. Well, kvm_pmu_trigger_event() is very different, as it's used from the fastpath run loop and we explicitly want to support that. For kvm_pmu_handle_event(), its caller(s) really should acquire SCRU. I'm not opposed to a more precise fix, though if we go that route, I'd vote to make it *extremely* precise, e.g. put it in svm_leave_nested() or something. > I think we generally don't want to hold the SRCU read lock more than > needed? Eh, only if we care about letting flows run in parallel. I suppose it's possible userspace would configure memslots and/or I/O buses concurrently with restoring vCPU state, but I doubt it's a high priority. > I can add this as patch #1 in the series for a stable fix, not sure if > you'd prefer that we keep it anyway or revert it later in the series: If we go with a precise fix, revert it. If we smack all of KVM_SET_SREGS, leave it there.