From patchwork Tue Jul 9 13:20:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727955 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83F5EC2BD09 for ; Tue, 9 Jul 2024 13:21:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D06F56B00B4; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C89676B00B5; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8D566B00B6; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 80C706B00B4 for ; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 46E061218D1 for ; Tue, 9 Jul 2024 13:21:27 +0000 (UTC) X-FDA: 82320275814.13.B7A413B Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf03.hostedemail.com (Postfix) with ESMTP id 39ADA20025 for ; Tue, 9 Jul 2024 13:21:25 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=dXktH00b; spf=pass (imf03.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531269; a=rsa-sha256; cv=none; b=GGXSzyrU/3HwHoxE2xAT2J3vVjBsr5YPKyMqf1b2hWp4F6Es/1DreMF7mq6azhYen7Tn7r r3eN6FEw8pKqDM0QdNw7x6ra4zQXBrMRjmoiQxZGafVhQczKAgx2Mw3txo72d78KykXf/s 4EDPJ/ACFhX9km7dpnINKg4BfXKctpM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=dXktH00b; spf=pass (imf03.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720531269; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OYFwwPMzkwoeUqGjPsHTCVZ/9U32HO+9FB3G59spnNM=; b=sP1yDqfFKJuzzw3coozgZcxtQ3K8NMc0VQkxsXRX7xRuAcWsCfkfRlSVoLgPvj6hcuQ85o 7z5BjOMCVoBq2m1YvSpj3OhmKijpnEKOveBTZvUvFN6ZkkHuN4Y2XoYiOxvMPU64L4o5Od en+S6L5exkS3TGF83nbOsIldD7P6xSk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531286; x=1752067286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OYFwwPMzkwoeUqGjPsHTCVZ/9U32HO+9FB3G59spnNM=; b=dXktH00bgudEtkH8gqyQWtLeEwchY/Rsn+qT4/HUHkrq3l33oKGZZQv4 r75OAD8uJZVtyBzyHiIN7Zx8fJHXgYf2sRgWTIUKF5TguvJQIMMu6d/qm l+QB6cPwtgHWFY+SyPxE3xt2lyDfFZl36P+15mPQ4pJ7AnSb0YfBC+q2x 0=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="432897857" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:23 +0000 Received: from EX19MTAUEB001.ant.amazon.com [10.0.0.204:11016] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.59.218:2525] with esmtp (Farcaster) id b525f254-64fd-4b54-ab1d-bef2d53e8187; Tue, 9 Jul 2024 13:21:21 +0000 (UTC) X-Farcaster-Flow-ID: b525f254-64fd-4b54-ab1d-bef2d53e8187 Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) by EX19MTAUEB001.ant.amazon.com (10.252.135.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:20 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:20 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Tue, 9 Jul 2024 13:21:17 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings Date: Tue, 9 Jul 2024 14:20:36 +0100 Message-ID: <20240709132041.3625501-9-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Stat-Signature: zh71dtypiaynsfbfhf8jdcas6p6hxjiz X-Rspamd-Queue-Id: 39ADA20025 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1720531285-363323 X-HE-Meta: U2FsdGVkX1/lJFW/Z14h5RUd4aNSQGYq3IvsvODCFjhWcy1V3RPElmaoWuljHBBNMpd+zEM5KO/6U5zAsIyqhOG/I2/IW3mHR+CCmu1v1G9JZXEqa6YKyPcdb9AbQ0uMDXGuvhP/QksiZqQaBlEWzu0raLjP1jC+PaYMc3hQEo4Zm8Zs3vI+2d/9Trnmf8eYThaG+UAZxOx15srIi+tDLZIBPGER/IXFBx5iNVIIjBcYEavHBwd/tjRVhgw7fQq4mlBDjjxK3gdZssX5p2OGcYYkrLonC+PRjb627R7SP7/VPltyDKxMJ3f3PTZNftE9c0+wcL/BqZx2awluXcAF9X8vq5ba6bI2sMFgx/e5kBWf4Oinznb010GSRk2M4u2gsUTR0Ldk2aRe0sqNz6gEVN5TKVhpBiuXgIln5ad/nySBg3DSWQUK1Ng1SoauTq0LxKES4N9adoKhv2ZRFlRzxuarV9Duw1IF502ZaxjSWIvsW5cP2TP9e0QCL8/iQlGhWMPOQw104PVYTL/yRuiC5uzJfvMcfVt5dMN+FOVVDAgduqNJDppbOa5bxof1p9eOJ3b6IzmCSRf6zZBOEgVUuzJd0d0ImX8V29Rd7QS6dBiWl3Je8/flImQGDv5qd+AOIapzFcUsRiHl8dRtAMsO6Fkij+CLQGH6uwi6CE5WTZsCaR5yEpFU4ht2FWQy325rhZ21R6meOwEKmUgfF6p/gl5xJarQfZin7KRmSyNNIyGFcSZHtFa4J/7lpOpQxUbJFh0ZcCorNtTyYDJI/6y4NGJ8KF2Z7AwX5P/Gwbvm2ioP5/z51sRJ+iBI0dLmjifGQnWfTplOmfooCc0O0BDfllweupZc12B9w4njqzgjlPabPRnF6LsTGfbQuv1lKBqlzUCEcWZ48BlkWL/ISFV5ZKcU/CznTw4OwzJTzW8Kmzt/mrEg/KURJNwfXMd6XqGVuW10k6mEd7ejTHUCVhw 9S66xmvy 9qh/6/d3QIWL8SNrdWIzHsQ22RKsFnM/HgG59/RZTfiwXNILyUnh8eQ72CKF3yGdVt8r3J6BYofABuRkMGii42WFTEpUMFfRRDAHS1QqhXgkjkCLcOYWkPqfc8OCD4BNaeYjWmxtPq4ofJN8MsC/DlkSFUBCfnBYghik7my4LClXF1AoTfshqILudZ6e8PQguQCKA/0FOX98Tj0WVFz80tcBjIjp+mMt22z7eSG/UVAzAgAwNzVFiHGtCJq7Kr7YpvImFrDAprPWSuynainH2sI9Es3LB9I68+m2s24t2jX64R0lKm9oug8zf5vGEovlwXrs1TcOkerI4xlc0X/EcZD13CujZLPV+ro0a X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Allow mapping guest_memfd into userspace. Since AS_INACCESSIBLE is set on the underlying address_space struct, no GUP of guest_memfd will be possible. Signed-off-by: Patrick Roy --- virt/kvm/guest_memfd.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index dc9b0c2d0b0e..101ec2b248bf 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -319,7 +319,37 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) return get_file_active(&slot->gmem.file); } +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct folio *folio; + + folio = kvm_gmem_get_folio(file_inode(vmf->vma->vm_file), vmf->pgoff, true); + + if (!folio) + return VM_FAULT_SIGBUS; + + vmf->page = folio_file_page(folio, vmf->pgoff); + + return VM_FAULT_LOCKED; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault +}; + +static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) + return -EINVAL; + + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, @@ -594,7 +624,6 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return -EFAULT; } - gmem = file->private_data; if (xa_load(&gmem->bindings, index) != slot) { WARN_ON_ONCE(xa_load(&gmem->bindings, index)); return -EIO;