From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6855DCE7A8B for ; Sat, 23 Sep 2023 09:25:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231162AbjIWJZo (ORCPT ); Sat, 23 Sep 2023 05:25:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230497AbjIWJZl (ORCPT ); Sat, 23 Sep 2023 05:25:41 -0400 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 3B6FB136 for ; Sat, 23 Sep 2023 02:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695461091; 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=nXH/z9I2U5yZm4eO+7VtM4L0zfP0q4sZwQaS4eYQojo=; b=AXXh9R0UxfoXSeyHJpkfsycYtevp6pYuTi+aKpo8ANfZUU3RnRz456L7eePTdbFAz89MZb bpr9g1OMg9oFwTidOSKchYCpVLHWodKoikTHaFjkOmocOspNZcc+LL8OeICQgdp58sIKrm RzpwXzqqmvVjY4MiCWchtC3lxc/RuTw= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-93-L7yxJSn_NJ2KNsLNJtHjKw-1; Sat, 23 Sep 2023 05:24:48 -0400 X-MC-Unique: L7yxJSn_NJ2KNsLNJtHjKw-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-40469bcd1eeso27092415e9.2 for ; Sat, 23 Sep 2023 02:24:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695461087; x=1696065887; h=content-transfer-encoding:in-reply-to:subject:from:references:cc:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nXH/z9I2U5yZm4eO+7VtM4L0zfP0q4sZwQaS4eYQojo=; b=njyRsHNgsoKY+17Vo2p/rrY0Wd5jSkMd++9ZAamd/lgw6JVKAv1bKYQLrjteGNMRa0 sE2t0hXivyoXFcUk0lA/tuzFmkZOprVfYTSHdtEeNnLcsa3Ckgw4ENHgI5hIFeIwF+Nf XQJqAliqpXH5g8LvFjfqVoNbpwK8gRmpfi2yJFrrVGzaFMEeyzejFiU3NU+1CrX6w7oB urll/VCI/PHEtcR5mzEWUAGMaAjlmnF8ZpqoKTf18TKusXaNwzmb/7uG/P0PbD3J2f3l CeK04Od3rzMSuMCy5Lw3wDIevyeWq35Ayr3XPwfsH0nZWKnzmmVopjQ6PBa5I8gGXmk+ ETmA== X-Gm-Message-State: AOJu0YwxeBxkG0+AloSVd65S9XgBbVdwgGuQkzgmcKHcNQT+xgz0HPka IWN0q3Hb/meLJSk6Ww+m5p1qFtVuGuz6+MrDHSWvV4I/lVQvyQ7aLkd8h17d+/ijogDLZtSbV6y mdGfIOdvfyzBZruc4kK4CRSmd X-Received: by 2002:a7b:ce10:0:b0:3ff:28b8:c53d with SMTP id m16-20020a7bce10000000b003ff28b8c53dmr1324590wmc.33.1695461087303; Sat, 23 Sep 2023 02:24:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzWAQVkjvFxUa3O8HwBx+l+WmSWzKfVnpxt/9gjJpYJRF2Cxuv42ygz201/3oojgDSFFucSA== X-Received: by 2002:a7b:ce10:0:b0:3ff:28b8:c53d with SMTP id m16-20020a7bce10000000b003ff28b8c53dmr1324570wmc.33.1695461086985; Sat, 23 Sep 2023 02:24:46 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e? ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.googlemail.com with ESMTPSA id n11-20020adffe0b000000b0031c3ee933b5sm6445672wrr.108.2023.09.23.02.24.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 23 Sep 2023 02:24:46 -0700 (PDT) Message-ID: <3dc66987-49c7-abda-eb70-1898181ef3fe@redhat.com> Date: Sat, 23 Sep 2023 11:24:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Content-Language: en-US To: David Woodhouse Cc: kvm@vger.kernel.org, Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, graf@amazon.de, Nicolas Saenz Julienne , "Griffoul, Fred" References: <1b52b557beb6606007f7ec5672eab0adf1606a34.camel@infradead.org> From: Paolo Bonzini Subject: Re: [RFC] KVM: x86: Allow userspace exit on HLT and MWAIT, else yield on MWAIT In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/23/23 09:22, David Woodhouse wrote: > On Fri, 2023-09-22 at 14:00 +0200, Paolo Bonzini wrote: >> To avoid races you need two flags though; there needs to be also a >> kernel->userspace communication of whether the vCPU is currently in >> HLT or MWAIT, using the "flags" field for example. If it was HLT only, >> moving the mp_state in kvm_run would seem like a good idea; but not if >> MWAIT or PAUSE are also included. > > Right. When work is added to an empty workqueue, the VMM will want to > hunt for a vCPU which is currently idle and then signal it to exit. > > As you say, for HLT it's simple enough to look at the mp_state, and we > can move that into kvm_run so it doesn't need an ioctl... Looking at it again: not so easy because the mpstate is changed in the vCPU thread by vcpu_block() itself. > although it > would also be nice to get an *event* on an eventfd when the vCPU > becomes runnable (as noted, we want that for VSM anyway). Or perhaps > even to be able to poll() on the vCPU fd. Why do you need it? You can just use KVM_RUN to go to sleep, and if you get another job you kick out the vCPU with pthread_kill. (I also didn't get the VSM reference). An interesting quirk is that kvm_run->immediate_exit is processed before kvm_vcpu_block(), but TIF_SIGPENDING is processed afterwards. This means that you can force an mpstate update with pthread_kill + KVM_RUN. It's not going to be a speed demon, but it's worth writing a selftest for it. > But MWAIT (as currently not-really-emulated) and PAUSE are both just > transient states with nothing you can really *wait* for, which is why > they're such fun to deal with. PAUSE is easier because it is just momentary and you stick it inside what's already a busy wait. MWAIT is less fun because you don't really want to busy wait. Paolo