make VM_SOFTDIRTY a sticky VMA flag
Subsystems (5)
cover make VM_SOFTDIRTY a sticky VMA flag
Currently we set VM_SOFTDIRTY when a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in __mmap_complete() and do_brk_flags().
However, when performing a merge of existing mappings such as when performing mprotect(), we may lose the VM_SOFTDIRTY flag.
Lorenzo Stoakes (2):
mm: propagate VM_SOFTDIRTY on merge testing/selftests/mm: add soft-dirty merge self-test
include/linux/mm.h | 23 ++++++----- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- tools/testing/vma/vma_internal.h | 23 ++++++----- 3 files changed, 72 insertions(+), 25 deletions(-)
--
2.51.0
PATCH 1/2 mm: propagate VM_SOFTDIRTY on merge
Currently we set VM_SOFTDIRTY when a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in __mmap_complete() and do_brk_flags().
However, when performing a merge of existing mappings such as when performing mprotect(), we may lose the VM_SOFTDIRTY flag.
This is because currently we simply ignore VM_SOFTDIRTY for the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not have VM_SOFTDIRTY set.
Now we have the concept of 'sticky' VMA flags, let's make VM_SOFTDIRTY one which solves this issue.
Additionally update VMA userland tests to propagate changes.
--- include/linux/mm.h | 23 +++++++++++------------ tools/testing/vma/vma_internal.h | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-)
include/linux/mm.h +11 -12
@@ -532,29 +532,28 @@ extern unsigned int kobjsize(const void *objp);* possesses it but the other does not, the merged VMA should nonetheless have * applied to it: * +* VM_SOFTDIRTY - if a VMA is marked soft-dirty, that is has not had its +* references cleared via /proc/$pid/clear_refs, any merged VMA +* should be considered soft-dirty also as it operates at a VMA +* granularity. +* * VM_MAYBE_GUARD - If a VMA may have guard regions in place it implies that * mapped page tables may contain metadata not described by the * VMA and thus any merged VMA may also contain this metadata, * and thus we must make this flag sticky. */ -#define VM_STICKY VM_MAYBE_GUARD +#define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)/* * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one * of these flags and the other not does not preclude a merge. * -* VM_SOFTDIRTY - Should not prevent from VMA merging, if we match the flags but -* dirty bit -- the caller should mark merged VMA as dirty. If -* dirty bit won't be excluded from comparison, we increase -* pressure on the memory system forcing the kernel to generate -* new VMAs when old one could be extended instead. -* -* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones -* which should propagate to all VMAs, but the other does not, -* the merge should still proceed with the merge logic applying -* sticky flags to the final VMA. +* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones +* which should propagate to all VMAs, but the other does not, +* the merge should still proceed with the merge logic applying +* sticky flags to the final VMA. */ -#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +#define VM_IGNORE_MERGE VM_STICKY/* * Flags which should result in page tables being copied on fork. These are
tools/testing/vma/vma_internal.h +11 -12
@@ -122,29 +122,28 @@ extern unsigned long dac_mmap_min_addr;* possesses it but the other does not, the merged VMA should nonetheless have * applied to it: * +* VM_SOFTDIRTY - if a VMA is marked soft-dirty, that is has not had its +* references cleared via /proc/$pid/clear_refs, any merged VMA +* should be considered soft-dirty also as it operates at a VMA +* granularity. +* * VM_MAYBE_GUARD - If a VMA may have guard regions in place it implies that * mapped page tables may contain metadata not described by the * VMA and thus any merged VMA may also contain this metadata, * and thus we must make this flag sticky. */ -#define VM_STICKY VM_MAYBE_GUARD +#define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)/* * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one * of these flags and the other not does not preclude a merge. * -* VM_SOFTDIRTY - Should not prevent from VMA merging, if we match the flags but -* dirty bit -- the caller should mark merged VMA as dirty. If -* dirty bit won't be excluded from comparison, we increase -* pressure on the memory system forcing the kernel to generate -* new VMAs when old one could be extended instead. -* -* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones -* which should propagate to all VMAs, but the other does not, -* the merge should still proceed with the merge logic applying -* sticky flags to the final VMA. +* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones +* which should propagate to all VMAs, but the other does not, +* the merge should still proceed with the merge logic applying +* sticky flags to the final VMA. */ -#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +#define VM_IGNORE_MERGE VM_STICKY/* * Flags which should result in page tables being copied on fork. These are
Signature · 1 line
2.51.0
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
Quoted 56 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>--- include/linux/mm.h | 23 +++++++++++------------ tools/testing/vma/vma_internal.h | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-)include/linux/mm.h +6
-8@@ -532,29 +532,28 @@ extern unsigned int kobjsize(const void *objp);* possesses it but the other does not, the merged VMA should nonetheless have * applied to it: * +* VM_SOFTDIRTY - if a VMA is marked soft-dirty, that is has not had its +* references cleared via /proc/$pid/clear_refs, any merged VMA +* should be considered soft-dirty also as it operates at a VMA +* granularity. +* * VM_MAYBE_GUARD - If a VMA may have guard regions in place it implies that * mapped page tables may contain metadata not described by the * VMA and thus any merged VMA may also contain this metadata, * and thus we must make this flag sticky. */ -#define VM_STICKY VM_MAYBE_GUARD +#define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)/* * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one * of these flags and the other not does not preclude a merge. * -* VM_SOFTDIRTY - Should not prevent from VMA merging, if we match the flags but -* dirty bit -- the caller should mark merged VMA as dirty. If -* dirty bit won't be excluded from comparison, we increase -* pressure on the memory system forcing the kernel to generate -* new VMAs when old one could be extended instead. -* -* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones
include/linux/mm.h extern unsigned int kobjsize(const void *objp); · line 556
Quoted 10 lines
include/linux/mm.h +5
-4-* which should propagate to all VMAs, but the other does not, -* the merge should still proceed with the merge logic applying -* sticky flags to the final VMA. +* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones +* which should propagate to all VMAs, but the other does not, +* the merge should still proceed with the merge logic applying +* sticky flags to the final VMA. */ -#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +#define VM_IGNORE_MERGE VM_STICKY
Logically VM_STICKY should be the only flag qualifying for VM_IGNORE_MERGE. In that case should not VM_IGNORE_MERGE flag be dropped all together ?
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
Quoted 64 lines · Anshuman Khandual
Quoted 61 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>--- include/linux/mm.h | 23 +++++++++++------------ tools/testing/vma/vma_internal.h | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-)include/linux/mm.h +8
-11@@ -532,29 +532,28 @@ extern unsigned int kobjsize(const void *objp);* possesses it but the other does not, the merged VMA should nonetheless have * applied to it: * +* VM_SOFTDIRTY - if a VMA is marked soft-dirty, that is has not had its +* references cleared via /proc/$pid/clear_refs, any merged VMA +* should be considered soft-dirty also as it operates at a VMA +* granularity. +* * VM_MAYBE_GUARD - If a VMA may have guard regions in place it implies that * mapped page tables may contain metadata not described by the * VMA and thus any merged VMA may also contain this metadata, * and thus we must make this flag sticky. */ -#define VM_STICKY VM_MAYBE_GUARD +#define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)/* * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one * of these flags and the other not does not preclude a merge. * -* VM_SOFTDIRTY - Should not prevent from VMA merging, if we match the flags but -* dirty bit -- the caller should mark merged VMA as dirty. If -* dirty bit won't be excluded from comparison, we increase -* pressure on the memory system forcing the kernel to generate -* new VMAs when old one could be extended instead. -* -* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones -* which should propagate to all VMAs, but the other does not, -* the merge should still proceed with the merge logic applying -* sticky flags to the final VMA. +* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones +* which should propagate to all VMAs, but the other does not,
Quoted 8 lines
include/linux/mm.h extern unsigned int kobjsize(const void *objp); · line 556
Quoted 5 lines
include/linux/mm.h +3
-1+* the merge should still proceed with the merge logic applying +* sticky flags to the final VMA. */ -#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +#define VM_IGNORE_MERGE VM_STICKYLogically
VM_STICKYshould be the only flag qualifying forVM_IGNORE_MERGE. In that case should notVM_IGNORE_MERGEflag be dropped all together ?
I intentionally kept it to be explicit. This is self-documenting as-is.
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
In reply to Lorenzo StoakesQuoted 12 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.
Quoted 8 lines
Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>---
Looks reasonable to me. I thought that we had that behavior in the past ... but I also remember scenarios where we would have imprecise soft-dirty handling. So I assume this was semi-broken for a while (soft-broken :) )
Signature · 3 lines
Cheers David
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
Quoted 18 lines · David Hildenbrand (Red Hat)
Quoted 17 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Quoted 7 lines
Quoted 3 lines
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>---
Looks reasonable to me. I thought that we had that behavior in the past ... but I also remember scenarios where we would have imprecise soft-dirty handling. So I assume this was semi-broken for a while (soft-broken :) )
:))
Yeah it's only specific merge scenarios, and only when you e.g. already cleared refs then mapped a new VMA and it happened to merge.
Nicer thing about this change is we stop treating VM_SOFTDIRTY like an exception on merge :)
Quoted 2 lines
Cheers!
Quoted 5 lines
--
CheersDavid
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
In reply to Lorenzo StoakesQuoted 6 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.
Does it make sense to backport this to stable? A more minimal version, that is.
Quoted 6 lines
This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.
Quoted 7 lines
Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>
Signature · 1 line
Pedro
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
Quoted 9 lines · Pedro Falcato
Quoted 6 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.Does it make sense to backport this to stable? A more minimal version, that is.
No :) This has been subtly broken since forever. I don't think it warrants that and it'd require significant and risky changes to older kernels to even make it possible.
It's more a biproduct of features added so let's fix this going forward.
Quoted 10 lines
Quoted 10 lines
This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.
Quoted 6 lines
Quoted 4 lines
Additionally update VMA userland tests to propagate changes.
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>Reviewed-by Pedro Falcato <[email protected]>
Thanks!
Quoted 3 lines
--
Pedro
Cheers, Lorenzo
Re: [PATCH 1/2] mm: propagate VM_SOFTDIRTY on merge
In reply to Lorenzo StoakesQuoted 12 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.This is because currently we simply ignore
VM_SOFTDIRTYfor the purposes of merge, so one VMA may possess the flag and another not, and whichever happens to be the target VMA will be the one upon which the merge is performed which may or may not haveVM_SOFTDIRTYset.
Quoted 5 lines
Now we have the concept of 'sticky' VMA flags, let's make
VM_SOFTDIRTYone which solves this issue.Additionally update VMA userland tests to propagate changes.
Nit below on the comment, but looks good.
Quoted 36 lines
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Lorenzo Stoakes <[email protected]>--- include/linux/mm.h | 23 +++++++++++------------ tools/testing/vma/vma_internal.h | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-)include/linux/mm.h +6
-5@@ -532,29 +532,28 @@ extern unsigned int kobjsize(const void *objp);* possesses it but the other does not, the merged VMA should nonetheless have * applied to it: * +* VM_SOFTDIRTY - if a VMA is marked soft-dirty, that is has not had its +* references cleared via /proc/$pid/clear_refs, any merged VMA +* should be considered soft-dirty also as it operates at a VMA +* granularity. +* * VM_MAYBE_GUARD - If a VMA may have guard regions in place it implies that * mapped page tables may contain metadata not described by the * VMA and thus any merged VMA may also contain this metadata, * and thus we must make this flag sticky. */ -#define VM_STICKY VM_MAYBE_GUARD +#define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)/* * VMA flags we ignore for the purposes of merge, i.e. one VMA possessing one * of these flags and the other not does not preclude a merge. * -* VM_SOFTDIRTY - Should not prevent from VMA merging, if we match the flags but -* dirty bit -- the caller should mark merged VMA as dirty. If -* dirty bit won't be excluded from comparison, we increase -* pressure on the memory system forcing the kernel to generate
include/linux/mm.h extern unsigned int kobjsize(const void *objp); · line 554
Quoted 10 lines
include/linux/mm.h +4
-6-* new VMAs when old one could be extended instead. -* -* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones -* which should propagate to all VMAs, but the other does not, -* the merge should still proceed with the merge logic applying -* sticky flags to the final VMA. +* VM_STICKY - If one VMA has flags which most be 'sticky', that is ones +* which should propagate to all VMAs, but the other does not, +* the merge should still proceed with the merge logic applying +* sticky flags to the final VMA.
Nit, could we also fix the wording of this comment when you are fixing the spacing?
include/linux/mm.h extern unsigned int kobjsize(const void *objp); · line 556
Quoted 3 lines
include/linux/mm.h +1
-1*/ -#define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +#define VM_IGNORE_MERGE VM_STICKY
I also like keeping this for self-documenting code.
Thanks,
Liam
PATCH 2/2 testing/selftests/mm: add soft-dirty merge self-test
In reply to Lorenzo StoakesAssert that we correctly merge VMAs containing VM_SOFTDIRTY flags now that we correctly handle these as sticky.
In order to do so, we have to account for the fact the pagemap interface checks soft dirty PTEs and additionally that newly merged VMAs are marked VM_SOFTDIRTY.
To account for this we use unfaulted anon VMAs, mapping one VMA in and clearing soft-dirty, then another separate from the first which will be marked soft-dirty which we then mremap() into place.
--- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/soft-dirty.c +50 -1
@@ -184,6 +184,54 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)close (test_fd); } +static void test_merge (int pagemap_fd,int pagesize) +{ +char *reserved, *map, *map2; + +/* Reserve space. */ + reserved =mmap (NULL ,4 * pagesize,PROT_NONE , +MAP_ANON |MAP_PRIVATE ,-1 ,0 ); +if (reserved ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); +munmap (reserved,4 * pagesize); + +/* Map a page. */ + map =mmap (&reserved[pagesize], pagesize,PROT_READ |PROT_WRITE , +MAP_ANON |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +if (map ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); + +/* This will clear VM_SOFTDIRTY too. */ +clear_softdirty (); + +/* +* Now place a new mapping which will be marked VM_SOFTDIRTY. Away from +* map. +*/ + map2 =mmap (&reserved[3 * pagesize], pagesize,PROT_READ |PROT_WRITE , +MAP_ANON |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +if (map2 ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); + +/* +* Now remap it immediately adjacent to map, if the merge correctly +* propagates VM_SOFTDIRTY, we should then observe the VMA as a whole +* being marked soft-dirty. +*/ + map2 =mremap (map2, pagesize, pagesize,MREMAP_FIXED |MREMAP_MAYMOVE , + &reserved[2 * pagesize]); +if (map2 ==MAP_FAILED ) +ksft_exit_fail_msg ("mremap failed\n" ); +ksft_test_result (pagemap_is_softdirty (pagemap_fd, map) ==1 , +"Test %s-anon soft-dirty after merge 1st pg\n" , + __func__); +ksft_test_result (pagemap_is_softdirty (pagemap_fd, map2) ==1 , +"Test %s-anon soft-dirty after merge 2nd pg\n" , + __func__); + +munmap (map,2 * pagesize); +} +static void test_mprotect_anon (int pagemap_fd,int pagesize) {test_mprotect (pagemap_fd, pagesize, true);
@@ -204,7 +252,7 @@ int main(int argc, char **argv)if (!softdirty_supported ())ksft_exit_skip ("soft-dirty is not support\n" ); -ksft_set_plan (15 ); +ksft_set_plan (17 ); pagemap_fd =open (PAGEMAP_FILE_PATH ,O_RDONLY );if (pagemap_fd <0 )ksft_exit_fail_msg ("Failed to open %s\n" ,PAGEMAP_FILE_PATH );
@@ -216,6 +264,7 @@ int main(int argc, char **argv)test_hugepage (pagemap_fd, pagesize);test_mprotect_anon (pagemap_fd, pagesize);test_mprotect_file (pagemap_fd, pagesize); +test_merge (pagemap_fd, pagesize);close (pagemap_fd);
Signature · 1 line
2.51.0
David Hildenbrand (Red Hat)
maintainerRe: [PATCH 2/2] testing/selftests/mm: add soft-dirty merge self-test
Quoted 21 lines · Lorenzo Stoakes
Assert that we correctly merge VMAs containing
VM_SOFTDIRTYflags now that we correctly handle these as sticky.In order to do so, we have to account for the fact the pagemap interface checks soft dirty PTEs and additionally that newly merged VMAs are marked
VM_SOFTDIRTY.To account for this we use unfaulted anon VMAs, mapping one VMA in and clearing soft-dirty, then another separate from the first which will be marked soft-dirty which we then
mremap()into place.Signed-off-by Lorenzo Stoakes <[email protected]>--- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)tools/testing/selftests/mm/soft-dirty.c +0
-0
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 191
Quoted 8 lines
tools/testing/selftests/mm/soft-dirty.c +5
-0@@ -184,6 +184,54 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)close (test_fd); } +static void test_merge (int pagemap_fd,int pagesize) +{ +char *reserved, *map, *map2; + +/* Reserve space. */
It took me a while to figure out why you are using 4 pages. I guess you want to make sure that we don't end up merging to the left (or the right). A diagram would have helped me.
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 198
Quoted 7 lines
tools/testing/selftests/mm/soft-dirty.c +7
-0+ reserved =mmap (NULL ,4 * pagesize,PROT_NONE , +MAP_ANON |MAP_PRIVATE ,-1 ,0 ); +if (reserved ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); +munmap (reserved,4 * pagesize); + +/* Map a page. */
Note that we are not actually "mapping a page". "Create a new page-sized VMA" or sth like that.
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 209
Quoted 11 lines
tools/testing/selftests/mm/soft-dirty.c +11
-0+ map =mmap (&reserved[pagesize], pagesize,PROT_READ |PROT_WRITE , +MAP_ANON |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +if (map ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); + +/* This will clear VM_SOFTDIRTY too. */ +clear_softdirty (); + +/* +* Now place a new mapping which will be marked VM_SOFTDIRTY. Away from +* map.
Could we have something "to the right" of this new VMA that we might be merging with (that might interfere?) and if so, do we care?
Just wondering if we would actually want a reserved area that spans 5 page sizes to rule out these cases.
mmap1
[ empty ][ VMA1 ][ empty ]
mmap2
[ empty ][ VMA1 ][ empty ][ VMA2 ][ empty ]
mremap
[ empty ][ VMA1 ][ VMA2 ][ empty ]
which is after the merge
[ empty ][ VMA (SD) ][ ]
Signature · 3 lines
Cheers David
Re: [PATCH 2/2] testing/selftests/mm: add soft-dirty merge self-test
Quoted 17 lines · David Hildenbrand (Red Hat)
Quoted 16 lines · Lorenzo Stoakes
Assert that we correctly merge VMAs containing
VM_SOFTDIRTYflags now that we correctly handle these as sticky.In order to do so, we have to account for the fact the pagemap interface checks soft dirty PTEs and additionally that newly merged VMAs are marked
VM_SOFTDIRTY.To account for this we use unfaulted anon VMAs, mapping one VMA in and clearing soft-dirty, then another separate from the first which will be marked soft-dirty which we then
mremap()into place.Signed-off-by Lorenzo Stoakes <[email protected]>--- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)
Quoted 16 lines
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 190
Quoted 12 lines
tools/testing/selftests/mm/soft-dirty.c +5
-0@@ -184,6 +184,54 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)close (test_fd); } +static void test_merge (int pagemap_fd,int pagesize) +{ +char *reserved, *map, *map2; + +/* Reserve space. */ It took me a while to figure out why you are using 4 pages. I guess you want to make sure that we don't end up merging to the left (or the right). A diagram would have helped me.
I have spoiled everybody too much with my ASCII diagrams ;)
But _just for you_ I will make one again ;))
Quoted 11 lines
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 198
Quoted 8 lines
tools/testing/selftests/mm/soft-dirty.c +7
-0+ reserved =mmap (NULL ,4 * pagesize,PROT_NONE , +MAP_ANON |MAP_PRIVATE ,-1 ,0 ); +if (reserved ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); +munmap (reserved,4 * pagesize); + +/* Map a page. */ Note that we are not actually "mapping a page". "Create a new page-sized VMA" or sth like that.
Lol depends on your definition I suppose. Not in the sense of page table mappings. I guess this is fairly redundant anyway so can dorp
Quoted 16 lines
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 209
Quoted 12 lines
tools/testing/selftests/mm/soft-dirty.c +11
-0+ map =mmap (&reserved[pagesize], pagesize,PROT_READ |PROT_WRITE , +MAP_ANON |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +if (map ==MAP_FAILED ) +ksft_exit_fail_msg ("mmap failed\n" ); + +/* This will clear VM_SOFTDIRTY too. */ +clear_softdirty (); + +/* +* Now place a new mapping which will be marked VM_SOFTDIRTY. Away from +* map. Could we have something "to the right" of this new VMA that we might be merging with (that might interfere?) and if so, do we care?
Quoted 3 lines
Just wondering if we would actually want a reserved area that spans 5 page sizes to rule out these cases.
You're right! This is an oversight, will fix.
Quoted 8 lines
mmap1
[ empty ][ VMA1 ][ empty ]
mmap2
[ empty ][ VMA1 ][ empty ][ VMA2 ][ empty ]
Quoted 7 lines
mremap
[ empty ][ VMA1 ][ VMA2 ][ empty ]
which is after the merge
[ empty ][ VMA (SD) ][ ]
Yeah this is the purpose of adding space around.
I also want to add an additional test anyway so can do both things at once :)
Quoted 6 lines
--
CheersDavid
Cheers, Lorenzo
Andrew Morton
maintainerQuoted 7 lines · On Fri, 14 Nov 2025 17:53:17 +0000 Lorenzo Stoakes…
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.
userspace-visible effects?
Documentation/admin-guide/mm/soft-dirty.rst tells me that this can already happen in other circumstances so I guess it isn't very serious. CRIU inefficiency. perhaps?
Please review Documentation/admin-guide/mm/soft-dirty.rst, check that it is complete and accurate?
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Fri, 14 Nov 2025 17:53:17 +0000 Lorenzo Stoakes…
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.userspace-visible effects?
Simply more correct accounting of soft-dirty :)
Quoted 4 lines
Documentation/admin-guide/mm/soft-dirty.rsttells me that this can already happen in other circumstances so I guess it isn't very serious. CRIU inefficiency. perhaps?
I don't think it should cause inefficiency other than us already _accidentally_ being more efficient, see the discussion in thread :)
Quoted 3 lines
Please review
Documentation/admin-guide/mm/soft-dirty.rst, check that it is complete and accurate?
It LGTM, we are changing some very specific internal implementation detail here which I don't think is worth mentioning here (effectively - 'we used to be wrong sometimes, now not so much' :)
(Staring at tihs I realise I _probably_ need to change something very specific in the original sticky implementation. The VMA implementation is so finnicky... will send follow up fixpatch/respin on that w/details)
Cheers, Lorenzo
Quoted 7 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.
Losing VM_SOFTDIRTY is definitely a bug, thank you for fixing it.
A separate concern is whether merging two VMAs should be permitted when one has the VM_SOFTDIRTY flag set and another does not. I think the merging operation should be disallowed.The issue is that PAGE_IS_SOFT_DIRTY will be reported for every page in the resulting VMA. Consider a scenario where a large VMA has only a small number of pages marked SOFT_DIRTY. If we merge it with a smaller VMA that does have VM_SOFTDIRTY, all pages in the originally large VMA will subsequently be reported as SOFT_DIRTY. As a result, CRIU will needlessly dump all of these pages again, even though the vast majority of them were unchanged since the prior checkpoint iteration.
Thanks,
Andrei
Quoted 14 lines
Lorenzo Stoakes (2):
mm: propagateVM_SOFTDIRTYon merge testing/selftests/mm: add soft-dirty merge self-testinclude/linux/mm.h | 23 ++++++----- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- tools/testing/vma/vma_internal.h | 23 ++++++----- 3 files changed, 72 insertions(+), 25 deletions(-)--
2.51.0
Quoted 7 lines · Andrei Vagin
Quoted 5 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().
Quoted 8 lines
Quoted 2 lines
However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.Losing
VM_SOFTDIRTYis definitely a bug, thank you for fixing it.A separate concern is whether merging two VMAs should be permitted when one has the
VM_SOFTDIRTYflag set and another does not. I think the merging operation should be disallowed.The issue is that
If merging VM_SOFTDIRTY and non-VM_SOFTDIRTY VMAs would not be allowed then what is the point for moving VM_SOFTDIRTY as VM_STICKY ?
Quoted 26 lines
PAGE_IS_SOFT_DIRTYwill be reported for every page in the resulting VMA. Consider a scenario where a large VMA has only a small number of pages markedSOFT_DIRTY. If we merge it with a smaller VMA that does haveVM_SOFTDIRTY, all pages in the originally large VMA will subsequently be reported asSOFT_DIRTY. As a result, CRIU will needlessly dump all of these pages again, even though the vast majority of them were unchanged since the prior checkpoint iteration.Thanks,
AndreiQuoted 14 lines
Lorenzo Stoakes (2):
mm: propagateVM_SOFTDIRTYon merge testing/selftests/mm: add soft-dirty merge self-testinclude/linux/mm.h | 23 ++++++----- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- tools/testing/vma/vma_internal.h | 23 ++++++----- 3 files changed, 72 insertions(+), 25 deletions(-)--
2.51.0
Quoted 7 lines · Andrei Vagin
Quoted 5 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().
Quoted 8 lines
Quoted 2 lines
However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.Losing
VM_SOFTDIRTYis definitely a bug, thank you for fixing it.A separate concern is whether merging two VMAs should be permitted when one has the
VM_SOFTDIRTYflag set and another does not. I think the merging operation should be disallowed.The issue is that
This patch doesn't change anything in terms of merging, it only _correctly_ marks VMAs as soft-dirty where certain, very specific, circumstances might result in a merged VMA being incorrectly indicated to not be soft-dirty when it in fact contains pages which are.
Since VMA fragmentation is an issue that impacts non-softydirty users, I'm afraid we cannot split on this parameter.
It'd also be a user-visible change that could cause breaking issues (mremap() for instances in _most_ cases requires that it operates on a single VMA).
So this isn't possible.
Quoted 7 lines
PAGE_IS_SOFT_DIRTYwill be reported for every page in the resulting VMA. Consider a scenario where a large VMA has only a small number of pages markedSOFT_DIRTY. If we merge it with a smaller VMA that does haveVM_SOFTDIRTY, all pages in the originally large VMA will subsequently be reported asSOFT_DIRTY. As a result, CRIU will needlessly dump all of these pages again, even though the vast majority of them were unchanged since the prior checkpoint iteration.
I think there's some confusion about what is possible here.
Currently if you don't invoke /proc/$pid/clear_refs, all VMAs will have soft-dirty set until you do.
So this is a situation that _already exists_.
And intentionally so - we default all VMAs to soft-dirty so users can detect new mappings in order not to perceive e.g. mmap()'ing over an existing range as as being no change.
OK so what if you clear references? Considering:
1. Map large VMA
2. Clear references
3. Dirty several pages (VM_SOFTDIRTY clear)
4. Map new VMA immediately _after_ it (VM_SOFTDIRTY set)
5. Merge - Before this patch: VM_SOFTDIRTY bit cleared on merge BUT SET AGAIN due to it being an mmap() invocation. After this patch:
VM_SOFTDIRTY bit retained on merge but also set again due to it being an mmap() invocation.
So this kind of merge has no change in behaviour.
And again, it's correct - the user needs to be able to identify what's changed.
This change fixes this behaviour to be consistent for other types of merge, when previously it was not.
In the past, you'd get soft-dirty set/not set _depending on the type of merge_. So if the target VMA had the flag set, you'd have it marked soft-dirty, otherwise not.
Since it's unacceptabale to fragment VMAs on the basis of soft-dirty, we're _only_ improving correctness here, and this patch is a net good no matter what.
Quoted 18 lines
Thanks,
AndreiQuoted 14 lines
Lorenzo Stoakes (2):
mm: propagateVM_SOFTDIRTYon merge testing/selftests/mm: add soft-dirty merge self-testinclude/linux/mm.h | 23 ++++++----- tools/testing/selftests/mm/soft-dirty.c | 51 ++++++++++++++++++++++++- tools/testing/vma/vma_internal.h | 23 ++++++----- 3 files changed, 72 insertions(+), 25 deletions(-)--
2.51.0
Cheers, Lorenzo
Quoted 19 lines · Lorenzo Stoakes
Quoted 7 lines · Andrei Vagin
Quoted 5 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().Quoted 8 lines
Quoted 2 lines
However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.Losing
VM_SOFTDIRTYis definitely a bug, thank you for fixing it.A separate concern is whether merging two VMAs should be permitted when one has the
VM_SOFTDIRTYflag set and another does not. I think the merging operation should be disallowed.The issue is that
Quoted 4 lines
This patch doesn't change anything in terms of merging, it only _correctly_ marks VMAs as soft-dirty where certain, very specific, circumstances might result in a merged VMA being incorrectly indicated to not be soft-dirty when it in fact contains pages which are.
As I mentioned in the previous message, this patch is correct, and I appreciate your effort to solve this issue. My comment was about whether we should allow merging VMAs if one has VM_SOFTDIRTY and the other does not. You are right, this is a separate question unrelated to this patch.
I recall correctly that initially, merging vma-s with different VM_SORTDIRTY bit values was not allowed. It was a bit surprising that this behavior was changed by Cyrill in 34228d473efe. Cyrill was an active CRIU contributor at the time, so we can't even blame anyone for breaking CRIU :).
Thanks,
Andrei
Quoted 26 lines · Andrei Vagin
Quoted 19 lines · Lorenzo Stoakes
Quoted 7 lines · Andrei Vagin
Quoted 5 lines · Lorenzo Stoakes
Currently we set
VM_SOFTDIRTYwhen a new mapping is set up (whether by establishing a new VMA, or via merge) as implemented in__mmap_complete()anddo_brk_flags().Quoted 8 lines
Quoted 2 lines
However, when performing a merge of existing mappings such as when performing
mprotect(), we may lose theVM_SOFTDIRTYflag.Losing
VM_SOFTDIRTYis definitely a bug, thank you for fixing it.A separate concern is whether merging two VMAs should be permitted when one has the
VM_SOFTDIRTYflag set and another does not. I think the merging operation should be disallowed.The issue is thatQuoted 4 lines
This patch doesn't change anything in terms of merging, it only _correctly_ marks VMAs as soft-dirty where certain, very specific, circumstances might result in a merged VMA being incorrectly indicated to not be soft-dirty when it in fact contains pages which are.
Quoted 4 lines
As I mentioned in the previous message, this patch is correct, and I appreciate your effort to solve this issue. My comment was about whether we should allow merging VMAs if one has
VM_SOFTDIRTYand the other does not. You are right, this is a separate question unrelated to this patch.
Thanks :)
Quoted 6 lines
I recall correctly that initially, merging vma-s with different
VM_SORTDIRTYbit values was not allowed. It was a bit surprising that this behavior was changed by Cyrill in34228d473efe. Cyrill was an active CRIU contributor at the time, so we can't even blame anyone for breaking CRIU :).
Well I think Cyrill is in the right here :) the problem described there - that of hitting the max_map_count simply due to failed VM_SOFTDIRTY merges
- is very serious and clearly highlights the issue that arises from not merging these - that is VMA fragmentation.
Quoted 3 lines
Thanks,
Andrei
Cheers, Lorenzo
Cyrill Gorcunov
Acked-byOn Mon, Nov 17, 2025 at 07:57:30PM +0000, Lorenzo Stoakes wrote:
...
Quoted 10 lines
Quoted 5 lines
I recall correctly that initially, merging vma-s with different
VM_SORTDIRTYbit values was not allowed. It was a bit surprising that this behavior was changed by Cyrill in34228d473efe. Cyrill was an active CRIU contributor at the time, so we can't even blame anyone for breaking CRIU :).Well I think Cyrill is in the right here :) the problem described there - that of hitting the
max_map_countsimply due to failedVM_SOFTDIRTYmerges
- is very serious and clearly highlights the issue that arises from not merging these - that is VMA fragmentation.
Hi guys! Happen to miss this thread due to high message traffic, thanks for CC'ing me ;) Yeah, disability to merge VMAs due to softdirty bit has been a serious issue, so better criu to dump more (redundant) memory than apps got broken. As to this patch series, I think it is good, thanks a huge Lorenzo!
Quoted 13 lines · Cyrill Gorcunov
On Mon, Nov 17, 2025 at 07:57:30PM +0000, Lorenzo Stoakes wrote:
...Quoted 10 lines
Quoted 5 lines
I recall correctly that initially, merging vma-s with different
VM_SORTDIRTYbit values was not allowed. It was a bit surprising that this behavior was changed by Cyrill in34228d473efe. Cyrill was an active CRIU contributor at the time, so we can't even blame anyone for breaking CRIU :).Well I think Cyrill is in the right here :) the problem described there - that of hitting the
max_map_countsimply due to failedVM_SOFTDIRTYmerges
- is very serious and clearly highlights the issue that arises from not merging these - that is VMA fragmentation.
Quoted 7 lines
Hi guys! Happen to miss this thread due to high message traffic, thanks for CC'ing me ;) Yeah, disability to merge VMAs due to softdirty bit has been a serious issue, so better criu to dump more (redundant) memory than apps got broken. As to this patch series, I think it is good, thanks a huge Lorenzo!
Acked-by Cyrill Gorcunov <[email protected]>
Thanks :)