Skip to contentSkip to conversation

mm: prevent droppable mappings from being locked

PATCH Superseded Anthony Yznaga 3 participants 9 messages Mar 6 – Mar 10

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: prevent droppable mappings from being locked

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 include/linux/mm.h | 3 +++
 mm/mlock.c         | 4 ++--
 mm/vma.c           | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +3 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
 /* This mask represents all the VMA flag bits used by mlock */
 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)
+
 /* These flags can be updated atomically via VMA/mmap read lock. */
 #define VM_ATOMIC_SET_ALLOWED VM_MAYBE_GUARD
 
mm/mlock.c +2 -2
index 2f699c3497a5..fd35c1e88c4c 100644
@@ -472,9 +472,9 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	int ret = 0;
 	vm_flags_t oldflags = vma->vm_flags;
 
-	if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
+	if (newflags == oldflags || (oldflags & VM_NO_MLOCK_MASK) ||
 	    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))
 		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
 		goto out;
 
mm/vma.c +1 -1
index be64f781a3aa..1334622e4a03 100644
@@ -2589,7 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
 	vm_stat_account(mm, vma->vm_flags, map->pglen);
 	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
+		if ((vm_flags & VM_NO_MLOCK_MASK) || vma_is_dax(vma) ||
 					is_vm_hugetlb_page(vma) ||
 					vma == get_gate_vma(mm))
 			vm_flags_clear(vma, VM_LOCKED_MASK);
Signature · 1 line
2.47.3
#1 lore raw
Quoted 17 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 include/linux/mm.h | 3 +++
 mm/mlock.c         | 4 ++--
 mm/vma.c           | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644

include/linux/mm.h enum { · line 578

Quoted 5 lines
include/linux/mm.h +2 -0
@@ -574,6 +574,9 @@ enum {
 /* This mask represents all the VMA flag bits used by mlock */
 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

The usage of "vma->vm_mm" must be double checked, and we'll have to take care of making the tools/testing/vma test happy.

Not even compile tested, so will require some more work.

include/linux/hugetlb_inline.h +1 -1
index 593f5d4e108b..755281fab23d 100644
@@ -30,7 +30,7 @@ static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
 
 #endif
 
-static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
+static inline bool is_vm_hugetlb_page(const struct vm_area_struct *vma)
 {
 	return is_vm_hugetlb_flags(vma->vm_flags);
 }
mm/internal.h +9 -0
index 6e1162e13289..b70ebbdafe00 100644
@@ -1242,6 +1242,15 @@ static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
 	}
 	return fpin;
 }
+
+static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
+{
+	if (vma->vm_flags & (VM_SPECIAL | VM_DROPPABLE))
+		return false;
+	if (vma_is_dax(vma) || is_vm_hugetlb_page(vma))
+		return false;
+	return vma != get_gate_vma(vma->vm_mm);
+}
 #else /* !CONFIG_MMU */
 static inline void unmap_mapping_folio(struct folio *folio) { }
 static inline void mlock_new_folio(struct folio *folio) { }
mm/mlock.c +1 -3
index 1a92d16f3684..e16b2ea234f7 100644
@@ -472,9 +472,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	int ret = 0;
 	vm_flags_t oldflags = vma->vm_flags;
 
-	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))
+	if (newflags == oldflags || !vma_supports_mlock(vma))
 		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
 		goto out;
 
mm/vma.c +1 -3
index e95fd5a5fe5c..b7055c264b5d 100644
@@ -2589,9 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
 	vm_stat_account(mm, vma->vm_flags, map->pglen);
 	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
-					is_vm_hugetlb_page(vma) ||
-					vma == get_gate_vma(mm))
+		if (!vma_supports_mlock(vma))
 			vm_flags_clear(vma, VM_LOCKED_MASK);
 		else
 			mm->locked_vm += map->pglen;
Signature · 6 lines
2.43.0

-- 
Cheers,

David
#2 lore raw
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 20 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 include/linux/mm.h | 3 +++
 mm/mlock.c         | 4 ++--
 mm/vma.c           | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
 /* This mask represents all the VMA flag bits used by mlock */
 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
Quoted 7 lines

include/linux/mm.h enum { · line 578

Quoted 2 lines
include/linux/mm.h +2 -0
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

The usage of "vma->vm_mm" must be double checked, and we'll have to take care of making the tools/testing/vma test happy.

Yeah Anthony - please do a simple:

$ cd tools/testing/vma
$ make && ./vma

To make sure that your changes don't introduce anything that breaks that.

If you need to add duplicate header defines put them in tools/testing/vma/include/dup.h, if you need to stub stuff out put in stubs.h and if you need to customise something for testing purposes, put it in custom.h.

Quoted 24 lines

Not even compile tested, so will require some more work.

include/linux/hugetlb_inline.h +1 -1
index 593f5d4e108b..755281fab23d 100644
@@ -30,7 +30,7 @@ static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
 
 #endif
 
-static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
+static inline bool is_vm_hugetlb_page(const struct vm_area_struct *vma)
 {
 	return is_vm_hugetlb_flags(vma->vm_flags);
 }
mm/internal.h +0 -0
index 6e1162e13289..b70ebbdafe00 100644
@@ -1242,6 +1242,15 @@ static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
 	}
 	return fpin;

mm/internal.h static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, · line 1253

Quoted 10 lines
mm/internal.h +9 -0
 }
+
+static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
+{
+	if (vma->vm_flags & (VM_SPECIAL | VM_DROPPABLE))
+		return false;
+	if (vma_is_dax(vma) || is_vm_hugetlb_page(vma))
+		return false;
+	return vma != get_gate_vma(vma->vm_mm);
+}

Yeah this is nice.

Quoted 23 lines
mm/internal.h +0 -0
 #else /* !CONFIG_MMU */
 static inline void unmap_mapping_folio(struct folio *folio) { }
 static inline void mlock_new_folio(struct folio *folio) { }
mm/mlock.c +1 -3
index 1a92d16f3684..e16b2ea234f7 100644
@@ -472,9 +472,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	int ret = 0;
 	vm_flags_t oldflags = vma->vm_flags;
 
-	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))
+	if (newflags == oldflags || !vma_supports_mlock(vma))
 		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
 		goto out;
 
mm/vma.c +0 -0
index e95fd5a5fe5c..b7055c264b5d 100644

mm/vma.c static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma) · line 2595

Quoted 10 lines
mm/vma.c +1 -3
@@ -2589,9 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
 	vm_stat_account(mm, vma->vm_flags, map->pglen);
 	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
-					is_vm_hugetlb_page(vma) ||
-					vma == get_gate_vma(mm))
+		if (!vma_supports_mlock(vma))
 			vm_flags_clear(vma, VM_LOCKED_MASK);
 		else
 			mm->locked_vm += map->pglen;

Very much preferable!

Quoted 7 lines

--
2.43.0

--
Cheers,

David

Cheers, Lorenzo

#3 lore raw
Quoted 30 lines · Lorenzo Stoakes (Oracle)
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 20 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
  include/linux/mm.h | 3 +++
  mm/mlock.c         | 4 ++--
  mm/vma.c           | 2 +-
  3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
  /* This mask represents all the VMA flag bits used by mlock */
  #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
Quoted 6 lines

include/linux/mm.h enum { · line 578

Quoted 2 lines
include/linux/mm.h +2 -0
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

The usage of "vma->vm_mm" must be double checked, and we'll have to take care of making the tools/testing/vma test happy.

Yeah Anthony - please do a simple:

Quoted 8 lines
$ cd tools/testing/vma
$ make && ./vma

To make sure that your changes don't introduce anything that breaks that.

If you need to add duplicate header defines put them in tools/testing/vma/include/dup.h, if you need to stub stuff out put in stubs.h and if you need to customise something for testing purposes, put it in custom.h.

Thanks, Lorenzo. Much appreciated!

Anthony

Quoted 79 lines
Quoted 24 lines

Not even compile tested, so will require some more work.

include/linux/hugetlb_inline.h +1 -1
index 593f5d4e108b..755281fab23d 100644
@@ -30,7 +30,7 @@ static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
 
  #endif
 
-static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
+static inline bool is_vm_hugetlb_page(const struct vm_area_struct *vma)
  {
  	return is_vm_hugetlb_flags(vma->vm_flags);
  }
mm/internal.h +0 -0
index 6e1162e13289..b70ebbdafe00 100644
@@ -1242,6 +1242,15 @@ static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
  	}
  	return fpin;

mm/internal.h static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, · line 1253

Quoted 10 lines
mm/internal.h +9 -0
  }
+
+static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
+{
+	if (vma->vm_flags & (VM_SPECIAL | VM_DROPPABLE))
+		return false;
+	if (vma_is_dax(vma) || is_vm_hugetlb_page(vma))
+		return false;
+	return vma != get_gate_vma(vma->vm_mm);
+}

Yeah this is nice.

Quoted 23 lines
mm/internal.h +0 -0
  #else /* !CONFIG_MMU */
  static inline void unmap_mapping_folio(struct folio *folio) { }
  static inline void mlock_new_folio(struct folio *folio) { }
mm/mlock.c +1 -3
index 1a92d16f3684..e16b2ea234f7 100644
@@ -472,9 +472,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
  	int ret = 0;
  	vm_flags_t oldflags = vma->vm_flags;
 
-	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))
+	if (newflags == oldflags || !vma_supports_mlock(vma))
  		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
  		goto out;
 
mm/vma.c +0 -0
index e95fd5a5fe5c..b7055c264b5d 100644

mm/vma.c static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma) · line 2595

Quoted 10 lines
mm/vma.c +1 -3
@@ -2589,9 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
  	vm_stat_account(mm, vma->vm_flags, map->pglen);
  	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
-					is_vm_hugetlb_page(vma) ||
-					vma == get_gate_vma(mm))
+		if (!vma_supports_mlock(vma))
  			vm_flags_clear(vma, VM_LOCKED_MASK);
  		else
  			mm->locked_vm += map->pglen;

Very much preferable!

Quoted 7 lines

--
2.43.0

--
Cheers,

David

Cheers, Lorenzo

#4 lore raw
In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 20 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
  include/linux/mm.h | 3 +++
  mm/mlock.c         | 4 ++--
  mm/vma.c           | 2 +-
  3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
  /* This mask represents all the VMA flag bits used by mlock */
  #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
Quoted 8 lines

include/linux/mm.h enum { · line 578

Quoted 2 lines
include/linux/mm.h +2 -0
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

The usage of "vma->vm_mm" must be double checked, and we'll have to take care of making the tools/testing/vma test happy.

Not even compile tested, so will require some more work.

Thanks, David. This is a better approach that I'll implement. One thing to note is that the check for secretmem has to stay in mlock_fixup() because it's preventing the always-locked memory from being unlocked. I can add an extra comment for that.

Anthony

Quoted 65 lines
include/linux/hugetlb_inline.h +1 -1
index 593f5d4e108b..755281fab23d 100644
@@ -30,7 +30,7 @@ static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
 
  #endif
 
-static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
+static inline bool is_vm_hugetlb_page(const struct vm_area_struct *vma)
  {
  	return is_vm_hugetlb_flags(vma->vm_flags);
  }
mm/internal.h +9 -0
index 6e1162e13289..b70ebbdafe00 100644
@@ -1242,6 +1242,15 @@ static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
  	}
  	return fpin;
  }
+
+static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
+{
+	if (vma->vm_flags & (VM_SPECIAL | VM_DROPPABLE))
+		return false;
+	if (vma_is_dax(vma) || is_vm_hugetlb_page(vma))
+		return false;
+	return vma != get_gate_vma(vma->vm_mm);
+}
  #else /* !CONFIG_MMU */
  static inline void unmap_mapping_folio(struct folio *folio) { }
  static inline void mlock_new_folio(struct folio *folio) { }
mm/mlock.c +1 -3
index 1a92d16f3684..e16b2ea234f7 100644
@@ -472,9 +472,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
  	int ret = 0;
  	vm_flags_t oldflags = vma->vm_flags;
 
-	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))
+	if (newflags == oldflags || !vma_supports_mlock(vma))
  		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
  		goto out;
 
mm/vma.c +1 -3
index e95fd5a5fe5c..b7055c264b5d 100644
@@ -2589,9 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
  	vm_stat_account(mm, vma->vm_flags, map->pglen);
  	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
-					is_vm_hugetlb_page(vma) ||
-					vma == get_gate_vma(mm))
+		if (!vma_supports_mlock(vma))
  			vm_flags_clear(vma, VM_LOCKED_MASK);
  		else
  			mm->locked_vm += map->pglen;
#5 lore raw
In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 20 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
  include/linux/mm.h | 3 +++
  mm/mlock.c         | 4 ++--
  mm/vma.c           | 2 +-
  3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
  /* This mask represents all the VMA flag bits used by mlock */
  #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
Quoted 5 lines

include/linux/mm.h enum { · line 578

Quoted 2 lines
include/linux/mm.h +2 -0
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

The usage of "vma->vm_mm" must be double checked,

This sent me down an interesting rabbit hole since gate_vma->vm_mm is initialized to NULL. I can't see how the gate VMA could ever be passed to mlock_fixup() or __mmap_complete() if it's not part of the VMA tree of an mm and is not mapped through mmap. There are a couple of other places in the kernel that assume the gate VMA may be encountered when iterating VMAs, too. Am I missing something? Happy to clean these up if it makes sense.

Anthony

Quoted 69 lines

and we'll have to take care of making
the tools/testing/vma test happy.

Not even compile tested, so will require some more work.

include/linux/hugetlb_inline.h +1 -1
index 593f5d4e108b..755281fab23d 100644
@@ -30,7 +30,7 @@ static inline bool is_vma_hugetlb_flags(const vma_flags_t *flags)
 
  #endif
 
-static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
+static inline bool is_vm_hugetlb_page(const struct vm_area_struct *vma)
  {
  	return is_vm_hugetlb_flags(vma->vm_flags);
  }
mm/internal.h +9 -0
index 6e1162e13289..b70ebbdafe00 100644
@@ -1242,6 +1242,15 @@ static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
  	}
  	return fpin;
  }
+
+static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
+{
+	if (vma->vm_flags & (VM_SPECIAL | VM_DROPPABLE))
+		return false;
+	if (vma_is_dax(vma) || is_vm_hugetlb_page(vma))
+		return false;
+	return vma != get_gate_vma(vma->vm_mm);
+}
  #else /* !CONFIG_MMU */
  static inline void unmap_mapping_folio(struct folio *folio) { }
  static inline void mlock_new_folio(struct folio *folio) { }
mm/mlock.c +1 -3
index 1a92d16f3684..e16b2ea234f7 100644
@@ -472,9 +472,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
  	int ret = 0;
  	vm_flags_t oldflags = vma->vm_flags;
 
-	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))
+	if (newflags == oldflags || !vma_supports_mlock(vma))
  		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
  		goto out;
 
mm/vma.c +1 -3
index e95fd5a5fe5c..b7055c264b5d 100644
@@ -2589,9 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
  	vm_stat_account(mm, vma->vm_flags, map->pglen);
  	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
-					is_vm_hugetlb_page(vma) ||
-					vma == get_gate_vma(mm))
+		if (!vma_supports_mlock(vma))
  			vm_flags_clear(vma, VM_LOCKED_MASK);
  		else
  			mm->locked_vm += map->pglen;
#6 lore raw
Quoted 28 lines · On 3/10/26 03:04, [email protected] wrote:
Quoted 26 lines · David Hildenbrand (Arm)
Quoted 20 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always

lazily freeable mappings")

---
  include/linux/mm.h | 3 +++
  mm/mlock.c         | 4 ++--
  mm/vma.c           | 2 +-

  3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644
@@ -574,6 +574,9 @@ enum {
   /* This mask represents all the VMA flag bits used by mlock */
   #define VM_LOCKED_MASK    (VM_LOCKED | VM_LOCKONFAULT)
Quoted 2 lines

  +/* This mask prevents VMAs from being mlock'd */ +#define VM_NO_MLOCK_MASK    (VM_SPECIAL | VM_DROPPABLE)

Instead of adding that, could we cleanup further by doing something like the following?

Quoted 6 lines
Quoted 1 line

The usage of "vma->vm_mm" must be double checked,

This sent me down an interesting rabbit hole since gate_vma->vm_mm is initialized to NULL. I can't see how the gate VMA could ever be passed to mlock_fixup() or __mmap_complete() if it's not part of the VMA tree of an mm and is not mapped through mmap.

Right, gate_vma() would be shared across all processes.

Wouldn't code like the following be questionable as well?

fs/coredump.c:  if (vma == get_gate_vma(vma->vm_mm))
mm/vmscan.c:    if (vma == get_gate_vma(vma->vm_mm))

I mean, that cannot possibly be true unless I am missing something.

There are a couple of other

Quoted 3 lines

places in the kernel that assume the gate VMA may be encountered when iterating VMAs, too. Am I missing something? Happy to clean these up if it makes sense.

Yes, please look into that. As an alternative, we could maybe pass current->mm to get_gate_vma() ... but it'd be best to just clean that up.

Signature · 3 lines
Cheers,

David
#7 lore raw
In reply to Anthony Yznaga

-cc old mail (this is going to take some time to propagate I realise :P)

Quoted 3 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

You need to add more details here.

For e.g.: 'in apply_mlockall_flags(), if the flags parameter has MCL_FUTURE set, the current task's mm's default VMA flag field mm->def_flags has VM_LOCKED applied to it. Therefore, in __mmap_complete(), extend the test for VM_SPECIAL to include a test for VM_DROPPABLE'.

Do you have a test that can check for this? It'd be good to have a regression test to assert that it now behaves correctly.

You could extend either tools/testing/selftests/mm/mlock2-tests.c or droppable.c?

It's worth mentioning that mlockall(MCL_ONFAULT) is handled too, as VM_LOCKONFAULT is always set with VM_LOCKED (the only difference being that, when trying to fault in memory for VM_LOCKED ranges, gup exits early in populate_vma_page_range() which has an explicit test for VM_LOCKONFAULT) , and apply_mlockall_flags() will invoke mlock_fixup() which already has the VM_DROPPABLE check.

Quoted 2 lines
Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")

Do we want to cc: stable here?

Quoted 12 lines
---
 include/linux/mm.h | 3 +++
 mm/mlock.c         | 4 ++--
 mm/vma.c           | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644

include/linux/mm.h enum { · line 579

Quoted 6 lines
include/linux/mm.h +3 -0
@@ -574,6 +574,9 @@ enum {
 /* This mask represents all the VMA flag bits used by mlock */
 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)
+

It'd be preferable to not use the legacy VMA flags implementation, but if we're backporting I guess... However there's only one place you need to update, the other already manually checks droppable, and it'd make my life easier for the VMA flags conversions to not define a flag like this also :)

Quoted 8 lines
include/linux/mm.h +0 -0
 /* These flags can be updated atomically via VMA/mmap read lock. */
 #define VM_ATOMIC_SET_ALLOWED VM_MAYBE_GUARD
 
mm/mlock.c +0 -0
index 2f699c3497a5..fd35c1e88c4c 100644

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 477

Quoted 8 lines
mm/mlock.c +2 -2
@@ -472,9 +472,9 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	int ret = 0;
 	vm_flags_t oldflags = vma->vm_flags;
 
-	if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
+	if (newflags == oldflags || (oldflags & VM_NO_MLOCK_MASK) ||
 	    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))

This obviously wouldn't be necessary without adding a new VM_xxx...

mm/vma.c static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma) · line 2592

Quoted 13 lines
mm/mlock.c +0 -0
 		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
 		goto out;
 
mm/vma.c +1 -1
index be64f781a3aa..1334622e4a03 100644
@@ -2589,7 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
 	vm_stat_account(mm, vma->vm_flags, map->pglen);
 	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
+		if ((vm_flags & VM_NO_MLOCK_MASK) || vma_is_dax(vma) ||

For backport maybe just put an additional vm_flags & VM_DROPPABLE here?

Quoted 6 lines
mm/vma.c +0 -1
 					is_vm_hugetlb_page(vma) ||
 					vma == get_gate_vma(mm))
 			vm_flags_clear(vma, VM_LOCKED_MASK);
--

2.47.3

Though I saw David suggested something different so that also addresses my review here :)

Cheers, Lorenzo

#8 lore raw
Quoted 19 lines · Lorenzo Stoakes (Oracle)

-cc old mail (this is going to take some time to propagate I realise :P)

Quoted 3 lines · Anthony Yznaga

Mappings created with MAP_DROPPABLE cannot be locked via mlock() due to the check in mlock_fixup(). However, they will be locked indirectly if they are created after mlockall(MCL_FUTURE).

You need to add more details here.

For e.g.: 'in apply_mlockall_flags(), if the flags parameter has MCL_FUTURE set, the current task's mm's default VMA flag field mm->def_flags has VM_LOCKED applied to it. Therefore, in __mmap_complete(), extend the test for VM_SPECIAL to include a test for VM_DROPPABLE'.

Do you have a test that can check for this? It'd be good to have a regression test to assert that it now behaves correctly.

You could extend either tools/testing/selftests/mm/mlock2-tests.c or droppable.c?

Quoted 6 lines

It's worth mentioning that mlockall(MCL_ONFAULT) is handled too, as VM_LOCKONFAULT is always set with VM_LOCKED (the only difference being that, when trying to fault in memory for VM_LOCKED ranges, gup exits early in populate_vma_page_range() which has an explicit test for VM_LOCKONFAULT) , and apply_mlockall_flags() will invoke mlock_fixup() which already has the VM_DROPPABLE check.

I'll add a more detail commit message, and look at adding a test.

Quoted 3 lines
Quoted 2 lines
Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")

Do we want to cc: stable here?

I don't think so? It seems unlikely to be hit in practice, but I couldn't say for sure.

Anthony

Quoted 66 lines
Quoted 13 lines
---
  include/linux/mm.h | 3 +++
  mm/mlock.c         | 4 ++--
  mm/vma.c           | 2 +-
  3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm.h +0 -0
index 5be3d8a8f806..bb830574d112 100644

include/linux/mm.h enum { · line 579

Quoted 6 lines
include/linux/mm.h +3 -0
@@ -574,6 +574,9 @@ enum {
  /* This mask represents all the VMA flag bits used by mlock */
  #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
 
+/* This mask prevents VMAs from being mlock'd */
+#define VM_NO_MLOCK_MASK	(VM_SPECIAL | VM_DROPPABLE)
+

It'd be preferable to not use the legacy VMA flags implementation, but if we're backporting I guess... However there's only one place you need to update, the other already manually checks droppable, and it'd make my life easier for the VMA flags conversions to not define a flag like this also :)

Quoted 8 lines
include/linux/mm.h +0 -0
  /* These flags can be updated atomically via VMA/mmap read lock. */
  #define VM_ATOMIC_SET_ALLOWED VM_MAYBE_GUARD
 
mm/mlock.c +0 -0
index 2f699c3497a5..fd35c1e88c4c 100644

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 477

Quoted 8 lines
mm/mlock.c +2 -2
@@ -472,9 +472,9 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
  	int ret = 0;
  	vm_flags_t oldflags = vma->vm_flags;
 
-	if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
+	if (newflags == oldflags || (oldflags & VM_NO_MLOCK_MASK) ||
  	    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))

This obviously wouldn't be necessary without adding a new VM_xxx...

mm/vma.c static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma) · line 2592

Quoted 13 lines
mm/mlock.c +0 -0
  		/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
  		goto out;
 
mm/vma.c +1 -1
index be64f781a3aa..1334622e4a03 100644
@@ -2589,7 +2589,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)
 
  	vm_stat_account(mm, vma->vm_flags, map->pglen);
  	if (vm_flags & VM_LOCKED) {
-		if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
+		if ((vm_flags & VM_NO_MLOCK_MASK) || vma_is_dax(vma) ||

For backport maybe just put an additional vm_flags & VM_DROPPABLE here?

Quoted 6 lines
mm/vma.c +0 -1
  					is_vm_hugetlb_page(vma) ||
  					vma == get_gate_vma(mm))
  			vm_flags_clear(vma, VM_LOCKED_MASK);
--

2.47.3

Though I saw David suggested something different so that also addresses my review here :)

Cheers, Lorenzo