Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp213626rdh; Sat, 23 Sep 2023 07:31:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWcMKUXaT7IgQbGx+UCiEhJOB2GRL4P/gD7TM+SX5vwFFoSYxXEzhcjPX8V8u3EwXpswIU X-Received: by 2002:a05:6a00:2e8b:b0:68f:f741:57a1 with SMTP id fd11-20020a056a002e8b00b0068ff74157a1mr3666170pfb.7.1695479475080; Sat, 23 Sep 2023 07:31:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695479475; cv=none; d=google.com; s=arc-20160816; b=YNetPxgySiCQ4UljueW6oKmApV6e4RWFKQNd7do6mYMUxtDpAbiOIErbc5JC+5NCvA CMhiz7ZVyhBjPNu21Y4c4Z9muZQ2DrtqSkd5Lci+Z/fipuoq66CxuzdNL5h/dZGwByGH CAl6CkjCZSjBKl3VtSMqZX/01WiKpF6KZlEzXi8JY9qdB/yIyY2tI7qNz4kkcFPT8zlI GFIc2nso+FE48r1P/+dPUeslPFED/6PMfjP9wCt5tKRiasIgusacPPJR10carq3W0K3i DY/celER6q8GPa4LPL4sGUzP3pEcBkgWYAbmI1emKalKhft1VYgEXqBgmg1m/3GosWGz KjQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=nXH/z9I2U5yZm4eO+7VtM4L0zfP0q4sZwQaS4eYQojo=; fh=TtjmOBnGh+rl216a7HA32xMpT1XbeZcLcfurMxwpIaU=; b=I3gaJTqFQ+EQ8OYBnfzjAU1zKCA2l8ZQJ2coQVCTGBdDd1A59yzUE0zc++i8YiWxWt Bg77TFbcu7RacTZGTN9jVrtYz+FFpthcSUf6Sjye+V7zyqHDPA+hOFKMP91DqdyB8Z8Q 2KX9lmRCqKZ77KxCBprtMxorxZdI1VdkgGDewD1zwdxGvKLjtPhxPEBbQ/asJGuOd3We 7WXYdJ0pCxFWmHeprGNZTcwyNDyTp0hir9QiwyxFnHiVi8CtPpFBoxKAG+uzmn6uhVvY lwqJSOaba/kXEQN/tqhhvLR09+L8Al1sQ95fZdvX79CcNb2rWupSykfTXgyIBCwXduWu 6JbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AXXh9R0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id y35-20020a056a00182300b00690f191430csi6260399pfa.56.2023.09.23.07.31.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 07:31:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AXXh9R0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 1A6BF8359B44; Sat, 23 Sep 2023 02:26:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231129AbjIWJZn (ORCPT + 99 others); Sat, 23 Sep 2023 05:25:43 -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-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 X-Spam-Status: No, score=1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Sat, 23 Sep 2023 02:26:04 -0700 (PDT) X-Spam-Level: * 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