From patchwork Wed Oct 20 12:04:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hikaru Nishida X-Patchwork-Id: 12572217 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0CCCC433EF for ; Wed, 20 Oct 2021 12:07:20 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BEB6160EE2 for ; Wed, 20 Oct 2021 12:07:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BEB6160EE2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7/rINw5YnBkPILCEiS1zjYuceo/gF+Fe/DPGB5I9t0g=; b=2ULpBfG16OjXYE mjWnqwRTu6J3jJc3HgITEh87ywQ4GjgAzA1YncUn0x+uOhv7pjeZeUnnTCAfFnN7QDrUwLHU/ALEc js+JJK6FPUkCeySuOyHm2Yn4CLD8bUqonl4DvuxBuuZTGlonp5IPrXOGGTTcav7WqJBtIv4IjCPwq YMf8i309uhYOYtOrDZ2MXx9hl6ruyZxzj13xJmWpT3ZzxTMvM6xAnexmox9EsUInyL5b319KGOPgN yWH19AEEpTu9TV8sLT1pQkipkvGpWZJvYJegkscYF8qsUxBrZhKwg5eBNuvUdm9Y86Ch4LmmEdK8+ xeQDRPItrPC80z9DrdXg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdALr-004PyU-9e; Wed, 20 Oct 2021 12:05:47 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdAL3-004PcZ-QG for linux-arm-kernel@lists.infradead.org; Wed, 20 Oct 2021 12:04:59 +0000 Received: by mail-pf1-x42b.google.com with SMTP id k26so2783479pfi.5 for ; Wed, 20 Oct 2021 05:04:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pU7j7Xk9+/byWORMoTjuh+PtfIleRNtFNR9pisRaOIs=; b=V++5qWbqy5428E4+L8MN0z76u0WFToo1W/QPmRiz3Y2mC128yoEwYEbB+FWVhgenuB n+q6iWsUMxoBq63k7TZlDaAFsLpNQMhhMZKNtAvJHVTqVAJ9VUojVmNE8vYiYx8R+ufz cVJPMZ5WhVbo7SnHuHoClSMPP6oZyFSv/o/vg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pU7j7Xk9+/byWORMoTjuh+PtfIleRNtFNR9pisRaOIs=; b=JxxqVjl3Q0OA4gwlFpsKUkHnSQHhhHaApTchAWhiFyFzXInX4eR27x4+l/0zXYIbW3 40kVC4fclwFEliXGlstbOW9iaSO+sSD4ZtFsdAH5ExoDTtkQsYZN8TZxtsr31l2Erz7u rvPOxNlZ6PQFe0fQnn72FD3XCTGD6SVe+TN2CHj7v7OD1lCujEVdknn5Wo32yJW1nOuZ J+Qzj72HJGxluxPjaeFUytnOW2oAQYPE4t/gPyWstMcBSkbQGkWep/jvZjRvns1P8u17 VK7D1YOdPOJerrRqaoWmVZiiIJu1KUCBEtvFAul4fI5Akqox+ndGCCCBkuAYG3n8p54j FrfQ== X-Gm-Message-State: AOAM533w9qvLOm52bUyuiKreRus4pVC0bYdeiKPgQBpsMgcDFeco3iPn eL7azeHjoHg8tFr5S/McufsAYg== X-Google-Smtp-Source: ABdhPJxaf4wQew/0fxehgf4we7eoDadSQ+ziLebkvEYdeY0bf6NB9PnUwEUIMDvlNw3FihCdjsAaDA== X-Received: by 2002:a63:710d:: with SMTP id m13mr33036191pgc.467.1634731496586; Wed, 20 Oct 2021 05:04:56 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:e516:d575:e6f:a526]) by smtp.gmail.com with UTF8SMTPSA id i12sm2108246pgd.56.2021.10.20.05.04.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Oct 2021 05:04:56 -0700 (PDT) From: Hikaru Nishida To: linux-kernel@vger.kernel.org, dme@dme.org, tglx@linutronix.de, mlevitsk@redhat.com, linux@roeck-us.net, pbonzini@redhat.com, vkuznets@redhat.com, maz@kernel.org, will@kernel.org Cc: suleiman@google.com, senozhatsky@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Hikaru Nishida , Arnd Bergmann , Geert Uytterhoeven , Ingo Molnar , John Stultz , Linus Walleij , Stephen Boyd Subject: [RFC PATCH v3 1/5] timekeeping: Expose tk->offs_boot via ktime_get_offs_boot_ns Date: Wed, 20 Oct 2021 21:04:26 +0900 Message-Id: <20211020210348.RFC.v3.1.Ic2211981441d0504581923b8770809f0750a4d0d@changeid> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211020120431.776494-1-hikalium@chromium.org> References: <20211020120431.776494-1-hikalium@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211020_050457_911241_4BE3B8DF X-CRM114-Status: GOOD ( 11.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Expose tk->offs_boot to be used in kvm virtual suspend injection. Signed-off-by: Hikaru Nishida --- Changes in v3: - Added this patch. include/linux/timekeeping.h | 2 ++ kernel/time/timekeeping.c | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 78a98bdff76d..f7be69c81dab 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -179,6 +179,8 @@ extern u64 ktime_get_raw_fast_ns(void); extern u64 ktime_get_boot_fast_ns(void); extern u64 ktime_get_real_fast_ns(void); +extern u64 ktime_get_offs_boot_ns(void); + /* * timespec64/time64_t interfaces utilizing the ktime based ones * for API completeness, these could be implemented more efficiently diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index b348749a9fc6..e77580d9f8c1 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -565,6 +565,16 @@ u64 ktime_get_real_fast_ns(void) } EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns); +/** + * ktime_get_offs_boot_ns - boottime offset to monotonic. + * Return: boottime offset in nanoseconds. + */ +u64 ktime_get_offs_boot_ns(void) +{ + return ktime_to_ns(tk_core.timekeeper.offs_boot); +} +EXPORT_SYMBOL_GPL(ktime_get_offs_boot_ns); + /** * ktime_get_fast_timestamps: - NMI safe timestamps * @snapshot: Pointer to timestamp storage From patchwork Wed Oct 20 12:04:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hikaru Nishida X-Patchwork-Id: 12572219 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F2F8C433EF for ; Wed, 20 Oct 2021 12:07:45 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 49F47611C6 for ; Wed, 20 Oct 2021 12:07:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 49F47611C6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xkHp+GZTSe0wbUloVyIVMS39Dekt7Yz989Ax7qxbeSs=; b=3tBZYZNNo2aUOv vMOtFkMjMGqkzRDy/yArNZrSf8zcpYv140pF9FzdGjNtRE9Bu8oDrpfyaEZhsvglvZj+kC3ZAyZqW TDoYu2Fci0iFSMypL6SGHPBAeafguWGThSc2L+2JlEKOGomVJuGcpOS1hm9EA9cpC77tFKKMFofqW +6G54C0gJtMpr88q26hl/EfWWWsumo5isd6r9xwUQcBwbvwxXpcgmf511oDJMRd8/s7WQkBuvXYhj iEM774N4xDyiZZdfTP+yweIVPA0NzgUqv+JV3gtT+iKJmrtE8URp2D1iu59pfwY4ITlSsfwA8Zrnx HNzM+CQTKghK1h6RLv+A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdAMA-004Q7t-VI; Wed, 20 Oct 2021 12:06:07 +0000 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdALC-004Pep-Sz for linux-arm-kernel@lists.infradead.org; Wed, 20 Oct 2021 12:05:08 +0000 Received: by mail-pg1-x52e.google.com with SMTP id e65so19903340pgc.5 for ; Wed, 20 Oct 2021 05:05:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=waYD8dsvUPs/3L1638bXCQkgi6UBgjkYBImUZ1krvko=; b=IPB6vWRptmpwaKLEJ2X1nkyazjjUtdTq5CkxdHOjuBZhT/HfwszGKGLZSkLoLYq0R5 ztf2MLxBp40XOAYLBCWmO4AK+rH0/qqbhm4tJLLEASl582myoqUsTP0DfzoqHWybF3i/ 7j+Z09yo9qBg9ov5T9IFPSKwM5D7QkuuL7muY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=waYD8dsvUPs/3L1638bXCQkgi6UBgjkYBImUZ1krvko=; b=HOl81UYJF7IC7ZrVAGsxMDRklx2VMuGFmYqInFxiIrHzDoM22C2Ez2kgnAD4myw1tj qBguGXE1jD8+CkAMvtnwtXxOOkMiOlZ6yevW9C9fF5gWcXqdGwbNLuhIMS0ZRmbTuEEZ FUdi6SqVwpOwiUI4mCUPEFd1elPqoLCttlz2MHEZ2Cdbd6mr1wwP6bedi4svVmI4qhVo B94eN6UfDP3XV644gn68h+sa+cwoKb8g1KqRU/ZtuwySB9b4BcFFRQ5Q8PpdNrvaWy1M 7D2kvamqomFarXU4FWWvq//p3WBgBC+p/88McDT3gfJt/UcxO3qOt76mLJmHS2K2OKNm caTg== X-Gm-Message-State: AOAM531FRhKOmvnOO9yVsMDD9R4FJyLWBqDIKTqoHccIsEVvJxC749wd Jexyai9ARboZx71txj1caiFNBg== X-Google-Smtp-Source: ABdhPJyAa/wmxbz/aUVIPHp/LNEJI1zHjIqTNYybV06ibBEmD12XdN3Ms4lEYjUO9FXnD0ifkyqlBg== X-Received: by 2002:a05:6a00:1916:b0:44d:b930:df4f with SMTP id y22-20020a056a00191600b0044db930df4fmr5921135pfi.39.1634731504152; Wed, 20 Oct 2021 05:05:04 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:e516:d575:e6f:a526]) by smtp.gmail.com with UTF8SMTPSA id z11sm2424576pfk.204.2021.10.20.05.04.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Oct 2021 05:05:03 -0700 (PDT) From: Hikaru Nishida To: linux-kernel@vger.kernel.org, dme@dme.org, tglx@linutronix.de, mlevitsk@redhat.com, linux@roeck-us.net, pbonzini@redhat.com, vkuznets@redhat.com, maz@kernel.org, will@kernel.org Cc: suleiman@google.com, senozhatsky@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Hikaru Nishida , Borislav Petkov , "H. Peter Anvin" , Ingo Molnar , Jim Mattson , Joerg Roedel , Sean Christopherson , Wanpeng Li , kvm@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v3 2/5] kvm/x86: Include asm/pvclock.h in asm/kvmclock.h Date: Wed, 20 Oct 2021 21:04:27 +0900 Message-Id: <20211020120431.776494-2-hikalium@chromium.org> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211020120431.776494-1-hikalium@chromium.org> References: <20211020120431.776494-1-hikalium@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211020_050506_987634_1FACC525 X-CRM114-Status: GOOD ( 11.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Include asm/pvclock.h in asm/kvmclock.h to make struct pvclock_vsyscall_time_info visible since kvmclock.h defines this_cpu_pvti() that needs a definition of the struct. Signed-off-by: Hikaru Nishida --- Changes in v3: - Added this patch. arch/x86/include/asm/kvmclock.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/include/asm/kvmclock.h b/arch/x86/include/asm/kvmclock.h index 6c5765192102..9add14edc24d 100644 --- a/arch/x86/include/asm/kvmclock.h +++ b/arch/x86/include/asm/kvmclock.h @@ -4,6 +4,8 @@ #include +#include + extern struct clocksource kvm_clock; DECLARE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu); From patchwork Wed Oct 20 12:04:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hikaru Nishida X-Patchwork-Id: 12572241 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99195C433EF for ; Wed, 20 Oct 2021 12:08:10 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6746A61074 for ; Wed, 20 Oct 2021 12:08:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6746A61074 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JTq7jxrEBPSfJCUqjtg7h8qDMgvgcPuD1JiisUrX5Jk=; b=iN6nLM9jgzSmcm D6b5uBhpFrNZiN0AGDb8UnKmzdC3HwDNmZsNh8hcyuoSCFJ4YQjscjx9QscQnPof8tL/SoJsQcrrf Fa/fiwB4+VEdqAD7nJcC1bYfhF/exHEiVnjaaQSZbfW52rNyx0E/BPkePccWw+yq/zgM/RnDH7KTk nHBFbHLe04qm1zdD4RfFtnkQWL+s4dKKeZDTVes6hqSpsU02Qe3WblweQg+NxEJYObYm6HE9vKhko nSo3gRsK/cpbrI8fXkoRQNvLpAc3cJ9G2M9AKQT+6cfIEZQccdIgHQS4q/QYC9wHOw7jcRgjnyru+ MuGP5Ylpde0HFIVpWMSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdAMc-004QL5-HW; Wed, 20 Oct 2021 12:06:35 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdALJ-004Phs-J4 for linux-arm-kernel@lists.infradead.org; Wed, 20 Oct 2021 12:05:16 +0000 Received: by mail-pj1-x1033.google.com with SMTP id q2-20020a17090a2e0200b001a0fd4efd49so3693117pjd.1 for ; Wed, 20 Oct 2021 05:05:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7bzPbU2Xaa8fmzxUh6rCSbTEYDD93djqUZnD3TW+SAs=; b=UX+uqnX27wkcUQgZ7Np/jTXc2gdVOauvft2bDy1JTzrj3fz3ZiNp7p/BLL6K/zXTIw R4Q5EID0HPrqtxjWoUKWlr3IayZYodQ+mwfmBI1Ehvyhu5BtHD03bI1iChvsTHq8wxSq PhMLe2jeLzD9QSxuYYTqv4dIPnC124TCI+wow= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7bzPbU2Xaa8fmzxUh6rCSbTEYDD93djqUZnD3TW+SAs=; b=A3hUauDchCrjCOyQXD1dFQWXjZPNYsEUpr/Pko8BzyKwmZQlOhBJ1rai8o0CizrnKg H7puNrNVGFux/7A6V6pANybFzTibUAPZc9XSn/nOVLj9hk58sc4h1iLJXvMdvozU0/cK SxUxifI3C5Eg5UZogwE51gvom+AxIGPbWBJZ9IAmtvauYKOApXDO36RqI8ul8Ni0w222 emllhGrc5GOhVnfIxLWhVs633fWuaE2VPstk79cZXZtbRr8EpXzo+4M0KefeSCxfkccA 03l85tQ0E4r8NCcfwyX+DQoU2hc+iYKOQNHM03wMq+H3jf99DFg1PZLgzA+6EXltOYo2 B2CA== X-Gm-Message-State: AOAM531tLOJ9t2dIg/SIFonQiQv6uPlWQri1/wo4R/Q4XeER3rSSChSs RL6ZYS5pPjThdkQzcm189IHbrQ== X-Google-Smtp-Source: ABdhPJw1+iL+I6HcYGcYZD7jYmOIU3e4eK8PQhco2KBIwknjmRZyUPYqWMY+ib0NNjlPhxwohXSeBQ== X-Received: by 2002:a17:902:8682:b0:13f:8e12:c977 with SMTP id g2-20020a170902868200b0013f8e12c977mr32473932plo.62.1634731512056; Wed, 20 Oct 2021 05:05:12 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:e516:d575:e6f:a526]) by smtp.gmail.com with UTF8SMTPSA id n14sm2115748pgd.68.2021.10.20.05.05.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Oct 2021 05:05:11 -0700 (PDT) From: Hikaru Nishida To: linux-kernel@vger.kernel.org, dme@dme.org, tglx@linutronix.de, mlevitsk@redhat.com, linux@roeck-us.net, pbonzini@redhat.com, vkuznets@redhat.com, maz@kernel.org, will@kernel.org Cc: suleiman@google.com, senozhatsky@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Hikaru Nishida , Borislav Petkov , "H. Peter Anvin" , Ingo Molnar , Jim Mattson , Joerg Roedel , Jonathan Corbet , Sean Christopherson , Wanpeng Li , kvm@vger.kernel.org, linux-doc@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v3 3/5] kvm/x86: virtual suspend time injection: Add common definitions Date: Wed, 20 Oct 2021 21:04:28 +0900 Message-Id: <20211020210348.RFC.v3.3.I6e8f979820f45e38370aa19180a33a8c046d0fa9@changeid> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211020120431.776494-1-hikalium@chromium.org> References: <20211020120431.776494-1-hikalium@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211020_050513_662951_88E78A0B X-CRM114-Status: GOOD ( 17.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add definitions of MSR, KVM_FEATURE bit and a structure called kvm_suspend_time that are used by later patches to support the virtual suspend time injection mechanism. Also add documentations for them. Signed-off-by: Hikaru Nishida --- Changes in v3: - Moved the definition of struct kvm_suspend_time into this patch. Documentation/virt/kvm/cpuid.rst | 3 +++ Documentation/virt/kvm/msr.rst | 30 ++++++++++++++++++++++++++++ arch/x86/include/uapi/asm/kvm_para.h | 6 ++++++ 3 files changed, 39 insertions(+) diff --git a/Documentation/virt/kvm/cpuid.rst b/Documentation/virt/kvm/cpuid.rst index bda3e3e737d7..f17b95b0d943 100644 --- a/Documentation/virt/kvm/cpuid.rst +++ b/Documentation/virt/kvm/cpuid.rst @@ -103,6 +103,9 @@ KVM_FEATURE_HC_MAP_GPA_RANGE 16 guest checks this feature bit bef KVM_FEATURE_MIGRATION_CONTROL 17 guest checks this feature bit before using MSR_KVM_MIGRATION_CONTROL +KVM_FEATURE_HOST_SUSPEND_TIME 18 host suspend time information + is available at msr 0x4b564d09. + KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 host will warn if no guest-side per-cpu warps are expected in kvmclock diff --git a/Documentation/virt/kvm/msr.rst b/Documentation/virt/kvm/msr.rst index 9315fc385fb0..40ec0fd263ac 100644 --- a/Documentation/virt/kvm/msr.rst +++ b/Documentation/virt/kvm/msr.rst @@ -389,3 +389,33 @@ data: guest is communicating page encryption status to the host using the ``KVM_HC_MAP_GPA_RANGE`` hypercall, it can set bit 0 in this MSR to allow live migration of the guest. + +MSR_KVM_HOST_SUSPEND_TIME: + 0x4b564d09 + +data: + 8-byte alignment physical address of a memory area which must be + in guest RAM, plus an enable bit in bit 0. This memory is expected to + hold a copy of the following structure:: + + struct kvm_suspend_time { + __u64 suspend_time_ns; + }; + + whose data will be filled in by the hypervisor. + If the guest register this structure through the MSR write, the host + will stop all the clocks visible to the guest (including TSCs) during + the host's suspension and report the duration of suspend through this + structure. The update will be notified through + HYPERVISOR_CALLBACK_VECTOR IRQ. Fields have the following meanings: + + suspend_time_ns: + Total number of nanoseconds passed during the host's suspend + while the VM is running. This value will be increasing + monotonically and cumulative. + + Note that although MSRs are per-CPU entities, the effect of this + particular MSR is global. + + Availability of this MSR must be checked via bit 18 in 0x4000001 cpuid + leaf prior to usage. diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h index 5146bbab84d4..ccea4e344f46 100644 --- a/arch/x86/include/uapi/asm/kvm_para.h +++ b/arch/x86/include/uapi/asm/kvm_para.h @@ -35,6 +35,7 @@ #define KVM_FEATURE_MSI_EXT_DEST_ID 15 #define KVM_FEATURE_HC_MAP_GPA_RANGE 16 #define KVM_FEATURE_MIGRATION_CONTROL 17 +#define KVM_FEATURE_HOST_SUSPEND_TIME 18 #define KVM_HINTS_REALTIME 0 @@ -57,6 +58,7 @@ #define MSR_KVM_ASYNC_PF_INT 0x4b564d06 #define MSR_KVM_ASYNC_PF_ACK 0x4b564d07 #define MSR_KVM_MIGRATION_CONTROL 0x4b564d08 +#define MSR_KVM_HOST_SUSPEND_TIME 0x4b564d09 struct kvm_steal_time { __u64 steal; @@ -79,6 +81,10 @@ struct kvm_clock_pairing { __u32 pad[9]; }; +struct kvm_suspend_time { + __u64 suspend_time_ns; +}; + #define KVM_STEAL_ALIGNMENT_BITS 5 #define KVM_STEAL_VALID_BITS ((-1ULL << (KVM_STEAL_ALIGNMENT_BITS + 1))) #define KVM_STEAL_RESERVED_MASK (((1 << KVM_STEAL_ALIGNMENT_BITS) - 1 ) << 1) From patchwork Wed Oct 20 12:04:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hikaru Nishida X-Patchwork-Id: 12572243 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2160EC433F5 for ; Wed, 20 Oct 2021 12:09:14 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D969261074 for ; Wed, 20 Oct 2021 12:09:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D969261074 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=bVYS2MwqomZEbUG2GnIqF9pzYKfVh210dOX8ByohLOg=; b=1uK5IBfQPQCxNk jcTCm1YtvBqyh/0Pt8jV4Ktk9HTdhiWfYDJ+gIT7jeUflqs/56ntesMtJirrg/DjWXiUPra+g9I9H ba/Eg3dt2D+aFQnWEiT91N+aBLmzsHzpH5vN4RtKNfaXxwarRx70ANhP37ZEMtM3hxq5sYlU8F4Qm U19lTUt00gk9HzoLYqXXxM/wQy1toPUTuD1V2Zw7nKE7/5Of5s7xqqSDRcgozQAif/xMTTGJUj2DW /wTj67ech8ZKBcY3n9YmrCR41LG9C+Gv0sVDEzxhKC6155bMbdQPM6jESEZ3aDF1gCRVTiXi4OoOv 2LlPdTuOMF3bCxLfn9sw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdAN9-004QbG-Tp; Wed, 20 Oct 2021 12:07:09 +0000 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdALQ-004Pks-DL for linux-arm-kernel@lists.infradead.org; Wed, 20 Oct 2021 12:05:23 +0000 Received: by mail-pg1-x52d.google.com with SMTP id c4so15264078pgv.11 for ; Wed, 20 Oct 2021 05:05:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=W0/hAItlN4jumE3bXtgyJTFeomKZFRmDDJAfP2T9OV0=; b=LiB0botG46s85lJShFIjIEdGtGJ/XTL2dN4M+c0BCZDWGdKgAAvj5HVDVxjwOga1Ct J72JmsM5HiGYoSFvtIKkMbA5u1W/H/ajHQWlVRlzsUdJ9hApUlPml5WIgGNKdDTI38rW AKhE/8iLPYbdrFNWW3SWe8NUzdkFajh/e1f/4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=W0/hAItlN4jumE3bXtgyJTFeomKZFRmDDJAfP2T9OV0=; b=Xc5OBXUw6ymDAODBuPoXJ89UlWc1uBVvyoeKfKHhDB/dvVLcb19eMJemERDR9RzLNh 4uJdrO8rv88SRh1XkpegToBcbBKUlfyH1GYcaxVJRHxAyPR34GPEfRmR41Q3JRfZl89H 2FNv4akaA3NFPH7CjsxsxqD/OaPqzCdWrr0SCaZfEGkONDwIbzZn0KeG3CvpqUoBgBaw QS1VU+4QGtPhfMtnYbq4SHSzo44lLM7t4RchrVBQnLrB8jgTd0fxcq5HHPMnbL8Fo9HA SHuNxs5bxsx/bHtYE+cNq7w9hDTzAwMu8IY35iD2aDSUHjYleb58LiDyQZHCqCImf83K QIjQ== X-Gm-Message-State: AOAM532e+giuBNWMneNxxw/RSIyeuoW6rij4yJwBRRCNM+RtNtP4hAAK LlwoSvql/GDdjQdSDEiR0Ekg/A== X-Google-Smtp-Source: ABdhPJyehwX66T1GfBbIRkPa96YkjVy5nacylS+1zw5CnZZj/eF0uf6zuqh5HuXXG90EGK3hkTbsiw== X-Received: by 2002:a62:3102:0:b0:44b:63db:fc88 with SMTP id x2-20020a623102000000b0044b63dbfc88mr6029199pfx.75.1634731519072; Wed, 20 Oct 2021 05:05:19 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:e516:d575:e6f:a526]) by smtp.gmail.com with UTF8SMTPSA id a12sm5693553pjq.16.2021.10.20.05.05.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Oct 2021 05:05:18 -0700 (PDT) From: Hikaru Nishida To: linux-kernel@vger.kernel.org, dme@dme.org, tglx@linutronix.de, mlevitsk@redhat.com, linux@roeck-us.net, pbonzini@redhat.com, vkuznets@redhat.com, maz@kernel.org, will@kernel.org Cc: suleiman@google.com, senozhatsky@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Hikaru Nishida , Borislav Petkov , "H. Peter Anvin" , Ingo Molnar , Jim Mattson , Joerg Roedel , Sean Christopherson , Wanpeng Li , kvm@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v3 4/5] kvm/x86: virtual suspend time injection: Implement host side Date: Wed, 20 Oct 2021 21:04:29 +0900 Message-Id: <20211020210348.RFC.v3.4.I9c4e7c844507384b546e6d1ea1a5286996eed908@changeid> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211020120431.776494-1-hikalium@chromium.org> References: <20211020120431.776494-1-hikalium@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211020_050520_506836_307F7BE4 X-CRM114-Status: GOOD ( 29.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add main logics that adjust the guest's clocks and notify about the suspension to the guest. Adjustment flow: - Before going into suspend, KVM_REQ_SUSPEND_TIME_ADJ will be requested for each vcpus through the PM notifier if the suspend time injection is enabled for the kvm. - Before the first vmenter after the resume, each vcpu will check the the request and do two kinds of adjustments. - One is kvm-wide adjustment: kvm-clock will be adjusted to the value before the suspend. - Another is per-vcpu adjustment: tsc will be adjusted to the value before the suspend. - Those adjustments happen before the vcpu run: so the guest will not observe the "rewinding" of the clocks. - After the adjustment is made, the guest will be notified about the adjustment through HYPERVISOR_CALLBACK_VECTOR IRQ. - It is guest's responsibility to adjust their CLOCK_BOOTTIME and the wall clock to reflect the suspend. This will be done in the later patch. Signed-off-by: Hikaru Nishida --- Changes in v3: - Used PM notifier instead of modifying timekeeping_resume() - This avoids holding kvm_lock under interrupt disabled context. - Used KVM_REQ_* to make a request for vcpus. - Reused HYPERVISOR_CALLBACK_VECTOR IRQ instead of adding a new one. - Extracted arch-independent parts. arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/Kconfig | 13 ++++ arch/x86/kvm/cpuid.c | 4 ++ arch/x86/kvm/x86.c | 109 ++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 48 ++++++++++++++ virt/kvm/kvm_main.c | 88 ++++++++++++++++++++++++++ 6 files changed, 264 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f8f48a7ec577..bdff8f777632 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1085,6 +1085,8 @@ struct kvm_arch { bool pause_in_guest; bool cstate_in_guest; + u64 msr_suspend_time; + unsigned long irq_sources_bitmap; s64 kvmclock_offset; raw_spinlock_t tsc_write_lock; diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index ac69894eab88..6d68a4d6be87 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -129,4 +129,17 @@ config KVM_MMU_AUDIT This option adds a R/W kVM module parameter 'mmu_audit', which allows auditing of KVM MMU events at runtime. +config KVM_VIRT_SUSPEND_TIMING + bool "Host support for virtual suspend time injection" + depends on KVM=y && HAVE_KVM_PM_NOTIFIER + default n + help + This option makes the host's suspension reflected on the guest's clocks. + In other words, guest's CLOCK_MONOTONIC will stop and + CLOCK_BOOTTIME keeps running during the host's suspension. + This feature will only be effective when both guest and host support + this feature. For the guest side, see KVM_VIRT_SUSPEND_TIMING_GUEST. + + If unsure, say N. + endif # VIRTUALIZATION diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 751aa85a3001..34a2fe147503 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -886,6 +886,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) (1 << KVM_FEATURE_PV_SCHED_YIELD) | (1 << KVM_FEATURE_ASYNC_PF_INT); +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING + entry->eax |= (1 << KVM_FEATURE_HOST_SUSPEND_TIME); +#endif + if (sched_info_on()) entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index aabd3a2ec1bc..b6d0d7f73196 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1367,6 +1367,7 @@ static const u32 emulated_msrs_all[] = { MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, + MSR_KVM_HOST_SUSPEND_TIME, MSR_IA32_TSC_ADJUST, MSR_IA32_TSC_DEADLINE, @@ -3467,6 +3468,19 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.msr_kvm_poll_control = data; break; + case MSR_KVM_HOST_SUSPEND_TIME: + if (!guest_pv_has(vcpu, KVM_FEATURE_HOST_SUSPEND_TIME)) + return 1; + + if (!(data & KVM_MSR_ENABLED)) + break; + + if (kvm_init_suspend_time_ghc(vcpu->kvm, data & ~1ULL)) + return 1; + + vcpu->kvm->arch.msr_suspend_time = data; + break; + case MSR_IA32_MCG_CTL: case MSR_IA32_MCG_STATUS: case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: @@ -3785,6 +3799,12 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vcpu->arch.msr_kvm_poll_control; break; + case MSR_KVM_HOST_SUSPEND_TIME: + if (!guest_pv_has(vcpu, KVM_FEATURE_HOST_SUSPEND_TIME)) + return 1; + + msr_info->data = vcpu->kvm->arch.msr_suspend_time; + break; case MSR_IA32_P5_MC_ADDR: case MSR_IA32_P5_MC_TYPE: case MSR_IA32_MCG_CAP: @@ -9392,6 +9412,93 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING +bool virt_suspend_time_enabled(struct kvm *kvm) +{ + return kvm->arch.msr_suspend_time & KVM_MSR_ENABLED; +} + +/* + * Do per-vcpu suspend time adjustment (tsc) and + * make an interrupt to notify it. + */ +static void vcpu_do_suspend_time_adjustment(struct kvm_vcpu *vcpu, + u64 total_ns) +{ + struct kvm_lapic_irq irq = { + .delivery_mode = APIC_DM_FIXED, + .vector = HYPERVISOR_CALLBACK_VECTOR + }; + u64 last_suspend_duration = 0; + s64 adj; + + spin_lock(&vcpu->suspend_time_ns_lock); + if (total_ns > vcpu->suspend_time_ns) { + last_suspend_duration = total_ns - vcpu->suspend_time_ns; + vcpu->suspend_time_ns = total_ns; + } + spin_unlock(&vcpu->suspend_time_ns_lock); + + if (!last_suspend_duration) { + /* It looks like the suspend is not happened yet. Retry. */ + kvm_make_request(KVM_REQ_SUSPEND_TIME_ADJ, vcpu); + return; + } + + adj = __this_cpu_read(cpu_tsc_khz) * + (last_suspend_duration / 1000000); + adjust_tsc_offset_host(vcpu, -adj); + /* + * This request should be processed before + * the first vmenter after resume to avoid + * an unadjusted TSC value is observed. + */ + kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); + kvm_write_suspend_time(vcpu->kvm); + if (!kvm_apic_set_irq(vcpu, &irq, NULL)) + pr_err("kvm: failed to set suspend time irq\n"); +} + +/* + * Do kvm-wide suspend time adjustment (kvm-clock). + */ +static void kvm_do_suspend_time_adjustment(struct kvm *kvm, u64 total_ns) +{ + spin_lock(&kvm->suspend_time_ns_lock); + if (total_ns > kvm->suspend_time_ns) { + u64 last_suspend_duration = total_ns - kvm->suspend_time_ns; + /* + * Move the offset of kvm_clock here as if it is stopped + * during the suspension. + */ + kvm->arch.kvmclock_offset -= last_suspend_duration; + + /* suspend_time is accumulated per VM. */ + kvm->suspend_time_ns += last_suspend_duration; + /* + * This adjustment will be reflected to the struct provided + * from the guest via MSR_KVM_HOST_SUSPEND_TIME before + * the notification interrupt is injected. + */ + kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); + } + spin_unlock(&kvm->suspend_time_ns_lock); +} + +static void kvm_adjust_suspend_time(struct kvm_vcpu *vcpu) +{ + u64 total_ns = kvm_total_suspend_time(vcpu->kvm); + /* Do kvm-wide adjustment (kvm-clock) */ + kvm_do_suspend_time_adjustment(vcpu->kvm, total_ns); + /* Do per-vcpu adjustment (tsc) */ + vcpu_do_suspend_time_adjustment(vcpu, total_ns); +} +#else +static void kvm_adjust_suspend_time(struct kvm_vcpu *vcpu) +{ +} +#endif + /* * Returns 1 to let vcpu_run() continue the guest execution loop without * exiting to the userspace. Otherwise, the value will be returned to the @@ -9421,6 +9528,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r = -EIO; goto out; } + if (kvm_check_request(KVM_REQ_SUSPEND_TIME_ADJ, vcpu)) + kvm_adjust_suspend_time(vcpu); if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { r = 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0f18df7fe874..ef93c067ceba 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -151,6 +151,7 @@ static inline bool is_error_page(struct page *page) #define KVM_REQ_UNBLOCK 2 #define KVM_REQ_UNHALT 3 #define KVM_REQ_VM_BUGGED (4 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_SUSPEND_TIME_ADJ 5 #define KVM_REQUEST_ARCH_BASE 8 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \ @@ -336,6 +337,11 @@ struct kvm_vcpu { } async_pf; #endif +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING + u64 suspend_time_ns; + spinlock_t suspend_time_ns_lock; +#endif + #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT /* * Cpu relax intercept or pause loop exit optimization @@ -623,6 +629,12 @@ struct kvm { struct notifier_block pm_notifier; #endif char stats_id[KVM_STATS_NAME_SIZE]; +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING + u64 suspend_time_ns; + spinlock_t suspend_time_ns_lock; + u64 base_offs_boot_ns; + struct gfn_to_hva_cache suspend_time_ghc; +#endif }; #define kvm_err(fmt, ...) \ @@ -1829,6 +1841,42 @@ static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu) } #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */ +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING +bool virt_suspend_time_enabled(struct kvm *kvm); +void kvm_write_suspend_time(struct kvm *kvm); +int kvm_init_suspend_time_ghc(struct kvm *kvm, gpa_t gpa); +static inline u64 kvm_total_suspend_time(struct kvm *kvm) +{ + return ktime_get_offs_boot_ns() - kvm->base_offs_boot_ns; +} + +static inline u64 vcpu_suspend_time_injected(struct kvm_vcpu *vcpu) +{ + return vcpu->suspend_time_ns; +} +#else +static inline bool virt_suspend_time_enabled(struct kvm *kvm) +{ + return 0; +} +static inline void kvm_write_suspend_time(struct kvm *kvm) +{ +} +static inline int kvm_init_suspend_time_ghc(struct kvm *kvm, gpa_t gpa) +{ + return 1; +} +static inline u64 kvm_total_suspend_time(struct kvm *kvm) +{ + return 0; +} + +static inline u64 vcpu_suspend_time_injected(struct kvm_vcpu *vcpu) +{ + return 0; +} +#endif /* CONFIG_KVM_VIRT_SUSPEND_TIMING */ + /* * This defines how many reserved entries we want to keep before we * kick the vcpu to the userspace to avoid dirty ring full. This diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7851f3a1b5f7..a4fedd2455d4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -425,6 +425,11 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) vcpu->ready = false; preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); vcpu->last_used_slot = 0; + +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING + vcpu->suspend_time_ns = kvm->suspend_time_ns; + spin_lock_init(&vcpu->suspend_time_ns_lock); +#endif } void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) @@ -812,12 +817,70 @@ static int kvm_init_mmu_notifier(struct kvm *kvm) #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER +static int kvm_suspend_notifier(struct kvm *kvm) +{ + struct kvm_vcpu *vcpu; + int i; + + if (!virt_suspend_time_enabled(kvm)) + return NOTIFY_DONE; + + mutex_lock(&kvm->lock); + kvm_for_each_vcpu(i, vcpu, kvm) + kvm_make_request(KVM_REQ_SUSPEND_TIME_ADJ, vcpu); + mutex_unlock(&kvm->lock); + + return NOTIFY_DONE; +} + +static int kvm_resume_notifier(struct kvm *kvm) +{ + struct kvm_vcpu *vcpu; + int i; + + if (!virt_suspend_time_enabled(kvm)) + return NOTIFY_DONE; + + mutex_lock(&kvm->lock); + kvm_for_each_vcpu(i, vcpu, kvm) { + /* + * Clear KVM_REQ_SUSPEND_TIME_ADJ if the suspend injection is + * not needed (e.g. suspend failure) + * The following condition is also true when the adjustment is + * already done and it is safe to clear the request again here. + */ + if (kvm_total_suspend_time(kvm) == + vcpu_suspend_time_injected(vcpu)) + kvm_clear_request(KVM_REQ_SUSPEND_TIME_ADJ, vcpu); + } + mutex_unlock(&kvm->lock); + + return NOTIFY_DONE; +} + +static int kvm_pm_notifier(struct kvm *kvm, unsigned long state) +{ + switch (state) { + case PM_HIBERNATION_PREPARE: + case PM_SUSPEND_PREPARE: + return kvm_suspend_notifier(kvm); + case PM_POST_HIBERNATION: + case PM_POST_SUSPEND: + return kvm_resume_notifier(kvm); + } + + return NOTIFY_DONE; +} + static int kvm_pm_notifier_call(struct notifier_block *bl, unsigned long state, void *unused) { struct kvm *kvm = container_of(bl, struct kvm, pm_notifier); + if (kvm_pm_notifier(kvm, state) != NOTIFY_DONE) + return NOTIFY_BAD; + return kvm_arch_pm_notifier(kvm, state); } @@ -843,6 +906,26 @@ static void kvm_destroy_pm_notifier(struct kvm *kvm) } #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */ +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING +void kvm_write_suspend_time(struct kvm *kvm) +{ + struct kvm_suspend_time st; + + st.suspend_time_ns = kvm->suspend_time_ns; + kvm_write_guest_cached(kvm, &kvm->suspend_time_ghc, &st, sizeof(st)); +} + +int kvm_init_suspend_time_ghc(struct kvm *kvm, gpa_t gpa) +{ + if (kvm_gfn_to_hva_cache_init(kvm, &kvm->suspend_time_ghc, gpa, + sizeof(struct kvm_suspend_time))) + return 1; + + kvm_write_suspend_time(kvm); + return 0; +} +#endif + static struct kvm_memslots *kvm_alloc_memslots(void) { int i; @@ -1080,6 +1163,11 @@ static struct kvm *kvm_create_vm(unsigned long type) if (r) goto out_err_no_disable; +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING + spin_lock_init(&kvm->suspend_time_ns_lock); + kvm->base_offs_boot_ns = ktime_get_offs_boot_ns(); +#endif + #ifdef CONFIG_HAVE_KVM_IRQFD INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); #endif From patchwork Wed Oct 20 12:04:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hikaru Nishida X-Patchwork-Id: 12572245 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE702C433EF for ; Wed, 20 Oct 2021 12:09:19 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7A904611C6 for ; Wed, 20 Oct 2021 12:09:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7A904611C6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/nAzE4WqdP38dulStLQVnr5Gvng1Xb3MH64gVVzTXqo=; b=wiI56qyCVCcKDi alf4RSEUcWeFxHfrR4X3xygrSSQkWK0KBURtfGGKem2+vFw/g+P7KxrKPdHjccND4sGPptN8SI7FH Xbp2y4UEZHisBPsmSZnbvQuxmq/UKNe9WObExvBYKxJLjoedtupd53QGN4N/yTcVoYyipQwFQxG9D 65FQlJ1S3/4B99tb6V2G9KWWhz5DtSowCHW1TTVD3yH3zXXbcrD9EuC9q3VJ4uzvv5pS2Bz/6uYI4 NYgtThWxUVyblCNcSUhvMBr+rDiYH/isKt4M62xnqGsKobCGebd+Q4aaGirlxfThyuoW3Py61zr8v aY5vgg724H1uBeiEonvA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdANd-004QoC-Ba; Wed, 20 Oct 2021 12:07:37 +0000 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdALa-004Pqi-CY for linux-arm-kernel@lists.infradead.org; Wed, 20 Oct 2021 12:05:33 +0000 Received: by mail-pg1-x530.google.com with SMTP id e65so19904362pgc.5 for ; Wed, 20 Oct 2021 05:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=daEoZ9rYJqo1FQMfaT8aFEhZ3akBhgJvK+CaM2KA7dQ=; b=BuMErA/DuZ/0Gh1Uip5zDi4nhZy8bA5ENyyLMru7SulwHDqlPXRRu5eNimVwwXElC5 lq6qqAFosuH8kSRKMypo3mBPVPfFxgsCNvZm3K0A3h4isPdM1oj9JNELuPiXi0g495MS 1sTscOCX5xW9BG0Y+KVKAbxzfafNY9PQlv49k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=daEoZ9rYJqo1FQMfaT8aFEhZ3akBhgJvK+CaM2KA7dQ=; b=aGOcwoRB2ZLmD5h3gIUqDIs2F89qT4wYJOLrIrkU4AoXxRxOZoMtWxeGxQVve1/N4Q /NI7JEBkrMekHs6Zt4+NKrFEnWtKPKJO72PG1ufXXXIpo/pPQXCUHrRZSfoEZXLIyG24 5Rz8RRUQct2M/UoelNgb1eZtPQv+oWdStft6dEo4sUB4a57y6lPwO1dsXPNYjBRSqmqA ivW+aRBAwaNmrd5QFP8KiUnppO+yKiAVG09Z6M3i7hFAvR9tgyDrLjNfPZC/BvHxBftu PQr64sz1/lasQtBshdh9YXadPfOt311XEA3qeo9Iq8lWi8+GfoVxo522puaTUZ+/mxfJ K8Vw== X-Gm-Message-State: AOAM531vdEqsdK8lRnj4OX+Fa5KplyfLzQC+7QlBx1T5dtyKkf5q+qJ+ O82Ps6iNAz9mbJTe7w3gUYDUQA== X-Google-Smtp-Source: ABdhPJx/Rvr3qcPZwTYCDZsWLdqpM8lN7/5ngKAA29ZJgCF1EtMVqY2etsqhKYaFyuZhxkUHOUFKeQ== X-Received: by 2002:a63:7888:: with SMTP id t130mr26794255pgc.279.1634731528796; Wed, 20 Oct 2021 05:05:28 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:e516:d575:e6f:a526]) by smtp.gmail.com with UTF8SMTPSA id d137sm2573453pfd.72.2021.10.20.05.05.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Oct 2021 05:05:28 -0700 (PDT) From: Hikaru Nishida To: linux-kernel@vger.kernel.org, dme@dme.org, tglx@linutronix.de, mlevitsk@redhat.com, linux@roeck-us.net, pbonzini@redhat.com, vkuznets@redhat.com, maz@kernel.org, will@kernel.org Cc: suleiman@google.com, senozhatsky@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Hikaru Nishida , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Dave Hansen , Geert Uytterhoeven , "H. Peter Anvin" , Ingo Molnar , Jim Mattson , Joerg Roedel , John Stultz , Juergen Gross , Kees Cook , Lai Jiangshan , Linus Walleij , Peter Zijlstra , Sean Christopherson , Stephen Boyd , Wanpeng Li , kvm@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v3 5/5] kvm/x86: virtual suspend time injection: Implement guest side Date: Wed, 20 Oct 2021 21:04:30 +0900 Message-Id: <20211020210348.RFC.v3.5.I99f7da15fd68fc098709ea4bcf74525e0883ea92@changeid> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211020120431.776494-1-hikalium@chromium.org> References: <20211020120431.776494-1-hikalium@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211020_050530_488648_2A835330 X-CRM114-Status: GOOD ( 32.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add guest side implementation of KVM virtual suspend time injection. How it works from guest's view: - Guest will be paused without going through suspend/resume path in the guest kernel - Before resuming the execution of the guest's vcpus, host will adjust the hardware clock (and kvm_clock) to the time before the suspend. - By this action, guest's CLOCK_MONOTONIC behaves as expected (stops during the host's suspension.) - the guest will receive an IRQ from the guest that notifies about the suspend which was invisible to the guest. In the handler, the guest can adjust their CLOCK_BOOTTIME to reflect the suspension. - Now, CLOCK_BOOTTIME includes the time passed during the host's suspension. Signed-off-by: Hikaru Nishida --- Changes in v3: - Reused HYPERVISOR_CALLBACK_VECTOR IRQ instead of adding a new one. - Extracted arch-independent parts. arch/x86/Kconfig | 13 ++++++++ arch/x86/include/asm/idtentry.h | 2 +- arch/x86/include/asm/kvmclock.h | 9 ++++++ arch/x86/kernel/kvm.c | 14 ++++++--- arch/x86/kernel/kvmclock.c | 26 ++++++++++++++++ arch/x86/mm/fault.c | 2 +- include/linux/timekeeper_internal.h | 5 ++++ include/linux/timekeeping.h | 4 +++ kernel/time/timekeeping.c | 46 +++++++++++++++++++++++++++++ 9 files changed, 115 insertions(+), 6 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index d9830e7e1060..1d4a529d1577 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -824,6 +824,19 @@ config KVM_GUEST underlying device model, the host provides the guest with timing infrastructure such as time of day, and system time +config KVM_VIRT_SUSPEND_TIMING_GUEST + bool "Guest support for virtual suspend time injection" + depends on KVM_GUEST + default n + help + This option makes the host's suspension reflected on the guest's clocks. + In other words, guest's CLOCK_MONOTONIC will stop and + CLOCK_BOOTTIME keeps running during the host's suspension. + This feature will only be effective when both guest and host support + this feature. For the host side, see KVM_VIRT_SUSPEND_TIMING. + + If unsure, say N. + config ARCH_CPUIDLE_HALTPOLL def_bool n prompt "Disable host haltpoll when loading haltpoll driver" diff --git a/arch/x86/include/asm/idtentry.h b/arch/x86/include/asm/idtentry.h index 1345088e9902..5e30f84ea07e 100644 --- a/arch/x86/include/asm/idtentry.h +++ b/arch/x86/include/asm/idtentry.h @@ -686,7 +686,7 @@ DECLARE_IDTENTRY_SYSVEC(HYPERVISOR_CALLBACK_VECTOR, sysvec_xen_hvm_callback); #endif #ifdef CONFIG_KVM_GUEST -DECLARE_IDTENTRY_SYSVEC(HYPERVISOR_CALLBACK_VECTOR, sysvec_kvm_asyncpf_interrupt); +DECLARE_IDTENTRY_SYSVEC(HYPERVISOR_CALLBACK_VECTOR, sysvec_kvm_hv_callback); #endif #undef X86_TRAP_OTHER diff --git a/arch/x86/include/asm/kvmclock.h b/arch/x86/include/asm/kvmclock.h index 9add14edc24d..2bf1a5c92319 100644 --- a/arch/x86/include/asm/kvmclock.h +++ b/arch/x86/include/asm/kvmclock.h @@ -20,4 +20,13 @@ static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void) return this_cpu_read(hv_clock_per_cpu); } +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST +u64 kvm_get_suspend_time(void); +#else +static inline u64 kvm_get_suspend_time(void) +{ + return 0; +} +#endif + #endif /* _ASM_X86_KVM_CLOCK_H */ diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index b656456c3a94..3d84ef6d9df2 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -40,6 +40,7 @@ #include #include #include +#include DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled); @@ -270,7 +271,7 @@ noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token) return true; } -DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt) +DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_hv_callback) { struct pt_regs *old_regs = set_irq_regs(regs); u32 token; @@ -286,6 +287,8 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt) wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1); } + timekeeping_inject_virtual_suspend_time(kvm_get_suspend_time()); + set_irq_regs(old_regs); } @@ -710,10 +713,13 @@ static void __init kvm_guest_init(void) if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) apic_set_eoi_write(kvm_guest_apic_eoi_write); - if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) { + if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) static_branch_enable(&kvm_async_pf_enabled); - alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt); - } + + if ((kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) || + kvm_para_has_feature(KVM_FEATURE_HOST_SUSPEND_TIME)) + alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, + asm_sysvec_kvm_hv_callback); #ifdef CONFIG_SMP if (pv_tlb_flush_supported()) { diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 73c74b961d0f..3e16d0ab79f3 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -16,11 +16,15 @@ #include #include #include +#include +#include #include #include #include #include +#include +#include static int kvmclock __initdata = 1; static int kvmclock_vsyscall __initdata = 1; @@ -48,6 +52,9 @@ early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall); static struct pvclock_vsyscall_time_info hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __bss_decrypted __aligned(PAGE_SIZE); +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST +static struct kvm_suspend_time suspend_time __bss_decrypted; +#endif static struct pvclock_wall_clock wall_clock __bss_decrypted; static struct pvclock_vsyscall_time_info *hvclock_mem; DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu); @@ -281,6 +288,17 @@ static int kvmclock_setup_percpu(unsigned int cpu) return p ? 0 : -ENOMEM; } +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST +/** + * kvm_get_suspend_time - duration of host suspend. + * Return: Cumulative duration of host suspend in nanoseconds. + */ +u64 kvm_get_suspend_time(void) +{ + return suspend_time.suspend_time_ns; +} +#endif + void __init kvmclock_init(void) { u8 flags; @@ -295,6 +313,14 @@ void __init kvmclock_init(void) return; } +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST + if (kvm_para_has_feature(KVM_FEATURE_HOST_SUSPEND_TIME)) { + /* Register the suspend time structure */ + wrmsrl(MSR_KVM_HOST_SUSPEND_TIME, + slow_virt_to_phys(&suspend_time) | KVM_MSR_ENABLED); + } +#endif + if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu", kvmclock_setup_percpu, NULL) < 0) { return; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 84a2c8c4af73..f36f49585d5d 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1509,7 +1509,7 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault) * memory is swapped out). Note, the corresponding "page ready" event * which is injected when the memory becomes available, is delivered via * an interrupt mechanism and not a #PF exception - * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()). + * (see arch/x86/kernel/kvm.c: sysvec_kvm_hv_callback()). * * We are relying on the interrupted context being sane (valid RSP, * relevant locks not held, etc.), which is fine as long as the diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 84ff2844df2a..0d5b29122d40 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -68,6 +68,8 @@ struct tk_read_base { * shifted nano seconds. * @ntp_error_shift: Shift conversion between clock shifted nano seconds and * ntp shifted nano seconds. + * @kvm_suspend_time: The cumulative duration of suspend injected through KVM + * in nano seconds. * @last_warning: Warning ratelimiter (DEBUG_TIMEKEEPING) * @underflow_seen: Underflow warning flag (DEBUG_TIMEKEEPING) * @overflow_seen: Overflow warning flag (DEBUG_TIMEKEEPING) @@ -124,6 +126,9 @@ struct timekeeper { u32 ntp_err_mult; /* Flag used to avoid updating NTP twice with same second */ u32 skip_second_overflow; +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST + u64 kvm_suspend_time; +#endif #ifdef CONFIG_DEBUG_TIMEKEEPING long last_warning; /* diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index f7be69c81dab..a2228300c3f9 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -310,4 +310,8 @@ void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock, extern int update_persistent_clock64(struct timespec64 now); #endif +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST +void timekeeping_inject_virtual_suspend_time(u64 total_duration_ns); +#endif + #endif diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index e77580d9f8c1..5f474cde0bae 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2133,6 +2133,52 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, return offset; } +#ifdef CONFIG_KVM_VIRT_SUSPEND_TIMING_GUEST +/** + * timekeeping_inject_virtual_suspend_time - Inject virtual suspend time + * when requested by the kvm host. + * @total_duration_ns: Total suspend time to be injected in nanoseconds. + */ +void timekeeping_inject_virtual_suspend_time(u64 total_duration_ns) +{ + struct timekeeper *tk = &tk_core.timekeeper; + unsigned long flags; + + raw_spin_lock_irqsave(&timekeeper_lock, flags); + if (total_duration_ns > tk->kvm_suspend_time) { + /* + * Do injection only if the time is not injected yet. + * total_duration_ns and tk->kvm_suspend_time values are + * cumulative, so the delta between them will be an amount + * of adjustments. For example, if the host suspends 2 times + * during the guest is running and each suspend is 5 seconds, + * total_duration_ns will be 5 seconds at the first injection + * and tk->kvm_suspend_time was initialized to zero so the + * adjustment injected here will be 5 - 0 = 5 seconds and + * tk->kvm_suspend_time will be updated to 5 seconds. + * On the second injection after the second resume, + * total_duration_ns will be 10 seconds and + * tk->kvm_suspend_time will be 5 seconds so 10 - 5 = 5 seconds + * of the suspend time will be injected again. + */ + struct timespec64 delta = + ns_to_timespec64(total_duration_ns - + tk->kvm_suspend_time); + tk->kvm_suspend_time = total_duration_ns; + + write_seqcount_begin(&tk_core.seq); + timekeeping_forward_now(tk); + __timekeeping_inject_sleeptime(tk, &delta); + timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + write_seqcount_end(&tk_core.seq); + + /* signal hrtimers about time change */ + clock_was_set_delayed(); + } + raw_spin_unlock_irqrestore(&timekeeper_lock, flags); +} +#endif + /* * timekeeping_advance - Updates the timekeeper to the current time and * current NTP tick length