1 .\" Copyright (c) 1996 Tom Bjorkholm <tomb@mydata.se>
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\" 1996-04-11 Tom Bjorkholm <tomb@mydata.se>
6 .\" First version written (1.3.86)
7 .\" 1996-04-12 Tom Bjorkholm <tomb@mydata.se>
8 .\" Update for Linux 1.3.87 and later
9 .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text.
11 .TH MREMAP 2 2021-03-22 "Linux man-pages (unreleased)"
13 mremap \- remap a virtual memory address
16 .RI ( libc ", " \-lc )
19 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
20 .B #include <sys/mman.h>
22 .BI "void *mremap(void *" old_address ", size_t " old_size ,
23 .BI " size_t " new_size ", int " flags ", ... /* void *" new_address " */);"
27 expands (or shrinks) an existing memory mapping, potentially
28 moving it at the same time (controlled by the \fIflags\fP argument and
29 the available virtual address space).
31 \fIold_address\fP is the old address of the virtual memory block that you
32 want to expand (or shrink).
33 Note that \fIold_address\fP has to be page
35 \fIold_size\fP is the old size of the
37 \fInew_size\fP is the requested size of the
38 virtual memory block after the resize.
39 An optional fifth argument,
41 may be provided; see the description of
45 If the value of \fIold_size\fP is zero, and \fIold_address\fP refers to
46 a shareable mapping (see
51 will create a new mapping of the same pages.
53 will be the size of the new mapping and the location of the new mapping
54 may be specified with \fInew_address\fP; see the description of
57 If a new mapping is requested via this method, then the
59 flag must also be specified.
61 The \fIflags\fP bit-mask argument may be 0, or include the following flags:
64 By default, if there is not sufficient space to expand a mapping
65 at its current location, then
68 If this flag is specified, then the kernel is permitted to
69 relocate the mapping to a new virtual address, if necessary.
70 If the mapping is relocated,
71 then absolute pointers into the old mapping location
72 become invalid (offsets relative to the starting address of
73 the mapping should be employed).
75 .BR MREMAP_FIXED " (since Linux 2.3.31)"
76 This flag serves a similar purpose to the
80 If this flag is specified, then
82 accepts a fifth argument,
83 .IR "void\ *new_address" ,
84 which specifies a page-aligned address to which the mapping must
86 Any previous mapping at the address range specified by
96 must also be specified.
98 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
99 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
100 This flag, which must be used in conjunction with
102 remaps a mapping to a new address but does not unmap the mapping at
107 flag can be used only with private anonymous mappings
108 (see the description of
116 any access to the range specified by
120 will result in a page fault.
121 The page fault will be handled by a
124 if the address is in a range previously registered with
126 Otherwise, the kernel allocates a zero-filled page to handle the fault.
130 flag may be used to atomically move a mapping while leaving the source
132 See NOTES for some possible applications of
133 .BR MREMAP_DONTUNMAP .
135 If the memory segment specified by
141 or similar), then this lock is maintained when the segment is
142 resized and/or relocated.
143 As a consequence, the amount of memory locked by the process may change.
147 returns a pointer to the new virtual memory area.
150 (that is, \fI(void\ *)\ \-1\fP) is returned,
151 and \fIerrno\fP is set to indicate the error.
155 The caller tried to expand a memory segment that is locked,
156 but this was not possible without exceeding the
161 Some address in the range
162 \fIold_address\fP to \fIold_address\fP+\fIold_size\fP is an invalid
163 virtual memory address for this process.
166 even if there exist mappings that cover the
167 whole address space requested, but those mappings are of different types.
170 An invalid argument was given.
174 \fIold_address\fP was not
194 the new address range specified by
198 overlapped the old address range specified by
206 was specified without also specifying
210 was specified, but one or more pages in the range specified by
214 were not private anonymous;
222 \fIold_size\fP was zero and \fIold_address\fP does not refer to a
223 shareable mapping (but see BUGS);
225 \fIold_size\fP was zero and the
227 flag was not specified.
231 Not enough memory was available to complete the operation.
235 The memory area cannot be expanded at the current virtual address, and the
237 flag is not set in \fIflags\fP.
238 Or, there is not enough (virtual) memory available.
241 was used causing a new mapping to be created that would exceed the
242 (virtual) memory available.
243 Or, it would exceed the maximum number of allowed mappings.
246 This call is Linux-specific, and should not be used in programs
247 intended to be portable.
248 .\" 4.2BSD had a (never actually implemented)
250 .\" call with completely different semantics.
254 mapping between virtual addresses and memory pages.
255 This can be used to implement a very efficient
258 In Linux, memory is divided into pages.
259 A process has (one or)
260 several linear virtual memory segments.
261 Each virtual memory segment has one
262 or more mappings to real memory pages (in the page table).
263 Each virtual memory segment has its own
264 protection (access rights), which may cause
265 a segmentation violation
267 if the memory is accessed incorrectly (e.g.,
268 writing to a read-only segment).
269 Accessing virtual memory outside of the
270 segments will also cause a segmentation violation.
274 is used to move or expand an area locked with
278 call will make a best effort to populate the new area but will not fail
281 if the area cannot be populated.
283 Prior to version 2.4, glibc did not expose the definition of
285 and the prototype for
287 did not allow for the
291 .SS MREMAP_DONTUNMAP use cases
292 Possible applications for
298 an application can yank out a virtual address range using
302 handler to handle the page faults that subsequently occur
303 as other threads in the process touch pages in the yanked range.
307 can be used in conjunction with
309 to implement garbage collection algorithms (e.g., in a Java virtual machine).
310 Such an implementation can be cheaper (and simpler)
311 than conventional garbage collection techniques that involve
312 marking pages with protection
314 in conjunction with the use of a
316 handler to catch accesses to those pages.
321 was zero and the mapping referred to by
323 was a private mapping
324 .RB ( mmap "(2) " MAP_PRIVATE ),
326 created a new private mapping unrelated to the original mapping.
327 This behavior was unintended
328 and probably unexpected in user-space applications
329 (since the intention of
331 is to create a new mapping based on the original mapping).
333 .\" commit dba58d3b8c5045ad89c1c95d33d01451e3964db7
348 Your favorite text book on operating systems
349 for more information on paged memory
350 (e.g., \fIModern Operating Systems\fP by Andrew S.\& Tanenbaum,
351 \fIInside Linux\fP by Randolph Bentson,
352 \fIThe Design of the UNIX Operating System\fP by Maurice J.\& Bach)