Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp50066lqg; Wed, 10 Apr 2024 15:08:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUwMSmSBz/jVBFQHl4mNske5Lg/M6TDDaLkJ2ooL+vhJgIGfK/PyQgoX1sHv45WCR3vf14dsfjPyjM+HdIkypkg0XAU6utxe0O4W9Stmw== X-Google-Smtp-Source: AGHT+IGQR2gThV9QzPERC8CzQ/CHVtBmi685+A94Hh3wSrXa0n2iVZRowNtpdeS/+DUo0oqeNwhd X-Received: by 2002:a05:6a21:3288:b0:1a3:3260:7555 with SMTP id yt8-20020a056a21328800b001a332607555mr5050963pzb.11.1712786919283; Wed, 10 Apr 2024 15:08:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712786919; cv=pass; d=google.com; s=arc-20160816; b=ZL326QHFja/uRkg6jOEKuU17JmZtyCsbijuyxLB3h8IacrdzjHSWokdgwdWDGIhY13 kPzqlpy6vw4/cw+cEteoDiSQU3wu1J8UnwJd2Ma63SBoZmLcsHzNgSeg3dYtIfbfk4Mz 5HgvRYwhGXXUqzung6y4ylBUBM1JWsNJ74zrM+g5jtE8/SoF1ThFjFSjhP0IohfBzzVs /Owz2o3akYWvzLe6d34X0spRn31qQ3wzX9phJpq/c352MrKkzWmi0cphq6d7RQuUTQIw N/hLOfghuRv5gV3fw0PUPcw2QRkQLkuZjovBNWLvVC6ICLF9RVfk/RqYTvWsoGFCSEts FQFg== 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=FFweYF/VeklnyAdk2quumgKEHXDNyq2sQ1Mq1RnyNKI=; fh=6GvjuWek5e/NmuoNTFo6o746VvXwCIN6aC0mGYYr2FM=; b=TV7aysioTx1q3YdCvUDFIYr5w4HBubyI5rDzMWjhXrgmIAYRMm/hdphPxV5Dxn3CFF O8mslA6t31R0ZSoY7wYwmcyffSywT6unUJU6l1lTYUMW2OcW1ZqTHLHrSbJ1nhfXStSy scM9efT7UUUllQvcBjVuot8MU67gk0Ry0s52rd/jOg1o94WnMup5pCyVLlJVHmDuNrJp 2Vlp8UJze2+dG7aLOcKGl8uFpUMQ6sbo41nZMYuJoEUjs0h/w9cqgSv33gCpu1a518tg SGx3/yzRHlApqOBLh3QvPBV44k7R5EbotoNM7/6vSpMmtmVt235qk7/rwlvUMiVpnCOT harQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AjZB8IIe; 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-139400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139400-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y22-20020a63de56000000b005dc15e82c4fsi57195pgi.170.2024.04.10.15.08.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 15:08:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AjZB8IIe; 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-139400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139400-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id ED67028651D for ; Wed, 10 Apr 2024 22:08:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E69BA190697; Wed, 10 Apr 2024 22:07:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="AjZB8IIe" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 38F1518410B; Wed, 10 Apr 2024 22:07:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712786878; cv=none; b=CR4+Nj9+PqfdvUleS48IpDx5eP/h43HgFYMW0z1oT7Fa8ZxTDjUVCIi39CXW2bXe9JcjVGQ/lz+Nn2X5puV7ECVqENlqZcJeL2DWY5kfVhujyEIiiHC8yak0dEOQGSvgCbYrKco5gwWfDByT/ZqwqQTS3bXp91S9+XY6PBrU4jI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712786878; c=relaxed/simple; bh=RGRZLnS1e0K8pXOQ+HzpK57xndukuVd+qCbivG6HpcQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UkNNKJtkoXstXGDfqUv2YQrX+1ZEiAyMp5ZfEL2l5ut/zIMQx9+BK4D8i5M8ZwQQDZuRuKUREwKAcz4v9CYU1DgzjqMpIFBvvsmJPJeEW59MYXQhxWI7Sr53zSh36gOcms1QIOV7pXoivoqPs2QaHV49VhKDlzlGHwV0NSEqx/Y= 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=AjZB8IIe; arc=none smtp.client-ip=192.198.163.17 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=1712786877; x=1744322877; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RGRZLnS1e0K8pXOQ+HzpK57xndukuVd+qCbivG6HpcQ=; b=AjZB8IIe3Jec3XJ6Z/XF+p3te2lQgfCoAgk7qo5DfYu0Mt4bAT39yVm+ gUnS4xCh4DiXXPJ7721waNWYJiQ/j+WFkvBdShOpq1ZhAlmV7crN+RJ9/ t1XbOOGQJ2NsbXgGE2k0jh549jCkS+3J8W5+mqA6Ik9V57Ah/fqJBoukl F71dtN1obFvqHoEMFTpV1PUeWyRMzSIXsduV6l19zZvLqkpUlL7NXD+6j xdUzP4xqaYjcbXdVoTzjV4AXLSIWn76MZzMKNbfd7KQM7M+pC4FcYkZ85 SqZ24dgpxdGLcjAMmtryuPCuhb9ACGpkTcTh8cDEvWTS1f1t9AI9X4s1E g==; X-CSE-ConnectionGUID: 2QDMHJKVSweq0egWJtWBlQ== X-CSE-MsgGUID: UlfDWJi6TZaRXSmNHld0pQ== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8041111" X-IronPort-AV: E=Sophos;i="6.07,191,1708416000"; d="scan'208";a="8041111" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Apr 2024 15:07:54 -0700 X-CSE-ConnectionGUID: EOOvi2lWSD+k+1BWQbxUVQ== X-CSE-MsgGUID: EUaxKyxZQs6D8QQUWpiJsQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,191,1708416000"; d="scan'208";a="25476300" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Apr 2024 15:07:53 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, Sean Christopherson , Paolo Bonzini , Michael Roth , David Matlack , Federico Parola , Kai Huang Subject: [PATCH v2 02/10] KVM: Add KVM_MAP_MEMORY vcpu ioctl to pre-populate guest memory Date: Wed, 10 Apr 2024 15:07:28 -0700 Message-ID: <819322b8f25971f2b9933bfa4506e618508ad782.1712785629.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.43.2 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 a new ioctl KVM_MAP_MEMORY in the KVM common code. It iterates on the memory range and calls the arch-specific function. Add stub arch function as a weak symbol. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- v2: - Drop need_resched(). (David, Sean, Kai) - Move cond_resched() at the end of loop. (Kai) - Drop added check. (David) - Use EINTR instead of ERESTART. (David, Sean) - Fix srcu lock leak. (Kai, Sean) - Add comment above copy_to_user(). - Drop pointless comment. (Sean) - Drop kvm_arch_vcpu_pre_map_memory(). (Sean) - Don't overwrite error code. (Sean, David) - Make the parameter in bytes, not pages. (Michael) - Drop source member in struct kvm_memory_mapping. (Sean, Michael) --- include/linux/kvm_host.h | 3 +++ include/uapi/linux/kvm.h | 9 +++++++ virt/kvm/kvm_main.c | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 48f31dcd318a..e56a0c7e5b42 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2445,4 +2445,7 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, } #endif /* CONFIG_KVM_PRIVATE_MEM */ +int kvm_arch_vcpu_map_memory(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 2190adbe3002..972aa9e054d3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -917,6 +917,7 @@ struct kvm_enable_cap { #define KVM_CAP_MEMORY_ATTRIBUTES 233 #define KVM_CAP_GUEST_MEMFD 234 #define KVM_CAP_VM_TYPES 235 +#define KVM_CAP_MAP_MEMORY 236 struct kvm_irq_routing_irqchip { __u32 irqchip; @@ -1548,4 +1549,12 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_MAP_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_memory_mapping) + +struct kvm_memory_mapping { + __u64 base_address; + __u64 size; + __u64 flags; +}; + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fb49c2a60200..f2ad9e106cdb 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4415,6 +4415,48 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) return fd; } +__weak int kvm_arch_vcpu_map_memory(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + return -EOPNOTSUPP; +} + +static int kvm_vcpu_map_memory(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + int idx, r; + + if (mapping->flags) + return -EINVAL; + + if (!PAGE_ALIGNED(mapping->base_address) || + !PAGE_ALIGNED(mapping->size) || + mapping->base_address + mapping->size <= mapping->base_address) + return -EINVAL; + + vcpu_load(vcpu); + idx = srcu_read_lock(&vcpu->kvm->srcu); + + r = 0; + while (mapping->size) { + if (signal_pending(current)) { + r = -EINTR; + break; + } + + r = kvm_arch_vcpu_map_memory(vcpu, mapping); + if (r) + break; + + cond_resched(); + } + + srcu_read_unlock(&vcpu->kvm->srcu, idx); + vcpu_put(vcpu); + + return r; +} + static long kvm_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -4616,6 +4658,18 @@ static long kvm_vcpu_ioctl(struct file *filp, r = kvm_vcpu_ioctl_get_stats_fd(vcpu); break; } + case KVM_MAP_MEMORY: { + struct kvm_memory_mapping mapping; + + r = -EFAULT; + if (copy_from_user(&mapping, argp, sizeof(mapping))) + break; + r = kvm_vcpu_map_memory(vcpu, &mapping); + /* Don't check error to tell the processed range. */ + if (copy_to_user(argp, &mapping, sizeof(mapping))) + r = -EFAULT; + break; + } default: r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); } -- 2.43.2