From patchwork Wed Nov 2 16:35:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinank Jain X-Patchwork-Id: 14386 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp16973wru; Wed, 2 Nov 2022 09:42:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM61SZt6MGhQg+jwGFFZ+KGoNukyRhsDkFyfjY/6R6uDdnbrgigmpCcZ/x96HRFByOz3tC1R X-Received: by 2002:a17:902:f786:b0:180:6f9e:23b with SMTP id q6-20020a170902f78600b001806f9e023bmr26145287pln.37.1667407330589; Wed, 02 Nov 2022 09:42:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667407330; cv=none; d=google.com; s=arc-20160816; b=MOz43IfX/dqr2meUz0JDw+5Qi8QZB8wF13gkyE7EwfFpdkfciIcRi4bZFIeSrhnnfD gXjU55a/NPPlzp8uHff4OQkZ05++pSPVOWtdhSq3UBkkIumrKQN8IIctjzXLmw21hqfG KgDWhegNDKrajcAwPpQGNNhtv/N77Qgct7KmzeWGAewnBq2w0N0NANQPblnTplQ+DorX lRS3jGMavcbKp1ZmMuDBhBdQEdfhsuHgWTyidZlV9WmGZkGH/8YV3YBZIye9Q3XaxEWl p0DfSNKQiwT0GxzonQ+SR0YfFxL8WpKXB+7GgmCs514M/5l5t80TK0S0a/VuDJOdgtVL OuFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=oW+0gwVBEPgjicK1Au5RL8r3QijDOXjFhJUc9gdVX18=; b=ZiAgmREYaDIR2iZbB+mNZuB8KZ6O90kaK7Z1RJeSD+zLFpUSHUNltCvArr5jTF3lZa ep9CWjN5EHsJkHpGVZFwVTVvPTmIGV2Z56H35h7VWdu8In3xHtCSAXLQ3TQclGXk49P2 Ls6N76EE2eh/zt2t2Waj4YTuLTcUa3rQIDIt9fgQR6U2VTVhsv+IWbne6a65UxuStDkO c89fP5HICnfxHSLAnaXE7fpPFnWHfcvAGX2wVJyjjY6SsGvrRBaWu8e5/0bsg+p9+rjf hsp3APvroaoDFkpPyTKTgA5mSBUb+cLOvDYcDbhlBXh5b8Xb2Yft4TrG4I57ej7V9c28 qhfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b="KBNLn/Ux"; 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=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g6-20020a056a000b8600b0056c2a741b03si19616725pfj.81.2022.11.02.09.41.53; Wed, 02 Nov 2022 09:42:10 -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=@linux.microsoft.com header.s=default header.b="KBNLn/Ux"; 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=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229875AbiKBQlb (ORCPT + 99 others); Wed, 2 Nov 2022 12:41:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231209AbiKBQk5 (ORCPT ); Wed, 2 Nov 2022 12:40:57 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B72D22ED65; Wed, 2 Nov 2022 09:36:12 -0700 (PDT) Received: from jinankjain-dranzer.zrrkmle5drku1h0apvxbr2u2ee.ix.internal.cloudapp.net (unknown [20.188.121.5]) by linux.microsoft.com (Postfix) with ESMTPSA id 8C23D20C28BD; Wed, 2 Nov 2022 09:36:08 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 8C23D20C28BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1667406972; bh=oW+0gwVBEPgjicK1Au5RL8r3QijDOXjFhJUc9gdVX18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KBNLn/UxImLyYQaCXoVZ1wFrGLXg2bT+PKX6p+TRyOPjFbpW2Y2X2CtVdcMuBk15A MVB8xCRUd9jvg23ifU5s1XZ90oLgx04F8+kJ8W845y0FzC/9DVVpixxk4dPo3JRuPN 5BXeOrwBkoXkV5KOU+w8ICezSIVB5yhutNDKuV8Q= From: Jinank Jain To: jinankjain@microsoft.com Cc: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, arnd@arndb.de, peterz@infradead.org, jpoimboe@kernel.org, jinankjain@linux.microsoft.com, seanjc@google.com, kirill.shutemov@linux.intel.com, ak@linux.intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: [PATCH v2 1/5] mshv: Add support for detecting nested hypervisor Date: Wed, 2 Nov 2022 16:35:58 +0000 Message-Id: <4200eb3ffd5453cc9c9812b05283f6c2d6a6bef5.1667406350.git.jinankjain@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748403308983257273?= X-GMAIL-MSGID: =?utf-8?q?1748403308983257273?= When Linux runs as a root partition for Microsoft Hypervisor. It is possible to detect if it is running as nested hypervisor using hints exposed by mshv. While at it expose a new variable called hv_nested which can be used later for making decisions specific to nested use case. Signed-off-by: Jinank Jain --- arch/x86/include/asm/hyperv-tlfs.h | 3 +++ arch/x86/include/asm/mshyperv.h | 2 ++ arch/x86/kernel/cpu/mshyperv.c | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index 3089ec352743..d9a611565859 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -114,6 +114,9 @@ /* Recommend using the newer ExProcessorMasks interface */ #define HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED BIT(11) +/* Indicates that the hypervisor is nested within a Hyper-V partition. */ +#define HV_X64_HYPERV_NESTED BIT(12) + /* Recommend using enlightened VMCS */ #define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED BIT(14) diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index 61f0c206bff0..29388567eafd 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -190,6 +190,8 @@ static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {} extern bool hv_isolation_type_snp(void); +extern bool hv_nested; + static inline bool hv_is_synic_reg(unsigned int reg) { if ((reg >= HV_REGISTER_SCONTROL) && diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 831613959a92..2555535f5237 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -37,6 +37,8 @@ /* Is Linux running as the root partition? */ bool hv_root_partition; +/* Is Linux running on nested Microsoft Hypervisor */ +bool hv_nested; struct ms_hyperv_info ms_hyperv; #if IS_ENABLED(CONFIG_HYPERV) @@ -301,6 +303,11 @@ static void __init ms_hyperv_init_platform(void) pr_info("Hyper-V: running as root partition\n"); } + if (ms_hyperv.hints & HV_X64_HYPERV_NESTED) { + hv_nested = true; + pr_info("Hyper-V: Linux running on a nested hypervisor\n"); + } + /* * Extract host information. */