Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp826640rwe; Thu, 1 Sep 2022 08:11:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR7eU3vztiflo9uyUuYDy8cO/UUTZ0MOPb62Ja4CiQ23De4UGxPSvV5sAIzQYO42qUgcjDVh X-Received: by 2002:a17:90a:f698:b0:1fb:b605:df71 with SMTP id cl24-20020a17090af69800b001fbb605df71mr9399695pjb.134.1662045069669; Thu, 01 Sep 2022 08:11:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662045069; cv=none; d=google.com; s=arc-20160816; b=SxGKTMFaDnmVe1iOMyq2M1hktLI2HUdcDk0RSx509gCPjDjwpp6o/aPd/n/U5toRdx bXGeEBCC+s7dXx3AYcIRYsIgy80CzgLT9aerfiyEjB2y3IXCg1mQ3DtfgOaqBuMG5Hwc THOKST9JdLiS62KMZM3Sqwy0qpLiEDk33oq50Yv8RBuUfjhe5+bvft4xa/AHhUT079dn e022Uz74SE9MTYCxk+LucUgZQeHXZZVbIoM2/Imoc00smq2ApQYIfsu31EPZ/cTj/tGP pDaQiJH4TeTjJIc2Hu/RHhUiCBUVcRJO96k8huNpULD+Nv6VgRbpuAqb8iHWVF7lr6ru /Jdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ykCshDeZ61ZZ/vkL3R+EuwdKCNbdXXwsdoUEjEdbwdQ=; b=Q5snLJHZlJjRjeGD1aOrGM2p8QKFBGtq1HMPS77USKkMBc/wr/zIiFX+cKH1J3IVAP Q82yUmJ3hezYJT+BGShy/JNlTBSP/WOoS+lo0poGQcI0Z32GQ4bdK656g6HA7FzyROVh p35HiQKhl3XheToKmESrLJiOvTvE7+zVjH4NghdQ8O7kHX8wAECnltZKMVcCN1UCSQjm EdTcOUgX9DeYhr7/TdHEETxJwaCvQuNE0tSqsg8oH6sJf4okjV8rLAiz/t05Z+ZLSD8+ 0YOMsxk7kVQI15Mr5X5b/euxjoOqHhMaNo8Ql20781Lw6bODrt1JsmW1yhnLhDBNqywO aSlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qmqwyM6Q; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y15-20020a17090abd0f00b001f303395c8esi4602727pjr.141.2022.09.01.08.10.57; Thu, 01 Sep 2022 08:11:09 -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=@google.com header.s=20210112 header.b=qmqwyM6Q; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233291AbiIAONF (ORCPT + 99 others); Thu, 1 Sep 2022 10:13:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232852AbiIAONE (ORCPT ); Thu, 1 Sep 2022 10:13:04 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57AE26527A for ; Thu, 1 Sep 2022 07:13:01 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id j9-20020a17090a3e0900b001fd9568b117so2706020pjc.3 for ; Thu, 01 Sep 2022 07:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=ykCshDeZ61ZZ/vkL3R+EuwdKCNbdXXwsdoUEjEdbwdQ=; b=qmqwyM6QLvOyy/NksQ/znaqhrjGktO4iWyd/wg/8Y/ejpkNQ+vMG8ssvLbwA75HiBJ 9t0d93vgiaUAwdRl+JsGmX6oiVa5JXacgjvVtH/i5iHHGiAGhROybji/gNddGgwemhZx sBeJPleTlX9s25Z1w8LnoAqjmY26Q1kKqkN6esZ4a3WHqnUJMPOLPx4NX9iGJUE3vYOa +75n45CQS5lVZ3DTKaLCgwMdCKfFBW5QSKKdAk6mkk2y4fNZVg9Fr2tqHixMicek2cNg NCMYwycAPtoyJ5lXZuyN+W0ywNtQada2rdQJgabHnh1mO2qxXxQkbCBsHz//cUBBmG3K GECQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=ykCshDeZ61ZZ/vkL3R+EuwdKCNbdXXwsdoUEjEdbwdQ=; b=WAoWlAkL5Wk/XjG3H70UxTVaUh+evESieSiInWq5PNp4lblP05SCxN+G3RzrxQwmBU ER+Lmc8/c7XxpUQO9kZeYHr9ihnxJ7/BZpGtNPZee0GqcCGJM4DEt/JpxHLT5poeXmhj ZKusxBIyBVVIguHxqCvrKJzEChqP0a6YHnoOtLzrtgzqdo3JReMSrPLuVXJi/y1Tb77k OAdkqwEvuNcVk03HsfJlWQ7Et6bLKJ33zvW1Ur4IeN/43A6gVHCRCvcczBWp1/nN3M5G 2LteK2uVMWQGJwaaQU5sLKa5mf0VWZmrqhwh9PUnHH5a2qMZN7SYhrRyZSv0y3//bcUn 3N/Q== X-Gm-Message-State: ACgBeo2iW0EOIrKDhn7Ca055DWBFNscD6o01cftFv6JfC+gfKC2H3GFQ CX9h1xp5Ny9+XJyNdQrTSGPIMA== X-Received: by 2002:a17:903:2102:b0:174:4d5f:8abf with SMTP id o2-20020a170903210200b001744d5f8abfmr28211096ple.11.1662041580361; Thu, 01 Sep 2022 07:13:00 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id u7-20020a170902e5c700b0017550eaa3eesm2661099plf.71.2022.09.01.07.12.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 07:12:59 -0700 (PDT) Date: Thu, 1 Sep 2022 14:12:56 +0000 From: Sean Christopherson To: Chao Gao Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , Kai Huang , Will Deacon Subject: Re: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Thu, Sep 01, 2022, Chao Gao wrote: > On Tue, Aug 30, 2022 at 05:01:16AM -0700, isaku.yamahata@intel.com wrote: > >From: Isaku Yamahata > > > >KVM/X86 uses user return notifier to switch MSR for guest or user space. > >Snapshot host values on CPU online, change MSR values for guest, and > >restore them on returning to user space. The current code abuses > >kvm_arch_hardware_enable() which is called on kvm module initialization or > >CPU online. > > > >Remove such the abuse of kvm_arch_hardware_enable by capturing the host > >value on the first change of the MSR value to guest VM instead of CPU > >online. > > > >Suggested-by: Sean Christopherson > >Signed-off-by: Isaku Yamahata > >--- > > arch/x86/kvm/x86.c | 43 ++++++++++++++++++++++++------------------- > > 1 file changed, 24 insertions(+), 19 deletions(-) > > > >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > >index 205ebdc2b11b..16104a2f7d8e 100644 > >--- a/arch/x86/kvm/x86.c > >+++ b/arch/x86/kvm/x86.c > >@@ -200,6 +200,7 @@ struct kvm_user_return_msrs { > > struct kvm_user_return_msr_values { > > u64 host; > > u64 curr; > >+ bool initialized; > > } values[KVM_MAX_NR_USER_RETURN_MSRS]; > > The benefit of having an "initialized" state for each user return MSR on > each CPU is small. A per-cpu state looks suffice. With it, you can keep > kvm_user_return_msr_cpu_online() and simply call the function from > kvm_set_user_return_msr() if initialized is false on current CPU. Yep, a per-CPU flag is I intended. This is the completely untested patch that's sitting in a development branch of mine. --- arch/x86/kvm/x86.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index eca76f187e4b..1328326acfae 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -194,6 +194,7 @@ module_param(eager_page_split, bool, 0644); struct kvm_user_return_msrs { struct user_return_notifier urn; + bool initialized; bool registered; struct kvm_user_return_msr_values { u64 host; @@ -400,18 +401,20 @@ int kvm_find_user_return_msr(u32 msr) return -1; } -static void kvm_user_return_msr_cpu_online(void) +static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs) { - unsigned int cpu = smp_processor_id(); - struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); u64 value; int i; + if (msrs->initialized) + return; + for (i = 0; i < kvm_nr_uret_msrs; ++i) { rdmsrl_safe(kvm_uret_msrs_list[i], &value); msrs->values[i].host = value; msrs->values[i].curr = value; } + msrs->initialized = true; } int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) @@ -420,6 +423,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); int err; + kvm_user_return_msr_init_cpu(msrs); + value = (value & mask) | (msrs->values[slot].host & ~mask); if (value == msrs->values[slot].curr) return 0; @@ -11740,7 +11745,6 @@ int kvm_arch_hardware_enable(void) u64 max_tsc = 0; bool stable, backwards_tsc = false; - kvm_user_return_msr_cpu_online(); ret = static_call(kvm_x86_hardware_enable)(); if (ret != 0) return ret; base-commit: a8f21d1980fbd7e877ed174142f7f572d547e611 --