Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp284170rdb; Thu, 21 Dec 2023 09:03:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1vV953LKa5lOFFTZUmml/olfq1ipUbe5LQBDTUFJC6rf/Gi0sjZ4wb9qmPscmhoaZ5GQU X-Received: by 2002:a17:902:ea0b:b0:1d0:8e08:35be with SMTP id s11-20020a170902ea0b00b001d08e0835bemr22555242plg.7.1703178189354; Thu, 21 Dec 2023 09:03:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703178189; cv=none; d=google.com; s=arc-20160816; b=A84XAtUtSWXwBLjK2plmMsqoKMGAZaF10YHjNI974LF66kKiLwPIo1ewftqq3EJhdh BWsz4UVwlnFjSsaW10dosq9wC2zR9TG5qg2rbT5324i95KhYLkHm9xEui2IeuZRlfour dqdxJ5bOCB20h5hIWkyZsfVUH4fU8RIXO7j1FRIEPCnop4zDt03fRIJ9RZU6+gfdIHJ4 xPwKLdY1p9X+ujzmnLrrjo+IvHRPI3TlKNtwjrIeTwreHvneXZYmLaGNRpvdYWZ5gyf4 iVG6f/Lu7ujRnvX+i/aXJdFblzPTEzO62orl7hO5BLYSWeYH3vwfNf+6fgts95z0EibG 7SrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :date:cc:to:from:subject:message-id:dkim-signature; bh=MTJ5ESIOyUqDB8n0Xz8qCRHr+4/Mt0yPLsVWj0QhBx4=; fh=Sf37RCh/BYkmFG6WzP391DxZIXoJpXlzESiwZNkt1os=; b=0pOZ2TKLgYppJktwNxBomxHbXzx+88XEASympqAa/zbw9L5jjIj3lIIOh2Qn+avwdg Iwzoi9BTTr8stHFrGjmipRXAN4OL+lVbWrgcAz2SQBgxUxkvhxrOmrGGdr6OVvNILg7C JSZv3DtfrSAX9cWVlu87C5rrTtF4nl56t0XK0fI5f3v2vaTVCNyZ2fxGDrTdbycNurbm h1R4ihqCtbxlzwTFZJ+7jQ+qbEIBItdL5dIMjdLLGdTZOFcmtSW+C7vdHi/TbnrfMlJD uCFbxva/2VhdHbHSzZeBj168e60wmQ6JCUOclxed9hFW0+OBzx0T7q9Ln0I/bgIJ/ndk wYQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MOY7DnoZ; spf=pass (google.com: domain of linux-kernel+bounces-8809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8809-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b8-20020a63d808000000b005c680fbab19si1809476pgh.513.2023.12.21.09.03.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 09:03:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MOY7DnoZ; spf=pass (google.com: domain of linux-kernel+bounces-8809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8809-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 233C0B242EC for ; Thu, 21 Dec 2023 17:01:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3F30627E5; Thu, 21 Dec 2023 17:01:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MOY7DnoZ" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74FA059924 for ; Thu, 21 Dec 2023 17:01:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703178072; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MTJ5ESIOyUqDB8n0Xz8qCRHr+4/Mt0yPLsVWj0QhBx4=; b=MOY7DnoZ/b4FCXh9kHlVaPLlKuclFCkcs8rUZSDU0W6Is84CQAhj92KZhQARqQUABoObPG k6+q50o6e16WkpnCLGcpmsrL59ETVcye9fwD9O+WUhoIp3EwVk78GjR2RC22ftxhSaaZE9 FS1IJWVIVcblQN8y+Tji8jfgfPXc9t8= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-575-qt-fjS7nMii7M50Cqo9KZA-1; Thu, 21 Dec 2023 12:01:10 -0500 X-MC-Unique: qt-fjS7nMii7M50Cqo9KZA-1 Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-553dd5dfba8so1124468a12.0 for ; Thu, 21 Dec 2023 09:01:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703178069; x=1703782869; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=MTJ5ESIOyUqDB8n0Xz8qCRHr+4/Mt0yPLsVWj0QhBx4=; b=tZDYrtZ7KOhGm3lk9sHPIFmqMSVuU+fPbNjE0H8ELLtgJNtWtUUfKcjxLUF/gutxh2 xYitHA/glOCjvAjHfwoySpSjhz/ze98z5sUgdq7OA0YA/OOlKK2y1BTvVnewoyGnuKVX fmgkEA2a99sbHVudOKDmozBobPFWdj7JCNeZCf8Ba/0NTiGF1VpVKxlincU/TmSswb/p d3inaJ46jd8AINk8eaJOb4YfnnUYSviXAc+H0dkCXQ6E5ccU9bbaaI9yDN8A2eZFmT85 IMGmMw6+KWcFnuTyYmXBT4jpgzgYpHGOGX1HNaW4OW+VhMvQbvY1vZYgzOGylKruj0AH q77A== X-Gm-Message-State: AOJu0YwfxBE+4P/ATIHa6v0R9CPKflwJFJsMG/SYpecW2wuWa65lEyVP k4zYf2uR5KrbZZm9cUiI8snCI89b4b957kYD4xjzsUHcZR3ja2XKBD3Y/MWxY+klZ7v+lnRFZer PYYB6ZpYaN1Ewy2nSWb9zCBb9 X-Received: by 2002:aa7:cf99:0:b0:552:fccb:c3b0 with SMTP id z25-20020aa7cf99000000b00552fccbc3b0mr1154658edx.25.1703178069310; Thu, 21 Dec 2023 09:01:09 -0800 (PST) X-Received: by 2002:aa7:cf99:0:b0:552:fccb:c3b0 with SMTP id z25-20020aa7cf99000000b00552fccbc3b0mr1154644edx.25.1703178069009; Thu, 21 Dec 2023 09:01:09 -0800 (PST) Received: from starship ([77.137.131.62]) by smtp.gmail.com with ESMTPSA id f20-20020a056402195400b005543f50e53asm258311edz.93.2023.12.21.09.01.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 09:01:08 -0800 (PST) Message-ID: Subject: Re: [PATCH v2 1/3] KVM: x86: Make the hardcoded APIC bus frequency vm variable From: Maxim Levitsky To: Isaku Yamahata , Sean Christopherson Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Vishal Annapurve , Jim Mattson Date: Thu, 21 Dec 2023 19:01:06 +0200 In-Reply-To: <20231219014045.GA2639779@ls.amr.corp.intel.com> References: <1c12f378af7de16d7895f8badb18c3b1715e9271.1699936040.git.isaku.yamahata@intel.com> <938efd3cfcb25d828deab0cc0ba797177cc69602.camel@redhat.com> <20231219014045.GA2639779@ls.amr.corp.intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit On Mon, 2023-12-18 at 17:40 -0800, Isaku Yamahata wrote: > On Thu, Dec 14, 2023 at 08:41:43AM -0800, > Sean Christopherson wrote: > > > On Thu, Dec 14, 2023, Maxim Levitsky wrote: > > > On Wed, 2023-12-13 at 15:10 -0800, Sean Christopherson wrote: > > > > Upstream KVM's non-TDX behavior is fine, because KVM doesn't advertise support > > > > for CPUID 0x15, i.e. doesn't announce to host userspace that it's safe to expose > > > > CPUID 0x15 to the guest. Because TDX makes exposing CPUID 0x15 mandatory, KVM > > > > needs to be taught to correctly emulate the guest's APIC bus frequency, a.k.a. > > > > the TDX guest core crystal frequency of 25Mhz. > > > > > > I assume that TDX doesn't allow to change the CPUID 0x15 leaf. > > > > Correct. I meant to call that out below, but left my sentence half-finished. It > > was supposed to say: > > > > I halfheartedly floated the idea of "fixing" the TDX module/architecture to either > > use 1Ghz as the base frequency or to allow configuring the base frequency > > advertised to the guest. > > > > > > I halfheartedly floated the idea of "fixing" the TDX module/architecture to either > > > > use 1Ghz as the base frequency (off list), but it definitely isn't a hill worth > > > > dying on since the KVM changes are relatively simple. > > > > > > > > https://lore.kernel.org/all/ZSnIKQ4bUavAtBz6@google.com > > > > > > > > > > Best regards, > > > Maxim Levitsky > > The followings are the updated version of the commit message. > > > KVM: x86: Make the hardcoded APIC bus frequency VM variable > > The TDX architecture hard-codes the APIC bus frequency to 25MHz in the > CPUID leaf 0x15. The > TDX mandates it to be exposed and doesn't allow the VMM to override > its value. The KVM APIC timer emulation hard-codes the frequency to > 1GHz. It doesn't unconditionally enumerate it to the guest unless the > user space VMM sets the CPUID leaf 0x15 by KVM_SET_CPUID. > > If the CPUID leaf 0x15 is enumerated, the guest kernel uses it as the > APIC bus frequency. If not, the guest kernel measures the frequency > based on other known timers like the ACPI timer or the legacy PIT. > The TDX guest kernel gets timer interrupt more times by 1GHz / 25MHz. > > To ensure that the guest doesn't have a conflicting view of the APIC > bus frequency, allow the userspace to tell KVM to use the same > frequency that TDX mandates instead of the default 1Ghz. Looks great! In theory this gives me an idea that KVM could parse the guest CPUID leaf 0x15 and deduce the frequency from it automatically instead of a new capability, but I understand that this is (also in theory) not backward compatible assuming that some hypervisors already expose this leaf for some reason, thus a new capability will be needed anyway. Thus I have no more complaints, and thanks for addressing my feedback! Best regards, Maxim Levitsky > > There are several options to address this. > 1. Make the KVM able to configure APIC bus frequency (This patch). > Pros: It resembles the existing hardware. The recent Intel CPUs > adapts 25MHz. > Cons: Require the VMM to emulate the APIC timer at 25MHz. > 2. Make the TDX architecture enumerate CPUID 0x15 to configurable > frequency or not enumerate it. > Pros: Any APIC bus frequency is allowed. > Cons: Deviation from the real hardware. > 3. Make the TDX guest kernel use 1GHz when it's running on KVM. > Cons: The kernel ignores CPUID leaf 0x15. > >