Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp966873rdb; Fri, 2 Feb 2024 09:12:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj9tfC7j1/J4Oq76S87+fwGLpVy7OhfNoTC5+QS5fqoQeDBpmT+UilL+1EQZhbV1v/PgcC X-Received: by 2002:a05:6a21:9102:b0:19c:5ae6:4425 with SMTP id tn2-20020a056a21910200b0019c5ae64425mr3711835pzb.59.1706893960913; Fri, 02 Feb 2024 09:12:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706893960; cv=pass; d=google.com; s=arc-20160816; b=V7r+3oIFxd997wIXSC1VaJadKa1WiSP4d681ZPYV2TeGae7QvEEuRNeYDrHjYiEWpS pwePAk0byCp2O0jqgQXe+J+FGCv4Go3P1NbGqzxWQFjabgorL6g1dY5WYR4+GZFNm2OG Ljui0XiKs6awZMdp6GgVGX7DM91KtgglGL/wr8utD+f056Lzer95vhC5hIirynT6kDnm 1EuhBal+web24w5u9rtm7Ego6btKwBp5yTRWQdGmGtS7koio1rDoxMXJ3ExYF7v8YqUM FmNHXH/86N9l0/usooUg4AzwPn8GZ99HRlQlLS5YNjpHJvF2M42qaIxs2EaJsdQwbmpN wyAQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=52hREpEdz1wY5YQcf323vOpsz5jH6pb2sFOoSjdgNLo=; fh=1F1113hcVLbcAOrL5J9+5OVVDXEznWE+3CrXAx7kWX8=; b=HFx2p7LhVcPI+avgx1AO3e8Tn20awtbi5jSRyjKRRHVPkGmgLF24ykGMbRnc07bSVx KpkE91veSQxldc53gx410mYdAepbzuxhd7y7GGb4Z7X5nLka0wTDCqzpenjjuqvCVtaN rE5DBTn5wm7NTLizbDxHMiAlAuHG7xJgbxI3D5MpO1y4eY0SKRv++h3YuQvAUoMMt4Wm CB80Hgrk7ObD8Huxd7hqikDStQPG79kwB9W+R9gBV8/qq6nbWi8qQ+RegMA4/tayd8G/ y4qd8D6OBUWYYiJD2NO+f4N1aUdI/zGWfcDVzmJGMQPeTuInC59lflc0AILBYadHdXlU z3Jw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IUjnD8Sb; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50207-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50207-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXkZKUdloEczqykyw5GAnWrq+zqlL0itjFKoempMQdMOEyxOH6H6mY+1yMsC0om/ohiCj2kLrSIK9+26u0rM3UxQDuwR7AQsHxEXy2vMg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z7-20020a656647000000b005cd783d410csi1790511pgv.610.2024.02.02.09.12.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 09:12:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50207-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IUjnD8Sb; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50207-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50207-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BF04128D440 for ; Fri, 2 Feb 2024 17:08:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 51F6514D432; Fri, 2 Feb 2024 17:07:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IUjnD8Sb" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 BA7F314A4F8 for ; Fri, 2 Feb 2024 17:07:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706893637; cv=none; b=fsMrOxuZO4SEav7G66kVlXzblm6O1urg5iOn6I+IVt0LLEflMjgm/wxvS/vPlbWhkkyohHtvZDXoY0mqRJt4hd5U6+3SWrFcXlACd6MFIFXNYly81aodyzhiFWWhj5C8njeLp57PSopu28iToPY4+pXP7/NQUygXxhTwsy3gPqA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706893637; c=relaxed/simple; bh=ZxYwtQl6gwkvn4vNBtszF0Bkjvg2DtSi0J2BX7wpt0I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fbzeyBLZ6wUdRsSMdYrFNwhESk8iIN3Cw+qa/7AOF5Qc2GX01bxNU0XWk6TBTCNMBUTvj7GNOi00sWyQG+B4CtqR1BTURZuRraNY6FFHeAUf+graQD2HWQkzMko2STRnrQ6MSqCsSagAIPnHcOkLTSHWU2lVxAjR/kptdryk02Q= 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=IUjnD8Sb; arc=none smtp.client-ip=209.85.128.201 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6041bb56dbfso33971017b3.2 for ; Fri, 02 Feb 2024 09:07:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706893635; x=1707498435; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=52hREpEdz1wY5YQcf323vOpsz5jH6pb2sFOoSjdgNLo=; b=IUjnD8SbUswvKgPrQdKkOr6oQcGZRuLtv1WIzQgie9KMEz2kTg7czQExQriW6rTUB5 3kzAdeIL6KTVIy2Pu3E8tAVFjO1B8JlrYDkgiLpInp5FHo0dQRswHSOAI1dAIQFYBHz9 6gf/Jn4K7dDzOpNnt1wfXfPtonlxXvBaBhQVqhaSXTIRYLcTopeuPkLZpKVhUneFKc2Q A21Nt2LtrW7gYZ5hVgXSDg6tQZWcgJblYqX4Z9iTV1D8JY7emZ1C3ARUHoFbZUvQ/r0N 50qOFApM98meSeXwi61tFfVF8G3eb0AO5TD+qo6AnCW6wo2ZU20KxH5oS6cdocUOZb1e 1enA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706893635; x=1707498435; h=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; bh=52hREpEdz1wY5YQcf323vOpsz5jH6pb2sFOoSjdgNLo=; b=NIMdxp8lSJCnh+4ue1L2rirPSD49d3tymaoVZr3QYwN9YcivpbThhPWojbbKaSsPPw lMAyPoqDrB7yVN/Sei932HNmdTBg0dxLxahKihLfDPME405dqw/VZ1U/b7/LmMD16mrk Srh7RNqniEXPPWmpxG5qrZKcER9MNCeRX1sUivET5X3FNNlRVVPUYIpBfKeRzGdvb0Vk 5WEy9svo4mvfHSpBquaE6ZazFj11pcQ8ZmDGzu+4gZe/C5zQ0GBpMBsEaEHNWPIU6LXz +S7vBlkyH6Jz94VxrW9EUV7rljJV75DD58SIx8YrkP0iygFcPM9pP5KvPAHWeqVF3t4x kuSQ== X-Gm-Message-State: AOJu0YwDDgkz+03wZH9OHjGxrPqGkjAyxpk3YJSkLKghZk1ulfEnD+XW OLqfxhIlE4Aj2YxwHoZie5ANvKqRY2UBNXsxNka9/oYCnp70v9c72JQtHma6lEmtdCK6AggVHvj gIw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:9805:0:b0:602:d545:a3bb with SMTP id p5-20020a819805000000b00602d545a3bbmr990910ywg.1.1706893634791; Fri, 02 Feb 2024 09:07:14 -0800 (PST) Date: Fri, 2 Feb 2024 09:07:13 -0800 In-Reply-To: <9098e8bb-cbe4-432c-98d6-ce96a4f7094f@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123221220.3911317-1-mizhang@google.com> <368248d0-d379-23c8-dedf-af7e1e8d23c7@oracle.com> <9098e8bb-cbe4-432c-98d6-ce96a4f7094f@linux.intel.com> Message-ID: Subject: Re: [PATCH] KVM: x86/pmu: Fix type length error when reading pmu->fixed_ctr_ctrl From: Sean Christopherson To: Xiong Y Zhang Cc: Mingwei Zhang , Dongli Zhang , Paolo Bonzini , "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Fri, Feb 02, 2024, Xiong Y Zhang wrote: > > > On 2/2/2024 3:36 AM, Sean Christopherson wrote: > > On Thu, Feb 01, 2024, Mingwei Zhang wrote: > >> On Thu, Feb 01, 2024, Sean Christopherson wrote: > >>> On Wed, Jan 31, 2024, Mingwei Zhang wrote: > >>>>> The PMC is still active while the VM side handle_pmi_common() is not going to handle it? > >>>> > >>>> hmm, so the new value is '0', but the old value is non-zero, KVM is > >>>> supposed to zero out (stop) the fix counter), but it skips it. This > >>>> leads to the counter continuously increasing until it overflows, but > >>>> guest PMU thought it had disabled it. That's why you got this warning? > >>> > >>> No, that can't happen, and KVM would have a massive bug if that were the case. > >>> The truncation can _only_ cause bits to disappear, it can't magically make bits > >>> appear, i.e. the _only_ way this can cause a problem is for KVM to incorrectly > >>> think a PMC is being disabled. > >> > >> The reason why the bug does not happen is because there is global > >> control. So disabling a counter will be effectively done in the global > >> disable part, ie., when guest PMU writes to MSR 0x38f. > > > > > >>> fixed PMC is disabled. KVM will pause the counter in reprogram_counter(), and > >>> then leave the perf event paused counter as pmc_event_is_allowed() will return > >>> %false due to the PMC being locally disabled. > >>> > >>> But in this case, _if_ the counter is actually enabled, KVM will simply reprogram > >>> the PMC. Reprogramming is unnecessary and wasteful, but it's not broken. > >> > >> no, if the counter is actually enabled, but then it is assigned to > >> old_fixed_ctr_ctrl, the value is truncated. When control goes to the > >> check at the time of disabling the counter, KVM thinks it is disabled, > >> since the value is already truncated to 0. So KVM will skip by saying > >> "oh, the counter is already disabled, why reprogram? No need!". > > > > Ooh, I had them backwards. KVM can miss 1=>0, but not 0=>1. I'll apply this > > for 6.8; does this changelog work for you? > > > > Use a u64 instead of a u8 when taking a snapshot of pmu->fixed_ctr_ctrl > > when reprogramming fixed counters, as truncating the value results in KVM > > thinking all fixed counters, except counter 0, > each counter has four bits in fixed_ctr_ctrl, here u8 could cover counter 0 > and counter 1, so "except counter 0" can be modified to "except counter 0 and > 1" Ugh, math. I'll adjust it to: Use a u64 instead of a u8 when taking a snapshot of pmu->fixed_ctr_ctrl when reprogramming fixed counters, as truncating the value results in KVM thinking fixed counter 2 is already disabled (the bug also affects fixed counters 3+, but KVM doesn't yet support those). As a result, if the guest disables fixed counter 2, KVM will get a false negative and fail to reprogram/disable emulation of the counter, which can leads to incorrect counts and spurious PMIs in the guest. Thanks!