Skip to contentSkip to conversation

mm, compaction: make testing mapping_unmovable() safe

PATCH Applied Vlastimil Babka 2 participants 2 messages Sep 8, 2023 – Sep 9, 2023

Subsystems (10)
Memory management VFS KVM MIPS +6 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm, compaction: make testing mapping_unmovable() safe

As Kirill pointed out, mapping can be removed under us due to truncation. Test it under folio lock as already done for the async compaction / dirty folio case. To prevent locking every folio with mapping to do the test, do it only for unevictable folios, as we can expect the unmovable mapping folios are also unevictable. To enforce that expecation, make mapping_set_unmovable() also set AS_UNEVICTABLE.

Also incorporate comment update suggested by Matthew.

Fixes 3424873596ce ("mm: Add AS_UNMOVABLE to mark mapping as completely unmovable")

---
v2: mapping_set_unmovable() sets also AS_UNEVICTABLE, as Sean suggested.

 include/linux/pagemap.h |  6 +++++
 mm/compaction.c         | 49 +++++++++++++++++++++++++++--------------
 virt/kvm/guest_mem.c    |  2 +-
 3 files changed, 39 insertions(+), 18 deletions(-)
include/linux/pagemap.h +6 -0
index 931d2f1da7d5..4070c59e6f25 100644
@@ -276,6 +276,12 @@ static inline int mapping_use_writeback_tags(struct address_space *mapping)
 
 static inline void mapping_set_unmovable(struct address_space *mapping)
 {
+	/*
+	 * It's expected unmovable 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);
 }
 
mm/compaction.c +32 -17

82 lines not on this page · raw message

virt/kvm/guest_mem.c +1 -1
index c81d2bb9ae93..85903c32163f 100644
@@ -390,7 +390,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags,
 	inode->i_size = size;
 	mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
 	mapping_set_large_folios(inode->i_mapping);
-	mapping_set_unevictable(inode->i_mapping);
+	/* this also sets the mapping as unevictable */
 	mapping_set_unmovable(inode->i_mapping);
 
 	fd = get_unused_fd_flags(0);
Signature · 1 line
2.42.0
reviewer Applied
#1 lore raw
Quoted 8 lines · Vlastimil Babka

As Kirill pointed out, mapping can be removed under us due to truncation. Test it under folio lock as already done for the async compaction / dirty folio case. To prevent locking every folio with mapping to do the test, do it only for unevictable folios, as we can expect the unmovable mapping folios are also unevictable. To enforce that expecation, make mapping_set_unmovable() also set AS_UNEVICTABLE.

[...]

Applied to kvm-x86 guest_memfd, thanks!

[1/1] mm, compaction: make testing mapping_unmovable() safe

      https://github.com/kvm-x86/linux/commit/4876a35647b9

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes