]> www.infradead.org Git - nvme.git/commitdiff
mm, virt: merge AS_UNMOVABLE and AS_INACCESSIBLE
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 11 Jul 2024 17:56:54 +0000 (13:56 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 12 Jul 2024 15:13:13 +0000 (11:13 -0400)
The flags AS_UNMOVABLE and AS_INACCESSIBLE were both added just for guest_memfd;
AS_UNMOVABLE is already in existing versions of Linux, while AS_INACCESSIBLE was
acked for inclusion in 6.11.

But really, they are the same thing: only guest_memfd uses them, at least for
now, and guest_memfd pages are unmovable because they should not be
accessed by the CPU.

So merge them into one; use the AS_INACCESSIBLE name which is more comprehensive.
At the same time, this fixes an embarrassing bug where AS_INACCESSIBLE was used
as a bit mask, despite it being just a bit index.

The bug was mostly benign, because AS_INACCESSIBLE's bit representation (1010)
corresponded to setting AS_UNEVICTABLE (which is already set) and AS_ENOSPC
(except no async writes can happen on the guest_memfd).  So the AS_INACCESSIBLE
flag simply had no effect.

Fixes: 1d23040caa8b ("KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode")
Fixes: c72ceafbd12c ("mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory")
Cc: linux-mm@kvack.org
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: David Hildenbrand <david@redhat.com>
Tested-by: Michael Roth <michael.roth@amd.com>
Reviewed-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/linux/pagemap.h
mm/compaction.c
mm/migrate.c
mm/truncate.c
virt/kvm/guest_memfd.c

index ce7bac8f81da5451554cc4137e22655a626cc7d0..e05585eda7710d5c008621a196fc5d24a71262a2 100644 (file)
@@ -208,8 +208,8 @@ enum mapping_flags {
        AS_RELEASE_ALWAYS,      /* Call ->release_folio(), even if no private data */
        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 */
+       AS_INACCESSIBLE,        /* Do not attempt direct R/W access to the mapping,
+                                  including to move the mapping */
 };
 
 /**
@@ -310,20 +310,20 @@ static inline void mapping_clear_stable_writes(struct address_space *mapping)
        clear_bit(AS_STABLE_WRITES, &mapping->flags);
 }
 
-static inline void mapping_set_unmovable(struct address_space *mapping)
+static inline void mapping_set_inaccessible(struct address_space *mapping)
 {
        /*
-        * It's expected unmovable mappings are also unevictable. Compaction
+        * It's expected inaccessible mappings are also unevictable. Compaction
         * migrate scanner (isolate_migratepages_block()) relies on this to
         * reduce page locking.
         */
        set_bit(AS_UNEVICTABLE, &mapping->flags);
-       set_bit(AS_UNMOVABLE, &mapping->flags);
+       set_bit(AS_INACCESSIBLE, &mapping->flags);
 }
 
-static inline bool mapping_unmovable(struct address_space *mapping)
+static inline bool mapping_inaccessible(struct address_space *mapping)
 {
-       return test_bit(AS_UNMOVABLE, &mapping->flags);
+       return test_bit(AS_INACCESSIBLE, &mapping->flags);
 }
 
 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
index e731d45befc780865b5ecb6681fe00de31099e7a..714afd9c6df6246e2a5a3092ee2ae0c413ef7a21 100644 (file)
@@ -1172,22 +1172,22 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
                if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) ||
                    (mapping && is_unevictable)) {
                        bool migrate_dirty = true;
-                       bool is_unmovable;
+                       bool is_inaccessible;
 
                        /*
                         * Only folios without mappings or that have
                         * a ->migrate_folio callback are possible to migrate
                         * without blocking.
                         *
-                        * Folios from unmovable mappings are not migratable.
+                        * Folios from inaccessible mappings are not migratable.
                         *
                         * However, we can be racing with truncation, which can
                         * free the mapping that we need to check. Truncation
                         * holds the folio lock until after the folio is removed
                         * from the page so holding it ourselves is sufficient.
                         *
-                        * To avoid locking the folio just to check unmovable,
-                        * assume every unmovable folio is also unevictable,
+                        * To avoid locking the folio just to check inaccessible,
+                        * assume every inaccessible folio is also unevictable,
                         * which is a cheaper test.  If our assumption goes
                         * wrong, it's not a correctness bug, just potentially
                         * wasted cycles.
@@ -1200,9 +1200,9 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
                                migrate_dirty = !mapping ||
                                                mapping->a_ops->migrate_folio;
                        }
-                       is_unmovable = mapping && mapping_unmovable(mapping);
+                       is_inaccessible = mapping && mapping_inaccessible(mapping);
                        folio_unlock(folio);
-                       if (!migrate_dirty || is_unmovable)
+                       if (!migrate_dirty || is_inaccessible)
                                goto isolate_fail_put;
                }
 
index dd04f578c19c3e26cc4c299e340db7a897cc3bfe..50b60fb414e93f1756dc319728bcb7417ef61e89 100644 (file)
@@ -965,7 +965,7 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,
 
                if (!mapping)
                        rc = migrate_folio(mapping, dst, src, mode);
-               else if (mapping_unmovable(mapping))
+               else if (mapping_inaccessible(mapping))
                        rc = -EOPNOTSUPP;
                else if (mapping->a_ops->migrate_folio)
                        /*
index 60388935086d799b1951a0b71c1b142713f2c2d7..581977d2356fb2d51fcca3aa9385418ce839f3d2 100644 (file)
@@ -233,7 +233,7 @@ 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.
         */
-       if (!(folio->mapping->flags & AS_INACCESSIBLE))
+       if (!mapping_inaccessible(folio->mapping))
                folio_zero_range(folio, offset, length);
 
        if (folio_has_private(folio))
index 9148b9679bb12d648207eb22c3208c83f8e935d7..1c509c3512614666f6cf592766ca4e5c3a8b9266 100644 (file)
@@ -416,11 +416,10 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
        inode->i_private = (void *)(unsigned long)flags;
        inode->i_op = &kvm_gmem_iops;
        inode->i_mapping->a_ops = &kvm_gmem_aops;
-       inode->i_mapping->flags |= AS_INACCESSIBLE;
        inode->i_mode |= S_IFREG;
        inode->i_size = size;
        mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
-       mapping_set_unmovable(inode->i_mapping);
+       mapping_set_inaccessible(inode->i_mapping);
        /* Unmovable mappings are supposed to be marked unevictable as well. */
        WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping));