Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1021048rwe; Thu, 1 Sep 2022 11:07:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR7gq1n/tMP8HL4nDNjMREj/789h/VHqw01ae7eNOB1sYCV0xrXSAvZrcIi6OQ0p9vYZ60rT X-Received: by 2002:a17:90a:2e12:b0:1fb:a4d4:be3f with SMTP id q18-20020a17090a2e1200b001fba4d4be3fmr345512pjd.237.1662055654997; Thu, 01 Sep 2022 11:07:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662055654; cv=none; d=google.com; s=arc-20160816; b=hFI231rRuFyoVhZhn11Ufb2lO1KAQBU9WMjBu46g934wxldb427k29xAFkTzrasyPG xls4GgjCbgaTjbpIEyhZHrc7kL5rVQnozv5jK3mLLuxMD4nA0mgV9byVO2LAKc1DKH1T Rfx3AXFpIss6WuMqx7nOqFqMVpXcUWODs6ryoRHLdKb2+RNbA9b55FlIUZYs/kZ4K8nb C140ALBMrMK8Gl2XNj3SgSVtrrcVkBeVjjYzkVhw7P7sKTgRv+sMl1NuOetmN4Tt0Jag NSaEW/68a+Y2YpmBKUmq5VGl5Exii7mr5MM0nVpjZtv4KROj5orULoSYr0QV3BMf/u8y aO6w== 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=qWpjRl4ZF6VPWjvFhKrl1KyHnirvLl4yMapa4BXRQks=; b=0OOG+0hbohDaysCENSIbQaUX10K0ixHle3FI9oIKWcr41wp51nRKWHg5dA5mONS+i4 3WskiusqxXD9bTeOdOTDBW3HxkW2iv8FsRJk9UpQHO0HDRqaylPVY2top6ZZ0DpRY6X3 Ov/RcCf2Iunc0ZUw5P1flS+xoGHxFGWb6ukmmuGq3T0bGsowoCIsHeos7GRN0kXn+Aay hxA60LculM1LcuYHgZiK58bF4+lllFv3kj9SmcKIxX3syDAGWTnojXJI5EpT266IYnhL V+jaO4NuK0XXHiWjHGUBwRp6JxwgzCLD5lRL+j2pDZAGC9D1YtbvMSoXjMotgFp7RnQl IASQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OJCRgC1j; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id on8-20020a17090b1d0800b001f750254ccfsi5315579pjb.17.2022.09.01.11.07.22; Thu, 01 Sep 2022 11:07:34 -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=@gmail.com header.s=20210112 header.b=OJCRgC1j; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233643AbiIARuA (ORCPT + 99 others); Thu, 1 Sep 2022 13:50:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231304AbiIARt6 (ORCPT ); Thu, 1 Sep 2022 13:49:58 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C20B9B86D; Thu, 1 Sep 2022 10:49:55 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id x19so16298743pfr.1; Thu, 01 Sep 2022 10:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc; bh=qWpjRl4ZF6VPWjvFhKrl1KyHnirvLl4yMapa4BXRQks=; b=OJCRgC1jpyrffufXczCD7jm2h7wjD3cisOSu9PONuvqge5obk8aYt5ZSgZiv2e6otx VElkCUPXaoQhmYIBHDdWH+qE8Nsangsj2pLqJE8MwD65hxBhcNsdcXv3nP1TW68yRy54 hllrf9FSwpsAbAERkXHMh9QlDEF2QBgtRxLuzkiS/14NI63dbEhs5JpWjixI4Kj+glNo jLel6UHLDlz0Y6z/WGGZQB96bN6wP+jGvOpgu7qd5Ela2MlQUwVhd1imDmIhDQCN6gat GKh8M61lJY/xEWY2TiXDVPLTkTyILboz6YKjnAQnEyC9WFYFek/5LrSJxRJsXpNG/0lw fR9w== 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; bh=qWpjRl4ZF6VPWjvFhKrl1KyHnirvLl4yMapa4BXRQks=; b=6tNTlALoipVcDiu1TrVcKe3V2eqf1zTs7CUx3C1ejPGM7oJr88c1wLiJZDZiNrgdUX V4XOdr1Zz2hvLh7403XaucsR/YxOsX1DRs2XcqQT+I9zQid7e2Jv6qCJmVBsh2sLlg0i 1FCOaTvc4F5DsHiDGvkCBRTCSClcxObY9qUQFOICTKAS/pSuhl55W7vSwwCpfmLDmIDt vfyjsnqiV/NqsbyYwreNyIE9lHvVO577wXneQiJd4LqqHJCpN7ixAjpGOIV9s2+3iEMy 4uRjYpKXCoBNn6O+QQo/CMbNCCVeXXiNqosSfPVYpIqHufYGjgAx989dWcir6CETeVO2 MmUA== X-Gm-Message-State: ACgBeo0y/D1vnTdJghWu/9MN8YMdN/t36w597of8GskzCGSqLH4fV5NS 5tG1azz73f1fGej5AwaVtQ4= X-Received: by 2002:a63:81c6:0:b0:42b:c3fa:3a0 with SMTP id t189-20020a6381c6000000b0042bc3fa03a0mr21184925pgd.72.1662054594477; Thu, 01 Sep 2022 10:49:54 -0700 (PDT) Received: from localhost ([192.55.55.51]) by smtp.gmail.com with ESMTPSA id d68-20020a621d47000000b00535e46171c1sm13508621pfd.117.2022.09.01.10.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 10:49:54 -0700 (PDT) Date: Thu, 1 Sep 2022 10:49:53 -0700 From: Isaku Yamahata To: Sean Christopherson Cc: Chao Gao , 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: <20220901174953.GH2711697@ls.amr.corp.intel.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 at 02:12:56PM +0000, Sean Christopherson wrote: > 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. With the following fix, it worked. I'll replace this patch with yours. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 205ebdc2b11b..0e200fe44b35 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9212,7 +9217,12 @@ int kvm_arch_init(void *opaque) return -ENOMEM; } - user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); + /* + * __GFP_ZERO to ensure user_return_msrs.values[].initialized = false. + * See kvm_user_return_msr_init_cpu(). + */ + user_return_msrs = alloc_percpu_gfp(struct kvm_user_return_msrs, + GFP_KERNEL | __GFP_ZERO); if (!user_return_msrs) { printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); r = -ENOMEM; -- Isaku Yamahata