Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp1057491lqs; Wed, 6 Mar 2024 05:17:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWz7gk+iUqSoNntmNSXAotZTiq2C+SlnJKOLFG7dcKjDKguGvXsWGlHdmTj9AhgzBXUAoKTGjihGPHWTzlP5W5qnETwFxDFm7SlTR8qzA== X-Google-Smtp-Source: AGHT+IFFXr5yE7PsMDXSKP3akcsJd3pWxSxvBTZFS4BmiYC8YD47DxUOiY2emVxU1u3R/PPAi3Ak X-Received: by 2002:a17:906:b16:b0:a45:b506:11ca with SMTP id u22-20020a1709060b1600b00a45b50611camr2402935ejg.7.1709731078425; Wed, 06 Mar 2024 05:17:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709731078; cv=pass; d=google.com; s=arc-20160816; b=WEi3jopP+s93A6VVgZVyLD4QlcJkRRYjHXfD9PzOzS7DjjoAOEMx/Cu58pRAJtlupn ilD+oPgvBGtoNEdsyJZz3WeI6Hqo9hDxBPf7qd8vbVA6d9F4W64+D+O2VEYqMCnIIk8q KSoRtQciAgIUYQc1Zx79zM6WIjAD2nWyRKLkCAplK7lW9zDwTL7co2A88Xm+4X6Uil1W CYa+3qDy0dBzWn1uZelRjSO9u7lSt4U1pSrsjN5wwZ2t9gMeHUvO/PzdgMbukhMh7OM6 98AJxpN+S7v+61yNXBJ+D3GUa80T0rdmTCLw8rvwo+9JhbCIIgBU85bpdVpPN0TiS5ZH mKBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id:dkim-signature; bh=yvphWiGwkI80I+7R5tIZ+AXj1QsHaHq62+EVD69yCiw=; fh=EIee8CzZD92nOFt2BViAL39aXSnRmeUprq6zkqqTptE=; b=HVomlb6L7qKSutrBWhURi8bx3DB2jI00uNYu4sxjwiHdvgrBadcm0mB3WkBIBPOyn3 NLO2syymZN+RNFoRs23XJp4GTo+7B1iN6EmW+LxZes72sagc6HStWLRwUdnDbVL+/Sc0 wDk43FcMMvIKt+F5nHgVO1PVoIExOjZP6lq8TCWYAlvRgUnXiBS9tzrUhQPu/kgrh0rB xv9oJa1wYlP5npU4wu0hRzhXvE7LjFxoQu+kuBqfTQnGhZ/cQ3rgLn0FhrdIXc8bg70L 44H+aKLLN9M+gjuopBaWaQzQRHhIBNRntKbmY5MTEqPVlpDyvUdRid6ukHZrNGh52yv5 c4iQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZO2pvPgs; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id q23-20020a170906a09700b00a45ad08475asi1269450ejy.78.2024.03.06.05.17.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 05:17:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZO2pvPgs; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93405-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CB7F11F2420E for ; Wed, 6 Mar 2024 07:13:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 096C65C60A; Wed, 6 Mar 2024 07:13:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZO2pvPgs" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E0795BAF0; Wed, 6 Mar 2024 07:13:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709709210; cv=none; b=cV8RVPQvgwHn3gegvQE/PtQqFS9RQmyiegAb8m4ZQK4JyAEy2kODjNGfWXLBp35Lcyhk7eT844Vk7cmR33QgzXVuczd3m54ne8EbVbouhsGRVkACBSeGEKhjh0LB089wI5O4Mnw9xe7niAQTxwj6glnDcmVyesm/rzr+NtVDpQk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709709210; c=relaxed/simple; bh=MyleJJKYVaih73yAil6EbhYeZ5POQD3vU4WlCfEGD7U=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=clXqOVndvK3X7fhJoUnw02y/hMJ86wrJPGg/qcuFuh67VGmTq1if+/QAPyj51xDenYfbDms5x2H17Qp2IN5XtDbO/mqR+bokb0/yJq37Q7mbHnNrh/Vv4vCETN4VbWeet+gFp/FgHV+o5cfcuXauO/aDFTICv8HvoAlpYfOLRr4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ZO2pvPgs; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709709209; x=1741245209; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=MyleJJKYVaih73yAil6EbhYeZ5POQD3vU4WlCfEGD7U=; b=ZO2pvPgsnGw6Zo9GdpT4xHEWqGrFXwPbXX3UQH4hT4wvOdNzU8qytmNY aVUHUa1Mixtd3AMHpbVOnv6EPxufYiVngTj+CbInQbSkj5VUxtlAlmtOk vbOZKka0OFyS6ysj13ooNi9VVIAUOo/KSjNP4ODOR6T5/2N/twQXz2K7g pkP610azjsoY4gzSMlZ6niJz1M8X6eV+EQolPeW/md7Su8s9XpXkk0Tuq NaZ4iZ77qfMtVf7TeuYmEQHxnGJ/BcAh9+hMucb5xGWi9p5d7Pfu43WAh zc7vgfuIY3N0EPcYamU42ThUDmR/61J4P9CCSsH7KhqkppD4U9d0c/pjk Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11004"; a="8111505" X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="8111505" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 23:13:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="9737922" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.8.218]) ([10.238.8.218]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 23:13:24 -0800 Message-ID: <4555c300-5934-4563-a639-3e43d2ce405f@linux.intel.com> Date: Wed, 6 Mar 2024 15:13:22 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v19 016/130] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Sean Christopherson References: From: Binbin Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/26/2024 4:25 PM, isaku.yamahata@intel.com wrote: > From: Sean Christopherson > > Introduce a helper to directly (pun intended) fault-in a TDP page > without having to go through the full page fault path. This allows > TDX to get the resulting pfn and also allows the RET_PF_* enums to > stay in mmu.c where they belong. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > --- > v19: > - Move up for KVM_MEMORY_MAPPING. > - Add goal_level for the caller to know how many pages are mapped. > > v14 -> v15: > - Remove loop in kvm_mmu_map_tdp_page() and return error code based on > RET_FP_xxx value to avoid potential infinite loop. The caller should > loop on -EAGAIN instead now. > > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/mmu.h | 3 +++ > arch/x86/kvm/mmu/mmu.c | 58 ++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 61 insertions(+) > > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h > index 60f21bb4c27b..d96c93a25b3b 100644 > --- a/arch/x86/kvm/mmu.h > +++ b/arch/x86/kvm/mmu.h > @@ -183,6 +183,9 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, > __kvm_mmu_refresh_passthrough_bits(vcpu, mmu); > } > > +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, > + u8 max_level, u8 *goal_level); > + > /* > * Check if a given access (described through the I/D, W/R and U/S bits of a > * page fault error code pfec) causes a permission fault with the given PTE > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 61674d6b17aa..ca0c91f14063 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -4615,6 +4615,64 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) > return direct_page_fault(vcpu, fault); > } > > +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, > + u8 max_level, u8 *goal_level) > +{ > + int r; > + struct kvm_page_fault fault = (struct kvm_page_fault) { > + .addr = gpa, > + .error_code = error_code, > + .exec = error_code & PFERR_FETCH_MASK, > + .write = error_code & PFERR_WRITE_MASK, > + .present = error_code & PFERR_PRESENT_MASK, > + .rsvd = error_code & PFERR_RSVD_MASK, > + .user = error_code & PFERR_USER_MASK, > + .prefetch = false, > + .is_tdp = true, > + .is_private = error_code & PFERR_GUEST_ENC_MASK, > + .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(vcpu->kvm), > + }; > + > + WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct); > + fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn); > + > + r = mmu_topup_memory_caches(vcpu, false); Does it need a cache topup here? Both kvm_tdp_mmu_page_fault() and direct_page_fault() will call mmu_topup_memory_caches() when needed. > + if (r) > + return r; > + > + fault.max_level = max_level; > + fault.req_level = PG_LEVEL_4K; > + fault.goal_level = PG_LEVEL_4K; > + > +#ifdef CONFIG_X86_64 > + if (tdp_mmu_enabled) > + r = kvm_tdp_mmu_page_fault(vcpu, &fault); > + else > +#endif > + r = direct_page_fault(vcpu, &fault); > + > + if (is_error_noslot_pfn(fault.pfn) || vcpu->kvm->vm_bugged) > + return -EFAULT; > + > + switch (r) { > + case RET_PF_RETRY: > + return -EAGAIN; > + > + case RET_PF_FIXED: > + case RET_PF_SPURIOUS: > + if (goal_level) > + *goal_level = fault.goal_level; > + return 0; > + > + case RET_PF_CONTINUE: > + case RET_PF_EMULATE: > + case RET_PF_INVALID: > + default: > + return -EIO; > + } > +} > +EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); > + > static void nonpaging_init_context(struct kvm_mmu *context) > { > context->page_fault = nonpaging_page_fault;