Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796175272; b=3qYDjaU5gimu79Tn1AkFdBvpkiyZ1Ki43OTOwwf5ojfAJcocm3Z8VZrJWyBsXsAc4saa TZtJQbIFv3tMPxLkHQu8UxWZMo012GVK22GaRRIg4RzttDYkh5VNrPPqRuvuoPQdiJR4W PxBJKlbDCCgr2f5NRZbwKiJI/aeanDnyx0dmk1A1UOQPx9qzmJFhCWpl03fidu2lAXHMl lhCw1USzOqZNgx8ZFpo0QNb1POrljodjxUhIPyesqKvpph0Br2O3ZCFRdAGscuNZhRTrG 4RKahL2uOxoFuY/hVazh+B/TRDJaRhwgw83eA3xsQ3WeGBjMv5UdovxZLubMrTW+ZoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796175272; 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=OCp7M1o0WjugiX5WkkOxZp4tqMOmyUtv5DpaKIhKy2o=; b=gDSbQUXEXxycXLKUc/aXWdmI7GpgmSOUbyweHiXLcZQdIOnTiDkUI0Xl+fcPppZB2LGu /iw4EvLtr8l8DhuYOWkSxBT25uPatQ0eGc1VK7oBxbMcaw6PfRTyKQ25BT0ibCyXMEzHC k77xiX5j1OP05IavHK/zThNpjBuQ+y9b5dPaw6Jx9XSVA0+e/vDECvB1duqro4d56qLvB MLTJS4m/sPfcPDW9+YNWFzz2CiONSKDx+w2MVUeKQntoggMfmAJuxj3ErBXxfSKhd46U9 IgDwlyduemeD5E3A7z7IowtbVG9MdcHlF/8YMzbgqEKSC5ZkospcfoCR3u50+x2kS5w== 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="ACij/Ypp"; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 47A5D606A31A for ; Wed, 2 Dec 2026 01:34:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 47A5D606A31A 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ACij/Ypp Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cdb869327e7so9776008a12.0 for ; Tue, 01 Dec 2026 17:34:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796175271; x=1796780071; 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=OCp7M1o0WjugiX5WkkOxZp4tqMOmyUtv5DpaKIhKy2o=; b=ACij/YppQmhB0EZ7w11DAdGN0QIwZpQlPbb7nx8ZJ//+90Rj55hEuguUuUwLnjb1tl jRKgZAP1qvX6mWq5ZZBL3zOgGeFqXb80KY6QSCyecwXCp90gl9w4TIkzsjoEn1djxGB+ I5F0Jp0AgtIixOTfGAby2Rkos7FHy6wj1pJuZANRRfb6SDJu6KvwzN+bVEjE3W7Pk1zI XZdstnZZlGJz9s13ESx6ULzDId1qhcA/o7xq/lH7KqDVxVuZ95kkk6RpVXKMKTX9w03E osYK6C+ocZbB99osyxbo8aBZ1JQrOPJtsWURgf+d0gemEJriR0uiR+kGC+R1iZAuwNs4 E53g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796175271; x=1796780071; 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=OCp7M1o0WjugiX5WkkOxZp4tqMOmyUtv5DpaKIhKy2o=; b=zaRcQCf8908mxoiQ7sauClovl5YUyL+pn+/0PT3ndVXzkpU4Zz2mgGXlD6VCaV4pcS KhrSxNLqjod306f0a46byCfvH1W6nSGpHKa9bx6VFJ/xyOkfVRxurjQtJvkPDi+zsP66 KISUNlK6virDxX7taTmJKOZ/Ocp6uSm+TnzatIfheKPv69eVUrcft3qzbExnFjU0AKdJ TllERDJ0/XxEBaI3LhOHAIFyseTK7IS2AkshBLxh2ngLJ3O3KbulujKuqjHqMgkEkPS4 VABasVRbWBHoub+wDuaOZUzIEwPXGXoiIFZXrFmPOS+mjJcMLx6RiM+BVUn9X6qZ8Wlw OWRQ== X-Forwarded-Encrypted: i=1; AL+hErGKbscW72JMCaXRTPR1M2s5GK65A5M3ZQ1mReFByaLAlC/0sLUjksnYX3fiXnLfe9XvjkvtNMExg4Z0dMAtxZG0@vger.kernel.org X-Gm-Message-State: APuU4Z8J4jkzzdNnSb9sFgg2jp4J7FmcOehwPW4nZ/P8Qa6bRPOoxFd0 utNMVsMuqefCKa8wCmvF6q61qI6bac910TZOGTBUwW5yzHgmkXuKgLz8rWlpwlFMscSeJyqONn9 7MJ0sHw== X-Received: from pjxd18.prod.google.com ([2002:a17:90a:c252:b0:3b3:fc12:c57c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:da87:b0:3b0:9a2e:b494 with SMTP id 98e67ed59e1d1-3b41c95b089mr36563219a91.54.1796175271344; Tue, 01 Dec 2026 17:34:31 -0800 (PST) Date: Tue, 1 Dec 2026 17:34:30 -0800 In-Reply-To: X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20231107183605.409588-1-seanjc@google.com> <20231117103236.GI3818@noisy.programming.kicks-ass.net> <20231129112019.GG30650@noisy.programming.kicks-ass.net> Message-ID: Subject: Re: [PATCH] perf/x86: Don't enforce minimum period for KVM guest-only events From: Sean Christopherson To: Yosry Ahmed Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu , Jim Mattson , Mingwei Zhang Content-Type: text/plain; charset="us-ascii" On Wed, Nov 25, 2026, Yosry Ahmed wrote: > On Wed, Nov 29, 2023 at 12:20:19PM +0100, Peter Zijlstra wrote: > > On Tue, Nov 28, 2023 at 05:33:16PM -0800, Sean Christopherson wrote: > > > > > If programming a "period" of 1 puts the host at risk in some way, then I agree > > > that this is unsafe and we need a different solution. > > > > IIRC if you put in -1 on a Nehalem, you end up with an NMI-storm which > > wasn't trivial to recover from if at all (it's too long ago and I don't > > have ancient hardware like that anymore :/) > > > > > But if the worst case > > > scenario is non-determinstic or odd behavior from the guest's perspective, then > > > that's the guest's problem (with the caveat that the guest might not have accurate > > > Family/Model/Stepping data to make informed decisions). > > > > Things like bdm_limit_period() will cause odd behaviour IIRC, it does > > daft things like generate extra PEBS records on overflow and gives > > otherwise daft results for PDIR. > > > > glc_limit_period() lacks a useful comment :/ > > Sorry for thread necromancy, but it seems like the discussion stopped > here and I'd like to get this fixed as I am observing the same failures > that Sean did. > > Basically, if KVM needs to program period=1 (guest counter is -1), perf > overwrites it to 2. After 1 event, the guest counter should overflow, > but the hardware counter doesn't, so there is no PMI. On the other hand, > KVM relies on the hardware PMIs to signal overflows while perf events > are counting. > > The overflow is then lost: when KVM pauses the event (e.g. to account > for an emulated instruction), it folds the perf count into its software > counter and restarts the event with a new period, so the late hardware > overflow never comes, and KVM only checks for wraps on its own emulated > counts. > > Sean's fix makes the problem go away on all CPU platforms, and Peter's > makes it go away on Skylake+ on Intel (with updated microcode) and all > AMD platforms. So I am mostly happy with both. It's worth noting that > the blast radius is smaller with Sean's fix as it only affects guest > events. > > A middle ground would be skipping the left < 2 clamp on exclude_host > events, but not the x86_pmu_limit_period() callback, which makes the > problem go away on slightly more CPUs than Peter's patch (I think > Sandy/Ivy Bridge, and Skylake on older microcode versions). This keeps > the *_limit_period() subtleties in place. > > All three versions seem to make the problem go away on AMD Turin. > > WDYT? If Peter's fix works, go with that. I'm largely at the "I don't care" stage when it comes to the emulated vPMU. The only reason I sent this patch is because of KVM's testcase for this exact edgecase. In practice, the emulated vPMU can't get anywhere near perfect, we can only make it suck less. > An alternative to all of this is a "fix" on the KVM side where KVM is > always responsible for signaling all PMC overflows, instead of signaling > some of them from the PMI code path. KVM would keep track of the guest > counter value as of the last pause or write, and on PMI it would just > pause the event, manually check for the counter wrapping, and deliver > the PMI to the guest. > > This would obviously be a more complex change. Yeah, no thanks :-)