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);