Skip to contentSkip to conversation

man/man2/mremap.2: describe multiple mapping move, shrink

PATCH Superseded Lorenzo Stoakes 2 participants 12 messages Aug 5, 2025 – Aug 11, 2025

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover man/man2/mremap.2: describe multiple mapping move, shrink

We have added new functionality to mremap() in Linux 6.17, permitting the move of multiple VMAs when performing a move alone (that is - providing MREMAP_MAYMOVE | MREMAP_FIXED flags and specifying old_size == new_size).

We document this new feature.

Additionally, we document previously undocumented behaviour around shrinking of input VMA ranges which permits the input range to span multiple VMAs.

v4:
* Update description of newly discovered mremap() behaviour to highlight the fact that, if in-place, [old_address, old_address + new_length) may span multiple VMAs also.
* Fix up commit message for 2/2 to correct typo on specified range.
* Added code sample to 1/2 as per Alejandro.

v3:
* Use more precise language around mremap() move description as per Jann.
* Fix some typos in commit messages.
https://lore.kernel.org/all/[email protected]/

v2:
* Split out the two man page changes as requested by Alejandro.
https://lore.kernel.org/all/[email protected]/

v1:
https://lore.kernel.org/all/[email protected]/

Lorenzo Stoakes (2):
man/man2/mremap.2: describe multiple mapping move man/man2/mremap.2: describe previously undocumented shrink behaviour

 man/man2/mremap.2 | 111 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 100 insertions(+), 11 deletions(-)

--
2.50.1

#1 lore raw

PATCH v4 1/2 man/man2/mremap.2: describe multiple mapping move

Document the new behaviour introduced in Linux 6.17 whereby it is now possible to move multiple mappings in a single operation, as long as the operation is purely a move, that is old_size is equal to new_size and MREMAP_FIXED is specified.

To make things clearer, also describe this 'pure move' operation, before expanding upon it to describe the newly introduced behaviour.

This change also explains the limitations of of this method and the possibility of partial failure.

Finally, we pluralise language where it makes sense to so the documentation does not contradict either this new capability nor the pre-existing edge case.

Example code is enclosed below demonstrating the behaviour which is now possible:

int main(void)

{
	unsigned long page_size = sysconf(_SC_PAGESIZE);
	void *ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
			 MAP_ANON | MAP_PRIVATE, -1, 0);
	void *tgt_ptr = mmap(NULL, 10 * page_size, PROT_NONE,
			     MAP_ANON | MAP_PRIVATE, -1, 0);
	int i;

	if (ptr == MAP_FAILED || tgt_ptr == MAP_FAILED) {
		perror("mmap");
		return EXIT_FAILURE;
	}

	/* Unmap every other page. */
	for (i = 1; i < 10; i += 2)
		munmap(ptr + i * page_size, page_size);

	/* Now move all 5 distinct mappings to tgt_ptr. */
	ptr = mremap(ptr, 10 * page_size, 10 * page_size,
		     MREMAP_MAYMOVE | MREMAP_FIXED, tgt_ptr);
	if (ptr == MAP_FAILED) {
		perror("mremap");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
---
 man/man2/mremap.2 | 84 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 11 deletions(-)
man/man2/mremap.2 +73 -11
index 2168ca728..6ba51310c 100644
@@ -25,18 +25,47 @@ moving it at the same time (controlled by the
 argument and
 the available virtual address space).
 .P
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,
+.I old_size
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.
+The
+.B MREMAP_DONTUNMAP
+flag may be specified.
+.P
+If the operation is not
+simply moving mappings,
+then
+.I old_size
+must span only a single mapping.
+.P
 .I old_address
-is the old address of the virtual memory block that you
-want to expand (or shrink).
+is the old address of the first virtual memory block that you
+want to expand, shrink, and/or move.
 Note that
 .I old_address
 has to be page aligned.
 .I old_size
-is the old size of the
-virtual memory block.
+is the size of the range containing
+virtual memory blocks to be manipulated.
 .I new_size
 is the requested size of the
-virtual memory block after the resize.
+virtual memory blocks after the resize.
 An optional fifth argument,
 .IR new_address ,
 may be provided; see the description of
@@ -105,13 +134,43 @@ If
 is specified, then
 .B MREMAP_MAYMOVE
 must also be specified.
+.IP
+Since Linux 6.17,
+if
+.I old_size
+is equal to
+.I new_size
+and
+.B MREMAP_FIXED
+is specified, then
+.I old_size
+may span beyond the mapping in which
+.I old_address
+resides.
+In this case,
+gaps between mappings in the original range
+are maintained in the new range.
+The whole operation is performed atomically
+unless an error arises,
+in which case the operation may be partially
+completed,
+that is,
+some mappings may be moved and others not.
+.IP
+
+Moving multiple mappings is not permitted if
+any of those mappings have either
+been registered with
+.BR userfaultfd (2) ,
+or map drivers that
+specify their own custom address mapping logic.
 .TP
 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
 This flag, which must be used in conjunction with
 .BR MREMAP_MAYMOVE ,
-remaps a mapping to a new address but does not unmap the mapping at
-.IR old_address .
+remaps mappings to a new address but does not unmap them
+from their original address.
 .IP
 The
 .B MREMAP_DONTUNMAP
@@ -149,13 +208,13 @@ mapped.
 See NOTES for some possible applications of
 .BR MREMAP_DONTUNMAP .
 .P
-If the memory segment specified by
+If the memory segments specified by
 .I old_address
 and
 .I old_size
-is locked (using
+are locked (using
 .BR mlock (2)
-or similar), then this lock is maintained when the segment is
+or similar), then this lock is maintained when the segments are
 resized and/or relocated.
 As a consequence, the amount of memory locked by the process may change.
 .SH RETURN VALUE
@@ -188,7 +247,10 @@ virtual memory address for this process.
 You can also get
 .B EFAULT
 even if there exist mappings that cover the
-whole address space requested, but those mappings are of different types.
+whole address space requested, but those mappings are of different types,
+and the
+.BR mremap ()
+operation being performed does not support this.
 .TP
 .B EINVAL
 An invalid argument was given.
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v4 1/2] man/man2/mremap.2: describe multiple mapping move

Hi Lorenzo,

Quoted 7 lines · Lorenzo Stoakes

Document the new behaviour introduced in Linux 6.17 whereby it is now possible to move multiple mappings in a single operation, as long as the operation is purely a move, that is old_size is equal to new_size and MREMAP_FIXED is specified.

To make things clearer, also describe this 'pure move' operation, before expanding upon it to describe the newly introduced behaviour.

Can we split this further into two commits?

Quoted 57 lines

This change also explains the limitations of of this method and the possibility of partial failure.

Finally, we pluralise language where it makes sense to so the documentation does not contradict either this new capability nor the pre-existing edge case.

Example code is enclosed below demonstrating the behaviour which is now possible:

int main(void)

{
	unsigned long page_size = sysconf(_SC_PAGESIZE);
	void *ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
			 MAP_ANON | MAP_PRIVATE, -1, 0);
	void *tgt_ptr = mmap(NULL, 10 * page_size, PROT_NONE,
			     MAP_ANON | MAP_PRIVATE, -1, 0);
	int i;

	if (ptr == MAP_FAILED || tgt_ptr == MAP_FAILED) {
		perror("mmap");
		return EXIT_FAILURE;
	}

	/* Unmap every other page. */
	for (i = 1; i < 10; i += 2)
		munmap(ptr + i * page_size, page_size);

	/* Now move all 5 distinct mappings to tgt_ptr. */
	ptr = mremap(ptr, 10 * page_size, 10 * page_size,
		     MREMAP_MAYMOVE | MREMAP_FIXED, tgt_ptr);
	if (ptr == MAP_FAILED) {
		perror("mremap");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
---
 man/man2/mremap.2 | 84 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 11 deletions(-)
man/man2/mremap.2 +4 -0
index 2168ca728..6ba51310c 100644
@@ -25,18 +25,47 @@ moving it at the same time (controlled by the
 argument and
 the available virtual address space).
 .P
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size

man/man2/mremap.2 moving it at the same time (controlled by the · line 41

Quoted 10 lines
man/man2/mremap.2 +10 -0
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,

I don't understand this. What does it mean that old_address must reside within a mapping? It's a point, not a size, so I'm not sure I understand it.

Quoted 53 lines
man/man2/mremap.2 +34 -5
+.I old_size
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.
+The
+.B MREMAP_DONTUNMAP
+flag may be specified.
+.P
+If the operation is not
+simply moving mappings,
+then
+.I old_size
+must span only a single mapping.
+.P
 .I old_address
-is the old address of the virtual memory block that you
-want to expand (or shrink).
+is the old address of the first virtual memory block that you
+want to expand, shrink, and/or move.
 Note that
 .I old_address
 has to be page aligned.
 .I old_size
-is the old size of the
-virtual memory block.
+is the size of the range containing
+virtual memory blocks to be manipulated.
 .I new_size
 is the requested size of the
-virtual memory block after the resize.
+virtual memory blocks after the resize.
 An optional fifth argument,
 .IR new_address ,
 may be provided; see the description of
@@ -105,13 +134,43 @@ If
 is specified, then
 .B MREMAP_MAYMOVE
 must also be specified.
+.IP
+Since Linux 6.17,
+if
+.I old_size
+is equal to
+.I new_size
+and
+.B MREMAP_FIXED
+is specified, then
+.I old_size
+may span beyond the mapping in which
+.I old_address
+resides.
+In this case,

man/man2/mremap.2 If · line 160

Quoted 10 lines
man/man2/mremap.2 +10 -0
+gaps between mappings in the original range
+are maintained in the new range.
+The whole operation is performed atomically
+unless an error arises,
+in which case the operation may be partially
+completed,
+that is,
+some mappings may be moved and others not.
+.IP
+

Why this blank?

Cheers,
Alex

Quoted 50 lines
man/man2/mremap.2 +15 -6
+Moving multiple mappings is not permitted if
+any of those mappings have either
+been registered with
+.BR userfaultfd (2) ,
+or map drivers that
+specify their own custom address mapping logic.
 .TP
 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
 This flag, which must be used in conjunction with
 .BR MREMAP_MAYMOVE ,
-remaps a mapping to a new address but does not unmap the mapping at
-.IR old_address .
+remaps mappings to a new address but does not unmap them
+from their original address.
 .IP
 The
 .B MREMAP_DONTUNMAP
@@ -149,13 +208,13 @@ mapped.
 See NOTES for some possible applications of
 .BR MREMAP_DONTUNMAP .
 .P
-If the memory segment specified by
+If the memory segments specified by
 .I old_address
 and
 .I old_size
-is locked (using
+are locked (using
 .BR mlock (2)
-or similar), then this lock is maintained when the segment is
+or similar), then this lock is maintained when the segments are
 resized and/or relocated.
 As a consequence, the amount of memory locked by the process may change.
 .SH RETURN VALUE
@@ -188,7 +247,10 @@ virtual memory address for this process.
 You can also get
 .B EFAULT
 even if there exist mappings that cover the
-whole address space requested, but those mappings are of different types.
+whole address space requested, but those mappings are of different types,
+and the
+.BR mremap ()
+operation being performed does not support this.
 .TP
 .B EINVAL
 An invalid argument was given.
Signature · 1 line
2.50.1
Signature · 1 line
<https://www.alejandro-colomar.es/>
#3 lore raw

Re: [PATCH v4 1/2] man/man2/mremap.2: describe multiple mapping move

Quoted 8 lines · Alejandro Colomar

Hi Lorenzo,

Quoted 5 lines · Lorenzo Stoakes

Document the new behaviour introduced in Linux 6.17 whereby it is now possible to move multiple mappings in a single operation, as long as the operation is purely a move, that is old_size is equal to new_size and MREMAP_FIXED is specified.

Quoted 4 lines
Quoted 2 lines

To make things clearer, also describe this 'pure move' operation, before expanding upon it to describe the newly introduced behaviour.

Can we split this further into two commits?

OK can do.

Quoted 64 lines
Quoted 64 lines

This change also explains the limitations of of this method and the possibility of partial failure.

Finally, we pluralise language where it makes sense to so the documentation does not contradict either this new capability nor the pre-existing edge case.

Example code is enclosed below demonstrating the behaviour which is now possible:

int main(void)

{
	unsigned long page_size = sysconf(_SC_PAGESIZE);
	void *ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
			 MAP_ANON | MAP_PRIVATE, -1, 0);
	void *tgt_ptr = mmap(NULL, 10 * page_size, PROT_NONE,
			     MAP_ANON | MAP_PRIVATE, -1, 0);
	int i;

	if (ptr == MAP_FAILED || tgt_ptr == MAP_FAILED) {
		perror("mmap");
		return EXIT_FAILURE;
	}

	/* Unmap every other page. */
	for (i = 1; i < 10; i += 2)
		munmap(ptr + i * page_size, page_size);

	/* Now move all 5 distinct mappings to tgt_ptr. */
	ptr = mremap(ptr, 10 * page_size, 10 * page_size,
		     MREMAP_MAYMOVE | MREMAP_FIXED, tgt_ptr);
	if (ptr == MAP_FAILED) {
		perror("mremap");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
---
 man/man2/mremap.2 | 84 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 11 deletions(-)
man/man2/mremap.2 +10 -0
index 2168ca728..6ba51310c 100644
@@ -25,18 +25,47 @@ moving it at the same time (controlled by the
 argument and
 the available virtual address space).
 .P
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
Quoted 8 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 41

Quoted 4 lines
man/man2/mremap.2 +4 -0
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,

I don't understand this. What does it mean that old_address must reside within a mapping? It's a point, not a size, so I'm not sure I understand it.

I think if it were a size it would be more confusing no?

It's an address, the address must be located within an existing memory mapping.

Will replace with 'located' for clarity.

Quoted 58 lines
Quoted 58 lines
man/man2/mremap.2 +38 -5
+.I old_size
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.
+The
+.B MREMAP_DONTUNMAP
+flag may be specified.
+.P
+If the operation is not
+simply moving mappings,
+then
+.I old_size
+must span only a single mapping.
+.P
 .I old_address
-is the old address of the virtual memory block that you
-want to expand (or shrink).
+is the old address of the first virtual memory block that you
+want to expand, shrink, and/or move.
 Note that
 .I old_address
 has to be page aligned.
 .I old_size
-is the old size of the
-virtual memory block.
+is the size of the range containing
+virtual memory blocks to be manipulated.
 .I new_size
 is the requested size of the
-virtual memory block after the resize.
+virtual memory blocks after the resize.
 An optional fifth argument,
 .IR new_address ,
 may be provided; see the description of
@@ -105,13 +134,43 @@ If
 is specified, then
 .B MREMAP_MAYMOVE
 must also be specified.
+.IP
+Since Linux 6.17,
+if
+.I old_size
+is equal to
+.I new_size
+and
+.B MREMAP_FIXED
+is specified, then
+.I old_size
+may span beyond the mapping in which
+.I old_address
+resides.
+In this case,
+gaps between mappings in the original range
+are maintained in the new range.
+The whole operation is performed atomically
+unless an error arises,
Quoted 8 lines

man/man2/mremap.2 If · line 160

Quoted 6 lines
man/man2/mremap.2 +6 -0
+in which case the operation may be partially
+completed,
+that is,
+some mappings may be moved and others not.
+.IP
+

Why this blank?

Oops that was a mistake, will fix on respin.

Quoted 58 lines

Cheers,
Alex

Quoted 48 lines
man/man2/mremap.2 +15 -7
+Moving multiple mappings is not permitted if
+any of those mappings have either
+been registered with
+.BR userfaultfd (2) ,
+or map drivers that
+specify their own custom address mapping logic.
 .TP
 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
 This flag, which must be used in conjunction with
 .BR MREMAP_MAYMOVE ,
-remaps a mapping to a new address but does not unmap the mapping at
-.IR old_address .
+remaps mappings to a new address but does not unmap them
+from their original address.
 .IP
 The
 .B MREMAP_DONTUNMAP
@@ -149,13 +208,13 @@ mapped.
 See NOTES for some possible applications of
 .BR MREMAP_DONTUNMAP .
 .P
-If the memory segment specified by
+If the memory segments specified by
 .I old_address
 and
 .I old_size
-is locked (using
+are locked (using
 .BR mlock (2)
-or similar), then this lock is maintained when the segment is
+or similar), then this lock is maintained when the segments are
 resized and/or relocated.
 As a consequence, the amount of memory locked by the process may change.
 .SH RETURN VALUE
@@ -188,7 +247,10 @@ virtual memory address for this process.
 You can also get
 .B EFAULT
 even if there exist mappings that cover the
-whole address space requested, but those mappings are of different types.
+whole address space requested, but those mappings are of different types,
+and the
+.BR mremap ()
+operation being performed does not support this.
 .TP
 .B EINVAL
 An invalid argument was given.
--
Quoted 2 lines

2.50.1

--
<https://www.alejandro-colomar.es/>

#4 lore raw

Re: [PATCH v4 1/2] man/man2/mremap.2: describe multiple mapping move

Hi Lorenzo,

Quoted 19 lines · Lorenzo Stoakes
Quoted 10 lines
Quoted 10 lines
man/man2/mremap.2 +10 -0
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
Quoted 8 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 41

Quoted 4 lines
man/man2/mremap.2 +4 -0
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,

I don't understand this. What does it mean that old_address must reside within a mapping? It's a point, not a size, so I'm not sure I understand it.

Quoted 3 lines

I think if it were a size it would be more confusing no?

It's an address, the address must be located within an existing memory mapping.

What I don't understand is: how could you not comply with that? Could you pass some old_address that is in two mappings? Being a single address, that would be impossible, right?

Quoted 8 lines

Will replace with 'located' for clarity.

Quoted 6 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 46

Quoted 6 lines

+.I old_size

man/man2/mremap.2 +4 -0
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.

Have a lovely day!
Alex

Signature · 1 line
<https://www.alejandro-colomar.es/>
#5 lore raw

Re: [PATCH v4 1/2] man/man2/mremap.2: describe multiple mapping move

Quoted 22 lines · Alejandro Colomar

Hi Lorenzo,

Quoted 19 lines · Lorenzo Stoakes
Quoted 18 lines
Quoted 4 lines
man/man2/mremap.2 +4 -0
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size

man/man2/mremap.2 moving it at the same time (controlled by the · line 41

Quoted 10 lines
man/man2/mremap.2 +10 -0
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,

I don't understand this. What does it mean that old_address must reside within a mapping? It's a point, not a size, so I'm not sure I understand it.

Quoted 7 lines
Quoted 3 lines

I think if it were a size it would be more confusing no?

It's an address, the address must be located within an existing memory mapping.

What I don't understand is: how could you not comply with that? Could you pass some old_address that is in two mappings? Being a single address, that would be impossible, right?

It can be in an unmapped area. It's either in an unmapped area or a mapped one.

I could simply reword this as 'old_address must be mapped'?

Quoted 16 lines
Quoted 9 lines

Will replace with 'located' for clarity.

Quoted 6 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 46

Quoted 6 lines

+.I old_size

man/man2/mremap.2 +4 -0
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.

Have a lovely day!
Alex

--
<https://www.alejandro-colomar.es/>

#6 lore raw

Re: [PATCH v4 1/2] man/man2/mremap.2: describe multiple mapping move

Quoted 27 lines · Lorenzo Stoakes
Quoted 26 lines · Alejandro Colomar

Hi Lorenzo,

Quoted 22 lines · Lorenzo Stoakes
Quoted 10 lines
Quoted 10 lines
man/man2/mremap.2 +10 -0
+Mappings can also simply be moved
+(without any resizing)
+by specifying equal
+.I old_size
+and
+.I new_size
+and using the
+.B MREMAP_FIXED
+flag
+(see below).
Quoted 8 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 41

Quoted 4 lines
man/man2/mremap.2 +4 -0
+Since Linux 6.17,
+while
+.I old_address
+must reside within a mapping,

I don't understand this. What does it mean that old_address must reside within a mapping? It's a point, not a size, so I'm not sure I understand it.

I think if it were a size it would be more confusing no?

It's an address, the address must be located within an existing memory mapping.

Quoted 7 lines
Quoted 3 lines

What I don't understand is: how could you not comply with that? Could you pass some old_address that is in two mappings? Being a single address, that would be impossible, right?

It can be in an unmapped area. It's either in an unmapped area or a mapped one.

I could simply reword this as 'old_address must be mapped'?

Yup, that seems better. Thanks!

Cheers,
Alex

Quoted 15 lines
Quoted 15 lines
Quoted 8 lines

Will replace with 'located' for clarity.

Quoted 6 lines

man/man2/mremap.2 moving it at the same time (controlled by the · line 46

Quoted 6 lines

+.I old_size

man/man2/mremap.2 +4 -0
+may span multiple mappings
+which do not have to be
+adjacent to one another when
+performing a move like this.

Have a lovely day!
Alex

--
<https://www.alejandro-colomar.es/>

Signature · 1 line
<https://www.alejandro-colomar.es/>
#7 lore raw

PATCH v4 2/2 man/man2/mremap.2: describe previously undocumented shrink behaviour

In reply to Lorenzo Stoakes

There is pre-existing logic that appears to be undocumented for an mremap() shrink operation, where it turns out that the usual 'input range must span a single mapping' requirement no longer applies.

In fact, it turns out that the input range specified by [old_address, old_address + old_size) may span any number of mappings.

If shrinking in-place (that is, neither the MREMAP_FIXED nor MREMAP_DONTUNMAP flags are specified), then the new span may also span any number of VMAs - [old_address, old_address + new_size).

If shrinking and moving, the range specified by [old_address, old_address + new_size) must span a single VMA.

There must be at least one VMA contained within the [old_address, old_address + old_size) range, and old_address must be within the range of a VMA.

Explicitly document this.

---
 man/man2/mremap.2 | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
man/man2/mremap.2 +29 -2
index 6ba51310c..631c835b8 100644
@@ -48,8 +48,35 @@ The
 .B MREMAP_DONTUNMAP
 flag may be specified.
 .P
-If the operation is not
-simply moving mappings,
+Equally, if the operation performs a shrink,
+that is if
+.I old_size
+is greater than
+.IR new_size ,
+then
+.I old_size
+may also span multiple mappings
+which do not have to be
+adjacent to one another.
+If this shrink is performed
+in-place,
+that is,
+neither
+.BR MREMAP_FIXED ,
+nor
+.B MREMAP_DONTUNMAP
+are specified,
+.I new_size
+may also span multiple VMAs.
+However, if the range is moved,
+then
+.I new_size
+must span only a single mapping.
+.P
+If the operation is neither a
+.B MREMAP_FIXED
+move
+nor a shrink,
 then
 .I old_size
 must span only a single mapping.
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH v4 2/2] man/man2/mremap.2: describe previously undocumented shrink behaviour

Hi Lorenzo,

Quoted 14 lines · Lorenzo Stoakes

There is pre-existing logic that appears to be undocumented for an mremap() shrink operation, where it turns out that the usual 'input range must span a single mapping' requirement no longer applies.

In fact, it turns out that the input range specified by [old_address, old_address + old_size) may span any number of mappings.

If shrinking in-place (that is, neither the MREMAP_FIXED nor MREMAP_DONTUNMAP flags are specified), then the new span may also span any number of VMAs - [old_address, old_address + new_size).

If shrinking and moving, the range specified by [old_address, old_address + new_size) must span a single VMA.

Quoted 7 lines

There must be at least one VMA contained within the [old_address, old_address + old_size) range, and old_address must be within the range of a VMA.

Explicitly document this.

Since this is documenting old behavior, could we have this patch before the patch documenting new behavior? Or do you prefer it in this order?

Cheers,
Alex

Quoted 49 lines
---
 man/man2/mremap.2 | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
man/man2/mremap.2 +29 -2
index 6ba51310c..631c835b8 100644
@@ -48,8 +48,35 @@ The
 .B MREMAP_DONTUNMAP
 flag may be specified.
 .P
-If the operation is not
-simply moving mappings,
+Equally, if the operation performs a shrink,
+that is if
+.I old_size
+is greater than
+.IR new_size ,
+then
+.I old_size
+may also span multiple mappings
+which do not have to be
+adjacent to one another.
+If this shrink is performed
+in-place,
+that is,
+neither
+.BR MREMAP_FIXED ,
+nor
+.B MREMAP_DONTUNMAP
+are specified,
+.I new_size
+may also span multiple VMAs.
+However, if the range is moved,
+then
+.I new_size
+must span only a single mapping.
+.P
+If the operation is neither a
+.B MREMAP_FIXED
+move
+nor a shrink,
 then
 .I old_size
 must span only a single mapping.
Signature · 1 line
2.50.1
Signature · 1 line
<https://www.alejandro-colomar.es/>
#9 lore raw

Re: [PATCH v4 2/2] man/man2/mremap.2: describe previously undocumented shrink behaviour

Quoted 21 lines · Alejandro Colomar

Hi Lorenzo,

Quoted 18 lines · Lorenzo Stoakes

There is pre-existing logic that appears to be undocumented for an mremap() shrink operation, where it turns out that the usual 'input range must span a single mapping' requirement no longer applies.

In fact, it turns out that the input range specified by [old_address, old_address + old_size) may span any number of mappings.

If shrinking in-place (that is, neither the MREMAP_FIXED nor MREMAP_DONTUNMAP flags are specified), then the new span may also span any number of VMAs - [old_address, old_address + new_size).

If shrinking and moving, the range specified by [old_address, old_address + new_size) must span a single VMA.

There must be at least one VMA contained within the [old_address, old_address + old_size) range, and old_address must be within the range of a VMA.

Quoted 6 lines
Quoted 3 lines

Explicitly document this.

Since this is documenting old behavior, could we have this patch before the patch documenting new behavior? Or do you prefer it in this order?

I think it's fine in this order, it's more convenient for me as it'd be a pain to re-order otherwise, and we've waited ~20 years (or longer?) to document this so a delay in ordering is probably fine :P

Cheers, Lorenzo

Quoted 57 lines

Cheers,
Alex

Quoted 47 lines
---
 man/man2/mremap.2 | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
man/man2/mremap.2 +29 -3
index 6ba51310c..631c835b8 100644
@@ -48,8 +48,35 @@ The
 .B MREMAP_DONTUNMAP
 flag may be specified.
 .P
-If the operation is not
-simply moving mappings,
+Equally, if the operation performs a shrink,
+that is if
+.I old_size
+is greater than
+.IR new_size ,
+then
+.I old_size
+may also span multiple mappings
+which do not have to be
+adjacent to one another.
+If this shrink is performed
+in-place,
+that is,
+neither
+.BR MREMAP_FIXED ,
+nor
+.B MREMAP_DONTUNMAP
+are specified,
+.I new_size
+may also span multiple VMAs.
+However, if the range is moved,
+then
+.I new_size
+must span only a single mapping.
+.P
+If the operation is neither a
+.B MREMAP_FIXED
+move
+nor a shrink,
 then
 .I old_size
 must span only a single mapping.
--
Quoted 2 lines

2.50.1

--
<https://www.alejandro-colomar.es/>

#10 lore raw

Re: [PATCH v4 2/2] man/man2/mremap.2: describe previously undocumented shrink behaviour

Quoted 8 lines · Lorenzo Stoakes
Quoted 2 lines

Since this is documenting old behavior, could we have this patch before the patch documenting new behavior? Or do you prefer it in this order?

I think it's fine in this order, it's more convenient for me as it'd be a pain to re-order otherwise, and we've waited ~20 years (or longer?) to document this so a delay in ordering is probably fine :P

Cheers, Lorenzo

Okay, that's fine. Thanks!

Cheers,
Alex

Signature · 1 line
<https://www.alejandro-colomar.es/>
#11 lore raw
In reply to Lorenzo Stoakes
Quoted 33 lines · Lorenzo Stoakes

We have added new functionality to mremap() in Linux 6.17, permitting the move of multiple VMAs when performing a move alone (that is - providing MREMAP_MAYMOVE | MREMAP_FIXED flags and specifying old_size == new_size).

We document this new feature.

Additionally, we document previously undocumented behaviour around shrinking of input VMA ranges which permits the input range to span multiple VMAs.

v4:
* Update description of newly discovered mremap() behaviour to highlight the fact that, if in-place, [old_address, old_address + new_length) may span multiple VMAs also.
* Fix up commit message for 2/2 to correct typo on specified range.
* Added code sample to 1/2 as per Alejandro.

v3:
* Use more precise language around mremap() move description as per Jann.
* Fix some typos in commit messages.
https://lore.kernel.org/all/[email protected]/

v2:
* Split out the two man page changes as requested by Alejandro.
https://lore.kernel.org/all/[email protected]/

v1:
https://lore.kernel.org/all/[email protected]/

Lorenzo Stoakes (2):
man/man2/mremap.2: describe multiple mapping move man/man2/mremap.2: describe previously undocumented shrink behaviour

Quoted 5 lines
 man/man2/mremap.2 | 111 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 100 insertions(+), 11 deletions(-)

--
2.50.1

Hey Alejandro,

Just wondering if this has everything you need, let me know if there's anything I need to do here!

Cheers, Lorenzo