Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2259487rwb; Wed, 30 Nov 2022 04:37:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf5i3f+d60gEKOowBBaK1gAv3NoaXJ2/0GteOv0na1wgB8o1amRMPr80ybGLoviA0n+B2qyY X-Received: by 2002:a63:5a10:0:b0:476:c39b:bd67 with SMTP id o16-20020a635a10000000b00476c39bbd67mr41996331pgb.46.1669811860876; Wed, 30 Nov 2022 04:37:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669811860; cv=none; d=google.com; s=arc-20160816; b=qQagnUc8qXo6zD2gefyzDUWjmNRQXblSOvjXEy/r12vopXqe/i3t4vxh92Fa0Qr1pn KoCR+8gP8mtWIlOy2lWluoI0WHVuAShIGG/aQELiPawrVr2cVeZueHrxmEOIxPXSkHZc ToYDJ56Lb4W5QPT1EckQapyfy0ULgnRy7vAR8dzg7cQl1hfN47LA6Dtk03rxx2cK9Qby 1iGFawGqbXilFFZXC2iHDXaG9cEHzU6YOtxRybBLjOAcSGvOjFiWIYbjWuOP2NdZP59N 1ejHee7PTP9iM5zFR0IjVDwC+AiOwKcp14ZeNgTlrfN8x6lenzgSar4yR/2ytMOuy2Vq lPTg== 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=aDUZsQ4ebsI3B74ltAql+qgtgRR7G69xLxugWM4TCSc=; b=uLVGxTR6/ufoBCeeniSOhpfpnmsA8d1NwaejlPwJHApKe0/Kcw1uheIjxEjziDlkBN oJ2AAw37oViTxQdMLlvh+ITjBtf6qc8L+f8Ram7pk+Nrpbal7t/NmcPchfAwiUzxyys8 tUt4Pb5OflNPmCwsORyOnguP/4h4aDNLXuoz3HfxsomuWrHR1ePKL2r5+2XvLL2C+DoY g2f0n2d59EaYCEW5ZnV/1oKRDWY73B/wlhhE/jPY0hHqphfN/fXy2INh/jxkgiSqRiol tdoY5YBXuDxYwA4p7+JmRHS7obJHduCl9FYcpPizqKEYbMc3KzffBVc9D/u4h91qyo8R 9Dog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=Zz8W2z9v; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=semihalf.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x64-20020a638643000000b00477ba9ec461si1115481pgd.879.2022.11.30.04.37.29; Wed, 30 Nov 2022 04:37:40 -0800 (PST) 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=Zz8W2z9v; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=semihalf.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230367AbiK3MZ0 (ORCPT + 84 others); Wed, 30 Nov 2022 07:25:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231968AbiK3MZY (ORCPT ); Wed, 30 Nov 2022 07:25:24 -0500 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87C4673BBE for ; Wed, 30 Nov 2022 04:25:22 -0800 (PST) Received: by mail-yb1-xb30.google.com with SMTP id 136so2272029ybl.4 for ; Wed, 30 Nov 2022 04:25:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf.com; s=google; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aDUZsQ4ebsI3B74ltAql+qgtgRR7G69xLxugWM4TCSc=; b=Zz8W2z9vEL93OLfsHNnUXDZS/TbxmIuKGQr91bg/GIfUoCS/uDdK8dJyqeDTgQImIp dn1mS7laR7RUdKdoshyPXeDyeq4h3fM4OQNpBh27IG8d6QZ7+jdzcWG11Ryznwx8X0mP CgHxAGEorZEBnpaWLsjLjQr9+gfeVWcJ1Ewg13Rsbzdd1jyw0wp23kupb5lvpN17zxqL 9uB5SRZdyDnLBoGSGffrXXi8DQZYaGdGE0nJQZlAjJy7pYw9/C9EOu2TH8fZTzYKsgqv b9nB6PETo6wi8y0WJY+TbFEQHUfl/f3D1bu5+8eptsmcMwNKqV/tf90YtX3HwDI1ikgZ UMJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aDUZsQ4ebsI3B74ltAql+qgtgRR7G69xLxugWM4TCSc=; b=o3v3j1VgacjXPTukW3VK0G7mlVWaTlhG0DSe62Wxv/SpxQgoeUbnHJqM1dCcn4ccih gB2zzYv0k65eY7kiG0EmFJR0TVJ7pVMFRofIwqxXJCtPZji7Hmj3dCmAz7attx6lnd0F fEOiIBVWrj+aPuMfKwj1E3Lt5kNh62VHDAFnZIxDaxTiviotV2BUWiq/pQr2oaNPYUHx +lDcy8hxCXynyW/dpG291IvKvbaNjk7sxz7EQnb3YxRhQnDhpRGbQSFwU227Y4FhDfjk oKV7ujM6ROoCtt+7+Y9WLIoPwboEIH0VU7KFLklHJF5hCB7i3tFwaItTyJImppokuFT6 eAMg== X-Gm-Message-State: ANoB5plvEySbzkSmWFwQmxmgLnSMNd+ftTSxsWHBaMElfF1fBO8+pGvT 3/0lf6PdmhdcqEiggxlk3yndE479PoU8BRs+3RReow== X-Received: by 2002:a05:6902:4d3:b0:6ca:10da:acb7 with SMTP id v19-20020a05690204d300b006ca10daacb7mr43561380ybs.475.1669811121633; Wed, 30 Nov 2022 04:25:21 -0800 (PST) MIME-Version: 1.0 References: <20220707125329.378277-1-jaz@semihalf.com> <20220707125329.378277-2-jaz@semihalf.com> In-Reply-To: From: Grzegorz Jaszczyk Date: Wed, 30 Nov 2022 13:25:10 +0100 Message-ID: Subject: Re: [RFC PATCH 1/2] suspend: extend S2Idle ops by new notify handler To: "Rafael J. Wysocki" Cc: Linux Kernel Mailing List , Dmytro Maluka , Mario Limonciello , Sean Christopherson , Dominik Behr , upstream@semihalf.com, Zide Chen , Len Brown , Hans de Goede , Mark Gross , Pavel Machek , Mika Westerberg , Sachi King , "open list:ACPI" , "open list:X86 PLATFORM DRIVERS" , "open list:HIBERNATION (aka Software Suspend, aka swsusp)" 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 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 Hi Rafael, Kindly reminder about this topic. BTW I've noticed that in the meantime v. similar patch was merged but aimed for debugging purposes [1] (it uses s/notify/check and invokes the callback a bit earlier just before s2idle_entry). Perhaps combining Mario's [1] with aligned to it patch #2 of this series [2] could be used and accepted as s2idle notifications method? [1] https://patchwork.kernel.org/project/linux-pm/patch/20220829162953.5947= -2-mario.limonciello@amd.com [2] https://patchwork.kernel.org/project/linux-pm/patch/20220707125329.3782= 77-3-jaz@semihalf.com/ Best regards, Grzegorz pon., 12 wrz 2022 o 16:44 Grzegorz Jaszczyk napisa=C5=82= (a): > > Hi Rafael, > > Gentle ping > > Best regards, > Grzegorz > > pon., 22 sie 2022 o 11:26 Grzegorz Jaszczyk napisa=C5= =82(a): > > > > Hi Rafael, > > > > Could you please kindly comment on the above? > > > > Thank you in advance, > > Grzegorz > > > > =C5=9Br., 20 lip 2022 o 15:15 Grzegorz Jaszczyk napi= sa=C5=82(a): > > > > > > wt., 19 lip 2022 o 20:09 Rafael J. Wysocki napisa= =C5=82(a): > > > > > > > > On Thu, Jul 7, 2022 at 2:56 PM Grzegorz Jaszczyk = wrote: > > > > > > > > > > Currently the LPS0 prepare_late callback is aimed to run as the v= ery > > > > > last thing before entering the S2Idle state from LPS0 perspective= , > > > > > nevertheless between this call and the system actually entering t= he > > > > > S2Idle state there are several places where the suspension proces= s could > > > > > be canceled. > > > > > > > > And why is this a problem? > > > > > > > > The cancellation will occur only if there is a wakeup signal that > > > > would otherwise cause one of the CPUs to exit the idle state. Such= a > > > > wakeup signal can appear after calling the new notifier as well, so > > > > why does it make a difference? > > > > > > It could also occur due to suspend_test. Additionally with new > > > notifier we could get notification when the system wakes up from > > > s2idle_loop and immediately goes to sleep again (due to e.g. > > > acpi_s2idle_wake condition not being met) - in this case relying on > > > prepare_late callback is not possible since it is not called in this > > > path. > > > > > > > > > > > > In order to notify VMM about guest entering suspend, extend the S= 2Idle > > > > > ops by new notify callback, which will be really invoked as a ver= y last > > > > > thing before guest actually enters S2Idle state. > > > > > > > > It is not guaranteed that "suspend" (defined as all CPUs entering i= dle > > > > states) will be actually entered even after this "last step". > > > > > > Since this whole patchset is aimed at notifying the host about a gues= t > > > entering s2idle state, reaching this step can be considered as a > > > suspend "entry point" for VM IMO. It is because we are talking about > > > the vCPU not the real CPU. Therefore it seems to me, that even if som= e > > > other vCPUs could still get some wakeup signal they will not be able > > > to kick (through s2idle_wake->swake_up_one(&s2idle_wait_head);) the > > > original vCPU which entered s2idle_loop, triggered the new notifier > > > and is halted due to handling vCPU exit (and was about to trigger > > > swait_event_exclusive). So it will prevent the VM's resume process > > > from being started. > > > > > > > > > > > > Additionally extend the acpi_s2idle_dev_ops by notify() callback = so > > > > > any driver can hook into it and allow to implement its own notifi= cation. > > > > > > > > > > Taking advantage of e.g. existing acpi_s2idle_dev_ops's prepare/r= estore > > > > > hooks is not an option since it will not allow to prevent race > > > > > conditions: > > > > > - VM0 enters s2idle > > > > > - host notes about VM0 is in s2idle > > > > > - host continues with system suspension but in the meantime VM0 e= xits > > > > > s2idle and sends notification but it is already too late (VM coul= d not > > > > > even send notification on time). > > > > > > > > Too late for what? > > > > > > Too late to cancel the host suspend process, which thinks that the VM > > > is in s2idle state while it isn't. > > > > > > > > > > > > Introducing notify() as a very last step before the system enters= S2Idle > > > > > together with an assumption that the VMM has control over guest > > > > > resumption allows preventing mentioned races. > > > > > > > > How does it do that? > > > > > > At the moment when VM triggers this new notifier we trap on MMIO > > > access and the VMM handles vCPU exit (so the vCPU is "halted"). > > > Therefore the VMM could control when it finishes such handling and > > > releases the vCPU again. > > > > > > Maybe adding some more context will be helpful. This patchset was > > > aimed for two different scenarios actually: > > > 1) Host is about to enter the suspend state and needs first to suspen= d > > > VM with all pass-through devices. In this case the host waits for > > > s2idle notification from the guest and when it receives it, it > > > continues with its own suspend process. > > > 2) Guest could be a "privileged" one (in terms of VMM) and when the > > > guest enters s2idle state it notifies the host, which in turn trigger= s > > > the suspend process of the host. > > > > > > > > > > > It looks like you want suspend-to-idle to behave like S3 and it won= 't. > > > > > > In a way, yes, we compensate for the lack of something like PM1_CNT t= o > > > trap on for detecting that the guest is suspending. > > > We could instead force the guest to use S3 but IMO it is undesirable, > > > since it generally does make a difference which suspend mode is used > > > in the guest, s2idle or S3, e.g some drivers check which suspend type > > > is used and based on that behaves differently during suspend. One of > > > the example is: > > > https://elixir.bootlin.com/linux/v5.18.12/source/drivers/gpu/drm/amd/= amdgpu/amdgpu_drv.c#L2323 > > > https://elixir.bootlin.com/linux/v5.18.12/source/drivers/gpu/drm/amd/= amdgpu/amdgpu_acpi.c#L1069 > > > https://elixir.bootlin.com/linux/v5.18.12/source/drivers/gpu/drm/amd/= amdgpu/amdgpu_gfx.c#L583 > > > > > > Thank you, > > > Grzegorz