Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2950964ybz; Sun, 19 Apr 2020 13:48:03 -0700 (PDT) X-Google-Smtp-Source: APiQypKHnKD4v2DtwcpGtbuCOahfR4QI3ol2nxhOvwej1CmAxZVHbx+lh1ZhUhz6Lzu5vO5C0gLD X-Received: by 2002:a50:f298:: with SMTP id f24mr12174714edm.199.1587329283185; Sun, 19 Apr 2020 13:48:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587329283; cv=none; d=google.com; s=arc-20160816; b=bmHdoqgIyg3cTt82btRB+RLywCMVnewL+h1Gh+w2NUCcZ1T5n9iFJMO/6vJNVpyxaO +RUeWQSEFtBqBEN/C/AEKDoDIeVpbNbHR1U/5zKnkpW1aToNL4iS6Ld3zppLvj9pjArp fxqNjRE1ypYp8l4ccKmPygVEkrPyWow8Oc0ZLUMPSLP59JfO0p2Wb4F03zWbLpq+xPIo dIPrz3SUy64malTuINsk91IR+RAOSu1AwXxxq4VVY2EaIYA6JUheIJ0caV9/JLiZZqwd dBPRqQfMkCIC7q6LGLC4v3PaOV34jAh/OBbmUDPLhSLjLFqxwvzh9O+6hzFgnQGUsQVD SQow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=QYLscaipbpGaDqlHvBu8FlIWcjCnc2WaOfL03yyepS0=; b=pNbbJmOXuqRI+eApGMO1bxLP/uExksfX+Xe6TNJmsYEkx+hm51G9Wi7SuyfDLpOpFp MV2z3kY4MIj+91HXZ2gREQdtXV15wBxCfSd+ZegpMTJYbK/nEx6ZwY6mbAbRDU31eaq9 GzjVqWjdTGucO97SXE7tpFIoiNRGCeXozYWOkKPn6HkpPVt5GkAVvbN1MIbM2zUVGBYQ u4GPTPsMqEmTwswbuua5BUAcEwMtwQ4FcQK0ABjDW9OE4TxE9AK3loTERzKvF/MyOyoJ 0xTKtzenPurCP5KrIXECqFvwfmE7UXRgo/2hJOhu0eawXPbLORCceTCdOmYjEcS4oJaI BEOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dZV03zoI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si19771195edk.571.2020.04.19.13.47.38; Sun, 19 Apr 2020 13:48:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dZV03zoI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725988AbgDSUq3 (ORCPT + 99 others); Sun, 19 Apr 2020 16:46:29 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:53730 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725848AbgDSUq2 (ORCPT ); Sun, 19 Apr 2020 16:46:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587329186; 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: in-reply-to:in-reply-to:references:references; bh=QYLscaipbpGaDqlHvBu8FlIWcjCnc2WaOfL03yyepS0=; b=dZV03zoIsEp576owEp02sFHHw1HWCb462ycusxH6gTuA4Jovpnilr1LZXtA14bgiGCPmHz dhqXUj3iqFfMNoeH3NvDlf2gYn1bqlEXVKb9aHPyGNGNFLpvhHq6kQKJ3+QgOCBTVXHusA IBzfpw0zPTUjFQonryOIKF7yXsljnoA= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-433-YKeyPkA9N7mjvPSUFqj5gQ-1; Sun, 19 Apr 2020 16:46:24 -0400 X-MC-Unique: YKeyPkA9N7mjvPSUFqj5gQ-1 Received: by mail-wm1-f69.google.com with SMTP id 14so2774824wmo.9 for ; Sun, 19 Apr 2020 13:46:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=QYLscaipbpGaDqlHvBu8FlIWcjCnc2WaOfL03yyepS0=; b=T5bQsxDegs1xlqUX11tQ1HACsTMLZ6fAFH3al7YjsC0PmHcmFRiduvozejoX4wukdk toNVbFBhdt3GdZDm+OC2zk9AQSO1LL8J9yEbsRW8+TsHOYD4aXp1jfaCVsZuaJCESga2 rLkxLhP4btyhY0pMh+IbDKc1OZJ7a2G/ucT5fnAiurFdzkoBZzcjdgg0Utle1zrjwCHo gcdhxzhWh1opDv1L1wx+ilQ343uXfltYgyb8yj9tTeOMm4G5uwFdio4SHMRbjXIq3maV uUDZzwx1UQ0xDh4nT46EP5xaHRjmaVRdnWkelwCNjsGsi/BczkfXIwJh/gQ+TxXJsgAr M0KA== X-Gm-Message-State: AGi0PuarmwNb5Rib7yHoqzirqHgbW3uuj6qUCkcVSpKz/DT/81lnczyi LkT2uBbzYoYkbFORVgg+jcAAzJOAUpzhpl8o7hgXD2cVuEI8Dqs4TjNkRX9yGEi5yj07r2Iv5AB hsIEDiSmxa5znF+GiqqSyhK1P X-Received: by 2002:a5d:428a:: with SMTP id k10mr15170070wrq.59.1587329182440; Sun, 19 Apr 2020 13:46:22 -0700 (PDT) X-Received: by 2002:a5d:428a:: with SMTP id k10mr15170053wrq.59.1587329182151; Sun, 19 Apr 2020 13:46:22 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id n2sm20689701wrq.74.2020.04.19.13.46.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 13:46:21 -0700 (PDT) From: Vitaly Kuznetsov To: Jon Cargille Cc: David Matlack , Jon Cargille , Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] kvm: add capability for halt polling In-Reply-To: <20200417221446.108733-1-jcargill@google.com> References: <20200417221446.108733-1-jcargill@google.com> Date: Sun, 19 Apr 2020 22:46:20 +0200 Message-ID: <87d083td9f.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jon Cargille writes: > From: David Matlack > > KVM_CAP_HALT_POLL is a per-VM capability that lets userspace > control the halt-polling time, allowing halt-polling to be tuned or > disabled on particular VMs. > > With dynamic halt-polling, a VM's VCPUs can poll from anywhere from > [0, halt_poll_ns] on each halt. KVM_CAP_HALT_POLL sets the > upper limit on the poll time. Out of pure curiosity, why is this a per-VM and not a per-VCPU property? > > Signed-off-by: David Matlack > Signed-off-by: Jon Cargille > Reviewed-by: Jim Mattson > --- > Documentation/virt/kvm/api.rst | 17 +++++++++++++++++ > include/linux/kvm_host.h | 1 + > include/uapi/linux/kvm.h | 1 + > virt/kvm/kvm_main.c | 19 +++++++++++++++---- > 4 files changed, 34 insertions(+), 4 deletions(-) > > diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst > index efbbe570aa9b7b..d871dacb984e98 100644 > --- a/Documentation/virt/kvm/api.rst > +++ b/Documentation/virt/kvm/api.rst > @@ -5802,6 +5802,23 @@ If present, this capability can be enabled for a VM, meaning that KVM > will allow the transition to secure guest mode. Otherwise KVM will > veto the transition. > > +7.20 KVM_CAP_HALT_POLL > +---------------------- > + > +:Architectures: all > +:Target: VM > +:Parameters: args[0] is the maximum poll time in nanoseconds > +:Returns: 0 on success; -1 on error > + > +This capability overrides the kvm module parameter halt_poll_ns for the > +target VM. > + > +VCPU polling allows a VCPU to poll for wakeup events instead of immediately > +scheduling during guest halts. The maximum time a VCPU can spend polling is > +controlled by the kvm module parameter halt_poll_ns. This capability allows > +the maximum halt time to specified on a per-VM basis, effectively overriding > +the module parameter for the target VM. > + > 8. Other capabilities. > ====================== > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 6d58beb65454f7..922b24ce5e7297 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -503,6 +503,7 @@ struct kvm { > struct srcu_struct srcu; > struct srcu_struct irq_srcu; > pid_t userspace_pid; > + unsigned int max_halt_poll_ns; > }; > > #define kvm_err(fmt, ...) \ > diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h > index 428c7dde6b4b37..ac9eba0289d1b6 100644 > --- a/include/uapi/linux/kvm.h > +++ b/include/uapi/linux/kvm.h > @@ -1017,6 +1017,7 @@ struct kvm_ppc_resize_hpt { > #define KVM_CAP_S390_VCPU_RESETS 179 > #define KVM_CAP_S390_PROTECTED 180 > #define KVM_CAP_PPC_SECURE_GUEST 181 > +#define KVM_CAP_HALT_POLL 182 > > #ifdef KVM_CAP_IRQ_ROUTING > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 74bdb7bf32952e..ec038a9e60a275 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -710,6 +710,8 @@ static struct kvm *kvm_create_vm(unsigned long type) > goto out_err_no_arch_destroy_vm; > } > > + kvm->max_halt_poll_ns = halt_poll_ns; > + > r = kvm_arch_init_vm(kvm, type); > if (r) > goto out_err_no_arch_destroy_vm; > @@ -2716,15 +2718,16 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) > if (!kvm_arch_no_poll(vcpu)) { > if (!vcpu_valid_wakeup(vcpu)) { > shrink_halt_poll_ns(vcpu); > - } else if (halt_poll_ns) { > + } else if (vcpu->kvm->max_halt_poll_ns) { > if (block_ns <= vcpu->halt_poll_ns) > ; > /* we had a long block, shrink polling */ > - else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns) > + else if (vcpu->halt_poll_ns && > + block_ns > vcpu->kvm->max_halt_poll_ns) > shrink_halt_poll_ns(vcpu); > /* we had a short halt and our poll time is too small */ > - else if (vcpu->halt_poll_ns < halt_poll_ns && > - block_ns < halt_poll_ns) > + else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns && > + block_ns < vcpu->kvm->max_halt_poll_ns) > grow_halt_poll_ns(vcpu); > } else { > vcpu->halt_poll_ns = 0; > @@ -3516,6 +3519,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) > case KVM_CAP_IOEVENTFD_ANY_LENGTH: > case KVM_CAP_CHECK_EXTENSION_VM: > case KVM_CAP_ENABLE_CAP_VM: > + case KVM_CAP_HALT_POLL: > return 1; > #ifdef CONFIG_KVM_MMIO > case KVM_CAP_COALESCED_MMIO: > @@ -3566,6 +3570,13 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, > return 0; > } > #endif > + case KVM_CAP_HALT_POLL: { > + if (cap->flags || cap->args[0] != (unsigned int)cap->args[0]) > + return -EINVAL; > + > + kvm->max_halt_poll_ns = cap->args[0]; Is it safe to allow any value from userspace here or would it maybe make sense to only allow [0, global halt_poll_ns]? > + return 0; > + } > default: > return kvm_vm_ioctl_enable_cap(kvm, cap); > } -- Vitaly