From: Patrick Roy <roypat@amazon.co.uk>
To: <rppt@kernel.org>, <david@redhat.com>, <seanjc@google.com>
Cc: Patrick Roy <roypat@amazon.co.uk>, <pbonzini@redhat.com>,
<corbet@lwn.net>, <willy@infradead.org>,
<akpm@linux-foundation.org>, <song@kernel.org>,
<jolsa@kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
<andrii@kernel.org>, <martin.lau@linux.dev>, <eddyz87@gmail.com>,
<yonghong.song@linux.dev>, <john.fastabend@gmail.com>,
<kpsingh@kernel.org>, <sdf@fomichev.me>, <haoluo@google.com>,
<Liam.Howlett@oracle.com>, <lorenzo.stoakes@oracle.com>,
<vbabka@suse.cz>, <jannh@google.com>, <shuah@kernel.org>,
<kvm@vger.kernel.org>, <linux-doc@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-fsdevel@vger.kernel.org>,
<linux-mm@kvack.org>, <bpf@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <tabba@google.com>,
<jgowans@amazon.com>, <graf@amazon.com>, <kalyazin@amazon.com>,
<xmarcalx@amazon.com>, <derekmn@amazon.com>,
<jthoughton@google.com>
Subject: [PATCH v4 01/12] mm: introduce AS_NO_DIRECT_MAP
Date: Fri, 21 Feb 2025 16:07:14 +0000 [thread overview]
Message-ID: <20250221160728.1584559-2-roypat@amazon.co.uk> (raw)
In-Reply-To: <20250221160728.1584559-1-roypat@amazon.co.uk>
Add AS_NO_DIRECT_MAP for mappings where direct map entries of folios are
set to not present . Currently, mappings that match this description are
secretmem mappings (memfd_secret()). Later, some guest_memfd
configurations will also fall into this category.
Reject this new type of mappings in all locations that currently reject
secretmem mappings, on the assumption that if secretmem mappings are
rejected somewhere, it is precisely because of an inability to deal with
folios without direct map entries.
Use a new flag instead of overloading AS_INACCESSIBLE (which is already
set by guest_memfd) because not all guest_memfd mappings will end up
being direct map removed (e.g. in pKVM setups, parts of guest_memfd that
can be mapped to userspace should also be GUP-able, and generally not
have restrictions on who can access it).
Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
---
include/linux/pagemap.h | 16 ++++++++++++++++
lib/buildid.c | 4 ++--
mm/gup.c | 6 +++++-
mm/mlock.c | 3 ++-
4 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 47bfc6b1b632..903b41e89cf8 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -210,6 +210,7 @@ enum mapping_flags {
AS_STABLE_WRITES = 7, /* must wait for writeback before modifying
folio contents */
AS_INACCESSIBLE = 8, /* Do not attempt direct R/W access to the mapping */
+ AS_NO_DIRECT_MAP = 9, /* Folios in the mapping are not in the direct map */
/* Bits 16-25 are used for FOLIO_ORDER */
AS_FOLIO_ORDER_BITS = 5,
AS_FOLIO_ORDER_MIN = 16,
@@ -335,6 +336,21 @@ static inline bool mapping_inaccessible(struct address_space *mapping)
return test_bit(AS_INACCESSIBLE, &mapping->flags);
}
+static inline void mapping_set_no_direct_map(struct address_space *mapping)
+{
+ set_bit(AS_NO_DIRECT_MAP, &mapping->flags);
+}
+
+static inline bool mapping_no_direct_map(struct address_space *mapping)
+{
+ return test_bit(AS_NO_DIRECT_MAP, &mapping->flags);
+}
+
+static inline bool vma_is_no_direct_map(const struct vm_area_struct *vma)
+{
+ return vma->vm_file && mapping_no_direct_map(vma->vm_file->f_mapping);
+}
+
static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
{
return mapping->gfp_mask;
diff --git a/lib/buildid.c b/lib/buildid.c
index c4b0f376fb34..80b5d805067f 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -65,8 +65,8 @@ static int freader_get_folio(struct freader *r, loff_t file_off)
freader_put_folio(r);
- /* reject secretmem folios created with memfd_secret() */
- if (secretmem_mapping(r->file->f_mapping))
+ /* reject secretmem folios created with memfd_secret() or guest_memfd() */
+ if (secretmem_mapping(r->file->f_mapping) || mapping_no_direct_map(r->file->f_mapping))
return -EFAULT;
r->folio = filemap_get_folio(r->file->f_mapping, file_off >> PAGE_SHIFT);
diff --git a/mm/gup.c b/mm/gup.c
index 3883b307780e..7ddaf93c5b6a 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1283,7 +1283,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
return -EOPNOTSUPP;
- if (vma_is_secretmem(vma))
+ if (vma_is_secretmem(vma) || vma_is_no_direct_map(vma))
return -EFAULT;
if (write) {
@@ -2849,6 +2849,10 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)
*/
if (check_secretmem && secretmem_mapping(mapping))
return false;
+
+ if (mapping_no_direct_map(mapping))
+ return false;
+
/* The only remaining allowed file system is shmem. */
return !reject_file_backed || shmem_mapping(mapping);
}
diff --git a/mm/mlock.c b/mm/mlock.c
index cde076fa7d5e..07a351491d9d 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -474,7 +474,8 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) ||
- vma_is_dax(vma) || vma_is_secretmem(vma) || (oldflags & VM_DROPPABLE))
+ vma_is_dax(vma) || vma_is_secretmem(vma) || vma_is_no_direct_map(vma) ||
+ (oldflags & VM_DROPPABLE))
/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
goto out;
base-commit: da40655874b54a2b563f8ceb3ed839c6cd38e0b4
--
2.48.1
next prev parent reply other threads:[~2025-02-21 16:09 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-21 16:07 [PATCH v4 00/12] Direct Map Removal for guest_memfd Patrick Roy
2025-02-21 16:07 ` Patrick Roy [this message]
2025-02-25 16:52 ` [PATCH v4 01/12] mm: introduce AS_NO_DIRECT_MAP David Hildenbrand
2025-03-03 9:29 ` Vlastimil Babka
2025-02-21 16:07 ` [PATCH v4 02/12] mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing Patrick Roy
2025-02-25 16:52 ` David Hildenbrand
2025-02-26 8:44 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map Patrick Roy
2025-02-25 16:54 ` David Hildenbrand
2025-02-26 8:48 ` Patrick Roy
2025-02-26 9:08 ` David Hildenbrand
2025-02-26 15:14 ` Patrick Roy
2025-02-26 15:30 ` David Hildenbrand
2025-03-19 7:53 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 04/12] KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support Patrick Roy
2025-02-25 16:55 ` David Hildenbrand
2025-02-26 8:37 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 05/12] KVM: Documentation: document KVM_GMEM_NO_DIRECT_MAP flag Patrick Roy
2025-02-21 16:07 ` [PATCH v4 06/12] KVM: selftests: load elf via bounce buffer Patrick Roy
2025-02-21 16:07 ` [PATCH v4 07/12] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Patrick Roy
2025-02-21 16:07 ` [PATCH v4 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Patrick Roy
2025-02-25 14:12 ` Patrick Roy
2025-02-21 16:07 ` [PATCH v4 09/12] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Patrick Roy
2025-02-21 16:07 ` [PATCH v4 10/12] KVM: selftests: adjust test_create_guest_memfd_invalid Patrick Roy
2025-02-21 16:07 ` [PATCH v4 11/12] KVM: selftests: set KVM_GMEM_NO_DIRECT_MAP in mem conversion tests Patrick Roy
2025-02-21 16:07 ` [PATCH v4 12/12] KVM: selftests: Test guest execution from direct map removed gmem Patrick Roy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250221160728.1584559-2-roypat@amazon.co.uk \
--to=roypat@amazon.co.uk \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=david@redhat.com \
--cc=derekmn@amazon.com \
--cc=eddyz87@gmail.com \
--cc=graf@amazon.com \
--cc=haoluo@google.com \
--cc=jannh@google.com \
--cc=jgowans@amazon.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=jthoughton@google.com \
--cc=kalyazin@amazon.com \
--cc=kpsingh@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=martin.lau@linux.dev \
--cc=pbonzini@redhat.com \
--cc=rppt@kernel.org \
--cc=sdf@fomichev.me \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tabba@google.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=xmarcalx@amazon.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox