From patchwork Mon Feb 26 08:25:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 206383 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1942816dyb; Mon, 26 Feb 2024 00:44:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7HPiVRDuFBwqECcnnG0AZlaBpezz3FPs/G9q80L0vNSzxwH7Nolc7TFIEUnU+MgQqpDdCIRzEONJIJgs39kPwh2IRkw== X-Google-Smtp-Source: AGHT+IHsXaECA8akfYy0SyQYqxnokjMPzQ4GlZJQaqT6v6U25B/adlgn3wmdAK2fKXkU1N/W3YhY X-Received: by 2002:a17:902:650c:b0:1db:d811:732a with SMTP id b12-20020a170902650c00b001dbd811732amr5086776plk.37.1708937079700; Mon, 26 Feb 2024 00:44:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937079; cv=pass; d=google.com; s=arc-20160816; b=mKwVJX0tZWp2GqvzvdyxsGn8z8+eHbXfEe0NDy45x2hNSWxA3wUpBzZ8MYLL2L0MA1 GERG4t9+dLcSSWwNYzVZ8EMZShh3blZYpIBBLZ862FBt73QJti8XjBPVta6QeQjSDYLo OIYDGpm28QfTKUdPvTkGio3LyjkG7iqpu0L8R9vg2UAwZHcnCvS/xoPemgJHtGtqeBBr QegIJR2SZUcKeyjza4s6F6t/7ULLOpWcEeHjsgv4lkvb28KUWx+WqXI3bTXRuIZlkVy4 gSp1jeQTvCO1f7zUPoElYH7MIay0l106eI9uwrOIgy1+m+oZ2+hXHO8AGbgL8POP4Zzs uTLg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=sWisP1oF0jYjSwTkHzCijbyYbjD34f4AsHQe0ODvwkA=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=IXQyECfChKFZNYPugCHRayccnWkzR9gGuKlGgwVKGZgBImgIOT6g1oTWWsNCA4/6ca +7Rak6kjqHDtubyVlBJPhDRe7RbRU/OebFghBCrXSMKAhbO0x8xa5AifBELXXtIk9OjS dAS5GkhV3CcEh8Yi6tE/ydW+dJ/b4D9ehZpea41WzDv5lFIPwClI5ZUx9QfsqYHvJ5Ax 4V6rsLW7qH8tZAqVp4pD43WoH1inH5xRa9LGyuSKswETW1Vpf16/ITKEUMieSoAl/MWt LfQclCfHVxQCovWOJ/vP1E0jh4Krox3CeGQbJbejRfanCL4FE5tyEMCfRflD77BhHMYv +nPA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aj5qsrUs; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80778-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80778-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o1-20020a1709026b0100b001dc788e13cdsi3283853plk.241.2024.02.26.00.44.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:44:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80778-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aj5qsrUs; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80778-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80778-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8E0B9B21877 for ; Mon, 26 Feb 2024 08:34:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F25B955766; Mon, 26 Feb 2024 08:27:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="aj5qsrUs" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 7010951C4C; Mon, 26 Feb 2024 08:27:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936070; cv=none; b=WYQTY+BDKmL9DtW3m6Ui+zw9b0+77douwVH3Bwc97w4YarW0/ke7fOf0MWFwFtdMJP+LvqzVM1XvbNQuOd2x7h3DeiEp4o5pqRvtvv1Jx88SRnV0QGeSMNDK6fTx9PGH1Zc/WjivMTkllaEFuSMsOOT2vO45GiRdMjTNosuKHg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936070; c=relaxed/simple; bh=o8YPZiq6LeeZQGwQQQuIE5wqxkP0AxPJNMylilPEjus=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=POK9SGWXNt5Pwsg8gV1AZkU3XtTILmPlmFpI5BibedFfnK1JgWj7qyIDW9FAyMC9JjODoEGmUe+cF0zj+ajF5vJNGbtzMzWM1wzv7OrNxyCaFTgAfFLpuLVSqwRv574d0gCiPNaLHUASTiaUGniGbXPwwczEw3+ju6hsnOrMilk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=aj5qsrUs; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936068; x=1740472068; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o8YPZiq6LeeZQGwQQQuIE5wqxkP0AxPJNMylilPEjus=; b=aj5qsrUsb0uB8pdy59w+SY8cx0gRe8kKqrf8ZYPVrwbz9hnCAOmktJmO kTXuh3VyIqc1hgpzb5sDU+hAtP9mDnWEoS21FYufyDUPq6bcjHOtYONl3 +QQFzsm/1KWbp/HOMtQ+pw03csWPVaFzC1K/+VFdho9uz5KzmzYdchFi1 QjS3i8FKtjYafwk86Rk44pHbvVk6SIeQuOVYsG6f6nLnQCJwsItL5XuUq 4XUVnRSGWuJhL46IFluOpZQAQIT12Exjg/KHVmDmtpYslZboM1c8tBFJJ mn1MpLOQX3+gMUclX71+AKnrIItX5lpK9hNJtacZUUQ6Nm12yUZ4PQ7Ge w==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="28631497" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="28631497" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6474348" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:43 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, 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 Subject: [PATCH v19 017/130] KVM: x86: Implement kvm_arch_{, pre_}vcpu_memory_mapping() Date: Mon, 26 Feb 2024 00:25:19 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791950406881996340 X-GMAIL-MSGID: 1791950406881996340 From: Isaku Yamahata Wire KVM_MEMORY_MAPPING ioctl to kvm_mmu_map_tdp_page() to populate guest memory. Signed-off-by: Isaku Yamahata --- v19: - newly added --- arch/x86/kvm/x86.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 48a61d283406..03dab4266172 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4663,6 +4663,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_MEMORY_MAPPING: r = 1; break; case KVM_CAP_EXIT_HYPERCALL: @@ -5801,6 +5802,31 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, } } +void kvm_arch_vcpu_pre_memory_mapping(struct kvm_vcpu *vcpu) +{ + kvm_mmu_reload(vcpu); +} + +int kvm_arch_vcpu_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + u8 max_level = KVM_MAX_HUGEPAGE_LEVEL; + u64 error_code = PFERR_WRITE_MASK; + u8 goal_level = PG_LEVEL_4K; + int r; + + r = kvm_mmu_map_tdp_page(vcpu, gfn_to_gpa(mapping->base_gfn), error_code, + max_level, &goal_level); + if (r) + return r; + + if (mapping->source) + mapping->source += KVM_HPAGE_SIZE(goal_level); + mapping->base_gfn += KVM_PAGES_PER_HPAGE(goal_level); + mapping->nr_pages -= KVM_PAGES_PER_HPAGE(goal_level); + return r; +} + long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) {