From patchwork Thu Oct 24 09:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13848666 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (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 2AB123D97A; Thu, 24 Oct 2024 09:54:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763694; cv=none; b=LFDjrDvdphwQBdXU8e9j7YamU7jasTjTOl69IJfYfAhZNjRVD3/f4Afa7PKnS3OP7+bS9Kot1hcR5juakV52jZx1lIJT5Ry0WNT0RkUDfK94ZulEiQJ82K2cgw+l27M/9mMZV7Xd4XJGD6ICoQcpap1of+ZRgtZOnUInT16CDUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763694; c=relaxed/simple; bh=Vm30obtvNBOsfRWOkhOhURf+utpftRsj6VLtVbbLf3g=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=t5h+6waBukCqWlGcTt6TjrFSpsfvdTpZAjR8MXedIxhSbtet4Yc4KkMqDKDBi0HP0at+CP/BqA421fLy8Cxb4jRfyVdm6SaZTn/wUhIYjnNvfic/pRd2FZR8vHJEco2SAWSBxxdzMaHGk+NVPNI0Vu+1PCVewwGVB81HkbZ+j5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=ObSHBuuj; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="ObSHBuuj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1729763693; x=1761299693; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zGaGRh6pThs28O+PYGMQA/2BKq+XUg9DWEGRq2ZHKy0=; b=ObSHBuujhDJdh4qVRv4XEDEmJCpidT3u0WpgbODOIMRuz7SjrWAWwSNG JAjPReS6FHVgRZU1ZqPJZ6hNh9XLBTleeVI2T785DlelWs1M3Tw86Ax3o yUoKXBfp7+04OTcfCE+6gELphMrIsyiCVaZAoo0z7Fk2h14ATbkb0NOrT Q=; X-IronPort-AV: E=Sophos;i="6.11,228,1725321600"; d="scan'208";a="346285217" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2024 09:54:52 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:5902] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.24.36:2525] with esmtp (Farcaster) id 1d508ede-fb85-4b3c-82e1-78b54e777340; Thu, 24 Oct 2024 09:54:51 +0000 (UTC) X-Farcaster-Flow-ID: 1d508ede-fb85-4b3c-82e1-78b54e777340 Received: from EX19D003UWB004.ant.amazon.com (10.13.138.24) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:54:45 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D003UWB004.ant.amazon.com (10.13.138.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Thu, 24 Oct 2024 09:54:45 +0000 Received: from email-imr-corp-prod-pdx-all-2c-619df93b.us-west-2.amazon.com (10.25.36.214) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Thu, 24 Oct 2024 09:54:45 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-all-2c-619df93b.us-west-2.amazon.com (Postfix) with ESMTPS id 6CD5140397; Thu, 24 Oct 2024 09:54:43 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , Subject: [PATCH 1/4] KVM: guest_memfd: add generic post_populate callback Date: Thu, 24 Oct 2024 09:54:26 +0000 Message-ID: <20241024095429.54052-2-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241024095429.54052-1-kalyazin@amazon.com> References: <20241024095429.54052-1-kalyazin@amazon.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This adds a generic implementation of the `post_populate` callback for the `kvm_gmem_populate`. The only thing it does is populates the pages with data provided by userspace if the user pointer is not NULL, otherwise it clears the pages. This is supposed to be used by KVM_X86_SW_PROTECTED_VM VMs. Signed-off-by: Nikita Kalyazin --- virt/kvm/guest_memfd.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8f079a61a56d..954312fac462 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -620,6 +620,27 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); #ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM +static int kvm_gmem_post_populate_generic(struct kvm *kvm, gfn_t gfn_start, kvm_pfn_t pfn, + void __user *src, int order, void *opaque) +{ + int ret = 0, i; + int npages = (1 << order); + gfn_t gfn; + + if (src) { + void *vaddr = kmap_local_pfn(pfn); + + ret = copy_from_user(vaddr, src, npages * PAGE_SIZE); + if (ret) + ret = -EINVAL; + kunmap_local(vaddr); + } else + for (gfn = gfn_start, i = 0; gfn < gfn_start + npages; gfn++, i++) + clear_highpage(pfn_to_page(pfn + i)); + + return ret; +} + long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages, kvm_gmem_populate_cb post_populate, void *opaque) { From patchwork Thu Oct 24 09:54:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13848667 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (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 F29D31BD028; Thu, 24 Oct 2024 09:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763701; cv=none; b=kQc+LWsA4xD4Dokw5qryXLdeP491+LwUKPfzJOeDQA1CKT7mRyD/HuioLogHUHXN39RiQDVHiAsVJ5X5rSQKJtuG/qrY4VDT+K5jdRzEEmcT8K0qu2/tKp/p9Yb42qxvOTa/El/Aj72TJxqE+IcowsLtb7qhmGz+gDUD+0m3Y2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763701; c=relaxed/simple; bh=V6a1tnpgTrpi5tfy9KML/vCwRHQQ5hweosaua4JfMzk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zmr6VPkZaAZNIOsTYOORTx2KQVXJgx8CFYEXNYe2CZ/he8TRxZ7Z+cLELv+mskWnyMrH8c7dDOnZ/rx7KK1Lg+yQj53huujSRi3Z5qOBNiRIRi9N8bAlac48jn5F/fJCtWBfs+9+DSdHPgXNhWD7Sxva0aOCCO2aJGLuoIRQuXE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=l4TU/9pV; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="l4TU/9pV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1729763699; x=1761299699; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z9vqie+qwxIDDZ4iRqHz/Fa2emMPN1oyvmAoTg4g0iw=; b=l4TU/9pVijIF8PsCFw5jtMhUC/eY/ZMgLsGuC+mEt90dU10jXMFbdkO8 EJxCsz2IyzrjJr4H++a0nyvtbOdi3JpEwCfU9rOmdUwJe6K50b8IAjkTo fp9EYTVpULtmiAod+dlSO1DjezA2Kt8AIZq2YIXFoqNZ7T6O5UTWz+Rcm g=; X-IronPort-AV: E=Sophos;i="6.11,228,1725321600"; d="scan'208";a="346285243" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2024 09:54:59 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.38.20:48893] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.24.247:2525] with esmtp (Farcaster) id 55ebfe2c-a83b-4838-9e79-7cc1f7f0fb46; Thu, 24 Oct 2024 09:54:58 +0000 (UTC) X-Farcaster-Flow-ID: 55ebfe2c-a83b-4838-9e79-7cc1f7f0fb46 Received: from EX19D003UWC004.ant.amazon.com (10.13.138.150) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:54:58 +0000 Received: from EX19MTAUWC002.ant.amazon.com (10.250.64.143) by EX19D003UWC004.ant.amazon.com (10.13.138.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Thu, 24 Oct 2024 09:54:58 +0000 Received: from email-imr-corp-prod-iad-all-1b-85daddd1.us-east-1.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Thu, 24 Oct 2024 09:54:57 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-iad-all-1b-85daddd1.us-east-1.amazon.com (Postfix) with ESMTPS id 3F8CB40332; Thu, 24 Oct 2024 09:54:56 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , Subject: [PATCH 2/4] KVM: add KVM_GUEST_MEMFD_POPULATE ioctl for guest_memfd Date: Thu, 24 Oct 2024 09:54:27 +0000 Message-ID: <20241024095429.54052-3-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241024095429.54052-1-kalyazin@amazon.com> References: <20241024095429.54052-1-kalyazin@amazon.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The ioctl populates guest_memfd with userspace-provided data. Signed-off-by: Nikita Kalyazin --- include/linux/kvm_host.h | 3 +++ include/uapi/linux/kvm.h | 9 +++++++++ virt/kvm/guest_memfd.c | 7 +++++++ virt/kvm/kvm_main.c | 10 ++++++++++ 4 files changed, 29 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index db567d26f7b9..5b0347783598 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2505,6 +2505,9 @@ typedef int (*kvm_gmem_populate_cb)(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, long kvm_gmem_populate(struct kvm *kvm, gfn_t gfn, void __user *src, long npages, kvm_gmem_populate_cb post_populate, void *opaque); + +int kvm_gmem_guest_memfd_populate(struct kvm *kvm, + struct kvm_guest_memfd_populate *populate); #endif #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 637efc055145..5d8073de0d96 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1573,4 +1573,13 @@ struct kvm_pre_fault_memory { __u64 padding[5]; }; +struct kvm_guest_memfd_populate { + __u64 gpa; + __u64 size; + void __user *from; + __u64 flags; +}; + +#define KVM_GUEST_MEMFD_POPULATE _IOW(KVMIO, 0xd6, struct kvm_guest_memfd_populate) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 954312fac462..08630b87f0e3 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -720,4 +720,11 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long return ret && !i ? ret : i; } EXPORT_SYMBOL_GPL(kvm_gmem_populate); + +int kvm_gmem_guest_memfd_populate(struct kvm *kvm, + struct kvm_guest_memfd_populate *populate) +{ + return kvm_gmem_populate(kvm, populate->gpa >> PAGE_SHIFT, populate->from, + populate->size >> PAGE_SHIFT, kvm_gmem_post_populate_generic, NULL); +} #endif diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 05cbb2548d99..e5bd2c0031bf 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5383,6 +5383,16 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_gmem_create(kvm, &guest_memfd); break; } + case KVM_GUEST_MEMFD_POPULATE: { + struct kvm_guest_memfd_populate populate; + + r = -EFAULT; + if (copy_from_user(&populate, argp, sizeof(populate))) + goto out; + + r = kvm_gmem_guest_memfd_populate(kvm, &populate); + break; + } #endif default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); From patchwork Thu Oct 24 09:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13848668 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) (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 41F9C1B6D1A; Thu, 24 Oct 2024 09:55:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763718; cv=none; b=hJI6eo5bg+M3v/fMLwQiBDmYnYL/QlIXoAe4gWYbqBK47Bh6tuK4gZTAbVog4gcGjGTUehjLEXj5yxVaxVftXFuBpc/x2endWCsrdL5rmME1TLLq5PSj6ZjCwgoEQgearhuuAscJAif0aSxzSbMgAExa3/F1qKKOdSJPCkX4hJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763718; c=relaxed/simple; bh=O6drzlv3N80zmY7Pe/nx5UWGSkU3ccJmkWXqNPnK4jY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WslQQa28ECeBlZWKeJQJSFUghfld8CnnN8m3xQdkYBYwkkw/iQ/777BpViUtjWoF2z4G1guEBywDNeF4nLsZDH6uw08/wIkUfFzcm75DyBltltBrJj+jQyP1zRlxuU3DzSm+lyz7sh3MUUthE/aOYQvyrO3t4CdCqQbnJviqX/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=CmIIy1Ft; arc=none smtp.client-ip=52.119.213.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="CmIIy1Ft" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1729763717; x=1761299717; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MmRiiZPN8/PxouBRWWyXIjMBR7ptHi0tvj2mQku4mOY=; b=CmIIy1FtgHOYnMZRSdFmk8BPmESvLk6tEd1/m08xTsSeCcErIPcuwrx/ Vvj7tUJmYOzY+qmASgJmikCO70uLHi1/w2g0T0loBfomlG5Ekq6jjbiy8 +mlmc/FIla0j3qb5TcfrAE9kz7w+cvDu21VoeaJJBv/pk69tAugGwlwHJ Y=; X-IronPort-AV: E=Sophos;i="6.11,228,1725321600"; d="scan'208";a="690218938" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2024 09:55:12 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:45954] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.24.247:2525] with esmtp (Farcaster) id 9c21d62f-0fbe-4351-baa5-5eb88fe8a31f; Thu, 24 Oct 2024 09:55:11 +0000 (UTC) X-Farcaster-Flow-ID: 9c21d62f-0fbe-4351-baa5-5eb88fe8a31f Received: from EX19D020UWA002.ant.amazon.com (10.13.138.222) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:55:10 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D020UWA002.ant.amazon.com (10.13.138.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:55:10 +0000 Received: from email-imr-corp-prod-pdx-all-2c-619df93b.us-west-2.amazon.com (10.25.36.214) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Thu, 24 Oct 2024 09:55:10 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-all-2c-619df93b.us-west-2.amazon.com (Postfix) with ESMTPS id 8D8D540397; Thu, 24 Oct 2024 09:55:08 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , Subject: [PATCH 3/4] KVM: allow KVM_GUEST_MEMFD_POPULATE in another mm Date: Thu, 24 Oct 2024 09:54:28 +0000 Message-ID: <20241024095429.54052-4-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241024095429.54052-1-kalyazin@amazon.com> References: <20241024095429.54052-1-kalyazin@amazon.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Allow calling KVM_GUEST_MEMFD_POPULATE ioctl by the process that does not own the KVM context. This is to enable guest_memfd population by a non-VMM process that is useful for isolation of the memory management logic from the VMM for security and performance reasons. Signed-off-by: Nikita Kalyazin --- virt/kvm/kvm_main.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e5bd2c0031bf..eb626c4bf4d7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5159,8 +5159,25 @@ static long kvm_vm_ioctl(struct file *filp, void __user *argp = (void __user *)arg; int r; - if (kvm->mm != current->mm || kvm->vm_dead) + if (kvm->vm_dead) return -EIO; + +#ifdef CONFIG_KVM_PRIVATE_MEM + if (ioctl == KVM_GUEST_MEMFD_POPULATE) { + struct kvm_guest_memfd_populate populate; + + r = -EFAULT; + if (copy_from_user(&populate, argp, sizeof(populate))) + goto out; + + r = kvm_gmem_guest_memfd_populate(kvm, &populate); + goto out; + } +#endif + + if (kvm->mm != current->mm) + return -EIO; + switch (ioctl) { case KVM_CREATE_VCPU: r = kvm_vm_ioctl_create_vcpu(kvm, arg); @@ -5383,16 +5400,6 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_gmem_create(kvm, &guest_memfd); break; } - case KVM_GUEST_MEMFD_POPULATE: { - struct kvm_guest_memfd_populate populate; - - r = -EFAULT; - if (copy_from_user(&populate, argp, sizeof(populate))) - goto out; - - r = kvm_gmem_guest_memfd_populate(kvm, &populate); - break; - } #endif default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); From patchwork Thu Oct 24 09:54:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13848669 Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) (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 A8BCC1B3939; Thu, 24 Oct 2024 09:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763736; cv=none; b=SsCPnsDI01CpKO00vMKHFeT+uJr249TZK1Utq3q4uWkiXSEOQhI2BykeyjpgeQQCP4oiEAyy9DA5W8Zo8Ys0jCxJT+kC0pLvUUT/+WCUs+jdwQDhbz9BYGgjelapCWbX/gjsgxxZYMNiWJAgXYWjP0WKsIYv7EmPmK0TeXId7Yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729763736; c=relaxed/simple; bh=i5vK9aLmsT0GGDyg7WEDIE5qnmQKXG3tKt7ZzyssSAs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QXrf06shuDdecr6gwQAOwyeH06LIG8b7FSAWxo2MXLlwGSvUbFEsjgp223YgCXNxwVn0oDu2JR5GjaqoTpnHCE/vL54Jt/FnqaQqPg1wbgIVUgdLOAXywY93ay4dpKTkutfEI6pcSajHUi2r/MS+Dh21s7FsBuE2WNmpEsLSr8U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=Uu5C/MiG; arc=none smtp.client-ip=99.78.197.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="Uu5C/MiG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1729763730; x=1761299730; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HXXzFn3jpMW6RTAoikwJEYClFQzi+J2nHDJ1RTMwPhs=; b=Uu5C/MiG7gAh/Hd3Jf9tmHGovTdeqSgFAbXWn3jx2xt4k593ZpAaBuHK V9mqvZgBsVI+OoqAbxS7nC47+X9OeBmJtCi8IacT5Qn+TYYDKSDSAgRNX jhTZqp6yCaS9KF2+4+DALSeUhSxYgL6szAEqRrbfDqzZm00dJ/Ixhe4PQ Y=; X-IronPort-AV: E=Sophos;i="6.11,228,1725321600"; d="scan'208";a="140248274" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2024 09:55:28 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:19110] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.32.104:2525] with esmtp (Farcaster) id 79e479ff-e6f5-4772-9796-14113ad03430; Thu, 24 Oct 2024 09:55:28 +0000 (UTC) X-Farcaster-Flow-ID: 79e479ff-e6f5-4772-9796-14113ad03430 Received: from EX19D020UWA002.ant.amazon.com (10.13.138.222) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:55:24 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D020UWA002.ant.amazon.com (10.13.138.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Thu, 24 Oct 2024 09:55:23 +0000 Received: from email-imr-corp-prod-pdx-all-2c-d1311ce8.us-west-2.amazon.com (10.25.36.214) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Thu, 24 Oct 2024 09:55:23 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-all-2c-d1311ce8.us-west-2.amazon.com (Postfix) with ESMTPS id BDF2140617; Thu, 24 Oct 2024 09:55:21 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , Subject: [PATCH 4/4] KVM: document KVM_GUEST_MEMFD_POPULATE ioctl Date: Thu, 24 Oct 2024 09:54:29 +0000 Message-ID: <20241024095429.54052-5-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241024095429.54052-1-kalyazin@amazon.com> References: <20241024095429.54052-1-kalyazin@amazon.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Nikita Kalyazin --- Documentation/virt/kvm/api.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e32471977d0a..f192dab41bad 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6442,6 +6442,30 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_GUEST_MEMFD_POPULATE +---------------------------- + +:Capability: KVM_CAP_GUEST_MEMFD +:Architectures: none +:Type: vm ioctl +:Parameters: struct kvm_guest_memfd_populate(in) +:Returns: 0 if all requested pages populated, < 0 on error + +KVM_GUEST_MEMFD_POPULATE populates guest_memfd with data provided by userspace. + +:: + + struct kvm_guest_memfd_populate { + __u64 gpa; + __u64 size; + void __user *from; + __u64 flags; + }; + +A gfn can only be populated once. If a gfn is attempted to get populated +multiple times without prior calls to fallocate(PUNCH_HOLE), subsequent calls +will return EEXIST. +If the `from` pointer is NULL, the pages are cleared. 5. The kvm_run structure ========================