From patchwork Tue Sep 10 23:43:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799500 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 92EC9EE01F1 for ; Tue, 10 Sep 2024 23:45:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9BB68D00E6; Tue, 10 Sep 2024 19:45:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFB458D00E2; Tue, 10 Sep 2024 19:45:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92A218D00E6; Tue, 10 Sep 2024 19:45:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6FEF68D00E2 for ; Tue, 10 Sep 2024 19:45:23 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1EADA160C63 for ; Tue, 10 Sep 2024 23:45:23 +0000 (UTC) X-FDA: 82550462526.14.43076D7 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf21.hostedemail.com (Postfix) with ESMTP id 5CB511C0011 for ; Tue, 10 Sep 2024 23:45:21 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KVMrRjcC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3D9rgZgsKCIQiksmztm61voowwotm.kwutqv25-uus3iks.wzo@flex--ackerleytng.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3D9rgZgsKCIQiksmztm61voowwotm.kwutqv25-uus3iks.wzo@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011893; a=rsa-sha256; cv=none; b=orO/P2TU4ITuhNAiGQ/BBPdxrEbPlXSl+yt7L2oD+LZZh/HKdCtGemHa5Beo0fLOLRk0a5 7qmUZSL1Xwhv15bqEBVOOqtyPz+AZB0XKAFJ3dXLbA2Ruo42jMArobw/fMiRQa1j/ivM9Q SmD32A96IZK8Q0E5yyrUanZgluQCjig= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KVMrRjcC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3D9rgZgsKCIQiksmztm61voowwotm.kwutqv25-uus3iks.wzo@flex--ackerleytng.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3D9rgZgsKCIQiksmztm61voowwotm.kwutqv25-uus3iks.wzo@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011893; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=34wlcJFkfGNQzzb1hZdL7ujzeulibiS7N8dJMuUgV78=; b=Q/J2ba4qDS6ZHaqnt2p+1B1zY68w9xCKeBh+skedc3TobexcQD3tAL6CQMvapXtR/3hixc FvnlfaMOIzZ80ctmrrC0mcMAxmUT+2RAJq2OeI3f/O/Fc8JVfAludio7A9ntw8Qy1LsivP itvt5redeB2AEoSv72sVk/Lc/a+aelg= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7d511de6348so2576190a12.0 for ; Tue, 10 Sep 2024 16:45:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011920; x=1726616720; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=34wlcJFkfGNQzzb1hZdL7ujzeulibiS7N8dJMuUgV78=; b=KVMrRjcCgdYY/ILCnWda/rSMyvhrCwpCbHgeVtBygfEDeU2A/Gbib5enwONFG0pIpZ VLgvkmBUS/t9EvMid+IY96GEiBq50b/deRUSrEsmiw4mGmKMySfqqdI+636jCCY3bRIw O28Mk4chUOUop2uHXobrLF84fkWOfz2ZZ8BAqUNUFJQJ1HQRllJzS+mnol/KUHPryVcv 2SNEiNQlsLKrf6+LfaL8hc894fFIg7itmF/AuFE1Hvihk3cjn0BcW3AbZoJbByDdptmV NjKTrf6Zlzhs1iRuy4g8IWr7SOWIuvygClkrOJGqskv2mPwgQeu9O7vd7PQND0veaqjw 2viA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011920; x=1726616720; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=34wlcJFkfGNQzzb1hZdL7ujzeulibiS7N8dJMuUgV78=; b=PIUeNkZ5wlAsVisvumSHtR+9Jh6Fovg6cbHMMBk7WYFfJpnQnOX4LyIPwBD1X1IA3Z HpJRJPh9BlTZTedQntaIHZAClC0Rhw4Im1PTDSmNHgroqVt7cbPX2/z19qX1atwEk3z7 FmexiUE/Z5tXUZNd4tjGUHlOr93AHvOrMW6FjJLRVxAvJsOlydts2s2pCAfc04a2fzzs wBEhlFqdCg+P/YknfEPfNcxCKgzBkb0A2flo3cXEKigRS3ZmWRd1SBVVwpKX5CG2OpKA MrsZW+FJbpYSOmU8aR6SaJuRA2AvxB3IabKzp9XO/xGZNJ0+NYOn86bP8uhDMvVJFHoG EMWw== X-Forwarded-Encrypted: i=1; AJvYcCXadLN+bvc1TRK7LrgiexejGAmEfLGgm+DBGO2y+jLMMcuhiNEljs40yxa9V2SHaAO/LE/wsw00qQ==@kvack.org X-Gm-Message-State: AOJu0YwtENFUTO228YTZFf8S+GXE6X0W6umMNfA1Lc7xNRe08XBygndZ /UMynuLRq+KtoTkAXVaBkin2Hp21hCu6yMznKeGC6hEt9pJoL1XZPIBNP90/C8eaMF1Ek7xx6gR JUOnQfFngj8eoN8wJfdBF/g== X-Google-Smtp-Source: AGHT+IHhJHaKR4KPnPlGSVYGkBp4x4+Xb5fuhdBUkXuBYQ8usu5fhaV07d9H987RUToHw3cMQAZNLNO7RWO0YnhXdQ== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a65:41c6:0:b0:785:e3e:38db with SMTP id 41be03b00d2f7-7db08543c58mr8464a12.8.1726011919943; Tue, 10 Sep 2024 16:45:19 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:58 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: <5a05eb947cf7aa21f00b94171ca818cc3d5bdfee.1726009989.git.ackerleytng@google.com> Subject: [RFC PATCH 27/39] KVM: guest_memfd: Allow mmapping guest_memfd files From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspam-User: X-Rspamd-Queue-Id: 5CB511C0011 X-Rspamd-Server: rspam01 X-Stat-Signature: 58r4mq779wqrgeobodc4et11n5okcnn8 X-HE-Tag: 1726011921-128130 X-HE-Meta: U2FsdGVkX18RKej2bZT4toy4zOziuhQ+jc+1dVKGpFHssWknUwVpSxkhonvV/ShnRMhl2fG+ZWNFGPPukrNUY2n5p8e4bgAqANaNcZ+LoDQuPJEy6pZwt4r/3Jf1e+iX456XR9yyIUZSBzwbcdO4z121u20WTXdj5xClTzMmkV0S2X7wcxD3krw39ijIopOYssNZWkA0nS6eI2ps4Sc2+aaH6gRP5qrundNKBuTvF+c04kntl3bbeSHq6W9S6KRFTpBlvkxow05c10QkR3F38symMIsqOBwNJ+JPFt3lhv//0SUmEERtyjtwUYXMd+6GeB9NApocgeNXrzo1PhBNelbKQp/8on1H8ZJzoIIXPZPg1GhtWJooqaakGEHAbiVA3760nwktZaw7BTvIplLrk51oppAv79KTK63Jwz+HzT5vC51z5Ga81cHvn5E1cWi6nUBnQtn3DZPRNpSGVQEHuql6Pp97Mt2z2ZrIBIT+r/j/kw+HaFA29Pzd4OT5c++fIpP2i8026bIVPc3o4yET2/mOQBF9SgnqBV2PlI087mGsL01Jemhae+qii1lUQ6vSoc+LpE20pgxfgO2xMxlyTHsxAGdaBViS1QuGSdvstXG7UiTYCpVa5LrPjSwDO1wn6tzZ4iR5rCoJ9rDJmDoVTw/h4+RU/0eMSzSNM8mjMSSbDgQeSeQzbNlydb/GNHuVce55sEB9NvmbPyiUpL8erx/f1p8VS1LMmGH/AwArd3ktv2ukatMFaQ9qtT9yw/bOyZRzxPId6nzLz6AFsoIIqQar+jIuU3hieVN1peB1TwRgtw81WUE9k5Yl/r+Zs8nMe43wu74rb5IdHAIHbpfXxoAuS3H3Nr6OFomFXuoGfkYQFQesfh6H1NoyS4QzAL1LbyXSJqVAyydz4ChgFLmHlQ0kk/LlFNh+pvj2w7wwIlgHv7Sq6l7jcn0ML+P7M6O1HyKbcxgtDAHAA+1GyC0 K1sDNBZr ad+2o9sx5t29wcLGc5qGkcZ0EfyiI2yXKCU/Ejia55KIeiDehUMT9nOMQPZyYJFkcoAsWFnjmuDotd2ZovVIigrM9tVZfuZGMuO5r3HlAbOrYPlKH/H/s5AXQnFQ24KZKtvhqSgqur+UNBsc/pJK7+PsS94VRZ1SdT7iYWW8jg+5fXWfLi6g8cUXGsIffrAhy3+hh48Nk1H6AZi0MuBnMH14cTXcAoFn5PZKMjKrvXu2x0HIhjdpn3NudjhcJS+JKA5bSxy7SMMQU7ulKbeLm/5pMNeSWIFtuBaNt1kDay+ku6CgFjPOLaXO6QF+D3U0lW9KvLInzqlqgm/FLGBLVqQifjLqMnKp88wB584JU5DsrWwwGvw9BTTpmaNUHsWGFgIQjJtZb6X7C1zlpDSpkh4V+3IOUe+x7GdwXCiDrs81GNK4oJvRIwy1szc0rzYrs9BfcPr+Nb2NEyASWYfKb0Pjmu8KgbnDLgD4np8XIwGsP2b7YV1ZpYOjR6CJ6dBLcHJoygjAohCcwIC+l0kAjYEZvgR0yrSyG4G8M0QVGYccRz3gzQ/kSXbNQ10zJbM5rrN/x2s/dSpFJVamnbzdUJ7QxZK14IHUakjnAZYgSAch3e6aKYR3IhQvEV0q+l6QzhP7Bb8AQBoqDe4Gv6UFPLInSRj21rxsrVL7hWn2DoOMPwukSvvLU874gLD9NITEMzklv3FvQluxLGjo= 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: guest_memfd files can always be mmap()ed to userspace, but faultability is controlled by an attribute on the inode. Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng --- virt/kvm/guest_memfd.c | 46 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index b603518f7b62..fc2483e35876 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -781,7 +781,8 @@ static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len) { struct list_head *gmem_list = &inode->i_mapping->i_private_list; pgoff_t start = offset >> PAGE_SHIFT; - pgoff_t end = (offset + len) >> PAGE_SHIFT; + pgoff_t nr = len >> PAGE_SHIFT; + pgoff_t end = start + nr; struct kvm_gmem *gmem; /* @@ -790,6 +791,9 @@ static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len) */ filemap_invalidate_lock(inode->i_mapping); + /* TODO: Check if even_cows should be 0 or 1 */ + unmap_mapping_range(inode->i_mapping, start, len, 0); + list_for_each_entry(gmem, gmem_list, entry) kvm_gmem_invalidate_begin(gmem, start, end); @@ -946,6 +950,9 @@ static void kvm_gmem_hugetlb_teardown(struct inode *inode) { struct kvm_gmem_hugetlb *hgmem; + /* TODO: Check if even_cows should be 0 or 1 */ + unmap_mapping_range(inode->i_mapping, 0, LLONG_MAX, 0); + truncate_inode_pages_final_prepare(inode->i_mapping); kvm_gmem_hugetlb_truncate_folios_range(inode, 0, LLONG_MAX); @@ -1003,11 +1010,46 @@ static void kvm_gmem_init_mount(void) kvm_gmem_mnt = kern_mount(&kvm_gmem_fs); BUG_ON(IS_ERR(kvm_gmem_mnt)); - /* For giggles. Userspace can never map this anyways. */ kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; } +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct inode *inode; + struct folio *folio; + + inode = file_inode(vmf->vma->vm_file); + if (!kvm_gmem_is_faultable(inode, vmf->pgoff)) + return VM_FAULT_SIGBUS; + + folio = kvm_gmem_get_folio(inode, vmf->pgoff); + 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)) != + (VM_SHARED | VM_MAYSHARE)) { + return -EINVAL; + } + + file_accessed(file); + 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,