Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5628539rdb; Wed, 13 Dec 2023 14:40:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IEteBiZQUjq2L9OvB8vConAIKFy1lT3iJ248agx6Xy4h9wC3ya7TyxIZxLft1XeRIicvGtd X-Received: by 2002:a9d:6c56:0:b0:6d9:f6b6:939 with SMTP id g22-20020a9d6c56000000b006d9f6b60939mr8841031otq.73.1702507254047; Wed, 13 Dec 2023 14:40:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702507254; cv=none; d=google.com; s=arc-20160816; b=lB6pm2iEkcLPyAwuHJkm03yH3ITAn1vVVLItQ6ulyRivsy5w7lYWr2+EJPv6uayPBm YVICtGmIwtEZouhFjip8f8oPEclp/o4pGPGqvILkqUQhcYn2+iWqskQebrTYM2j3/2Iz 0N+fT33fMVygYt+s34NRNz16Cqqo/N+vS+j0VjwzK2B27lN3kmtKTXCJdMo71HDLjtt1 0MyOfVgJz18p4TWZW4dblVNph7GmlduvKEnRyf+0HZAn5G94Y5BlgXwvYqL7aI1KYiGD RSawluggeIJfy31JQCnrxbpIU+BWRJDUAFVvl464zxeXbe6Pp93hvTBP+ij9+y3QfmTB +O7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=U6q+SujD6T+0deWVz0gmo5g4QASJTJJ5VQOUwvjSYJk=; fh=FXFKIQD420JFOz0zug9mSK7maOz2MFsJO4J3hdidocI=; b=sVObnGMp1P73rfGp2DWahUHD9iVEsG6dSyLhAT8sMI0/5B72WUXl7mVkzuqSXQzQMT ItkGG3WN6dkrPK5XqlX0Ns2wYzVjT4hQ9N/0yBBOxzpHaGSHNImVYxG9FIVQ1InPXc2y iQe9rVpIT0vZ8otvobjcri5fZ0Bti2U4pONkwV0n1voT0RxZY4vm9cVNRlks+L51S0oO h8VcUO8ApP/vAMk3rDm6sbq8rSTG5VipoNFWAo24iR6kn+ZAIz4p8JvNnYha3zFh6m9Q LWhLt2UqPH2bWwsa6NRUFK4s13Y9Ww7oLFNimStyGyqC6CaiH7yuZk7Q/ugCi3Y7RXcH yxow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MZy7C13r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id u12-20020a056a00124c00b006ce50011c9asi10247019pfi.345.2023.12.13.14.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 14:40:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MZy7C13r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 1ED2F819D9D0; Wed, 13 Dec 2023 14:40:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233928AbjLMWkX (ORCPT + 99 others); Wed, 13 Dec 2023 17:40:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbjLMWkT (ORCPT ); Wed, 13 Dec 2023 17:40:19 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15F9C111 for ; Wed, 13 Dec 2023 14:40:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702507224; 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=U6q+SujD6T+0deWVz0gmo5g4QASJTJJ5VQOUwvjSYJk=; b=MZy7C13rUA8xtbdBueinfeqCEFrRGPCuID0gTR/4CDOQdmjoe7VE+AkVislmci2YIuDw4B /AEZA3lMevUb3U0K8VaJRdcGBXT9eQx/vx/TLiSBzYFMAm/4O1pS9lLfGYfCWD6ttyRa3N inzj7Cge4n1mM69AV3aVZduZXgAUjJU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-N9tP6_86NX6VW8dUv98Tnw-1; Wed, 13 Dec 2023 17:40:22 -0500 X-MC-Unique: N9tP6_86NX6VW8dUv98Tnw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3334286b720so6218553f8f.1 for ; Wed, 13 Dec 2023 14:40:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702507221; x=1703112021; 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=U6q+SujD6T+0deWVz0gmo5g4QASJTJJ5VQOUwvjSYJk=; b=tA4A13kBWxBejjsrta73JfsscKSkjowOKryBCyk89tluYO26FL8iTfbu2hcBQfCeME nodq8rTU6Mt/lzG5l3n0zOQeWm7n129Q63RNiY50A7NuTjIbpLkxACyOa3DvhjsaMv5A 2ythwJpeYOKy/441PuEx9vIzyxi7L/z8n5Fp196sxBAauoJ5BdEhdTdGkbsVc7iW+AwV EoBnvWxqZnFRfjn+QBHFrsv33RI+2O1CuVY9pJkhSmYf1Llc1KtyVtx3vlxMIbdMK554 6okP+61PkHN33rpNu1aABv4bMIf/pgcLvE9H910CBgJI/cbsz/ghgjXoe9lFAhUTChjS Z1Rw== X-Gm-Message-State: AOJu0YzkvQxMYZc8jxlNSe3k5Lz58RO7cG3O8kSotMkZPbwwe9i1conM +4ntmU7GapVtN3tJAQUVsxLsU3fSOZvHhyYCCWcZ8JCB9eOtfDZ9B1w+otqqxENnRu0VyThlTCv 7674Jy2DB2kJKJVbqM1Idzp3N5ZlC19ah X-Received: by 2002:a05:600c:1649:b0:40c:328f:e0ec with SMTP id o9-20020a05600c164900b0040c328fe0ecmr2082715wmn.310.1702507221201; Wed, 13 Dec 2023 14:40:21 -0800 (PST) X-Received: by 2002:a05:600c:1649:b0:40c:328f:e0ec with SMTP id o9-20020a05600c164900b0040c328fe0ecmr2082713wmn.310.1702507220970; Wed, 13 Dec 2023 14:40:20 -0800 (PST) Received: from starship ([77.137.131.62]) by smtp.gmail.com with ESMTPSA id be9-20020a05600c1e8900b0040596352951sm24529011wmb.5.2023.12.13.14.40.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 14:40:20 -0800 (PST) Message-ID: <61530aa466a038efb0e648da002432d02a9692a3.camel@redhat.com> Subject: Re: [PATCH v2 2/3] KVM: X86: Add a capability to configure bus frequency for APIC timer From: Maxim Levitsky To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Vishal Annapurve , Jim Mattson Date: Thu, 14 Dec 2023 00:40:18 +0200 In-Reply-To: <88774b9dc566c89141bf75aef341fdf7e238e60b.1699936040.git.isaku.yamahata@intel.com> References: <88774b9dc566c89141bf75aef341fdf7e238e60b.1699936040.git.isaku.yamahata@intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 13 Dec 2023 14:40:51 -0800 (PST) On Mon, 2023-11-13 at 20:35 -0800, isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > Add KVM_CAP_X86_BUS_FREQUENCY_CONTROL capability to configure the core > crystal clock (or processor's bus clock) for APIC timer emulation. Allow > KVM_ENABLE_CAPABILITY(KVM_CAP_X86_BUS_FREQUENCY_CONTROL) to set the > frequency. > > TDX virtualizes CPUID[0x15] for the core crystal clock to be 25MHz. The > x86 KVM hardcodes its frequency for APIC timer to be 1GHz. This mismatch > causes the vAPIC timer to fire earlier than the guest expects. [1] The KVM > APIC timer emulation uses hrtimer, whose unit is nanosecond. Make the > parameter configurable for conversion from the TMICT value to nanosecond. > > This patch doesn't affect the TSC deadline timer emulation. The TSC > deadline emulation path records its expiring TSC value and calculates the > expiring time in nanoseconds. The APIC timer emulation path calculates the > TSC value from the TMICT register value and uses the TSC deadline timer > path. This patch touches the APIC timer-specific code but doesn't touch > common logic. Nitpick: To be honest IMHO the paragraph about tsc deadline is redundant, because by definition (x86 spec) the tsc deadline timer doesn't use APIC bus frequency. Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky > > [1] https://lore.kernel.org/lkml/20231006011255.4163884-1-vannapurve@google.com/ > Reported-by: Vishal Annapurve > Signed-off-by: Isaku Yamahata > --- > Changes v2: > - Add check if vcpu isn't created. > - Add check if lapic chip is in-kernel emulation. > - Fix build error for i386 > - Add document to api.rst > - typo in the commit message > --- > Documentation/virt/kvm/api.rst | 14 ++++++++++++++ > arch/x86/kvm/x86.c | 35 ++++++++++++++++++++++++++++++++++ > include/uapi/linux/kvm.h | 1 + > 3 files changed, 50 insertions(+) > > diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst > index 7025b3751027..cc976df2651e 100644 > --- a/Documentation/virt/kvm/api.rst > +++ b/Documentation/virt/kvm/api.rst > @@ -7858,6 +7858,20 @@ This capability is aimed to mitigate the threat that malicious VMs can > cause CPU stuck (due to event windows don't open up) and make the CPU > unavailable to host or other VMs. > > +7.34 KVM_CAP_X86_BUS_FREQUENCY_CONTROL > +-------------------------------------- > + > +:Architectures: x86 > +:Target: VM > +:Parameters: args[0] is the value of apic bus clock frequency > +:Returns: 0 on success, -EINVAL if args[0] contains invalid value for the > + frequency, or -ENXIO if virtual local APIC isn't enabled by > + KVM_CREATE_IRQCHIP, or -EBUSY if any vcpu is created. > + > +This capability sets the APIC bus clock frequency (or core crystal clock > +frequency) for kvm to emulate APIC in the kernel. The default value is 1000000 > +(1GHz). > + > 8. Other capabilities. > ====================== > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index a9f4991b3e2e..a8fb862c4f8e 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -4625,6 +4625,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) > case KVM_CAP_ENABLE_CAP: > case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES: > case KVM_CAP_IRQFD_RESAMPLE: > + case KVM_CAP_X86_BUS_FREQUENCY_CONTROL: > r = 1; > break; > case KVM_CAP_EXIT_HYPERCALL: > @@ -6616,6 +6617,40 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, > } > mutex_unlock(&kvm->lock); > break; > + case KVM_CAP_X86_BUS_FREQUENCY_CONTROL: { > + u64 bus_frequency = cap->args[0]; > + u64 bus_cycle_ns; > + > + if (!bus_frequency) > + return -EINVAL; > + /* CPUID[0x15] only support 32bits. */ > + if (bus_frequency != (u32)bus_frequency) > + return -EINVAL; > + > + /* Cast to avoid 64bit division on 32bit platform. */ > + bus_cycle_ns = 1000000000UL / (u32)bus_frequency; > + if (!bus_cycle_ns) > + return -EINVAL; > + > + r = 0; > + mutex_lock(&kvm->lock); > + /* > + * Don't allow to change the frequency dynamically during vcpu > + * running to avoid potentially bizarre behavior. > + */ > + if (kvm->created_vcpus) > + r = -EBUSY; > + /* This is for in-kernel vAPIC emulation. */ > + else if (!irqchip_in_kernel(kvm)) > + r = -ENXIO; > + > + if (!r) { > + kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; > + kvm->arch.apic_bus_frequency = bus_frequency; > + } > + mutex_unlock(&kvm->lock); > + return r; > + } > default: > r = -EINVAL; > break; > diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h > index 211b86de35ac..d74a057df173 100644 > --- a/include/uapi/linux/kvm.h > +++ b/include/uapi/linux/kvm.h > @@ -1201,6 +1201,7 @@ struct kvm_ppc_resize_hpt { > #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228 > #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 > #define KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES 230 > +#define KVM_CAP_X86_BUS_FREQUENCY_CONTROL 231 > > #ifdef KVM_CAP_IRQ_ROUTING >