Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1339681iol; Fri, 10 Jun 2022 05:38:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4TUJljkU+lkWuDIEdoXHPXJiRyxprVl1zNzBy7pZRbKIt7AMv0rzY+dW9sE7C8HDL2i4J X-Received: by 2002:a05:6402:684:b0:431:503e:76e6 with SMTP id f4-20020a056402068400b00431503e76e6mr31761497edy.308.1654864738214; Fri, 10 Jun 2022 05:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654864738; cv=none; d=google.com; s=arc-20160816; b=KTSdsqFV+hbmHE3ci1BHlZU1Vc9VsOR1Pn6j2vstbc8GPaEmgN1/2o0aCiRvFZfRcR cgeYe6rcXLRGgVT5D1i/OWmLQaPWijO1Ao4UYjLm2BehxU/1nFrq5i/rAyj+FYPKdzZW 55lg1/NlNktpzG4NnD1GPLA+wXfGJLlEH19ywavOGBU0Ime8ptdlGCbLHQg2tsgpAmEw iCeWrguURE+6sHXpT+11wy6NRw3up4BdnZ+e3NtVeO2CW/9pwsJZJZGhM0xUs+Y6vSWs nthXY+WIPc8Psrl/vy+JYvAvnO9z9r5YOc/Bh7cQ+00Qt5l1nTTO4jORtGCctw0aoweD 85og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=JQd3cZZglBGuv+koPRhcabLewHLT00jyRGFi+QsDi+s=; b=sDoZbp3xBAecK6qhjXmh87ZQfRULdAAd8uZP/kreny8fiHj2n5ar89bbRB9Pz8lwg+ pWPgm+olOgxDYGFxIuGcapP9U2eHkQt6DKRkHwKSm5bm/mAyxVViYgn2ZxjUV9liqzuo IpO4RB3j1q5bPoez7NpeR1vAZTeeSgeUAApMTWdN4nntMny24u5T0R/ojav8k5LIRv/s c8lxkcupjJC5Z4DdXTtFNC5eTLqpp1SY5JClWzhpQTd0lmhr1x5PWNCxk11G9kVgMFH2 xT9E5jxmD+nz46Ula2fIOklKXHg+atKdEhnRAULmntF7thlmqVwSyht7c1Nem5ia0fY3 YL5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=j4p+Wn5V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn40-20020a1709070d2800b006f42491e0a0si8416595ejc.655.2022.06.10.05.38.32; Fri, 10 Jun 2022 05:38:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=j4p+Wn5V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245330AbiFJM0n (ORCPT + 99 others); Fri, 10 Jun 2022 08:26:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245428AbiFJM0l (ORCPT ); Fri, 10 Jun 2022 08:26:41 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BD4E2D2530 for ; Fri, 10 Jun 2022 05:26:39 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id r1so5416452plo.10 for ; Fri, 10 Jun 2022 05:26:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=JQd3cZZglBGuv+koPRhcabLewHLT00jyRGFi+QsDi+s=; b=j4p+Wn5VFiG1u8/FOoebn+HCQVFrBmjRKQ4DgRdq39k0j7rFQHxmSm2A6/OyAIeoLD ACC6ItbMasygaaHlolFNFGeS6r2KzwoFZpRdROqghoT7AUtSnQwtrxeSWxMAb9xn3/mC fpXb9WWCHwD7Nesl2Jve9oZroDP33S4p6jNlavWmbxzAPguGrAL5Qq93KAlHLY1QxbjX aKb10839GkMx0Fkx2t7UN5LpcwD21GWhFITvThcWMEYTpRCpqYHW81V22M8IJBp7dCES AKf9lbw5GV8KLQdW4vD4pr7JF1mbM26xNJvN1kXn3x8uF31tGs4qsqd7EQtjoVeNcYc4 H32Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=JQd3cZZglBGuv+koPRhcabLewHLT00jyRGFi+QsDi+s=; b=59uBqDwoMgc5w5zNM5wouES/cPskZf8KPAOQdfji3F3b5J/kjcTPGLnIfeSxlPIrfh c2uyI+F4nXR03S5NhUwikhrC5FoKigka98gexsPvoYSS/7W/yCDz3inj4L4Ytgob2iq4 9xCKAKGcvx7aw1xOBq/vER/12ThuQxjY2aBVx5NgYi8nipGGAEFfhyIs5jVOHq6fnoAr 6K8Ce48rpDsoHlky3vABDJAn7kFrO9PFRm+bp1UVvOu4CJYThJN3o7CXcipuEF6kCaxP 7Esp1CkiNxOgOWQGOmAftyl4E0jyitCRP/Vc4x4tqJhuWAm+dcnCAuwkgs4MuyyLG/ko VQoA== X-Gm-Message-State: AOAM532ZHhbU5ZRqz+ckcNkSi336INYRLslZAVw98QVYdauHxf5mPaPJ jYU20Xd4tdIbR8D3ArsFHJVcD6386w7I6tTQg4BGNw== X-Received: by 2002:a17:903:2303:b0:166:313f:a85f with SMTP id d3-20020a170903230300b00166313fa85fmr45260497plh.57.1654863998845; Fri, 10 Jun 2022 05:26:38 -0700 (PDT) MIME-Version: 1.0 References: <20220609110337.1238762-1-jaz@semihalf.com> <20220609110337.1238762-2-jaz@semihalf.com> In-Reply-To: From: Grzegorz Jaszczyk Date: Fri, 10 Jun 2022 14:26:27 +0200 Message-ID: Subject: Re: [PATCH 1/2] x86: notify hypervisor about guest entering s2idle state To: Sean Christopherson Cc: linux-kernel@vger.kernel.org, Dmytro Maluka , Zide Chen , Peter Fang , Tomasz Nowicki , Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" , "Rafael J. Wysocki" , Len Brown , Pavel Machek , Ashish Kalra , Mario Limonciello , Hans de Goede , Sachi King , Arnaldo Carvalho de Melo , David Dunn , Wei Wang , Nicholas Piggin , "open list:KERNEL VIRTUAL MACHINE (KVM)" , "open list:DOCUMENTATION" , "open list:ACPI" , "open list:HIBERNATION (aka Software Suspend, aka swsusp)" , dbehr@google.com, dtor@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,TVD_PH_BODY_ACCOUNTS_PRE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org czw., 9 cze 2022 o 16:55 Sean Christopherson napisa=C5= =82(a): > > On Thu, Jun 09, 2022, Grzegorz Jaszczyk wrote: > > +9. KVM_HC_SYSTEM_S2IDLE > > +------------------------ > > + > > +:Architecture: x86 > > +:Status: active > > +:Purpose: Notify the hypervisor that the guest is entering s2idle stat= e. > > What about exiting s2idle? E.g. > > 1. VM0 enters s2idle > 2. host notes that VM0 is in s2idle > 3. VM0 exits s2idle > 4. host still thinks VM0 is in s2idle > 5. VM1 enters s2idle > 6. host thinks all VMs are in s2idle, suspends the system I think that this problem couldn't be solved by adding notification about exiting s2idle. Please consider (even after simplifying your example to one VM): 1. VM0 enters s2idle 2. host notes about VM0 is in s2idle 3. host continues with system suspension but in the meantime VM0 exits s2idle and sends notification but it is already too late (VM could not even send notification on time). Above could be actually prevented if the VMM had control over the guest resumption. E.g. after VMM receives notification about guest entering s2idle state, it would park the vCPU actually preventing it from exiting s2idle without VMM intervention. > > > +static void s2idle_hypervisor_notify(void) > > +{ > > + if (static_cpu_has(X86_FEATURE_HYPERVISOR)) > > + kvm_hypercall0(KVM_HC_SYSTEM_S2IDLE); > > Checking the HYPERVISOR flag is not remotely sufficient. The hypervisor = may not > be KVM, and if it is KVM, it may be an older version of KVM that doesn't = support > the hypercall. The latter scenario won't be fatal unless KVM has been mo= dified, > but blindly doing a hypercall for a different hypervisor could have disas= trous > results, e.g. the registers ABIs are different, so the above will make a = random > request depending on what is in other GPRs. Good point: we've actually thought about not confusing/breaking VMMs so I've introduced KVM_CAP_X86_SYSTEM_S2IDLE VM capability in the second patch, but not breaking different hypervisors is another story. Would hiding it under new 's2idle_notify_kvm' module parameter work for upstream?: +static bool s2idle_notify_kvm __read_mostly; +module_param(s2idle_notify_kvm, bool, 0644); +MODULE_PARM_DESC(s2idle_notify_kvm, "Notify hypervisor about guest entering s2idle state"); + .. +static void s2idle_hypervisor_notify(void) +{ + if (static_cpu_has(X86_FEATURE_HYPERVISOR) && s2idle_notify_kvm) + kvm_hypercall0(KVM_HC_SYSTEM_S2IDLE); +} + > > The bigger question is, why is KVM involved at all? KVM is just a dumb p= ipe out > to userspace, and not a very good one at that. There are multiple well e= stablished > ways to communicate with the VMM without custom hypercalls. Could you please kindly advise about the recommended way of communication with VMM, taking into account that we want to send this notification just before entering s2idle state (please see also answer to next comment), which is at a very late stage of the suspend process with a lot of functionality already suspended? > > > I bet if you're clever this can even be done without any guest changes, e= .g. I > gotta imagine acpi_sleep_run_lps0_dsm() triggers MMIO/PIO with the right = ACPI > configuration. The problem is that between acpi_sleep_run_lps0_dsm and the place where we introduced hypercall there are several places where we can actually cancel and not enter the suspend state. So trapping on acpi_sleep_run_lps0_dsm which triggers MMIO/PIO would be premature. The other reason for doing it in this place is the fact that s2idle_enter is called from an infinite loop inside s2idle_loop, which could be interrupted by e.g. ACPI EC GPE (not aim for waking-up the system) so s2idle_ops->wake() would return false and s2idle_enter will be triggered again. In this case we would want to get notification about guests actually entering s2idle state again, which wouldn't be possible if we would rely on acpi_sleep_run_lps0_dsm. Best regards, Grzegorz