All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Roman Kagan <rkagan@amazon.de>
Cc: kvm@vger.kernel.org, Dave Hansen <dave.hansen@linux.intel.com>,
	Jim Mattson <jmattson@google.com>, Like Xu <likexu@tencent.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	x86@kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	Eric Hankland <ehankland@google.com>,
	linux-kernel@vger.kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Borislav Petkov <bp@alien8.de>, Ingo Molnar <mingo@redhat.com>
Subject: Re: [PATCH] KVM: x86: vPMU: truncate counter value to allowed width
Date: Mon, 5 Jun 2023 17:51:51 -0700	[thread overview]
Message-ID: <ZH6DJ8aFq/LM6Bk9@google.com> (raw)
In-Reply-To: <20230504120042.785651-1-rkagan@amazon.de>

On Thu, May 04, 2023, Roman Kagan wrote:
> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
> index 5c7bbf03b599..6a91e1afef5a 100644
> --- a/arch/x86/kvm/pmu.h
> +++ b/arch/x86/kvm/pmu.h
> @@ -60,6 +60,12 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
>  	return counter & pmc_bitmask(pmc);
>  }
>  
> +static inline void pmc_set_counter(struct kvm_pmc *pmc, u64 val)
> +{
> +	pmc->counter += val - pmc_read_counter(pmc);

Ugh, not your code, but I don't see how the current code can possibly be correct.

The above unpacks to

	counter = pmc->counter;
	if (pmc->perf_event && !pmc->is_paused)
		counter += perf_event_read_value(pmc->perf_event,
						 &enabled, &running);
	pmc->counter += val - (counter & pmc_bitmask(pmc));

which distills down to

	counter = 0;
	if (pmc->perf_event && !pmc->is_paused)
		counter += perf_event_read_value(pmc->perf_event,
						 &enabled, &running);
	pmc->counter = val - (counter & pmc_bitmask(pmc));

or more succinctly

	if (pmc->perf_event && !pmc->is_paused)
		val -= perf_event_read_value(pmc->perf_event, &enabled, &running);

	pmc->counter = val;

which is obviously wrong.  E.g. if the guest writes '0' to an active counter, the
adjustment will cause pmc->counter to be loaded with a large (in unsigned terms)
value, and thus quickly overflow after a write of '0'.

I assume the intent it to purge any accumulated counts that occurred since the
last read, but *before* the write, but then shouldn't this just be:

	/* Purge any events that were acculumated before the write. */
	if (pmc->perf_event && !pmc->is_paused)
		(void)perf_event_read_value(pmc->perf_event, &enabled, &running);

	pmc->counter = val & pmc_bitmask(pmc);

Am I missing something?

I'd like to get this sorted out before applying this patch, because I really want
to document what on earth this new helper is doing.  Seeing a bizarre partial
RMW operation in a helper with "set" as the verb is super weird.

  parent reply	other threads:[~2023-06-06  0:51 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-04 12:00 [PATCH] KVM: x86: vPMU: truncate counter value to allowed width Roman Kagan
2023-05-23 12:40 ` Like Xu
2023-05-23 16:42   ` Roman Kagan
2023-06-06  0:26     ` Sean Christopherson
2023-06-06  0:51 ` Sean Christopherson [this message]
2023-06-29 21:16   ` Jim Mattson
2023-06-30  0:11     ` Sean Christopherson
2023-06-30  0:31       ` Jim Mattson
2023-06-30 11:14       ` Roman Kagan
2023-06-30 14:28         ` Sean Christopherson
2023-06-30 15:21           ` Roman Kagan
2023-06-30 15:45             ` Sean Christopherson
2023-06-30 17:07               ` Mingwei Zhang
2023-06-30 17:16                 ` Jim Mattson
2023-06-30 17:32                   ` Mingwei Zhang
2023-06-30 18:03                     ` Mingwei Zhang
2023-06-30 21:26               ` Sean Christopherson
2023-07-01 19:51                 ` Mingwei Zhang
2023-08-11  8:30                 ` Dapeng Mi
2023-08-22  9:29                 ` Like Xu
2023-08-23 18:28                   ` Mingwei Zhang
2023-07-03 13:33               ` Roman Kagan
2023-06-30 16:40             ` Jim Mattson
2023-06-30 23:25               ` Jim Mattson
2023-09-28 16:41 ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZH6DJ8aFq/LM6Bk9@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=ehankland@google.com \
    --cc=hpa@zytor.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=likexu@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rkagan@amazon.de \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.