mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory
authorMichael Roth <michael.roth@amd.com>
Fri, 29 Mar 2024 21:24:42 +0000 (16:24 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 10 May 2024 17:11:45 +0000 (13:11 -0400)
filemap users like guest_memfd may use page cache pages to
allocate/manage memory that is only intended to be accessed by guests
via hardware protections like encryption. Writes to memory of this sort
in common paths like truncation may cause unexpected behavior such as
writing garbage instead of zeros when attempting to zero pages, or
worse, triggering hardware protections that are considered fatal as far
as the kernel is concerned.

Introduce a new address_space flag, AS_INACCESSIBLE, and use this
initially to prevent zero'ing of pages during truncation, with the
understanding that it is up to the owner of the mapping to handle this
specially if needed.

This is admittedly a rather blunt solution, but it seems like
there are no other places that should take into account the
flag to keep its promise.

Link: https://lore.kernel.org/lkml/ZR9LYhpxTaTk6PJX@google.com/
Cc: Matthew Wilcox <willy@infradead.org>
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Message-ID: <20240329212444.395559-5-michael.roth@amd.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/linux/pagemap.h
mm/truncate.c

index 2df35e65557d27c19fc3d87e865bc869b87bd18c..f879c1d54da7a00199e430ca1f692a4442f2e7e5 100644 (file)
@@ -207,6 +207,7 @@ enum mapping_flags {
        AS_STABLE_WRITES,       /* must wait for writeback before modifying
                                   folio contents */
        AS_UNMOVABLE,           /* The mapping cannot be moved, ever */
+       AS_INACCESSIBLE,        /* Do not attempt direct R/W access to the mapping */
 };
 
 /**
index 725b150e47ac4cc72f3d8238b46c20bf43f1c061..c501338c7ebddc4ac02c80e9592c5742e427cbc0 100644 (file)
@@ -233,7 +233,8 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
         * doing a complex calculation here, and then doing the zeroing
         * anyway if the page split fails.
         */
-       folio_zero_range(folio, offset, length);
+       if (!(folio->mapping->flags & AS_INACCESSIBLE))
+               folio_zero_range(folio, offset, length);
 
        if (folio_has_private(folio))
                folio_invalidate(folio, offset, length);