Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp413790rdb; Thu, 1 Feb 2024 12:06:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IGkRihIOWAeJftXdfEHOWCtMKdgSMk1ZVTppFxWiXytutUUEEfdcqm2uIGuE19h9FZizTnx X-Received: by 2002:a05:6a20:3d84:b0:19c:a8f0:d8bb with SMTP id s4-20020a056a203d8400b0019ca8f0d8bbmr119883pzi.16.1706818000979; Thu, 01 Feb 2024 12:06:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706818000; cv=pass; d=google.com; s=arc-20160816; b=gu531oS9Z1FgleFF95WmVcaENXA4CDEa7KO1FeXueppOyK8QPx75Fa0l9wYfSYLwIp ++EWL7tyt/g7OwBD8BrJlfw8nlpKE0zZ6hleZAUqxyPw+DdPs9LVKXx2dCvWM/7ZuUgL 3A53HxN4ha1bazy7SkyV+M+871MD+COcJqU69JTHM4aTwv1cY34Edxi30ul9wCno9MhP jYcRcQUcyawbu1zS7T0IBw+39BPCl7w1Fn3hT4N93qYSB9eh7gpi1rPxcEhPUTMKcU99 9W9I3Y5aeeiUo5RAen+d063zEH4hEOY3xDq3RIhQLSWnHuB9uxMctZ3dVgqXIhhZGJ9n F3ug== 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=FsllCZK2FAGWJHcpt8xBy9rt8mAFX0Y7/cdzkhjefJw=; fh=cApxQzKsuzX+9AFv10p976r77cVk1u9oK+tYN2C6ssc=; b=02r+eniYZ9s4YSm2jeh7XeyLFKPQPckWB4aEr63K8vK4bqUA5/Ke2yyuOe2i2NEXzh vSxl/ZDBoQJ8018SkICFDuheT/aXPfj3RWOsigaBnpeFbm1AZzNG7YOvC7tB5RPJBCL8 wkx+FeXktk3BO4s+PbGO412yOy/03IwvjxTHHW711QJwqi2WwjUY4P2unOfHkG0VXpoB MadUZoiIPBO93fN85gW/ONMulUDCfaMnd5U0DXU5AyCuwf8EL7Ssm9nYxLk03tKzUwbx 1BzCQ71748llRy7ZKpKuzgWBcyyrShp5eiR3L8oIphAzQADGng4GqXPi6UScjsJ63ar7 IuJQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cIaNTsdg; 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-48776-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48776-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; AJvYcCUpgsKdXhFrr3poxwp0uivHwAaSh2GT0nGlBQByQozaw50g9JxqsaSEbCERVcJDAmTXQECSj6llyMt8RWiSNUQ6MUxfcGpOVJ32BoVy2g== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 37-20020a630b25000000b005cf5895bfa6si243276pgl.814.2024.02.01.12.06.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 12:06:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48776-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cIaNTsdg; 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-48776-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48776-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E7402B28F3B for ; Thu, 1 Feb 2024 19:36:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30A0A8528E; Thu, 1 Feb 2024 19:36:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cIaNTsdg" Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 0657984FCC for ; Thu, 1 Feb 2024 19:36:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706816178; cv=none; b=uh0qt7fJJVkyzn2Z1yQEqedI8MT8A6FViFgh/GFy6dXo2nA4Y/xB5SLax21Efbhd+geoFeIJ1IHWJNJYQ27aKH2SJgpOiMJXROHJRY4UlgbtlGu9xfVoFCVTnZ5aLXftz8OS6sH+FNmxqFuOAVuA8yW0oSAD8/lm2FPpE0tO8Tg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706816178; c=relaxed/simple; bh=qfMTZUwrX0hQseYFBDB6O+l3/V7yP1wzA1uQCR9WF6M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V10a9gBb4pxyMrSkTYy+MdSjulAHjMYQz3STYxdipGiyxdFoXJHy+RB5MRNVh/6Co9YcHKicfcXznfDCs9Q5B7BbjecAtAJXa8dIxilIl4ouYN6LlPumTQjZaQi6SzJuVLx+3CGhW5LU50YPWbYfCTM6LakKJFqZfBsKGheLEew= 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=cIaNTsdg; arc=none smtp.client-ip=209.85.214.202 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-pl1-f202.google.com with SMTP id d9443c01a7336-1d96da0f7b1so468335ad.3 for ; Thu, 01 Feb 2024 11:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706816176; x=1707420976; 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=FsllCZK2FAGWJHcpt8xBy9rt8mAFX0Y7/cdzkhjefJw=; b=cIaNTsdgRqqgDv8m1WRIY9xzMLxL5yvzgHNSZVnL5gbjVk8YtpQw6QSsvoNn/skTWD eckgKoAG3avVzDHgtLdFYtqW0hSIPcT1tGXKEDpXioJvK8EJcewQZ+dPzuuoo5tBYL7I 9DgWOA/b21QojdCu8ESBusw6wuYVlw+XIyYBOMD3QOl6MqcMTARldYR2CnxldT5RNnA1 P1/cYEtJH4tevcECe/nY7M+Kx98uK5P5GXrTYFKL7rWehbwILQerOuXa1v7dHlQLLacw pQU7miM2gJGx+30SxuUDXqMRH199rlxq4ABVmUwHhcevOVE7+kHjjfNhtztDa+VUYp6Z 002w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706816176; x=1707420976; 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=FsllCZK2FAGWJHcpt8xBy9rt8mAFX0Y7/cdzkhjefJw=; b=B30r1/mgFUO6Fb9EJtZHJqwu8KYQlTgSk/jaG5LevoeIgwyG2GNu67GcMlHt9ZNF1g R9l2dBpKjOMWL4XliRZFVeyAXbs+zKIy9YUWOWLQX422d3hve+G126RyGu/rZrTZci+I pfG0dAW8nYHaDjqyLGt0CLI3wMFEc0vgNtw2m3IkQHIpO6vu192UCJqQPim0Z136FJkC zSIsd6+MG+/jdZ/C5v+2nFX4NvtNzxVS/1fvcYWWj6e6ruNwpOhOIhKENCho/BryXu7h 49ORG2ZeputnmkrA+fuTiLjRs5gr8w4a+4KcggBcAm8UTbWiTka6ZGtLW7YL6ImO8j4e dpkg== X-Gm-Message-State: AOJu0YyiGVa5EllWFDAdbDMptHRwdQ8gJRu0hcurLKsef51rtmF/vymc 3aC9p0DjN9k7jOLzvQ3pyciO66oibLk0DhxPr6FiPWBePHyrM0FxTRuoMOaZA4PyNkBb30fnhCn Cag== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:8ecc:b0:1d8:debb:413a with SMTP id x12-20020a1709028ecc00b001d8debb413amr18118plo.8.1706816176244; Thu, 01 Feb 2024 11:36:16 -0800 (PST) Date: Thu, 1 Feb 2024 11:36:14 -0800 In-Reply-To: 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> Message-ID: Subject: Re: [PATCH] KVM: x86/pmu: Fix type length error when reading pmu->fixed_ctr_ctrl From: Sean Christopherson To: Mingwei Zhang Cc: Dongli Zhang , Paolo Bonzini , "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" 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, are already disabled. As a result, if the guest disables a fixed counter, KVM will get a false negative and fail to reprogram/disable emulation of the counter, which can leads to spurious PMIs in the guest.