mm: Add MREMAP_DONTUNMAP to mremap().
Subsystems (5)
PATCH v7 1/2 mm: Add MREMAP_DONTUNMAP to mremap().
When remapping an anonymous, private mapping, if MREMAP_DONTUNMAP is set, the source mapping will not be removed. The remap operation will be performed as it would have been normally by moving over the page tables to the new mapping. The old vma will have any locked flags cleared, have no pagetables, and any userfaultfds that were watching that range will continue watching it.
For a mapping that is shared or not anonymous, MREMAP_DONTUNMAP will cause the mremap() call to fail. Because MREMAP_DONTUNMAP always results in moving a VMA you MUST use the MREMAP_MAYMOVE flag, it's not possible to resize a VMA while also moving with MREMAP_DONTUNMAP so old_len must always be equal to the new_len otherwise it will return -EINVAL.
We hope to use this in Chrome OS where with userfaultfd we could write an anonymous mapping to disk without having to STOP the process or worry about VMA permission changes.
This feature also has a use case in Android, Lokesh Gidra has said that "As part of using userfaultfd for GC, We'll have to move the physical pages of the java heap to a separate location. For this purpose mremap will be used. Without the MREMAP_DONTUNMAP flag, when I mremap the java heap, its virtual mapping will be removed as well. Therefore, we'll require performing mmap immediately after. This is not only time consuming but also opens a time window where a native thread may call mmap and reserve the java heap's address range for its own usage. This flag solves the problem."
v6 -> v7:
- Don't allow resizing VMA as part of MREMAP_DONTUNMAP. There is no clear use case at the moment and it can be added later as it simplifies the implementation for now.
v5 -> v6:
- Code cleanup suggested by Kirill.
v4 -> v5:
- Correct commit message to more accurately reflect the behavior.
- Clear VM_LOCKED and VM_LOCKEDONFAULT on the old vma.
--- include/uapi/linux/mman.h | 5 ++- mm/mremap.c | 90 ++++++++++++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 23 deletions(-)
include/uapi/linux/mman.h +3 -2
@@ -5,8 +5,9 @@#include <asm/mman.h> #include <asm-generic/hugetlb_encode.h> -#define MREMAP_MAYMOVE 1 -#define MREMAP_FIXED 2 +#define MREMAP_MAYMOVE 1 +#define MREMAP_FIXED 2 +#define MREMAP_DONTUNMAP 4#define OVERCOMMIT_GUESS 0#define OVERCOMMIT_ALWAYS 1
mm/mremap.c +69 -21
191 lines not on this page · raw message
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
PATCH v7 2/2 selftest: Add MREMAP_DONTUNMAP selftest.
Add a few simple self tests for the new flag MREMAP_DONTUNMAP, they are simple smoke tests which also demonstrate the behavior.
--- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mremap_dontunmap.c | 313 ++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 15 + 3 files changed, 329 insertions(+) create mode 100644 tools/testing/selftests/vm/mremap_dontunmap.c
tools/testing/selftests/vm/Makefile +1 -0
@@ -12,6 +12,7 @@ TEST_GEN_FILES += map_fixed_noreplace TEST_GEN_FILES += map_populate TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests +TEST_GEN_FILES += mremap_dontunmap TEST_GEN_FILES += on-fault-limit TEST_GEN_FILES += thuge-gen TEST_GEN_FILES += transhuge-stress
tools/testing/selftests/vm/mremap_dontunmap.c new file +313 -0
313 lines not on this page · raw message
tools/testing/selftests/vm/run_vmtests +15 -0
@@ -227,4 +227,19 @@ else exitcode=1 fi +echo "------------------------------------" +echo "running MREMAP_DONTUNMAP smoke test" +echo "------------------------------------" +./mremap_dontunmap +ret_val=$? + +if [ $ret_val -eq 0 ]; then + echo "[PASS]" +elif [ $ret_val -eq $ksft_skip ]; then + echo "[SKIP]" + exitcode=$ksft_skip +else + echo "[FAIL]" + exitcode=1 +fi exit $exitcode
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
PATCH v7 1/1 mremap.2: Add information for MREMAP_DONTUNMAP.
In reply to Brian Geffon--- man2/mremap.2 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
man2/mremap.2 +29 -1
@@ -26,7 +26,8 @@ .\" 1996-04-12 Tom Bjorkholm <[email protected]> .\" Update for Linux 1.3.87 and later .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text. -.\" +.\" 2020-02-05 Brian Geffon <[email protected]> +.\" Update for MREMAP_DONTUNMAP. .TH MREMAP 2 2019-03-06 "Linux" "Linux Programmer's Manual" .SH NAME mremap \- remap a virtual memory address
@@ -129,6 +130,13 @@ If is specified, then .B MREMAP_MAYMOVE must also be specified. +.TP +.BR MREMAP_DONTUNMAP " (since Linux 5.7)" +This flag which must be used in conjuction with +.B MREMAP_MAYMOVE +remaps a mapping to a new address and it does not unmap the mapping at \fIold_address\fP. This flag can only be used with private anonymous mappings. Any access to the range specified at \fIold_address\fP after completion will result in a page fault. If a +.BR userfaultfd (2) +was registered on the mapping specified by \fIold_address\fP it will continue to watch that mapping for faults. .PP If the memory segment specified by .I old_address
@@ -176,6 +184,8 @@ a value other than .B MREMAP_MAYMOVE or .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified in .IR flags ; .IP *
@@ -197,9 +207,17 @@ and .IR old_size ; .IP * .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified without also specifying .BR MREMAP_MAYMOVE ; .IP * +.B MREMAP_DONTUNMAP +was specified with an \fIold_address\fP that was not private anonymous; +.IP * +.B MREMAP_DONTUNMAP +was specified and \fIold_size\fP was not equal to \fInew_size\fP; +.IP * \fIold_size\fP was zero and \fIold_address\fP does not refer to a shareable mapping (but see BUGS); .IP *
@@ -209,10 +227,20 @@ flag was not specified. .RE .TP .B ENOMEM +Not enough memory was available to complete the operation. +Possible causes are: +.RS +.IP * 3 The memory area cannot be expanded at the current virtual address, and the .B MREMAP_MAYMOVE flag is not set in \fIflags\fP. Or, there is not enough (virtual) memory available. +.IP * +.B MREMAP_DONTUNMAP +was used without +.B MREMAP_FIXED +causing a new mapping to be created that would exceed the virtual memory available or it would exceed the maximum number of allowed mappings. +.RE .SH CONFORMING TO This call is Linux-specific, and should not be used in programs intended to be portable.
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
Quoted 35 lines · Brian Geffon
When remapping an anonymous, private mapping, if
MREMAP_DONTUNMAPis set, the source mapping will not be removed. The remap operation will be performed as it would have been normally by moving over the page tables to the new mapping. The old vma will have any locked flags cleared, have no pagetables, and any userfaultfds that were watching that range will continue watching it.For a mapping that is shared or not anonymous,
MREMAP_DONTUNMAPwill cause themremap()call to fail. BecauseMREMAP_DONTUNMAPalways results in moving a VMA you MUST use theMREMAP_MAYMOVEflag, it's not possible to resize a VMA while also moving withMREMAP_DONTUNMAPsoold_lenmust always be equal to thenew_lenotherwise it will return-EINVAL.We hope to use this in Chrome OS where with userfaultfd we could write an anonymous mapping to disk without having to STOP the process or worry about VMA permission changes.
This feature also has a use case in Android, Lokesh Gidra has said that "As part of using userfaultfd for GC, We'll have to move the physical pages of the java heap to a separate location. For this purpose mremap will be used. Without the
MREMAP_DONTUNMAPflag, when I mremap the java heap, its virtual mapping will be removed as well. Therefore, we'll require performing mmap immediately after. This is not only time consuming but also opens a time window where a native thread may call mmap and reserve the java heap's address range for its own usage. This flag solves the problem."v6 -> v7:
- Don't allow resizing VMA as part ofMREMAP_DONTUNMAP. There is no clear use case at the moment and it can be added later as it simplifies the implementation for now.v5 -> v6:
- Code cleanup suggested by Kirill.
Quoted 7 lines
v4 -> v5:
- Correct commit message to more accurately reflect the behavior.
- ClearVM_LOCKEDandVM_LOCKEDONFAULTon the old vma.Signed-off-by Brian Geffon <[email protected]>Reviewed-by Minchan Kim <[email protected]>Tested-by Lokesh Gidra <[email protected]>
Signature · 1 line
Kirill A. Shutemov
Quoted 35 lines · Brian Geffon
When remapping an anonymous, private mapping, if
MREMAP_DONTUNMAPis set, the source mapping will not be removed. The remap operation will be performed as it would have been normally by moving over the page tables to the new mapping. The old vma will have any locked flags cleared, have no pagetables, and any userfaultfds that were watching that range will continue watching it.For a mapping that is shared or not anonymous,
MREMAP_DONTUNMAPwill cause themremap()call to fail. BecauseMREMAP_DONTUNMAPalways results in moving a VMA you MUST use theMREMAP_MAYMOVEflag, it's not possible to resize a VMA while also moving withMREMAP_DONTUNMAPsoold_lenmust always be equal to thenew_lenotherwise it will return-EINVAL.We hope to use this in Chrome OS where with userfaultfd we could write an anonymous mapping to disk without having to STOP the process or worry about VMA permission changes.
This feature also has a use case in Android, Lokesh Gidra has said that "As part of using userfaultfd for GC, We'll have to move the physical pages of the java heap to a separate location. For this purpose mremap will be used. Without the
MREMAP_DONTUNMAPflag, when I mremap the java heap, its virtual mapping will be removed as well. Therefore, we'll require performing mmap immediately after. This is not only time consuming but also opens a time window where a native thread may call mmap and reserve the java heap's address range for its own usage. This flag solves the problem."v6 -> v7:
- Don't allow resizing VMA as part ofMREMAP_DONTUNMAP. There is no clear use case at the moment and it can be added later as it simplifies the implementation for now.v5 -> v6:
- Code cleanup suggested by Kirill.
Quoted 7 lines
v4 -> v5:
- Correct commit message to more accurately reflect the behavior.
- ClearVM_LOCKEDandVM_LOCKEDONFAULTon the old vma.Signed-off-by Brian Geffon <[email protected]>Reviewed-by Minchan Kim <[email protected]>Tested-by Lokesh Gidra <[email protected]>
Thanks.
mm/mremap.c unsigned long move_page_tables(struct vm_area_struct *vma, · line 322
Quoted 12 lines
mm/mremap.c +2
-2@@ -318,8 +318,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma,static unsigned long move_vma (struct vm_area_struct *vma,unsigned long old_addr,unsigned long old_len,unsigned long new_len,unsigned long new_addr, -bool *locked,struct vm_userfaultfd_ctx *uf, -struct list_head *uf_unmap) +bool *locked,unsigned long flags, +struct vm_userfaultfd_ctx *uf,struct list_head *uf_unmap)
The usage of MREMAP_DONTUNMAP directly in the "flags" parameter seems weird for generically named vma manipulation functions, but as they are all local to mremap.c then it's probably fine.
Re: [PATCH v7] mremap.2: Add information for MREMAP_DONTUNMAP.
In reply to Brian GeffonQuoted 7 lines · Brian Geffon
man2/mremap.2 +3
-0@@ -209,10 +227,20 @@ flag was not specified. .RE .TP .B ENOMEM +Not enough memory was available to complete the operation. +Possible causes are: +.RS
man2/mremap.2 flag was not specified. · line 242
Quoted 10 lines
man2/mremap.2 +6
-0+.IP * 3 The memory area cannot be expanded at the current virtual address, and the .B MREMAP_MAYMOVE flag is not set in \fIflags\fP. Or, there is not enough (virtual) memory available. +.IP * +.B MREMAP_DONTUNMAP +was used without +.B MREMAP_FIXED +causing a new mapping to be created that would exceed the virtual memory available or it would exceed the maximum number of allowed mappings.
So this can also result with MREMAP_FIXED, no?
Quoted 5 lines
man2/mremap.2 +1
-0+.RE .SH CONFORMING TO This call is Linux-specific, and should not be used in programs intended to be portable.
Re: [PATCH v7] mremap.2: Add information for MREMAP_DONTUNMAP.
Yes, you're correct that is poorly worded. I'll send an updated manpage closer to the release of this in case anything else changes. Thanks for taking a look.
Brian
Quoted 28 lines · Vlastimil Babka
Quoted 7 lines · Brian Geffon
man2/mremap.2 +3
-0@@ -209,10 +227,20 @@ flag was not specified. .RE .TP .B ENOMEM +Not enough memory was available to complete the operation. +Possible causes are: +.RSman2/mremap.2 flag was not specified. · line 242
Quoted 10 lines
man2/mremap.2 +6
-0+.IP * 3 The memory area cannot be expanded at the current virtual address, and the .B MREMAP_MAYMOVE flag is not set in \fIflags\fP. Or, there is not enough (virtual) memory available. +.IP * +.B MREMAP_DONTUNMAP +was used without +.B MREMAP_FIXED +causing a new mapping to be created that would exceed the virtual memory available or it would exceed the maximum number of allowed mappings.So this can also result with
MREMAP_FIXED, no?Quoted 5 lines
man2/mremap.2 +1
-0+.RE .SH CONFORMING TO This call is Linux-specific, and should not be used in programs intended to be portable.
Re: [PATCH v7] mremap.2: Add information for MREMAP_DONTUNMAP.
In reply to Brian GeffonHello Brian,
I see that MREMAP_DONTUNMAP has been merged. Thanks for the patch below.
In addition to Vlastimil's comments, could you please take a look at my comments below.
Quoted 10 lines · Brian Geffon
Signed-off-by Brian Geffon <[email protected]>--- man2/mremap.2 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)man2/mremap.2 +0
-0
man2/mremap.2 line 30
Quoted 6 lines
man2/mremap.2 +2
-1@@ -26,7 +26,8 @@ .\" 1996-04-12 Tom Bjorkholm <[email protected]> .\" Update for Linux 1.3.87 and later .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text. -.\" +.\" 2020-02-05 Brian Geffon <[email protected]> +.\" Update for MREMAP_DONTUNMAP.
No need to add this piece. This info is maintained via the Git log these days.
man2/mremap.2 If · line 135
Quoted 10 lines
man2/mremap.2 +3
-0.TH MREMAP 2 2019-03-06 "Linux" "Linux Programmer's Manual" .SH NAME mremap \- remap a virtual memory address@@ -129,6 +130,13 @@ If is specified, then .B MREMAP_MAYMOVE must also be specified. +.TP +.BR MREMAP_DONTUNMAP " (since Linux 5.7)" +This flag which must be used in conjuction with
s/conjuction/conjunction/
man2/mremap.2 If · line 137
Quoted 2 lines
man2/mremap.2 +2
-0+.B MREMAP_MAYMOVE +remaps a mapping to a new address and it does not unmap the mapping at \fIold_address\fP. This flag can only be used with private anonymous mappings. Any access to the range specified at \fIold_address\fP after completion will result in a page fault. If a
Please wrap source lines to no more than about 75 columns. Also, always start new sentences on new lines ("Semantic newlines").
As a general rule, I prefer formatting to be done like this:
.BR old_address .
rather than:
\fIold_address\fP.
(Yes, I know there's plenty of existing text that goes the other way, but I try to avoid the \fX...\fP style for new text.
Re the "Any access to the range ... will result in a page fault", I think it would be helpful to be more explicit. I presume that if we access the range at old_address the mapping is repopulated with zero-filled pages, right? It would be good to note that explicitly,
man2/mremap.2 If · line 139
Quoted 2 lines
man2/mremap.2 +2
-0+.BR userfaultfd (2) +was registered on the mapping specified by \fIold_address\fP it will continue to watch that mapping for faults.
(See comments above re wrapping and formatting.)
Perhaps it would be nice to have a short paragraph on use cases?
Quoted 37 lines
man2/mremap.2 +13
-0.PP If the memory segment specified by .I old_address@@ -176,6 +184,8 @@ a value other than .B MREMAP_MAYMOVE or .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified in .IR flags ; .IP *@@ -197,9 +207,17 @@ and .IR old_size ; .IP * .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified without also specifying .BR MREMAP_MAYMOVE ; .IP * +.B MREMAP_DONTUNMAP +was specified with an \fIold_address\fP that was not private anonymous; +.IP * +.B MREMAP_DONTUNMAP +was specified and \fIold_size\fP was not equal to \fInew_size\fP; +.IP * \fIold_size\fP was zero and \fIold_address\fP does not refer to a shareable mapping (but see BUGS); .IP *@@ -209,10 +227,20 @@ flag was not specified. .RE .TP .B ENOMEM +Not enough memory was available to complete the operation. +Possible causes are: +.RS
man2/mremap.2 flag was not specified. · line 242
Quoted 10 lines
man2/mremap.2 +6
-0+.IP * 3 The memory area cannot be expanded at the current virtual address, and the .B MREMAP_MAYMOVE flag is not set in \fIflags\fP. Or, there is not enough (virtual) memory available. +.IP * +.B MREMAP_DONTUNMAP +was used without +.B MREMAP_FIXED +causing a new mapping to be created that would exceed the virtual memory available or it would exceed the maximum number of allowed mappings.
(See comments above re wrapping.)
man2/mremap.2 flag was not specified. · line 246
Quoted 4 lines
man2/mremap.2 +1
-0+.RE .SH CONFORMING TO This call is Linux-specific, and should not be used in programs intended to be portable.
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH v7] mremap.2: Add information for MREMAP_DONTUNMAP.
Hi Michael,
I'll make those changes and start a new thread.
Thanks for the feedback,
Brian
Quoted 137 lines · Michael Kerrisk (man-pages)
Hello Brian,
I see that
MREMAP_DONTUNMAPhas been merged. Thanks for the patch below.In addition to Vlastimil's comments, could you please take a look at my comments below.
Quoted 10 lines · Brian Geffon
Signed-off-by Brian Geffon <[email protected]>--- man2/mremap.2 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)man2/mremap.2 +0
-0man2/mremap.2 line 30
Quoted 6 lines
man2/mremap.2 +2
-1@@ -26,7 +26,8 @@ .\" 1996-04-12 Tom Bjorkholm <[email protected]> .\" Update for Linux 1.3.87 and later .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text. -.\" +.\" 2020-02-05 Brian Geffon <[email protected]> +.\" Update for MREMAP_DONTUNMAP.No need to add this piece. This info is maintained via the Git log these days.
man2/mremap.2 If · line 135
Quoted 10 lines
man2/mremap.2 +3
-0.TH MREMAP 2 2019-03-06 "Linux" "Linux Programmer's Manual" .SH NAME mremap \- remap a virtual memory address@@ -129,6 +130,13 @@ If is specified, then .B MREMAP_MAYMOVE must also be specified. +.TP +.BR MREMAP_DONTUNMAP " (since Linux 5.7)" +This flag which must be used in conjuction withs/conjuction/conjunction/
man2/mremap.2 If · line 137
Quoted 2 lines
man2/mremap.2 +2
-0+.B MREMAP_MAYMOVE +remaps a mapping to a new address and it does not unmap the mapping at \fIold_address\fP. This flag can only be used with private anonymous mappings. Any access to the range specified at \fIold_address\fP after completion will result in a page fault. If aPlease wrap source lines to no more than about 75 columns. Also, always start new sentences on new lines ("Semantic newlines").
As a general rule, I prefer formatting to be done like this:
.BR
old_address.rather than:
\fIold_address\fP.
(Yes, I know there's plenty of existing text that goes the other way, but I try to avoid the \fX...\fP style for new text.
Re the "Any access to the range ... will result in a page fault", I think it would be helpful to be more explicit. I presume that if we access the range at
old_addressthe mapping is repopulated with zero-filled pages, right? It would be good to note that explicitly,man2/mremap.2 If · line 139
Quoted 2 lines
man2/mremap.2 +2
-0+.BR userfaultfd (2) +was registered on the mapping specified by \fIold_address\fP it will continue to watch that mapping for faults.(See comments above re wrapping and formatting.)
Perhaps it would be nice to have a short paragraph on use cases?
Quoted 37 lines
man2/mremap.2 +13
-0.PP If the memory segment specified by .I old_address@@ -176,6 +184,8 @@ a value other than .B MREMAP_MAYMOVE or .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified in .IR flags ; .IP *@@ -197,9 +207,17 @@ and .IR old_size ; .IP * .B MREMAP_FIXED +or +.B MREMAP_DONTUNMAP was specified without also specifying .BR MREMAP_MAYMOVE ; .IP * +.B MREMAP_DONTUNMAP +was specified with an \fIold_address\fP that was not private anonymous; +.IP * +.B MREMAP_DONTUNMAP +was specified and \fIold_size\fP was not equal to \fInew_size\fP; +.IP * \fIold_size\fP was zero and \fIold_address\fP does not refer to a shareable mapping (but see BUGS); .IP *@@ -209,10 +227,20 @@ flag was not specified. .RE .TP .B ENOMEM +Not enough memory was available to complete the operation. +Possible causes are: +.RSman2/mremap.2 flag was not specified. · line 242
Quoted 10 lines
man2/mremap.2 +6
-0+.IP * 3 The memory area cannot be expanded at the current virtual address, and the .B MREMAP_MAYMOVE flag is not set in \fIflags\fP. Or, there is not enough (virtual) memory available. +.IP * +.B MREMAP_DONTUNMAP +was used without +.B MREMAP_FIXED +causing a new mapping to be created that would exceed the virtual memory available or it would exceed the maximum number of allowed mappings.(See comments above re wrapping.)
man2/mremap.2 flag was not specified. · line 246
Quoted 4 lines
man2/mremap.2 +1
-0+.RE .SH CONFORMING TO This call is Linux-specific, and should not be used in programs intended to be portable.Thanks,
Michael
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/