From patchwork Wed Nov 8 11:17:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162955 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp841275vqo; Wed, 8 Nov 2023 03:18:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFEHHGmZJxd97Fmn12T7CDkGEeYM/MxcX2qvyGpt7y/lV+6U7kSvuEs48qcdDaUuYvpr8C+ X-Received: by 2002:a17:90b:1a8a:b0:280:e85a:9dde with SMTP id ng10-20020a17090b1a8a00b00280e85a9ddemr1558331pjb.33.1699442320588; Wed, 08 Nov 2023 03:18:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442320; cv=none; d=google.com; s=arc-20160816; b=HqdCTbSGdmB8DQVtINSQpUf25+yjnwXZIDfH0hbvS3kI4hXbW+y8vLwT40kdx+PKxB aaeYKmheRgZlRMXLwjwlGBxF4adF7c2/F9+8bNsetzfaSeeGtXCQf7zoydErWVyfEW1t 2dHd0Ac2yNpGZmhZUcv76ub2FKrfrljPWlr1zhxEwAghQafx2qspJrQbPwbXO+E309Id WkFj5t+96IbT6qCcdeKElBg6PMJ6mfWY13Vi2tPV+dmWxISOjHC4Vjw8ELt95lfonJPF Ib0CTo0tufpY4/MhMyc+BmgOFnaLNMOObyPnp6vQ/Osr6/3Ajk79hQpQ+C+Chme3AVyM LDsA== 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; bh=Uq8x7FLfoIR318BRKD0i4hgYzW68ONMdLEubh/+ZbLg=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=KDT0iScmQdUB2zqBRvlqEQAiBqMqQThO9h+tVhcdpDz3pPhJeyb8DahGtxlYbWHijd D18u//XiX3Vql9HirpiNatmXij2Pk2u6fzj31r+eynRlkl2CM/21IKHJKlDv2ckD09eY XFUqiJKTQPEXKe6dLnbTJzvgJcKDufu8JIrhRYDErbvtoum8wzI2AbPNyQrfXvhQtjST Ftt8un6fNc1CmpKHaKTl9hgZMl9097sAVEEBB2PIFkG9PdSTpwf+rY9cb2yvDv8p6LGj R6ONq91utai/8ZYuQJAwWL9MHh7ogGym0T/oks6r1XGu9zhKKRTg2mdhHC9Cj51Kv/+8 prUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=cY0+dRyP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id gm13-20020a17090b100d00b002803c4a0684si1963532pjb.189.2023.11.08.03.18.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:18:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=cY0+dRyP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 72CDF838B3C9; Wed, 8 Nov 2023 03:18:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344412AbjKHLSf (ORCPT + 32 others); Wed, 8 Nov 2023 06:18:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344383AbjKHLSe (ORCPT ); Wed, 8 Nov 2023 06:18:34 -0500 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7871186; Wed, 8 Nov 2023 03:18:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442313; x=1730978313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uq8x7FLfoIR318BRKD0i4hgYzW68ONMdLEubh/+ZbLg=; b=cY0+dRyPVwx/+v+HXR0Jhe77ryGubFD0miWlX3/F6w2LU8JJY03beq5H UlHqWKLtYvOXKD9YLBb5CoWwh+l9/3uWt2XKQauGXUdroYEFFOz7AAtFO G8lWw2z/bZWgPl7FrUHwDU7yi7pL+HkfHPkrhLjv/g2592ptsfSupSXi/ o=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="361602176" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-44b6fc51.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:18:29 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2a-m6i4x-44b6fc51.us-west-2.amazon.com (Postfix) with ESMTPS id 8BB26A07F7; Wed, 8 Nov 2023 11:18:26 +0000 (UTC) Received: from EX19MTAEUC001.ant.amazon.com [10.0.10.100:45371] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.26.101:2525] with esmtp (Farcaster) id 58210a6b-8421-43e3-b3a6-2ece76fce745; Wed, 8 Nov 2023 11:18:25 +0000 (UTC) X-Farcaster-Flow-ID: 58210a6b-8421-43e3-b3a6-2ece76fce745 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:25 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:20 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 01/33] KVM: x86: Decouple lapic.h from hyperv.h Date: Wed, 8 Nov 2023 11:17:34 +0000 Message-ID: <20231108111806.92604-2-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D037UWC004.ant.amazon.com (10.13.139.254) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:18:39 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994430427409273 X-GMAIL-MSGID: 1781994430427409273 lapic.h has no dependencies with hyperv.h, so don't include it there. Additionally, cpuid.c implicitly relied on hyperv.h's inclusion through lapic.h, so include it explicitly there. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/lapic.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 773132c3bf5a..eabd5e9dc003 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -28,6 +28,7 @@ #include "trace.h" #include "pmu.h" #include "xen.h" +#include "hyperv.h" /* * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 0a0ea4b5dd8c..e1021517cf04 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -6,7 +6,6 @@ #include -#include "hyperv.h" #include "smm.h" #define KVM_APIC_INIT 0 From patchwork Wed Nov 8 11:17:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162956 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp841604vqo; Wed, 8 Nov 2023 03:19:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGPsCUZ8s/EC3PL3OCDDAIBEMyWBDxWd7GenjXL6tt5FuiV8R4Hn4ZO6xNoJdp2cPYMLMjz X-Received: by 2002:a17:902:ea01:b0:1cc:f60:28b2 with SMTP id s1-20020a170902ea0100b001cc0f6028b2mr1927945plg.6.1699442361740; Wed, 08 Nov 2023 03:19:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442361; cv=none; d=google.com; s=arc-20160816; b=F0Momg2L7qz7RneA9Z0sncqhKxywU3tLCC3ygaHUr5hXXw7why2flNRaOVriW1p91P dyYHXVEbYppPQtVi5gntRaRgF3fN4ZKzEWfMC23c0bJSW24WYIXWj7C1N5V3mDiQh+6P 63+OoBkezbaXELEHEcl+eOctlENIfqYQZObA0tBUYqIksuKBdCCA6mFTq2GK2dAH369z euOTlrr0p9LjDg6dTQ18P+6YwOHZ8j8iuiAtv/8+sRU3I4PnFFmZbNPJqa1B63umCg5y xhXXPvFIEnRt3vJ4mWG+Htv6R1Oy/Bek59JQ2ZDlGFAGhaQGhzUauWi51XwEso7zm6w/ yDKA== 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; bh=S0vg453WVyLXU8F3J/4ltfll9IYzPE7UP39ZvRkTsE0=; fh=Vtfomk7p38jwhuURSX3QKdwx2HmjtcadOKHF122cnYY=; b=Pts0qPcgs3jrT4WR5QBSL5tdRks0PyEND31wniIOJ7XpZGOzPlHDuAym7/HZHYnSNO Sgd/uMkQsnC4ll67vDSTmF/3CF+5n7IWHwb171oUVnBDlXL8dVhm6N81AhDCh46SRuYD FKO2lbkIF7uDMxZgHXIvDrYf0e2u62TF87XuSo1vJX1VeJv81UO4gUReAxSkROVkLCNy mU1+tbd9N7rIUKlRcXczHf+Ki7XPA9H2rOYHgDM7iD4Yr9AINyP2aoPSa803BC7BjfZE Rw/3w6UQKqT5F1O2s1iLgVlJUVcEiAbeDuYTeKxP7e7oAK1KkYx1jiGU5rT9Lh7HxQfL a4Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=UD8ea73X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id s17-20020a170903321100b001cc2ed18253si1796026plh.15.2023.11.08.03.19.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:19:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=UD8ea73X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id AA3B4825548F; Wed, 8 Nov 2023 03:19:17 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344503AbjKHLSn (ORCPT + 32 others); Wed, 8 Nov 2023 06:18:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344466AbjKHLSl (ORCPT ); Wed, 8 Nov 2023 06:18:41 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 883921BD8; Wed, 8 Nov 2023 03:18:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442318; x=1730978318; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=S0vg453WVyLXU8F3J/4ltfll9IYzPE7UP39ZvRkTsE0=; b=UD8ea73Xzy/ZISUJsDmyzeYbm6jPHWFJiSf6gYE6ahEt2jUNt3AM5UPd Sn74+de783X/vxdRS3qR2RLRrnLMeWxsgXoqlriXMCvvttJFBS27wzX/a ZBzAUGDqeLYVigpXSl+Ko5djfyY6rT8956P1rIvnyeNwE0b6UsVpUuvE0 4=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366811567" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-1197e3af.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:18:33 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2a-m6i4x-1197e3af.us-west-2.amazon.com (Postfix) with ESMTPS id DD42C100386; Wed, 8 Nov 2023 11:18:31 +0000 (UTC) Received: from EX19MTAEUA001.ant.amazon.com [10.0.10.100:6644] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.22.222:2525] with esmtp (Farcaster) id 87b998a2-b394-4602-b733-977feede779e; Wed, 8 Nov 2023 11:18:30 +0000 (UTC) X-Farcaster-Flow-ID: 87b998a2-b394-4602-b733-977feede779e Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:30 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:25 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Anel Orazgaliyeva , "Nicolas Saenz Julienne" Subject: [RFC 02/33] KVM: x86: Introduce KVM_CAP_APIC_ID_GROUPS Date: Wed, 8 Nov 2023 11:17:35 +0000 Message-ID: <20231108111806.92604-3-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D037UWC004.ant.amazon.com (10.13.139.254) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:19:17 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994473775285067 X-GMAIL-MSGID: 1781994473775285067 From: Anel Orazgaliyeva Introduce KVM_CAP_APIC_ID_GROUPS, this capability segments the VM's APIC ids into two. The lower bits, the physical APIC id, represent the part that's exposed to the guest. The higher bits, which are private to KVM, groups APICs together. APICs in different groups are isolated from each other, and IPIs can only be directed at APICs that share the same group as its source. Furthermore, groups are only relevant to IPIs, anything incoming from outside the local APIC complex: from the IOAPIC, MSIs, or PV-IPIs is targeted at the default APIC group, group 0. When routing IPIs with physical destinations, KVM will OR the source's vCPU APIC group with the ICR's destination ID and use that to resolve the target lAPIC. The APIC physical map is also made group aware in order to speed up this process. For the sake of simplicity, the logical map is not built while KVM_CAP_APIC_ID_GROUPS is in use and we defer IPI routing to the slower per-vCPU scan method. This capability serves as a building block to implement virtualisation based security features like Hyper-V's Virtual Secure Mode (VSM). VSM introduces a para-virtualised switch that allows for guest CPUs to jump into a different execution context, this switches into a different CPU state, lAPIC state, and memory protections. We model this in KVM by using distinct kvm_vcpus for each context. Moreover, execution contexts are hierarchical and its APICs are meant to remain functional even when the context isn't 'scheduled in'. For example, we have to keep track of timers' expirations, and interrupt execution of lesser priority contexts when relevant. Hence the need to alias physical APIC ids, while keeping the ability to target specific execution contexts. Signed-off-by: Anel Orazgaliyeva Co-developed-by: Nicolas Saenz Julienne Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/include/uapi/asm/kvm.h | 5 +++ arch/x86/kvm/lapic.c | 59 ++++++++++++++++++++++++++++----- arch/x86/kvm/lapic.h | 33 ++++++++++++++++++ arch/x86/kvm/x86.c | 15 +++++++++ include/uapi/linux/kvm.h | 2 ++ 6 files changed, 108 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index dff10051e9b6..a2f224f95404 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1298,6 +1298,9 @@ struct kvm_arch { struct rw_semaphore apicv_update_lock; unsigned long apicv_inhibit_reasons; + u32 apic_id_group_mask; + u8 apic_id_group_shift; + gpa_t wall_clock; bool mwait_in_guest; diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index a448d0964fc0..f73d137784d7 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -565,4 +565,9 @@ struct kvm_pmu_event_filter { #define KVM_X86_DEFAULT_VM 0 #define KVM_X86_SW_PROTECTED_VM 1 +/* for KVM_SET_APIC_ID_GROUPS */ +struct kvm_apic_id_groups { + __u8 n_bits; /* nr of bits used to represent group in the APIC ID */ +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 3e977dbbf993..f55d216cb2a0 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -141,7 +141,7 @@ static inline int apic_enabled(struct kvm_lapic *apic) static inline u32 kvm_x2apic_id(struct kvm_lapic *apic) { - return apic->vcpu->vcpu_id; + return kvm_apic_id(apic->vcpu); } static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu) @@ -219,8 +219,8 @@ static int kvm_recalculate_phys_map(struct kvm_apic_map *new, bool *xapic_id_mismatch) { struct kvm_lapic *apic = vcpu->arch.apic; - u32 x2apic_id = kvm_x2apic_id(apic); - u32 xapic_id = kvm_xapic_id(apic); + u32 x2apic_id = kvm_apic_id_and_group(vcpu); + u32 xapic_id = kvm_apic_id_and_group(vcpu); u32 physical_id; /* @@ -299,6 +299,13 @@ static void kvm_recalculate_logical_map(struct kvm_apic_map *new, u16 mask; u32 ldr; + /* + * Using maps for logical destinations when KVM_CAP_APIC_ID_GRUPS is in + * use isn't supported. + */ + if (kvm_apic_group(vcpu)) + new->logical_mode = KVM_APIC_MODE_MAP_DISABLED; + if (new->logical_mode == KVM_APIC_MODE_MAP_DISABLED) return; @@ -370,6 +377,25 @@ enum { DIRTY }; +int kvm_vm_ioctl_set_apic_id_groups(struct kvm *kvm, + struct kvm_apic_id_groups *groups) +{ + u8 n_bits = groups->n_bits; + + if (n_bits > 32) + return -EINVAL; + + kvm->arch.apic_id_group_mask = n_bits ? GENMASK(31, 32 - n_bits): 0; + /* + * Bitshifts >= than the width of the type are UD, so set the + * apic group shift to 0 when n_bits == 0. The group mask above will + * clear the APIC ID, so group querying functions will return the + * correct value. + */ + kvm->arch.apic_id_group_shift = n_bits ? 32 - n_bits : 0; + return 0; +} + void kvm_recalculate_apic_map(struct kvm *kvm) { struct kvm_apic_map *new, *old = NULL; @@ -414,7 +440,7 @@ void kvm_recalculate_apic_map(struct kvm *kvm) kvm_for_each_vcpu(i, vcpu, kvm) if (kvm_apic_present(vcpu)) - max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic)); + max_id = max(max_id, kvm_apic_id_and_group(vcpu)); new = kvzalloc(sizeof(struct kvm_apic_map) + sizeof(struct kvm_lapic *) * ((u64)max_id + 1), @@ -525,7 +551,7 @@ static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id) { u32 ldr = kvm_apic_calc_x2apic_ldr(id); - WARN_ON_ONCE(id != apic->vcpu->vcpu_id); + WARN_ON_ONCE(id != kvm_apic_id(apic->vcpu)); kvm_lapic_set_reg(apic, APIC_ID, id); kvm_lapic_set_reg(apic, APIC_LDR, ldr); @@ -1067,6 +1093,17 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, struct kvm_lapic *target = vcpu->arch.apic; u32 mda = kvm_apic_mda(vcpu, dest, source, target); + /* + * Make sure vCPUs belong to the same APIC group, it's not possible + * to send interrupts across groups. + * + * Non-IPIs and PV-IPIs can only be injected into the default APIC + * group (group 0). + */ + if ((source && !kvm_match_apic_group(source->vcpu, vcpu)) || + kvm_apic_group(vcpu)) + return false; + ASSERT(target); switch (shorthand) { case APIC_DEST_NOSHORT: @@ -1518,6 +1555,10 @@ void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high) else irq.dest_id = GET_XAPIC_DEST_FIELD(icr_high); + if (irq.dest_mode == APIC_DEST_PHYSICAL) + kvm_apic_id_set_group(apic->vcpu->kvm, + kvm_apic_group(apic->vcpu), &irq.dest_id); + trace_kvm_apic_ipi(icr_low, irq.dest_id); kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL); @@ -2541,7 +2582,7 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) /* update jump label if enable bit changes */ if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) { if (value & MSR_IA32_APICBASE_ENABLE) { - kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); + kvm_apic_set_xapic_id(apic, kvm_apic_id(vcpu)); static_branch_slow_dec_deferred(&apic_hw_disabled); /* Check if there are APF page ready requests pending */ kvm_make_request(KVM_REQ_APF_READY, vcpu); @@ -2553,9 +2594,9 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) if ((old_value ^ value) & X2APIC_ENABLE) { if (value & X2APIC_ENABLE) - kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id); + kvm_apic_set_x2apic_id(apic, kvm_apic_id(vcpu)); else if (value & MSR_IA32_APICBASE_ENABLE) - kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); + kvm_apic_set_xapic_id(apic, kvm_apic_id(vcpu)); } if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) { @@ -2685,7 +2726,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) /* The xAPIC ID is set at RESET even if the APIC was already enabled. */ if (!init_event) - kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); + kvm_apic_set_xapic_id(apic, kvm_apic_id(vcpu)); kvm_apic_set_version(apic->vcpu); for (i = 0; i < apic->nr_lvt_entries; i++) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index e1021517cf04..542bd208e52b 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -97,6 +97,8 @@ void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu); void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value); u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu); +int kvm_vm_ioctl_set_apic_id_groups(struct kvm *kvm, + struct kvm_apic_id_groups *groups); void kvm_recalculate_apic_map(struct kvm *kvm); void kvm_apic_set_version(struct kvm_vcpu *vcpu); void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu); @@ -277,4 +279,35 @@ static inline u8 kvm_xapic_id(struct kvm_lapic *apic) return kvm_lapic_get_reg(apic, APIC_ID) >> 24; } +static inline u32 kvm_apic_id(struct kvm_vcpu *vcpu) +{ + return vcpu->vcpu_id & ~vcpu->kvm->arch.apic_id_group_mask; +} + +static inline u32 kvm_apic_id_and_group(struct kvm_vcpu *vcpu) +{ + return vcpu->vcpu_id; +} + +static inline u32 kvm_apic_group(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + + return (vcpu->vcpu_id & kvm->arch.apic_id_group_mask) >> + kvm->arch.apic_id_group_shift; +} + +static inline void kvm_apic_id_set_group(struct kvm *kvm, u32 group, + u32 *apic_id) +{ + *apic_id |= ((group << kvm->arch.apic_id_group_shift) & + kvm->arch.apic_id_group_mask); +} + +static inline bool kvm_match_apic_group(struct kvm_vcpu *src, + struct kvm_vcpu *dst) +{ + return kvm_apic_group(src) == kvm_apic_group(dst); +} + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e3eb608b6692..4cd3f00475c1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4526,6 +4526,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES: case KVM_CAP_IRQFD_RESAMPLE: case KVM_CAP_MEMORY_FAULT_INFO: + case KVM_CAP_APIC_ID_GROUPS: r = 1; break; case KVM_CAP_EXIT_HYPERCALL: @@ -7112,6 +7113,20 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm_vm_ioctl_set_msr_filter(kvm, &filter); break; } + case KVM_SET_APIC_ID_GROUPS: { + struct kvm_apic_id_groups groups; + + r = -EINVAL; + if (kvm->created_vcpus) + goto out; + + r = -EFAULT; + if (copy_from_user(&groups, argp, sizeof(groups))) + goto out; + + r = kvm_vm_ioctl_set_apic_id_groups(kvm, &groups); + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 5b5820d19e71..d7a01766bf21 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1219,6 +1219,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MEMORY_ATTRIBUTES 232 #define KVM_CAP_GUEST_MEMFD 233 #define KVM_CAP_VM_TYPES 234 +#define KVM_CAP_APIC_ID_GROUPS 235 #ifdef KVM_CAP_IRQ_ROUTING @@ -2307,4 +2308,5 @@ struct kvm_create_guest_memfd { #define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) +#define KVM_SET_APIC_ID_GROUPS _IOW(KVMIO, 0xd7, struct kvm_apic_id_groups) #endif /* __LINUX_KVM_H */ From patchwork Wed Nov 8 11:17:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162957 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp841622vqo; Wed, 8 Nov 2023 03:19:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFptcP35+d/VJ4SSKXdQBrM9rGhfhZ6RdyISWhfz6pvnRXuxn1ufSmtmj7UpPbWZqE3po6A X-Received: by 2002:a05:6a20:2453:b0:166:82cf:424a with SMTP id t19-20020a056a20245300b0016682cf424amr1610783pzc.33.1699442363796; Wed, 08 Nov 2023 03:19:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442363; cv=none; d=google.com; s=arc-20160816; b=LADmrpKVKIAFRyCI/lBOAHkpPaivJqz//3Kk29KZPajkKdXbKE2GeOYzmolKb9XV2P UWFxwAp8H+wzQY3ZAFzqqBGDphhcd517rnAj9r6EV3iN3x1vAG+dQQdy5c83Sz6hXY1f EuFxyjCMPe/tzFdx067AmcV/5N0PQJaJcH5lms+uFy8MTTCuPnYgWOLcVNMVns+ce+u8 hinFJ185wWT82HdMZ8mf9+KRhuHyMVLOl6UQV6cfn/0IB5gLsSs5kXkxsLsXLO5tPXcE TAwkFe78Nk9Uz7CJrcYuOgArOy8p1S88OwHSYIwgsoQWaTf6qIEnaf0QRAB/wjYWwChp xl5A== 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; bh=yuGMTR3q9ZG4fjpyArVTAZq+f+G14drPZZ43vxh56XA=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=dSA5pW6gjceX3FXnljQ679wtqFdaSd7T+B6UGRwHe+Dnne7VqMF3gFlAjMse/tzrlx OUAhA/dMd5a391ly1sW1aoN3zhdOFhmttLnZWgjvr/8nvIUdrtKiZEap8WoG2zRghtFU N9KLAgiih+unJpiP/+/3FzRbM+ghvv2iKaAOT2YPTErtkC2Qh0cmfIndFrs+ysY24sGB Atl2yj0hWT3FxCJMeJQRliGUAog1qv8wd+bUSsfSX1R2LA45pzojvWpViqkJs9ygUkq9 +9MGAMcWJ+40PYs2FWOFtUT1hWUrlgNH2sq594n4qRsynwIms5qnhScM3L+AEA6VshoD ae1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=H13rTqvg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id bq22-20020a056a02045600b005b87be63da6si5114797pgb.488.2023.11.08.03.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:19:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=H13rTqvg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E7C838075ED2; Wed, 8 Nov 2023 03:19:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344296AbjKHLTN (ORCPT + 32 others); Wed, 8 Nov 2023 06:19:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344402AbjKHLTK (ORCPT ); Wed, 8 Nov 2023 06:19:10 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFAB41729; Wed, 8 Nov 2023 03:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442349; x=1730978349; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yuGMTR3q9ZG4fjpyArVTAZq+f+G14drPZZ43vxh56XA=; b=H13rTqvgCM+7YyMNXgDOxbs3kVLjOhs1p0gn6psav2wPbvMurt4ONC7a 9JBZiA6VsFjY47es/uCk7SuO/5d+3CQCPgGlFszUvbcsbj6VkgFMEBUxB J7GhMMBld7elvG/ql3A5P47afHnD/sqb0oaWnnVmRPyfZvpahETo7rJri 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164958711" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:02 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com (Postfix) with ESMTPS id 6B97681C1A; Wed, 8 Nov 2023 11:19:00 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.10.100:26732] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.10.247:2525] with esmtp (Farcaster) id 051368ee-dbbf-415b-be33-7d8aaa805a15; Wed, 8 Nov 2023 11:18:59 +0000 (UTC) X-Farcaster-Flow-ID: 051368ee-dbbf-415b-be33-7d8aaa805a15 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:59 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:54 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 03/33] KVM: x86: hyper-v: Introduce XMM output support Date: Wed, 8 Nov 2023 11:17:36 +0000 Message-ID: <20231108111806.92604-4-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D036UWB003.ant.amazon.com (10.13.139.172) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:19:21 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994476033445946 X-GMAIL-MSGID: 1781994476033445946 Prepare infrastructure to be able to return data through the XMM registers when Hyper-V hypercalls are issues in fast mode. The XMM registers are exposed to user-space through KVM_EXIT_HYPERV_HCALL and restored on successful hypercall completion. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/hyperv-tlfs.h | 2 +- arch/x86/kvm/hyperv.c | 33 +++++++++++++++++++++++++++++- include/uapi/linux/kvm.h | 6 ++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index 2ff26f53cd62..af594aa65307 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -49,7 +49,7 @@ /* Support for physical CPU dynamic partitioning events is available*/ #define HV_X64_CPU_DYNAMIC_PARTITIONING_AVAILABLE BIT(3) /* - * Support for passing hypercall input parameter block via XMM + * Support for passing hypercall input and output parameter block via XMM * registers is available */ #define HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE BIT(4) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 238afd7335e4..e1bc861ab3b0 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1815,6 +1815,7 @@ struct kvm_hv_hcall { u16 rep_idx; bool fast; bool rep; + bool xmm_dirty; sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS]; /* @@ -2346,9 +2347,33 @@ static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) return ret; } +static void kvm_hv_write_xmm(struct kvm_hyperv_xmm_reg *xmm) +{ + int reg; + + kvm_fpu_get(); + for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++) { + const sse128_t data = sse128(xmm[reg].low, xmm[reg].high); + _kvm_write_sse_reg(reg, &data); + } + kvm_fpu_put(); +} + +static bool kvm_hv_is_xmm_output_hcall(u16 code) +{ + return false; +} + static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) { - return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result); + bool fast = !!(vcpu->run->hyperv.u.hcall.input & HV_HYPERCALL_FAST_BIT); + u16 code = vcpu->run->hyperv.u.hcall.input & 0xffff; + u64 result = vcpu->run->hyperv.u.hcall.result; + + if (kvm_hv_is_xmm_output_hcall(code) && hv_result_success(result) && fast) + kvm_hv_write_xmm(vcpu->run->hyperv.u.hcall.xmm); + + return kvm_hv_hypercall_complete(vcpu, result); } static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) @@ -2623,6 +2648,9 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) break; } + if ((ret & HV_HYPERCALL_RESULT_MASK) == HV_STATUS_SUCCESS && hc.xmm_dirty) + kvm_hv_write_xmm((struct kvm_hyperv_xmm_reg*)hc.xmm); + hypercall_complete: return kvm_hv_hypercall_complete(vcpu, ret); @@ -2632,6 +2660,8 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) vcpu->run->hyperv.u.hcall.input = hc.param; vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; + if (hc.fast) + memcpy(vcpu->run->hyperv.u.hcall.xmm, hc.xmm, sizeof(hc.xmm)); vcpu->arch.complete_userspace_io = kvm_hv_hypercall_complete_userspace; return 0; } @@ -2780,6 +2810,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS; ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; + ent->edx |= HV_X64_HYPERCALL_XMM_OUTPUT_AVAILABLE; ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index d7a01766bf21..5ce06a1eee2b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -192,6 +192,11 @@ struct kvm_s390_cmma_log { __u64 values; }; +struct kvm_hyperv_xmm_reg { + __u64 low; + __u64 high; +}; + struct kvm_hyperv_exit { #define KVM_EXIT_HYPERV_SYNIC 1 #define KVM_EXIT_HYPERV_HCALL 2 @@ -210,6 +215,7 @@ struct kvm_hyperv_exit { __u64 input; __u64 result; __u64 params[2]; + struct kvm_hyperv_xmm_reg xmm[6]; } hcall; struct { __u32 msr; From patchwork Wed Nov 8 11:17:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162963 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842904vqo; Wed, 8 Nov 2023 03:21:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IERo4KApWSEl2jBRPFdyAMhDP0aT1/Hw+2P/cgIpLaxXkxnRIVi/hrq4vHF9k02nlYlRHFm X-Received: by 2002:a17:903:244b:b0:1cc:644a:211b with SMTP id l11-20020a170903244b00b001cc644a211bmr1892356pls.47.1699442514386; Wed, 08 Nov 2023 03:21:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442514; cv=none; d=google.com; s=arc-20160816; b=Y6ri8vm3xfe31lVcvqgCwfHHqvvfymE/SgLjj2ehBsQd8UdPZBEHjTmHYYhr5OI375 uPdE1/nJHpJfRAXE2esU/KFwi4o+NZy/XfH3Iup4gxN7jxR2F3NU0F2zLmPwdn73bPXm P1CGTEdusq36WUs/dR+cI6Gygn/dVIsM+c6Xac77gy9cavCX7XaQv0m8NUTxJ0Sz+gHK zatxnEh4SwsBZhQ6d04fX3ZLnWcC5amUcO4EWcIuzvgKBUd2DXxhd/PZ8UKj09gQvkc2 2VFh8lHtzhE1TpOWNXjs5iCdvshS1Yta86S5IxxganJulvAULkNAExJrcfLdMC+Qcx9d eknA== 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; bh=G8tv4EqIfPFJRUiW9cZCsErULQwL6MkqMdyO6PRnfzY=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=lSbnQonK3FuAEaJKfydvGD8V5tTAfflNbbPGt87yvhjd0uVHEG/g49YDgdzNM7ruzI KxzR9HIzleI1De8mD+QsU3ik/ruQ9Y2o3LljWQ9xS1FWK8apFwjCigQQ6W6buJC2A6w9 8QRmVZ+iHRo5D7usLa+9HMjw52tlIDNFabe2shXjCgGxf41KyujYyA5PlSy0QqCQznzq f+n13MGeJKYhrsnHdOd8CfplSp6am7Q10IER40Xyn67P0vT4ZwZyoHwXEpMlKy4TNY8G CZvnnX1cwzsOgTxlcy7rWLWYIoGIL+sEd/B86Ore1BtDI8PXLsV88cvymWvrtLIrucDF f4OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=TNe4BukL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id kg14-20020a170903060e00b001cc58f174fasi1835911plb.364.2023.11.08.03.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=TNe4BukL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 918E3807E792; Wed, 8 Nov 2023 03:20:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344609AbjKHLTe (ORCPT + 32 others); Wed, 8 Nov 2023 06:19:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344455AbjKHLTR (ORCPT ); Wed, 8 Nov 2023 06:19:17 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45B491729; Wed, 8 Nov 2023 03:19:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442355; x=1730978355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G8tv4EqIfPFJRUiW9cZCsErULQwL6MkqMdyO6PRnfzY=; b=TNe4BukLRNg0YJfWA90/7JIByXU+dM2VDsbXlcy/qOnFybyCFrYRcHD8 ZQ2wY9eXVgY73blHeJws0Szlh9DQPrDdIa28YsluQgaSq9LIeTWGrNCzO gSWADZe4RXHIN6DqUxD2pE+OXoWeGGMtdU66EPFsK+1fAs3dn0w0wwAlm U=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="251427581" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:09 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com (Postfix) with ESMTPS id 8CC3F8057A; Wed, 8 Nov 2023 11:19:05 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.43.254:28968] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.17.103:2525] with esmtp (Farcaster) id a4bb631b-a22d-4d34-9086-5dcfe9a4daf9; Wed, 8 Nov 2023 11:19:04 +0000 (UTC) X-Farcaster-Flow-ID: a4bb631b-a22d-4d34-9086-5dcfe9a4daf9 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:04 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:59 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 04/33] KVM: x86: hyper-v: Move hypercall page handling into separate function Date: Wed, 8 Nov 2023 11:17:37 +0000 Message-ID: <20231108111806.92604-5-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D036UWB003.ant.amazon.com (10.13.139.172) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:22 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994633836850460 X-GMAIL-MSGID: 1781994633836850460 The hypercall page patching is about to grow considerably, move it into its own function. No functional change intended. Signed-off-by: Nicolas Saenz Julienne Reviewed-by: Maxim Levitsky --- arch/x86/kvm/hyperv.c | 69 ++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index e1bc861ab3b0..78d053042667 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -256,6 +256,42 @@ static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) kvm_make_request(KVM_REQ_HV_EXIT, vcpu); } +static int patch_hypercall_page(struct kvm_vcpu *vcpu, u64 data) +{ + struct kvm *kvm = vcpu->kvm; + u8 instructions[9]; + int i = 0; + u64 addr; + + /* + * If Xen and Hyper-V hypercalls are both enabled, disambiguate + * the same way Xen itself does, by setting the bit 31 of EAX + * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just + * going to be clobbered on 64-bit. + */ + if (kvm_xen_hypercall_enabled(kvm)) { + /* orl $0x80000000, %eax */ + instructions[i++] = 0x0d; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x80; + } + + /* vmcall/vmmcall */ + static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); + i += 3; + + /* ret */ + ((unsigned char *)instructions)[i++] = 0xc3; + + addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; + if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) + return 1; + + return 0; +} + static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 data, bool host) { @@ -1338,11 +1374,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, if (!hv->hv_guest_os_id) hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; break; - case HV_X64_MSR_HYPERCALL: { - u8 instructions[9]; - int i = 0; - u64 addr; - + case HV_X64_MSR_HYPERCALL: /* if guest os id is not set hypercall should remain disabled */ if (!hv->hv_guest_os_id) break; @@ -1351,34 +1383,11 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, break; } - /* - * If Xen and Hyper-V hypercalls are both enabled, disambiguate - * the same way Xen itself does, by setting the bit 31 of EAX - * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just - * going to be clobbered on 64-bit. - */ - if (kvm_xen_hypercall_enabled(kvm)) { - /* orl $0x80000000, %eax */ - instructions[i++] = 0x0d; - instructions[i++] = 0x00; - instructions[i++] = 0x00; - instructions[i++] = 0x00; - instructions[i++] = 0x80; - } - - /* vmcall/vmmcall */ - static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); - i += 3; - - /* ret */ - ((unsigned char *)instructions)[i++] = 0xc3; - - addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; - if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) + if (patch_hypercall_page(vcpu, data)) return 1; + hv->hv_hypercall = data; break; - } case HV_X64_MSR_REFERENCE_TSC: hv->hv_tsc_page = data; if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) { From patchwork Wed Nov 8 11:17:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp841907vqo; Wed, 8 Nov 2023 03:19:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFztnI1L31V5vQuZ5Fkk37t1iMYQ3CBpZIKMSpLUKPH1FWZBFvA2FM5WVTEyIRj7lRx6CH0 X-Received: by 2002:a05:6358:7f13:b0:169:98e4:af37 with SMTP id p19-20020a0563587f1300b0016998e4af37mr1277931rwn.24.1699442396086; Wed, 08 Nov 2023 03:19:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442396; cv=none; d=google.com; s=arc-20160816; b=j1eEtZEq1Q7/LTF8kahePHLK0Psrsamu9yruHxvYIRJjybMNg8J4OAAVTaYIQkwhg0 Auuv4d34uv7KfyIA7Pm+apyGOTAtSepz7sQt7L9xSZCc75jBogBrpg5XJGaGxPuugX5O IEXbYRWm5jR1BwxI0OarcXUXLW8faN3KBeO18PoXMGOPlt3/ZJB90ucu0rHeRuN8T34S cKBUh/aNNMI7JyF94wB6/CwdfGgMcpKwwS28Xw3ZkdoHLCT/hCNZi9rZvmY0zlBqrjqD gWPjv6BZdugQmMUbvKpJX/G7MeLSeREIoYFfTjLWeZh9Std9sg8smXNfZ/1XvLExRRF2 8BRw== 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; bh=Xwm80GfbjkdS94nlx2StfaEHABGwZZtCfK9AfoMHOMQ=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=MK7BzDqbTFiwFy7E4KV2b59M8mrhDCbei7h3b9B5XD/7xYdR+PW3fJGcaSJWUE2qnf CrZKRnWizn41spKKbopKLMI7V1PSJVMt+5zUoJBuKY2W0S60j6nbaagI9pr9EKAvN1/e 0fCEcEwDz9UuNaBLXt/8BfedW79TBSqwXLhzQY5XRAQ8s3eUJLoXwABeqr5RVMGLC27W qmc/LmrScNdyPovoo2NS/DHuzCTpX2NB6CbOrdz8UzXzQ3anh7ET9c+IB0srR/qvucKr eFu1tJvFJXON74p7xbNBlpBV83S7f6UmoMlSnvdsbwrF9RHF2Uoxr4G5WNmYMJ71qISL q5Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="YPK5Up4/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id b1-20020a655cc1000000b00569fd44093fsi4166457pgt.230.2023.11.08.03.19.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:19:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="YPK5Up4/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id EA21A8256C86; Wed, 8 Nov 2023 03:19:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344473AbjKHLT3 (ORCPT + 32 others); Wed, 8 Nov 2023 06:19:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344482AbjKHLTU (ORCPT ); Wed, 8 Nov 2023 06:19:20 -0500 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D64519BD; Wed, 8 Nov 2023 03:19:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442356; x=1730978356; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xwm80GfbjkdS94nlx2StfaEHABGwZZtCfK9AfoMHOMQ=; b=YPK5Up4/JXj73WVhsq8zCU9Ya+YBhJDPIMsH7BJvTzQnBqvsL1mXOfrH +dztxjHsnisUYBzKgrPs9+ni7x+c1/Gs/lt/hhwumU30MMqti7MYK26fm NYp62hyOQUAfkIBX4sRNlukC0abaP3+4Za61ENg97K5f3xei7i9TFeQZu A=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="614865362" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:12 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com (Postfix) with ESMTPS id 9D678804AE; Wed, 8 Nov 2023 11:19:10 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.17.79:22087] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.32.187:2525] with esmtp (Farcaster) id d33121c7-11f2-4715-adf8-59384f014902; Wed, 8 Nov 2023 11:19:09 +0000 (UTC) X-Farcaster-Flow-ID: d33121c7-11f2-4715-adf8-59384f014902 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:09 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:04 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 05/33] KVM: x86: hyper-v: Introduce VTL call/return prologues in hypercall page Date: Wed, 8 Nov 2023 11:17:38 +0000 Message-ID: <20231108111806.92604-6-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D036UWB003.ant.amazon.com (10.13.139.172) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:19:48 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994509571269228 X-GMAIL-MSGID: 1781994509571269228 VTL call/return hypercalls have their own entry points in the hypercall page because they don't follow normal hyper-v hypercall conventions. Move the VTL call/return control input into ECX/RAX and set the hypercall code into EAX/RCX before calling the hypercall instruction in order to be able to use the Hyper-V hypercall entry function. Guests can read an emulated code page offsets register to know the offsets into the hypercall page for the VTL call/return entries. Signed-off-by: Nicolas Saenz Julienne --- My tree has the additional patch, we're still trying to understand under what conditions Windows expects the offset to be fixed. diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 54f7f36a89bf..9f2ea8c34447 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -294,6 +294,7 @@ static int patch_hypercall_page(struct kvm_vcpu *vcpu, u64 data) /* VTL call/return entries */ if (!kvm_xen_hypercall_enabled(kvm) && kvm_hv_vsm_enabled(kvm)) { + i = 22; #ifdef CONFIG_X86_64 if (is_64_bit_mode(vcpu)) { /* --- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/hyperv.c | 78 ++++++++++++++++++++++++++++++- include/asm-generic/hyperv-tlfs.h | 11 +++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index a2f224f95404..00cd21b09f8c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1105,6 +1105,8 @@ struct kvm_hv { u64 hv_tsc_emulation_status; u64 hv_invtsc_control; + union hv_register_vsm_code_page_offsets vsm_code_page_offsets; + /* How many vCPUs have VP index != vCPU index */ atomic_t num_mismatched_vp_indexes; diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 78d053042667..d4b1b53ea63d 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -259,7 +259,8 @@ static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) static int patch_hypercall_page(struct kvm_vcpu *vcpu, u64 data) { struct kvm *kvm = vcpu->kvm; - u8 instructions[9]; + struct kvm_hv *hv = to_kvm_hv(kvm); + u8 instructions[0x30]; int i = 0; u64 addr; @@ -285,6 +286,81 @@ static int patch_hypercall_page(struct kvm_vcpu *vcpu, u64 data) /* ret */ ((unsigned char *)instructions)[i++] = 0xc3; + /* VTL call/return entries */ + if (!kvm_xen_hypercall_enabled(kvm) && kvm_hv_vsm_enabled(kvm)) { +#ifdef CONFIG_X86_64 + if (is_64_bit_mode(vcpu)) { + /* + * VTL call 64-bit entry prologue: + * mov %rcx, %rax + * mov $0x11, %ecx + * jmp 0: + */ + hv->vsm_code_page_offsets.vtl_call_offset = i; + instructions[i++] = 0x48; + instructions[i++] = 0x89; + instructions[i++] = 0xc8; + instructions[i++] = 0xb9; + instructions[i++] = 0x11; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0xeb; + instructions[i++] = 0xe0; + /* + * VTL return 64-bit entry prologue: + * mov %rcx, %rax + * mov $0x12, %ecx + * jmp 0: + */ + hv->vsm_code_page_offsets.vtl_return_offset = i; + instructions[i++] = 0x48; + instructions[i++] = 0x89; + instructions[i++] = 0xc8; + instructions[i++] = 0xb9; + instructions[i++] = 0x12; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0xeb; + instructions[i++] = 0xd6; + } else +#endif + { + /* + * VTL call 32-bit entry prologue: + * mov %eax, %ecx + * mov $0x11, %eax + * jmp 0: + */ + hv->vsm_code_page_offsets.vtl_call_offset = i; + instructions[i++] = 0x89; + instructions[i++] = 0xc1; + instructions[i++] = 0xb8; + instructions[i++] = 0x11; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0xeb; + instructions[i++] = 0xf3; + /* + * VTL return 32-bit entry prologue: + * mov %eax, %ecx + * mov $0x12, %eax + * jmp 0: + */ + hv->vsm_code_page_offsets.vtl_return_offset = i; + instructions[i++] = 0x89; + instructions[i++] = 0xc1; + instructions[i++] = 0xb8; + instructions[i++] = 0x12; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0xeb; + instructions[i++] = 0xea; + } + } addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) return 1; diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index fdac4a1714ec..0e7643c1ef01 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -823,4 +823,15 @@ struct hv_mmio_write_input { u8 data[HV_HYPERCALL_MMIO_MAX_DATA_LENGTH]; } __packed; +/* + * VTL call/return hypercall page offsets register + */ +union hv_register_vsm_code_page_offsets { + u64 as_u64; + struct { + u64 vtl_call_offset:12; + u64 vtl_return_offset:12; + u64 reserved:40; + } __packed; +}; #endif From patchwork Wed Nov 8 11:17:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162959 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842200vqo; Wed, 8 Nov 2023 03:20:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IEFN0wY7EMrCALp69Pwa3TFUic1Lsy6++HgvZLaFBPih8t/Y782bnNPDKAFHbEbjnxE10ZH X-Received: by 2002:a17:903:1109:b0:1c3:c687:478c with SMTP id n9-20020a170903110900b001c3c687478cmr2050469plh.8.1699442429885; Wed, 08 Nov 2023 03:20:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442429; cv=none; d=google.com; s=arc-20160816; b=AfziE5leuGoIf+qOZKxKrDFdxHyJofRBOPRW4atZkUZ3+2AqsPVnLEhSNfxRZC4eqf iqz3SLNA0EahDXS20mcmS7sqvSI8n4KPCWteAyNBJe1/x1niO0PgKIvQsA7ZWzOp4qga FbkT7hXeqFyVCNHZ3CM9G8GPnVyxa0FLrpHiHYfkpztvcD9MpZ2fh6o5orzd3BP5z7FR FDQUEEWhqAga3a4zlsWJv/qBj3fTdYOROJf09WF8kAKOmxFTZxCGmyK0x74krAP/2Ew1 dGXYq7wH6OtaoLe40/9VuDRZEI0kcbxoIRr4hc41mYuV2rzDSWlQ9XWapccQDl02v9QW 4v0Q== 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; bh=wig6s7B83KYNLTquljMX0q4Ptac31Xr4L9axIrcRcNM=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=0tXoaK4ZwHBhryiboLpsN6IacR0o/x15y10gquZSNBVlo3va7PKWCaQcbC2qX+s0eg Zq3diCddAsu9sskdNWguj4R1jTsfrPqpVzSX6HGFLhyIilLHBa++XDqZm/z1F3Y6c9NP izSeQ4NsXB/PGBXDJQrSVjxjZUMlYWIUdyOCRZV2TPcb7JZuiBZRsVGfyADTqYa6iBei 576UwGUlvue9x7wqvNvbZqRp1R5nLW6jLC0XNS4xniST98UrcSkSWxPNp7g4PJ/sdrx0 1TamFO7arIybbK1ZCESNKuVx+qggRsC131j+RPPM5fceEMj6xGeiPJB86hYyJ5dgnhhR QPXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=M4wgJ4HB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id c13-20020a170902d48d00b001bb3bcd05bbsi1825381plg.471.2023.11.08.03.20.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:20:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=M4wgJ4HB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id EBF7D8026893; Wed, 8 Nov 2023 03:20:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344193AbjKHLTz (ORCPT + 32 others); Wed, 8 Nov 2023 06:19:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344652AbjKHLTr (ORCPT ); Wed, 8 Nov 2023 06:19:47 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8CD11FCC; Wed, 8 Nov 2023 03:19:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442385; x=1730978385; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wig6s7B83KYNLTquljMX0q4Ptac31Xr4L9axIrcRcNM=; b=M4wgJ4HB1LfCwKdtqmsclWizgthienECE/g9Em3VyZhPBSLDHlnUTXEH hk31EPxUkZA9JSu4lpki0afzSqaXpiVA03u0zmsxVj4TG2XTcKoky/wP9 FFJ2SW5xAi6oB9Lhwu2MDYkkJWye8QUQHOyU0bP7vbKNipDI8Q0sV2TBs U=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366812182" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-d2040ec1.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:43 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2c-m6i4x-d2040ec1.us-west-2.amazon.com (Postfix) with ESMTPS id CBF6E40DAE; Wed, 8 Nov 2023 11:19:41 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.43.254:55579] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.32.187:2525] with esmtp (Farcaster) id a8c00b42-a893-40dc-869b-f1f46fa9119f; Wed, 8 Nov 2023 11:19:40 +0000 (UTC) X-Farcaster-Flow-ID: a8c00b42-a893-40dc-869b-f1f46fa9119f Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:37 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:33 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 06/33] KVM: x86: hyper-v: Introduce VTL awareness to Hyper-V's PV-IPIs Date: Wed, 8 Nov 2023 11:17:39 +0000 Message-ID: <20231108111806.92604-7-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D033UWC002.ant.amazon.com (10.13.139.196) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:27 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994545077248003 X-GMAIL-MSGID: 1781994545077248003 HVCALL_SEND_IPI and HVCALL_SEND_IPI_EX allow targeting specific a specific VTL. Honour the requests. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 24 +++++++++++++++++------- arch/x86/kvm/trace.h | 20 ++++++++++++-------- include/asm-generic/hyperv-tlfs.h | 6 ++++-- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index d4b1b53ea63d..2cf430f6ddd8 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2230,7 +2230,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) } static void kvm_hv_send_ipi_to_many(struct kvm *kvm, u32 vector, - u64 *sparse_banks, u64 valid_bank_mask) + u64 *sparse_banks, u64 valid_bank_mask, int vtl) { struct kvm_lapic_irq irq = { .delivery_mode = APIC_DM_FIXED, @@ -2245,6 +2245,9 @@ static void kvm_hv_send_ipi_to_many(struct kvm *kvm, u32 vector, valid_bank_mask, sparse_banks)) continue; + if (kvm_hv_get_active_vtl(vcpu) != vtl) + continue; + /* We fail only when APIC is disabled */ kvm_apic_set_irq(vcpu, &irq, NULL); } @@ -2257,13 +2260,19 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) struct kvm *kvm = vcpu->kvm; struct hv_send_ipi_ex send_ipi_ex; struct hv_send_ipi send_ipi; + union hv_input_vtl *in_vtl; u64 valid_bank_mask; u32 vector; bool all_cpus; + u8 vtl; + + /* VTL is at the same offset on both IPI types */ + in_vtl = &send_ipi.in_vtl; + vtl = in_vtl->use_target_vtl ? in_vtl->target_vtl : kvm_hv_get_active_vtl(vcpu); if (hc->code == HVCALL_SEND_IPI) { if (!hc->fast) { - if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi, + if (unlikely(kvm_vcpu_read_guest(vcpu, hc->ingpa, &send_ipi, sizeof(send_ipi)))) return HV_STATUS_INVALID_HYPERCALL_INPUT; sparse_banks[0] = send_ipi.cpu_mask; @@ -2278,10 +2287,10 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) all_cpus = false; valid_bank_mask = BIT_ULL(0); - trace_kvm_hv_send_ipi(vector, sparse_banks[0]); + trace_kvm_hv_send_ipi(vector, sparse_banks[0], vtl); } else { if (!hc->fast) { - if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex, + if (unlikely(kvm_vcpu_read_guest(vcpu, hc->ingpa, &send_ipi_ex, sizeof(send_ipi_ex)))) return HV_STATUS_INVALID_HYPERCALL_INPUT; } else { @@ -2292,7 +2301,8 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector, send_ipi_ex.vp_set.format, - send_ipi_ex.vp_set.valid_bank_mask); + send_ipi_ex.vp_set.valid_bank_mask, + vtl); vector = send_ipi_ex.vector; valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask; @@ -2322,9 +2332,9 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) return HV_STATUS_INVALID_HYPERCALL_INPUT; if (all_cpus) - kvm_hv_send_ipi_to_many(kvm, vector, NULL, 0); + kvm_hv_send_ipi_to_many(kvm, vector, NULL, 0, vtl); else - kvm_hv_send_ipi_to_many(kvm, vector, sparse_banks, valid_bank_mask); + kvm_hv_send_ipi_to_many(kvm, vector, sparse_banks, valid_bank_mask, vtl); ret_success: return HV_STATUS_SUCCESS; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 83843379813e..ab8839c47bc7 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1606,42 +1606,46 @@ TRACE_EVENT(kvm_hv_flush_tlb_ex, * Tracepoints for kvm_hv_send_ipi. */ TRACE_EVENT(kvm_hv_send_ipi, - TP_PROTO(u32 vector, u64 processor_mask), - TP_ARGS(vector, processor_mask), + TP_PROTO(u32 vector, u64 processor_mask, u8 vtl), + TP_ARGS(vector, processor_mask, vtl), TP_STRUCT__entry( __field(u32, vector) __field(u64, processor_mask) + __field(u8, vtl) ), TP_fast_assign( __entry->vector = vector; __entry->processor_mask = processor_mask; + __entry->vtl = vtl; ), - TP_printk("vector %x processor_mask 0x%llx", - __entry->vector, __entry->processor_mask) + TP_printk("vector %x processor_mask 0x%llx vtl %d", + __entry->vector, __entry->processor_mask, __entry->vtl) ); TRACE_EVENT(kvm_hv_send_ipi_ex, - TP_PROTO(u32 vector, u64 format, u64 valid_bank_mask), - TP_ARGS(vector, format, valid_bank_mask), + TP_PROTO(u32 vector, u64 format, u64 valid_bank_mask, u8 vtl), + TP_ARGS(vector, format, valid_bank_mask, vtl), TP_STRUCT__entry( __field(u32, vector) __field(u64, format) __field(u64, valid_bank_mask) + __field(u8, vtl) ), TP_fast_assign( __entry->vector = vector; __entry->format = format; __entry->valid_bank_mask = valid_bank_mask; + __entry->vtl = vtl; ), - TP_printk("vector %x format %llx valid_bank_mask 0x%llx", + TP_printk("vector %x format %llx valid_bank_mask 0x%llx vtl %d", __entry->vector, __entry->format, - __entry->valid_bank_mask) + __entry->valid_bank_mask, __entry->vtl) ); TRACE_EVENT(kvm_pv_tlb_flush, diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index 0e7643c1ef01..40d7dc793c03 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -424,14 +424,16 @@ struct hv_vpset { /* HvCallSendSyntheticClusterIpi hypercall */ struct hv_send_ipi { u32 vector; - u32 reserved; + union hv_input_vtl in_vtl; + u8 reserved[3]; u64 cpu_mask; } __packed; /* HvCallSendSyntheticClusterIpiEx hypercall */ struct hv_send_ipi_ex { u32 vector; - u32 reserved; + union hv_input_vtl in_vtl; + u8 reserved[3]; struct hv_vpset vp_set; } __packed; From patchwork Wed Nov 8 11:17:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162965 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842953vqo; Wed, 8 Nov 2023 03:21:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IELQnXZ+4gkI3pbTzIDSuokeTwo5NiKb0TKJ06JUI42dgvmsEhWEndm/38lm1bQY3/PlxsT X-Received: by 2002:a05:6358:7245:b0:169:9859:ccc3 with SMTP id i5-20020a056358724500b001699859ccc3mr1173307rwa.12.1699442518094; Wed, 08 Nov 2023 03:21:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442518; cv=none; d=google.com; s=arc-20160816; b=AE9ZwMBklh5vLQo7ZAUzhYZ4t5eoKYFSfDCkiouy6N7I0a3IcjG6kEbolXW4ebU9Kp 6WOg6vEM2lTxynKl7ObiMxa7mIh5fVK8MB4xDfRO6hBeCsuSaQfhbiSf6Ev8dLGnQyoU mgufL43pl+nQ1/atESOXdqgEhiJnWjBeWmpiXHb8qrl/xjdPVUKSL1iEOclxeJvnPGmi ZplatsOvHAiBDEgoXisGRUVSNm6A77q6/ZH3xzl5QpocPToGt8hllul7bR8I2N+tdubN h5kzZXFWQrxRiHNqLYyMarkZxjZbi5xtoh1b8GpJMRb/vY2OSysMCRxZCD+QxchFadAF tNrg== 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; bh=LwvxkM973QXPotqf1BmHlmaFhGY0KPkEa30ixGz574A=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=RIiyGluKdoCpjC/j00FhGDzAGbXtiDID5Nh74QJnGRpy5Awfbw96rBWPgN/9yPCTUX f0XZXHp5GQKDKCfFVSZMl4G34CTvntQL9zl/XhIFUXYuZnyeSQkfxSr7to/23mm+422g ZHNv1VUhLYhQmC6RMLwjRKLO1fQ4aIbn4++EdgzbT2VXSrQWRFbwHhT/JWIivOOJ7+di 9kaRbwrMLCr2cZ/bpreTh8X8zb4xrbvtbQK9KBU+8LHhZTV9uEXCuGo6JEDvoo5eJvwB NXk7G9Qwv2n1DFKpozW3GbWRyfq4Fg9MC32gnEA5eQhWzxK3Nyt0PVEjeCP9H/5vDQw9 XWBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sE+JHi5R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id a6-20020a656046000000b005859c81f1e9si5078475pgp.229.2023.11.08.03.21.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sE+JHi5R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8A5FD808463B; Wed, 8 Nov 2023 03:20:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344598AbjKHLUH (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344455AbjKHLT4 (ORCPT ); Wed, 8 Nov 2023 06:19:56 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1FAE1BD4; Wed, 8 Nov 2023 03:19:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442395; x=1730978395; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LwvxkM973QXPotqf1BmHlmaFhGY0KPkEa30ixGz574A=; b=sE+JHi5RRfWD6k0hVhdwIFulDOEIzbEvBk1b20Dtzd+LgoeZDDXT9ZP1 rK/bYKS8hJlfuxIMXlpuQXE+F0CufSmv3NYAAFrFYY4cesWml5X+UXPTa 83aWhUFHSCCL1xxv2imR9frXrfMK5SvUJ0bjsPDxo6zGpfALx9bFAhamv 0=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164958920" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-b5bd57cf.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:53 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-b5bd57cf.us-east-1.amazon.com (Postfix) with ESMTPS id DDFEC48DA2; Wed, 8 Nov 2023 11:19:49 +0000 (UTC) Received: from EX19MTAEUC001.ant.amazon.com [10.0.10.100:37568] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.45.210:2525] with esmtp (Farcaster) id b100eba5-8ee9-421b-9077-66ac2c5bb9c6; Wed, 8 Nov 2023 11:19:48 +0000 (UTC) X-Farcaster-Flow-ID: b100eba5-8ee9-421b-9077-66ac2c5bb9c6 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:42 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:38 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 07/33] KVM: x86: hyper-v: Introduce KVM_CAP_HYPERV_VSM Date: Wed, 8 Nov 2023 11:17:40 +0000 Message-ID: <20231108111806.92604-8-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D033UWC002.ant.amazon.com (10.13.139.196) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:47 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994638081353800 X-GMAIL-MSGID: 1781994638081353800 Introduce a new capability to enable Hyper-V Virtual Secure Mode (VSM) emulation support. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/hyperv.h | 5 +++++ arch/x86/kvm/x86.c | 5 +++++ include/uapi/linux/kvm.h | 1 + 4 files changed, 13 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 00cd21b09f8c..7712e31b7537 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1118,6 +1118,8 @@ struct kvm_hv { struct hv_partition_assist_pg *hv_pa_pg; struct kvm_hv_syndbg hv_syndbg; + + bool hv_enable_vsm; }; struct msr_bitmap_range { diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index f83b8db72b11..2bfed69ba0db 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -238,4 +238,9 @@ static inline int kvm_hv_verify_vp_assist(struct kvm_vcpu *vcpu) int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu); +static inline bool kvm_hv_vsm_enabled(struct kvm *kvm) +{ + return kvm->arch.hyperv.hv_enable_vsm; +} + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4cd3f00475c1..b0512e433032 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4485,6 +4485,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_HYPERV_CPUID: case KVM_CAP_HYPERV_ENFORCE_CPUID: case KVM_CAP_SYS_HYPERV_CPUID: + case KVM_CAP_HYPERV_VSM: case KVM_CAP_PCI_SEGMENT: case KVM_CAP_DEBUGREGS: case KVM_CAP_X86_ROBUST_SINGLESTEP: @@ -6519,6 +6520,10 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, } mutex_unlock(&kvm->lock); break; + case KVM_CAP_HYPERV_VSM: + kvm->arch.hyperv.hv_enable_vsm = true; + r = 0; + break; default: r = -EINVAL; break; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 5ce06a1eee2b..168b6ac6ebe5 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1226,6 +1226,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_GUEST_MEMFD 233 #define KVM_CAP_VM_TYPES 234 #define KVM_CAP_APIC_ID_GROUPS 235 +#define KVM_CAP_HYPERV_VSM 237 #ifdef KVM_CAP_IRQ_ROUTING From patchwork Wed Nov 8 11:17:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162964 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842944vqo; Wed, 8 Nov 2023 03:21:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHpbLGk14uVdPKDNuoqVvzZSp44eFHqqurOLaN8pcK0Y6h6SrTKbKa0aqNTufFImd/sOH5 X-Received: by 2002:a05:6a00:21c9:b0:6be:5367:211b with SMTP id t9-20020a056a0021c900b006be5367211bmr1391067pfj.3.1699442517452; Wed, 08 Nov 2023 03:21:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442517; cv=none; d=google.com; s=arc-20160816; b=Q5mbW3D7DwEB7okmVwIgtdtwByMYCtviUJvksVy6BN4soitsWhDWAZw+09ZO9X+HKU I/Z5QCYZIXqcwYIlcWNyHZwB0T+6AdKC8URBc4gHanFL2+L2W+v0PWLinbqIyTTq5Why mepBVPOB/RiMuHMy2Rsm0UlMMdNKONZiSDYU0fPJg9BoQ0Y5V44uS23sK0jaahIaEQOc Bx/bZau2ssWBDhKAXm22g7dRUwCVsrNrdtLV4HLPLPa2lXAHbMjrlLeEM4Zk9yPxp/6I VlpQBFCethrrD+vRQ4XuKysivH+AwgRwt49ZRClTFzZqaX70fL5WNs8B3lZZHdqkmW28 Y/ow== 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; bh=cyTCynOmMb0cQQa0wX5d7cLulbacT3tvj9NSCTRwhHI=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=eyUQ2Yzs3ZOZjSQlcA1A0fKruLqf14weAtCcRUCjK7EYwGtqBflGvc9nIFq2uj412B u2DZY2jVTkwQ+9q1P4ftZtZGHslHIKst5ccd1a1uKDAP+8NQl+sYesi97al+10vi3LlY Yy6Dbz5nxfPPTJ8uifrsPNe18rEUroYerdIvGpdCuEN9NYYKMImMyD/8B5kVzx1rLGrn UxjpXQQ5+mxdWJzHpCMcA6Xd3nnr03emcG6fTTWTx0LZIgHPO5flt1yBZ6WcBx7/wf6/ pI5AdJhxCf0JhuozS3vlaIIwwIR0WwIkdGqeVDpksfGxUA8Ogm8wUocs+upjNFzlWzPN F4mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=uoXdxXqx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id t25-20020aa79399000000b006bf2e5f0841si11751400pfe.393.2023.11.08.03.21.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=uoXdxXqx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id CBC228084632; Wed, 8 Nov 2023 03:20:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344648AbjKHLUE (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344628AbjKHLTz (ORCPT ); Wed, 8 Nov 2023 06:19:55 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0FA91BEB; Wed, 8 Nov 2023 03:19:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442393; x=1730978393; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cyTCynOmMb0cQQa0wX5d7cLulbacT3tvj9NSCTRwhHI=; b=uoXdxXqxTJ+aXNNZAiSwqC9CpZyqE0rCCsnQYVD3oz+KZfPUCxjMgi4v DkfMnOq7N8xKvScGfVrJBv3yt4HvH5QadPHsir17js6YhAczT4/E+p9rz oaY9CCXZCcpgJ/iaZplwBa90b6O131+qYzCpi7o2Pu+rtUv12nNOFpB2p Q=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366812308" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-f05d30a1.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:52 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-f05d30a1.us-east-1.amazon.com (Postfix) with ESMTPS id 23B2F80D5F; Wed, 8 Nov 2023 11:19:48 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.10.100:19113] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.43.105:2525] with esmtp (Farcaster) id fbe08b9c-13d3-4e3e-a014-4f60b4421048; Wed, 8 Nov 2023 11:19:48 +0000 (UTC) X-Farcaster-Flow-ID: fbe08b9c-13d3-4e3e-a014-4f60b4421048 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:47 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:43 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 08/33] KVM: x86: Don't use hv_timer if CAP_HYPERV_VSM enabled Date: Wed, 8 Nov 2023 11:17:41 +0000 Message-ID: <20231108111806.92604-9-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D033UWC002.ant.amazon.com (10.13.139.196) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:40 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994636865797204 X-GMAIL-MSGID: 1781994636865797204 VSM's VTLs are modeled by using a distinct vCPU per VTL. While one VTL is running the rest of vCPUs are left idle. This doesn't play well with the approach of tracking emulated timer expiration by using the VMX preemption timer. Inactive VTL's timers are still meant to run and inject interrupts regardless of their runstate. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/lapic.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index f55d216cb2a0..8cc75b24381b 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -152,9 +152,10 @@ static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu) bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu) { - return kvm_x86_ops.set_hv_timer - && !(kvm_mwait_in_guest(vcpu->kvm) || - kvm_can_post_timer_interrupt(vcpu)); + return kvm_x86_ops.set_hv_timer && + !(kvm_mwait_in_guest(vcpu->kvm) || + kvm_can_post_timer_interrupt(vcpu)) && + !(kvm_hv_vsm_enabled(vcpu->kvm)); } static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu) From patchwork Wed Nov 8 11:17:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162966 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842967vqo; Wed, 8 Nov 2023 03:21:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IExNHWJOvGeJuh93HcBgOUzgRBzZIuKu5smh4LcQZpi53KmdvD9qFgyCrYSo+Vtu64JvPv7 X-Received: by 2002:a05:6808:3847:b0:3af:e67d:8295 with SMTP id ej7-20020a056808384700b003afe67d8295mr2075608oib.40.1699442519073; Wed, 08 Nov 2023 03:21:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442519; cv=none; d=google.com; s=arc-20160816; b=SNP8oMUsTfHpWsi2LsFDG8D2kGWyE88bb1RKEFzTKjaTr0Rrn634YQrZ60D7sRnWiv E1iE86ANxn155YKlzkS5GeVJlHZx84O6mv9xk/5a+oZEcZjl0XQ6NitZv9Ix+XXokcVv qoOPJ5B2P7TY84dWPH7o46IA1HZKRFyu5JTgwpO22oShcxcC2RRXJgd63VzUe+MOVD0j VLDuSQr7t6QOwaBPg7y3uzBmCwypmvZJTdcyivy8qtO+SEpFjfbT4ntS+KeRGNWEVJ5H Ol24F/sF7eGPZWj7OKwKFlyFBWvXUXqoTxObhh59ZzDPqdZdEmwMxUYs3aPZmPlAaK1x NRCw== 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; bh=+q6HaRK4F5IqO9uASaPkSnlSL0f+bqvxqAxqsapmXI8=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=ru6be41n0Os4qRnjiS6UuMiiS6HsARQ/N8w24C5dAPnLiFg3uVfVFKNgrlCZ4Mw1A7 Sg0rn99SPoklYZxtPArZMdwgY5FQ/0h0wM96yVvEG0+2TM7Y8QMAW+YLVCMGU6WPM93f tkXGrXFA4x8ZDrX0v/fNOg1pwerweFVsAZ+FsBi3O/OirBAqTsHug/xvJlTADtSusJGS 2KcbALTPnYKDJBqkfqrR8wl02HL5yuY+UFU3S3eFs8FObk6BqfhY+idjbjMtUng9jmFI GJyPvz07eMFboJRiFpt3C51m+qsom+zTqQqR7zcCU7vJx+MQFU6k/gCROgI3PJ386vz3 cHNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=C0dt2eL7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id c2-20020aa78802000000b0068fce6a86acsi11914527pfo.121.2023.11.08.03.21.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=C0dt2eL7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id AA764808474B; Wed, 8 Nov 2023 03:20:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344455AbjKHLUe (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344676AbjKHLUV (ORCPT ); Wed, 8 Nov 2023 06:20:21 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8552D1FC8; Wed, 8 Nov 2023 03:20:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442419; x=1730978419; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+q6HaRK4F5IqO9uASaPkSnlSL0f+bqvxqAxqsapmXI8=; b=C0dt2eL7HaTLnf+XladYEXWisf8ff8t8FXQGWEY1ORRsP8yKVNUG6dcQ Eq4tEIis1CZvWqwIrhNeHYugTgs6YAFQIKVddISCpF40nigHDaRTCl9L+ gdfO3zAwk0J+wPdQ3mhMrK2UqnhfGb1l5hlOsO7sSxpUU3o7i534VyMoH 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="251427918" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-a893d89c.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:20:18 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-a893d89c.us-west-2.amazon.com (Postfix) with ESMTPS id 05BA740D73; Wed, 8 Nov 2023 11:20:17 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:7728] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.33.209:2525] with esmtp (Farcaster) id 3cca8fad-d704-4370-b632-03b67dd48c0d; Wed, 8 Nov 2023 11:20:16 +0000 (UTC) X-Farcaster-Flow-ID: 3cca8fad-d704-4370-b632-03b67dd48c0d Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:16 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:11 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 09/33] KVM: x86: hyper-v: Introduce per-VTL vcpu helpers Date: Wed, 8 Nov 2023 11:17:42 +0000 Message-ID: <20231108111806.92604-10-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D044UWB002.ant.amazon.com (10.13.139.188) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:55 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994638830439039 X-GMAIL-MSGID: 1781994638830439039 Introduce two helper functions. The first one queries a vCPU's VTL level, the second one, given a struct kvm_vcpu and VTL pair, returns the corresponding 'sibling' struct kvm_vcpu at the right VTL. We keep track of each VTL's state by having a distinct struct kvm_vpcu for each level. VTL-vCPUs that belong to the same guest CPU share the same physical APIC id, but belong to different APIC groups where the apic group represents the vCPU's VTL. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 2bfed69ba0db..5433107e7cc8 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -23,6 +23,7 @@ #include #include "x86.h" +#include "lapic.h" /* "Hv#1" signature */ #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 @@ -83,6 +84,23 @@ static inline struct kvm_hv_syndbg *to_hv_syndbg(struct kvm_vcpu *vcpu) return &vcpu->kvm->arch.hyperv.hv_syndbg; } +static inline struct kvm_vcpu *kvm_hv_get_vtl_vcpu(struct kvm_vcpu *vcpu, int vtl) +{ + struct kvm *kvm = vcpu->kvm; + u32 target_id = kvm_apic_id(vcpu); + + kvm_apic_id_set_group(kvm, vtl, &target_id); + if (vcpu->vcpu_id == target_id) + return vcpu; + + return kvm_get_vcpu_by_id(kvm, target_id); +} + +static inline u8 kvm_hv_get_active_vtl(struct kvm_vcpu *vcpu) +{ + return kvm_apic_group(vcpu); +} + static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) { struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); From patchwork Wed Nov 8 11:17:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842863vqo; Wed, 8 Nov 2023 03:21:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8FIA8orI+9jF6ZPpEm9LeCjJbizow2pVQxeFDA5uguTyi5Xqr8wGM8BGAGvJb8iXXm+QT X-Received: by 2002:a17:903:41cd:b0:1cc:6fa6:ab62 with SMTP id u13-20020a17090341cd00b001cc6fa6ab62mr1726146ple.29.1699442507371; Wed, 08 Nov 2023 03:21:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442507; cv=none; d=google.com; s=arc-20160816; b=JUvtotTXtuJG1k0ZU64w9IQJg5q5LDN/ZB+6A4Ld/1tl5dvR94c9W4AJtxyZ1bOadQ fdoGh217XOchgRky8CISLAmCXLy70iK519XOYgZec/VJVb0Ob+c0MwrTdYNM1jDDDqvj wvyLOkf8OdBAXxSHdpFkhZocvfsJvsrHzXNCgFXqlblA5FIpwIjTzk6A8kPE0KFzWWPY Hwg/AY1g1K4ZZ62WW2RVLw6nGBg5ciT86QFHz6pHfNnei0zPw36QQKoiaI6vb93SxlFo 3yQWNMe/zljdLN+/Frjf+I79DpytnYmUSMbXPNpCiMCAc4SaEzAav0oWLQM5upuSrKiG 0OaA== 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; bh=O25EX7uu/BZNg+CriP/M93KalXQ52Vh7t09zkAv2Ya4=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=k1TrE26HACffCsMQ9k+c7TY+PQ+OA+3Ok4MwIpvgkzc5gWklzZ2jo0UQ5tJjDH6YKV b+UDSQWV54N60mJSoT7Sp69NKw+XL0hDxrM1IzYbY+RHYfEsQvqtp3da1a6oU9yEum95 XH117Y0ExdgE4b8tTfKHCOA3UOkQsTYO1xqHDOCtLrnCPoffrHvogQA/jXx3XAyWWZI5 Rxi14FCcBPeDp9b06QR2Jqis04lF2Go5TqIb9NApPTgQBOSaBdyXpSxqLEM9OB65fZAH x8gG/R82oZiDWV1mIU22gURbyIgBBtb6kWiQxCT+5P7iWesv5mnniHtzhSvFpIglcv7U eH5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aykVI1Qf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n18-20020a170902d2d200b001c61817e049si2271000plc.67.2023.11.08.03.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aykVI1Qf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6CD9580907B7; Wed, 8 Nov 2023 03:21:01 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344600AbjKHLUh (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344193AbjKHLUb (ORCPT ); Wed, 8 Nov 2023 06:20:31 -0500 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC5F101; Wed, 8 Nov 2023 03:20:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442429; x=1730978429; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O25EX7uu/BZNg+CriP/M93KalXQ52Vh7t09zkAv2Ya4=; b=aykVI1QfFrWWdgvl5K28nJc2jLfTCZz/bsoiUlASsTpFbkDS41nZfZr3 D6azOjVhkVl7UL9tqeYijznMZNP5k8HOiCYJHwm4bfyvTjk6jYm4M++87 qhmwsdlgVGNs+hOQearaS91Oy2ZVQ5ATF/zBhTpyNuvjCuCQE0ToZBBkX I=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="593807472" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:20:27 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com (Postfix) with ESMTPS id 527E148E12; Wed, 8 Nov 2023 11:20:23 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.17.79:53919] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.33.209:2525] with esmtp (Farcaster) id 70e20ac2-f2b6-471e-8f0e-3ab2809c6acc; Wed, 8 Nov 2023 11:20:21 +0000 (UTC) X-Farcaster-Flow-ID: 70e20ac2-f2b6-471e-8f0e-3ab2809c6acc Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:21 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:16 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 10/33] KVM: x86: hyper-v: Introduce KVM_HV_GET_VSM_STATE Date: Wed, 8 Nov 2023 11:17:43 +0000 Message-ID: <20231108111806.92604-11-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D044UWB002.ant.amazon.com (10.13.139.188) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:21:01 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994626995090302 X-GMAIL-MSGID: 1781994626995090302 HVCALL_GET_VP_REGISTERS exposes the VTL call hypercall page entry offsets to the guest. This hypercall is implemented in user-space while the hypercall page patching happens in-kernel. So expose it as part of the partition wide VSM state. NOTE: Alternatively there is the option of sharing this information through a VTL KVM device attribute (the device is introduced in subsequent patches). Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/uapi/asm/kvm.h | 5 +++++ arch/x86/kvm/hyperv.c | 8 ++++++++ arch/x86/kvm/hyperv.h | 2 ++ arch/x86/kvm/x86.c | 18 ++++++++++++++++++ include/uapi/linux/kvm.h | 4 ++++ 5 files changed, 37 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index f73d137784d7..370483d5d5fd 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -570,4 +570,9 @@ struct kvm_apic_id_groups { __u8 n_bits; /* nr of bits used to represent group in the APIC ID */ }; +/* for KVM_HV_GET_VSM_STATE */ +struct kvm_hv_vsm_state { + __u64 vsm_code_page_offsets; +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 2cf430f6ddd8..caaa859932c5 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2990,3 +2990,11 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, return 0; } + +int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *state) +{ + struct kvm_hv* hv = &kvm->arch.hyperv; + + state->vsm_code_page_offsets = hv->vsm_code_page_offsets.as_u64; + return 0; +} diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 5433107e7cc8..b3d1113efe82 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -261,4 +261,6 @@ static inline bool kvm_hv_vsm_enabled(struct kvm *kvm) return kvm->arch.hyperv.hv_enable_vsm; } +int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *state); + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b0512e433032..57f9c58e1e32 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7132,6 +7132,24 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm_vm_ioctl_set_apic_id_groups(kvm, &groups); break; } + case KVM_HV_GET_VSM_STATE: { + struct kvm_hv_vsm_state vsm_state; + + r = -EINVAL; + if (!kvm_hv_vsm_enabled(kvm)) + goto out; + + r = kvm_vm_ioctl_get_hv_vsm_state(kvm, &vsm_state); + if (r) + goto out; + + r = -EFAULT; + if (copy_to_user(argp, &vsm_state, sizeof(vsm_state))) + goto out; + + r = 0; + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 168b6ac6ebe5..03f5c08fd7aa 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2316,4 +2316,8 @@ struct kvm_create_guest_memfd { #define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) #define KVM_SET_APIC_ID_GROUPS _IOW(KVMIO, 0xd7, struct kvm_apic_id_groups) + +/* Get/Set Hyper-V VSM state. Available with KVM_CAP_HYPERV_VSM */ +#define KVM_HV_GET_VSM_STATE _IOR(KVMIO, 0xd5, struct kvm_hv_vsm_state) + #endif /* __LINUX_KVM_H */ From patchwork Wed Nov 8 11:17:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162960 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842779vqo; Wed, 8 Nov 2023 03:21:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IECP8w7by4TElCBjdx+cByHKnyOjOhi5BPqeDXd8fMpo1HgG6RNJuuG8+wLz+m5BJ97UqwJ X-Received: by 2002:a17:90a:b94b:b0:281:1404:c36f with SMTP id f11-20020a17090ab94b00b002811404c36fmr1403921pjw.16.1699442498708; Wed, 08 Nov 2023 03:21:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442498; cv=none; d=google.com; s=arc-20160816; b=PLVK82g8NS4dhW/YEn/SoDshn/pdM7GCjmA9F0/XLIQ+w1OJrjreQUQTSIW77MEEyI FO7yx8kizapIpPE26L0OPqw1ReuFlElTBIEOfRv6686DU4+SwGnTmlG3gSAktH47quMO 2aLWrwoIwOposVRwmWj1E+5yh04xFqc+S9wzim1jOApwHvODTyP25HjQLUBbZRsE9w1e GeXAB+1BgGw3VSatMIMh8qIQ8yidv4S22iaiNfmF5D92HFFWmDm55hf+nKaNbD3wMPzI w0feiwDtjUwsa00hEB8Y65IxK38MmOD1EmF3uguXHxxqzhryOntHBa3w1pkZfgp92p/i 1x+g== 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; bh=QBVqEnmtZNOjPktuGk9xsojMAUYhuWQTJQf/5jldNU4=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=K55W9x2NZ4GoguI46nKD/WMrfgsiGHWdHTDgh8MRrUsK+Vj5eWDo2ZQoqTsbItDBbm TrQqgeqQmmGG4NQxnFDD0lVqT/ZgyyxpcptH/LMu0zNjuZV9B1hzMVgiUDu61C7tbwmp 4FsfBcD5St4vOvhO+6CARQI9+tY6oVuVZJgiB7FPSbGCS0esVxdphumqQ1ByYRy3sTii ws4H0gkV/NixiTs5G69+NtAetqCLPqyWh8nxSp4C8fx+pPlwp2+m87wUzCvtraiSauRf So8BvM+qZct0063hs1mKb8izNQaps1S1C3trLDLJ7WsTLmSPnSRKnCFtcsKLMPOpv5EL AsOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=B2oubJH9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id x65-20020a17090a6c4700b002805aa7b138si2047175pjj.59.2023.11.08.03.21.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=B2oubJH9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9060C82696D3; Wed, 8 Nov 2023 03:21:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344690AbjKHLUs (ORCPT + 32 others); Wed, 8 Nov 2023 06:20:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344193AbjKHLUk (ORCPT ); Wed, 8 Nov 2023 06:20:40 -0500 Received: from smtp-fw-52003.amazon.com (smtp-fw-52003.amazon.com [52.119.213.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08B351BEC; Wed, 8 Nov 2023 03:20:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442436; x=1730978436; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QBVqEnmtZNOjPktuGk9xsojMAUYhuWQTJQf/5jldNU4=; b=B2oubJH9/QQ5t5/Q0OeG0guV4eO6kmOmkBgbZF7D1k6BpayFGxHSYPhJ F8aitM+eksavt7Zw8G7NNTM3kwknGEnRgkGHE9KAL0EiWwkevxBpVLCEV etr9kWKoYI8QxvJhcB84JKM5YembBF0auUEjAUSHxKASziVievrPVAfPY 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="618315974" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-f7c754c9.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-52003.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:20:32 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-f7c754c9.us-west-2.amazon.com (Postfix) with ESMTPS id B84FB40D92; Wed, 8 Nov 2023 11:20:29 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.43.254:62810] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id 3ff2c4a7-a3e7-46e0-936d-e9e005f722e6; Wed, 8 Nov 2023 11:20:28 +0000 (UTC) X-Farcaster-Flow-ID: 3ff2c4a7-a3e7-46e0-936d-e9e005f722e6 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:26 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:21 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 11/33] KVM: x86: hyper-v: Handle GET/SET_VP_REGISTER hcall in user-space Date: Wed, 8 Nov 2023 11:17:44 +0000 Message-ID: <20231108111806.92604-12-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D044UWB002.ant.amazon.com (10.13.139.188) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:21:26 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994617851435715 X-GMAIL-MSGID: 1781994617851435715 Let user-space handle HVCALL_GET_VP_REGISTERS and HVCALL_SET_VP_REGISTERS through the KVM_EXIT_HYPERV_HVCALL exit reason. Additionally, expose the cpuid bit. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 9 +++++++++ include/asm-generic/hyperv-tlfs.h | 1 + 2 files changed, 10 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index caaa859932c5..a3970d52eef1 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2456,6 +2456,9 @@ static void kvm_hv_write_xmm(struct kvm_hyperv_xmm_reg *xmm) static bool kvm_hv_is_xmm_output_hcall(u16 code) { + if (code == HVCALL_GET_VP_REGISTERS) + return true; + return false; } @@ -2520,6 +2523,8 @@ static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: case HVCALL_SEND_IPI_EX: + case HVCALL_GET_VP_REGISTERS: + case HVCALL_SET_VP_REGISTERS: return true; } @@ -2738,6 +2743,9 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) break; } goto hypercall_userspace_exit; + case HVCALL_GET_VP_REGISTERS: + case HVCALL_SET_VP_REGISTERS: + goto hypercall_userspace_exit; default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; break; @@ -2903,6 +2911,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->ebx |= HV_POST_MESSAGES; ent->ebx |= HV_SIGNAL_EVENTS; ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS; + ent->ebx |= HV_ACCESS_VP_REGISTERS; ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; ent->edx |= HV_X64_HYPERCALL_XMM_OUTPUT_AVAILABLE; diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index 40d7dc793c03..24ea699a3d8e 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -89,6 +89,7 @@ #define HV_ACCESS_STATS BIT(8) #define HV_DEBUGGING BIT(11) #define HV_CPU_MANAGEMENT BIT(12) +#define HV_ACCESS_VP_REGISTERS BIT(17) #define HV_ENABLE_EXTENDED_HYPERCALLS BIT(20) #define HV_ISOLATION BIT(22) From patchwork Wed Nov 8 11:17:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162967 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843154vqo; Wed, 8 Nov 2023 03:22:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IF5qJ+hflil5WNBJWVZvSLlvn9a+y87jtGwE6/5gNZ6luZB5K6GduxkhwaICrIVV84oOAeH X-Received: by 2002:a05:6a00:9385:b0:6c2:bc81:2787 with SMTP id ka5-20020a056a00938500b006c2bc812787mr1366591pfb.6.1699442543192; Wed, 08 Nov 2023 03:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442543; cv=none; d=google.com; s=arc-20160816; b=xxQVG+NJO77Opxbv9C4en8hLSHbegH/v6T1yOi3Ae2XnG4WCOf1lUWS3O1aY2oTX8z Y1W59hUqT+SL3ePT+au0ldT3AZYuvj5ig8w+zLwXHuV5aMwZtf48klN5ZJFptq098xd3 NVH+dvsEE6zG6quiI9yigrG5I3tv6lg3XLHrBo9/BX9FD4IZWzWzY1tQE5g6fJ7jX/MW kRSn+9u5kBwcdX7KF5g3K9zQ34cMtqj30VoEX+JwhAMl3X7ykBP+6BEkcpH1Dtu1E8WA HgbSaLbpJvT/bHOMhvcgF4j/mgdBoWZn/9tBMSX99yF4D4GiVUz4Jd8oh7HLjkD55bCZ dO4w== 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; bh=7C45WW2plKc9LGS1hg04kUsdvaSyLe7Yr2kIsIf6Lys=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=T++eo84xwgHlKxGtKsu9sCMcF783zFVZ/FddalrKUcY6z2bXSLRuDWu7whl26GVhVv znxYm5gz1WypjKUxXiq0ufaEBAZyLgPYEAHDQ0jBXmz0QVyKCRXSzdQ9OLaC5YwneBVk Wu1gLtHtAQxEh8YPE9DzDVx9811FfOt+/9ugMl6NqYnO0oUS6mX4TNw8zkeO37V5rP6m vkIAy3q8Pmk1S2rcUJAdWcXz1x8FO644ez3jVk1BB53knD5fGoCPWS4oxvkwrSaV3nJg ZmReGPNPSlezKSZV8MvKlfj1IXKC1wYK57pjbpp9CuC8yIleHt5o+s27hE2HcEyUU/36 k4eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=PBBdMmlK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id j12-20020a056a00174c00b0069100de2235si13681501pfc.187.2023.11.08.03.22.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:22:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=PBBdMmlK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1BE3280A22F8; Wed, 8 Nov 2023 03:21:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344644AbjKHLVO (ORCPT + 32 others); Wed, 8 Nov 2023 06:21:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344402AbjKHLVF (ORCPT ); Wed, 8 Nov 2023 06:21:05 -0500 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDC0B1BF5; Wed, 8 Nov 2023 03:21:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442464; x=1730978464; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7C45WW2plKc9LGS1hg04kUsdvaSyLe7Yr2kIsIf6Lys=; b=PBBdMmlKTVNOph/CauxitBiIZC2KKZDRZoDPYrP9M4s46/tyZCXrGesw EopIKJhR5etsB50qYBfKZhCn3BEkZk7/oahfr0RJhI3xKx8AZ9wy5DxYs QJ1k7B0GZJsSKlIGCX3MXcBrNLAyqtfsi4dGzUoeQB+V5Wi9flFULTzz6 Y=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="250876207" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:01 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com (Postfix) with ESMTPS id 11B3F806CD; Wed, 8 Nov 2023 11:20:56 +0000 (UTC) Received: from EX19MTAEUA001.ant.amazon.com [10.0.17.79:3212] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.32.187:2525] with esmtp (Farcaster) id 71b58972-fe97-42fd-b068-56f96eb17f93; Wed, 8 Nov 2023 11:20:55 +0000 (UTC) X-Farcaster-Flow-ID: 71b58972-fe97-42fd-b068-56f96eb17f93 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:55 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:50 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 12/33] KVM: x86: hyper-v: Handle VSM hcalls in user-space Date: Wed, 8 Nov 2023 11:17:45 +0000 Message-ID: <20231108111806.92604-13-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:21:27 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994664234564186 X-GMAIL-MSGID: 1781994664234564186 Let user-space handle all hypercalls that fall under the AccessVsm partition privilege flag. That is: - HVCALL_MODIFY_VTL_PROTECTION_MASK: - HVCALL_ENABLE_PARTITION_VTL: - HVCALL_ENABLE_VP_VTL: - HVCALL_VTL_CALL: - HVCALL_VTL_RETURN: The hypercalls are processed through the KVM_EXIT_HYPERV_HVCALL exit. Additionally, expose the cpuid bit. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 15 +++++++++++++++ include/asm-generic/hyperv-tlfs.h | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index a3970d52eef1..a266c5d393f5 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2462,6 +2462,11 @@ static bool kvm_hv_is_xmm_output_hcall(u16 code) return false; } +static inline bool kvm_hv_is_vtl_call_return(u16 code) +{ + return code == HVCALL_VTL_CALL || code == HVCALL_VTL_RETURN; +} + static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) { bool fast = !!(vcpu->run->hyperv.u.hcall.input & HV_HYPERCALL_FAST_BIT); @@ -2471,6 +2476,9 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) if (kvm_hv_is_xmm_output_hcall(code) && hv_result_success(result) && fast) kvm_hv_write_xmm(vcpu->run->hyperv.u.hcall.xmm); + if (kvm_hv_is_vtl_call_return(code)) + return kvm_skip_emulated_instruction(vcpu); + return kvm_hv_hypercall_complete(vcpu, result); } @@ -2525,6 +2533,7 @@ static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) case HVCALL_SEND_IPI_EX: case HVCALL_GET_VP_REGISTERS: case HVCALL_SET_VP_REGISTERS: + case HVCALL_MODIFY_VTL_PROTECTION_MASK: return true; } @@ -2745,6 +2754,11 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) goto hypercall_userspace_exit; case HVCALL_GET_VP_REGISTERS: case HVCALL_SET_VP_REGISTERS: + case HVCALL_MODIFY_VTL_PROTECTION_MASK: + case HVCALL_ENABLE_PARTITION_VTL: + case HVCALL_ENABLE_VP_VTL: + case HVCALL_VTL_CALL: + case HVCALL_VTL_RETURN: goto hypercall_userspace_exit; default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; @@ -2912,6 +2926,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->ebx |= HV_SIGNAL_EVENTS; ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS; ent->ebx |= HV_ACCESS_VP_REGISTERS; + ent->ebx |= HV_ACCESS_VSM; ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; ent->edx |= HV_X64_HYPERCALL_XMM_OUTPUT_AVAILABLE; diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index 24ea699a3d8e..a8b5c8a84bbc 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -89,6 +89,7 @@ #define HV_ACCESS_STATS BIT(8) #define HV_DEBUGGING BIT(11) #define HV_CPU_MANAGEMENT BIT(12) +#define HV_ACCESS_VSM BIT(16) #define HV_ACCESS_VP_REGISTERS BIT(17) #define HV_ENABLE_EXTENDED_HYPERCALLS BIT(20) #define HV_ISOLATION BIT(22) @@ -147,9 +148,13 @@ union hv_reference_tsc_msr { /* Declare the various hypercall operations. */ #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE 0x0002 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST 0x0003 -#define HVCALL_ENABLE_VP_VTL 0x000f #define HVCALL_NOTIFY_LONG_SPIN_WAIT 0x0008 #define HVCALL_SEND_IPI 0x000b +#define HVCALL_MODIFY_VTL_PROTECTION_MASK 0x000c +#define HVCALL_ENABLE_PARTITION_VTL 0x000d +#define HVCALL_ENABLE_VP_VTL 0x000f +#define HVCALL_VTL_CALL 0x0011 +#define HVCALL_VTL_RETURN 0x0012 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX 0x0013 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX 0x0014 #define HVCALL_SEND_IPI_EX 0x0015 From patchwork Wed Nov 8 11:17:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162969 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843254vqo; Wed, 8 Nov 2023 03:22:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IG/o9XrWuzukB6RvpweE2tJTI1KxxhsobAeKWPn+IVXzlsN/Fv+LSjIvcGeMSWtFN9ZvYdB X-Received: by 2002:a05:6808:1a0b:b0:3a7:4e0c:faa2 with SMTP id bk11-20020a0568081a0b00b003a74e0cfaa2mr2064251oib.27.1699442558616; Wed, 08 Nov 2023 03:22:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442558; cv=none; d=google.com; s=arc-20160816; b=FxmXQwaCE1yBivRLFivDgkR4HhYzR6dVopzXMz2EaOdLZrDVKkMGUi2AnBlhjPmcNz WFQVjDU6MaMwWrsq246zAZBV419uMCOkaQFnG2w7zvCwVCrJSmUGkGOrg/Oem3X0l41x mKZCxTd2qa/TTxPm3EOKqMobRBPs/RoAfKyjEUoF3wTvTPoX+Hr6xzj8DGawaMY0p+f0 QNTKsEkYWxGyXxu3UklvxYbYU3MYuvriq3IYSw2KThswT+HaeViKHzZ1upflTONDpGiU eNInhUlzg3vcpgCwazFZrcGxy7JGrQhOoez0HVewJORKAHHC58JYYGFq4MrfHKSaWqY5 81gg== 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; bh=WNkxJnNlKH45HLgy7MuxT0PXy6Pk8Rwqfkx/NFZb78M=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=qBrAs1QTJMtiki7VesYAdH5lHvLJ/dvM+V/ez9pSK4X0g9MXLouRtMzNeLPdZ29o0Y xExrTdJP/jUDHVm8cgUQPL1RCTSzm9KLIqvUbdDSazVhRxJRyxk6oQhMTqdwEPLSyN3Z wQwySgPSdJzvy5WyOBXlF8kJA1QIG01ytU/fIxOIk57tkOBUH5CoWA2iOIN7MTxBXIzB trzET6VKF+3u7BfMdF4+cg2veZcfSSOmDl3nK0r+eF6ldclAiAm1MfKFZrR20Cym1yx0 CWGdk1Bo6XMX/F9NQIOCAWKNsuaxo16Tzi25piq2xZCQl2b12K+YH4ZLOFOi6cmZpOhH sy+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=oopPMrgK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id e124-20020a636982000000b005af44ed3148si4012419pgc.367.2023.11.08.03.22.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:22:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=oopPMrgK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 1D2DA808463F; Wed, 8 Nov 2023 03:22:34 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344668AbjKHLV0 (ORCPT + 32 others); Wed, 8 Nov 2023 06:21:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344620AbjKHLVR (ORCPT ); Wed, 8 Nov 2023 06:21:17 -0500 Received: from smtp-fw-52003.amazon.com (smtp-fw-52003.amazon.com [52.119.213.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FBE01BE7; Wed, 8 Nov 2023 03:21:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442476; x=1730978476; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WNkxJnNlKH45HLgy7MuxT0PXy6Pk8Rwqfkx/NFZb78M=; b=oopPMrgK3huNH+S58PID/PJGmaZuTodV0aPjByLPAEF84pnf67QPYUyS e2U3hibcw/NpOZdGuV6JX4r0FkStxP/wdkDa+cAJsbnsk71MQuiA5Iqhx r8+EiaDXiDXcjOWtGkKmnbmHTK7aoGUXnZFEehEMICMccuKFru1ijXDmK o=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="618316123" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-b404fda3.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52003.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:15 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-b404fda3.us-east-1.amazon.com (Postfix) with ESMTPS id CC42F80587; Wed, 8 Nov 2023 11:21:02 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.17.79:44839] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.22.222:2525] with esmtp (Farcaster) id 18ea7cc4-781f-406b-ba74-c06699172525; Wed, 8 Nov 2023 11:21:00 +0000 (UTC) X-Farcaster-Flow-ID: 18ea7cc4-781f-406b-ba74-c06699172525 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:00 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:20:55 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 13/33] KVM: Allow polling vCPUs for events Date: Wed, 8 Nov 2023 11:17:46 +0000 Message-ID: <20231108111806.92604-14-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:22:34 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994680593072575 X-GMAIL-MSGID: 1781994680593072575 A number of use cases have surfaced where it'd be beneficial to have a vCPU stop its execution in user-space, as opposed to having it sleep in-kernel. Be it in order to make better use of the pCPU's time while the vCPU is halted, or to implement security features like Hyper-V's VSM. A problem with this approach is that user-space has no way of knowing whether the vCPU has pending events (interrupts, timers, etc...), so we need a new interface to query if they are. poll() turned out to be a very good fit. So enable polling vCPUs. The poll() interface considers a vCPU has a pending event if it didn't enter the guest since being kicked by an event source (being kicked forces a guest exit). Kicking a vCPU that has pollers wakes up the polling threads. NOTES: - There is a race between the 'vcpu->kicked' check in the polling thread and the vCPU thread re-entering the guest. This hardly affects the use-cases stated above, but needs to be fixed. - This was tested alongside a WIP Hyper-V Virtual Trust Level implementation which makes ample use of the poll() interface. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/x86.c | 2 ++ include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 57f9c58e1e32..bf4891bc044e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10788,6 +10788,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto cancel_injection; } + WRITE_ONCE(vcpu->kicked, false); + if (req_immediate_exit) { kvm_make_request(KVM_REQ_EVENT, vcpu); static_call(kvm_x86_request_immediate_exit)(vcpu); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 687589ce9f63..71e1e8cf8936 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -336,6 +336,7 @@ struct kvm_vcpu { #endif int mode; u64 requests; + bool kicked; unsigned long guest_debug; struct mutex mutex; @@ -395,6 +396,7 @@ struct kvm_vcpu { */ struct kvm_memory_slot *last_used_slot; u64 last_used_slot_gen; + wait_queue_head_t wqh; }; /* diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ad9aab898a0c..fde004a0ac46 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -497,12 +497,14 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) kvm_vcpu_set_dy_eligible(vcpu, false); vcpu->preempted = false; vcpu->ready = false; + vcpu->kicked = false; preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); vcpu->last_used_slot = NULL; /* Fill the stats id string for the vcpu */ snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d", task_pid_nr(current), id); + init_waitqueue_head(&vcpu->wqh); } static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) @@ -3970,6 +3972,10 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu) if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) smp_send_reschedule(cpu); } + + if (!cmpxchg(&vcpu->kicked, false, true)) + wake_up_interruptible(&vcpu->wqh); + out: put_cpu(); } @@ -4174,6 +4180,29 @@ static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) return 0; } +static __poll_t kvm_vcpu_poll(struct file *file, poll_table *wait) +{ + struct kvm_vcpu *vcpu = file->private_data; + + poll_wait(file, &vcpu->wqh, wait); + + /* + * Make sure we read vcpu->kicked after adding the vcpu into + * the waitqueue list. Otherwise we might have the following race: + * + * READ_ONCE(vcpu->kicked) + * cmpxchg(&vcpu->kicked, false, true)) + * wake_up_interruptible(&vcpu->wqh) + * list_add_tail(wait, &vcpu->wqh) + */ + smp_mb(); + if (READ_ONCE(vcpu->kicked)) { + return EPOLLIN; + } + + return 0; +} + static int kvm_vcpu_release(struct inode *inode, struct file *filp) { struct kvm_vcpu *vcpu = filp->private_data; @@ -4186,6 +4215,7 @@ static const struct file_operations kvm_vcpu_fops = { .release = kvm_vcpu_release, .unlocked_ioctl = kvm_vcpu_ioctl, .mmap = kvm_vcpu_mmap, + .poll = kvm_vcpu_poll, .llseek = noop_llseek, KVM_COMPAT(kvm_vcpu_compat_ioctl), }; From patchwork Wed Nov 8 11:17:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162962 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842890vqo; Wed, 8 Nov 2023 03:21:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IEP9kYtCOy+WB7bzdJ6o/f3GYejbIlWmk6YSUT4G4QPOvGV/NScqYSKojb/4wi4Ywphi6m+ X-Received: by 2002:a05:6a20:12c6:b0:17f:fef8:1f3f with SMTP id v6-20020a056a2012c600b0017ffef81f3fmr1939827pzg.4.1699442510991; Wed, 08 Nov 2023 03:21:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442510; cv=none; d=google.com; s=arc-20160816; b=z9voxfuL4LNL2BFKsQOFytX3yKK+rY6Mt7hDL4EzzuP49iwx2S05/LNa4L9FPU/FJ8 8tU6Mtp5bfrNw0ytLVjOfnVVwbWKpB25zF5X80W3jK0NxvH/I5M9lzJeP0U9fPGbKUlQ Gj+5YbPnRFYQYTaiRzdjNjHSEN3i1hP1r5lr4HzqHY07RJIhYid9HHUV6TaQOmWs/cDw XdtAtOukNJaIZOR/XIa75MBPaLsK+CGZT9FCy+QYcCtezaYK6+aACnAf/fF+qeafkgXb i9YghmLwdCPjh9B4Fy/YzUnuBJeQaZnV9CjURiIYyFyO9u17ASD51hwtmpQKSpVIZE7m Jxrg== 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; bh=hsnpzZHwTij1DO+oBadMSuiUb8OXWa0py8QnRL9ObtU=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=eaqiM4eDHnGfPD5DcM+X9jpJcGN7VgCD7hVijYgqpMK+ld/+G5HWKxOEStD/YDv8Ed F6g2/coH85wun/rxwr/oarv/ryaDvV9x3vfQy6L8q5EIVVGCszYOli/ghVWvMiu3FhtM I2o3Wvi95ctauUoMdA7evf/F8ltpg5rrIzW86c5zwD6ip/A0pORP1BITsrzQlqfWqqGr o+WHlze1lfX3svhOk0X9Nmk4YweQGcB95k4KnnfRAHVoTeZVu4Mefg8BF1W4wH1ssYKk yryzMu/A8AcP2bTcFAk2YNeMhG4zXYyhuaRO3ObWQn8bPjNhb7INqq5/JMTysX4a6LgP s3Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=tTG2Z5me; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id o14-20020a170902d4ce00b001c0eefc0dfesi2280455plg.130.2023.11.08.03.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=tTG2Z5me; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C21BD8246305; Wed, 8 Nov 2023 03:21:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344402AbjKHLVY (ORCPT + 32 others); Wed, 8 Nov 2023 06:21:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344641AbjKHLVN (ORCPT ); Wed, 8 Nov 2023 06:21:13 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E31B3181; Wed, 8 Nov 2023 03:21:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442471; x=1730978471; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hsnpzZHwTij1DO+oBadMSuiUb8OXWa0py8QnRL9ObtU=; b=tTG2Z5meFI6gLZozCnUrMYh6fXI1igfyppPiCfK8rMdXuUfJ+cr6P9du p2bla8f9VUANUldCb68SC8CYgEXpujR8nDAALwrnErClO3UmfojOKzt7q EDO9LYKnMuJXcdZGtzE6Mzh57LsCgt3SbP+dtj7GOaSJvkbzWPyC2WCv4 M=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366812557" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:10 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com (Postfix) with ESMTPS id 3191748ED2; Wed, 8 Nov 2023 11:21:06 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.17.79:8159] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.26.101:2525] with esmtp (Farcaster) id b036e4e3-6edd-4dbe-817d-58a60e75dd60; Wed, 8 Nov 2023 11:21:06 +0000 (UTC) X-Farcaster-Flow-ID: b036e4e3-6edd-4dbe-817d-58a60e75dd60 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:05 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:00 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 14/33] KVM: x86: Add VTL to the MMU role Date: Wed, 8 Nov 2023 11:17:47 +0000 Message-ID: <20231108111806.92604-15-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:21:46 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994630361055313 X-GMAIL-MSGID: 1781994630361055313 With the upcoming introduction of per-VTL memory protections, make MMU roles VTL aware. This will avoid sharing PTEs between vCPUs that belong to different VTLs, and that have distinct memory access restrictions. Four bits are allocated to store the VTL number in the MMU role, since the TLFS states there is a maximum of 16 levels. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/hyperv.h | 6 ++++++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 7712e31b7537..1f5a85d461ce 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -338,7 +338,8 @@ union kvm_mmu_page_role { unsigned ad_disabled:1; unsigned guest_mode:1; unsigned passthrough:1; - unsigned :5; + unsigned vtl:4; + unsigned :1; /* * This is left at the top of the word so that diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index b3d1113efe82..605e80b9e5eb 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -263,4 +263,10 @@ static inline bool kvm_hv_vsm_enabled(struct kvm *kvm) int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *state); +static inline void kvm_mmu_role_set_hv_bits(struct kvm_vcpu *vcpu, + union kvm_mmu_page_role *role) +{ + role->vtl = kvm_hv_get_active_vtl(vcpu); +} + #endif diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 253fb2093d5d..e170388c6da1 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -304,4 +304,5 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, return gpa; return translate_nested_gpa(vcpu, gpa, access, exception); } + #endif diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index baeba8fc1c38..2afef86863fb 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -28,6 +28,7 @@ #include "page_track.h" #include "cpuid.h" #include "spte.h" +#include "hyperv.h" #include #include @@ -5197,6 +5198,7 @@ static union kvm_cpu_role kvm_calc_cpu_role(struct kvm_vcpu *vcpu, role.base.smm = is_smm(vcpu); role.base.guest_mode = is_guest_mode(vcpu); role.ext.valid = 1; + kvm_mmu_role_set_hv_bits(vcpu, &role.base); if (!____is_cr0_pg(regs)) { role.base.direct = 1; @@ -5271,6 +5273,7 @@ kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, role.level = kvm_mmu_get_tdp_level(vcpu); role.direct = true; role.has_4_byte_gpte = false; + kvm_mmu_role_set_hv_bits(vcpu, &role); return role; } From patchwork Wed Nov 8 11:17:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162968 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843197vqo; Wed, 8 Nov 2023 03:22:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhky5vKfma/t/V3Ba/T7+CKJIvs2VO0R8DoYYXm60KOfOnhVc2wY3YfnczuxHFtZvBq9TV X-Received: by 2002:a05:6a20:7289:b0:180:1b3b:d560 with SMTP id o9-20020a056a20728900b001801b3bd560mr1876768pzk.41.1699442551238; Wed, 08 Nov 2023 03:22:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442551; cv=none; d=google.com; s=arc-20160816; b=sf7XzmKg5tTEzsOdI2K7UI4OZYtRq0Z9VGmvryd/kbxEj0l4wWgTgmrOKCNMJ7cv9A lArIHn9l0IxeKocobTklXy3a8iJxCp5YjjNmFIax1SZP95IwXiZx1W0nTM1azAa6UqSM RawAwqQNfz5LY03XguQVSXKk0DDmjBnGKOERQhObzCa4RFZYbzBryn37YhI1hkUVhfNv JAfrPAuyu5I8wt6zq5ogACyp6BqtLueIKETS7LSjd/SUOvgPD8qEYoTLtKLlUW9xoqc/ fxoo7K5UD7RevwMCYM/SOaHL6bRQbo5d8LAXAFYHctfz4Aw2nEmO3ZpRdgTug2piIbGb fC8w== 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; bh=iycFoTQhbiLFSko2vQExEp1NsC8B9ilkhIKK0YicW+o=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=xyzYQmHsXjFJuOgYnyIPkKBLMqHd6RWzPdJVX+6sEYClgHSdbGhv/5Hbe61QruVas8 RHdG/FNpzAaj/F73QcRC1HL3Fuo601++byl13M/H4jvM1x11AZXHEJs9THlfcw94a/T0 MbN3cj9aMsTAOVn4eUmKJs1nS4krNarB8XTZtKeZkiYlWRCnbWyTS2jWft5SnDblmVvQ 73FJw1sVeBmjrUG0tJh1fJPTlDCipLCObHaEhg3AzZ4UEmjfuTTy+k+WYbffmqKNEAwz 6zGswbhZGr7pYPrluKEQBn4ueT8BPZ9O40F5ESwhmUtoAQCYU4SXxOGlWmPC4dZIW32R C6Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sNm8tk4b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id fk11-20020a056a003a8b00b006c320a95e8esi11661074pfb.404.2023.11.08.03.22.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:22:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sNm8tk4b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 82BA981E9CA8; Wed, 8 Nov 2023 03:22:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344672AbjKHLV6 (ORCPT + 32 others); Wed, 8 Nov 2023 06:21:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344618AbjKHLVt (ORCPT ); Wed, 8 Nov 2023 06:21:49 -0500 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55A721BE1; Wed, 8 Nov 2023 03:21:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442507; x=1730978507; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iycFoTQhbiLFSko2vQExEp1NsC8B9ilkhIKK0YicW+o=; b=sNm8tk4bdiSv9RcK/p31fYA5P8oPP9zXu7plXO17n8vzt3AEhJxPRgaG HpxMQtnznovUjHqlU4klh8y3Yh5f0cMHfyTFUPpGHvsH5W41JUc8qY1Qf SsuE9Ar5H845RLHucxk3HM+TLuFPWUIMhBV/09vgIKnWKDEPWauz9kkEb k=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="375131996" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:40 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com (Postfix) with ESMTPS id BA00148E12; Wed, 8 Nov 2023 11:21:36 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:28792] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id cc98cadf-a052-4f23-b472-377eb7ac99f7; Wed, 8 Nov 2023 11:21:35 +0000 (UTC) X-Farcaster-Flow-ID: cc98cadf-a052-4f23-b472-377eb7ac99f7 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:34 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:29 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 15/33] KVM: x86/mmu: Introduce infrastructure to handle non-executable faults Date: Wed, 8 Nov 2023 11:17:48 +0000 Message-ID: <20231108111806.92604-16-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D043UWA003.ant.amazon.com (10.13.139.31) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:22:28 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994672136633059 X-GMAIL-MSGID: 1781994672136633059 The upcoming per-VTL memory protections support needs to fault in non-executable memory. Introduce a new attribute in struct kvm_page_fault, map_executable, to control whether the gfn range should be mapped as executable. No functional change intended. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 6 +++++- arch/x86/kvm/mmu/mmu_internal.h | 2 ++ arch/x86/kvm/mmu/tdp_mmu.c | 8 ++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2afef86863fb..4e02d506cc25 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3245,6 +3245,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) struct kvm_mmu_page *sp; int ret; gfn_t base_gfn = fault->gfn; + unsigned access = ACC_ALL; kvm_mmu_hugepage_adjust(vcpu, fault); @@ -3274,7 +3275,10 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) if (WARN_ON_ONCE(it.level != fault->goal_level)) return -EFAULT; - ret = mmu_set_spte(vcpu, fault->slot, it.sptep, ACC_ALL, + if (!fault->map_executable) + access &= ~ACC_EXEC_MASK; + + ret = mmu_set_spte(vcpu, fault->slot, it.sptep, access, base_gfn, fault->pfn, fault); if (ret == RET_PF_SPURIOUS) return ret; diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index b66a7d47e0e4..bd62c4d5d5f1 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -239,6 +239,7 @@ struct kvm_page_fault { kvm_pfn_t pfn; hva_t hva; bool map_writable; + bool map_executable; /* * Indicates the guest is trying to write a gfn that contains one or @@ -298,6 +299,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .map_executable = true, }; int r; diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 6cd4dd631a2f..46f3e72ab770 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -957,14 +957,18 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, u64 new_spte; int ret = RET_PF_FIXED; bool wrprot = false; + unsigned access = ACC_ALL; if (WARN_ON_ONCE(sp->role.level != fault->goal_level)) return RET_PF_RETRY; + if (!fault->map_executable) + access &= ~ACC_EXEC_MASK; + if (unlikely(!fault->slot)) - new_spte = make_mmio_spte(vcpu, iter->gfn, ACC_ALL); + new_spte = make_mmio_spte(vcpu, iter->gfn, access); else - wrprot = make_spte(vcpu, sp, fault->slot, ACC_ALL, iter->gfn, + wrprot = make_spte(vcpu, sp, fault->slot, access, iter->gfn, fault->pfn, iter->old_spte, fault->prefetch, true, fault->map_writable, &new_spte); From patchwork Wed Nov 8 11:17:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843298vqo; Wed, 8 Nov 2023 03:22:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHeqTYKV31Sgh0ed4dV4e+YUbBq7jiHYpW6QF4FFS02A6uWqI71F1eFsEYny9RRch9Ug3Ex X-Received: by 2002:a05:6808:23cb:b0:3b2:e0f0:e53d with SMTP id bq11-20020a05680823cb00b003b2e0f0e53dmr2055596oib.37.1699442564669; Wed, 08 Nov 2023 03:22:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442564; cv=none; d=google.com; s=arc-20160816; b=HEDf17bjwGjwOrZDM6MIOhl5/fRUpH/uGmvwq0ZJd7VR6CA6Mu8G0uh7JcNVxB8PRy 1PTHVj0YN0zRYiWYj96Bzwcb4bWBmhi1mKIQ9kfiyixYVOqwoAasCQEOl1DIS3hFxYcm awLPB9/SGxMcssk5N7NWcC6H1Z4VrGlm8t0WD2nl1GUR3PmAwHQxENu0qmdLec1s4YaD nowlPgXh+d4tbAqevVRLH1COykJiQrTIHsYhRK16xmBZ3yHAR/nSGz3/L7pz1QffjUoo g++u0RuwFTfajPnth9t4Bs1yxZTHU+99aaRO1vcaf3ldAagk0kt47U7M+lkh6QVGjEXc cRpw== 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; bh=oGmQSWddAE8KqMq8lmVDTRLTnkN/hdaUFRsiDOi+ttg=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=nR1bUOjmU7EpNR6R3/kj+CoQGSbWETkeZDDJ5UulvM5jm+f7c91+sUWdjy+2306Y6C RlX51U/+nAm7g5ZZ+NiyWSAz0Gytz8aHDDCn5XmGSE6J4aZN8UsciGy9qZzARJG7kprO qbp+kBCtDiGVfHKJy/zWVw6mji3CRbLli+62qHaT6d3rBdQanJruOkvb5rW9LVKr6f4C MSsUMT4VoGQPqj/+30ePYwX/yRywVLjgPwCwCVjuCVr2PoOHDf8gpqoSLRygMouhhza5 /dq7JqALe6O/0vyu6xPtbZQkPptu7zRI8ZdRzMvAkaOQYvTMVABNkKBJd3ruqA6tgl+j uJrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R0A3p8uD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id j2-20020a056a00130200b0069100d1fc37si12848937pfu.49.2023.11.08.03.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:22:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R0A3p8uD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id A3D1680615CE; Wed, 8 Nov 2023 03:22:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344807AbjKHLWJ (ORCPT + 32 others); Wed, 8 Nov 2023 06:22:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344686AbjKHLVy (ORCPT ); Wed, 8 Nov 2023 06:21:54 -0500 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79FD91BF3; Wed, 8 Nov 2023 03:21:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442512; x=1730978512; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oGmQSWddAE8KqMq8lmVDTRLTnkN/hdaUFRsiDOi+ttg=; b=R0A3p8uD3LsJkr+hknwGV/eFTPcdmhtskkROAvqShaRGrh3/7kuIY6J5 E1ayrjy8yPpJDtg5MnX8hQfU94cr+Tn/JzSv3TLHJKJPtXnt4Vb6yc0uE RlmSPbWY/jS/ePgDiCUEHdcPNwDldtVccxL7H3IWSVm+B6ChCt736aqRr 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="361603061" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:50 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com (Postfix) with ESMTPS id 9FEC448ED2; Wed, 8 Nov 2023 11:21:46 +0000 (UTC) Received: from EX19MTAEUC001.ant.amazon.com [10.0.10.100:61983] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id 93cbfb2a-c5dc-4a75-bf0d-dbebc073ea77; Wed, 8 Nov 2023 11:21:45 +0000 (UTC) X-Farcaster-Flow-ID: 93cbfb2a-c5dc-4a75-bf0d-dbebc073ea77 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:39 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:34 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 16/33] KVM: x86/mmu: Expose R/W/X flags during memory fault exits Date: Wed, 8 Nov 2023 11:17:49 +0000 Message-ID: <20231108111806.92604-17-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D043UWA003.ant.amazon.com (10.13.139.31) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:22:41 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994686707577523 X-GMAIL-MSGID: 1781994686707577523 Include the fault's read, write and execute status when exiting to user-space. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 4 ++-- include/linux/kvm_host.h | 9 +++++++-- include/uapi/linux/kvm.h | 6 ++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4e02d506cc25..feca077c0210 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4300,8 +4300,8 @@ static inline u8 kvm_max_level_for_order(int order) static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { - kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, - PAGE_SIZE, fault->write, fault->exec, + kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, PAGE_SIZE, + fault->write, fault->exec, fault->user, fault->is_private); } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 71e1e8cf8936..631fd532c97a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2367,14 +2367,19 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr) static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, gpa_t gpa, gpa_t size, bool is_write, bool is_exec, - bool is_private) + bool is_read, bool is_private) { vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; vcpu->run->memory_fault.gpa = gpa; vcpu->run->memory_fault.size = size; - /* RWX flags are not (yet) defined or communicated to userspace. */ vcpu->run->memory_fault.flags = 0; + if (is_read) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_READ; + if (is_write) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_WRITE; + if (is_exec) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_EXECUTE; if (is_private) vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 03f5c08fd7aa..0ddffb8b0c99 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -533,7 +533,13 @@ struct kvm_run { } notify; /* KVM_EXIT_MEMORY_FAULT */ struct { +#define KVM_MEMORY_EXIT_FLAG_READ (1ULL << 0) +#define KVM_MEMORY_EXIT_FLAG_WRITE (1ULL << 1) +#define KVM_MEMORY_EXIT_FLAG_EXECUTE (1ULL << 2) #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) +#define KVM_MEMORY_EXIT_NO_ACCESS \ + (KVM_MEMORY_EXIT_FLAG_NR | KVM_MEMORY_EXIT_FLAG_NW | \ + KVM_MEMORY_EXIT_FLAG_NX) __u64 flags; __u64 gpa; __u64 size; From patchwork Wed Nov 8 11:17:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162973 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843441vqo; Wed, 8 Nov 2023 03:23:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFw8F2PBfZHvsgfG0fNvfteq4zH1u/taLttZudf9kH/0AL0DzsYCPQrMjb4tk0n7UIzisoE X-Received: by 2002:a9d:6e84:0:b0:6cd:941:418c with SMTP id a4-20020a9d6e84000000b006cd0941418cmr1789641otr.21.1699442586985; Wed, 08 Nov 2023 03:23:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442586; cv=none; d=google.com; s=arc-20160816; b=QYGbXS0wfS4a/xYdsDMbvwretMyVOqCIIW7mzuaZNha7lzgVv6axMgRXldUkwPC9ov 8RXeZ59DsbIVbkSPCHLvlgifRU+rx4hAfU1AmkJTXBJSeSdXpn3pshHXvTDmrCtrzlb9 MBGvSEQpPosLEjLgInlUkcoYwgNjUwnMUjgJUvzWVSKaedDedHDw+KHESDQNV9PqwnQn 5eZUuNkLEb5dZZ94Pmw/gJQWODhJIZ6MHGGo9u6tkmOELzjP6+QwTm2OPmrm1diM9N4L XpF7ofL7VgExlUN+KD/qXmSaHdZZEuALIPmUBWbsaQ2Od5R62lf9+utOioJJ41uj98xF dg7w== 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; bh=v9CrHyw1JpyXXRLGKDb9DzQRe5BqtRC9dyxOB/2B9Mo=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=DHZru6E1O3N8ZVoF2BuLnOr8bWOAkW3tye6T6uNu/YDoAkaiVd6K1uRjqPC7xsTQP0 JfPkGGf+POxJcg2zDHQockYCdIMoo5Z46mPmErDlDASO5zcPpp1TdwmHtK6K23H4Bync tKHZzgK3BrKSrcKDSBnlniQb7+dZOu9KxFhfGXFdyglJ3aGCCczj/Bdlp8WiDDfiNjqZ 7I8fIcI/6TP2m1+gdQd/C3reNM1+OeRJIKdkXacOwgSvYvHq2P2+Fy4wKcYSeRKneeyN yEI2mbLdPCforulelX8KOOMR0x0Q7cgS+RtQiUZ1Cg2mn5AqlzYK/3r915jGd1qCkJg9 N09Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=kKfzBpCq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id ay29-20020a056a00301d00b006c3211608adsi12908977pfb.80.2023.11.08.03.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:23:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=kKfzBpCq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id E5E89826BB8A; Wed, 8 Nov 2023 03:22:59 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344652AbjKHLWQ (ORCPT + 32 others); Wed, 8 Nov 2023 06:22:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344634AbjKHLV5 (ORCPT ); Wed, 8 Nov 2023 06:21:57 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D02D1BE1; Wed, 8 Nov 2023 03:21:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442515; x=1730978515; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v9CrHyw1JpyXXRLGKDb9DzQRe5BqtRC9dyxOB/2B9Mo=; b=kKfzBpCqkT/h6wLsqI+jdt228IuVPdJRChlL4wuFcfzpSUgzKmFwfG5l vIXYKjZFv7WgL5B1VfC4pEy0KboL7P6IEWTpVg2mRJHNfSlxcW+hwWQ1V RbkMtzuUuZdG0SsYrK7Jp9RYQ1Snx+lhZ1DhuvRZ612IyVwygY1gsY09i 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366812660" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-f323d91c.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:21:53 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-f323d91c.us-west-2.amazon.com (Postfix) with ESMTPS id 3352840D95; Wed, 8 Nov 2023 11:21:52 +0000 (UTC) Received: from EX19MTAEUA001.ant.amazon.com [10.0.17.79:36943] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.22.222:2525] with esmtp (Farcaster) id a2d9683d-2fd0-47e3-9e16-e83a0bd5b6ea; Wed, 8 Nov 2023 11:21:51 +0000 (UTC) X-Farcaster-Flow-ID: a2d9683d-2fd0-47e3-9e16-e83a0bd5b6ea Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:44 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:21:39 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 17/33] KVM: x86/mmu: Allow setting memory attributes if VSM enabled Date: Wed, 8 Nov 2023 11:17:50 +0000 Message-ID: <20231108111806.92604-18-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D043UWA003.ant.amazon.com (10.13.139.31) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:23:00 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994710093069136 X-GMAIL-MSGID: 1781994710093069136 VSM is also a user of memory attributes, so let it use kvm_set_mem_attributes(). Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index feca077c0210..a1fbb905258b 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7265,7 +7265,8 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, * Zapping SPTEs in this case ensures KVM will reassess whether or not * a hugepage can be used for affected ranges. */ - if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm) && + !kvm_hv_vsm_enabled(kvm))) return false; return kvm_unmap_gfn_range(kvm, range); @@ -7322,7 +7323,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, * a range that has PRIVATE GFNs, and conversely converting a range to * SHARED may now allow hugepages. */ - if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm) && + !kvm_hv_vsm_enabled(kvm))) return false; /* From patchwork Wed Nov 8 11:17:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162972 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843440vqo; Wed, 8 Nov 2023 03:23:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGsa1FZvywAMB8B6kfh6idA21EFEnvezd1k8UXCg04VZfGaVnotIMf6y2KHeANgnDhtxmWT X-Received: by 2002:a17:902:d487:b0:1cc:4d4e:bfb4 with SMTP id c7-20020a170902d48700b001cc4d4ebfb4mr8012569plg.12.1699442586944; Wed, 08 Nov 2023 03:23:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442586; cv=none; d=google.com; s=arc-20160816; b=KvDTThSdjC5LwltyncsyI+Mi2LiaiALoN4pOv2hPjlGZFupW7/GvU1dxROG5e1jVPI QbLhbSCByqjjF9cLkmjY7DhmnHpryzXSO1Hhc3WQue6dZP0skpOzmVSa70Lw19rYRn/r evkkHJdx7TmtLUu9TDThonNn0zZ3ttIBVUCwV/27bs5y39an6+Fn0eERhl5gl23jEkSh vNnSoV565NAwwU8Qr8dwEDEdSpDfFxokg8SYNybpml+I6OHZ37CrVtgJzQndw5Fu/+5t syCEgzKl9Mkt93mc9Zj/jI0IxMASuYUAaPfYhefQN+CQg6J5tBZrjUXRJwP8JyFA/uEx fKlg== 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; bh=v0xPIztXiJ7BozvZWMLf19nS4eQCyoOv0IyQ7RPZ9oY=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=iRqdb/yvja9SDULOp2q0USGXqh3r0sZe99WrTM8+aFi2OBB1i91XBDj+5GRY7NJBu9 5/CfS3Mt70zGczSh+IDLILYtiCJBmAanDo+P9S6BOfEgRc+nbl2+JzUckCRcmci3YwaN LIJmASsiPy3CE8fpOLws4x7h/om2cWkbq7D20rJpOgA23fZhFRzz40TdnWzXT1jMGq9t qNUujpYvn/Oll+FK6A2Oo6Y5cRuOxRsHnZLyVZvM8ufHukXdbUof27E6pS9r3Ilf44tn F/5a6laGtgRvStBL72Pq6jR4UNbWTABSGq2+twvo6+OcRbe/EnvUlIV7Ki+uVHinNSnb wQ7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ScG3mjd4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id e12-20020a170902744c00b001bbad1883d5si1904007plt.293.2023.11.08.03.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:23:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ScG3mjd4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 9E18281A1BC3; Wed, 8 Nov 2023 03:23:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344806AbjKHLWr (ORCPT + 32 others); Wed, 8 Nov 2023 06:22:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344775AbjKHLW3 (ORCPT ); Wed, 8 Nov 2023 06:22:29 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C9532595; Wed, 8 Nov 2023 03:22:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442535; x=1730978535; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v0xPIztXiJ7BozvZWMLf19nS4eQCyoOv0IyQ7RPZ9oY=; b=ScG3mjd4qwEUTBFvQ+qL53JHqLOqgY4i/iWkFCjCq/HOq1GCAgsSyW3X 6pMcvDICCD68aTy80gFBxxOnZwtbhliEV0N3yLlHqNPCUKXPefTq+npqV 4Frx7Bx5Aay2dODnOlP+XcPDWNWoyGh0dT9ydSBITImWlLnlqWIJ6BIcW w=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="251428368" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-fa5fe5fb.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:22:14 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-fa5fe5fb.us-west-2.amazon.com (Postfix) with ESMTPS id 372E640DB0; Wed, 8 Nov 2023 11:22:14 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.17.79:29860] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.43.105:2525] with esmtp (Farcaster) id 76b6a16b-a40f-4cc6-97d8-14f01b8119af; Wed, 8 Nov 2023 11:22:13 +0000 (UTC) X-Farcaster-Flow-ID: 76b6a16b-a40f-4cc6-97d8-14f01b8119af Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:12 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:08 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 18/33] KVM: x86: Decouple kvm_get_memory_attributes() from struct kvm's mem_attr_array Date: Wed, 8 Nov 2023 11:17:51 +0000 Message-ID: <20231108111806.92604-19-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D046UWB003.ant.amazon.com (10.13.139.174) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:23:00 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994709988910812 X-GMAIL-MSGID: 1781994709988910812 Decouple kvm_get_memory_attributes() from struct kvm's mem_attr_array to allow other memory attribute sources to use the function. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 5 +++-- include/linux/kvm_host.h | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a1fbb905258b..96421234ca88 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7301,7 +7301,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { if (hugepage_test_mixed(slot, gfn, level - 1) || - attrs != kvm_get_memory_attributes(kvm, gfn)) + attrs != kvm_get_memory_attributes(&kvm->mem_attr_array, gfn)) return false; } return true; @@ -7401,7 +7401,8 @@ void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm, * be manually checked as the attributes may already be mixed. */ for (gfn = start; gfn < end; gfn += nr_pages) { - unsigned long attrs = kvm_get_memory_attributes(kvm, gfn); + unsigned long attrs = + kvm_get_memory_attributes(&kvm->mem_attr_array, gfn); if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) hugepage_clear_mixed(slot, gfn, level); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 631fd532c97a..4242588e3dfb 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2385,9 +2385,10 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, } #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES -static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn) +static inline unsigned long +kvm_get_memory_attributes(struct xarray *mem_attr_array, gfn_t gfn) { - return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); + return xa_to_value(xa_load(mem_attr_array, gfn)); } bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, @@ -2400,7 +2401,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && - kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; + kvm_get_memory_attributes(&kvm->mem_attr_array, gfn) & + KVM_MEMORY_ATTRIBUTE_PRIVATE; } #else static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) From patchwork Wed Nov 8 11:17:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162971 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843397vqo; Wed, 8 Nov 2023 03:22:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjI0KAebNh2njjpeD6zTqs/H7/ZN1gWgbamO1W/cLQGI+DnnJSwdqt09WZW0ij65N/odgZ X-Received: by 2002:a05:6358:93a3:b0:168:ea50:fcc6 with SMTP id h35-20020a05635893a300b00168ea50fcc6mr1594987rwb.16.1699442578702; Wed, 08 Nov 2023 03:22:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442578; cv=none; d=google.com; s=arc-20160816; b=aOsNyw/BxCgJ+iPt4rZndVohOowoW0KJjgQo68Q63rEhh9x2VWOz2Op3Lk5NdUbEAy xByj2bUZLni9D8hcCndmeBYjY3tb4lE+jueDpASEvf8rWfmW9njTvQAe0u/tEvLdyDyK PnGiUxFNJL9gB6M12wj+c9A0FEpmFYw6Mjz7ebSRE2ylUkjW7edwHgxQCHlSR9W8js2t tOolrNced6YBYBikOhcU8DTZbB8dz6Rx8JHNAN/AbYIXXO0oqiglyBvU+UzP3LTUiQpf fwAo3TBnmIJD5C7oOU31IDSD+nGrT5Ha9/RC8Jd13Pv/a5VsWDuzJr3uutKaz+cJIn60 PqjA== 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; bh=rFj4X075aGu4UwwOIKw0SMbu/vxZLmqvGhwr4IjXpjU=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=aoQA921Zb/dXWsK+wRF/n/VM3av+DT+AV/88H+HyMmWSk8eyCDWobeYDauRey+nUMK 7arR4Z15xLGC84WMK35zMxjKo2UNaFjvsxu9bpcCHdLQ83EHIdKv4TGs0EkVomlLGRT3 MjMYSkLCJcsl5jeebhFuaLCWBSoZta76Ib9+djIwBoUsRbkJEEkG/GaDc/UwyNLQsK3s 9yhLn4hrjsbACsB93NPE5NqeXcnluyxtVzZ49OjKBcCUgG5UvgzJXmUT+f4kcdmbpVk1 gjdaoC9NxLHh7y6zLgH93/7qVSeoXZ26lygJdtPgVz06QCYB9XChp9W9KXYhz9+XzygA tAnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=bMupkCSx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id w190-20020a6382c7000000b0057404ce2fc8si4181828pgd.529.2023.11.08.03.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:22:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=bMupkCSx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id ECE18838B3FA; Wed, 8 Nov 2023 03:22:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344705AbjKHLWw (ORCPT + 32 others); Wed, 8 Nov 2023 06:22:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344694AbjKHLWf (ORCPT ); Wed, 8 Nov 2023 06:22:35 -0500 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F01271FE3; Wed, 8 Nov 2023 03:22:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442544; x=1730978544; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rFj4X075aGu4UwwOIKw0SMbu/vxZLmqvGhwr4IjXpjU=; b=bMupkCSx8kHw7yPBZciU2HV8F0Xl+xtZ09Ws2eUE53PJGeRZCYfT7Nop cnV59XpC57W4oEMdNZq6haUMzkf08ibV3IA+JhnmfJnYKUcqQ7/SNUWEw 4yCNTeBBg/HWh449gZlqWxpqszG8HZucYoctsn85OQPCaAymBvqneZNa4 0=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="593807859" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-189d700f.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:22:21 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2b-m6i4x-189d700f.us-west-2.amazon.com (Postfix) with ESMTPS id 452CD40DAA; Wed, 8 Nov 2023 11:22:19 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:28238] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.43.105:2525] with esmtp (Farcaster) id dcf98806-3299-4d1a-b110-916760b68cf4; Wed, 8 Nov 2023 11:22:18 +0000 (UTC) X-Farcaster-Flow-ID: dcf98806-3299-4d1a-b110-916760b68cf4 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:17 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:13 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 19/33] KVM: x86: Decouple kvm_range_has_memory_attributes() from struct kvm's mem_attr_array Date: Wed, 8 Nov 2023 11:17:52 +0000 Message-ID: <20231108111806.92604-20-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D046UWB003.ant.amazon.com (10.13.139.174) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:22:58 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994701645212677 X-GMAIL-MSGID: 1781994701645212677 Decouple kvm_range_has_memory_attributes() from struct kvm's mem_attr_array to allow other memory attribute sources to use the function. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 3 ++- include/linux/kvm_host.h | 4 ++-- virt/kvm/kvm_main.c | 9 +++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 96421234ca88..4ace2f8660b0 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7297,7 +7297,8 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, const unsigned long end = start + KVM_PAGES_PER_HPAGE(level); if (level == PG_LEVEL_2M) - return kvm_range_has_memory_attributes(kvm, start, end, attrs); + return kvm_range_has_memory_attributes(&kvm->mem_attr_array, + start, end, attrs); for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { if (hugepage_test_mixed(slot, gfn, level - 1) || diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4242588e3dfb..32cf05637647 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2391,8 +2391,8 @@ kvm_get_memory_attributes(struct xarray *mem_attr_array, gfn_t gfn) return xa_to_value(xa_load(mem_attr_array, gfn)); } -bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, - unsigned long attrs); +bool kvm_range_has_memory_attributes(struct xarray *mem_attr_array, gfn_t start, + gfn_t end, unsigned long attrs); bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fde004a0ac46..6bb23eaf7aa6 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2440,10 +2440,10 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, * Returns true if _all_ gfns in the range [@start, @end) have attributes * matching @attrs. */ -bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, - unsigned long attrs) +bool kvm_range_has_memory_attributes(struct xarray *mem_attr_array, gfn_t start, + gfn_t end, unsigned long attrs) { - XA_STATE(xas, &kvm->mem_attr_array, start); + XA_STATE(xas, mem_attr_array, start); unsigned long index; bool has_attrs; void *entry; @@ -2582,7 +2582,8 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, mutex_lock(&kvm->slots_lock); /* Nothing to do if the entire range as the desired attributes. */ - if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) + if (kvm_range_has_memory_attributes(&kvm->mem_attr_array, start, end, + attributes)) goto out_unlock; /* From patchwork Wed Nov 8 11:17:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162974 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843533vqo; Wed, 8 Nov 2023 03:23:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFiAaZ7E0rMAypzi8c61VtxeZPqr+s3lyl7Yp8roNulvnwVUHr9xLdDzdqcBH/i0yrcvu3i X-Received: by 2002:a05:6870:dc47:b0:1ea:30b2:3fcc with SMTP id nr7-20020a056870dc4700b001ea30b23fccmr1786596oab.31.1699442603977; Wed, 08 Nov 2023 03:23:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442603; cv=none; d=google.com; s=arc-20160816; b=Rk9Pys7BUE7NxTaIZyZDwvWsAvgKGueBhbL1o3+Jn+UTZ6LEDnPh4Erv0gNV7D5F4g 1Be7FaSyszhpfn72lUjwco/ZELRA/qa2Ki3FYzqTYHVo7l69vHOBfVT8BCc64oqhAnAT VKmvXDkwjhmIkNQgZE1cjXVmsMHPodJM+oq4Ox+SQi5AbnbFjsr723ufHF68mMAX5E58 nV2kmaBfsRfpZ4ifPaKDmO3Dtwh+gPDz3lhnmwsLQLHn4qW9fAhbg+Eo44gg+DhG2344 M4xRAeDnzgoH4/vAiLJ+Cm6LoBob5SkzfHEzPj2Q7MDtj4FKy3+XBZwVYhdtnqRUOrfR Yy9w== 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; bh=J7UoBAKDyQ8FEQ/hZE/ZqdsRX3l1s1mZYyf6joluUVw=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=kTKuzWsvwXDBqwn6d1KQBuWwGeKqFEaRWIiJE5uMTwSRK7zwNm3QKheLOv3NdsCop0 ItybFtENr3r0aWvAftFIEd8b5DrtDeOlz6tbj1SBguE9bSAbwJURIsYzPG7awf3SRrcN LyMCbq/QrWkcUDUplfmN14MqGNHUY82eAx5sqcBtlDO+UooBNjWW4VM3JoOv926Jbh9B BuCmpWzvFSCY6VBrdlCmSnxrOmsDAnGOfXs2Teln6K41Yrm1KXuWxIZnyw9a9xukct7H fwLzGwVvNK+qkWUM8iKKg0EOU7yjjUS0QmuiFtn0xmUUIcjTcjUazUXg+pUBfWoic3F7 bngQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=eKTIdGo5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id v10-20020a63610a000000b00570505c5267si3976452pgb.262.2023.11.08.03.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:23:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=eKTIdGo5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A2FDE826BB81; Wed, 8 Nov 2023 03:23:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344698AbjKHLW7 (ORCPT + 32 others); Wed, 8 Nov 2023 06:22:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344749AbjKHLWl (ORCPT ); Wed, 8 Nov 2023 06:22:41 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B8CC2101; Wed, 8 Nov 2023 03:22:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442550; x=1730978550; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=J7UoBAKDyQ8FEQ/hZE/ZqdsRX3l1s1mZYyf6joluUVw=; b=eKTIdGo5kTWW7brRwyBs8HubcaCaa+Bio9gajh4QmEzRn3QF8Z6Qha+V VSbpZ0aHF71h6EKaQ38pEVHzFv4Ad/ADlqtVTftyMLTBIqKTdrR4WoJI4 iD/tbY4wpYih0q0sPabhPnQTe3x0G4mHKe+KWGmstGMvvc7D382BP83vg E=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164959413" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-b1c0e1d0.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:22:28 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-b1c0e1d0.us-west-2.amazon.com (Postfix) with ESMTPS id 5F0088A33C; Wed, 8 Nov 2023 11:22:24 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.17.79:16387] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.43.105:2525] with esmtp (Farcaster) id 52d25461-f54c-47d3-a30b-cd79c101be6b; Wed, 8 Nov 2023 11:22:23 +0000 (UTC) X-Farcaster-Flow-ID: 52d25461-f54c-47d3-a30b-cd79c101be6b Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:22 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:18 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 20/33] KVM: x86/mmu: Decouple hugepage_has_attrs() from struct kvm's mem_attr_array Date: Wed, 8 Nov 2023 11:17:53 +0000 Message-ID: <20231108111806.92604-21-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D046UWB003.ant.amazon.com (10.13.139.174) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:23:21 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994727682996989 X-GMAIL-MSGID: 1781994727682996989 Decouple hugepage_has_attrs() from struct kvm's mem_attr_array to allow other memory attribute sources to use the function. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4ace2f8660b0..c0fd3afd6be5 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7290,19 +7290,19 @@ static void hugepage_set_mixed(struct kvm_memory_slot *slot, gfn_t gfn, lpage_info_slot(gfn, slot, level)->disallow_lpage |= KVM_LPAGE_MIXED_FLAG; } -static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, int level, unsigned long attrs) +static bool hugepage_has_attrs(struct xarray *mem_attr_array, + struct kvm_memory_slot *slot, gfn_t gfn, + int level, unsigned long attrs) { const unsigned long start = gfn; const unsigned long end = start + KVM_PAGES_PER_HPAGE(level); if (level == PG_LEVEL_2M) - return kvm_range_has_memory_attributes(&kvm->mem_attr_array, - start, end, attrs); + return kvm_range_has_memory_attributes(mem_attr_array, start, end, attrs); for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { if (hugepage_test_mixed(slot, gfn, level - 1) || - attrs != kvm_get_memory_attributes(&kvm->mem_attr_array, gfn)) + attrs != kvm_get_memory_attributes(mem_attr_array, gfn)) return false; } return true; @@ -7344,7 +7344,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, * misaligned address regardless of memory attributes. */ if (gfn >= slot->base_gfn) { - if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + if (hugepage_has_attrs(&kvm->mem_attr_array, + slot, gfn, level, attrs)) hugepage_clear_mixed(slot, gfn, level); else hugepage_set_mixed(slot, gfn, level); @@ -7366,7 +7367,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, */ if (gfn < range->end && (gfn + nr_pages) <= (slot->base_gfn + slot->npages)) { - if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + if (hugepage_has_attrs(&kvm->mem_attr_array, slot, gfn, + level, attrs)) hugepage_clear_mixed(slot, gfn, level); else hugepage_set_mixed(slot, gfn, level); @@ -7405,7 +7407,7 @@ void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm, unsigned long attrs = kvm_get_memory_attributes(&kvm->mem_attr_array, gfn); - if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + if (hugepage_has_attrs(&kvm->mem_attr_array, slot, gfn, level, attrs)) hugepage_clear_mixed(slot, gfn, level); else hugepage_set_mixed(slot, gfn, level); From patchwork Wed Nov 8 11:17:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162975 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843576vqo; Wed, 8 Nov 2023 03:23:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJjNYjJNS/6IPbLyKKKOWQyhzBU+WfVwQ/VXz+LxR/hs/HH8N2XjC2B/NY3goOAroNf41Q X-Received: by 2002:a05:6871:7248:b0:1dc:704c:7c83 with SMTP id ml8-20020a056871724800b001dc704c7c83mr1793373oac.30.1699442609939; Wed, 08 Nov 2023 03:23:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442609; cv=none; d=google.com; s=arc-20160816; b=jvX4Au/l6NgNC/K/eTnzLO7If1gqgKvelma1HEOVHJSufP4fP5Ed0d/dp59t2OHh45 jDDWRb4mr0uhSrBQz+yiY8upkwzciUGegGka8u1D/7C/YpVhB4iYiiMUDXUrcK0oVvTE lEBa3pqR0auXEyVqxxKAWrKfZ9/+dILie//x0FsGm/oiFBd8fBMZLMzkxsYdjaBPaBua gZrBXzs/n1BSoPNCQzvckSHNFDcMMp88DmqLVqx3Y/El2nc6pHRkjSFfqGykXH0dK+rz lzm36kbVUsSU6/jk/psS3k1sCE+PYH4YMdCs/wQi89W/+Fz5hgzoRDvKk/oH9vayZQx1 NfHg== 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; bh=RgZghGDVPKK1y7/jRC8uX7Dc8zxHwsko9uAHRrK/b/E=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=AkD6o3098WP514zBtOeUG6CTc8nsdb53oflxbhqb87fjiOb6jynxWOGOWjy7aY+y46 iyjZKX2wDKwoP81+tJj0lGbTNOEIjfjTsJPXWNfpN0u/h0QQrDki7ARY8esLwcqfZJ+8 +DXED0OYyJ/xuj7cCBg7dXwzc0jO9cgXovEEgaPd9kxm9NR6EpDX7zHOlkuONW+K+naW +S7vyLaq0xY43ApNMqDmlAugbSk0/W1npY3sxMITqTjeo6KCVeQK4h7zUkJl9hSf2hjv kTDbvRXsQlbiclzegNWvzz1vPtKJjaqk+7n3PDU4rK9sCWnoYhRAlxQcv2SPyqOsfvWD lU+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=KZxnAQq5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id g192-20020a636bc9000000b0057c2f61474asi1620850pgc.290.2023.11.08.03.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:23:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=KZxnAQq5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E479183AC0C1; Wed, 8 Nov 2023 03:23:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344805AbjKHLXX (ORCPT + 32 others); Wed, 8 Nov 2023 06:23:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344818AbjKHLXA (ORCPT ); Wed, 8 Nov 2023 06:23:00 -0500 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D763D1FF6; Wed, 8 Nov 2023 03:22:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442578; x=1730978578; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RgZghGDVPKK1y7/jRC8uX7Dc8zxHwsko9uAHRrK/b/E=; b=KZxnAQq5x79fecGfeJTdJwy7Asc3OjoDvMstzCK0CsEroWHde6inNPoH bAU2j0GSTfcIUcHmqvV6Wua1ulzGl2alPEfcFhkNCqK5ql5IvNSb6LsP9 lasAo5p7cJN+MQIsFD4w09XMJeESBGC7AfQ4VSo0pakLEqgS+5nD3v4yh k=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="593808027" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-d23e07e8.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:22:57 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-d23e07e8.us-east-1.amazon.com (Postfix) with ESMTPS id 6925380801; Wed, 8 Nov 2023 11:22:52 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:36451] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.43.105:2525] with esmtp (Farcaster) id 6ea11377-1737-4520-be2a-aa11258cc468; Wed, 8 Nov 2023 11:22:51 +0000 (UTC) X-Farcaster-Flow-ID: 6ea11377-1737-4520-be2a-aa11258cc468 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:51 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:46 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 21/33] KVM: Pass memory attribute array as a MMU notifier argument Date: Wed, 8 Nov 2023 11:17:54 +0000 Message-ID: <20231108111806.92604-22-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D033UWC001.ant.amazon.com (10.13.139.218) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:23:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994734248139405 X-GMAIL-MSGID: 1781994734248139405 Pass the memory attribute array through struct kvm_mmu_notifier_arg and use it in kvm_arch_post_set_memory_attributes() instead of defaulting on kvm->mem_attr_array. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 8 ++++---- include/linux/kvm_host.h | 5 ++++- virt/kvm/kvm_main.c | 1 + 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c0fd3afd6be5..c2bec2be2ba9 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7311,6 +7311,7 @@ static bool hugepage_has_attrs(struct xarray *mem_attr_array, bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range) { + struct xarray *mem_attr_array = range->arg.mem_attr_array; unsigned long attrs = range->arg.attributes; struct kvm_memory_slot *slot = range->slot; int level; @@ -7344,8 +7345,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, * misaligned address regardless of memory attributes. */ if (gfn >= slot->base_gfn) { - if (hugepage_has_attrs(&kvm->mem_attr_array, - slot, gfn, level, attrs)) + if (hugepage_has_attrs(mem_attr_array, slot, + gfn, level, attrs)) hugepage_clear_mixed(slot, gfn, level); else hugepage_set_mixed(slot, gfn, level); @@ -7367,8 +7368,7 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, */ if (gfn < range->end && (gfn + nr_pages) <= (slot->base_gfn + slot->npages)) { - if (hugepage_has_attrs(&kvm->mem_attr_array, slot, gfn, - level, attrs)) + if (hugepage_has_attrs(mem_attr_array, slot, gfn, level, attrs)) hugepage_clear_mixed(slot, gfn, level); else hugepage_set_mixed(slot, gfn, level); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 32cf05637647..652656444c45 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -256,7 +256,10 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg { pte_t pte; - unsigned long attributes; + struct { + unsigned long attributes; + struct xarray *mem_attr_array; + }; }; struct kvm_gfn_range { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6bb23eaf7aa6..f20dafaedc72 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2569,6 +2569,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, .start = start, .end = end, .arg.attributes = attributes, + .arg.mem_attr_array = &kvm->mem_attr_array, .handler = kvm_arch_post_set_memory_attributes, .on_lock = kvm_mmu_invalidate_end, .may_block = true, From patchwork Wed Nov 8 11:17:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162978 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp844197vqo; Wed, 8 Nov 2023 03:24:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IELvvSI795NOzwMVbSliPYSFQQysskftLw/Iicm16EMZ4j+36M3j+s+TuPmPTKG0XHKNBDD X-Received: by 2002:a05:6a00:a1a:b0:6c3:75e1:9dcd with SMTP id p26-20020a056a000a1a00b006c375e19dcdmr1458162pfh.18.1699442683223; Wed, 08 Nov 2023 03:24:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442683; cv=none; d=google.com; s=arc-20160816; b=KE2lBd7jpo6/gQYdpi4zsZHp7sVxAF7jqiIxDpuvLIdV/qsDFH1vfCKz1Xj2L8szBl pW3liMjyb1vh9kJZO4RTfwhHdlPTUMopQPR3nnPcx3MQ1YGu7LwR2WTQk5auOCcHz1Tp NM44fFznnVhCOoZz8zaa92dSsqUGXnEk4v2NCdH/AOuvgY5GevWY3f4fYZrZj3xG3ZpX Lz4zgb57L8gz+f1jtxSVD0VMI9bRrKlboqqX0xTjXr2mOkSD91WuLc/X+dQmeklvcPCq BntfvkBa25V916b0GU4GqCoCuEspmpraSeoL2pWMVjp3T3PoeUQo2dZi4ZF7XmluXcW2 8NHQ== 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; bh=c+LzbRrbsSUyvlGgFH27jCwf7iTA/jQxajqqAaH5CaQ=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=lWrEtIPWi7A0N5YZK0yIUIaW+aVGzh+vqRjHnuzCjkpAmZNevLz/uiR76i7cvy8msq CJZR9wm3xNy8MvgA5cPmSfSXUhvkzdw9KoLAtqr45dva6foYd6Ic60iqNobPGmVUh6aK PR/5ifqPh5PgdYiJVBseY44JAvvpOs/H5iVdjBweqWkL4w/i3O8+P9c2S5LhLzyr8HJ5 rGaCDLMJa9ahY/J8JxLbAenS0yfxgLn4EQrtV5C2gOLwsVh0zdC/PfQvOC/BYi2jbT+9 mUAHtzsTcsc5v0Ab4dmLUvu4ESCemqEbofNbnl3vE5IfesexcTttubuzcaRsimkORpah lq5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fx21Mtjt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id s10-20020a056a00194a00b006b8e367ed42si13511486pfk.54.2023.11.08.03.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:24:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fx21Mtjt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7CAE1826BBBE; Wed, 8 Nov 2023 03:24:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344476AbjKHLXe (ORCPT + 32 others); Wed, 8 Nov 2023 06:23:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344709AbjKHLXQ (ORCPT ); Wed, 8 Nov 2023 06:23:16 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AA8A1FE7; Wed, 8 Nov 2023 03:23:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442589; x=1730978589; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c+LzbRrbsSUyvlGgFH27jCwf7iTA/jQxajqqAaH5CaQ=; b=fx21MtjtuEYg4WSnWWtyzoP5MoH0tXFLtnQBIffZJ8BwJx9rNFKxkXqY uCthuyHuyYHVocvqF1+i2tQPqXX346ecbzUbofxrbUEfzzZbprlZ32WPv wFZgRWZ1XrOOheF9kYWirku2+mWbD1Gdht/jFGPjEwKkU+/mL2UUSZ3Mq c=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164959605" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-cadc3fbd.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:23:07 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-cadc3fbd.us-west-2.amazon.com (Postfix) with ESMTPS id 1A2F0A12E2; Wed, 8 Nov 2023 11:23:02 +0000 (UTC) Received: from EX19MTAEUA002.ant.amazon.com [10.0.17.79:48574] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.32.187:2525] with esmtp (Farcaster) id 78327385-25a6-4125-94a0-25fd575b12d8; Wed, 8 Nov 2023 11:23:01 +0000 (UTC) X-Farcaster-Flow-ID: 78327385-25a6-4125-94a0-25fd575b12d8 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:01 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:22:56 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 23/33] KVM: Expose memory attribute helper functions unanimously Date: Wed, 8 Nov 2023 11:17:56 +0000 Message-ID: <20231108111806.92604-24-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D033UWC001.ant.amazon.com (10.13.139.218) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:24:39 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994811070482358 X-GMAIL-MSGID: 1781994811070482358 Expose memory attribute helper functions even when CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES is disabled. Other KVM features, like Hyper-V VSM, make use of memory attributes but don't rely on the KVM ioctl. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/mmu/mmu.c | 2 +- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c2bec2be2ba9..a76028aa8fb3 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7250,7 +7250,6 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm) kthread_stop(kvm->arch.nx_huge_page_recovery_thread); } -#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range) { @@ -7377,6 +7376,7 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, return false; } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm, struct kvm_memory_slot *slot) { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ad104794037f..45e3e261755d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2387,7 +2387,6 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; } -#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES static inline unsigned long kvm_get_memory_attributes(struct xarray *mem_attr_array, gfn_t gfn) { @@ -2404,6 +2403,7 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 74c4c42b2126..b3f4b200f438 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2435,7 +2435,6 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, } #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ -#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES /* * Returns true if _all_ gfns in the range [@start, @end) have attributes * matching @attrs. @@ -2472,14 +2471,6 @@ bool kvm_range_has_memory_attributes(struct xarray *mem_attr_array, gfn_t start, return has_attrs; } -static u64 kvm_supported_mem_attributes(struct kvm *kvm) -{ - if (!kvm || kvm_arch_has_private_mem(kvm)) - return KVM_MEMORY_ATTRIBUTE_PRIVATE; - - return 0; -} - static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, struct kvm_mmu_notifier_range *range) { @@ -2644,6 +2635,15 @@ int kvm_ioctl_set_mem_attributes(struct kvm *kvm, struct xarray *mem_attr_array, attrs->attributes); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static u64 kvm_supported_mem_attributes(struct kvm *kvm) +{ + if (!kvm || kvm_arch_has_private_mem(kvm)) + return KVM_MEMORY_ATTRIBUTE_PRIVATE; + + return 0; +} + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { From patchwork Wed Nov 8 11:17:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162983 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp845409vqo; Wed, 8 Nov 2023 03:27:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOJaDo0yym2sRtr/xhC47GXKlr7L+IrBSGGlfiL8JHzz8/9fOzQUTJiiKiJMNLoapb7IUE X-Received: by 2002:a17:902:9041:b0:1ca:b820:74ed with SMTP id w1-20020a170902904100b001cab82074edmr1729891plz.14.1699442833998; Wed, 08 Nov 2023 03:27:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442833; cv=none; d=google.com; s=arc-20160816; b=GclYJdjknckOvPwJBCr0FYO8fRtZ/fB8Eil7MUPLTIg0amu4GIplRFOfGLOfb7KWaT g9RLhqLHiqSNr4pNUkF7n5mpaeko/FE1G2pgr9i5XMujKaRjsSor1Bxpob0WCNhnwlio ieLiEesLkKLGNf7CheE0shV7SZHttQkP5s3A9ghTREYpnFsX6wqNw3vDGDiedGhZCT0x jwxBWcV/hcFam+Hja+RH0CAQk0MuFX1UkJYzevhKAePVMUYcduvdOrOL/miRkEmrrbh3 RLswwCsPdImMlt3GUTVpRgNpSiyT+bphyRrhMje5XH0S1RdUz68St5D0W4qZAUCtZye8 SFGg== 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; bh=+clN3KXok8ZiZV9OuN8bZTBQbm7LUM4xz0FNLSqP1yE=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=k+eR1xeY6xfuGcjdFde9snC5qPSRZlqgrC4TxwrpD8cK0Tj8fee/oLbpHfOKZIja47 5RMSsfblaetJ+dS57dK2chjjFzzL3Dncbw+pdSUh6iZ7eHsF6j2zNZxB0NqTVWZdSjih pN/fRKQrUW+jy5qBQmmdPieOHUeAGgRtCWGgNRpRzVRch366NLXHZKmwuJUt7hwaK6tC 9RXfKsEaq571t8B/mzKb7U23lBek94uDzY10gu0lS4R5KJJhYpC1cvAHRyKnxH2awYvM JOZ6ApJbtyWrQNILlPFK/y3gRMnvWa4LI/kx0YnKR2Ss2lLdwy/ekCnQNt3PLFgGUoxs dFlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=dsR9+nlN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id q5-20020a170902dac500b001c5f37aeebdsi2187428plx.579.2023.11.08.03.27.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:27:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=dsR9+nlN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7C6468097160; Wed, 8 Nov 2023 03:26:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230323AbjKHLZz (ORCPT + 32 others); Wed, 8 Nov 2023 06:25:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344798AbjKHLXk (ORCPT ); Wed, 8 Nov 2023 06:23:40 -0500 Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40B892121; Wed, 8 Nov 2023 03:23:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442618; x=1730978618; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+clN3KXok8ZiZV9OuN8bZTBQbm7LUM4xz0FNLSqP1yE=; b=dsR9+nlNTvZWeN36ejoNmpatN4/sGj4vb+gYBXXzAUK9DNcGWYxrzcHa 5mFbkC4BobvbVurjLKznBiWxyIDlS1NdtuzwFIjhDafgssbNWdE61ND1T LDMga9lGx/YD2m3KACHUM3y5y8TCyykxkt7CheOxLSVlX2DgSJRQWB7VP U=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="42020483" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-366646a6.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:23:34 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan2.iad.amazon.com [10.32.235.34]) by email-inbound-relay-iad-1a-m6i4x-366646a6.us-east-1.amazon.com (Postfix) with ESMTPS id 7D955A685E; Wed, 8 Nov 2023 11:23:31 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:25245] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id 5d05d07b-01a7-4a6a-845a-8ba989f4a5df; Wed, 8 Nov 2023 11:23:30 +0000 (UTC) X-Farcaster-Flow-ID: 5d05d07b-01a7-4a6a-845a-8ba989f4a5df Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:30 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:25 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 24/33] KVM: x86: hyper-v: Introduce KVM VTL device Date: Wed, 8 Nov 2023 11:17:57 +0000 Message-ID: <20231108111806.92604-25-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D035UWB001.ant.amazon.com (10.13.138.33) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:26:39 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994969328090891 X-GMAIL-MSGID: 1781994969328090891 Introduce a new KVM device aimed at tracking partition wide VTL state, it'll be the one responsible from keeping track of VTL's memory protections. For now its functionality it's limited, it only exposes its VTL level through a device attribute. Additionally, the device type is only registered if the VSM cap is enabled. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 68 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/hyperv.h | 3 ++ arch/x86/kvm/x86.c | 3 ++ include/uapi/linux/kvm.h | 5 +++ 4 files changed, 79 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index a266c5d393f5..0d8402dba596 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -3022,3 +3022,71 @@ int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *stat state->vsm_code_page_offsets = hv->vsm_code_page_offsets.as_u64; return 0; } + +struct kvm_hv_vtl_dev { + int vtl; +}; + +static int kvm_hv_vtl_get_attr(struct kvm_device *dev, + struct kvm_device_attr *attr) +{ + struct kvm_hv_vtl_dev *vtl_dev = dev->private; + + switch (attr->group) { + case KVM_DEV_HV_VTL_GROUP: + switch (attr->attr){ + case KVM_DEV_HV_VTL_GROUP_VTLNUM: + return put_user(vtl_dev->vtl, (u32 __user *)attr->addr); + } + } + + return -EINVAL; +} + +static void kvm_hv_vtl_release(struct kvm_device *dev) +{ + struct kvm_hv_vtl_dev *vtl_dev = dev->private; + + kfree(vtl_dev); + kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */ +} + +static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type); + +static struct kvm_device_ops kvm_hv_vtl_ops = { + .name = "kvm-hv-vtl", + .create = kvm_hv_vtl_create, + .release = kvm_hv_vtl_release, + .get_attr = kvm_hv_vtl_get_attr, +}; + +static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type) +{ + struct kvm_hv_vtl_dev *vtl_dev; + struct kvm_device *tmp; + int vtl = 0; + + vtl_dev = kzalloc(sizeof(*vtl_dev), GFP_KERNEL_ACCOUNT); + if (!vtl_dev) + return -ENOMEM; + + /* Device creation is protected by kvm->lock */ + list_for_each_entry(tmp, &dev->kvm->devices, vm_node) + if (tmp->ops == &kvm_hv_vtl_ops) + vtl++; + + vtl_dev->vtl = vtl; + dev->private = vtl_dev; + + return 0; +} + +int kvm_hv_vtl_dev_register(void) +{ + return kvm_register_device_ops(&kvm_hv_vtl_ops, KVM_DEV_TYPE_HV_VSM_VTL); +} + +void kvm_hv_vtl_dev_unregister(void) +{ + kvm_unregister_device_ops(KVM_DEV_TYPE_HV_VSM_VTL); +} diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 605e80b9e5eb..3cc664e144d8 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -269,4 +269,7 @@ static inline void kvm_mmu_role_set_hv_bits(struct kvm_vcpu *vcpu, role->vtl = kvm_hv_get_active_vtl(vcpu); } +int kvm_hv_vtl_dev_register(void); +void kvm_hv_vtl_dev_unregister(void); + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bf4891bc044e..82d3b86d9c93 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6521,6 +6521,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, mutex_unlock(&kvm->lock); break; case KVM_CAP_HYPERV_VSM: + kvm_hv_vtl_dev_register(); kvm->arch.hyperv.hv_enable_vsm = true; r = 0; break; @@ -9675,6 +9676,8 @@ void kvm_x86_vendor_exit(void) mutex_lock(&vendor_module_lock); kvm_x86_ops.hardware_enable = NULL; mutex_unlock(&vendor_module_lock); + + kvm_hv_vtl_dev_unregister(); } EXPORT_SYMBOL_GPL(kvm_x86_vendor_exit); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0ddffb8b0c99..bd97c9852142 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1471,6 +1471,9 @@ struct kvm_device_attr { #define KVM_DEV_VFIO_GROUP_DEL KVM_DEV_VFIO_FILE_DEL #define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3 +#define KVM_DEV_HV_VTL_GROUP 1 +#define KVM_DEV_HV_VTL_GROUP_VTLNUM 1 + enum kvm_device_type { KVM_DEV_TYPE_FSL_MPIC_20 = 1, #define KVM_DEV_TYPE_FSL_MPIC_20 KVM_DEV_TYPE_FSL_MPIC_20 @@ -1494,6 +1497,8 @@ enum kvm_device_type { #define KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_RISCV_AIA, #define KVM_DEV_TYPE_RISCV_AIA KVM_DEV_TYPE_RISCV_AIA + KVM_DEV_TYPE_HV_VSM_VTL, +#define KVM_DEV_TYPE_HV_VSM_VTL KVM_DEV_TYPE_HV_VSM_VTL KVM_DEV_TYPE_MAX, }; From patchwork Wed Nov 8 11:17:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162976 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp843939vqo; Wed, 8 Nov 2023 03:24:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsl85hVADgUOSLHsh7SrqLrKOYFJIKEGOaV2Z2W/8f10h9wy1NWXfJZZC4VR9rtmtHY0RF X-Received: by 2002:a05:6a20:938a:b0:17d:faed:603c with SMTP id x10-20020a056a20938a00b0017dfaed603cmr2052318pzh.11.1699442651020; Wed, 08 Nov 2023 03:24:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442651; cv=none; d=google.com; s=arc-20160816; b=Ye7zTd8R9UW21f52Z521dk5bChBaLGTJY3A0VJMUVcCa8YDn9g77EqNKeYBadiAqLo hllJkB5KgGTwvmxEynbxjavp4/mx3WlVkXezYh5iuH1iYRvc4shco0h39QyOABkmP4u8 un9iPpbNRQR97+Ll5XC6n5x9/ozAcqIFKVp/ymrE6N0BW7YeDqPpXVHzsuiJZFyRkjTG W/rTpgfLDSIh8FVFhGbfrvOEufF/eV7ZiWhaFUShhhd3vyJwKmB5FY+Ssv4mkgPSwjkI 9PYHa3fl1OcvI8LU/PBVd+xWpxA6V3N6c6f0Kd58cv3Om7DeDIa0DxY9yB2QRbfKk/xz 4dSg== 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; bh=76x6TGe5VGp4tVKNePmei2SbV9igg1iA9dnaIZiL/jY=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=tZhKA5R4Y226XTVYsQ3Nzr6+gOsQm06seB8SYC50Xn5Cd8H6QpQHGTXRFTfzFuGPEj gJ9EyN6psJcgEnjBPFnOJeijB69EzSD9PTbKj7E7Vw37dmZIjnJp1NCPBRgJZB5hdiLp 4ZpNT0aoxYPZLQTI5dUQjYbwPgf4SPeWkE6SzTvVh0/hDPW9+sBOJrzkCrPkngmxwp0g aYN38cX3Qg8QHtEIn4TKc7ct95qV1DwWeB3qXiup7NuaKTQnh3CFV52gGZXzkwrY6fZz 44lWP8J8gdSJ/UgpI2OZZkCccVFtxICyMxL/czovr5EXFop9X/PPJxUpn182bRJfDjrt IuZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=i97E7MAB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id fe9-20020a056a002f0900b006c2d6a04897si11503907pfb.396.2023.11.08.03.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:24:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=i97E7MAB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 22BF183AC0EB; Wed, 8 Nov 2023 03:24:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344504AbjKHLYE (ORCPT + 32 others); Wed, 8 Nov 2023 06:24:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344827AbjKHLXm (ORCPT ); Wed, 8 Nov 2023 06:23:42 -0500 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE260212E; Wed, 8 Nov 2023 03:23:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442620; x=1730978620; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=76x6TGe5VGp4tVKNePmei2SbV9igg1iA9dnaIZiL/jY=; b=i97E7MABqSRQDtyaydCEB1rsc8IE0TjnNqrtGQB1JKMxakIFXmg9HS8g jyFUBdrAZWZXmYZxbYssDPmL4sN6rRYDGpd98gfbqQyLdKnF+9/vPEEoR TBOJVkPyqscHKzBCRwvqr6iUjrR3jar3V9/EfC3amdOV9VW2uN/irtgD3 U=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="614866283" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-83883bdb.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:23:37 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2a-m6i4x-83883bdb.us-west-2.amazon.com (Postfix) with ESMTPS id 4CDD76098A; Wed, 8 Nov 2023 11:23:36 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.43.254:38586] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.17.103:2525] with esmtp (Farcaster) id 9f4672c1-7f67-4ab2-9c06-a6e837041bd2; Wed, 8 Nov 2023 11:23:35 +0000 (UTC) X-Farcaster-Flow-ID: 9f4672c1-7f67-4ab2-9c06-a6e837041bd2 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:35 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:30 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 25/33] KVM: Introduce a set of new memory attributes Date: Wed, 8 Nov 2023 11:17:58 +0000 Message-ID: <20231108111806.92604-26-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D035UWB001.ant.amazon.com (10.13.138.33) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:24:10 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994777176746332 X-GMAIL-MSGID: 1781994777176746332 Introduce the following memory attributes: - KVM_MEMORY_ATTRIBUTE_READ - KVM_MEMORY_ATTRIBUTE_WRITE - KVM_MEMORY_ATTRIBUTE_EXECUTE - KVM_MEMORY_ATTRIBUTE_NO_ACCESS Note that NO_ACCESS is necessary in order to make a distinction between the lack of attributes for a gfn, which defaults to the memory protections of the backing memory, versus explicitly prohibiting any access to that gfn. These new memory attributes will, for now, only made be available through the VSM KVM device (which we introduce in subsequent patches). Signed-off-by: Nicolas Saenz Julienne --- include/uapi/linux/kvm.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index bd97c9852142..6b875c1040eb 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2314,7 +2314,11 @@ struct kvm_memory_attributes { __u64 flags; }; +#define KVM_MEMORY_ATTRIBUTE_READ (1ULL << 0) +#define KVM_MEMORY_ATTRIBUTE_WRITE (1ULL << 1) +#define KVM_MEMORY_ATTRIBUTE_EXECUTE (1ULL << 2) #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_MEMORY_ATTRIBUTE_NO_ACCESS (1ULL << 4) #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) From patchwork Wed Nov 8 11:17:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp844183vqo; Wed, 8 Nov 2023 03:24:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHH1123HOmls0QOrqbAquX4Rw6ROmouVD7ZMPZPk2xKvy0oyqufr2Mpe6Bt3KgWSrhf7dS/ X-Received: by 2002:a05:6a20:1584:b0:16b:e46e:1246 with SMTP id h4-20020a056a20158400b0016be46e1246mr1952961pzj.30.1699442681167; Wed, 08 Nov 2023 03:24:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442681; cv=none; d=google.com; s=arc-20160816; b=A9hI+put6Mo+F6dbF7hjMMdtVQ/6MQUe7RTPMNVhtJwiaUjuBcdgzDFiiK2c5M+nD6 AtGFkaABPC0DaWQ539lpPXoZrIBxJqKWBRQFQcwpas9evb17GCCWCjmArtto8wxMMwPF r2dygLvf/GSOFH/MIRjvpF/1jV5So3Pe1PiRZqvgEmRx+GrTSFhVjjtUQ5v2+drWDHTZ F+lBkq4tCvt1TVCQ7oxj/it/HDJqv88XnuqdleIwH67OLPajhnQmqmor9+tYsTgQLdfn qL8sp2osCD3WtRwf5xKOk2ywPvX/3y1P1iunIek+AFaJ9IGQqLndDJh5DgMBuqb6o9w5 63Rg== 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; bh=xf82pqS3tbHruxRFe/9qoNTFiZgXupnu5dstVI/jtoA=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=SizulAv6Uwapw5F7A4lzsTwPZtsHq4M2ozx4PUDjYHyfaUdZzN8MCWdZB3Quil660E Rj+ohDhTyPPycP9IdepdiA3GgBKmQwaMtuMTg85bpFV26yB7Dr5zw/Za8MqxSizRPdtX K1MZPUmW809f9slwA2IV0NYQQvUbTSVAdK0jLSZBKnjQqqz0rU4doEcjL7iTODLH/y0P 1Mkrn0NDN3EkhkXRqvKtCWOBRiTnLR/DadeQtY2Glaoi+sqVJVgd/bkXCEUKMAVqjUS+ tvgjCHkffeKT6cJXOwylqpWxWs3lywbHOMZqVji9Of7QfpOJJL1V5z0IVQFeLHXj3bqj 580Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=n49ZCsyV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id g19-20020a056a0023d300b006be2d998584si13076049pfc.47.2023.11.08.03.24.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:24:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=n49ZCsyV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 76EAC83AF8A8; Wed, 8 Nov 2023 03:24:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344608AbjKHLY2 (ORCPT + 32 others); Wed, 8 Nov 2023 06:24:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344673AbjKHLYG (ORCPT ); Wed, 8 Nov 2023 06:24:06 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A11AF1FCE; Wed, 8 Nov 2023 03:23:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442639; x=1730978639; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xf82pqS3tbHruxRFe/9qoNTFiZgXupnu5dstVI/jtoA=; b=n49ZCsyVQdBfWID3w3tnCBi3s065Hx05roSFRRi6awKchxMxny7b+b69 HhnWSkpHhcSGMlybeE8G089x52+EEHlLGVd4LeZs4cL/WS55zTRzr6NwB c5+xOnVsQ4SzsfvX1nxSycy2CZGiLr1+fZZMkgEOY4RWbkrUIPtEyEE9E 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164959696" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-94edd59b.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:23:48 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-94edd59b.us-west-2.amazon.com (Postfix) with ESMTPS id 5BC8140D4F; Wed, 8 Nov 2023 11:23:46 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:21927] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id c9b4010e-6bfd-4a71-a295-fab821898611; Wed, 8 Nov 2023 11:23:45 +0000 (UTC) X-Farcaster-Flow-ID: c9b4010e-6bfd-4a71-a295-fab821898611 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:40 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:35 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 26/33] KVM: x86: hyper-vsm: Allow setting per-VTL memory attributes Date: Wed, 8 Nov 2023 11:17:59 +0000 Message-ID: <20231108111806.92604-27-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D035UWB001.ant.amazon.com (10.13.138.33) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:24:40 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994808748811777 X-GMAIL-MSGID: 1781994808748811777 Introduce KVM_SET_MEMORY_ATTRIBUTES ioctl support for VTL KVM devices. The attributes are stored in an xarray private to the VTL device. The following memory attributes are supported: - KVM_MEMORY_ATTRIBUTE_READ - KVM_MEMORY_ATTRIBUTE_WRITE - KVM_MEMORY_ATTRIBUTE_EXECUTE - KVM_MEMORY_ATTRIBUTE_NO_ACCESS Although only some combinations are valid, see code comment below. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 0d8402dba596..bcace0258af1 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -62,6 +62,10 @@ */ #define HV_EXT_CALL_MAX (HV_EXT_CALL_QUERY_CAPABILITIES + 64) +#define KVM_HV_VTL_ATTRS \ + (KVM_MEMORY_ATTRIBUTE_READ | KVM_MEMORY_ATTRIBUTE_WRITE | \ + KVM_MEMORY_ATTRIBUTE_EXECUTE | KVM_MEMORY_ATTRIBUTE_NO_ACCESS) + static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, bool vcpu_kick); @@ -3025,6 +3029,7 @@ int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *stat struct kvm_hv_vtl_dev { int vtl; + struct xarray mem_attrs; }; static int kvm_hv_vtl_get_attr(struct kvm_device *dev, @@ -3047,16 +3052,71 @@ static void kvm_hv_vtl_release(struct kvm_device *dev) { struct kvm_hv_vtl_dev *vtl_dev = dev->private; + xa_destroy(&vtl_dev->mem_attrs); kfree(vtl_dev); kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */ } +/* + * The TLFS lists the valid memory protection combinations (15.9.3): + * - No access + * - Read-only, no execute + * - Read-only, execute + * - Read/write, no execute + * - Read/write, execute + */ +static bool kvm_hv_validate_vtl_mem_attributes(struct kvm_memory_attributes *attrs) +{ + u64 attr = attrs->attributes; + + if (attr & ~KVM_HV_VTL_ATTRS) + return false; + + if (attr == KVM_MEMORY_ATTRIBUTE_NO_ACCESS) + return true; + + if (!(attr & KVM_MEMORY_ATTRIBUTE_READ)) + return false; + + return true; +} + +static long kvm_hv_vtl_ioctl(struct kvm_device *dev, unsigned int ioctl, + unsigned long arg) +{ + switch (ioctl) { + case KVM_SET_MEMORY_ATTRIBUTES: { + struct kvm_hv_vtl_dev *vtl_dev = dev->private; + struct kvm_memory_attributes attrs; + int r; + + if (copy_from_user(&attrs, (void __user *)arg, sizeof(attrs))) + return -EFAULT; + + r = -EINVAL; + if (!kvm_hv_validate_vtl_mem_attributes(&attrs)) + return r; + + r = kvm_ioctl_set_mem_attributes(dev->kvm, &vtl_dev->mem_attrs, + KVM_HV_VTL_ATTRS, &attrs); + if (r) + return r; + break; + } + default: + return -ENOTTY; + } + + return 0; +} + static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type); static struct kvm_device_ops kvm_hv_vtl_ops = { .name = "kvm-hv-vtl", .create = kvm_hv_vtl_create, .release = kvm_hv_vtl_release, + .ioctl = kvm_hv_vtl_ioctl, .get_attr = kvm_hv_vtl_get_attr, }; @@ -3076,6 +3136,7 @@ static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type) vtl++; vtl_dev->vtl = vtl; + xa_init(&vtl_dev->mem_attrs); dev->private = vtl_dev; return 0; From patchwork Wed Nov 8 11:18:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162979 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp844330vqo; Wed, 8 Nov 2023 03:24:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlvEieBd49olIvzgjvPsLov/+wYVjP4bhirxl0TxRWuEZkke/oJ6Om+MQcdsWQOTTIiEq5 X-Received: by 2002:a05:6808:103:b0:3b2:e25d:9768 with SMTP id b3-20020a056808010300b003b2e25d9768mr1776308oie.53.1699442699568; Wed, 08 Nov 2023 03:24:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442699; cv=none; d=google.com; s=arc-20160816; b=ZrBGPKpTi+qqTj+VsgZR5tBi8caHjn1t1tPtqxAZoQC5ijuN1AmIOpZYUvzhEJiIpu ioNT3KkIXjuesow3P+CIFy/WnrZFl25kIfzIYO8/VOPwA1mSxQikGAiH8DQ0duwa33vs DagVHK25YwRdFVZovucp3poeq0bJHhBm1EpGzJfRhdZurALjYlZLgRftPXp6n3a9Mvli 8ehxq6oHkbnh9IJ5d5Zos0OulWl3zlQC/f+csGnqYxsCXTGCWzdoJrKMuOXS4Q115onc lSL0lw7OQARxOM0Mi2V+cap8g/nFAs79/CvXNcRB1bTbOesp1yP70FmMmyNkkDbQRUIV B+MQ== 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; bh=6ST5pXoQneZXqxqWwZOJM6gjg/ZZDrAnOFjwipV1/Y4=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=LTzMIV/GgQKqc7Dc18dfCJ8YC2JKNd5I2DXK5HqE9hIA0FI/Ly69x+d6ga8E1eEzHF m1phD0PWzYnYq1MIe6ZXGvpaAlEqB9dRC6X8QiLNURA6fqEGuquLfzGYUldZGdglDdVU JR7SWGl/7ERM1Fm8wEdQVjzmHEu+Q5RepdSvbivIZ+ksbI5591KiqRpgDjs8yCLuuRQE PgJZTvPW4X/AHpkcaWU4eCXJeFtq9l0bpKtTJT9YrNr3STH+wgzu0UlRLzSwBmUZzjQx +IMiQ9BxAM/YCfSzX8ngUM5ynBMPaKylmqZM6ckTS9X4bdqy2kw55D1jy95RunnkEBmc dDLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="C/MdZbLS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id e186-20020a6369c3000000b005ab53fee611si4224513pgc.423.2023.11.08.03.24.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:24:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="C/MdZbLS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 0A83E826CB40; Wed, 8 Nov 2023 03:24:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344386AbjKHLYk (ORCPT + 32 others); Wed, 8 Nov 2023 06:24:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235707AbjKHLYT (ORCPT ); Wed, 8 Nov 2023 06:24:19 -0500 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFF6C212A; Wed, 8 Nov 2023 03:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442656; x=1730978656; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6ST5pXoQneZXqxqWwZOJM6gjg/ZZDrAnOFjwipV1/Y4=; b=C/MdZbLSlXUQzVOmzjqclHrGAyQn/xkIzbBNFKdvvGB/k4+2AxA7reiU pr4iglofX+uAVbi+x3s2UyCnN8I6aWoJ4I1El7gHWCeljwW1zs5lwWqXj zx3KsYhYmD7in2+9B/ABuEMCHl0/kJKks8kpHZkNMgKSbU5S5JlE91Vv9 A=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="683506250" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-93c3b254.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9105.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:24:15 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan2.iad.amazon.com [10.32.235.34]) by email-inbound-relay-iad-1a-m6i4x-93c3b254.us-east-1.amazon.com (Postfix) with ESMTPS id B2EAAE2099; Wed, 8 Nov 2023 11:24:10 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:64382] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.17.103:2525] with esmtp (Farcaster) id fa9eeb09-4794-4c6e-b14d-d42e11167bfe; Wed, 8 Nov 2023 11:24:09 +0000 (UTC) X-Farcaster-Flow-ID: fa9eeb09-4794-4c6e-b14d-d42e11167bfe Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:09 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:04 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 27/33] KVM: x86/mmu/hyper-v: Validate memory faults against per-VTL memprots Date: Wed, 8 Nov 2023 11:18:00 +0000 Message-ID: <20231108111806.92604-28-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:24:57 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994827859509368 X-GMAIL-MSGID: 1781994827859509368 Introduce a new step in __kvm_faultin_pfn() that'll validate the fault against the vCPU's VTL protections and generate a user space exit when invalid. Note that kvm_hv_faultin_pfn() has to be run after resolving the fault against the memslots, since that operation steps over 'fault->map_writable'. Non VSM users shouldn't see any behaviour change. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 66 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/hyperv.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 +++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index bcace0258af1..eb6a4848e306 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -42,6 +42,8 @@ #include "irq.h" #include "fpu.h" +#include "mmu/mmu_internal.h" + #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, HV_VCPUS_PER_SPARSE_BANK) /* @@ -3032,6 +3034,55 @@ struct kvm_hv_vtl_dev { struct xarray mem_attrs; }; +static struct xarray *kvm_hv_vsm_get_memprots(struct kvm_vcpu *vcpu); + +bool kvm_hv_vsm_access_valid(struct kvm_page_fault *fault, unsigned long attrs) +{ + if (attrs == KVM_MEMORY_ATTRIBUTE_NO_ACCESS) + return false; + + /* We should never get here without read permissions, force a fault. */ + if (WARN_ON_ONCE(!(attrs & KVM_MEMORY_ATTRIBUTE_READ))) + return false; + + if (fault->write && !(attrs & KVM_MEMORY_ATTRIBUTE_WRITE)) + return false; + + if (fault->exec && !(attrs & KVM_MEMORY_ATTRIBUTE_EXECUTE)) + return false; + + return true; +} + +static unsigned long kvm_hv_vsm_get_memory_attributes(struct kvm_vcpu *vcpu, + gfn_t gfn) +{ + struct xarray *prots = kvm_hv_vsm_get_memprots(vcpu); + + if (!prots) + return 0; + + return xa_to_value(xa_load(prots, gfn)); +} + +int kvm_hv_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) +{ + unsigned long attrs; + + attrs = kvm_hv_vsm_get_memory_attributes(vcpu, fault->gfn); + if (!attrs) + return RET_PF_CONTINUE; + + if (kvm_hv_vsm_access_valid(fault, attrs)) { + fault->map_executable = + !!(attrs & KVM_MEMORY_ATTRIBUTE_EXECUTE); + fault->map_writable = !!(attrs & KVM_MEMORY_ATTRIBUTE_WRITE); + return RET_PF_CONTINUE; + } + + return -EFAULT; +} + static int kvm_hv_vtl_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) { @@ -3120,6 +3171,21 @@ static struct kvm_device_ops kvm_hv_vtl_ops = { .get_attr = kvm_hv_vtl_get_attr, }; +static struct xarray *kvm_hv_vsm_get_memprots(struct kvm_vcpu *vcpu) +{ + struct kvm_hv_vtl_dev *vtl_dev; + struct kvm_device *tmp; + + list_for_each_entry(tmp, &vcpu->kvm->devices, vm_node) + if (tmp->ops == &kvm_hv_vtl_ops) { + vtl_dev = tmp->private; + if (vtl_dev->vtl == kvm_hv_get_active_vtl(vcpu)) + return &vtl_dev->mem_attrs; + } + + return NULL; +} + static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type) { struct kvm_hv_vtl_dev *vtl_dev; diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 3cc664e144d8..ae781b4d4669 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -271,5 +271,6 @@ static inline void kvm_mmu_role_set_hv_bits(struct kvm_vcpu *vcpu, int kvm_hv_vtl_dev_register(void); void kvm_hv_vtl_dev_unregister(void); +int kvm_hv_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); #endif diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a76028aa8fb3..ba454c7277dc 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4374,7 +4374,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->write, &fault->map_writable, &fault->hva); if (!async) - return RET_PF_CONTINUE; /* *pfn has correct page already */ + goto pf_continue; /* *pfn has correct page already */ if (!fault->prefetch && kvm_can_do_async_pf(vcpu)) { trace_kvm_try_async_get_page(fault->addr, fault->gfn); @@ -4395,6 +4395,13 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, true, NULL, fault->write, &fault->map_writable, &fault->hva); +pf_continue: + if (kvm_hv_vsm_enabled(vcpu->kvm)) { + if (kvm_hv_faultin_pfn(vcpu, fault)) { + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; + } + } return RET_PF_CONTINUE; } From patchwork Wed Nov 8 11:18:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp847625vqo; Wed, 8 Nov 2023 03:31:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLDkb5ugy6RwKdZuOo7AjmygGfkuL8x/MXTQ7qNG5apkZhdfIwDEP2Avbp9jZto79l7vBj X-Received: by 2002:a05:6e02:1ca3:b0:359:4ae3:cd9a with SMTP id x3-20020a056e021ca300b003594ae3cd9amr1883264ill.30.1699443093534; Wed, 08 Nov 2023 03:31:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699443093; cv=none; d=google.com; s=arc-20160816; b=X1Lsb1zMREQRmxPZxLja5tJ5+ceGCHo/OemlDB8pAYcCx6XHGrUrNY0n/dFNFbwKWg Kk0/ctP26+JmrihUSM1uc8f7rU0V8vcW9R7IDwWqDo9DOmLt4cX7xHrpNztpx0vL+6/4 uktC9xpLQ/X0/25cm4KS6VjzilioYtdbQtuAcNQDa87zwo1ijt/fZyMDRZHD5wiJ09dP HwC0Kjku7owefUR7i/Xo9FhUP5PmTcLYYxOp0csISmBPoOQ8j8xaoC6HXGJhi42qOsfp nriy584nBAHnA2nZRqmKWTignMbgBo89j/F/S3Rk2FF4gPCvZhItxXfHnNINSZIBF6eh B28w== 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; bh=wLo3D2RUMSrAbuSqouFk5tJc54p3vvp1g/yHmuVwsBM=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=CVcycqPKRAgbI4Ktsot6V6qXmPdck/qquyj7euGSu2D3PWwNyBIVBN1zI4R7Uxma3M LdC2dwGYy+a69DCclbHFIH5dd6FUzmj+1w1tQjWbcfbNYGIrEjOVbmcuqSzrjW96C7st pbGrcAHa+t2pV6neHYqMfcZzVD2Z48U435SzhIiFWqr9YRWD+Z5H2/GyME0KnbCHye4Z hSlxDs2Q6VfR66Ib7jEErd9IN7yeaeWfqmH3wRc8FqSx4XO/XoOn2to1D1nyU+nX4Vo9 FRAgEYGkZo7X7K0ueH6V8/UcqXNQSWAyVn7bbXVlB9Lt7jbTXbHdnAMmMNL7ovVpeAsW Gu4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=NjJluvCs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id r73-20020a632b4c000000b005b99bfe3301si4596365pgr.462.2023.11.08.03.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:31:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=NjJluvCs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 0B02A8246844; Wed, 8 Nov 2023 03:31:31 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344193AbjKHLbR (ORCPT + 32 others); Wed, 8 Nov 2023 06:31:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235592AbjKHLbO (ORCPT ); Wed, 8 Nov 2023 06:31:14 -0500 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AB311BD5; Wed, 8 Nov 2023 03:24:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442663; x=1730978663; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wLo3D2RUMSrAbuSqouFk5tJc54p3vvp1g/yHmuVwsBM=; b=NjJluvCsCOrrryqvOqaerCThOkjj/PpkYw8LvGx1ve233rFh0+IfCpxW PoywF4AxUXWtszrYkNj8fgDp11qt5QGVFhaP+rxzf/7mmr29gIJb55apl 9ftHBAnSgUSRs7U+e9gUQRS9VRy/q9gseAHzWCywm08lTRdbl9HSp9/do s=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="41962461" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-00fceed5.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:24:19 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-00fceed5.us-east-1.amazon.com (Postfix) with ESMTPS id 8D412A0DAB; Wed, 8 Nov 2023 11:24:15 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:9042] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.26.101:2525] with esmtp (Farcaster) id ed9999cc-88dc-4223-ba6e-3219e585d98f; Wed, 8 Nov 2023 11:24:14 +0000 (UTC) X-Farcaster-Flow-ID: ed9999cc-88dc-4223-ba6e-3219e585d98f Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:14 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:09 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 28/33] x86/hyper-v: Introduce memory intercept message structure Date: Wed, 8 Nov 2023 11:18:01 +0000 Message-ID: <20231108111806.92604-29-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:31:31 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781995240690734251 X-GMAIL-MSGID: 1781995240690734251 Introduce struct hv_memory_intercept_message, which is used when issuing memory intercepts to a Hyper-V VSM guest. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/hyperv-tlfs.h | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index af594aa65307..d3d74fde6da1 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -799,6 +799,82 @@ struct hv_get_vp_from_apic_id_in { u32 apic_ids[]; } __packed; + +/* struct hv_intercept_header::access_type_mask */ +#define HV_INTERCEPT_ACCESS_MASK_NONE 0 +#define HV_INTERCEPT_ACCESS_MASK_READ 1 +#define HV_INTERCEPT_ACCESS_MASK_WRITE 2 +#define HV_INTERCEPT_ACCESS_MASK_EXECUTE 4 + +/* struct hv_intercept_exception::cache_type */ +#define HV_X64_CACHE_TYPE_UNCACHED 0 +#define HV_X64_CACHE_TYPE_WRITECOMBINING 1 +#define HV_X64_CACHE_TYPE_WRITETHROUGH 4 +#define HV_X64_CACHE_TYPE_WRITEPROTECTED 5 +#define HV_X64_CACHE_TYPE_WRITEBACK 6 + +/* Intecept message header */ +struct hv_intercept_header { + __u32 vp_index; + __u8 instruction_length; +#define HV_INTERCEPT_ACCESS_READ 0 +#define HV_INTERCEPT_ACCESS_WRITE 1 +#define HV_INTERCEPT_ACCESS_EXECUTE 2 + __u8 access_type_mask; + union { + __u16 as_u16; + struct { + __u16 cpl:2; + __u16 cr0_pe:1; + __u16 cr0_am:1; + __u16 efer_lma:1; + __u16 debug_active:1; + __u16 interruption_pending:1; + __u16 reserved:9; + }; + } exec_state; + struct hv_x64_segment_register cs; + __u64 rip; + __u64 rflags; +} __packed; + +union hv_x64_memory_access_info { + __u8 as_u8; + struct { + __u8 gva_valid:1; + __u8 _reserved:7; + }; +}; + +struct hv_memory_intercept_message { + struct hv_intercept_header header; + __u32 cache_type; + __u8 instruction_byte_count; + union hv_x64_memory_access_info memory_access_info; + __u16 _reserved; + __u64 gva; + __u64 gpa; + __u8 instruction_bytes[16]; + struct hv_x64_segment_register ds; + struct hv_x64_segment_register ss; + __u64 rax; + __u64 rcx; + __u64 rdx; + __u64 rbx; + __u64 rsp; + __u64 rbp; + __u64 rsi; + __u64 rdi; + __u64 r8; + __u64 r9; + __u64 r10; + __u64 r11; + __u64 r12; + __u64 r13; + __u64 r14; + __u64 r15; +} __packed; + #include #endif From patchwork Wed Nov 8 11:18:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162980 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp844469vqo; Wed, 8 Nov 2023 03:25:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKwCf3aETnQ5oTDZk2yfUjDlzwdev18MA5GGY2TxEriVgIqPPryBQmUVcRXQD28NzB/0sf X-Received: by 2002:a05:6870:a912:b0:1e9:bfe7:e842 with SMTP id eq18-20020a056870a91200b001e9bfe7e842mr1551441oab.32.1699442716387; Wed, 08 Nov 2023 03:25:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442716; cv=none; d=google.com; s=arc-20160816; b=diesn3EWJ6bk5ES8GYSq19XIhLg4wgE/WqjsAWJk/yYhM3o3oJcJseEwNwylb3K0bD P8aibnBxs77X+KokVfU7z6QSgcqqeC6ruby6NbuZ5e/HzsisB5o2139SntnaQciG4gr7 a6rdYui32ZudCFBfGbGdee8rLHswDz0lbuqG+zF01WjXOpwi9478hPHbk9yLyMgjDC3O QXwAN34pVImjyc3Vfm98q9Tqx07Pf8EW0YKb8qdQgcHQswwKqRIhyubGyQLmG7Mk7GRo urR/NIcJDZ2pbl6rGX8NIoU+zveDL8R0QmyyH5OpV6fz2KDWp05Iik5uH4wjoQKGoMqd bnLg== 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; bh=r3WKjqld4qql+FLcnzKJq/fsWtORZpLYW1PJ8L9F7HI=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=ro4ZBkugGbCJ9pu7iuWC1Bn76//9K5p0Z9rvbkxl6KOfbOZ7r4Kalq/b31BYTeqpI5 9oOnFfSupWLeTk3068Pj6tQQhyRHEd5ILWMqiUHiiioL4IsOpXrtuw9toxWKqHAy+PI+ 9YsCTm9uooYrjNbLgGz5ifBORKWV6r8oW83l6bmwHKRQm2cHzBm2lIs6yjC97A2O00PY T5SI7aL81VGaJOZq0NPBFDfIyELdkiIHooc+zzEYMVW+PV2uB4WZF3rWWS0OhPFB9cY0 PXFf8hWewVu4WnRSoD1eitoy46/q+izq251njwoS33Rmulw1+kXd/NuaV0/w7jCBl44E o0zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lRDOzF+2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id bq10-20020a056a02044a00b00563da455862si4632015pgb.311.2023.11.08.03.25.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:25:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lRDOzF+2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 4764681A1BD1; Wed, 8 Nov 2023 03:25:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344373AbjKHLYo (ORCPT + 32 others); Wed, 8 Nov 2023 06:24:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235583AbjKHLY2 (ORCPT ); Wed, 8 Nov 2023 06:24:28 -0500 Received: from smtp-fw-52003.amazon.com (smtp-fw-52003.amazon.com [52.119.213.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4A871FC2; Wed, 8 Nov 2023 03:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442667; x=1730978667; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r3WKjqld4qql+FLcnzKJq/fsWtORZpLYW1PJ8L9F7HI=; b=lRDOzF+2/uSWtu6g6xiZPogGFIFZOEPhkU35BDMtOA7U8brSCvQ0mnZX jD8hivVkW40ZS2DOg3FS40/NOB5hG3A4n2gQbMg/ocMKnrcgInsI44svG MLXztXFUWmKvEOTR2olymytYWaNwqvRsY2Z8rlBbsWNEtaQ+AKlJonGxq w=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="618316712" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52003.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:24:26 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-54a853e6.us-east-1.amazon.com (Postfix) with ESMTPS id 2BB5348ECD; Wed, 8 Nov 2023 11:24:21 +0000 (UTC) Received: from EX19MTAEUC001.ant.amazon.com [10.0.10.100:34530] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.22.222:2525] with esmtp (Farcaster) id ada2ae65-467f-4b1a-8aba-0d7dc7fba03f; Wed, 8 Nov 2023 11:24:21 +0000 (UTC) X-Farcaster-Flow-ID: ada2ae65-467f-4b1a-8aba-0d7dc7fba03f Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:19 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:14 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 29/33] KVM: VMX: Save instruction length on EPT violation Date: Wed, 8 Nov 2023 11:18:02 +0000 Message-ID: <20231108111806.92604-30-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:25:12 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994845751914758 X-GMAIL-MSGID: 1781994845751914758 Save the length of the instruction that triggered an EPT violation in struct kvm_vcpu_arch. This will be used to populate Hyper-V VSM memory intercept messages. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 1 + 2 files changed, 3 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 1f5a85d461ce..1a854776d91e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -967,6 +967,8 @@ struct kvm_vcpu_arch { /* set at EPT violation at this point */ unsigned long exit_qualification; + u32 exit_instruction_len; + /* pv related host specific info */ struct { bool pv_unhalted; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6e502ba93141..9c83ee3a293d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5773,6 +5773,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; vcpu->arch.exit_qualification = exit_qualification; + vcpu->arch.exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); /* * Check that the GPA doesn't exceed physical memory limits, as that is From patchwork Wed Nov 8 11:18:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162984 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp845646vqo; Wed, 8 Nov 2023 03:27:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEymgLpdE1FoLY6sdr7dmrvfFfMf5Pzph9/2jWZ6fL2LGdMGvbjWdFw7EHsC/PsFX05VpUj X-Received: by 2002:a05:6a00:849:b0:693:3963:847a with SMTP id q9-20020a056a00084900b006933963847amr1411851pfk.30.1699442868883; Wed, 08 Nov 2023 03:27:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442868; cv=none; d=google.com; s=arc-20160816; b=NFj77SJjC1AVAgLE4mSYHXhrviNw1kHmpPjmRTM+FnpFu0A3KkQxyj1Of8lXdEsxcM UjBSKrxz4iTwmZ5jKITRqAR7EStHbaK0KNWFPhCCj0EhJcNvWUELiG7CrbTss2BgGnYg zsdjWHEwr9iqtPj6Y15VX2Y3R1w7DK8HUV7u/AYslp0uojopIf9tUjyXSTFXB2eFhLA7 zQH4EjvsNa8lZz/IF3OKKFDpEIX4hIHAUpsTN5/1x5qHGcyxB24S/RkLfd/bKj5taTqT SM8pLfC4YBYmjWpPdHg2hM3yVKYckQvY2JYCUN5dHAqJyrs8l3vC/oKe9xqzPRSxLqiN w3LQ== 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; bh=5PF4/rNtaEl02NErsVfK5XTs+FbISN0CKbNnl5QOpHE=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=Z+14dQd1zY8pWW8E1SxQ7OedADJxQelKetGThk41G26XnuaxzO22+gtdjQV7CxO2yB zcw1PxdyT5k2PSg17js//REF397jiMlVPWYsEJTQP6Nd3+LBSROP55LbifbOn9om1cZu L3YyGzH72lPiePsNAYgFCQTRfh6OVepPv4ufEyvV2XVw6avhdKenctQgX+8eND0IOI0p McSH3q9rTQU5iaGpYm0p/EjlGqnYS+4AFPz1vd1t3zJRgfFaOF2NmzJbzvJ/tIAKRc9y JGICMbT2dNENi0gn8ydG64Mf809zyfee3ZbyjQIqXB6JH83uZy6n1AbC6uctF3DzAB+p 1NEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fxtmLd6L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id cd20-20020a056a00421400b006be55174f3fsi13256631pfb.28.2023.11.08.03.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:27:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fxtmLd6L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 094F48207332; Wed, 8 Nov 2023 03:27:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344694AbjKHLZL (ORCPT + 32 others); Wed, 8 Nov 2023 06:25:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344714AbjKHLYy (ORCPT ); Wed, 8 Nov 2023 06:24:54 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E66241FF3; Wed, 8 Nov 2023 03:24:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442693; x=1730978693; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5PF4/rNtaEl02NErsVfK5XTs+FbISN0CKbNnl5QOpHE=; b=fxtmLd6LCgEgxifWlx4X+luOWu83oDDvV+HeLZN+iSlz/iVZizDQHiC3 TDLOyCfLK1yyvDhGqktCP8G3daYWMXMlJgLiO2FByVu21L8uLcF+N9qwn 8sS1HpyoArvRIl35XeT5zU79HC0eP1NYcSdv7oJTMOI6k2rEWtc2lW+uZ 4=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="366813194" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-e7094f15.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:24:50 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-e7094f15.us-west-2.amazon.com (Postfix) with ESMTPS id 111B540E63; Wed, 8 Nov 2023 11:24:49 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:18294] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.32.187:2525] with esmtp (Farcaster) id 804134a2-3f2d-4fb3-a228-3f0ad905b402; Wed, 8 Nov 2023 11:24:47 +0000 (UTC) X-Farcaster-Flow-ID: 804134a2-3f2d-4fb3-a228-3f0ad905b402 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:47 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:42 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 30/33] KVM: x86: hyper-v: Introduce KVM_REQ_HV_INJECT_INTERCEPT request Date: Wed, 8 Nov 2023 11:18:03 +0000 Message-ID: <20231108111806.92604-31-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D032UWB003.ant.amazon.com (10.13.139.165) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:27:40 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781995005564055439 X-GMAIL-MSGID: 1781995005564055439 Introduce a new request type, KVM_REQ_HV_INJECT_INTERCEPT which allows injecting out-of-band Hyper-V secure intercepts. For now only memory access intercepts are supported. These are triggered when access a GPA protected by a higher VTL. The memory intercept metadata is filled based on the GPA provided through struct kvm_vcpu_hv_intercept_info, and injected into the guest through SynIC message. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/include/asm/kvm_host.h | 10 +++ arch/x86/kvm/hyperv.c | 114 ++++++++++++++++++++++++++++++++ arch/x86/kvm/hyperv.h | 2 + arch/x86/kvm/x86.c | 3 + 4 files changed, 129 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 1a854776d91e..39671e075555 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -113,6 +113,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_HV_INJECT_INTERCEPT KVM_ARCH_REQ(33) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ @@ -639,6 +640,13 @@ struct kvm_vcpu_hv_tlb_flush_fifo { DECLARE_KFIFO(entries, u64, KVM_HV_TLB_FLUSH_FIFO_SIZE); }; +struct kvm_vcpu_hv_intercept_info { + struct kvm_vcpu *vcpu; + int type; + u64 gpa; + u8 access; +}; + /* Hyper-V per vcpu emulation context */ struct kvm_vcpu_hv { struct kvm_vcpu *vcpu; @@ -673,6 +681,8 @@ struct kvm_vcpu_hv { u64 vm_id; u32 vp_id; } nested; + + struct kvm_vcpu_hv_intercept_info intercept_info; }; struct kvm_hypervisor_cpuid { diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index eb6a4848e306..38ee3abdef9c 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2789,6 +2789,120 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) return 0; } +static void store_kvm_segment(const struct kvm_segment *kvmseg, + struct hv_x64_segment_register *reg) +{ + reg->base = kvmseg->base; + reg->limit = kvmseg->limit; + reg->selector = kvmseg->selector; + reg->segment_type = kvmseg->type; + reg->present = kvmseg->present; + reg->descriptor_privilege_level = kvmseg->dpl; + reg->_default = kvmseg->db; + reg->non_system_segment = kvmseg->s; + reg->_long = kvmseg->l; + reg->granularity = kvmseg->g; + reg->available = kvmseg->avl; +} + +static void deliver_gpa_intercept(struct kvm_vcpu *target_vcpu, + struct kvm_vcpu *intercepted_vcpu, u64 gpa, + u64 gva, u8 access_type_mask) +{ + ulong cr0; + struct hv_message msg = { 0 }; + struct hv_memory_intercept_message *intercept = (struct hv_memory_intercept_message *)msg.u.payload; + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(target_vcpu); + struct x86_exception e; + struct kvm_segment kvmseg; + + msg.header.message_type = HVMSG_GPA_INTERCEPT; + msg.header.payload_size = sizeof(*intercept); + + intercept->header.vp_index = to_hv_vcpu(intercepted_vcpu)->vp_index; + intercept->header.instruction_length = intercepted_vcpu->arch.exit_instruction_len; + intercept->header.access_type_mask = access_type_mask; + kvm_x86_ops.get_segment(intercepted_vcpu, &kvmseg, VCPU_SREG_CS); + store_kvm_segment(&kvmseg, &intercept->header.cs); + + cr0 = kvm_read_cr0(intercepted_vcpu); + intercept->header.exec_state.cr0_pe = (cr0 & X86_CR0_PE); + intercept->header.exec_state.cr0_am = (cr0 & X86_CR0_AM); + intercept->header.exec_state.cpl = kvm_x86_ops.get_cpl(intercepted_vcpu); + intercept->header.exec_state.efer_lma = is_long_mode(intercepted_vcpu); + intercept->header.exec_state.debug_active = 0; + intercept->header.exec_state.interruption_pending = 0; + intercept->header.rip = kvm_rip_read(intercepted_vcpu); + intercept->header.rflags = kvm_get_rflags(intercepted_vcpu); + + /* + * For exec violations we don't have a way to decode an instruction that issued a fetch + * to a non-X page because CPU points RIP and GPA to the fetch destination in the faulted page. + * Instruction length though is the length of the fetch source. + * Seems like Hyper-V is aware of that and is not trying to access those fields. + */ + if (access_type_mask == HV_INTERCEPT_ACCESS_EXECUTE) { + intercept->instruction_byte_count = 0; + } else { + intercept->instruction_byte_count = intercepted_vcpu->arch.exit_instruction_len; + if (intercept->instruction_byte_count > sizeof(intercept->instruction_bytes)) + intercept->instruction_byte_count = sizeof(intercept->instruction_bytes); + if (kvm_read_guest_virt(intercepted_vcpu, + kvm_rip_read(intercepted_vcpu), + intercept->instruction_bytes, + intercept->instruction_byte_count, &e)) + goto inject_ud; + } + + intercept->memory_access_info.gva_valid = (gva != 0); + intercept->gva = gva; + intercept->gpa = gpa; + intercept->cache_type = HV_X64_CACHE_TYPE_WRITEBACK; + kvm_x86_ops.get_segment(intercepted_vcpu, &kvmseg, VCPU_SREG_DS); + store_kvm_segment(&kvmseg, &intercept->ds); + kvm_x86_ops.get_segment(intercepted_vcpu, &kvmseg, VCPU_SREG_SS); + store_kvm_segment(&kvmseg, &intercept->ss); + intercept->rax = kvm_rax_read(intercepted_vcpu); + intercept->rcx = kvm_rcx_read(intercepted_vcpu); + intercept->rdx = kvm_rdx_read(intercepted_vcpu); + intercept->rbx = kvm_rbx_read(intercepted_vcpu); + intercept->rsp = kvm_rsp_read(intercepted_vcpu); + intercept->rbp = kvm_rbp_read(intercepted_vcpu); + intercept->rsi = kvm_rsi_read(intercepted_vcpu); + intercept->rdi = kvm_rdi_read(intercepted_vcpu); + intercept->r8 = kvm_r8_read(intercepted_vcpu); + intercept->r9 = kvm_r9_read(intercepted_vcpu); + intercept->r10 = kvm_r10_read(intercepted_vcpu); + intercept->r11 = kvm_r11_read(intercepted_vcpu); + intercept->r12 = kvm_r12_read(intercepted_vcpu); + intercept->r13 = kvm_r13_read(intercepted_vcpu); + intercept->r14 = kvm_r14_read(intercepted_vcpu); + intercept->r15 = kvm_r15_read(intercepted_vcpu); + + if (synic_deliver_msg(&hv_vcpu->synic, 0, &msg, true)) + goto inject_ud; + + return; + +inject_ud: + kvm_queue_exception(target_vcpu, UD_VECTOR); +} + +void kvm_hv_deliver_intercept(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_hv_intercept_info *info = &to_hv_vcpu(vcpu)->intercept_info; + + switch (info->type) { + case HVMSG_GPA_INTERCEPT: + deliver_gpa_intercept(vcpu, info->vcpu, info->gpa, 0, + info->access); + break; + default: + pr_warn("Unknown exception\n"); + } +} +EXPORT_SYMBOL_GPL(kvm_hv_deliver_intercept); + void kvm_hv_init_vm(struct kvm *kvm) { struct kvm_hv *hv = to_kvm_hv(kvm); diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index ae781b4d4669..8efc4916e0cb 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -273,4 +273,6 @@ int kvm_hv_vtl_dev_register(void); void kvm_hv_vtl_dev_unregister(void); int kvm_hv_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); +void kvm_hv_deliver_intercept(struct kvm_vcpu *vcpu); + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 82d3b86d9c93..f2581eec39a9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10707,6 +10707,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_HV_INJECT_INTERCEPT, vcpu)) + kvm_hv_deliver_intercept(vcpu); } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || From patchwork Wed Nov 8 11:18:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162982 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp845397vqo; Wed, 8 Nov 2023 03:27:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGXGDvctbuVPY9k8EvANo+G8YkAnTzTHKNq4a/wGchKSzAMIVBaEjro+BBhsXarluxFput8 X-Received: by 2002:a05:6a20:914b:b0:171:c88a:891e with SMTP id x11-20020a056a20914b00b00171c88a891emr1659024pzc.55.1699442832146; Wed, 08 Nov 2023 03:27:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442832; cv=none; d=google.com; s=arc-20160816; b=vQ0KpP3cDCH2Vb7hkA8cSYpWBpwUqeNJqc+SjoRxAUuZZXQOnJ39IEAt4Mwsc41VHh eAf1hT6JrgEF5WXOTYAjqz3yf3RMU40FLQtce4TWSTJL2FyNIPHEPWJ1tjTkMHVfVjFO txdvymuXDaZtIj74a11W7WcsLcZX0RYLgvVPxhAs3P8qccs6lxvqeSxUgvt6VycVfbmN RLEzzTazY4OJ3ZeksM4s1Xrvfb8VcN6pp/+6QAyDVUG8RFRH/ztqLhB+NYgQGKzE4U0d 5NDj3/4xNrFfLWIiSpfRyEqtFcLbSaAqKhchb38/FDvYQ7A00tKZDRBpNYvGGC+AbyUJ xdZA== 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; bh=9NaCDvWcyp7wzwlWclmY5VS38Gp2lSjf1eIBJksVZ5k=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=0KGcKaadkblcZe/C2AhtcxZco90U3TGsmyDr01kUOclDYKLLVSVWoEZf9npgJ0QZbu Xi1DIJHDW9u5T6rU4F0AJbtPmXjeJoblLNq7gkZjqF8hHPZ8RcE3DVs51bQgBw2cysva 4YqmtW1abeVQVcXopb8HhG976827kkH79bEpcZxvmlLn9LT/zKM+jq86Bmskkf9Lsr39 Qjuj1FsEg3eSO+GGEHUOyW0BIOpZyu/fXaRyqu1EtsQXg69KgDKN+VaUPBudtha/HZQA Y9QYppxxrLLdFVcGYhklU+0P4eMzVuznVPjx9o2Z4EbQMe8g75EPqI0E8l2meLVyhHpi cgIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=nA0FG8vJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id ay5-20020a056a00300500b006c31b7dd73fsi13239910pfb.288.2023.11.08.03.27.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:27:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=nA0FG8vJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 6E2EF8097149; Wed, 8 Nov 2023 03:25:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235610AbjKHLZa (ORCPT + 32 others); Wed, 8 Nov 2023 06:25:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344805AbjKHLZG (ORCPT ); Wed, 8 Nov 2023 06:25:06 -0500 Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 263092129; Wed, 8 Nov 2023 03:25:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442704; x=1730978704; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9NaCDvWcyp7wzwlWclmY5VS38Gp2lSjf1eIBJksVZ5k=; b=nA0FG8vJfSi2WACdQiag3d6n2OWgLcDgkjIzcczrIZkcIrfAbaaSbyuJ K9teBRcCsqnDxZjbKve2Z2vQ543MpxE0AYPO32gZ37uVKjYDrsfz1Hm8w XG9BVKeVGNQcr0UKUCKgZgn4UWL2k3RNFLbikgTac6NLhyu4K7SRXfNHc k=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="42020831" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-6e7a78d7.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:25:02 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan2.iad.amazon.com [10.32.235.34]) by email-inbound-relay-iad-1e-m6i4x-6e7a78d7.us-east-1.amazon.com (Postfix) with ESMTPS id 566FB80832; Wed, 8 Nov 2023 11:24:59 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:35325] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.10.247:2525] with esmtp (Farcaster) id 7ff9fbc7-238e-442c-8b96-7408438601b8; Wed, 8 Nov 2023 11:24:57 +0000 (UTC) X-Farcaster-Flow-ID: 7ff9fbc7-238e-442c-8b96-7408438601b8 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:57 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:24:52 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 32/33] KVM: x86: hyper-v: Implement HVCALL_TRANSLATE_VIRTUAL_ADDRESS Date: Wed, 8 Nov 2023 11:18:05 +0000 Message-ID: <20231108111806.92604-33-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D032UWB003.ant.amazon.com (10.13.139.165) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:25:56 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994967113568046 X-GMAIL-MSGID: 1781994967113568046 Introduce HVCALL_TRANSLATE_VIRTUAL_ADDRESS, the hypercall receives a GVA, generally from a less privileged VTL, and returns the GPA backing it. The GVA -> GPA conversion is done by walking the target VTL's vCPU MMU. NOTE: The hypercall implementation is incomplete and only shared for completion. Additionally we'd like to move the VTL aware parts to user-space. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 98 +++++++++++++++++++++++++++++++ arch/x86/kvm/trace.h | 23 ++++++++ include/asm-generic/hyperv-tlfs.h | 28 +++++++++ 3 files changed, 149 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 983bf8af5f64..1cb53cd0708f 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2540,6 +2540,7 @@ static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) case HVCALL_GET_VP_REGISTERS: case HVCALL_SET_VP_REGISTERS: case HVCALL_MODIFY_VTL_PROTECTION_MASK: + case HVCALL_TRANSLATE_VIRTUAL_ADDRESS: return true; } @@ -2556,6 +2557,96 @@ static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc) kvm_fpu_put(); } +static bool kvm_hv_xlate_va_validate_input(struct kvm_vcpu* vcpu, + struct hv_xlate_va_input *in, + u8 *vtl, u8 *flags) +{ + union hv_input_vtl in_vtl; + + if (in->partition_id != HV_PARTITION_ID_SELF) + return false; + + if (in->vp_index != HV_VP_INDEX_SELF && + in->vp_index != kvm_hv_get_vpindex(vcpu)) + return false; + + in_vtl.as_uint8 = in->control_flags >> 56; + *flags = in->control_flags & HV_XLATE_GVA_FLAGS_MASK; + if (*flags > (HV_XLATE_GVA_VAL_READ | + HV_XLATE_GVA_VAL_WRITE | + HV_XLATE_GVA_VAL_EXECUTE)) + pr_info_ratelimited("Translate VA control flags unsupported and will be ignored: 0x%llx\n", + in->control_flags); + + *vtl = in_vtl.use_target_vtl ? in_vtl.target_vtl : + kvm_hv_get_active_vtl(vcpu); + if (*vtl > kvm_hv_get_active_vtl(vcpu)) + return false; + + return true; +} + +static u64 kvm_hv_xlate_va_walk(struct kvm_vcpu* vcpu, u64 gva, u8 flags) +{ + struct kvm_mmu *mmu = vcpu->arch.walk_mmu; + u32 access = 0; + + if (flags & HV_XLATE_GVA_VAL_WRITE) + access |= PFERR_WRITE_MASK; + if (flags & HV_XLATE_GVA_VAL_EXECUTE) + access |= PFERR_FETCH_MASK; + + return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, mmu, gva, access, NULL); +} + +static u64 kvm_hv_translate_virtual_address(struct kvm_vcpu* vcpu, + struct kvm_hv_hcall *hc) +{ + struct hv_xlate_va_output output = {}; + struct hv_xlate_va_input input; + struct kvm_vcpu *target_vcpu; + u8 flags, target_vtl; + + if (hc->fast) { + input.partition_id = hc->ingpa; + input.vp_index = hc->outgpa & 0xFFFFFFFF; + input.control_flags = sse128_lo(hc->xmm[0]); + input.gva = sse128_hi(hc->xmm[0]); + } else { + if (kvm_read_guest(vcpu->kvm, hc->ingpa, &input, sizeof(input))) + return HV_STATUS_INVALID_HYPERCALL_INPUT; + } + + trace_kvm_hv_translate_virtual_address(input.partition_id, + input.vp_index, + input.control_flags, input.gva); + + if (!kvm_hv_xlate_va_validate_input(vcpu, &input, &target_vtl, &flags)) + return HV_STATUS_INVALID_HYPERCALL_INPUT; + + target_vcpu = kvm_hv_get_vtl_vcpu(vcpu, target_vtl); + output.gpa = kvm_hv_xlate_va_walk(target_vcpu, input.gva << PAGE_SHIFT, + flags); + if (output.gpa == INVALID_GPA) { + output.result_code = HV_XLATE_GVA_UNMAPPED; + } else { + output.gpa >>= PAGE_SHIFT; + output.result_code = HV_XLATE_GVA_SUCCESS; + output.cache_type = HV_CACHE_TYPE_X64_WB; + } + + if (hc->fast) { + memcpy(&hc->xmm[1], &output, sizeof(output)); + hc->xmm_dirty = true; + } else { + if (kvm_write_guest(vcpu->kvm, hc->outgpa, &output, + sizeof(output))) + return HV_STATUS_INVALID_HYPERCALL_INPUT; + } + + return HV_STATUS_SUCCESS; +} + static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code) { if (!hv_vcpu->enforce_cpuid) @@ -2766,6 +2857,13 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) case HVCALL_VTL_CALL: case HVCALL_VTL_RETURN: goto hypercall_userspace_exit; + case HVCALL_TRANSLATE_VIRTUAL_ADDRESS: + if (unlikely(hc.rep_cnt)) { + ret = HV_STATUS_INVALID_HYPERCALL_INPUT; + break; + } + ret = kvm_hv_translate_virtual_address(vcpu, &hc); + break; default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; break; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index ab8839c47bc7..6b908671a0cc 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1372,6 +1372,29 @@ TRACE_EVENT(kvm_hv_stimer_cleanup, __entry->vcpu_id, __entry->timer_index) ); +TRACE_EVENT(kvm_hv_translate_virtual_address, + TP_PROTO(u64 partition_id, u32 vp_index, u64 control_flags, u64 gva), + TP_ARGS(partition_id, vp_index, control_flags, gva), + + TP_STRUCT__entry( + __field(u64, partition_id) + __field(u32, vp_index) + __field(u64, control_flags) + __field(u64, gva) + ), + + TP_fast_assign( + __entry->partition_id = partition_id; + __entry->vp_index = vp_index; + __entry->control_flags = control_flags; + __entry->gva = gva; + ), + + TP_printk("partition id 0x%llx, vp index 0x%x, control flags 0x%llx, gva 0x%llx", + __entry->partition_id, __entry->vp_index, + __entry->control_flags, __entry->gva) +); + TRACE_EVENT(kvm_apicv_inhibit_changed, TP_PROTO(int reason, bool set, unsigned long inhibits), TP_ARGS(reason, set, inhibits), diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index a8b5c8a84bbc..24f983222c96 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -163,6 +163,7 @@ union hv_reference_tsc_msr { #define HVCALL_CREATE_VP 0x004e #define HVCALL_GET_VP_REGISTERS 0x0050 #define HVCALL_SET_VP_REGISTERS 0x0051 +#define HVCALL_TRANSLATE_VIRTUAL_ADDRESS 0x0052 #define HVCALL_POST_MESSAGE 0x005c #define HVCALL_SIGNAL_EVENT 0x005d #define HVCALL_POST_DEBUG_DATA 0x0069 @@ -842,4 +843,31 @@ union hv_register_vsm_code_page_offsets { u64 reserved:40; } __packed; }; + +#define HV_XLATE_GVA_SUCCESS 0 +#define HV_XLATE_GVA_UNMAPPED 1 +#define HV_XLATE_GPA_UNMAPPED 4 +#define HV_CACHE_TYPE_X64_WB 6 + +#define HV_XLATE_GVA_VAL_READ 1 +#define HV_XLATE_GVA_VAL_WRITE 2 +#define HV_XLATE_GVA_VAL_EXECUTE 4 +#define HV_XLATE_GVA_FLAGS_MASK 0x3F + +struct hv_xlate_va_input { + u64 partition_id; + u32 vp_index; + u32 reserved; + u64 control_flags; + u64 gva; +}; + +struct hv_xlate_va_output { + u32 result_code; + u32 cache_type:8; + u32 overlay_page:1; + u32 reserved:23; + u64 gpa; +}; + #endif From patchwork Wed Nov 8 11:18:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162981 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp845183vqo; Wed, 8 Nov 2023 03:26:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IE4P81E/JutCsH/B1ywYXzvyRPVY0ue6UBS5BVSs2sXu0ZlpznetEUM/ZcpmUpr2f0yytjk X-Received: by 2002:a05:6808:14d3:b0:3b2:dd7d:fc5e with SMTP id f19-20020a05680814d300b003b2dd7dfc5emr1793700oiw.10.1699442803445; Wed, 08 Nov 2023 03:26:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442803; cv=none; d=google.com; s=arc-20160816; b=s3DLLNrbuXO8p896y4FMCD/9fF9Dyvf/nJarxvkpu8EH8ifLGAOVPzKJH1Ce+Le6GS s5gTx9dFvwE70L06z2ZhtKcER4HdHXGL7skBPmnLvL3OviWP5fLQ6wzvwveaktqOERSo ++oK31ejCp387n5CKuAVfdG+aE7DGpjBR32LRw8oKeKAAiC33rQlmtQrg+VtoeqoA9Ml Lx6WiFYYsEXbTqncjxDGVu4eElEB401Gva+8cqCUIQ+UXXhyDPFCjLVP3W7o1N3KjWth bs4496kyj+39MzKIOK5AFMh/S9wstqEbcEjjuyPgsZ8lI/1TadWEw4IxV9j8rv9DKSpb WmGw== 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; bh=pKFAR2whklJOfx0aE2YbJs1djQGs6K3PfD1UPNSSZz8=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=djAhvz40eCZl04bwJFiZPIUTFAjI7gWBqCpHc1NRzh/zmu623AFIvrxvx03O5ZC7pF YZnPp1uqXWywsht9QLY+eX1nnhjudHzEsgvpE5JWORO2H8YHj0dVjPsfUpVcubn87OG7 MHWIaN5fcHDqeNna1oVUnzSCUS26t69D5fdHsv0fm6AEPj9zwGbl8YAEIi+YtvCWVWF4 lQrhPyOTp384vHg9P1Z1S7BgRHAeBIVCvBheln0qwN7j5GcIjyMg6POIxN8Hdi4PIDyp vha7nXqYHQAVML/K/lXIK1sQ9UqYwp9SOCZF9xP/Eake/p3bfwE/K52C3T9gqKEzWXtN 3DVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lpv9ixHt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id fj13-20020a056a003a0d00b006b2562dbd24si13297451pfb.220.2023.11.08.03.26.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:26:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lpv9ixHt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 202A18246337; Wed, 8 Nov 2023 03:26:38 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344524AbjKHL0F (ORCPT + 32 others); Wed, 8 Nov 2023 06:26:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344684AbjKHLZb (ORCPT ); Wed, 8 Nov 2023 06:25:31 -0500 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE0251FE6; Wed, 8 Nov 2023 03:25:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442729; x=1730978729; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pKFAR2whklJOfx0aE2YbJs1djQGs6K3PfD1UPNSSZz8=; b=lpv9ixHtv3vsCQrayxp3rZEZ1aMCCZtv2Ex0GTnkf7QGEq+jVR0RVBHD Gt5PPp2pBc9vsTotBUFYExN9lLZt7EbH78gjtCU8pSc4kLzPE+s5b7wHC +yAhecJ/HG2TTnxJrYXeP0ba+SYcm9xiM7Fv0/64ut0+/NK36E43X2T4F Q=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="375132593" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-f253a3a3.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:25:28 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-f253a3a3.us-west-2.amazon.com (Postfix) with ESMTPS id BAE6980718; Wed, 8 Nov 2023 11:25:27 +0000 (UTC) Received: from EX19MTAEUB002.ant.amazon.com [10.0.43.254:32548] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.45.210:2525] with esmtp (Farcaster) id 5bc3b812-def8-49f3-9115-267bad567819; Wed, 8 Nov 2023 11:25:26 +0000 (UTC) X-Farcaster-Flow-ID: 5bc3b812-def8-49f3-9115-267bad567819 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:25:26 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:25:21 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 33/33] Documentation: KVM: Introduce "Emulating Hyper-V VSM with KVM" Date: Wed, 8 Nov 2023 11:18:06 +0000 Message-ID: <20231108111806.92604-34-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D036UWC002.ant.amazon.com (10.13.139.242) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:26:38 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994937215657580 X-GMAIL-MSGID: 1781994937215657580 Introduce "Emulating Hyper-V VSM with KVM", which describes the KVM APIs made available to a VMM that wants to emulate Hyper-V's VSM. Signed-off-by: Nicolas Saenz Julienne --- .../virt/kvm/x86/emulating-hyperv-vsm.rst | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Documentation/virt/kvm/x86/emulating-hyperv-vsm.rst diff --git a/Documentation/virt/kvm/x86/emulating-hyperv-vsm.rst b/Documentation/virt/kvm/x86/emulating-hyperv-vsm.rst new file mode 100644 index 000000000000..8f76bf09c530 --- /dev/null +++ b/Documentation/virt/kvm/x86/emulating-hyperv-vsm.rst @@ -0,0 +1,136 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============================== +Emulating Hyper-V VSM with KVM +============================== + +Hyper-V's Virtual Secure Mode (VSM) is a virtualisation security feature +that leverages the hypervisor to create secure execution environments +within a guest. VSM is documented as part of Microsoft's Hypervisor Top +Level Functional Specification[1]. + +Emulating Hyper-V's Virtual Secure Mode with KVM requires coordination +between KVM and the VMM. Most of the VSM state and configuration is left +to be handled by user-space, but some has made its way into KVM. This +document describes the mechanisms through which a VMM can implement VSM +support. + +Virtual Trust Levels +-------------------- + +The main concept VSM introduces are Virtual Trust Levels or VTLs. Each +VTL is a CPU mode, with its own private CPU architectural state, +interrupt subsystem (limited to a local APIC), and memory access +permissions. VTLs are hierarchical, where VTL0 corresponds to normal +guest execution and VTL > 0 to privileged execution contexts. In +practice, when virtualising Windows on top of KVM, we only see VTL0 and +VTL1. Although the spec allows going all the way to VTL15. VTLs are +orthogonal to ring levels, so each VTL is capable of runnig its own +operating system and user-space[2]. + + ┌──────────────────────────────┐ ┌──────────────────────────────┐ + │ Normal Mode (VTL0) │ │ Secure Mode (VTL1) │ + │ ┌──────────────────────────┐ │ │ ┌──────────────────────────┐ │ + │ │ User-mode Processes │ │ │ │Secure User-mode Processes│ │ + │ └──────────────────────────┘ │ │ └──────────────────────────┘ │ + │ ┌──────────────────────────┐ │ │ ┌──────────────────────────┐ │ + │ │ Kernel │ │ │ │ Secure Kernel │ │ + │ └──────────────────────────┘ │ │ └──────────────────────────┘ │ + └──────────────────────────────┘ └──────────────────────────────┘ + ┌───────────────────────────────────────────────────────────────┐ + │ Hypervisor/KVM │ + └───────────────────────────────────────────────────────────────┘ + ┌───────────────────────────────────────────────────────────────┐ + │ Hardware │ + └───────────────────────────────────────────────────────────────┘ + +VTLs break the core assumption that a vCPU has a single architectural +state, lAPIC state, SynIC state, etc. As such, each VTL is modeled as a +distinct KVM vCPU, with the restriction that only one is allowed to run +at any moment in time. Having multiple KVM vCPUs tracking a single guest +CPU complicates vCPU numbering. VMs that enable VSM are expected to use +CAP_APIC_ID_GROUPS to segregate vCPUs (and their lAPICs) into different +groups. For example, a 4 CPU VSM VM will setup the APIC ID groups feature +so only the first two bits of the APIC ID are exposed to the guest. The +remaining bits represent the vCPU's VTL. The 'sibling' vCPU to VTL0's +vCPU2 at VTL3 will have an APIC ID of 0xE. Using this approach a VMM and +KVM are capable of querying a vCPU's VTL, or finding the vCPU associated +to a specific VTL. + +KVM's lAPIC implementation is aware of groups, and takes note of the +source vCPU's group when delivering IPIs. As such, it shouldn't be +possible to target a different VTL through the APIC. Interrupts are +delivered to the vCPU's lAPIC subsystem regardless of the VTL's runstate, +this also includes timers. Ultimately, any interrupt incoming from an +outside source (IOAPIC/MSIs) is routed to VTL0. + +Moving Between VTLs +------------------- + +All VSM configuration and VTL handling hypercalls are passed through to +user-space. Notably the two primitives that allow switching between VTLs. +All shared state synchronization and KVM vCPU scheduling is left to the +VMM to manage. For example, upon receiving a VTL call, the VMM stops the +vCPU that issued the hypercall, and schedules the vCPU corresponding to +the next privileged VTL. When that privileged vCPU is done executing, it +issues a VTL return hypercall, so the opposite operation happens. All +this is transparent to KVM, which limits itself to running vCPUs. + +An interrupt directed at a privileged VTL always has precedence over the +execution of lower VTLs. To honor this, the VMM can monitor events +targeted at privileged vCPUs with poll(), and should trigger an +asynchronous VTL switch whenever events become available. Additionally, +the target VTL's vCPU VP assist overlay page is used to notify the target +VTL with the reason for the switch. The VMM can keep track of the VP +assist page by installing an MSR filter for HV_X64_MSR_VP_ASSIST_PAGE. + +Hyper-V VP registers +-------------------- + +VP register hypercalls are passed through to user-space. All requests can +be fulfilled either by using already existing KVM state ioctls, or are +related to VSM's configuration, which is already handled by the VMM. Note +that HV_REGISTER_VSM_CODE_PAGE_OFFSETS is the only VSM specific VP +register the kernel controls, as such it is made available through the +KVM_HV_GET_VSM_STATE ioctl. + +Per-VTL Memory Protections +-------------------------- + +A privileged VTL can change the memory access restrictions of lower VTLs. +It does so to hide secrets from them, or to control what they are allowed +to execute. The list of memory protections allowed is[3]: + - No access + - Read-only, no execute + - Read-only, execute + - Read/write, no execute + - Read/write, execute + +VTL memory protection hypercalls are passed through to user-space, but +KVM provides an interface that allows changing memory protections on a +per-VTL basis. This is made possible by the KVM VTL device. VMMs can +create one per VTL and it exposes a ioctl, KVM_SET_MEMORY_ATTRIBUTES, +that controls the memory protections applied to that VTL. The KVM TDP MMU +is VTL aware and page faults are resolved taking into account the +corresponding VTL device's memory attributes. + +When a memory access violates VTL memory protections, KVM issues a secure +memory intercept, which is passed as a SynIC message into the next +privileged VTL. This happens transparently for the VMM. Additionally, KVM +exits with a user-space memory fault. This allows the VMM to stop the +vCPU while the secure intercept is handled by the privileged VTL. In the +good case, the instruction that triggered the fault is emulated and +control is returned to the lower VTL, in the bad case, Windows crashes +gracefully. + +Hyper-V's TLFS also states that DMA should follow VTL0's memory access +restrictions. This is out of scope for this document, as IOMMU mappings +are not handled by KVM. + +[1] https://raw.githubusercontent.com/Microsoft/Virtualization-Documentation/master/tlfs/Hypervisor%20Top%20Level%20Functional%20Specification%20v6.0b.pdf + +[2] Conceptually this design is similar to arm's TrustZone: The +hypervisor plays the role of EL3. Windows (VTL0) runs in Non-Secure +(EL0/EL1) and the secure kernel (VTL1) in Secure World (EL1s/EL0s). + +[3] TLFS 15.9.3