Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1172676rbb; Mon, 26 Feb 2024 00:32:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVwCrDwtQfaduX3YTuBZb/4EApGkzWzhtVOxNKeHWw2XwEob0QJMVOrgXXbklI0HF91nGtAScb70sjDrNnuZhQAbzwPeZllxHnEf/W8mQ== X-Google-Smtp-Source: AGHT+IEi2DIBzr23UjbJAovu0iJgQGlHXlDpCWGWIKau6FvC5TZs4zOjQmIQ1rUCPemC24L2W0Qd X-Received: by 2002:ac8:5f11:0:b0:42e:5f6d:8f2d with SMTP id x17-20020ac85f11000000b0042e5f6d8f2dmr7337449qta.54.1708936361218; Mon, 26 Feb 2024 00:32:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708936361; cv=pass; d=google.com; s=arc-20160816; b=pzvmfknzX8QabyPtoCra5lqBGrEQnBveHaJxNsFh8UInciA2/rjsCoKCv1B8bzoHbn z8kCDXD8mhwml2MxnOBQuMHIZ68KR5zEO1wniKDPdA0dPpCC2QhegogYw0FVO/etCR9Y DsmIFMVO3KiSyUgkRdUodBmvhTzwqW0fg1xVnneAdsom2Afv7Enxwaofa/mgiK6EI4eo QEKd5MWxJlDiDzhnaI7FQbvre9ayP0dUxuXmM85U6h4B9KOBQaBu2hHDOdG4TDzxQzpF kRCcW/q8QMOWjzZX1YcBeTf2R4G82ezUVhA4iNP5+hXvGTj16tgRubUZvXCkl/xSWGWp Gxgw== 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=1sZ3dYiZs9QEmnBAKB1qxMQ/p3C96RmA9sxqyjUq+WY=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=KlxcgRUl0suQxK3QU3Ad437MBeDht0NOSdMPLtvOYl7qgssGxBqRm4pcsLrJSUsQsu dUbLdK/DK+4Horw1FgSkaWbaTp62qJ7JX/FkP+WmciJTo1gHbVpWkw1QyA+A0rxioGUc yam/fSqaO3vkuv5kvXIRNDAa54IDIs1zrnRfjqZN2WsNojL5hOwjOkXqyYpEW7gPfpLb aNFi6wgcnIyiM2h+ybC3oKd7hbYwV/Z9yDJuBO6K4OIvuvSTbbb4DQImi15MxnBL7II+ 3AtJTADHBlthHUeNItuXB++co56UxbzwajXvpUVgbqkBxxtjysYT2s3HbfqxCu1EsGhn rYOA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mVKnxvyW; 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-80775-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80775-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u3-20020ac858c3000000b0042c43fc863esi4973186qta.574.2024.02.26.00.32.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:32:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80775-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mVKnxvyW; 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-80775-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80775-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id F3B291C20F26 for ; Mon, 26 Feb 2024 08:32:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5BB2952F7B; Mon, 26 Feb 2024 08:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mVKnxvyW" 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 0C8094EB44; Mon, 26 Feb 2024 08:27:46 +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=1708936068; cv=none; b=VsML1MoBy9enias2gfd/IjpZH7nSXT3z23KlfTU2RlolotPDSRWC9o8USP6AWvmM09BRQi19oT7eMWYpebM8PtOUgcOYictKBGpHxC77WIFBolmT+r9J4vPsl2s5XjYDBFSFVxc8aLlUSaGtI4Y/5HgK5O3HGE8dbtSSB1j69mw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936068; c=relaxed/simple; bh=M0yJ/NQ+t9AuoodsI2KxRlq7XjG9s/CzpOfeudWoVdA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FgzyjHfi1rwO3GH9RYQTgVQ9VjYT5h6TwttAbErwBW4rU1O/vfWvsYECbpN17cqN+zo9tAXcWvTngE06Z/j0kshK+WBwXqd1tvuG/QyBCZq40b2hUgsY+QIhro7a8lb3g3WGUrFcxwzFJOM2yIuMZFnWGuVFEI8kJlVvMDLJUWs= 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=mVKnxvyW; 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=1708936066; x=1740472066; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=M0yJ/NQ+t9AuoodsI2KxRlq7XjG9s/CzpOfeudWoVdA=; b=mVKnxvyWLyCTuYqw6QzSOlsLS4RJcLMSBEMRkMLagLkUBLdbJr87FoZB cH/+NsbqdjS3IpYfraICpHRg0s61YJbACYaof/lIv8NrzNGwvHz0X0GQL OdsGInhlC/jT6lby7Qi2GWoYJWj9gNFsGZeGuAtf4fEXX8FI8GVJbfbIM t87czq2tcqvbJR7GW9K/k8D51JKy6QoeoOnuhr/oAOfNUmhHL8WpQY5hP 0JUDzH9p86hUBbYJp6RRYpL/b7SdOvEY/QRNAAPBZ3mRoNLjvkv0WpysU M2IzyQ+08BJMPcEKwTlY7RSqtyZUivjazlF9kKAQyzI5K6eJ0rNzJRLy+ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="28631485" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="28631485" 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:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6474338" 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:42 -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 014/130] KVM: Add KVM vcpu ioctl to pre-populate guest memory Date: Mon, 26 Feb 2024 00:25:16 -0800 Message-Id: <8b7380f1b02f8e3995f18bebb085e43165d5d682.1708933498.git.isaku.yamahata@intel.com> 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 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Add new ioctl KVM_MEMORY_MAPPING in the kvm common code. It iterates on the memory range and call arch specific function. Add stub function as weak symbol. Signed-off-by: Isaku Yamahata --- v19: - newly added --- include/linux/kvm_host.h | 4 +++ include/uapi/linux/kvm.h | 10 ++++++ virt/kvm/kvm_main.c | 67 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0520cd8d03cc..eeaf4e73317c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2389,4 +2389,8 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, } #endif /* CONFIG_KVM_PRIVATE_MEM */ +void kvm_arch_vcpu_pre_memory_mapping(struct kvm_vcpu *vcpu); +int kvm_arch_vcpu_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping); + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index c3308536482b..5e2b28934aa9 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1155,6 +1155,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MEMORY_ATTRIBUTES 233 #define KVM_CAP_GUEST_MEMFD 234 #define KVM_CAP_VM_TYPES 235 +#define KVM_CAP_MEMORY_MAPPING 236 #ifdef KVM_CAP_IRQ_ROUTING @@ -2227,4 +2228,13 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_MEMORY_MAPPING _IOWR(KVMIO, 0xd5, struct kvm_memory_mapping) + +struct kvm_memory_mapping { + __u64 base_gfn; + __u64 nr_pages; + __u64 flags; + __u64 source; +}; + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0349e1f241d1..2f0a8e28795e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4409,6 +4409,62 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) return fd; } +__weak void kvm_arch_vcpu_pre_memory_mapping(struct kvm_vcpu *vcpu) +{ +} + +__weak int kvm_arch_vcpu_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + return -EOPNOTSUPP; +} + +static int kvm_vcpu_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + bool added = false; + int idx, r = 0; + + /* flags isn't used yet. */ + if (mapping->flags) + return -EINVAL; + + /* Sanity check */ + if (!IS_ALIGNED(mapping->source, PAGE_SIZE) || + !mapping->nr_pages || + mapping->nr_pages & GENMASK_ULL(63, 63 - PAGE_SHIFT) || + mapping->base_gfn + mapping->nr_pages <= mapping->base_gfn) + return -EINVAL; + + vcpu_load(vcpu); + idx = srcu_read_lock(&vcpu->kvm->srcu); + kvm_arch_vcpu_pre_memory_mapping(vcpu); + + while (mapping->nr_pages) { + if (signal_pending(current)) { + r = -ERESTARTSYS; + break; + } + + if (need_resched()) + cond_resched(); + + r = kvm_arch_vcpu_memory_mapping(vcpu, mapping); + if (r) + break; + + added = true; + } + + srcu_read_unlock(&vcpu->kvm->srcu, idx); + vcpu_put(vcpu); + + if (added && mapping->nr_pages > 0) + r = -EAGAIN; + + return r; +} + static long kvm_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -4610,6 +4666,17 @@ static long kvm_vcpu_ioctl(struct file *filp, r = kvm_vcpu_ioctl_get_stats_fd(vcpu); break; } + case KVM_MEMORY_MAPPING: { + struct kvm_memory_mapping mapping; + + r = -EFAULT; + if (copy_from_user(&mapping, argp, sizeof(mapping))) + break; + r = kvm_vcpu_memory_mapping(vcpu, &mapping); + if (copy_to_user(argp, &mapping, sizeof(mapping))) + r = -EFAULT; + break; + } default: r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); } -- 2.25.1