Skip to contentSkip to conversation

mm/madvise: fail MADV_PAGEOUT on VM_DROPPABLE VMA

RFC In review Lance Yang 4 participants 7 messages Jan 20, 2025 – Jan 21, 2025

Subsystems (2)
lore
Share & tools
lore

Lance Yang

author
#0 lore raw

RFC 1/1 mm/madvise: fail MADV_PAGEOUT on VM_DROPPABLE VMA

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 mm/madvise.c | 2 ++
 1 file changed, 2 insertions(+)
mm/madvise.c +2 -0
index 49f3a75046f6..29d0234da8a1 100644
@@ -1263,6 +1263,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
 	case MADV_COLD:
 		return madvise_cold(vma, prev, start, end);
 	case MADV_PAGEOUT:
+		if (vma->vm_flags & VM_DROPPABLE)
+			return -EINVAL;
 		return madvise_pageout(vma, prev, start, end);
 	case MADV_FREE:
 	case MADV_DONTNEED:
Signature · 1 line
2.45.2
#1 lore raw
Quoted 9 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

Quoted 6 lines

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

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

I am not convinced this is a correct patch. try_to_unmap_one() will drop the folio.
madvise_pageout(vma, prev, start, end) will still go to shrink_folio_list() and try_to_unmap_one().

Quoted 18 lines
---
 mm/madvise.c | 2 ++
 1 file changed, 2 insertions(+)
mm/madvise.c +2 -1
index 49f3a75046f6..29d0234da8a1 100644
@@ -1263,6 +1263,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
        case MADV_COLD:
                return madvise_cold(vma, prev, start, end);
        case MADV_PAGEOUT:
+               if (vma->vm_flags & VM_DROPPABLE)
+                       return -EINVAL;
                return madvise_pageout(vma, prev, start, end);
        case MADV_FREE:
        case MADV_DONTNEED:
--
Quoted 2 lines

2.45.2

Thanks
Barry

#2 lore raw
In reply to Lance Yang
Quoted 7 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

It behaves exactly correct I think. They can be dropped any time, which includes on misplaced MADV_PAGEOUT.

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
In reply to Lance Yang

Sorry but NACK again on this :(

Quoted 10 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

No, let's not.

Firstly this behaviour, whether you feel it is right or not, is now _established userland behaviour_. A pedantic interpretation of how MADV_PAGEOUT ought to interact with MAP_DROPPABLE doesn't trump what is already in released kernel versions, unless it is wrong in a -broken- fashion.

Also I'd say you'd 100% expect an anon page to be dropped in the case of MAP_DROPPABLE, the semantics of which are 'if there is a request to drop this, drop it'. Here, the user is saying 'hey drop this'. So we drop it :)

I think the more correct patch would be to extend the man page to explicitly mention MAP_DROPPABLE.

Quoted 24 lines
Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 mm/madvise.c | 2 ++
 1 file changed, 2 insertions(+)
mm/madvise.c +2 -1
index 49f3a75046f6..29d0234da8a1 100644
@@ -1263,6 +1263,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
 	case MADV_COLD:
 		return madvise_cold(vma, prev, start, end);
 	case MADV_PAGEOUT:
+		if (vma->vm_flags & VM_DROPPABLE)
+			return -EINVAL;
 		return madvise_pageout(vma, prev, start, end);
 	case MADV_FREE:
 	case MADV_DONTNEED:
--

2.45.2

author
#4 lore raw

Hi Barry, David and Lorenzo,

Thanks a lot for taking time to review!

Quoted 8 lines · Lorenzo Stoakes

Sorry but NACK again on this :(

Quoted 4 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

Quoted 8 lines
Quoted 6 lines

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

No, let's not.

I think I completely got it wrong. Learning so much from your patient responses!

Quoted 6 lines

Firstly this behaviour, whether you feel it is right or not, is now _established userland behaviour_. A pedantic interpretation of how MADV_PAGEOUT ought to interact with MAP_DROPPABLE doesn't trump what is already in released kernel versions, unless it is wrong in a -broken- fashion.

Yeah, this patch would break the established userland behaviour as well.

Quoted 4 lines

Also I'd say you'd 100% expect an anon page to be dropped in the case of MAP_DROPPABLE, the semantics of which are 'if there is a request to drop this, drop it'. Here, the user is saying 'hey drop this'. So we drop it :)

Yep, you're right.

Quoted 3 lines

I think the more correct patch would be to extend the man page to explicitly mention MAP_DROPPABLE.

Thanks again for your time!
Lance

Quoted 25 lines
Quoted 25 lines
Fixes 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings")
---
 mm/madvise.c | 2 ++
 1 file changed, 2 insertions(+)
mm/madvise.c +2 -1
index 49f3a75046f6..29d0234da8a1 100644
@@ -1263,6 +1263,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
      case MADV_COLD:
              return madvise_cold(vma, prev, start, end);
      case MADV_PAGEOUT:
+             if (vma->vm_flags & VM_DROPPABLE)
+                     return -EINVAL;
              return madvise_pageout(vma, prev, start, end);
      case MADV_FREE:
      case MADV_DONTNEED:
--

2.45.2

#5 lore raw
Quoted 18 lines · Lance Yang

Hi Barry, David and Lorenzo,

Thanks a lot for taking time to review!

Quoted 12 lines · Lorenzo Stoakes

Sorry but NACK again on this :(

Quoted 8 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

No, let's not.

I think I completely got it wrong. Learning so much from your patient responses!

Maybe you can make the documentation (man pages?) clearer in that regard? Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
author
#6 lore raw
Quoted 23 lines · David Hildenbrand
Quoted 21 lines · Lance Yang

Hi Barry, David and Lorenzo,

Thanks a lot for taking time to review!

Quoted 15 lines · Lorenzo Stoakes

Sorry but NACK again on this :(

Quoted 10 lines · Lance Yang

MADV_PAGEOUT should fail on VMAs with the VM_DROPPABLE flag. While MADV_PAGEOUT is intended to move anonymous pages to swap, VM_DROPPABLE should not be swapped out.

There is an issue where using MADV_PAGEOUT on a VMA with the VM_DROPPABLE flag behaves like MADV_DONTNEED, causing the pages to be dropped. This could break the semantics of MADV_PAGEOUT, IMO.

So, let's add a check to detect the VM_DROPPABLE flag before doing MADV_PAGEOUT and returns -EINVAL.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

No, let's not.

I think I completely got it wrong. Learning so much from your patient responses!

Maybe you can make the documentation (man pages?) clearer in that regard? Thanks!

Of course, I will give it a try ASAP ;)

Thanks for your suggestion!
Lance

Quoted 6 lines

--
Cheers,

David / dhildenb