From patchwork Tue Apr 13 07:16:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12199501 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DAA1C433B4 for ; Tue, 13 Apr 2021 07:16:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 21AC66120E for ; Tue, 13 Apr 2021 07:16:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237770AbhDMHQu (ORCPT ); Tue, 13 Apr 2021 03:16:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237674AbhDMHQm (ORCPT ); Tue, 13 Apr 2021 03:16:42 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9E68C061574; Tue, 13 Apr 2021 00:16:23 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id b17so11287185pgh.7; Tue, 13 Apr 2021 00:16:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lEku+Nldal8skQz83jlsEXJCMsplj1mVF4FlMiQwdgc=; b=JAI3885dCP/Tr9ozd+eLPK7/87axdOXIeCxUAKh3AfMd6daXnaKe0cNkVKHEQu11Uz hx8MzT7UeMYkR94asHUkkvnMw3Y4OdF+xLQxo/2aZ8HZUrgu0rupDHzyPyQiVViBpV5T BuEEPJcrgtiprFqChHyqDFVeVWQwwF2P6naZEjL0842UM3BS2vmkmjh7Sh4rjkMZhLmj 1XY94DeHhbcF0htJTL/MUdZHxIpsNuYXScrZDXPD5YYDFt7OL1uKLEQuIBB3+kM37cP1 5Ql6TZZJ836ccOPOMtpat6jKnGZP1VEPnvVoe9D78HHsJY6SbWiF4JPb/oQjiXMAUV5A JJ+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lEku+Nldal8skQz83jlsEXJCMsplj1mVF4FlMiQwdgc=; b=HGgldkf5vsThUGHqu9WciJ447EfLaRIJmmQBPJDXu1+wiVR0FKwibyCAI5TqiqYRde i3sZUDPaVCSz1/nQ/yQpMQRlTCfP0b7vQUznJ6dc6TQbKKtYW0VCE2RioQvPjzze5DHp uUjWnhvcI1yYaIjp+Nz3qBJydj77GqE90MZtDYqnrj0f+IvljVk1peGn5oRfQcgbDNuo XhQCsN2L1x5rLjutcoUJLoqdV4qIoipgVk9IwdVM+wgdOG7b24n/2d40Bz1IOCDaJUnG Ky8zGoaNtK1lnoatuSictRlLrqqmuhuWlgZvqHiXquGNcTbyurt4WS6KYirMxUfhqcMQ Aq/Q== X-Gm-Message-State: AOAM532dhSjgr0ivn7HHQ/PSCUZQCH1zRvoi583c1lM6LUdUCA8sY6Jz Ce+cfo9/zxuJTNx0EDWzRhUTkzKG2uQ= X-Google-Smtp-Source: ABdhPJzLnFgeSQ05yCVeGcUCwoof/Fv9TY2evQJq2Kz2CsYLpNdbJPgrJJNvWckMMEUM4AGyNrZcOw== X-Received: by 2002:a65:640b:: with SMTP id a11mr30715660pgv.357.1618298183022; Tue, 13 Apr 2021 00:16:23 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id i10sm2031088pjm.1.2021.04.13.00.16.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Apr 2021 00:16:22 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Michael Tokarev Subject: [PATCH v2 1/3] context_tracking: Split guest_enter/exit_irqoff Date: Tue, 13 Apr 2021 15:16:07 +0800 Message-Id: <1618298169-3831-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> References: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Split context_tracking part from guest_enter/exit_irqoff, it will be called separately in later patches. Suggested-by: Thomas Gleixner Cc: Thomas Gleixner Cc: Sean Christopherson Cc: Michael Tokarev Signed-off-by: Wanpeng Li --- include/linux/context_tracking.h | 42 +++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/include/linux/context_tracking.h b/include/linux/context_tracking.h index bceb064..d8ad844 100644 --- a/include/linux/context_tracking.h +++ b/include/linux/context_tracking.h @@ -104,16 +104,8 @@ static inline void context_tracking_init(void) { } #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN -/* must be called with irqs disabled */ -static __always_inline void guest_enter_irqoff(void) +static __always_inline void context_guest_enter_irqoff(void) { - instrumentation_begin(); - if (vtime_accounting_enabled_this_cpu()) - vtime_guest_enter(current); - else - current->flags |= PF_VCPU; - instrumentation_end(); - if (context_tracking_enabled()) __context_tracking_enter(CONTEXT_GUEST); @@ -131,10 +123,28 @@ static __always_inline void guest_enter_irqoff(void) } } -static __always_inline void guest_exit_irqoff(void) +/* must be called with irqs disabled */ +static __always_inline void guest_enter_irqoff(void) +{ + instrumentation_begin(); + if (vtime_accounting_enabled_this_cpu()) + vtime_guest_enter(current); + else + current->flags |= PF_VCPU; + instrumentation_end(); + + context_guest_enter_irqoff(); +} + +static __always_inline void context_guest_exit_irqoff(void) { if (context_tracking_enabled()) __context_tracking_exit(CONTEXT_GUEST); +} + +static __always_inline void guest_exit_irqoff(void) +{ + context_guest_exit_irqoff(); instrumentation_begin(); if (vtime_accounting_enabled_this_cpu()) @@ -145,6 +155,13 @@ static __always_inline void guest_exit_irqoff(void) } #else +static __always_inline void context_guest_enter_irqoff(void) +{ + instrumentation_begin(); + rcu_virt_note_context_switch(smp_processor_id()); + instrumentation_end(); +} + static __always_inline void guest_enter_irqoff(void) { /* @@ -155,10 +172,13 @@ static __always_inline void guest_enter_irqoff(void) instrumentation_begin(); vtime_account_kernel(current); current->flags |= PF_VCPU; - rcu_virt_note_context_switch(smp_processor_id()); instrumentation_end(); + + context_guest_enter_irqoff(); } +static __always_inline void context_guest_exit_irqoff(void) { } + static __always_inline void guest_exit_irqoff(void) { instrumentation_begin(); From patchwork Tue Apr 13 07:16:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12199503 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18F69C43460 for ; Tue, 13 Apr 2021 07:16:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ECE70613B2 for ; Tue, 13 Apr 2021 07:16:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237902AbhDMHQ4 (ORCPT ); Tue, 13 Apr 2021 03:16:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237711AbhDMHQr (ORCPT ); Tue, 13 Apr 2021 03:16:47 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6496FC06175F; Tue, 13 Apr 2021 00:16:26 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id y2so7717480plg.5; Tue, 13 Apr 2021 00:16:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7Jftw9d1UNN2cAlTTeBC6cyl7AFTXXsjQPeVpHE1tC4=; b=LJWiE4dGQiKGJ5R43HEmLDipvwSYLViFRDYpFXrikdEcECXXNq6drU06/7utuzaK4b SUcxn4YyI1+N8Zg6EqIm2le6XAyXoWwDwwRmjPkQxUJnf95Noyfe1Gvp5RXqMlLfa26G epIDwSSKUET1XbTOt9BHCxQCKzsCCEDt0sG4Xy99bxRLs0jUm2JPvyoHYpcBBso/xXnl 1JDVyBHIVskqXmKIs5kXVq9VkJ/Qw66siCpicrEmRrEmQLL43EOkbwD4wZLvRmj4fL+s UMqTMHVymdRbdkAa8HVjlhHobFxE/JWQP2oN6s79hUvA23zeqleOkcV4epOaZBEhrIhl bzQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7Jftw9d1UNN2cAlTTeBC6cyl7AFTXXsjQPeVpHE1tC4=; b=Tf7XP4y2ii8ynpph/DqWZWenGytc+pBjm1QM8bpfFrkS2ghbyHLF/y93VZEKgklxls MCEeRHumS8dJ7FmyW6mKiAy+DqAvGhFIRXB5or+N9HM+j2VOXliwHBq4vncsLJUuCLyg OVQqCUvwqtEiBbcWEG5FPLZlJH0SqrhyxR8ZBrMVA+EPo51YyDzODYl4734sJ0eHwrks gm5F5XRXG0Wr376wTnUtMxY+YKAum0HepGByCCE+opEwFIaILiHltSMmVgtARIr+fC3u xWJt9brd42VaKyQN7kt79FYZaetZUbLjMMnv7aMccYemNehHsFTpR1KfCVTTA27Pjnmu CC1w== X-Gm-Message-State: AOAM530gh+j+aSFZ+WtZjMv6YS0nuKbM9lz2aCUwCK8BSlRakv2fgijn 5t0ApsVBtuHCb3qoE4/AdF+yk7/1GCU= X-Google-Smtp-Source: ABdhPJxfPdubRYlLaWbv+Oa1GiQv+QCzV5oxm8hBz68c8n4g2uhMuVTWhXYEiBUkLt/UYVy4hPE+kQ== X-Received: by 2002:a17:90a:c81:: with SMTP id v1mr3257153pja.23.1618298185789; Tue, 13 Apr 2021 00:16:25 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id i10sm2031088pjm.1.2021.04.13.00.16.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Apr 2021 00:16:25 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Michael Tokarev Subject: [PATCH v2 2/3] context_tracking: Provide separate vtime accounting functions Date: Tue, 13 Apr 2021 15:16:08 +0800 Message-Id: <1618298169-3831-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> References: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Provide separate vtime accounting functions, because having proper wrappers for that case would be too consistent and less confusing. Suggested-by: Thomas Gleixner Cc: Thomas Gleixner Cc: Sean Christopherson Cc: Michael Tokarev Signed-off-by: Wanpeng Li --- include/linux/context_tracking.h | 50 ++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/include/linux/context_tracking.h b/include/linux/context_tracking.h index d8ad844..491f889 100644 --- a/include/linux/context_tracking.h +++ b/include/linux/context_tracking.h @@ -102,6 +102,40 @@ extern void context_tracking_init(void); static inline void context_tracking_init(void) { } #endif /* CONFIG_CONTEXT_TRACKING_FORCE */ +static __always_inline void vtime_account_guest_enter(void) +{ + if (IS_ENABLED(CONFIG_VIRT_CPU_ACCOUNTING_GEN)) { + if (vtime_accounting_enabled_this_cpu()) + vtime_guest_enter(current); + else + current->flags |= PF_VCPU; + } else { + vtime_account_kernel(current); + current->flags |= PF_VCPU; + } +} + +static __always_inline void __vtime_account_guest_exit(void) +{ + if (IS_ENABLED(CONFIG_VIRT_CPU_ACCOUNTING_GEN)) { + if (vtime_accounting_enabled_this_cpu()) + vtime_guest_exit(current); + } else { + vtime_account_kernel(current); + } +} + +static __always_inline void vtime_account_guest_exit(void) +{ + __vtime_account_guest_exit(); + current->flags &= ~PF_VCPU; +} + +static __always_inline void vcpu_account_guest_exit(void) +{ + if (!vtime_accounting_enabled_this_cpu()) + current->flags &= ~PF_VCPU; +} #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN static __always_inline void context_guest_enter_irqoff(void) @@ -127,10 +161,7 @@ static __always_inline void context_guest_enter_irqoff(void) static __always_inline void guest_enter_irqoff(void) { instrumentation_begin(); - if (vtime_accounting_enabled_this_cpu()) - vtime_guest_enter(current); - else - current->flags |= PF_VCPU; + vtime_account_guest_enter(); instrumentation_end(); context_guest_enter_irqoff(); @@ -147,10 +178,7 @@ static __always_inline void guest_exit_irqoff(void) context_guest_exit_irqoff(); instrumentation_begin(); - if (vtime_accounting_enabled_this_cpu()) - vtime_guest_exit(current); - else - current->flags &= ~PF_VCPU; + vtime_account_guest_exit(); instrumentation_end(); } @@ -170,8 +198,7 @@ static __always_inline void guest_enter_irqoff(void) * to flush. */ instrumentation_begin(); - vtime_account_kernel(current); - current->flags |= PF_VCPU; + vtime_account_guest_enter(); instrumentation_end(); context_guest_enter_irqoff(); @@ -183,8 +210,7 @@ static __always_inline void guest_exit_irqoff(void) { instrumentation_begin(); /* Flush the guest cputime we spent on the guest */ - vtime_account_kernel(current); - current->flags &= ~PF_VCPU; + vtime_account_guest_exit(); instrumentation_end(); } #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ From patchwork Tue Apr 13 07:16:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12199505 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1FD33C433B4 for ; Tue, 13 Apr 2021 07:17:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02EF2613B7 for ; Tue, 13 Apr 2021 07:17:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238522AbhDMHRZ (ORCPT ); Tue, 13 Apr 2021 03:17:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238477AbhDMHRX (ORCPT ); Tue, 13 Apr 2021 03:17:23 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48A62C061574; Tue, 13 Apr 2021 00:17:03 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d8so7707747plh.11; Tue, 13 Apr 2021 00:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qBn3NeoemY9J5Bkb/rnTcONzq25KY3gcneMyVMKWJh4=; b=N6UMgf8jU+Ph1yHrv45fN2UQmjBSexJWoxDczpBiSXMn3vW8WiXgpxdbhONQdgvDuc 5AD4k+AIC3ZXKMGEdVogNSBv3J+NvTT1e0eiuJ7Vch0MytAFVjhRKrJKKIA8eaJWhA3Z OiN63URh3CJ541DhIXcwfA6eBoTEYqWFTP3haYCZX+VIMLODulSv5ZMMn0SnQK427UU/ yvLbMTDweMgUY8Adt4oW6BwLT7kbRDbOgG/a2Q0Rx9/ILBIq9IWU1N2BCuPs7Wbn93Ox SDIfHxtIeVbzsSArXBfEuhV9zPJ24Lzy+AhhN6wd7ek0Vy5Jm57s1Uv2gD5DYeUKTy/E MyWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qBn3NeoemY9J5Bkb/rnTcONzq25KY3gcneMyVMKWJh4=; b=V+qCHvOlSCG/MvOCUvtq0jlfT+8jVaSr/q73CUrarQMD5NxIlVeUNUbW/SZlKnAFEV oxbf/rzGYR1o1Jqj2bFs2lVKTm05ulLrNOUTKZcwkWPbUI1Mgi1rmThKs3kaCZYet/Jc arshLOd7LCFqk5s0VVQeqAVhOsbFYhMzLaUI+JFFzCahgMAu5NxJqnJR04pKUzFAhndS yINKYeMV+P/dPAfAHTcTeu8/AFPvm46kgovewIEg9L/2r4KY5Cy+hjF/Go3RTh94vnPL VtbDIRf747iBqjoOORggDuhNe88Iw7AxdlB4U+B5ILVMLv3xipfonj7YY+wAh1TasQkE DszA== X-Gm-Message-State: AOAM533ymMG0dfsV7EYW9DlXiu5ZP9gMjGtB7zbK4xBJE2acfvTl76DA eKuzcDX71PuteKL/n+yu4rXVCErINx8= X-Google-Smtp-Source: ABdhPJxhm0Sy6QK/KoC4cBVp3b45mZd0Y4H+Bx3swufVvcFqxy55E8mXgubHsHQU5bxVVc9qoXY2JA== X-Received: by 2002:a17:902:ab86:b029:e9:47c1:93de with SMTP id f6-20020a170902ab86b02900e947c193demr30787184plr.69.1618298222449; Tue, 13 Apr 2021 00:17:02 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id i10sm2031088pjm.1.2021.04.13.00.16.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Apr 2021 00:17:01 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Michael Tokarev , stable@vger.kernel.org Subject: [PATCH v2 3/3] x86/kvm: Fix vtime accounting Date: Tue, 13 Apr 2021 15:16:09 +0800 Message-Id: <1618298169-3831-4-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> References: <1618298169-3831-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li The bugzilla https://bugzilla.kernel.org/show_bug.cgi?id=209831 reported that the guest time remains 0 when running a while true loop in the guest. The commit 87fa7f3e98a131 ("x86/kvm: Move context tracking where it belongs") moves guest_exit_irqoff() close to vmexit breaks the tick-based time accouting when the ticks that happen after IRQs are disabled are incorrectly accounted to the host/system time. This is because we exit the guest state too early. Keep context tracking around the actual vmentry/exit code, the time accounting logic is separated out by preposed patch from guest_enter/exit_irqoff(). Keep vtime-based time accounting around the actual vmentry/exit code when vtime_accounting_enabled_this_cpu() is true, leave PF_VCPU clearing after handle_exit_irqoff() and explicit IRQ window for tick-based time accouting. Fixes: 87fa7f3e98a131 ("x86/kvm: Move context tracking where it belongs") Cc: Thomas Gleixner Cc: Sean Christopherson Cc: Michael Tokarev Cc: stable@vger.kernel.org#v5.9-rc1+ Suggested-by: Thomas Gleixner Signed-off-by: Wanpeng Li --- arch/x86/kvm/svm/svm.c | 6 ++++-- arch/x86/kvm/vmx/vmx.c | 6 ++++-- arch/x86/kvm/x86.c | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 48b396f3..2a4e284 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3726,11 +3726,12 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) * accordingly. */ instrumentation_begin(); + vtime_account_guest_enter(); trace_hardirqs_on_prepare(); lockdep_hardirqs_on_prepare(CALLER_ADDR0); instrumentation_end(); - guest_enter_irqoff(); + context_guest_enter_irqoff(); lockdep_hardirqs_on(CALLER_ADDR0); if (sev_es_guest(vcpu->kvm)) { @@ -3758,10 +3759,11 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) * into world and some more. */ lockdep_hardirqs_off(CALLER_ADDR0); - guest_exit_irqoff(); + context_guest_exit_irqoff(); instrumentation_begin(); trace_hardirqs_off_finish(); + __vtime_account_guest_exit(); instrumentation_end(); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c05e6e2..23be956 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6613,11 +6613,12 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, * accordingly. */ instrumentation_begin(); + vtime_account_guest_enter(); trace_hardirqs_on_prepare(); lockdep_hardirqs_on_prepare(CALLER_ADDR0); instrumentation_end(); - guest_enter_irqoff(); + context_guest_enter_irqoff(); lockdep_hardirqs_on(CALLER_ADDR0); /* L1D Flush includes CPU buffer clear to mitigate MDS */ @@ -6647,10 +6648,11 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, * into world and some more. */ lockdep_hardirqs_off(CALLER_ADDR0); - guest_exit_irqoff(); + context_guest_exit_irqoff(); instrumentation_begin(); trace_hardirqs_off_finish(); + __vtime_account_guest_exit(); instrumentation_end(); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ce9a1d2..0d2dd3f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9245,6 +9245,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) ++vcpu->stat.exits; local_irq_disable(); kvm_after_interrupt(vcpu); + vcpu_account_guest_exit(); if (lapic_in_kernel(vcpu)) { s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;