1 .\" Copyright (c) 1996 Tom Bjorkholm <tomb@mydata.se>
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 .\" GNU General Public License for more details.
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
24 .\" 1996-04-11 Tom Bjorkholm <tomb@mydata.se>
25 .\" First version written (1.3.86)
26 .\" 1996-04-12 Tom Bjorkholm <tomb@mydata.se>
27 .\" Update for Linux 1.3.87 and later
28 .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text.
30 .TH MREMAP 2 2019-03-06 "Linux" "Linux Programmer's Manual"
32 mremap \- remap a virtual memory address
35 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
36 .B #include <sys/mman.h>
38 .BI "void *mremap(void *" old_address ", size_t " old_size ,
39 .BI " size_t " new_size ", int " flags ", ... /* void *" new_address " */);"
43 expands (or shrinks) an existing memory mapping, potentially
44 moving it at the same time (controlled by the \fIflags\fP argument and
45 the available virtual address space).
47 \fIold_address\fP is the old address of the virtual memory block that you
48 want to expand (or shrink).
49 Note that \fIold_address\fP has to be page
51 \fIold_size\fP is the old size of the
53 \fInew_size\fP is the requested size of the
54 virtual memory block after the resize.
55 An optional fifth argument,
57 may be provided; see the description of
61 If the value of \fIold_size\fP is zero, and \fIold_address\fP refers to
62 a shareable mapping (see
67 will create a new mapping of the same pages.
69 will be the size of the new mapping and the location of the new mapping
70 may be specified with \fInew_address\fP; see the description of
73 If a new mapping is requested via this method, then the
75 flag must also be specified.
77 The \fIflags\fP bit-mask argument may be 0, or include the following flag:
80 By default, if there is not sufficient space to expand a mapping
81 at its current location, then
84 If this flag is specified, then the kernel is permitted to
85 relocate the mapping to a new virtual address, if necessary.
86 If the mapping is relocated,
87 then absolute pointers into the old mapping location
88 become invalid (offsets relative to the starting address of
89 the mapping should be employed).
91 .BR MREMAP_FIXED " (since Linux 2.3.31)"
92 This flag serves a similar purpose to the
96 If this flag is specified, then
98 accepts a fifth argument,
99 .IR "void\ *new_address" ,
100 which specifies a page-aligned address to which the mapping must
102 Any previous mapping at the address range specified by
112 must also be specified.
114 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
115 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
116 This flag, which must be used in conjunction with
118 remaps a mapping to a new address but does not unmap the mapping at
123 flag can be used only with private anonymous mappings
124 (see the description of
132 any access to the range specified by
136 will result in a page fault.
137 The page fault will be handled by a
140 if the address is in a range previously registered with
142 Otherwise, the kernel allocates a zero-filled page to handle the fault.
146 flag may be used to atomically move a mapping while leaving the source
148 Possible applications for this behavior might be garbage collection or
152 If the memory segment specified by
158 or similar), then this lock is maintained when the segment is
159 resized and/or relocated.
160 As a consequence, the amount of memory locked by the process may change.
164 returns a pointer to the new virtual memory area.
167 (that is, \fI(void\ *)\ \-1\fP) is returned,
168 and \fIerrno\fP is set appropriately.
172 The caller tried to expand a memory segment that is locked,
173 but this was not possible without exceeding the
178 Some address in the range
179 \fIold_address\fP to \fIold_address\fP+\fIold_size\fP is an invalid
180 virtual memory address for this process.
183 even if there exist mappings that cover the
184 whole address space requested, but those mappings are of different types.
187 An invalid argument was given.
191 \fIold_address\fP was not
211 the new address range specified by
215 overlapped the old address range specified by
223 was specified without also specifying
227 was specified, but one or more pages in the range specified by
231 were not private anonymous;
239 \fIold_size\fP was zero and \fIold_address\fP does not refer to a
240 shareable mapping (but see BUGS);
242 \fIold_size\fP was zero and the
244 flag was not specified.
248 Not enough memory was available to complete the operation.
252 The memory area cannot be expanded at the current virtual address, and the
254 flag is not set in \fIflags\fP.
255 Or, there is not enough (virtual) memory available.
258 was used causing a new mapping to be created that would exceed the
259 (virtual) memory available.
260 Or, it would exceed the maximum number of allowed mappings.
263 This call is Linux-specific, and should not be used in programs
264 intended to be portable.
265 .\" 4.2BSD had a (never actually implemented)
267 .\" call with completely different semantics.
272 mapping between virtual addresses and memory pages.
273 This can be used to implement a very efficient
276 In Linux, memory is divided into pages.
277 A process has (one or)
278 several linear virtual memory segments.
279 Each virtual memory segment has one
280 or more mappings to real memory pages (in the page table).
281 Each virtual memory segment has its own
282 protection (access rights), which may cause
283 a segmentation violation
285 if the memory is accessed incorrectly (e.g.,
286 writing to a read-only segment).
287 Accessing virtual memory outside of the
288 segments will also cause a segmentation violation.
292 is used to move or expand an area locked with
296 call will make a best effort to populate the new area but will not fail
299 if the area cannot be populated.
301 Prior to version 2.4, glibc did not expose the definition of
303 and the prototype for
305 did not allow for the
312 was zero and the mapping referred to by
314 was a private mapping
315 .RB ( mmap "(2) " MAP_PRIVATE ),
317 created a new private mapping unrelated to the original mapping.
318 This behavior was unintended
319 and probably unexpected in user-space applications
320 (since the intention of
322 is to create a new mapping based on the original mapping).
324 .\" commit dba58d3b8c5045ad89c1c95d33d01451e3964db7
339 Your favorite text book on operating systems
340 for more information on paged memory
341 (e.g., \fIModern Operating Systems\fP by Andrew S.\& Tanenbaum,
342 \fIInside Linux\fP by Randolf Bentson,
343 \fIThe Design of the UNIX Operating System\fP by Maurice J.\& Bach)